Skip to content

Instantly share code, notes, and snippets.

@a1k0n
Created September 2, 2020 20:23
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a1k0n/80f48aa8911fffd805316b8ba8f48e83 to your computer and use it in GitHub Desktop.
Save a1k0n/80f48aa8911fffd805316b8ba8f48e83 to your computer and use it in GitHub Desktop.
// the donut code with fixed-point arithmetic; no sines, cosines, square roots, or anything.
// a1k0n 2020
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define R(mul,shift,x,y) \
_=x; \
x -= mul*y>>shift; \
y += mul*_>>shift; \
_ = 3145728-x*x-y*y>>11; \
x = x*_>>10; \
y = y*_>>10;
char b[1760], z[1760];
void main() {
int sA=1024,cA=0,sB=1024,cB=0,_;
for (;;) {
memset(b, 32, 1760); // text buffer
memset(z, 127, 1760); // z buffer
int sj=0, cj=1024;
for (int j = 0; j < 90; j++) {
int si = 0, ci = 1024; // sine and cosine of angle i
for (int i = 0; i < 324; i++) {
int R1 = 1, R2 = 2048, K2 = 5120*1024;
int x0 = R1*cj + R2,
x1 = ci*x0 >> 10,
x2 = cA*sj >> 10,
x3 = si*x0 >> 10,
x4 = R1*x2 - (sA*x3 >> 10),
x5 = sA*sj >> 10,
x6 = K2 + R1*1024*x5 + cA*x3,
x7 = cj*si >> 10,
x = 40 + 30*(cB*x1 - sB*x4)/x6,
y = 12 + 15*(cB*x4 + sB*x1)/x6,
N = (-cA*x7 - cB*((-sA*x7>>10) + x2) - ci*(cj*sB >> 10) >> 10) - x5 >> 7;
int o = x + 80 * y;
char zz = (x6-K2)>>15;
if (22 > y && y > 0 && x > 0 && 80 > x && zz < z[o]) {
z[o] = zz;
b[o] = ".,-~:;=!*#$@"[N > 0 ? N : 0];
}
R(5, 8, ci, si) // rotate i
}
R(9, 7, cj, sj) // rotate j
}
for (int k = 0; 1761 > k; k++)
putchar(k % 80 ? b[k] : 10);
R(5, 7, cA, sA);
R(5, 8, cB, sB);
usleep(15000);
printf("\x1b[23A");
}
}
@a1k0n
Copy link
Author

a1k0n commented Sep 2, 2020

To run on the LiteX risc-v core, I had to change char to signed char for the two buffers, use putsnonl instead of printf and busy_wait(15) instead of usleep(15000). You will also need to set up an interrupt handler or it will crash on boot.

@cedyangks
Copy link

cedyangks commented Oct 13, 2021

sorry, might be dumb question here.

what operations is that _=x;? tried googling but no gd results there

was compiling it with clang... or that syntax works on GCC?

@a1k0n
Copy link
Author

a1k0n commented Oct 13, 2021

sorry, might be dumb question here.

what operations is that _=x;? tried googling but no gd results there

was compiling it with clang... or that syntax works on GCC?

There's an integer variable named _. So fundamentally it's the same operation as x=y; The #define was cribbed from the obfuscated version and it's still a bit obfuscated, haha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment