src/ds/gx.c (view raw)
1/* Copyright (c) 2013-2017 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include <mgba/internal/ds/gx.h>
7
8#include <mgba/internal/ds/ds.h>
9#include <mgba/internal/ds/io.h>
10
11mLOG_DEFINE_CATEGORY(DS_GX, "DS GX", "ds.gx");
12
13#define DS_GX_FIFO_SIZE 256
14#define DS_GX_PIPE_SIZE 4
15
16static void DSGXDummyRendererInit(struct DSGXRenderer* renderer);
17static void DSGXDummyRendererReset(struct DSGXRenderer* renderer);
18static void DSGXDummyRendererDeinit(struct DSGXRenderer* renderer);
19static void DSGXDummyRendererInvalidateTex(struct DSGXRenderer* renderer, int slot);
20static void DSGXDummyRendererSetRAM(struct DSGXRenderer* renderer, struct DSGXVertex* verts, struct DSGXPolygon* polys, unsigned polyCount, bool wSort);
21static void DSGXDummyRendererDrawScanline(struct DSGXRenderer* renderer, int y);
22static void DSGXDummyRendererGetScanline(struct DSGXRenderer* renderer, int y, const color_t** output);
23static void DSGXDummyRendererWriteRegister(struct DSGXRenderer* renderer, uint32_t address, uint16_t value);
24
25static void DSGXWriteFIFO(struct DSGX* gx, struct DSGXEntry entry);
26
27static const int32_t _gxCommandCycleBase[DS_GX_CMD_MAX] = {
28 [DS_GX_CMD_NOP] = 0,
29 [DS_GX_CMD_MTX_MODE] = 2,
30 [DS_GX_CMD_MTX_PUSH] = 34,
31 [DS_GX_CMD_MTX_POP] = 72,
32 [DS_GX_CMD_MTX_STORE] = 34,
33 [DS_GX_CMD_MTX_RESTORE] = 72,
34 [DS_GX_CMD_MTX_IDENTITY] = 38,
35 [DS_GX_CMD_MTX_LOAD_4x4] = 68,
36 [DS_GX_CMD_MTX_LOAD_4x3] = 60,
37 [DS_GX_CMD_MTX_MULT_4x4] = 70,
38 [DS_GX_CMD_MTX_MULT_4x3] = 62,
39 [DS_GX_CMD_MTX_MULT_3x3] = 56,
40 [DS_GX_CMD_MTX_SCALE] = 44,
41 [DS_GX_CMD_MTX_TRANS] = 44,
42 [DS_GX_CMD_COLOR] = 2,
43 [DS_GX_CMD_NORMAL] = 18,
44 [DS_GX_CMD_TEXCOORD] = 2,
45 [DS_GX_CMD_VTX_16] = 18,
46 [DS_GX_CMD_VTX_10] = 16,
47 [DS_GX_CMD_VTX_XY] = 16,
48 [DS_GX_CMD_VTX_XZ] = 16,
49 [DS_GX_CMD_VTX_YZ] = 16,
50 [DS_GX_CMD_VTX_DIFF] = 16,
51 [DS_GX_CMD_POLYGON_ATTR] = 2,
52 [DS_GX_CMD_TEXIMAGE_PARAM] = 2,
53 [DS_GX_CMD_PLTT_BASE] = 2,
54 [DS_GX_CMD_DIF_AMB] = 8,
55 [DS_GX_CMD_SPE_EMI] = 8,
56 [DS_GX_CMD_LIGHT_VECTOR] = 12,
57 [DS_GX_CMD_LIGHT_COLOR] = 2,
58 [DS_GX_CMD_SHININESS] = 64,
59 [DS_GX_CMD_BEGIN_VTXS] = 2,
60 [DS_GX_CMD_END_VTXS] = 2,
61 [DS_GX_CMD_SWAP_BUFFERS] = 784,
62 [DS_GX_CMD_VIEWPORT] = 2,
63 [DS_GX_CMD_BOX_TEST] = 206,
64 [DS_GX_CMD_POS_TEST] = 18,
65 [DS_GX_CMD_VEC_TEST] = 10,
66};
67
68static const int32_t _gxCommandParams[DS_GX_CMD_MAX] = {
69 [DS_GX_CMD_MTX_MODE] = 1,
70 [DS_GX_CMD_MTX_POP] = 1,
71 [DS_GX_CMD_MTX_STORE] = 1,
72 [DS_GX_CMD_MTX_RESTORE] = 1,
73 [DS_GX_CMD_MTX_LOAD_4x4] = 16,
74 [DS_GX_CMD_MTX_LOAD_4x3] = 12,
75 [DS_GX_CMD_MTX_MULT_4x4] = 16,
76 [DS_GX_CMD_MTX_MULT_4x3] = 12,
77 [DS_GX_CMD_MTX_MULT_3x3] = 9,
78 [DS_GX_CMD_MTX_SCALE] = 3,
79 [DS_GX_CMD_MTX_TRANS] = 3,
80 [DS_GX_CMD_COLOR] = 1,
81 [DS_GX_CMD_NORMAL] = 1,
82 [DS_GX_CMD_TEXCOORD] = 1,
83 [DS_GX_CMD_VTX_16] = 2,
84 [DS_GX_CMD_VTX_10] = 1,
85 [DS_GX_CMD_VTX_XY] = 1,
86 [DS_GX_CMD_VTX_XZ] = 1,
87 [DS_GX_CMD_VTX_YZ] = 1,
88 [DS_GX_CMD_VTX_DIFF] = 1,
89 [DS_GX_CMD_POLYGON_ATTR] = 1,
90 [DS_GX_CMD_TEXIMAGE_PARAM] = 1,
91 [DS_GX_CMD_PLTT_BASE] = 1,
92 [DS_GX_CMD_DIF_AMB] = 1,
93 [DS_GX_CMD_SPE_EMI] = 1,
94 [DS_GX_CMD_LIGHT_VECTOR] = 1,
95 [DS_GX_CMD_LIGHT_COLOR] = 1,
96 [DS_GX_CMD_SHININESS] = 32,
97 [DS_GX_CMD_BEGIN_VTXS] = 1,
98 [DS_GX_CMD_SWAP_BUFFERS] = 1,
99 [DS_GX_CMD_VIEWPORT] = 1,
100 [DS_GX_CMD_BOX_TEST] = 3,
101 [DS_GX_CMD_POS_TEST] = 2,
102 [DS_GX_CMD_VEC_TEST] = 1,
103};
104
105static struct DSGXRenderer dummyRenderer = {
106 .init = DSGXDummyRendererInit,
107 .reset = DSGXDummyRendererReset,
108 .deinit = DSGXDummyRendererDeinit,
109 .invalidateTex = DSGXDummyRendererInvalidateTex,
110 .setRAM = DSGXDummyRendererSetRAM,
111 .drawScanline = DSGXDummyRendererDrawScanline,
112 .getScanline = DSGXDummyRendererGetScanline,
113 .writeRegister = DSGXDummyRendererWriteRegister,
114};
115
116static void _pullPipe(struct DSGX* gx) {
117 if (CircleBufferSize(&gx->fifo) >= sizeof(struct DSGXEntry)) {
118 struct DSGXEntry entry = { 0 };
119 CircleBufferRead(&gx->fifo, &entry, sizeof(entry));
120 CircleBufferWrite(&gx->pipe, &entry, sizeof(entry));
121 }
122 if (CircleBufferSize(&gx->fifo) >= sizeof(struct DSGXEntry)) {
123 struct DSGXEntry entry = { 0 };
124 CircleBufferRead(&gx->fifo, &entry, sizeof(entry));
125 CircleBufferWrite(&gx->pipe, &entry, sizeof(entry));
126 }
127}
128
129static void _updateClipMatrix(struct DSGX* gx) {
130 DSGXMtxMultiply(&gx->clipMatrix, &gx->posMatrix, &gx->projMatrix);
131 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_00 >> 1] = gx->clipMatrix.m[0];
132 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_01 >> 1] = gx->clipMatrix.m[0] >> 16;
133 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_02 >> 1] = gx->clipMatrix.m[1];
134 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_03 >> 1] = gx->clipMatrix.m[1] >> 16;
135 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_04 >> 1] = gx->clipMatrix.m[2];
136 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_05 >> 1] = gx->clipMatrix.m[2] >> 16;
137 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_06 >> 1] = gx->clipMatrix.m[3];
138 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_07 >> 1] = gx->clipMatrix.m[3] >> 16;
139 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_08 >> 1] = gx->clipMatrix.m[4];
140 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_09 >> 1] = gx->clipMatrix.m[4] >> 16;
141 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_0A >> 1] = gx->clipMatrix.m[5];
142 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_0B >> 1] = gx->clipMatrix.m[5] >> 16;
143 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_0C >> 1] = gx->clipMatrix.m[6];
144 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_0D >> 1] = gx->clipMatrix.m[6] >> 16;
145 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_0E >> 1] = gx->clipMatrix.m[7];
146 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_0F >> 1] = gx->clipMatrix.m[7] >> 16;
147 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_10 >> 1] = gx->clipMatrix.m[8];
148 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_11 >> 1] = gx->clipMatrix.m[8] >> 16;
149 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_12 >> 1] = gx->clipMatrix.m[9];
150 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_13 >> 1] = gx->clipMatrix.m[9] >> 16;
151 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_14 >> 1] = gx->clipMatrix.m[10];
152 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_15 >> 1] = gx->clipMatrix.m[10] >> 16;
153 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_16 >> 1] = gx->clipMatrix.m[11];
154 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_17 >> 1] = gx->clipMatrix.m[11] >> 16;
155 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_18 >> 1] = gx->clipMatrix.m[12];
156 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_19 >> 1] = gx->clipMatrix.m[12] >> 16;
157 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_1A >> 1] = gx->clipMatrix.m[13];
158 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_1B >> 1] = gx->clipMatrix.m[13] >> 16;
159 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_1C >> 1] = gx->clipMatrix.m[14];
160 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_1D >> 1] = gx->clipMatrix.m[14] >> 16;
161 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_1E >> 1] = gx->clipMatrix.m[15];
162 gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_1F >> 1] = gx->clipMatrix.m[15] >> 16;
163}
164
165static inline int32_t _lerp(int32_t x0, int32_t x1, int32_t q, int64_t r) {
166 int64_t x = x1 - x0;
167 x *= q;
168 x /= r;
169 x += x0;
170 return x;
171}
172
173static int _cohenSutherlandCode(const struct DSGXVertex* v) {
174 int code = 0;
175 if (v->viewCoord[0] < -v->viewCoord[3]) {
176 code |= 1 << 0;
177 } else if (v->viewCoord[0] > v->viewCoord[3]) {
178 code |= 2 << 0;
179 }
180 if (v->viewCoord[1] < -v->viewCoord[3]) {
181 code |= 1 << 2;
182 } else if (v->viewCoord[1] > v->viewCoord[3]) {
183 code |= 2 << 2;
184 }
185 if (v->viewCoord[2] < -v->viewCoord[3]) {
186 code |= 1 << 4;
187 } else if (v->viewCoord[2] > v->viewCoord[3]) {
188 code |= 2 << 4;
189 }
190 return code;
191}
192
193static bool _lerpVertex(const struct DSGXVertex* v0, const struct DSGXVertex* v1, struct DSGXVertex* out, int32_t q, int64_t r) {
194 if (!r) {
195 return false;
196 }
197 int cr0 = (v0->color) & 0x1F;
198 int cg0 = (v0->color >> 5) & 0x1F;
199 int cb0 = (v0->color >> 10) & 0x1F;
200 int cr1 = (v1->color) & 0x1F;
201 int cg1 = (v1->color >> 5) & 0x1F;
202 int cb1 = (v1->color >> 10) & 0x1F;
203 cr0 = _lerp(cr0, cr1, q, r) & 0x1F;
204 cg0 = _lerp(cg0, cg1, q, r) & 0x1F;
205 cb0 = _lerp(cb0, cb1, q, r) & 0x1F;
206 out->color = cr0 | (cg0 << 5) | (cb0 << 10);
207
208 out->viewCoord[0] = _lerp(v0->viewCoord[0], v1->viewCoord[0], q, r);
209 out->viewCoord[1] = _lerp(v0->viewCoord[1], v1->viewCoord[1], q, r);
210 out->viewCoord[2] = _lerp(v0->viewCoord[2], v1->viewCoord[2], q, r);
211 out->viewCoord[3] = _lerp(v0->viewCoord[3], v1->viewCoord[3], q, r);
212 out->viewportX = v0->viewportX;
213 out->viewportY = v0->viewportY;
214 out->viewportWidth = v0->viewportWidth;
215 out->viewportHeight = v0->viewportHeight;
216
217 out->vs = _lerp(v0->vs, v1->vs, q, r);
218 out->vt = _lerp(v0->vt, v1->vt, q, r);
219 return true;
220}
221
222static bool _lerpVertexP(const struct DSGXVertex* v0, const struct DSGXVertex* v1, struct DSGXVertex* out, int plane, int sign) {
223 int32_t q = v0->viewCoord[3] - sign * v0->viewCoord[plane];
224 int64_t r = q - (v1->viewCoord[3] - sign * v1->viewCoord[plane]);
225 return _lerpVertex(v0, v1, out, q, r);
226}
227
228static bool _clipPolygon(struct DSGX* gx, struct DSGXPolygon* poly) {
229 int nOffscreen = 0;
230 int offscreenVerts[10] = { 0, 0, 0, 0 };
231 unsigned oldVerts[4];
232 int v;
233
234 if (!DSGXPolygonAttrsIsBackFace(poly->polyParams) || !DSGXPolygonAttrsIsFrontFace(poly->polyParams)) {
235 // Calculate normal direction and camera dot product average
236 int64_t nx = 0;
237 int64_t ny = 0;
238 int64_t nz = 0;
239 int64_t dot = 0;
240 for (v = 0; v < poly->verts; ++v) {
241 struct DSGXVertex* v0 = &gx->pendingVertices[poly->vertIds[v]];
242 struct DSGXVertex* v1;
243 struct DSGXVertex* v2;
244 if (v < poly->verts - 2) {
245 v1 = &gx->pendingVertices[poly->vertIds[v + 1]];
246 v2 = &gx->pendingVertices[poly->vertIds[v + 2]];
247 } else if (v < poly->verts - 1) {
248 v1 = &gx->pendingVertices[poly->vertIds[v + 1]];
249 v2 = &gx->pendingVertices[poly->vertIds[v + 2 - poly->verts]];
250 } else {
251 v1 = &gx->pendingVertices[poly->vertIds[v + 1 - poly->verts]];
252 v2 = &gx->pendingVertices[poly->vertIds[v + 2 - poly->verts]];
253 }
254 nx = ((int64_t) v0->viewCoord[1] * v2->viewCoord[3] - (int64_t) v0->viewCoord[3] * v2->viewCoord[1]) >> 24;
255 ny = ((int64_t) v0->viewCoord[3] * v2->viewCoord[0] - (int64_t) v0->viewCoord[0] * v2->viewCoord[3]) >> 24;
256 nz = ((int64_t) v0->viewCoord[0] * v2->viewCoord[1] - (int64_t) v0->viewCoord[1] * v2->viewCoord[0]) >> 24;
257 dot += nx * v1->viewCoord[0] + ny * v1->viewCoord[1] + nz * v1->viewCoord[3];
258 }
259 if (!DSGXPolygonAttrsIsBackFace(poly->polyParams) && dot < 0) {
260 return false;
261 }
262 if (!DSGXPolygonAttrsIsFrontFace(poly->polyParams) && dot > 0) {
263 return false;
264 }
265 }
266
267 // Collect offscreen vertices
268 for (v = 0; v < poly->verts; ++v) {
269 offscreenVerts[v] = _cohenSutherlandCode(&gx->pendingVertices[poly->vertIds[v]]);
270 oldVerts[v] = poly->vertIds[v];
271 if (offscreenVerts[v]) {
272 ++nOffscreen;
273 }
274 }
275
276 struct DSGXVertex* vbuf = gx->vertexBuffer[gx->bufferIndex];
277
278 if (!nOffscreen) {
279 for (v = 0; v < poly->verts; ++v) {
280 if (gx->vertexIndex == DS_GX_VERTEX_BUFFER_SIZE) {
281 return false;
282 }
283 int vertexId = oldVerts[v];
284 if (gx->pendingVertexIds[vertexId] >= 0) {
285 poly->vertIds[v] = gx->pendingVertexIds[vertexId];
286 } else {
287 vbuf[gx->vertexIndex] = gx->pendingVertices[vertexId];
288 gx->pendingVertexIds[vertexId] = gx->vertexIndex;
289 poly->vertIds[v] = gx->vertexIndex;
290 ++gx->vertexIndex;
291 }
292 }
293 return true;
294 }
295
296 struct DSGXVertex inList[10];
297 struct DSGXVertex outList[10];
298 int outOffscreenVerts[10] = { 0, 0, 0, 0 };
299 for (v = 0; v < poly->verts; ++v) {
300 inList[v] = gx->pendingVertices[oldVerts[v]];
301 }
302
303 int newV;
304
305 int plane;
306 for (plane = 5; plane >= 0; --plane) {
307 newV = 0;
308 for (v = 0; v < poly->verts; ++v) {
309 if (!(offscreenVerts[v] & (1 << plane))) {
310 outList[newV] = inList[v];
311 outOffscreenVerts[newV] = offscreenVerts[v];
312 ++newV;
313 } else {
314 struct DSGXVertex* in = &inList[v];
315 struct DSGXVertex* in2;
316 struct DSGXVertex* out;
317 int iv;
318
319 if (v > 0) {
320 iv = v - 1;
321 } else {
322 iv = poly->verts - 1;
323 }
324 if (!(offscreenVerts[iv] & (1 << plane))) {
325 in2 = &inList[iv];
326 out = &outList[newV];
327 if (_lerpVertexP(in, in2, out, plane >> 1, -1 + (plane & 1) * 2)) {
328 outOffscreenVerts[newV] = _cohenSutherlandCode(out);
329 ++newV;
330 }
331 }
332
333 if (v < poly->verts - 1) {
334 iv = v + 1;
335 } else {
336 iv = 0;
337 }
338 if (!(offscreenVerts[iv] & (1 << plane))) {
339 in2 = &inList[iv];
340 out = &outList[newV];
341 if (_lerpVertexP(in, in2, out, plane >> 1, -1 + (plane & 1) * 2)) {
342 outOffscreenVerts[newV] = _cohenSutherlandCode(out);
343 ++newV;
344 }
345 }
346 }
347 }
348 poly->verts = newV;
349 memcpy(inList, outList, newV * sizeof(*inList));
350 memcpy(offscreenVerts, outOffscreenVerts, newV * sizeof(*offscreenVerts));
351 }
352
353 for (v = 0; v < poly->verts; ++v) {
354 if (gx->vertexIndex == DS_GX_VERTEX_BUFFER_SIZE) {
355 return false;
356 }
357 // TODO: merge strips
358 vbuf[gx->vertexIndex] = inList[v];
359 poly->vertIds[v] = gx->vertexIndex;
360 ++gx->vertexIndex;
361 }
362
363 return newV > 2;
364}
365
366static int32_t _dotViewport(struct DSGXVertex* vertex, int32_t* col) {
367 int64_t a;
368 int64_t b;
369 int64_t sum;
370 a = col[0];
371 b = vertex->coord[0];
372 sum = a * b;
373 a = col[4];
374 b = vertex->coord[1];
375 sum += a * b;
376 a = col[8];
377 b = vertex->coord[2];
378 sum += a * b;
379 a = col[12];
380 b = MTX_ONE;
381 sum += a * b;
382 return sum >> 8LL;
383}
384
385static int16_t _dotTexture(struct DSGXVertex* vertex, int mode, int32_t* col) {
386 int64_t a;
387 int64_t b;
388 int64_t sum;
389 switch (mode) {
390 case 1:
391 a = col[0];
392 b = vertex->s << 8;
393 sum = a * b;
394 a = col[4];
395 b = vertex->t << 8;
396 sum += a * b;
397 a = col[8];
398 b = MTX_ONE >> 4;
399 sum += a * b;
400 a = col[12];
401 b = MTX_ONE >> 4;
402 sum += a * b;
403 break;
404 case 2:
405 return 0;
406 case 3:
407 a = col[0];
408 b = vertex->viewCoord[0] << 8;
409 sum = a * b;
410 a = col[4];
411 b = vertex->viewCoord[1] << 8;
412 sum += a * b;
413 a = col[8];
414 b = vertex->viewCoord[2] << 8;
415 sum += a * b;
416 a = col[12];
417 b = MTX_ONE;
418 sum += a * b;
419 }
420 return sum >> 20;
421}
422
423static int32_t _dotFrac(int16_t x, int16_t y, int16_t z, int32_t* col) {
424 int64_t a;
425 int64_t b;
426 int64_t sum;
427 a = col[0];
428 b = x;
429 sum = a * b;
430 a = col[4];
431 b = y;
432 sum += a * b;
433 a = col[8];
434 b = z;
435 sum += a * b;
436 return sum >> 12;
437}
438
439static int16_t _dot3(int32_t x0, int32_t y0, int32_t z0, int32_t x1, int32_t y1, int32_t z1) {
440 int32_t a = x0 * x1;
441 a += y0 * y1;
442 a += z0 * z1;
443 a >>= 12;
444 return a;
445}
446
447static void _emitVertex(struct DSGX* gx, uint16_t x, uint16_t y, uint16_t z) {
448 if (gx->vertexMode < 0 || gx->vertexIndex == DS_GX_VERTEX_BUFFER_SIZE || gx->polygonIndex == DS_GX_POLYGON_BUFFER_SIZE) {
449 return;
450 }
451 gx->currentVertex.coord[0] = x;
452 gx->currentVertex.coord[1] = y;
453 gx->currentVertex.coord[2] = z;
454 gx->currentVertex.viewCoord[0] = _dotViewport(&gx->currentVertex, &gx->clipMatrix.m[0]);
455 gx->currentVertex.viewCoord[1] = _dotViewport(&gx->currentVertex, &gx->clipMatrix.m[1]);
456 gx->currentVertex.viewCoord[2] = _dotViewport(&gx->currentVertex, &gx->clipMatrix.m[2]);
457 gx->currentVertex.viewCoord[3] = _dotViewport(&gx->currentVertex, &gx->clipMatrix.m[3]);
458
459 if (DSGXTexParamsGetCoordTfMode(gx->currentPoly.texParams) == 0) {
460 gx->currentVertex.vs = gx->currentVertex.s;
461 gx->currentVertex.vt = gx->currentVertex.t;
462 } else if (DSGXTexParamsGetCoordTfMode(gx->currentPoly.texParams) == 3) {
463 int32_t m12 = gx->texMatrix.m[12];
464 int32_t m13 = gx->texMatrix.m[13];
465 gx->texMatrix.m[12] = gx->currentVertex.s;
466 gx->texMatrix.m[13] = gx->currentVertex.t;
467 gx->currentVertex.vs = _dotTexture(&gx->currentVertex, 3, &gx->texMatrix.m[0]);
468 gx->currentVertex.vt = _dotTexture(&gx->currentVertex, 3, &gx->texMatrix.m[1]);
469 gx->texMatrix.m[12] = m12;
470 gx->texMatrix.m[13] = m13;
471 }
472
473 gx->pendingVertices[gx->pendingVertexIndex] = gx->currentVertex;
474 gx->currentPoly.vertIds[gx->currentPoly.verts] = gx->pendingVertexIndex;
475 gx->pendingVertexIndex = (gx->pendingVertexIndex + 1) & 3;
476
477 ++gx->currentPoly.verts;
478 int totalVertices;
479 switch (gx->vertexMode) {
480 case 0:
481 case 2:
482 totalVertices = 3;
483 break;
484 case 1:
485 case 3:
486 totalVertices = 4;
487 break;
488 }
489 if (gx->currentPoly.verts == totalVertices) {
490 struct DSGXPolygon* pbuf = gx->polygonBuffer[gx->bufferIndex];
491
492 pbuf[gx->polygonIndex] = gx->currentPoly;
493 switch (gx->vertexMode) {
494 case 0:
495 case 1:
496 gx->currentPoly.verts = 0;
497 break;
498 case 2:
499 // Reverse winding if needed
500 if (gx->reverseWinding) {
501 pbuf[gx->polygonIndex].vertIds[1] = gx->currentPoly.vertIds[2];
502 pbuf[gx->polygonIndex].vertIds[2] = gx->currentPoly.vertIds[1];
503 }
504 gx->reverseWinding = !gx->reverseWinding;
505 gx->currentPoly.vertIds[0] = gx->currentPoly.vertIds[1];
506 gx->currentPoly.vertIds[1] = gx->currentPoly.vertIds[2];
507 gx->currentPoly.verts = 2;
508 break;
509 case 3:
510 gx->currentPoly.vertIds[0] = gx->currentPoly.vertIds[2];
511 gx->currentPoly.vertIds[1] = gx->currentPoly.vertIds[3];
512 // Ensure quads don't cross over
513 pbuf[gx->polygonIndex].vertIds[2] = gx->currentPoly.vertIds[3];
514 pbuf[gx->polygonIndex].vertIds[3] = gx->currentPoly.vertIds[2];
515 gx->currentPoly.verts = 2;
516 break;
517 }
518
519 if (_clipPolygon(gx, &pbuf[gx->polygonIndex])) {
520 ++gx->polygonIndex;
521 }
522 if (gx->vertexMode < 2) {
523 memset(gx->pendingVertexIds, -1, sizeof(gx->pendingVertexIds));
524 } else {
525 gx->pendingVertexIds[gx->pendingVertexIndex] = -1;
526 gx->pendingVertexIds[(gx->pendingVertexIndex + 1) & 3] = -1;
527 }
528 }
529}
530
531static void _flushOutstanding(struct DSGX* gx) {
532 if (gx->p->cpuBlocked & DS_CPU_BLOCK_GX) {
533 gx->p->cpuBlocked &= ~DS_CPU_BLOCK_GX;
534 struct DSGXEntry entry = gx->outstandingEntry;
535 gx->outstandingEntry.command = 0;
536 DSGXWriteFIFO(gx, entry);
537 }
538 while (gx->outstandingCommand[0] && !gx->outstandingParams[0]) {
539 if (gx->p->cpuBlocked & DS_CPU_BLOCK_GX) {
540 return;
541 }
542 DSGXWriteFIFO(gx, (struct DSGXEntry) { gx->outstandingCommand[0] });
543 }
544}
545
546static bool _boxTest(struct DSGX* gx) {
547 int16_t x = gx->activeEntries[0].params[0];
548 x |= gx->activeEntries[0].params[1] << 8;
549 int16_t y = gx->activeEntries[0].params[2];
550 y |= gx->activeEntries[0].params[3] << 8;
551 int16_t z = gx->activeEntries[1].params[0];
552 z |= gx->activeEntries[1].params[1] << 8;
553 int16_t w = gx->activeEntries[1].params[2];
554 w |= gx->activeEntries[1].params[3] << 8;
555 int16_t h = gx->activeEntries[2].params[0];
556 h |= gx->activeEntries[2].params[1] << 8;
557 int16_t d = gx->activeEntries[2].params[2];
558 d |= gx->activeEntries[2].params[3] << 8;
559
560 struct DSGXVertex vertex[8] = {
561 { .coord = { x , y , z } },
562 { .coord = { x , y , z + d } },
563 { .coord = { x , y + h, z } },
564 { .coord = { x , y + h, z + d } },
565 { .coord = { x + w, y , z } },
566 { .coord = { x + w, y , z + d } },
567 { .coord = { x + w, y + h, z } },
568 { .coord = { x + w, y + h, z + d } },
569 };
570 int code[8];
571 int i;
572 for (i = 0; i < 8; ++i) {
573 vertex[i].viewCoord[0] = _dotViewport(&vertex[i], &gx->clipMatrix.m[0]);
574 vertex[i].viewCoord[1] = _dotViewport(&vertex[i], &gx->clipMatrix.m[1]);
575 vertex[i].viewCoord[2] = _dotViewport(&vertex[i], &gx->clipMatrix.m[2]);
576 vertex[i].viewCoord[3] = _dotViewport(&vertex[i], &gx->clipMatrix.m[3]);
577 code[i] = _cohenSutherlandCode(&vertex[i]);
578 }
579
580 if (!(code[0] & code[2] & code[4] & code[6])) {
581 return true;
582 }
583
584 if (!(code[1] & code[3] & code[5] & code[7])) {
585 return true;
586 }
587
588 if (!(code[0] & code[1] & code[4] & code[5])) {
589 return true;
590 }
591
592 if (!(code[2] & code[3] & code[6] & code[7])) {
593 return true;
594 }
595
596 if (!(code[0] & code[1] & code[2] & code[3])) {
597 return true;
598 }
599
600 if (!(code[4] & code[5] & code[6] & code[7])) {
601 return true;
602 }
603
604 return false;
605}
606
607static void _fifoRun(struct mTiming* timing, void* context, uint32_t cyclesLate) {
608 struct DSGX* gx = context;
609 uint32_t cycles;
610 bool first = true;
611 while (!gx->swapBuffers) {
612 if (CircleBufferSize(&gx->pipe) <= 2 * sizeof(struct DSGXEntry)) {
613 _pullPipe(gx);
614 }
615
616 if (!CircleBufferSize(&gx->pipe)) {
617 cycles = 0;
618 break;
619 }
620
621 DSRegGXSTAT gxstat = gx->p->memory.io9[DS9_REG_GXSTAT_LO >> 1];
622 int projMatrixPointer = DSRegGXSTATGetProjMatrixStackLevel(gxstat);
623
624 struct DSGXEntry entry = { 0 };
625 CircleBufferDump(&gx->pipe, (int8_t*) &entry.command, 1);
626 cycles = _gxCommandCycleBase[entry.command];
627
628 if (first) {
629 first = false;
630 } else if (!gx->activeParams && cycles > cyclesLate) {
631 break;
632 }
633 CircleBufferRead(&gx->pipe, &entry, sizeof(entry));
634
635 if (gx->activeParams) {
636 int index = _gxCommandParams[entry.command] - gx->activeParams;
637 gx->activeEntries[index] = entry;
638 --gx->activeParams;
639 } else {
640 gx->activeParams = _gxCommandParams[entry.command];
641 if (gx->activeParams) {
642 --gx->activeParams;
643 }
644 if (gx->activeParams) {
645 gx->activeEntries[0] = entry;
646 }
647 }
648
649 if (gx->activeParams) {
650 continue;
651 }
652
653 switch (entry.command) {
654 case DS_GX_CMD_MTX_MODE:
655 if (entry.params[0] < 4) {
656 gx->mtxMode = entry.params[0];
657 } else {
658 mLOG(DS_GX, GAME_ERROR, "Invalid GX MTX_MODE %02X", entry.params[0]);
659 }
660 break;
661 case DS_GX_CMD_MTX_PUSH:
662 switch (gx->mtxMode) {
663 case 0:
664 memcpy(&gx->projMatrixStack, &gx->projMatrix, sizeof(gx->projMatrix));
665 ++projMatrixPointer;
666 break;
667 case 1:
668 case 2:
669 memcpy(&gx->vecMatrixStack[gx->pvMatrixPointer & 0x1F], &gx->vecMatrix, sizeof(gx->vecMatrix));
670 memcpy(&gx->posMatrixStack[gx->pvMatrixPointer & 0x1F], &gx->posMatrix, sizeof(gx->posMatrix));
671 ++gx->pvMatrixPointer;
672 break;
673 case 3:
674 mLOG(DS_GX, STUB, "Unimplemented GX MTX_PUSH mode");
675 break;
676 }
677 break;
678 case DS_GX_CMD_MTX_POP: {
679 int8_t offset = entry.params[0];
680 offset <<= 2;
681 offset >>= 2;
682 switch (gx->mtxMode) {
683 case 0:
684 projMatrixPointer -= offset;
685 memcpy(&gx->projMatrix, &gx->projMatrixStack, sizeof(gx->projMatrix));
686 break;
687 case 1:
688 case 2:
689 gx->pvMatrixPointer -= offset;
690 memcpy(&gx->vecMatrix, &gx->vecMatrixStack[gx->pvMatrixPointer & 0x1F], sizeof(gx->vecMatrix));
691 memcpy(&gx->posMatrix, &gx->posMatrixStack[gx->pvMatrixPointer & 0x1F], sizeof(gx->posMatrix));
692 break;
693 case 3:
694 mLOG(DS_GX, STUB, "Unimplemented GX MTX_POP mode");
695 break;
696 }
697 _updateClipMatrix(gx);
698 break;
699 }
700 case DS_GX_CMD_MTX_STORE: {
701 int8_t offset = entry.params[0] & 0x1F;
702 // TODO: overflow
703 switch (gx->mtxMode) {
704 case 0:
705 memcpy(&gx->projMatrixStack, &gx->projMatrix, sizeof(gx->projMatrixStack));
706 break;
707 case 1:
708 case 2:
709 memcpy(&gx->vecMatrixStack[offset], &gx->vecMatrix, sizeof(gx->vecMatrix));
710 memcpy(&gx->posMatrixStack[offset], &gx->posMatrix, sizeof(gx->posMatrix));
711 break;
712 case 3:
713 mLOG(DS_GX, STUB, "Unimplemented GX MTX_STORE mode");
714 break;
715 }
716 break;
717 }
718 case DS_GX_CMD_MTX_RESTORE: {
719 int8_t offset = entry.params[0] & 0x1F;
720 // TODO: overflow
721 switch (gx->mtxMode) {
722 case 0:
723 memcpy(&gx->projMatrix, &gx->projMatrixStack, sizeof(gx->projMatrix));
724 break;
725 case 1:
726 case 2:
727 memcpy(&gx->vecMatrix, &gx->vecMatrixStack[offset], sizeof(gx->vecMatrix));
728 memcpy(&gx->posMatrix, &gx->posMatrixStack[offset], sizeof(gx->posMatrix));
729 break;
730 case 3:
731 mLOG(DS_GX, STUB, "Unimplemented GX MTX_RESTORE mode");
732 break;
733 }
734 _updateClipMatrix(gx);
735 break;
736 }
737 case DS_GX_CMD_MTX_IDENTITY:
738 switch (gx->mtxMode) {
739 case 0:
740 DSGXMtxIdentity(&gx->projMatrix);
741 break;
742 case 2:
743 DSGXMtxIdentity(&gx->vecMatrix);
744 // Fall through
745 case 1:
746 DSGXMtxIdentity(&gx->posMatrix);
747 break;
748 case 3:
749 DSGXMtxIdentity(&gx->texMatrix);
750 break;
751 }
752 _updateClipMatrix(gx);
753 break;
754 case DS_GX_CMD_MTX_LOAD_4x4: {
755 struct DSGXMatrix m;
756 int i;
757 for (i = 0; i < 16; ++i) {
758 m.m[i] = gx->activeEntries[i].params[0];
759 m.m[i] |= gx->activeEntries[i].params[1] << 8;
760 m.m[i] |= gx->activeEntries[i].params[2] << 16;
761 m.m[i] |= gx->activeEntries[i].params[3] << 24;
762 }
763 switch (gx->mtxMode) {
764 case 0:
765 memcpy(&gx->projMatrix, &m, sizeof(gx->projMatrix));
766 break;
767 case 2:
768 memcpy(&gx->vecMatrix, &m, sizeof(gx->vecMatrix));
769 // Fall through
770 case 1:
771 memcpy(&gx->posMatrix, &m, sizeof(gx->posMatrix));
772 break;
773 case 3:
774 memcpy(&gx->texMatrix, &m, sizeof(gx->texMatrix));
775 break;
776 }
777 _updateClipMatrix(gx);
778 break;
779 }
780 case DS_GX_CMD_MTX_LOAD_4x3: {
781 struct DSGXMatrix m;
782 int i, j;
783 for (j = 0; j < 4; ++j) {
784 for (i = 0; i < 3; ++i) {
785 m.m[i + j * 4] = gx->activeEntries[i + j * 3].params[0];
786 m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[1] << 8;
787 m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[2] << 16;
788 m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[3] << 24;
789 }
790 m.m[j * 4 + 3] = 0;
791 }
792 m.m[15] = MTX_ONE;
793 switch (gx->mtxMode) {
794 case 0:
795 memcpy(&gx->projMatrix, &m, sizeof(gx->projMatrix));
796 break;
797 case 2:
798 memcpy(&gx->vecMatrix, &m, sizeof(gx->vecMatrix));
799 // Fall through
800 case 1:
801 memcpy(&gx->posMatrix, &m, sizeof(gx->posMatrix));
802 break;
803 case 3:
804 memcpy(&gx->texMatrix, &m, sizeof(gx->texMatrix));
805 break;
806 }
807 _updateClipMatrix(gx);
808 break;
809 }
810 case DS_GX_CMD_MTX_MULT_4x4: {
811 struct DSGXMatrix m;
812 int i;
813 for (i = 0; i < 16; ++i) {
814 m.m[i] = gx->activeEntries[i].params[0];
815 m.m[i] |= gx->activeEntries[i].params[1] << 8;
816 m.m[i] |= gx->activeEntries[i].params[2] << 16;
817 m.m[i] |= gx->activeEntries[i].params[3] << 24;
818 }
819 switch (gx->mtxMode) {
820 case 0:
821 DSGXMtxMultiply(&gx->projMatrix, &m, &gx->projMatrix);
822 break;
823 case 2:
824 DSGXMtxMultiply(&gx->vecMatrix, &m, &gx->vecMatrix);
825 // Fall through
826 case 1:
827 DSGXMtxMultiply(&gx->posMatrix, &m, &gx->posMatrix);
828 break;
829 case 3:
830 DSGXMtxMultiply(&gx->texMatrix, &m, &gx->texMatrix);
831 break;
832 }
833 _updateClipMatrix(gx);
834 break;
835 }
836 case DS_GX_CMD_MTX_MULT_4x3: {
837 struct DSGXMatrix m;
838 int i, j;
839 for (j = 0; j < 4; ++j) {
840 for (i = 0; i < 3; ++i) {
841 m.m[i + j * 4] = gx->activeEntries[i + j * 3].params[0];
842 m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[1] << 8;
843 m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[2] << 16;
844 m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[3] << 24;
845 }
846 m.m[j * 4 + 3] = 0;
847 }
848 m.m[15] = MTX_ONE;
849 switch (gx->mtxMode) {
850 case 0:
851 DSGXMtxMultiply(&gx->projMatrix, &m, &gx->projMatrix);
852 break;
853 case 2:
854 DSGXMtxMultiply(&gx->vecMatrix, &m, &gx->vecMatrix);
855 // Fall through
856 case 1:
857 DSGXMtxMultiply(&gx->posMatrix, &m, &gx->posMatrix);
858 break;
859 case 3:
860 DSGXMtxMultiply(&gx->texMatrix, &m, &gx->texMatrix);
861 break;
862 }
863 _updateClipMatrix(gx);
864 break;
865 }
866 case DS_GX_CMD_MTX_MULT_3x3: {
867 struct DSGXMatrix m;
868 int i, j;
869 for (j = 0; j < 3; ++j) {
870 for (i = 0; i < 3; ++i) {
871 m.m[i + j * 4] = gx->activeEntries[i + j * 3].params[0];
872 m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[1] << 8;
873 m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[2] << 16;
874 m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[3] << 24;
875 }
876 m.m[j * 4 + 3] = 0;
877 }
878 m.m[12] = 0;
879 m.m[13] = 0;
880 m.m[14] = 0;
881 m.m[15] = MTX_ONE;
882 switch (gx->mtxMode) {
883 case 0:
884 DSGXMtxMultiply(&gx->projMatrix, &m, &gx->projMatrix);
885 break;
886 case 2:
887 DSGXMtxMultiply(&gx->vecMatrix, &m, &gx->vecMatrix);
888 // Fall through
889 case 1:
890 DSGXMtxMultiply(&gx->posMatrix, &m, &gx->posMatrix);
891 break;
892 case 3:
893 DSGXMtxMultiply(&gx->texMatrix, &m, &gx->texMatrix);
894 break;
895 }
896 _updateClipMatrix(gx);
897 break;
898 }
899 case DS_GX_CMD_MTX_TRANS: {
900 int32_t m[3];
901 m[0] = gx->activeEntries[0].params[0];
902 m[0] |= gx->activeEntries[0].params[1] << 8;
903 m[0] |= gx->activeEntries[0].params[2] << 16;
904 m[0] |= gx->activeEntries[0].params[3] << 24;
905 m[1] = gx->activeEntries[1].params[0];
906 m[1] |= gx->activeEntries[1].params[1] << 8;
907 m[1] |= gx->activeEntries[1].params[2] << 16;
908 m[1] |= gx->activeEntries[1].params[3] << 24;
909 m[2] = gx->activeEntries[2].params[0];
910 m[2] |= gx->activeEntries[2].params[1] << 8;
911 m[2] |= gx->activeEntries[2].params[2] << 16;
912 m[2] |= gx->activeEntries[2].params[3] << 24;
913 switch (gx->mtxMode) {
914 case 0:
915 DSGXMtxTranslate(&gx->projMatrix, m);
916 break;
917 case 2:
918 DSGXMtxTranslate(&gx->vecMatrix, m);
919 // Fall through
920 case 1:
921 DSGXMtxTranslate(&gx->posMatrix, m);
922 break;
923 case 3:
924 DSGXMtxTranslate(&gx->texMatrix, m);
925 break;
926 }
927 _updateClipMatrix(gx);
928 break;
929 }
930 case DS_GX_CMD_MTX_SCALE: {
931 int32_t m[3];
932 m[0] = gx->activeEntries[0].params[0];
933 m[0] |= gx->activeEntries[0].params[1] << 8;
934 m[0] |= gx->activeEntries[0].params[2] << 16;
935 m[0] |= gx->activeEntries[0].params[3] << 24;
936 m[1] = gx->activeEntries[1].params[0];
937 m[1] |= gx->activeEntries[1].params[1] << 8;
938 m[1] |= gx->activeEntries[1].params[2] << 16;
939 m[1] |= gx->activeEntries[1].params[3] << 24;
940 m[2] = gx->activeEntries[2].params[0];
941 m[2] |= gx->activeEntries[2].params[1] << 8;
942 m[2] |= gx->activeEntries[2].params[2] << 16;
943 m[2] |= gx->activeEntries[2].params[3] << 24;
944 switch (gx->mtxMode) {
945 case 0:
946 DSGXMtxScale(&gx->projMatrix, m);
947 break;
948 case 1:
949 case 2:
950 DSGXMtxScale(&gx->posMatrix, m);
951 break;
952 case 3:
953 DSGXMtxScale(&gx->texMatrix, m);
954 break;
955 }
956 _updateClipMatrix(gx);
957 break;
958 }
959 case DS_GX_CMD_COLOR:
960 gx->currentVertex.color = entry.params[0];
961 gx->currentVertex.color |= entry.params[1] << 8;
962 break;
963 case DS_GX_CMD_NORMAL: {
964 int32_t xyz = entry.params[0];
965 xyz |= entry.params[1] << 8;
966 xyz |= entry.params[2] << 16;
967 xyz |= entry.params[3] << 24;
968 int16_t x = (xyz << 6) & 0xFFC0;
969 int16_t y = (xyz >> 4) & 0xFFC0;
970 int16_t z = (xyz >> 14) & 0xFFC0;
971 x >>= 3;
972 y >>= 3;
973 z >>= 3;
974 if (DSGXTexParamsGetCoordTfMode(gx->currentPoly.texParams) == 2) {
975 gx->currentVertex.vs = (_dotFrac(x, y, z, &gx->texMatrix.m[0]) + gx->currentVertex.s) >> 11;
976 gx->currentVertex.vt = (_dotFrac(x, y, z, &gx->texMatrix.m[1]) + gx->currentVertex.t) >> 11;
977 }
978 int16_t nx = _dotFrac(x, y, z, &gx->vecMatrix.m[0]);
979 int16_t ny = _dotFrac(x, y, z, &gx->vecMatrix.m[1]);
980 int16_t nz = _dotFrac(x, y, z, &gx->vecMatrix.m[2]);
981 int r = gx->emit & 0x1F;
982 int g = (gx->emit >> 5) & 0x1F;
983 int b = (gx->emit >> 10) & 0x1F;
984 int i;
985 for (i = 0; i < 4; ++i) {
986 if (!(DSGXPolygonAttrsGetLights(gx->currentPoly.polyParams) & (1 << i))) {
987 continue;
988 }
989 struct DSGXLight* light = &gx->lights[i];
990 int diffuse = -_dot3(light->x, light->y, light->z, nx, ny, nz);
991 if (diffuse < 0) {
992 diffuse = 0;
993 }
994 int specular = -_dot3(-light->x >> 1, -light->y >> 1, (0x1000 - light->z) >> 1, nx, ny, nz);
995 if (specular < 0) {
996 specular = 0;
997 } else {
998 specular = 2 * specular * specular - (1 << 10);
999 }
1000 unsigned lr = (light->color) & 0x1F;
1001 unsigned lg = (light->color >> 5) & 0x1F;
1002 unsigned lb = (light->color >> 10) & 0x1F;
1003 unsigned xr, xg, xb;
1004 xr = gx->specular & 0x1F;
1005 xg = (gx->specular >> 5) & 0x1F;
1006 xb = (gx->specular >> 10) & 0x1F;
1007 r += (specular * xr * lr) >> 17;
1008 g += (specular * xg * lg) >> 17;
1009 b += (specular * xb * lb) >> 17;
1010 xr = gx->diffuse & 0x1F;
1011 xg = (gx->diffuse >> 5) & 0x1F;
1012 xb = (gx->diffuse >> 10) & 0x1F;
1013 r += (diffuse * xr * lr) >> 17;
1014 g += (diffuse * xg * lg) >> 17;
1015 b += (diffuse * xb * lb) >> 17;
1016 xr = gx->ambient & 0x1F;
1017 xg = (gx->ambient >> 5) & 0x1F;
1018 xb = (gx->ambient >> 10) & 0x1F;
1019 r += (xr * lr) >> 5;
1020 g += (xg * lg) >> 5;
1021 b += (xb * lb) >> 5;
1022 }
1023 if (r < 0) {
1024 r = 0;
1025 } else if (r > 0x1F) {
1026 r = 0x1F;
1027 }
1028 if (g < 0) {
1029 g = 0;
1030 } else if (g > 0x1F) {
1031 g = 0x1F;
1032 }
1033 if (b < 0) {
1034 b = 0;
1035 } else if (b > 0x1F) {
1036 b = 0x1F;
1037 }
1038 gx->currentVertex.color = r | (g << 5) | (b << 10);
1039 break;
1040 }
1041 case DS_GX_CMD_TEXCOORD:
1042 gx->currentVertex.s = entry.params[0];
1043 gx->currentVertex.s |= entry.params[1] << 8;
1044 gx->currentVertex.t = entry.params[2];
1045 gx->currentVertex.t |= entry.params[3] << 8;
1046 if (DSGXTexParamsGetCoordTfMode(gx->currentPoly.texParams) == 1) {
1047 gx->currentVertex.vs = _dotTexture(&gx->currentVertex, 1, &gx->texMatrix.m[0]);
1048 gx->currentVertex.vt = _dotTexture(&gx->currentVertex, 1, &gx->texMatrix.m[1]);
1049 }
1050 break;
1051 case DS_GX_CMD_VTX_16: {
1052 int16_t x = gx->activeEntries[0].params[0];
1053 x |= gx->activeEntries[0].params[1] << 8;
1054 int16_t y = gx->activeEntries[0].params[2];
1055 y |= gx->activeEntries[0].params[3] << 8;
1056 int16_t z = gx->activeEntries[1].params[0];
1057 z |= gx->activeEntries[1].params[1] << 8;
1058 _emitVertex(gx, x, y, z);
1059 break;
1060 }
1061 case DS_GX_CMD_VTX_10: {
1062 int32_t xyz = entry.params[0];
1063 xyz |= entry.params[1] << 8;
1064 xyz |= entry.params[2] << 16;
1065 xyz |= entry.params[3] << 24;
1066 int16_t x = (xyz << 6) & 0xFFC0;
1067 int16_t y = (xyz >> 4) & 0xFFC0;
1068 int16_t z = (xyz >> 14) & 0xFFC0;
1069 _emitVertex(gx, x, y, z);
1070 break;
1071 }
1072 case DS_GX_CMD_VTX_XY: {
1073 int16_t x = entry.params[0];
1074 x |= entry.params[1] << 8;
1075 int16_t y = entry.params[2];
1076 y |= entry.params[3] << 8;
1077 _emitVertex(gx, x, y, gx->currentVertex.coord[2]);
1078 break;
1079 }
1080 case DS_GX_CMD_VTX_XZ: {
1081 int16_t x = entry.params[0];
1082 x |= entry.params[1] << 8;
1083 int16_t z = entry.params[2];
1084 z |= entry.params[3] << 8;
1085 _emitVertex(gx, x, gx->currentVertex.coord[1], z);
1086 break;
1087 }
1088 case DS_GX_CMD_VTX_YZ: {
1089 int16_t y = entry.params[0];
1090 y |= entry.params[1] << 8;
1091 int16_t z = entry.params[2];
1092 z |= entry.params[3] << 8;
1093 _emitVertex(gx, gx->currentVertex.coord[0], y, z);
1094 break;
1095 }
1096 case DS_GX_CMD_VTX_DIFF: {
1097 int32_t xyz = entry.params[0];
1098 xyz |= entry.params[1] << 8;
1099 xyz |= entry.params[2] << 16;
1100 xyz |= entry.params[3] << 24;
1101 int16_t x = (xyz << 6) & 0xFFC0;
1102 int16_t y = (xyz >> 4) & 0xFFC0;
1103 int16_t z = (xyz >> 14) & 0xFFC0;
1104 _emitVertex(gx, gx->currentVertex.coord[0] + (x >> 6),
1105 gx->currentVertex.coord[1] + (y >> 6),
1106 gx->currentVertex.coord[2] + (z >> 6));
1107 break;
1108 }
1109 case DS_GX_CMD_DIF_AMB:
1110 gx->diffuse = entry.params[0];
1111 gx->diffuse |= entry.params[1] << 8;
1112 if (gx->diffuse & 0x8000) {
1113 gx->currentVertex.color = gx->diffuse;
1114 }
1115 gx->ambient = entry.params[2];
1116 gx->ambient |= entry.params[3] << 8;
1117 break;
1118 case DS_GX_CMD_SPE_EMI:
1119 gx->specular = entry.params[0];
1120 gx->specular |= entry.params[1] << 8;
1121 gx->emit = entry.params[2];
1122 gx->emit |= entry.params[3] << 8;
1123 break;
1124 case DS_GX_CMD_LIGHT_VECTOR: {
1125 uint32_t xyz = entry.params[0];
1126 xyz |= entry.params[1] << 8;
1127 xyz |= entry.params[2] << 16;
1128 xyz |= entry.params[3] << 24;
1129 struct DSGXLight* light = &gx->lights[xyz >> 30];
1130 int16_t x = (xyz << 6) & 0xFFC0;
1131 int16_t y = (xyz >> 4) & 0xFFC0;
1132 int16_t z = (xyz >> 14) & 0xFFC0;
1133 x >>= 3;
1134 y >>= 3;
1135 z >>= 3;
1136 light->x = _dotFrac(x, y, z, &gx->vecMatrix.m[0]);
1137 light->y = _dotFrac(x, y, z, &gx->vecMatrix.m[1]);
1138 light->z = _dotFrac(x, y, z, &gx->vecMatrix.m[2]);
1139 break;
1140 }
1141 case DS_GX_CMD_LIGHT_COLOR: {
1142 struct DSGXLight* light = &gx->lights[entry.params[3] >> 6];
1143 light->color = entry.params[0];
1144 light->color |= entry.params[1] << 8;
1145 break;
1146 }
1147 case DS_GX_CMD_POLYGON_ATTR:
1148 gx->nextPoly.polyParams = entry.params[0];
1149 gx->nextPoly.polyParams |= entry.params[1] << 8;
1150 gx->nextPoly.polyParams |= entry.params[2] << 16;
1151 gx->nextPoly.polyParams |= entry.params[3] << 24;
1152 break;
1153 case DS_GX_CMD_TEXIMAGE_PARAM:
1154 gx->nextPoly.texParams = entry.params[0];
1155 gx->nextPoly.texParams |= entry.params[1] << 8;
1156 gx->nextPoly.texParams |= entry.params[2] << 16;
1157 gx->nextPoly.texParams |= entry.params[3] << 24;
1158 gx->currentPoly.texParams = gx->nextPoly.texParams;
1159 break;
1160 case DS_GX_CMD_PLTT_BASE:
1161 gx->nextPoly.palBase = entry.params[0];
1162 gx->nextPoly.palBase |= entry.params[1] << 8;
1163 gx->nextPoly.palBase |= entry.params[2] << 16;
1164 gx->nextPoly.palBase |= entry.params[3] << 24;
1165 gx->currentPoly.palBase = gx->nextPoly.palBase;
1166 break;
1167 case DS_GX_CMD_BEGIN_VTXS:
1168 gx->vertexMode = entry.params[0] & 3;
1169 gx->currentPoly = gx->nextPoly;
1170 gx->reverseWinding = false;
1171 memset(gx->pendingVertexIds, -1, sizeof(gx->pendingVertexIds));
1172 break;
1173 case DS_GX_CMD_END_VTXS:
1174 gx->vertexMode = -1;
1175 break;
1176 case DS_GX_CMD_SWAP_BUFFERS:
1177 gx->swapBuffers = true;
1178 gx->wSort = entry.params[0] & 2;
1179 break;
1180 case DS_GX_CMD_VIEWPORT:
1181 gx->viewportX1 = (uint8_t) entry.params[0];
1182 gx->viewportY1 = (uint8_t) entry.params[1];
1183 gx->viewportX2 = (uint8_t) entry.params[2];
1184 gx->viewportY2 = (uint8_t) entry.params[3];
1185 gx->viewportWidth = gx->viewportX2 - gx->viewportX1 + 1;
1186 gx->viewportHeight = gx->viewportY2 - gx->viewportY1 + 1;
1187 gx->currentVertex.viewportX = gx->viewportX1;
1188 gx->currentVertex.viewportY = gx->viewportY1;
1189 gx->currentVertex.viewportWidth = gx->viewportWidth;
1190 gx->currentVertex.viewportHeight = gx->viewportHeight;
1191 break;
1192 case DS_GX_CMD_BOX_TEST:
1193 gxstat = DSRegGXSTATClearTestBusy(gxstat);
1194 gxstat = DSRegGXSTATTestFillBoxTestResult(gxstat, _boxTest(gx));
1195 break;
1196 default:
1197 mLOG(DS_GX, STUB, "Unimplemented GX command %02X:%02X %02X %02X %02X", entry.command, entry.params[0], entry.params[1], entry.params[2], entry.params[3]);
1198 break;
1199 }
1200
1201 gxstat = DSRegGXSTATSetPVMatrixStackLevel(gxstat, gx->pvMatrixPointer);
1202 gxstat = DSRegGXSTATSetProjMatrixStackLevel(gxstat, projMatrixPointer);
1203 gxstat = DSRegGXSTATTestFillMatrixStackError(gxstat, projMatrixPointer || gx->pvMatrixPointer >= 0x1F);
1204 gx->p->memory.io9[DS9_REG_GXSTAT_LO >> 1] = gxstat;
1205
1206 if (cyclesLate >= cycles) {
1207 cyclesLate -= cycles;
1208 } else {
1209 break;
1210 }
1211 }
1212 if (cycles && !gx->swapBuffers) {
1213 mTimingSchedule(timing, &gx->fifoEvent, cycles - cyclesLate);
1214 }
1215 if (CircleBufferSize(&gx->fifo) < (DS_GX_FIFO_SIZE * sizeof(struct DSGXEntry))) {
1216 _flushOutstanding(gx);
1217 }
1218 DSGXUpdateGXSTAT(gx);
1219}
1220
1221void DSGXInit(struct DSGX* gx) {
1222 gx->renderer = &dummyRenderer;
1223 CircleBufferInit(&gx->fifo, sizeof(struct DSGXEntry) * DS_GX_FIFO_SIZE);
1224 CircleBufferInit(&gx->pipe, sizeof(struct DSGXEntry) * DS_GX_PIPE_SIZE);
1225 gx->vertexBuffer[0] = malloc(sizeof(struct DSGXVertex) * DS_GX_VERTEX_BUFFER_SIZE);
1226 gx->vertexBuffer[1] = malloc(sizeof(struct DSGXVertex) * DS_GX_VERTEX_BUFFER_SIZE);
1227 gx->polygonBuffer[0] = malloc(sizeof(struct DSGXPolygon) * DS_GX_POLYGON_BUFFER_SIZE);
1228 gx->polygonBuffer[1] = malloc(sizeof(struct DSGXPolygon) * DS_GX_POLYGON_BUFFER_SIZE);
1229 gx->fifoEvent.name = "DS GX FIFO";
1230 gx->fifoEvent.priority = 0xC;
1231 gx->fifoEvent.context = gx;
1232 gx->fifoEvent.callback = _fifoRun;
1233}
1234
1235void DSGXDeinit(struct DSGX* gx) {
1236 DSGXAssociateRenderer(gx, &dummyRenderer);
1237 CircleBufferDeinit(&gx->fifo);
1238 CircleBufferDeinit(&gx->pipe);
1239 free(gx->vertexBuffer[0]);
1240 free(gx->vertexBuffer[1]);
1241 free(gx->polygonBuffer[0]);
1242 free(gx->polygonBuffer[1]);
1243}
1244
1245void DSGXReset(struct DSGX* gx) {
1246 CircleBufferClear(&gx->fifo);
1247 CircleBufferClear(&gx->pipe);
1248 DSGXMtxIdentity(&gx->projMatrix);
1249 DSGXMtxIdentity(&gx->texMatrix);
1250 DSGXMtxIdentity(&gx->posMatrix);
1251 DSGXMtxIdentity(&gx->vecMatrix);
1252
1253 DSGXMtxIdentity(&gx->clipMatrix);
1254 DSGXMtxIdentity(&gx->projMatrixStack);
1255 DSGXMtxIdentity(&gx->texMatrixStack);
1256 int i;
1257 for (i = 0; i < 32; ++i) {
1258 DSGXMtxIdentity(&gx->posMatrixStack[i]);
1259 DSGXMtxIdentity(&gx->vecMatrixStack[i]);
1260 }
1261 gx->swapBuffers = false;
1262 gx->bufferIndex = 0;
1263 gx->vertexIndex = 0;
1264 gx->polygonIndex = 0;
1265 gx->mtxMode = 0;
1266 gx->pvMatrixPointer = 0;
1267 gx->vertexMode = -1;
1268
1269 gx->viewportX1 = 0;
1270 gx->viewportY1 = 0;
1271 gx->viewportX2 = DS_VIDEO_HORIZONTAL_PIXELS - 1;
1272 gx->viewportY2 = DS_VIDEO_VERTICAL_PIXELS - 1;
1273 gx->viewportWidth = gx->viewportX2 - gx->viewportX1 + 1;
1274 gx->viewportHeight = gx->viewportY2 - gx->viewportY1 + 1;
1275
1276 memset(gx->outstandingParams, 0, sizeof(gx->outstandingParams));
1277 memset(gx->outstandingCommand, 0, sizeof(gx->outstandingCommand));
1278 memset(&gx->outstandingEntry, 0, sizeof(gx->outstandingEntry));
1279 gx->activeParams = 0;
1280 memset(&gx->currentVertex, 0, sizeof(gx->currentVertex));
1281 memset(&gx->nextPoly, 0, sizeof(gx-> nextPoly));
1282 gx->currentVertex.color = 0x7FFF;
1283 gx->currentPoly.polyParams = 0x001F00C0;
1284 gx->nextPoly.polyParams = 0x001F00C0;
1285 gx->dmaSource = -1;
1286}
1287
1288void DSGXAssociateRenderer(struct DSGX* gx, struct DSGXRenderer* renderer) {
1289 gx->renderer->deinit(gx->renderer);
1290 gx->renderer = renderer;
1291 memcpy(gx->renderer->tex, gx->tex, sizeof(gx->renderer->tex));
1292 memcpy(gx->renderer->texPal, gx->texPal, sizeof(gx->renderer->texPal));
1293 gx->renderer->init(gx->renderer);
1294}
1295
1296void DSGXUpdateGXSTAT(struct DSGX* gx) {
1297 uint32_t value = gx->p->memory.io9[DS9_REG_GXSTAT_HI >> 1] << 16;
1298 value = DSRegGXSTATIsDoIRQ(value);
1299
1300 size_t entries = CircleBufferSize(&gx->fifo) / sizeof(struct DSGXEntry);
1301 // XXX
1302 if (gx->swapBuffers) {
1303 entries++;
1304 }
1305 value = DSRegGXSTATSetFIFOEntries(value, entries);
1306 value = DSRegGXSTATSetFIFOLtHalf(value, entries < (DS_GX_FIFO_SIZE / 2));
1307 value = DSRegGXSTATSetFIFOEmpty(value, entries == 0);
1308
1309 if ((DSRegGXSTATGetDoIRQ(value) == 1 && entries < (DS_GX_FIFO_SIZE / 2)) ||
1310 (DSRegGXSTATGetDoIRQ(value) == 2 && entries == 0)) {
1311 DSRaiseIRQ(gx->p->ds9.cpu, gx->p->ds9.memory.io, DS_IRQ_GEOM_FIFO);
1312 }
1313
1314 value = DSRegGXSTATSetBusy(value, mTimingIsScheduled(&gx->p->ds9.timing, &gx->fifoEvent) || gx->swapBuffers);
1315
1316 gx->p->memory.io9[DS9_REG_GXSTAT_HI >> 1] = value >> 16;
1317
1318 struct GBADMA* dma = NULL;
1319 if (gx->dmaSource >= 0) {
1320 dma = &gx->p->ds9.memory.dma[gx->dmaSource];
1321 if (GBADMARegisterGetTiming9(dma->reg) != DS_DMA_TIMING_GEOM_FIFO) {
1322 gx->dmaSource = -1;
1323 } else if (GBADMARegisterIsEnable(dma->reg) && entries < (DS_GX_FIFO_SIZE / 2) && !dma->nextCount) {
1324 dma->nextCount = dma->count;
1325 dma->when = mTimingCurrentTime(&gx->p->ds9.timing);
1326 DSDMAUpdate(&gx->p->ds9);
1327 }
1328 }
1329}
1330
1331static void DSGXUnpackCommand(struct DSGX* gx, uint32_t command) {
1332 gx->outstandingCommand[0] = command;
1333 gx->outstandingCommand[1] = command >> 8;
1334 gx->outstandingCommand[2] = command >> 16;
1335 gx->outstandingCommand[3] = command >> 24;
1336 if (gx->outstandingCommand[0] >= DS_GX_CMD_MAX) {
1337 gx->outstandingCommand[0] = 0;
1338 }
1339 if (gx->outstandingCommand[1] >= DS_GX_CMD_MAX) {
1340 gx->outstandingCommand[1] = 0;
1341 }
1342 if (gx->outstandingCommand[2] >= DS_GX_CMD_MAX) {
1343 gx->outstandingCommand[2] = 0;
1344 }
1345 if (gx->outstandingCommand[3] >= DS_GX_CMD_MAX) {
1346 gx->outstandingCommand[3] = 0;
1347 }
1348 if (!_gxCommandCycleBase[gx->outstandingCommand[0]]) {
1349 gx->outstandingCommand[0] = gx->outstandingCommand[1];
1350 gx->outstandingCommand[1] = gx->outstandingCommand[2];
1351 gx->outstandingCommand[2] = gx->outstandingCommand[3];
1352 gx->outstandingCommand[3] = 0;
1353 }
1354 if (!_gxCommandCycleBase[gx->outstandingCommand[1]]) {
1355 gx->outstandingCommand[1] = gx->outstandingCommand[2];
1356 gx->outstandingCommand[2] = gx->outstandingCommand[3];
1357 gx->outstandingCommand[3] = 0;
1358 }
1359 if (!_gxCommandCycleBase[gx->outstandingCommand[2]]) {
1360 gx->outstandingCommand[2] = gx->outstandingCommand[3];
1361 gx->outstandingCommand[3] = 0;
1362 }
1363 gx->outstandingParams[0] = _gxCommandParams[gx->outstandingCommand[0]];
1364 gx->outstandingParams[1] = _gxCommandParams[gx->outstandingCommand[1]];
1365 gx->outstandingParams[2] = _gxCommandParams[gx->outstandingCommand[2]];
1366 gx->outstandingParams[3] = _gxCommandParams[gx->outstandingCommand[3]];
1367 _flushOutstanding(gx);
1368 DSGXUpdateGXSTAT(gx);
1369}
1370
1371static void DSGXWriteFIFO(struct DSGX* gx, struct DSGXEntry entry) {
1372 if (CircleBufferSize(&gx->fifo) == (DS_GX_FIFO_SIZE * sizeof(entry))) {
1373 mLOG(DS_GX, INFO, "FIFO full");
1374 while (gx->p->cpuBlocked & DS_CPU_BLOCK_GX) {
1375 // Can happen from STM
1376 if (gx->swapBuffers) {
1377 // XXX: Let's hope those GX entries aren't important, since STM isn't preemptable
1378 mLOG(DS_GX, ERROR, "FIFO full with swap pending");
1379 return;
1380 }
1381 mTimingDeschedule(&gx->p->ds9.timing, &gx->fifoEvent);
1382 _fifoRun(&gx->p->ds9.timing, gx, 0);
1383 }
1384 gx->p->cpuBlocked |= DS_CPU_BLOCK_GX;
1385 gx->outstandingEntry = entry;
1386 gx->p->ds9.cpu->nextEvent = 0;
1387 return;
1388 }
1389 if (gx->outstandingCommand[0]) {
1390 entry.command = gx->outstandingCommand[0];
1391 if (gx->outstandingParams[0]) {
1392 --gx->outstandingParams[0];
1393 }
1394 if (!gx->outstandingParams[0]) {
1395 // TODO: improve this
1396 memmove(&gx->outstandingParams[0], &gx->outstandingParams[1], sizeof(gx->outstandingParams[0]) * 3);
1397 memmove(&gx->outstandingCommand[0], &gx->outstandingCommand[1], sizeof(gx->outstandingCommand[0]) * 3);
1398 gx->outstandingParams[3] = 0;
1399 gx->outstandingCommand[3] = 0;
1400 }
1401 } else {
1402 gx->outstandingParams[0] = _gxCommandParams[entry.command];
1403 if (gx->outstandingParams[0]) {
1404 --gx->outstandingParams[0];
1405 }
1406 if (gx->outstandingParams[0]) {
1407 gx->outstandingCommand[0] = entry.command;
1408 }
1409 }
1410 uint32_t cycles = _gxCommandCycleBase[entry.command];
1411 if (!cycles) {
1412 return;
1413 }
1414 if (CircleBufferSize(&gx->fifo) == 0 && CircleBufferSize(&gx->pipe) < (DS_GX_PIPE_SIZE * sizeof(entry))) {
1415 CircleBufferWrite(&gx->pipe, &entry, sizeof(entry));
1416 } else if (CircleBufferSize(&gx->fifo) < (DS_GX_FIFO_SIZE * sizeof(entry))) {
1417 CircleBufferWrite(&gx->fifo, &entry, sizeof(entry));
1418 }
1419 if (entry.command == DS_GX_CMD_BOX_TEST) {
1420 DSRegGXSTAT gxstat = gx->p->memory.io9[DS9_REG_GXSTAT_LO >> 1];
1421 gxstat = DSRegGXSTATFillTestBusy(gxstat);
1422 gxstat = DSRegGXSTATClearBoxTestResult(gxstat);
1423 gx->p->memory.io9[DS9_REG_GXSTAT_LO >> 1] = gxstat;
1424 }
1425 if (!gx->swapBuffers && !mTimingIsScheduled(&gx->p->ds9.timing, &gx->fifoEvent)) {
1426 mTimingSchedule(&gx->p->ds9.timing, &gx->fifoEvent, cycles);
1427 }
1428
1429 _flushOutstanding(gx);
1430}
1431
1432uint16_t DSGXWriteRegister(struct DSGX* gx, uint32_t address, uint16_t value) {
1433 uint16_t oldValue = gx->p->memory.io9[address >> 1];
1434 switch (address) {
1435 case DS9_REG_DISP3DCNT:
1436 mLOG(DS_GX, STUB, "Unimplemented GX write %03X:%04X", address, value);
1437 break;
1438 case DS9_REG_CLEAR_COLOR_LO:
1439 case DS9_REG_CLEAR_COLOR_HI:
1440 gx->renderer->writeRegister(gx->renderer, address, value);
1441 break;
1442 case DS9_REG_GXSTAT_LO:
1443 value = DSRegGXSTATIsMatrixStackError(value);
1444 if (value) {
1445 oldValue = DSRegGXSTATClearMatrixStackError(oldValue);
1446 oldValue = DSRegGXSTATClearProjMatrixStackLevel(oldValue);
1447 }
1448 value = oldValue;
1449 break;
1450 case DS9_REG_GXSTAT_HI:
1451 value = DSRegGXSTATIsDoIRQ(value << 16) >> 16;
1452 gx->p->memory.io9[address >> 1] = value;
1453 DSGXUpdateGXSTAT(gx);
1454 value = gx->p->memory.io9[address >> 1];
1455 break;
1456 default:
1457 if (address < DS9_REG_GXFIFO_00) {
1458 mLOG(DS_GX, STUB, "Unimplemented GX write %03X:%04X", address, value);
1459 } else if (address <= DS9_REG_GXFIFO_1F) {
1460 mLOG(DS_GX, STUB, "Unimplemented GX write %03X:%04X", address, value);
1461 } else if (address < DS9_REG_GXSTAT_LO) {
1462 struct DSGXEntry entry = {
1463 .command = (address & 0x1FC) >> 2,
1464 .params = {
1465 value,
1466 value >> 8,
1467 }
1468 };
1469 if (entry.command < DS_GX_CMD_MAX) {
1470 DSGXWriteFIFO(gx, entry);
1471 }
1472 } else {
1473 mLOG(DS_GX, STUB, "Unimplemented GX write %03X:%04X", address, value);
1474 }
1475 break;
1476 }
1477 return value;
1478}
1479
1480uint32_t DSGXWriteRegister32(struct DSGX* gx, uint32_t address, uint32_t value) {
1481 switch (address) {
1482 case DS9_REG_DISP3DCNT:
1483 mLOG(DS_GX, STUB, "Unimplemented GX write %03X:%08X", address, value);
1484 break;
1485 case DS9_REG_GXSTAT_LO:
1486 value = (value & 0xFFFF0000) | DSGXWriteRegister(gx, DS9_REG_GXSTAT_LO, value);
1487 value = (value & 0x0000FFFF) | (DSGXWriteRegister(gx, DS9_REG_GXSTAT_HI, value >> 16) << 16);
1488 break;
1489 case DS9_REG_CLEAR_COLOR_LO:
1490 gx->renderer->writeRegister(gx->renderer, address, value);
1491 gx->renderer->writeRegister(gx->renderer, address + 2, value >> 16);
1492 break;
1493 default:
1494 if (address < DS9_REG_GXFIFO_00) {
1495 mLOG(DS_GX, STUB, "Unimplemented GX write %03X:%08X", address, value);
1496 } else if (address <= DS9_REG_GXFIFO_1F) {
1497 if (gx->outstandingParams[0]) {
1498 struct DSGXEntry entry = {
1499 .command = gx->outstandingCommand[0],
1500 .params = {
1501 value,
1502 value >> 8,
1503 value >> 16,
1504 value >> 24
1505 }
1506 };
1507 DSGXWriteFIFO(gx, entry);
1508 } else {
1509 DSGXUnpackCommand(gx, value);
1510 }
1511 } else if (address < DS9_REG_GXSTAT_LO) {
1512 struct DSGXEntry entry = {
1513 .command = (address & 0x1FC) >> 2,
1514 .params = {
1515 value,
1516 value >> 8,
1517 value >> 16,
1518 value >> 24
1519 }
1520 };
1521 DSGXWriteFIFO(gx, entry);
1522 } else {
1523 mLOG(DS_GX, STUB, "Unimplemented GX write %03X:%08X", address, value);
1524 }
1525 break;
1526 }
1527 return value;
1528}
1529
1530void DSGXFlush(struct DSGX* gx) {
1531 if (gx->swapBuffers) {
1532 gx->renderer->setRAM(gx->renderer, gx->vertexBuffer[gx->bufferIndex], gx->polygonBuffer[gx->bufferIndex], gx->polygonIndex, gx->wSort);
1533 gx->swapBuffers = false;
1534 gx->bufferIndex ^= 1;
1535 gx->vertexIndex = 0;
1536 gx->pendingVertexIndex = 0;
1537 gx->polygonIndex = 0;
1538 if (CircleBufferSize(&gx->fifo)) {
1539 mTimingSchedule(&gx->p->ds9.timing, &gx->fifoEvent, 0);
1540 }
1541 }
1542
1543 DSGXUpdateGXSTAT(gx);
1544}
1545
1546void DSGXScheduleDMA(struct DSCommon* dscore, int number, struct GBADMA* info) {
1547 UNUSED(info);
1548 dscore->p->gx.dmaSource = number;
1549}
1550
1551static void DSGXDummyRendererInit(struct DSGXRenderer* renderer) {
1552 UNUSED(renderer);
1553 // Nothing to do
1554}
1555
1556static void DSGXDummyRendererReset(struct DSGXRenderer* renderer) {
1557 UNUSED(renderer);
1558 // Nothing to do
1559}
1560
1561static void DSGXDummyRendererDeinit(struct DSGXRenderer* renderer) {
1562 UNUSED(renderer);
1563 // Nothing to do
1564}
1565
1566static void DSGXDummyRendererInvalidateTex(struct DSGXRenderer* renderer, int slot) {
1567 UNUSED(renderer);
1568 UNUSED(slot);
1569 // Nothing to do
1570}
1571
1572static void DSGXDummyRendererSetRAM(struct DSGXRenderer* renderer, struct DSGXVertex* verts, struct DSGXPolygon* polys, unsigned polyCount, bool wSort) {
1573 UNUSED(renderer);
1574 UNUSED(verts);
1575 UNUSED(polys);
1576 UNUSED(polyCount);
1577 UNUSED(wSort);
1578 // Nothing to do
1579}
1580
1581static void DSGXDummyRendererDrawScanline(struct DSGXRenderer* renderer, int y) {
1582 UNUSED(renderer);
1583 UNUSED(y);
1584 // Nothing to do
1585}
1586
1587static void DSGXDummyRendererGetScanline(struct DSGXRenderer* renderer, int y, const color_t** output) {
1588 UNUSED(renderer);
1589 UNUSED(y);
1590 *output = NULL;
1591 // Nothing to do
1592}
1593
1594static void DSGXDummyRendererWriteRegister(struct DSGXRenderer* renderer, uint32_t address, uint16_t value) {
1595 UNUSED(renderer);
1596 UNUSED(address);
1597 UNUSED(value);
1598 // Nothing to do
1599}