nds_test2

view src/ds3.c @ 2:dd8c9847bae9

cube
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 29 Jan 2018 14:40:45 +0200
parents d625ba001a62
children
line source
1 #include <stdint.h>
2 #include <math.h>
3 #include "dsregs.h"
4 #include "ds3.h"
6 void ds3_enable(unsigned int x)
7 {
8 REG_DISP3DCNT |= x;
9 }
11 void ds3_disable(unsigned int x)
12 {
13 REG_DISP3DCNT &= ~x;
14 }
16 void ds3_clear_color(uint16_t color, int a)
17 {
18 REG_CLEAR_COLOR = color | ((a & 0x1f) << 16);
19 }
21 void ds3_clear_depth(int z)
22 {
23 REG_CLEAR_DEPTH = z;
24 }
26 void ds3_viewport(int x, int y, int w, int h)
27 {
28 int x1 = x + w - 1;
29 int y1 = y + h - 1;
31 if(x1 > 255) x1 = 255;
32 if(y1 > 191) y1 = 191;
34 REG_VIEWPORT = x | (y << 8) | (x1 << 16) | (y1 << 24);
35 }
37 void ds3_matrix_mode(int mmode)
38 {
39 REG_MTX_MODE = mmode;
40 }
42 void ds3_load_identity(void)
43 {
44 REG_MTX_IDENTITY = 0;
45 }
47 void ds3_load_matrix(int32_t *m)
48 {
49 int i;
50 for(i=0; i<16; i++) {
51 int16_t val = (int16_t)(*m++ >> 4);
52 REG_MTX_LOAD_4X4 = val;
53 }
54 }
56 void ds3_load_matrix4x3(int32_t *m)
57 {
58 int i;
59 for(i=0; i<12; i++) {
60 int16_t val = (int16_t)(*m++ >> 4);
61 REG_MTX_LOAD_4X3 = val;
62 }
63 }
65 void ds3_mult_matrix(int32_t *m)
66 {
67 int i;
68 for(i=0; i<16; i++) {
69 int16_t val = (int16_t)(*m++ >> 4);
70 REG_MTX_MULT_4X4 = val;
71 }
72 }
74 void ds3_mult_matrix4x3(int32_t *m)
75 {
76 int i;
77 for(i=0; i<12; i++) {
78 int16_t val = (int16_t)(*m++ >> 4);
79 REG_MTX_MULT_4X3 = val;
80 }
81 }
83 void ds3_mult_matrix3x3(int32_t *m)
84 {
85 int i;
86 for(i=0; i<9; i++) {
87 int16_t val = (int16_t)(*m++ >> 4);
88 REG_MTX_MULT_3X3 = val;
89 }
90 }
92 void ds3_push_matrix(void)
93 {
94 REG_MTX_PUSH = 0;
95 }
97 void ds3_pop_matrix(void)
98 {
99 REG_MTX_POP = 1;
100 }
102 void ds3_translate(int32_t x, int32_t y, int32_t z)
103 {
104 REG_MTX_TRANS = (int16_t)(x >> 4);
105 REG_MTX_TRANS = (int16_t)(y >> 4);
106 REG_MTX_TRANS = (int16_t)(z >> 4);
107 }
109 void ds3_scale(int32_t x, int32_t y, int32_t z)
110 {
111 REG_MTX_SCALE = (int16_t)(x >> 4);
112 REG_MTX_SCALE = (int16_t)(y >> 4);
113 REG_MTX_SCALE = (int16_t)(z >> 4);
114 }
116 void ds3_swap_buffers(void)
117 {
118 REG_SWAP_BUFFERS = 0;
119 }
121 void ds3_begin(int prim)
122 {
123 REG_BEGIN_VTXS = prim;
124 }
126 void ds3_end(void)
127 {
128 REG_END_VTXS = 0;
129 }
131 void ds3_vertex3(int32_t x, int32_t y, int32_t z)
132 {
133 REG_VTX_16 = ((x >> 4) & 0xffff) | ((y << 12) & 0xffff0000);
134 REG_VTX_16 = (z >> 4) & 0xffff;
135 }
137 void ds3_vertex3f(float x, float y, float z)
138 {
139 ds3_vertex3((int32_t)(x * 65536.0f), (int32_t)(y * 65536.0f), (int32_t)(z * 65536.0f));
140 }
142 void ds3_vertex2(int32_t x, int32_t y)
143 {
144 REG_VTX_XY = ((x >> 4) & 0xffff) | ((y << 12) & 0xffff0000);
145 }
147 void ds3_vertex2f(float x, float y)
148 {
149 ds3_vertex2((int32_t)(x * 65536.0f), (int32_t)(y * 65536.0f));
150 }
152 void ds3_color(uint16_t color)
153 {
154 REG_COLOR = color;
155 }
157 void ds3_color3b(unsigned char r, unsigned char g, unsigned char b)
158 {
159 REG_COLOR = RGB15(r >> 3, g >> 3, b >> 3);
160 }
162 void ds3_color3f(float r, float g, float b)
163 {
164 uint16_t ir = r * 31.0f;
165 uint16_t ig = g * 31.0f;
166 uint16_t ib = b * 31.0f;
167 REG_COLOR = RGB15(ir, ig, ib);
168 }
170 void ds3_normal(int32_t x, int32_t y, int32_t z)
171 {
172 REG_NORMAL = ((x >> 7) & 0x3ff) | ((y << 17) & 0xffc00) | ((z << 27) & 0x3ff00000);
173 }
175 void ds3_normal3f(float x, float y, float z)
176 {
177 ds3_normal((int32_t)(x * 65536.0f), (int32_t)(y * 65536.0f), (int32_t)(z * 65536.0f));
178 }
180 void ds3_texcoord2(int32_t s, int32_t t)
181 {
182 REG_TEXCOORD = (((s) >> 12) & 0xffff) | (((t) << 4) & 0xffff0000);
183 }
185 void ds3_texcoord2f(float s, float t)
186 {
187 ds3_texcoord2((int32_t)(s * 65536.0f), (int32_t)(t * 65536.0f));
188 }
190 void ds3_ortho(int32_t left, int32_t right, int32_t top, int32_t bottom, int32_t znear, int32_t zfar)
191 {
192 int32_t m[16] = {0};
193 int32_t dx = right - left;
194 int32_t dy = top - bottom;
195 int32_t dz = zfar - znear;
197 m[0] = x16div(0x20000, dx);
198 m[5] = x16div(0x20000, dy);
199 m[10] = x16div(-0x20000, dz);
200 m[12] = x16div(-(right + left), dx);
201 m[13] = x16div(-(top + bottom), dy);
202 m[14] = x16div(-(zfar + znear), dz);
203 m[15] = 65536;
205 ds3_mult_matrix(m);
206 }
208 void ds3_orthof(float left, float right, float top, float bottom, float znear, float zfar)
209 {
210 int32_t m[16] = {0};
211 float dx = right - left;
212 float dy = top - bottom;
213 float dz = zfar - znear;
215 m[0] = (int32_t)(2.0f / dx * 65536.0f);
216 m[5] = (int32_t)(2.0f / dy * 65536.0f);
217 m[10] = (int32_t)(-2.0f / dz * 65536.0f);
218 m[12] = (int32_t)(-(right + left) / dx * 65536.0f);
219 m[13] = (int32_t)(-(top + bottom) / dy * 65536.0f);
220 m[14] = (int32_t)(-(zfar + znear) / dz * 65536.0f);
221 m[15] = 65536;
223 ds3_mult_matrix(m);
224 }
226 void ds3_frustum(int32_t left, int32_t right, int32_t top, int32_t bottom, int32_t znear, int32_t zfar)
227 {
228 int32_t m[16] = {0};
230 int32_t dx = right - left;
231 int32_t dy = top - bottom;
232 int32_t dz = zfar - znear;
234 int32_t a = x16div(right + left, dx);
235 int32_t b = x16div(top + bottom, dy);
236 int32_t c = x16div(-(zfar + znear), dz);
237 int32_t d = x16div(-((zfar >> 8) * znear) << 1, dz);
239 m[0] = x16div(znear << 1, dx);
240 m[5] = x16div(znear << 1, dy);
241 m[8] = a;
242 m[9] = b;
243 m[10] = c;
244 m[11] = -65536;
245 m[14] = d;
247 ds3_mult_matrix(m);
248 }
250 void g3d_frustumf(float left, float right, float bottom, float top, float nr, float fr)
251 {
252 int32_t m[16] = {0};
254 float dx = right - left;
255 float dy = top - bottom;
256 float dz = fr - nr;
258 float a = (right + left) / dx;
259 float b = (top + bottom) / dy;
260 float c = -(fr + nr) / dz;
261 float d = -2.0 * fr * nr / dz;
263 m[0] = (int32_t)(2.0 * nr / dx * 65536.0f);
264 m[5] = (int32_t)(2.0 * nr / dy * 65536.0f);
265 m[8] = (int32_t)(a * 65536.0f);
266 m[9] = (int32_t)(b * 65536.0f);
267 m[10] = (int32_t)(c * 65536.0f);
268 m[11] = -65536;
269 m[14] = (int32_t)(d * 65536.0f);
271 ds3_mult_matrix(m);
272 }
274 void ds3_perspectivef(float vfov_deg, float aspect, float znear, float zfar)
275 {
276 int32_t m[16] = {0};
278 float vfov = M_PI * vfov_deg / 180.0f;
279 float s = 1.0f / tan(vfov * 0.5f);
280 float range = znear - zfar;
282 m[0] = (int32_t)(s / aspect * 65536.0f);
283 m[5] = (int32_t)(s * 65536.0f);
284 m[10] = (int32_t)((znear + zfar) / range * 65536.0f);
285 m[11] = -65536;
286 m[14] = (int32_t)(2.0f * znear * zfar / range * 65536.0f);
288 ds3_mult_matrix(m);
289 }
291 int32_t x16div(int32_t a, int32_t b)
292 {
293 REG_DIVCNT = DIVCNT_64_32;
294 REG_DIV_NUMER = (int64_t)a << 16;
295 REG_DIV_DENOM = (int64_t)b;
297 while(REG_DIVCNT & DIVCNT_BUSY);
298 return (int32_t)REG_DIV_RESULT;
299 }