all repos — mgba @ 938c2f84b814337997363a9be6b171d1f38d23e0

mGBA Game Boy Advance Emulator

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