# HG changeset patch # User John Tsiombikas # Date 1517229645 -7200 # Node ID dd8c9847bae999087125e5326266404c396fe7be # Parent d625ba001a62154cc99c0158ed9929787948d4ca cube diff -r d625ba001a62 -r dd8c9847bae9 src/ds3.c --- a/src/ds3.c Mon Jan 29 03:48:05 2018 +0200 +++ b/src/ds3.c Mon Jan 29 14:40:45 2018 +0200 @@ -194,12 +194,13 @@ int32_t dy = top - bottom; int32_t dz = zfar - znear; - m[0] = 0x2000000 / (dx << 8); - m[5] = 0x2000000 / (dy << 8); - m[10] = -0x2000000 / (dz << 8); - m[12] = -((right + left) << 8) / (dx << 8); - m[13] = -((top + bottom) << 8) / (dy << 8); - m[14] = -((zfar + znear) << 8) / (dz << 8); + m[0] = x16div(0x20000, dx); + m[5] = x16div(0x20000, dy); + m[10] = x16div(-0x20000, dz); + m[12] = x16div(-(right + left), dx); + m[13] = x16div(-(top + bottom), dy); + m[14] = x16div(-(zfar + znear), dz); + m[15] = 65536; ds3_mult_matrix(m); } @@ -214,9 +215,10 @@ m[0] = (int32_t)(2.0f / dx * 65536.0f); m[5] = (int32_t)(2.0f / dy * 65536.0f); m[10] = (int32_t)(-2.0f / dz * 65536.0f); - m[12] = (int32_t)(-(right + left) / dx); - m[13] = (int32_t)(-(top + bottom) / dy); - m[14] = (int32_t)(-(zfar + znear) / dz); + m[12] = (int32_t)(-(right + left) / dx * 65536.0f); + m[13] = (int32_t)(-(top + bottom) / dy * 65536.0f); + m[14] = (int32_t)(-(zfar + znear) / dz * 65536.0f); + m[15] = 65536; ds3_mult_matrix(m); } @@ -229,13 +231,13 @@ int32_t dy = top - bottom; int32_t dz = zfar - znear; - int32_t a = ((right + left) << 8) / (dx << 8); - int32_t b = ((top + bottom) << 8) / (dy << 8); - int32_t c = (-(zfar + znear) << 8) / (dz << 8); - int32_t d = -(((zfar >> 8) * znear) << 1) / (dz << 8); + int32_t a = x16div(right + left, dx); + int32_t b = x16div(top + bottom, dy); + int32_t c = x16div(-(zfar + znear), dz); + int32_t d = x16div(-((zfar >> 8) * znear) << 1, dz); - m[0] = (znear << 9) / (dx << 8); - m[5] = (znear << 9) / (dy << 8); + m[0] = x16div(znear << 1, dx); + m[5] = x16div(znear << 1, dy); m[8] = a; m[9] = b; m[10] = c; @@ -245,7 +247,7 @@ ds3_mult_matrix(m); } -void g3d_frustum(float left, float right, float bottom, float top, float nr, float fr) +void g3d_frustumf(float left, float right, float bottom, float top, float nr, float fr) { int32_t m[16] = {0}; @@ -269,7 +271,7 @@ ds3_mult_matrix(m); } -void ds3_perspective(float vfov_deg, float aspect, float znear, float zfar) +void ds3_perspectivef(float vfov_deg, float aspect, float znear, float zfar) { int32_t m[16] = {0}; @@ -285,3 +287,13 @@ ds3_mult_matrix(m); } + +int32_t x16div(int32_t a, int32_t b) +{ + REG_DIVCNT = DIVCNT_64_32; + REG_DIV_NUMER = (int64_t)a << 16; + REG_DIV_DENOM = (int64_t)b; + + while(REG_DIVCNT & DIVCNT_BUSY); + return (int32_t)REG_DIV_RESULT; +} diff -r d625ba001a62 -r dd8c9847bae9 src/ds3.h --- a/src/ds3.h Mon Jan 29 03:48:05 2018 +0200 +++ b/src/ds3.h Mon Jan 29 14:40:45 2018 +0200 @@ -66,6 +66,8 @@ void ds3_frustum(int32_t left, int32_t right, int32_t top, int32_t bottom, int32_t znear, int32_t zfar); -void ds3_perspective(float vfov_deg, float aspect, float znear, float zfar); +void ds3_perspectivef(float vfov_deg, float aspect, float znear, float zfar); + +int32_t x16div(int32_t a, int32_t b); #endif /* DS3_H_ */ diff -r d625ba001a62 -r dd8c9847bae9 src/dsregs.h --- a/src/dsregs.h Mon Jan 29 03:48:05 2018 +0200 +++ b/src/dsregs.h Mon Jan 29 14:40:45 2018 +0200 @@ -211,17 +211,31 @@ #define REG_VRAMCNT_I REG8(0x249) /* ---- math hardware registers ---- */ -#define REG_DIVCNT REG16(0x280) -#define REG_DIV_NUMER REG64(0x290) -#define REG_DIV_DENOM REG64(0x298) -#define REG_DIV_RESULT REG64(0x2a0) +#define REG_DIVCNT REG32(0x280) +#define REG_DIV_NUMER REG64(0x290) +#define REG_DIV_NUMERL REG32(0x290) +#define REG_DIV_NUMERH REG32(0x294) +#define REG_DIV_DENOM REG64(0x298) +#define REG_DIV_DENOML REG32(0x298) +#define REG_DIV_DENOMH REG32(0x29c) +#define REG_DIV_RESULT REG64(0x2a0) +#define REG_DIV_RESULTL REG32(0x2a0) +#define REG_DIV_RESULTH REG32(0x2a4) #define REG_DIVREM_RESULT REG64(0x2a8) -#define REG_SQRTCNT REG16(0x2b0) -#define REG_SQRT_RESULT REG32(0x2b4) -#define REG_SQRT_PARAM REG64(0x2b8) -#define REG_POSTFLG REG32(0x300) -#define REG_POWCNT1 REG16(0x304) -#define REG_POWCNT2 REG16(0x304) +#define REG_DIVREM_RESULTL REG32(0x2a8) +#define REG_DIVREM_RESULTH REG32(0x2ac) +#define REG_SQRTCNT REG16(0x2b0) +#define REG_SQRT_RESULT REG32(0x2b4) +#define REG_SQRT_PARAM REG64(0x2b8) +#define REG_POSTFLG REG32(0x300) +#define REG_POWCNT1 REG16(0x304) +#define REG_POWCNT2 REG16(0x304) + +#define DIVCNT_32_32 0 +#define DIVCNT_64_32 1 +#define DIVCNT_64_64 2 +#define DIVCNT_DIV0 0x4000 +#define DIVCNT_BUSY 0x8000 #define POWCNT1_LCD 0x0001 #define POWCNT1_2DA 0x0002 diff -r d625ba001a62 -r dd8c9847bae9 src/main.c --- a/src/main.c Mon Jan 29 03:48:05 2018 +0200 +++ b/src/main.c Mon Jan 29 14:40:45 2018 +0200 @@ -3,6 +3,7 @@ #include "dsregs.h" #include "ds3.h" +static void draw_cube(void); static void xorpat(void *addr, int xsz, int ysz); static void *vram = VRAM_LCDC_PTR; @@ -53,37 +54,39 @@ ds3_enable(DS3_POLYGON_SMOOTH); - REG_POLYGON_ATTR = 0x001f00c0; /* alpha = 31, cull none */ + REG_POLYGON_ATTR = 0x001f0080; /* alpha = 31, cull back */ ds3_matrix_mode(DS3_PROJECTION); ds3_load_identity(); - ds3_scale(49152, 65536, 65536); + /*ds3_ortho(-87381, 87381, -65536, 65536, -65536, 65536);*/ + /*ds3_orthof(-1.3333, 1.33333, -1, 1, -1, 1);*/ + ds3_perspectivef(45, 1.33333, 1.0, 100.0); for(;;) { int idx = frame & 0xff; int32_t scale = (sintab[(frame >> 1) & 0xff] >> 9) + 204; int32_t sa = ((sintab[idx] >> 8) * scale) >> 8; int32_t ca = ((costab[idx] >> 8) * scale) >> 8; - int32_t x = ca * -128 + sa * -96 + (128 << 8); int32_t y = -sa * -128 + ca * -96 + (96 << 8); - m[0] = ca << 8; m[1] = sa << 8; - m[4] = -sa << 8; m[5] = ca << 8; + ds3_matrix_mode(DS3_MODELVIEW); + ds3_load_identity(); + ds3_translate(0, 0, -0x30000); - ds3_matrix_mode(DS3_MODELVIEW); - ds3_load_matrix(m); + m[0] = 0x10000; + m[2] = m[8] = 0; + m[5] = costab[idx]; m[6] = sintab[idx]; + m[9] = -sintab[idx]; m[10] = costab[idx]; + ds3_mult_matrix(m); - ds3_begin(DS3_QUADS); - ds3_color(RGB15(31, 0, 0)); - ds3_vertex3(-0x8000, -0x8000, 0); - ds3_color(RGB15(0, 31, 0)); - ds3_vertex3(0x8000, -0x8000, 0); - ds3_color(RGB15(0, 0, 31)); - ds3_vertex3(0x8000, 0x8000, 0); - ds3_color(RGB15(31, 0, 31)); - ds3_vertex3(-0x8000, 0x8000, 0); - ds3_end(); + m[5] = 0x10000; + m[6] = m[9] = 0; + m[0] = costab[idx]; m[2] = -sintab[idx]; + m[8] = sintab[idx]; + ds3_mult_matrix(m); + + draw_cube(); ds3_swap_buffers(); while(REG_VCOUNT < 192); @@ -100,6 +103,43 @@ return 0; } +#define VOFFS 0x8000 +static void draw_cube(void) +{ + ds3_begin(DS3_QUADS); + ds3_color(RGB15(31, 0, 0)); + ds3_vertex3(-VOFFS, -VOFFS, VOFFS); + ds3_vertex3(VOFFS, -VOFFS, VOFFS); + ds3_vertex3(VOFFS, VOFFS, VOFFS); + ds3_vertex3(-VOFFS, VOFFS, VOFFS); + ds3_color(RGB15(0, 31, 0)); + ds3_vertex3(VOFFS, -VOFFS, VOFFS); + ds3_vertex3(VOFFS, -VOFFS, -VOFFS); + ds3_vertex3(VOFFS, VOFFS, -VOFFS); + ds3_vertex3(VOFFS, VOFFS, VOFFS); + ds3_color(RGB15(0, 0, 31)); + ds3_vertex3(VOFFS, -VOFFS, -VOFFS); + ds3_vertex3(-VOFFS, -VOFFS, -VOFFS); + ds3_vertex3(-VOFFS, VOFFS, -VOFFS); + ds3_vertex3(VOFFS, VOFFS, -VOFFS); + ds3_color(RGB15(31, 31, 0)); + ds3_vertex3(-VOFFS, -VOFFS, -VOFFS); + ds3_vertex3(-VOFFS, -VOFFS, VOFFS); + ds3_vertex3(-VOFFS, VOFFS, VOFFS); + ds3_vertex3(-VOFFS, VOFFS, -VOFFS); + ds3_color(RGB15(31, 0, 31)); + ds3_vertex3(-VOFFS, VOFFS, VOFFS); + ds3_vertex3(VOFFS, VOFFS, VOFFS); + ds3_vertex3(VOFFS, VOFFS, -VOFFS); + ds3_vertex3(-VOFFS, VOFFS, -VOFFS); + ds3_color(RGB15(0, 31, 31)); + ds3_vertex3(VOFFS, -VOFFS, -VOFFS); + ds3_vertex3(VOFFS, -VOFFS, VOFFS); + ds3_vertex3(-VOFFS, -VOFFS, VOFFS); + ds3_vertex3(-VOFFS, -VOFFS, -VOFFS); + ds3_end(); +} + static void xorpat(void *addr, int xsz, int ysz) { int i, j;