From 93c28f3430f4b7bf83498f768c33c6816da7a508 Mon Sep 17 00:00:00 2001 From: Scott Duensing Date: Sun, 7 Jan 2024 23:15:26 -0600 Subject: [PATCH] Cube performance much improved! --- examples/cube/build.sh | 2 +- examples/cube/cube.c | 102 +++++++++++++++++++---------------------- f256lib/math.c | 4 +- f256lib/text.c | 7 +++ f256lib/text.h | 1 + 5 files changed, 58 insertions(+), 58 deletions(-) diff --git a/examples/cube/build.sh b/examples/cube/build.sh index 60307e7..3583c40 100755 --- a/examples/cube/build.sh +++ b/examples/cube/build.sh @@ -32,7 +32,7 @@ START=0x2000 echo "__f256_start = ${START};" > ${SETTINGS} -CLANG="mos-f256k-clang -I${F256}/include -I${F256}/f256lib -Os" +CLANG="mos-f256k-clang -I${F256}/include -I${F256}/f256lib -O3" ${CLANG} -c ${F256}/f256lib/f256.c ${CLANG} -c cube.c diff --git a/examples/cube/cube.c b/examples/cube/cube.c index 9fd3d5b..f4abe19 100644 --- a/examples/cube/cube.c +++ b/examples/cube/cube.c @@ -27,23 +27,21 @@ #include "f256.h" -#define FIX_PREC 9 -#define TO_FIX(x) ((int32_t)((x)*(1<> FIX_PREC) -#define fix_sqr(a) (mathSignedMultiply(a, a) >> FIX_PREC) -//#define fix_div(a,b) mathSignedDivision((a << FIX_PREC), b, &_remainder) -#define fix_div(a,b) ((a << FIX_PREC) / b) +#define FIX_PREC 9 +#define TO_FIX(x) ((int32_t)((x)*(1<>FIX_PREC) // ((x)/(1<> FIX_PREC) +#define fix_sqr(am) (mathSignedMultiply(am, am) >> FIX_PREC) +#define fix_div(num,den) (mathSignedMultiply(mathUnSignedDivision(0x8000, (den >> 1), (uint16_t *)&_remainder), num) >> 7) // ((a << FIX_PREC) / b) #define SIN_SIZE 512 -#define COS_OFF 128 +#define COS_OFF 128 -int32_t SIN[] = { 0, 6, 12, 18, 25, 31, 37, 43, 50, 56, 62, 68, 75, 81, 87, 93, +int16_t SIN[] = { 0, 6, 12, 18, 25, 31, 37, 43, 50, 56, 62, 68, 75, 81, 87, 93, 99, 106, 112, 118, 124, 130, 136, 142, 148, 154, 160, 166, 172, 178, 184, 190, 195, 201, 207, 213, 218, 224, 230, 235, 241, 246, 252, 257, 263, 268, 273, 279, 284, 289, 294, 299, 304, 310, 314, @@ -104,34 +102,16 @@ typedef struct lineS { int16_t y2; } lineT; -int32_t *COS = SIN + COS_OFF; +int16_t *COS = SIN + COS_OFF; uint16_t width; uint16_t height; int32_t a = TO_FIX(4); int32_t scale = TO_FIX(40); -lineT past[24][2]; +lineT past[12][2]; void draw_cube(byte p, int16_t t) { - static const int16_t edges[] = { - 0, 1, 1, 3, 3, 2, 2, 0, - 1, 5, 0, 4, 2, 6, 3, 7, - 4, 5, 5, 7, 7, 6, 6, 4 - }; - static const int32_t cubeX[] = { - TO_FIX(-1), TO_FIX(-1), TO_FIX( 1), TO_FIX( 1), - TO_FIX(-1), TO_FIX(-1), TO_FIX( 1), TO_FIX( 1) - }; - static const int32_t cubeY[] = { - TO_FIX(-1), TO_FIX( 1), TO_FIX(-1), TO_FIX( 1), - TO_FIX(-1), TO_FIX( 1), TO_FIX(-1), TO_FIX( 1) - }; - static const int32_t cubeZ[] = { - TO_FIX(-1), TO_FIX(-1), TO_FIX(-1), TO_FIX(-1), - TO_FIX( 1), TO_FIX( 1), TO_FIX( 1), TO_FIX( 1) - }; - int32_t cubeRotX[8]; int32_t cubeRotY[8]; int32_t cubeRotZ[8]; @@ -141,12 +121,27 @@ void draw_cube(byte p, int16_t t) { int32_t cubeProjY[8]; int16_t i; byte l; - int16_t e1; - int16_t e2; - int16_t x1; - int16_t y1; - int16_t x2; - int16_t y2; + + static const int16_t edges[] = { + 0, 1, 1, 3, 3, 2, 2, 0, + 1, 5, 0, 4, 2, 6, 3, 7, + 4, 5, 5, 7, 7, 6, 6, 4 + }; + + static const int32_t cubeX[] = { + TO_FIX(-1), TO_FIX(-1), TO_FIX( 1), TO_FIX( 1), + TO_FIX(-1), TO_FIX(-1), TO_FIX( 1), TO_FIX( 1) + }; + + static const int32_t cubeY[] = { + TO_FIX(-1), TO_FIX( 1), TO_FIX(-1), TO_FIX( 1), + TO_FIX(-1), TO_FIX( 1), TO_FIX(-1), TO_FIX( 1) + }; + + static const int32_t cubeZ[] = { + TO_FIX(-1), TO_FIX(-1), TO_FIX(-1), TO_FIX(-1), + TO_FIX( 1), TO_FIX( 1), TO_FIX( 1), TO_FIX( 1) + }; for (i=0; i<8; ++i) { // Rotation around Y @@ -163,25 +158,22 @@ void draw_cube(byte p, int16_t t) { // Projection to screen space cubeProjX[i] = fix_div(fix_mul(a, cubeRotX[i]), cubeRotZ[i]); cubeProjY[i] = fix_div(fix_mul(a, cubeRotY[i]), cubeRotZ[i]); + cubeProjX[i] = TO_LONG(fix_mul(cubeProjX[i], scale)) + width; + cubeProjY[i] = TO_LONG(fix_mul(cubeProjY[i], scale)) + height; } - l = 0; - for (i=0; i<24; i += 2) { - e1 = edges[i]; - e2 = edges[i + 1]; - x1 = TO_LONG(fix_mul(cubeProjX[e1], scale)) + width; - y1 = TO_LONG(fix_mul(cubeProjY[e1], scale)) + height; - x2 = TO_LONG(fix_mul(cubeProjX[e2], scale)) + width; - y2 = TO_LONG(fix_mul(cubeProjY[e2], scale)) + height; - past[l][p].x1 = x1; - past[l][p].y1 = y1; - past[l][p].x2 = x2; - past[l][p].y2 = y2; - l++; - bitmapLine(x1, y1, x2, y2); - } + l = 0; + for (i=0; i<24; i += 2) { + past[l][p].x1 = cubeProjX[edges[i]]; + past[l][p].y1 = cubeProjY[edges[i]]; + past[l][p].x2 = cubeProjX[edges[i + 1]]; + past[l][p].y2 = cubeProjY[edges[i + 1]]; + bitmapLine(past[l][p].x1, past[l][p].y1, past[l][p].x2, past[l][p].y2); + l++; + } } + int main(void) { byte i; int16_t t = 0; @@ -189,6 +181,8 @@ int main(void) { f256Init(); + textSetCursor(0); // No cursor. + bitmapGetResolution(&width, &height); width = width >> 1; height = height >> 1; @@ -204,9 +198,7 @@ int main(void) { bitmapShowPage(1); } bitmapSetColor(0); - for (i=0; i<12; i++) { - bitmapLine(past[i][p].x1, past[i][p].y1, past[i][p].x2, past[i][p].y2); - } + for (i=0; i<12; i++) bitmapLine(past[i][p].x1, past[i][p].y1, past[i][p].x2, past[i][p].y2); bitmapSetColor(255); draw_cube(p, t); t += 2; diff --git a/f256lib/math.c b/f256lib/math.c index 3643a18..958c7c6 100644 --- a/f256lib/math.c +++ b/f256lib/math.c @@ -38,8 +38,8 @@ int16_t mathSignedDivision(int16_t a, int16_t b, int16_t *remainder) { b = -b; } - POKEW(DIVU_DEN_L, b); POKEW(DIVU_NUM_L, a); + POKEW(DIVU_DEN_L, b); r = PEEKW(QUOU_LL); *remainder = PEEKW(REMU_HL); @@ -74,8 +74,8 @@ int32_t mathSignedMultiply(int16_t a, int16_t b) { uint16_t mathUnSignedDivision(uint16_t a, uint16_t b, uint16_t *remainder) { - POKEW(DIVU_DEN_L, b); POKEW(DIVU_NUM_L, a); + POKEW(DIVU_DEN_L, b); *remainder = PEEKW(REMU_HL); return PEEKW(QUOU_LL); } diff --git a/f256lib/text.c b/f256lib/text.c index 6a90aea..512d74b 100644 --- a/f256lib/text.c +++ b/f256lib/text.c @@ -208,3 +208,10 @@ void textSetColor(byte f, byte b) { _bcolor = b; _ccolor = (f << 4) + b; } + + +void textSetCursor(byte c) { + // Hack + if (c == 0) c = 32; + POKE(VKY_CRSR_CHAR, c); // Set cursor shape. (199 = Checkerboard) +} diff --git a/f256lib/text.h b/f256lib/text.h index 3086875..f9a0a4a 100644 --- a/f256lib/text.h +++ b/f256lib/text.h @@ -66,6 +66,7 @@ void textPrint(char *message); void textPrintInt(int32_t value); void textReset(void); void textSetColor(byte f, byte b); +void textSetCursor(byte c); #ifdef __cplusplus