Skip to content

Commit

Permalink
added flex program that allows selection of fractional precision
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnlon committed Jul 27, 2022
1 parent 654f189 commit 10a4aca
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 16 deletions.
15 changes: 9 additions & 6 deletions integer.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ void prt(short c, short s) {
printf("c=%d dec/%02d trunc dec / %02x\n", c, 0xff & c, 0xff & c);
}

short s(short i) {
return i;
}

int main(int argc, char* argv[])
{
Expand All @@ -34,20 +37,20 @@ int main(int argc, char* argv[])
short px=0;
while (px < width*zoom) {
// $380 = 3.5 $240=2.25 $180=1.5 $300=3
short x0 = (((px*0x380)/zoom) >> 5) - 0x240;
short y0 = (((py*0x300)/zoom) / 22) - 0x180;
short x0 = s(s(s(px*0x380)/zoom) >> 5) - 0x240;
short y0 = s(s(s(py*0x300)/zoom) / 22) - 0x180;

short x=0;
short y=0;

short i=0;


short xSqr;
short ySqr;
while (i < iters) {
xSqr = (x * x) >> 8;
ySqr = (y * y) >> 8;
xSqr = s(x * x) >> 8;
ySqr = s(y * y) >> 8;


if (i == 1 && px == 1 && py == 0)
prt(x, xSqr);
Expand All @@ -57,7 +60,7 @@ int main(int argc, char* argv[])
}

short xt = xSqr - ySqr + x0;
y = (((x * y)>>8) * 2 ) + y0;
y = (s(s(x * y)>>8) * 2 ) + y0;
x=xt;

i = i + 1;
Expand Down
65 changes: 55 additions & 10 deletions integerFlex.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,27 @@ short toPrec(double f, int bitsPrecision) {
printf(" %3f -> %3d 0x%04x %c%c %c%c%c%c%c%c%c%c\n",f, ret, ret, BYTE_TO_BINARY(ret));
return ret;
}
short s(short i) {
return i;
}


int main(int argc, char* argv[])
{
short log=0;
short logFrom=27;

// chosen to match https://www.youtube.com/watch?v=DC5wi6iv9io
short width = 32; // basic width of a zx81
short height = 22; // basic width of a zx81
short zoom=1; // bigger with finer detail ie a smaller step size - leave at 1 for 32x22
int width = 32; // basic width of a zx81
int height = 22; // basic width of a zx81
int zoom=1; // bigger with finer detail ie a smaller step size - leave at 1 for 32x22

// params
short bitsPrecision = 6;
printf("DECS=%d\n", bitsPrecision);

short X1 = toPrec(3.5,bitsPrecision) / zoom;
short X2 = toPrec(2.25,bitsPrecision) ;

short Y1 = toPrec(3,bitsPrecision)/zoom ; // horiz pos
short Y2 = toPrec(1.5,bitsPrecision) ; // vert pos
short LIMIT = toPrec(4,bitsPrecision);
Expand All @@ -64,41 +70,80 @@ int main(int argc, char* argv[])
short px=0;
while (px < width*zoom) {

short x0 = (short)(((short)(px*X1)) / width) - X2;
short y0 = (short)(((short)(py*Y1)) / height) - Y2;
short x0 = s(s(px*X1) / width) - X2;
short y0 = s(s(py*Y1) / height) - Y2;

if (x0 +y0 != 0) {
// printf("px=%4x ", px); printf("py=%4x ", py); printf("x0=%4x ", x0); printf("y0=%4x\n", y0);
}

short x=0;
short y=0;

short i=0;

if (0 && log >= logFrom) {
printf("\n-------\n");
printf("x0=%4x\n", x0&0xffff);
printf("y0=%4x\n", y0&0xffff);
}

short xSqr;
short ySqr;
while (i < iters) {
xSqr = ((short)(x * x)) >> bitsPrecision;
ySqr = ((short)(y * y)) >> bitsPrecision;
xSqr = s(x * x) >> bitsPrecision;
ySqr = s(y * y) >> bitsPrecision;

if (0 && log >= logFrom) {
printf("x =%4x\n", x&0xffff);
printf("x2=%4x\n", (xSqr)&0xffff);
printf("y =%4x\n", y&0xffff);
printf("y2=%4x\n", (ySqr)&0xffff);
printf("xsq+ysq=%4x\n", (xSqr+ySqr)&0xffff);
printf("\n");
}


// breakout also if negative as this indicates overflow of the addition which is a fault
if ((xSqr + ySqr) > LIMIT || (xSqr+ySqr) < 0) {
break;
}

short xt = xSqr - ySqr + x0;
y = ((((short)(x * y)) >> bitsPrecision) * 2 ) + y0;
x=xt;

if (0 && log >= logFrom) {
printf("i =%4x\n", i);
printf("diff =%4x\n", (xSqr-ySqr)&0xffff);
printf("xt =%4x\n", (xt)&0xffff);

short xy = x*y;
printf("x*y =%4x * %4x\n", (x)&0xffff, (y)&0xffff);
printf("x*y =%4x\n", (xy)&0xffff);
printf("x*y>6 =%4x\n", (xy >> 6)&0xffff);
printf("x*y>6 << 1 =%4x\n", ((short)(xy >> 6) * 2)&0xffff);
printf("x*y>6<<1+y0 =%4x\n", (((short)((short)(xy >> 6)) << 2)+ y0)&0xffff);
}

y = s(s(s(x * y) >> bitsPrecision) * 2) + y0;
x=xt;
i = i + 1;

}
if (0 && log >= logFrom) {
printf("x=%4x ", x&0xffff);
printf("xsqr=%4x\n", xSqr&0xffff);
exit(1);
}


i = i - 1;


printf("%c", chr[i]);

px = px + 1;

log ++;
}

printf("\n");
Expand Down

0 comments on commit 10a4aca

Please sign in to comment.