nds_test2

changeset 2:dd8c9847bae9 tip

cube
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 29 Jan 2018 14:40:45 +0200
parents d625ba001a62
children
files src/ds3.c src/ds3.h src/dsregs.h src/main.c
diffstat 4 files changed, 113 insertions(+), 45 deletions(-) [+]
line diff
     1.1 --- a/src/ds3.c	Mon Jan 29 03:48:05 2018 +0200
     1.2 +++ b/src/ds3.c	Mon Jan 29 14:40:45 2018 +0200
     1.3 @@ -194,12 +194,13 @@
     1.4  	int32_t dy = top - bottom;
     1.5  	int32_t dz = zfar - znear;
     1.6  
     1.7 -	m[0] = 0x2000000 / (dx << 8);
     1.8 -	m[5] = 0x2000000 / (dy << 8);
     1.9 -	m[10] = -0x2000000 / (dz << 8);
    1.10 -	m[12] = -((right + left) << 8) / (dx << 8);
    1.11 -	m[13] = -((top + bottom) << 8) / (dy << 8);
    1.12 -	m[14] = -((zfar + znear) << 8) / (dz << 8);
    1.13 +	m[0] = x16div(0x20000, dx);
    1.14 +	m[5] = x16div(0x20000, dy);
    1.15 +	m[10] = x16div(-0x20000, dz);
    1.16 +	m[12] = x16div(-(right + left), dx);
    1.17 +	m[13] = x16div(-(top + bottom), dy);
    1.18 +	m[14] = x16div(-(zfar + znear), dz);
    1.19 +	m[15] = 65536;
    1.20  
    1.21  	ds3_mult_matrix(m);
    1.22  }
    1.23 @@ -214,9 +215,10 @@
    1.24  	m[0] = (int32_t)(2.0f / dx * 65536.0f);
    1.25  	m[5] = (int32_t)(2.0f / dy * 65536.0f);
    1.26  	m[10] = (int32_t)(-2.0f / dz * 65536.0f);
    1.27 -	m[12] = (int32_t)(-(right + left) / dx);
    1.28 -	m[13] = (int32_t)(-(top + bottom) / dy);
    1.29 -	m[14] = (int32_t)(-(zfar + znear) / dz);
    1.30 +	m[12] = (int32_t)(-(right + left) / dx * 65536.0f);
    1.31 +	m[13] = (int32_t)(-(top + bottom) / dy * 65536.0f);
    1.32 +	m[14] = (int32_t)(-(zfar + znear) / dz * 65536.0f);
    1.33 +	m[15] = 65536;
    1.34  
    1.35  	ds3_mult_matrix(m);
    1.36  }
    1.37 @@ -229,13 +231,13 @@
    1.38  	int32_t dy = top - bottom;
    1.39  	int32_t dz = zfar - znear;
    1.40  
    1.41 -	int32_t a = ((right + left) << 8) / (dx << 8);
    1.42 -	int32_t b = ((top + bottom) << 8) / (dy << 8);
    1.43 -	int32_t c = (-(zfar + znear) << 8) / (dz << 8);
    1.44 -	int32_t d = -(((zfar >> 8) * znear) << 1) / (dz << 8);
    1.45 +	int32_t a = x16div(right + left, dx);
    1.46 +	int32_t b = x16div(top + bottom, dy);
    1.47 +	int32_t c = x16div(-(zfar + znear), dz);
    1.48 +	int32_t d = x16div(-((zfar >> 8) * znear) << 1, dz);
    1.49  
    1.50 -	m[0] = (znear << 9) / (dx << 8);
    1.51 -	m[5] = (znear << 9) / (dy << 8);
    1.52 +	m[0] = x16div(znear << 1, dx);
    1.53 +	m[5] = x16div(znear << 1, dy);
    1.54  	m[8] = a;
    1.55  	m[9] = b;
    1.56  	m[10] = c;
    1.57 @@ -245,7 +247,7 @@
    1.58  	ds3_mult_matrix(m);
    1.59  }
    1.60  
    1.61 -void g3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
    1.62 +void g3d_frustumf(float left, float right, float bottom, float top, float nr, float fr)
    1.63  {
    1.64  	int32_t m[16] = {0};
    1.65  
    1.66 @@ -269,7 +271,7 @@
    1.67  	ds3_mult_matrix(m);
    1.68  }
    1.69  
    1.70 -void ds3_perspective(float vfov_deg, float aspect, float znear, float zfar)
    1.71 +void ds3_perspectivef(float vfov_deg, float aspect, float znear, float zfar)
    1.72  {
    1.73  	int32_t m[16] = {0};
    1.74  
    1.75 @@ -285,3 +287,13 @@
    1.76  
    1.77  	ds3_mult_matrix(m);
    1.78  }
    1.79 +
    1.80 +int32_t x16div(int32_t a, int32_t b)
    1.81 +{
    1.82 +	REG_DIVCNT = DIVCNT_64_32;
    1.83 +	REG_DIV_NUMER = (int64_t)a << 16;
    1.84 +	REG_DIV_DENOM = (int64_t)b;
    1.85 +
    1.86 +	while(REG_DIVCNT & DIVCNT_BUSY);
    1.87 +	return (int32_t)REG_DIV_RESULT;
    1.88 +}
     2.1 --- a/src/ds3.h	Mon Jan 29 03:48:05 2018 +0200
     2.2 +++ b/src/ds3.h	Mon Jan 29 14:40:45 2018 +0200
     2.3 @@ -66,6 +66,8 @@
     2.4  
     2.5  void ds3_frustum(int32_t left, int32_t right, int32_t top, int32_t bottom, int32_t znear, int32_t zfar);
     2.6  
     2.7 -void ds3_perspective(float vfov_deg, float aspect, float znear, float zfar);
     2.8 +void ds3_perspectivef(float vfov_deg, float aspect, float znear, float zfar);
     2.9 +
    2.10 +int32_t x16div(int32_t a, int32_t b);
    2.11  
    2.12  #endif	/* DS3_H_ */
     3.1 --- a/src/dsregs.h	Mon Jan 29 03:48:05 2018 +0200
     3.2 +++ b/src/dsregs.h	Mon Jan 29 14:40:45 2018 +0200
     3.3 @@ -211,17 +211,31 @@
     3.4  #define REG_VRAMCNT_I	REG8(0x249)
     3.5  
     3.6  /* ---- math hardware registers ---- */
     3.7 -#define REG_DIVCNT		REG16(0x280)
     3.8 -#define REG_DIV_NUMER	REG64(0x290)
     3.9 -#define REG_DIV_DENOM	REG64(0x298)
    3.10 -#define REG_DIV_RESULT	REG64(0x2a0)
    3.11 +#define REG_DIVCNT			REG32(0x280)
    3.12 +#define REG_DIV_NUMER		REG64(0x290)
    3.13 +#define REG_DIV_NUMERL		REG32(0x290)
    3.14 +#define REG_DIV_NUMERH		REG32(0x294)
    3.15 +#define REG_DIV_DENOM		REG64(0x298)
    3.16 +#define REG_DIV_DENOML		REG32(0x298)
    3.17 +#define REG_DIV_DENOMH		REG32(0x29c)
    3.18 +#define REG_DIV_RESULT		REG64(0x2a0)
    3.19 +#define REG_DIV_RESULTL		REG32(0x2a0)
    3.20 +#define REG_DIV_RESULTH		REG32(0x2a4)
    3.21  #define REG_DIVREM_RESULT	REG64(0x2a8)
    3.22 -#define REG_SQRTCNT		REG16(0x2b0)
    3.23 -#define REG_SQRT_RESULT	REG32(0x2b4)
    3.24 -#define REG_SQRT_PARAM	REG64(0x2b8)
    3.25 -#define REG_POSTFLG		REG32(0x300)
    3.26 -#define REG_POWCNT1		REG16(0x304)
    3.27 -#define REG_POWCNT2		REG16(0x304)
    3.28 +#define REG_DIVREM_RESULTL	REG32(0x2a8)
    3.29 +#define REG_DIVREM_RESULTH	REG32(0x2ac)
    3.30 +#define REG_SQRTCNT			REG16(0x2b0)
    3.31 +#define REG_SQRT_RESULT		REG32(0x2b4)
    3.32 +#define REG_SQRT_PARAM		REG64(0x2b8)
    3.33 +#define REG_POSTFLG			REG32(0x300)
    3.34 +#define REG_POWCNT1			REG16(0x304)
    3.35 +#define REG_POWCNT2			REG16(0x304)
    3.36 +
    3.37 +#define DIVCNT_32_32		0
    3.38 +#define DIVCNT_64_32		1
    3.39 +#define DIVCNT_64_64		2
    3.40 +#define DIVCNT_DIV0			0x4000
    3.41 +#define DIVCNT_BUSY			0x8000
    3.42  
    3.43  #define POWCNT1_LCD			0x0001
    3.44  #define POWCNT1_2DA			0x0002
     4.1 --- a/src/main.c	Mon Jan 29 03:48:05 2018 +0200
     4.2 +++ b/src/main.c	Mon Jan 29 14:40:45 2018 +0200
     4.3 @@ -3,6 +3,7 @@
     4.4  #include "dsregs.h"
     4.5  #include "ds3.h"
     4.6  
     4.7 +static void draw_cube(void);
     4.8  static void xorpat(void *addr, int xsz, int ysz);
     4.9  
    4.10  static void *vram = VRAM_LCDC_PTR;
    4.11 @@ -53,37 +54,39 @@
    4.12  
    4.13  	ds3_enable(DS3_POLYGON_SMOOTH);
    4.14  
    4.15 -	REG_POLYGON_ATTR = 0x001f00c0;	/* alpha = 31, cull none */
    4.16 +	REG_POLYGON_ATTR = 0x001f0080;	/* alpha = 31, cull back */
    4.17  
    4.18  	ds3_matrix_mode(DS3_PROJECTION);
    4.19  	ds3_load_identity();
    4.20 -	ds3_scale(49152, 65536, 65536);
    4.21 +	/*ds3_ortho(-87381, 87381, -65536, 65536, -65536, 65536);*/
    4.22 +	/*ds3_orthof(-1.3333, 1.33333, -1, 1, -1, 1);*/
    4.23 +	ds3_perspectivef(45, 1.33333, 1.0, 100.0);
    4.24  
    4.25  	for(;;) {
    4.26  		int idx = frame & 0xff;
    4.27  		int32_t scale = (sintab[(frame >> 1) & 0xff] >> 9) + 204;
    4.28  		int32_t sa = ((sintab[idx] >> 8) * scale) >> 8;
    4.29  		int32_t ca = ((costab[idx] >> 8) * scale) >> 8;
    4.30 -
    4.31  		int32_t x = ca * -128 + sa * -96 + (128 << 8);
    4.32  		int32_t y = -sa * -128 + ca * -96 + (96 << 8);
    4.33  
    4.34 -		m[0] = ca << 8; m[1] = sa << 8;
    4.35 -		m[4] = -sa << 8; m[5] = ca << 8;
    4.36 +		ds3_matrix_mode(DS3_MODELVIEW);
    4.37 +		ds3_load_identity();
    4.38 +		ds3_translate(0, 0, -0x30000);
    4.39  
    4.40 -		ds3_matrix_mode(DS3_MODELVIEW);
    4.41 -		ds3_load_matrix(m);
    4.42 +		m[0] = 0x10000;
    4.43 +		m[2] = m[8] = 0;
    4.44 +		m[5] = costab[idx]; m[6] = sintab[idx];
    4.45 +		m[9] = -sintab[idx]; m[10] = costab[idx];
    4.46 +		ds3_mult_matrix(m);
    4.47  
    4.48 -		ds3_begin(DS3_QUADS);
    4.49 -		ds3_color(RGB15(31, 0, 0));
    4.50 -		ds3_vertex3(-0x8000, -0x8000, 0);
    4.51 -		ds3_color(RGB15(0, 31, 0));
    4.52 -		ds3_vertex3(0x8000, -0x8000, 0);
    4.53 -		ds3_color(RGB15(0, 0, 31));
    4.54 -		ds3_vertex3(0x8000, 0x8000, 0);
    4.55 -		ds3_color(RGB15(31, 0, 31));
    4.56 -		ds3_vertex3(-0x8000, 0x8000, 0);
    4.57 -		ds3_end();
    4.58 +		m[5] = 0x10000;
    4.59 +		m[6] = m[9] = 0;
    4.60 +		m[0] = costab[idx]; m[2] = -sintab[idx];
    4.61 +		m[8] = sintab[idx];
    4.62 +		ds3_mult_matrix(m);
    4.63 +
    4.64 +		draw_cube();
    4.65  
    4.66  		ds3_swap_buffers();
    4.67  		while(REG_VCOUNT < 192);
    4.68 @@ -100,6 +103,43 @@
    4.69  	return 0;
    4.70  }
    4.71  
    4.72 +#define VOFFS	0x8000
    4.73 +static void draw_cube(void)
    4.74 +{
    4.75 +	ds3_begin(DS3_QUADS);
    4.76 +	ds3_color(RGB15(31, 0, 0));
    4.77 +	ds3_vertex3(-VOFFS, -VOFFS, VOFFS);
    4.78 +	ds3_vertex3(VOFFS, -VOFFS, VOFFS);
    4.79 +	ds3_vertex3(VOFFS, VOFFS, VOFFS);
    4.80 +	ds3_vertex3(-VOFFS, VOFFS, VOFFS);
    4.81 +	ds3_color(RGB15(0, 31, 0));
    4.82 +	ds3_vertex3(VOFFS, -VOFFS, VOFFS);
    4.83 +	ds3_vertex3(VOFFS, -VOFFS, -VOFFS);
    4.84 +	ds3_vertex3(VOFFS, VOFFS, -VOFFS);
    4.85 +	ds3_vertex3(VOFFS, VOFFS, VOFFS);
    4.86 +	ds3_color(RGB15(0, 0, 31));
    4.87 +	ds3_vertex3(VOFFS, -VOFFS, -VOFFS);
    4.88 +	ds3_vertex3(-VOFFS, -VOFFS, -VOFFS);
    4.89 +	ds3_vertex3(-VOFFS, VOFFS, -VOFFS);
    4.90 +	ds3_vertex3(VOFFS, VOFFS, -VOFFS);
    4.91 +	ds3_color(RGB15(31, 31, 0));
    4.92 +	ds3_vertex3(-VOFFS, -VOFFS, -VOFFS);
    4.93 +	ds3_vertex3(-VOFFS, -VOFFS, VOFFS);
    4.94 +	ds3_vertex3(-VOFFS, VOFFS, VOFFS);
    4.95 +	ds3_vertex3(-VOFFS, VOFFS, -VOFFS);
    4.96 +	ds3_color(RGB15(31, 0, 31));
    4.97 +	ds3_vertex3(-VOFFS, VOFFS, VOFFS);
    4.98 +	ds3_vertex3(VOFFS, VOFFS, VOFFS);
    4.99 +	ds3_vertex3(VOFFS, VOFFS, -VOFFS);
   4.100 +	ds3_vertex3(-VOFFS, VOFFS, -VOFFS);
   4.101 +	ds3_color(RGB15(0, 31, 31));
   4.102 +	ds3_vertex3(VOFFS, -VOFFS, -VOFFS);
   4.103 +	ds3_vertex3(VOFFS, -VOFFS, VOFFS);
   4.104 +	ds3_vertex3(-VOFFS, -VOFFS, VOFFS);
   4.105 +	ds3_vertex3(-VOFFS, -VOFFS, -VOFFS);
   4.106 +	ds3_end();
   4.107 +}
   4.108 +
   4.109  static void xorpat(void *addr, int xsz, int ysz)
   4.110  {
   4.111  	int i, j;