all repos — mgba @ eb15e02412dc545a9da81b90778d13e51679d392

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		CircleBufferRead8(&gx->fifo, (int8_t*) &entry.command);
 120		CircleBufferRead8(&gx->fifo, (int8_t*) &entry.params[0]);
 121		CircleBufferRead8(&gx->fifo, (int8_t*) &entry.params[1]);
 122		CircleBufferRead8(&gx->fifo, (int8_t*) &entry.params[2]);
 123		CircleBufferRead8(&gx->fifo, (int8_t*) &entry.params[3]);
 124		CircleBufferWrite8(&gx->pipe, entry.command);
 125		CircleBufferWrite8(&gx->pipe, entry.params[0]);
 126		CircleBufferWrite8(&gx->pipe, entry.params[1]);
 127		CircleBufferWrite8(&gx->pipe, entry.params[2]);
 128		CircleBufferWrite8(&gx->pipe, entry.params[3]);
 129	}
 130	if (CircleBufferSize(&gx->fifo) >= sizeof(struct DSGXEntry)) {
 131		struct DSGXEntry entry = { 0 };
 132		CircleBufferRead8(&gx->fifo, (int8_t*) &entry.command);
 133		CircleBufferRead8(&gx->fifo, (int8_t*) &entry.params[0]);
 134		CircleBufferRead8(&gx->fifo, (int8_t*) &entry.params[1]);
 135		CircleBufferRead8(&gx->fifo, (int8_t*) &entry.params[2]);
 136		CircleBufferRead8(&gx->fifo, (int8_t*) &entry.params[3]);
 137		CircleBufferWrite8(&gx->pipe, entry.command);
 138		CircleBufferWrite8(&gx->pipe, entry.params[0]);
 139		CircleBufferWrite8(&gx->pipe, entry.params[1]);
 140		CircleBufferWrite8(&gx->pipe, entry.params[2]);
 141		CircleBufferWrite8(&gx->pipe, entry.params[3]);
 142	}
 143}
 144
 145static void _updateClipMatrix(struct DSGX* gx) {
 146	DSGXMtxMultiply(&gx->clipMatrix, &gx->posMatrix, &gx->projMatrix);
 147	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_00 >> 1] = gx->clipMatrix.m[0];
 148	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_01 >> 1] = gx->clipMatrix.m[0] >> 16;
 149	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_02 >> 1] = gx->clipMatrix.m[1];
 150	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_03 >> 1] = gx->clipMatrix.m[1] >> 16;
 151	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_04 >> 1] = gx->clipMatrix.m[2];
 152	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_05 >> 1] = gx->clipMatrix.m[2] >> 16;
 153	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_06 >> 1] = gx->clipMatrix.m[3];
 154	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_07 >> 1] = gx->clipMatrix.m[3] >> 16;
 155	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_08 >> 1] = gx->clipMatrix.m[4];
 156	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_09 >> 1] = gx->clipMatrix.m[4] >> 16;
 157	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_0A >> 1] = gx->clipMatrix.m[5];
 158	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_0B >> 1] = gx->clipMatrix.m[5] >> 16;
 159	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_0C >> 1] = gx->clipMatrix.m[6];
 160	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_0D >> 1] = gx->clipMatrix.m[6] >> 16;
 161	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_0E >> 1] = gx->clipMatrix.m[7];
 162	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_0F >> 1] = gx->clipMatrix.m[7] >> 16;
 163	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_10 >> 1] = gx->clipMatrix.m[8];
 164	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_11 >> 1] = gx->clipMatrix.m[8] >> 16;
 165	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_12 >> 1] = gx->clipMatrix.m[9];
 166	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_13 >> 1] = gx->clipMatrix.m[9] >> 16;
 167	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_14 >> 1] = gx->clipMatrix.m[10];
 168	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_15 >> 1] = gx->clipMatrix.m[10] >> 16;
 169	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_16 >> 1] = gx->clipMatrix.m[11];
 170	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_17 >> 1] = gx->clipMatrix.m[11] >> 16;
 171	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_18 >> 1] = gx->clipMatrix.m[12];
 172	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_19 >> 1] = gx->clipMatrix.m[12] >> 16;
 173	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_1A >> 1] = gx->clipMatrix.m[13];
 174	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_1B >> 1] = gx->clipMatrix.m[13] >> 16;
 175	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_1C >> 1] = gx->clipMatrix.m[14];
 176	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_1D >> 1] = gx->clipMatrix.m[14] >> 16;
 177	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_1E >> 1] = gx->clipMatrix.m[15];
 178	gx->p->memory.io9[DS9_REG_CLIPMTX_RESULT_1F >> 1] = gx->clipMatrix.m[15] >> 16;
 179}
 180
 181static bool _clipPolygon(struct DSGX* gx, struct DSGXPolygon* poly) {
 182	struct DSGXVertex* vbuf = gx->vertexBuffer[gx->bufferIndex];
 183	int nOffscreen = 0;
 184	unsigned offscreenVerts[4] = {};
 185	int v;
 186	// Collect offscreen vertices
 187	for (v = 0; v < poly->verts; ++v) {
 188		if (!_boxTestVertex(gx, &vbuf[gx->currentPoly.vertIds[v]])) {
 189			offscreenVerts[nOffscreen] = gx->currentPoly.vertIds[v];
 190			++nOffscreen;
 191		}
 192	}
 193	if (!nOffscreen) {
 194		return true;
 195	}
 196
 197	// All of the vertices are offscreen
 198	// TODO: clip vertices instead of discarding them
 199	if (nOffscreen == poly->verts) {
 200		// Remove all vertices before remaking them
 201		gx->vertexIndex -= poly->verts;
 202		// Put back the ones that are on the strip
 203		for (v = 0; v < gx->currentPoly.verts; ++v) {
 204			unsigned vid = gx->currentPoly.vertIds[v];
 205			int ov;
 206			for (ov = 0; ov < nOffscreen; ++ov) {
 207				if (vid != offscreenVerts[ov]) {
 208					continue;
 209				}
 210				vbuf[gx->vertexIndex] = vbuf[vid];
 211				gx->currentPoly.vertIds[v] = gx->vertexIndex;
 212				++gx->vertexIndex;
 213				break;
 214			}
 215		}
 216		return false;
 217	}
 218	return true;
 219}
 220
 221static int32_t _dotViewport(struct DSGXVertex* vertex, int32_t* col) {
 222	int64_t a;
 223	int64_t b;
 224	int64_t sum;
 225	a = col[0];
 226	b = vertex->x;
 227	sum = a * b;
 228	a = col[4];
 229	b = vertex->y;
 230	sum += a * b;
 231	a = col[8];
 232	b = vertex->z;
 233	sum += a * b;
 234	a = col[12];
 235	b = MTX_ONE;
 236	sum += a * b;
 237	return sum >> 8LL;
 238}
 239
 240static int16_t _dotTexture(struct DSGXVertex* vertex, int mode, int32_t* col) {
 241	int64_t a;
 242	int64_t b;
 243	int64_t sum;
 244	switch (mode) {
 245	case 1:
 246		a = col[0];
 247		b = vertex->s << 8;
 248		sum = a * b;
 249		a = col[4];
 250		b = vertex->t << 8;
 251		sum += a * b;
 252		a = col[8];
 253		b = MTX_ONE >> 4;
 254		sum += a * b;
 255		a = col[12];
 256		b = MTX_ONE >> 4;
 257		sum += a * b;
 258		break;
 259	case 2:
 260		return 0;
 261	case 3:
 262		a = col[0];
 263		b = vertex->vx << 8;
 264		sum = a * b;
 265		a = col[4];
 266		b = vertex->vy << 8;
 267		sum += a * b;
 268		a = col[8];
 269		b = vertex->vz << 8;
 270		sum += a * b;
 271		a = col[12];
 272		b = MTX_ONE;
 273		sum += a * b;
 274	}
 275	return sum >> 20;
 276}
 277
 278static void _emitVertex(struct DSGX* gx, uint16_t x, uint16_t y, uint16_t z) {
 279	if (gx->vertexMode < 0 || gx->vertexIndex == DS_GX_VERTEX_BUFFER_SIZE || gx->polygonIndex == DS_GX_POLYGON_BUFFER_SIZE) {
 280		return;
 281	}
 282	gx->currentVertex.x = x;
 283	gx->currentVertex.y = y;
 284	gx->currentVertex.z = z;
 285	gx->currentVertex.vx = _dotViewport(&gx->currentVertex, &gx->clipMatrix.m[0]);
 286	gx->currentVertex.vy = _dotViewport(&gx->currentVertex, &gx->clipMatrix.m[1]);
 287	gx->currentVertex.vz = _dotViewport(&gx->currentVertex, &gx->clipMatrix.m[2]);
 288	gx->currentVertex.vw = _dotViewport(&gx->currentVertex, &gx->clipMatrix.m[3]);
 289
 290	// TODO: What to do if w is 0?
 291
 292	gx->currentVertex.vx = (gx->currentVertex.vx + gx->currentVertex.vw) * (int64_t) (gx->viewportWidth << 12) / (gx->currentVertex.vw * 2) + (gx->viewportX1 << 12);
 293	gx->currentVertex.vy = (gx->currentVertex.vy + gx->currentVertex.vw) * (int64_t) (gx->viewportHeight << 12) / (gx->currentVertex.vw * 2) + (gx->viewportY1 << 12);
 294	//gx->currentVertex.vw = 0x40000000 / gx->currentVertex.vw;
 295
 296	if (DSGXTexParamsGetCoordTfMode(gx->currentPoly.texParams) > 0) {
 297		int32_t m12 = gx->texMatrix.m[12];
 298		int32_t m13 = gx->texMatrix.m[13];
 299		if (DSGXTexParamsGetCoordTfMode(gx->currentPoly.texParams) > 1) {
 300			gx->texMatrix.m[12] = gx->currentVertex.vs;
 301			gx->texMatrix.m[13] = gx->currentVertex.vt;
 302		}
 303		gx->currentVertex.vs = _dotTexture(&gx->currentVertex, DSGXTexParamsGetCoordTfMode(gx->currentPoly.texParams), &gx->texMatrix.m[0]);
 304		gx->currentVertex.vt = _dotTexture(&gx->currentVertex, DSGXTexParamsGetCoordTfMode(gx->currentPoly.texParams), &gx->texMatrix.m[1]);
 305		gx->texMatrix.m[12] = m12;
 306		gx->texMatrix.m[13] = m13;
 307	} else {
 308		gx->currentVertex.vs = gx->currentVertex.s;
 309		gx->currentVertex.vt = gx->currentVertex.t;
 310	}
 311
 312	struct DSGXVertex* vbuf = gx->vertexBuffer[gx->bufferIndex];
 313	vbuf[gx->vertexIndex] = gx->currentVertex;
 314
 315	gx->currentPoly.vertIds[gx->currentPoly.verts] = gx->vertexIndex;
 316
 317	++gx->vertexIndex;
 318	++gx->currentPoly.verts;
 319	int totalVertices;
 320	switch (gx->vertexMode) {
 321	case 0:
 322	case 2:
 323		totalVertices = 3;
 324		break;
 325	case 1:
 326	case 3:
 327		totalVertices = 4;
 328		break;
 329	}
 330	if (gx->currentPoly.verts == totalVertices) {
 331		struct DSGXPolygon* pbuf = gx->polygonBuffer[gx->bufferIndex];
 332		pbuf[gx->polygonIndex] = gx->currentPoly;
 333
 334		switch (gx->vertexMode) {
 335		case 0:
 336		case 1:
 337			gx->currentPoly.verts = 0;
 338			break;
 339		case 2:
 340			gx->currentPoly.vertIds[0] = gx->currentPoly.vertIds[1];
 341			gx->currentPoly.vertIds[1] = gx->currentPoly.vertIds[2];
 342			gx->currentPoly.verts = 2;
 343			break;
 344		case 3:
 345			gx->currentPoly.vertIds[0] = gx->currentPoly.vertIds[2];
 346			gx->currentPoly.vertIds[1] = gx->currentPoly.vertIds[3];
 347			// Ensure quads don't cross over
 348			pbuf[gx->polygonIndex].vertIds[2] = gx->currentPoly.vertIds[3];
 349			pbuf[gx->polygonIndex].vertIds[3] = gx->currentPoly.vertIds[2];
 350			gx->currentPoly.verts = 2;
 351			break;
 352		}
 353
 354		if (_clipPolygon(gx, &pbuf[gx->polygonIndex])) {
 355			++gx->polygonIndex;
 356		}
 357	}
 358}
 359
 360static void _flushOutstanding(struct DSGX* gx) {
 361	if (gx->p->cpuBlocked & DS_CPU_BLOCK_GX) {
 362		gx->p->cpuBlocked &= ~DS_CPU_BLOCK_GX;
 363		DSGXWriteFIFO(gx, gx->outstandingEntry);
 364		gx->outstandingEntry.command = 0;
 365	}
 366	while (gx->outstandingCommand[0] && !gx->outstandingParams[0]) {
 367		DSGXWriteFIFO(gx, (struct DSGXEntry) { 0 });
 368		if (CircleBufferSize(&gx->fifo) == (DS_GX_FIFO_SIZE * sizeof(struct DSGXEntry))) {
 369			return;
 370		}
 371	}
 372}
 373
 374static bool _boxTestVertex(struct DSGX* gx, struct DSGXVertex* vertex) {
 375	int32_t vx = _dotViewport(vertex, &gx->clipMatrix.m[0]);
 376	int32_t vy = _dotViewport(vertex, &gx->clipMatrix.m[1]);
 377	int32_t vz = _dotViewport(vertex, &gx->clipMatrix.m[2]);
 378	int32_t vw = _dotViewport(vertex, &gx->clipMatrix.m[3]);
 379
 380	vx = (vx + vw) * (int64_t) (gx->viewportWidth << 12) / (vw * 2) + (gx->viewportX1 << 12);
 381	vy = (vy + vw) * (int64_t) (gx->viewportHeight << 12) / (vw * 2) + (gx->viewportY1 << 12);
 382	vx >>= 12;
 383	vy >>= 12;
 384
 385	if (vx < gx->viewportX1) {
 386		return false;
 387	}
 388	if (vx >= gx->viewportX2) {
 389		return false;
 390	}
 391	if (vy < gx->viewportY1) {
 392		return false;
 393	}
 394	if (vy >= gx->viewportY2) {
 395		return false;
 396	}
 397	// TODO: depth clipping
 398	return true;
 399}
 400
 401static bool _boxTest(struct DSGX* gx) {
 402	int16_t x = gx->activeEntries[0].params[0];
 403	x |= gx->activeEntries[0].params[1] << 8;
 404	int16_t y = gx->activeEntries[0].params[2];
 405	y |= gx->activeEntries[0].params[3] << 8;
 406	int16_t z = gx->activeEntries[1].params[0];
 407	z |= gx->activeEntries[1].params[1] << 8;
 408	int16_t w = gx->activeEntries[1].params[2];
 409	w |= gx->activeEntries[1].params[3] << 8;
 410	int16_t h = gx->activeEntries[2].params[0];
 411	h |= gx->activeEntries[2].params[1] << 8;
 412	int16_t d = gx->activeEntries[2].params[2];
 413	d |= gx->activeEntries[2].params[3] << 8;
 414
 415	struct DSGXVertex vertex = {
 416		.x = x,
 417		.y = y,
 418		.z = z
 419	};
 420	if (_boxTestVertex(gx, &vertex)) {
 421		return true;
 422	}
 423
 424	vertex.x += w;
 425	if (_boxTestVertex(gx, &vertex)) {
 426		return true;
 427	}
 428
 429	vertex.x = x;
 430	vertex.y += h;
 431	if (_boxTestVertex(gx, &vertex)) {
 432		return true;
 433	}
 434
 435	vertex.x += w;
 436	if (_boxTestVertex(gx, &vertex)) {
 437		return true;
 438	}
 439
 440	vertex.x = x;
 441	vertex.y = y;
 442	vertex.z += d;
 443	if (_boxTestVertex(gx, &vertex)) {
 444		return true;
 445	}
 446
 447	vertex.x += w;
 448	if (_boxTestVertex(gx, &vertex)) {
 449		return true;
 450	}
 451
 452	vertex.x = x;
 453	vertex.y += h;
 454	if (_boxTestVertex(gx, &vertex)) {
 455		return true;
 456	}
 457
 458	vertex.x += w;
 459	if (_boxTestVertex(gx, &vertex)) {
 460		return true;
 461	}
 462
 463	return false;
 464}
 465
 466static void _fifoRun(struct mTiming* timing, void* context, uint32_t cyclesLate) {
 467	struct DSGX* gx = context;
 468	uint32_t cycles;
 469	bool first = true;
 470	while (!gx->swapBuffers) {
 471		if (CircleBufferSize(&gx->pipe) <= 2 * sizeof(struct DSGXEntry)) {
 472			_pullPipe(gx);
 473		}
 474
 475		if (!CircleBufferSize(&gx->pipe)) {
 476			cycles = 0;
 477			break;
 478		}
 479
 480		DSRegGXSTAT gxstat = gx->p->memory.io9[DS9_REG_GXSTAT_LO >> 1];
 481		int projMatrixPointer = DSRegGXSTATGetProjMatrixStackLevel(gxstat);
 482
 483		struct DSGXEntry entry = { 0 };
 484		CircleBufferDump(&gx->pipe, (int8_t*) &entry.command, 1);
 485		cycles = _gxCommandCycleBase[entry.command];
 486
 487		if (first) {
 488			first = false;
 489		} else if (!gx->activeParams && cycles > cyclesLate) {
 490			break;
 491		}
 492		CircleBufferRead8(&gx->pipe, (int8_t*) &entry.command);
 493		CircleBufferRead8(&gx->pipe, (int8_t*) &entry.params[0]);
 494		CircleBufferRead8(&gx->pipe, (int8_t*) &entry.params[1]);
 495		CircleBufferRead8(&gx->pipe, (int8_t*) &entry.params[2]);
 496		CircleBufferRead8(&gx->pipe, (int8_t*) &entry.params[3]);
 497
 498		if (gx->activeParams) {
 499			int index = _gxCommandParams[entry.command] - gx->activeParams;
 500			gx->activeEntries[index] = entry;
 501			--gx->activeParams;
 502		} else {
 503			gx->activeParams = _gxCommandParams[entry.command];
 504			if (gx->activeParams) {
 505				--gx->activeParams;
 506			}
 507			if (gx->activeParams) {
 508				gx->activeEntries[0] = entry;
 509			}
 510		}
 511
 512		if (gx->activeParams) {
 513			continue;
 514		}
 515
 516		switch (entry.command) {
 517		case DS_GX_CMD_MTX_MODE:
 518			if (entry.params[0] < 4) {
 519				gx->mtxMode = entry.params[0];
 520			} else {
 521				mLOG(DS_GX, GAME_ERROR, "Invalid GX MTX_MODE %02X", entry.params[0]);
 522			}
 523			break;
 524		case DS_GX_CMD_MTX_PUSH:
 525			switch (gx->mtxMode) {
 526			case 0:
 527				memcpy(&gx->projMatrixStack, &gx->projMatrix, sizeof(gx->projMatrix));
 528				++projMatrixPointer;
 529				break;
 530			case 2:
 531				memcpy(&gx->vecMatrixStack[gx->pvMatrixPointer & 0x1F], &gx->vecMatrix, sizeof(gx->vecMatrix));
 532				// Fall through
 533			case 1:
 534				memcpy(&gx->posMatrixStack[gx->pvMatrixPointer & 0x1F], &gx->posMatrix, sizeof(gx->posMatrix));
 535				++gx->pvMatrixPointer;
 536				break;
 537			case 3:
 538				mLOG(DS_GX, STUB, "Unimplemented GX MTX_PUSH mode");
 539				break;
 540			}
 541			break;
 542		case DS_GX_CMD_MTX_POP: {
 543			int8_t offset = entry.params[0];
 544			offset <<= 2;
 545			offset >>= 2;
 546			switch (gx->mtxMode) {
 547			case 0:
 548				projMatrixPointer -= offset;
 549				memcpy(&gx->projMatrix, &gx->projMatrixStack, sizeof(gx->projMatrix));
 550				break;
 551			case 1:
 552				gx->pvMatrixPointer -= offset;
 553				memcpy(&gx->posMatrix, &gx->posMatrixStack[gx->pvMatrixPointer & 0x1F], sizeof(gx->posMatrix));
 554				break;
 555			case 2:
 556				gx->pvMatrixPointer -= offset;
 557				memcpy(&gx->vecMatrix, &gx->vecMatrixStack[gx->pvMatrixPointer & 0x1F], sizeof(gx->vecMatrix));
 558				memcpy(&gx->posMatrix, &gx->posMatrixStack[gx->pvMatrixPointer & 0x1F], sizeof(gx->posMatrix));
 559				break;
 560			case 3:
 561				mLOG(DS_GX, STUB, "Unimplemented GX MTX_POP mode");
 562				break;
 563			}
 564			_updateClipMatrix(gx);
 565			break;
 566		}
 567		case DS_GX_CMD_MTX_STORE: {
 568			int8_t offset = entry.params[0] & 0x1F;
 569			// TODO: overflow
 570			switch (gx->mtxMode) {
 571			case 0:
 572				memcpy(&gx->projMatrixStack, &gx->projMatrix, sizeof(gx->projMatrixStack));
 573				break;
 574			case 2:
 575				memcpy(&gx->vecMatrixStack[offset], &gx->vecMatrix, sizeof(gx->vecMatrix));
 576				// Fall through
 577			case 1:
 578				memcpy(&gx->posMatrixStack[offset], &gx->posMatrix, sizeof(gx->posMatrix));
 579				break;
 580			case 3:
 581				mLOG(DS_GX, STUB, "Unimplemented GX MTX_STORE mode");
 582				break;
 583			}
 584			break;
 585		}
 586		case DS_GX_CMD_MTX_RESTORE: {
 587			int8_t offset = entry.params[0] & 0x1F;
 588			// TODO: overflow
 589			switch (gx->mtxMode) {
 590			case 0:
 591				memcpy(&gx->projMatrix, &gx->projMatrixStack, sizeof(gx->projMatrix));
 592				break;
 593			case 2:
 594				memcpy(&gx->vecMatrix, &gx->vecMatrixStack[offset], sizeof(gx->vecMatrix));
 595				// Fall through
 596			case 1:
 597				memcpy(&gx->posMatrix, &gx->posMatrixStack[offset], sizeof(gx->posMatrix));
 598				break;
 599			case 3:
 600				mLOG(DS_GX, STUB, "Unimplemented GX MTX_RESTORE mode");
 601				break;
 602			}
 603			_updateClipMatrix(gx);
 604			break;
 605		}
 606		case DS_GX_CMD_MTX_IDENTITY:
 607			switch (gx->mtxMode) {
 608			case 0:
 609				DSGXMtxIdentity(&gx->projMatrix);
 610				break;
 611			case 2:
 612				DSGXMtxIdentity(&gx->vecMatrix);
 613				// Fall through
 614			case 1:
 615				DSGXMtxIdentity(&gx->posMatrix);
 616				break;
 617			case 3:
 618				DSGXMtxIdentity(&gx->texMatrix);
 619				break;
 620			}
 621			_updateClipMatrix(gx);
 622			break;
 623		case DS_GX_CMD_MTX_LOAD_4x4: {
 624			struct DSGXMatrix m;
 625			int i;
 626			for (i = 0; i < 16; ++i) {
 627				m.m[i] = gx->activeEntries[i].params[0];
 628				m.m[i] |= gx->activeEntries[i].params[1] << 8;
 629				m.m[i] |= gx->activeEntries[i].params[2] << 16;
 630				m.m[i] |= gx->activeEntries[i].params[3] << 24;
 631			}
 632			switch (gx->mtxMode) {
 633			case 0:
 634				memcpy(&gx->projMatrix, &m, sizeof(gx->projMatrix));
 635				break;
 636			case 2:
 637				memcpy(&gx->vecMatrix, &m, sizeof(gx->vecMatrix));
 638				// Fall through
 639			case 1:
 640				memcpy(&gx->posMatrix, &m, sizeof(gx->posMatrix));
 641				break;
 642			case 3:
 643				memcpy(&gx->texMatrix, &m, sizeof(gx->texMatrix));
 644				break;
 645			}
 646			_updateClipMatrix(gx);
 647			break;
 648		}
 649		case DS_GX_CMD_MTX_LOAD_4x3: {
 650			struct DSGXMatrix m;
 651			int i, j;
 652			for (j = 0; j < 4; ++j) {
 653				for (i = 0; i < 3; ++i) {
 654					m.m[i + j * 4] = gx->activeEntries[i + j * 3].params[0];
 655					m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[1] << 8;
 656					m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[2] << 16;
 657					m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[3] << 24;
 658				}
 659				m.m[j * 4 + 3] = 0;
 660			}
 661			m.m[15] = MTX_ONE;
 662			switch (gx->mtxMode) {
 663			case 0:
 664				memcpy(&gx->projMatrix, &m, sizeof(gx->projMatrix));
 665				break;
 666			case 2:
 667				memcpy(&gx->vecMatrix, &m, sizeof(gx->vecMatrix));
 668				// Fall through
 669			case 1:
 670				memcpy(&gx->posMatrix, &m, sizeof(gx->posMatrix));
 671				break;
 672			case 3:
 673				memcpy(&gx->texMatrix, &m, sizeof(gx->texMatrix));
 674				break;
 675			}
 676			_updateClipMatrix(gx);
 677			break;
 678		}
 679		case DS_GX_CMD_MTX_MULT_4x4: {
 680			struct DSGXMatrix m;
 681			int i;
 682			for (i = 0; i < 16; ++i) {
 683				m.m[i] = gx->activeEntries[i].params[0];
 684				m.m[i] |= gx->activeEntries[i].params[1] << 8;
 685				m.m[i] |= gx->activeEntries[i].params[2] << 16;
 686				m.m[i] |= gx->activeEntries[i].params[3] << 24;
 687			}
 688			switch (gx->mtxMode) {
 689			case 0:
 690				DSGXMtxMultiply(&gx->projMatrix, &m, &gx->projMatrix);
 691				break;
 692			case 2:
 693				DSGXMtxMultiply(&gx->vecMatrix, &m, &gx->vecMatrix);
 694				// Fall through
 695			case 1:
 696				DSGXMtxMultiply(&gx->posMatrix, &m, &gx->posMatrix);
 697				break;
 698			case 3:
 699				DSGXMtxMultiply(&gx->texMatrix, &m, &gx->texMatrix);
 700				break;
 701			}
 702			_updateClipMatrix(gx);
 703			break;
 704		}
 705		case DS_GX_CMD_MTX_MULT_4x3: {
 706			struct DSGXMatrix m;
 707			int i, j;
 708			for (j = 0; j < 4; ++j) {
 709				for (i = 0; i < 3; ++i) {
 710					m.m[i + j * 4] = gx->activeEntries[i + j * 3].params[0];
 711					m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[1] << 8;
 712					m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[2] << 16;
 713					m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[3] << 24;
 714				}
 715				m.m[j * 4 + 3] = 0;
 716			}
 717			m.m[15] = MTX_ONE;
 718			switch (gx->mtxMode) {
 719			case 0:
 720				DSGXMtxMultiply(&gx->projMatrix, &m, &gx->projMatrix);
 721				break;
 722			case 2:
 723				DSGXMtxMultiply(&gx->vecMatrix, &m, &gx->vecMatrix);
 724				// Fall through
 725			case 1:
 726				DSGXMtxMultiply(&gx->posMatrix, &m, &gx->posMatrix);
 727				break;
 728			case 3:
 729				DSGXMtxMultiply(&gx->texMatrix, &m, &gx->texMatrix);
 730				break;
 731			}
 732			_updateClipMatrix(gx);
 733			break;
 734		}
 735		case DS_GX_CMD_MTX_MULT_3x3: {
 736			struct DSGXMatrix m;
 737			int i, j;
 738			for (j = 0; j < 3; ++j) {
 739				for (i = 0; i < 3; ++i) {
 740					m.m[i + j * 4] = gx->activeEntries[i + j * 3].params[0];
 741					m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[1] << 8;
 742					m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[2] << 16;
 743					m.m[i + j * 4] |= gx->activeEntries[i + j * 3].params[3] << 24;
 744				}
 745				m.m[j * 4 + 3] = 0;
 746			}
 747			m.m[12] = 0;
 748			m.m[13] = 0;
 749			m.m[14] = 0;
 750			m.m[15] = MTX_ONE;
 751			switch (gx->mtxMode) {
 752			case 0:
 753				DSGXMtxMultiply(&gx->projMatrix, &m, &gx->projMatrix);
 754				break;
 755			case 2:
 756				DSGXMtxMultiply(&gx->vecMatrix, &m, &gx->vecMatrix);
 757				// Fall through
 758			case 1:
 759				DSGXMtxMultiply(&gx->posMatrix, &m, &gx->posMatrix);
 760				break;
 761			case 3:
 762				DSGXMtxMultiply(&gx->texMatrix, &m, &gx->texMatrix);
 763				break;
 764			}
 765			_updateClipMatrix(gx);
 766			break;
 767		}
 768		case DS_GX_CMD_MTX_TRANS: {
 769			int32_t m[3];
 770			m[0] = gx->activeEntries[0].params[0];
 771			m[0] |= gx->activeEntries[0].params[1] << 8;
 772			m[0] |= gx->activeEntries[0].params[2] << 16;
 773			m[0] |= gx->activeEntries[0].params[3] << 24;
 774			m[1] = gx->activeEntries[1].params[0];
 775			m[1] |= gx->activeEntries[1].params[1] << 8;
 776			m[1] |= gx->activeEntries[1].params[2] << 16;
 777			m[1] |= gx->activeEntries[1].params[3] << 24;
 778			m[2] = gx->activeEntries[2].params[0];
 779			m[2] |= gx->activeEntries[2].params[1] << 8;
 780			m[2] |= gx->activeEntries[2].params[2] << 16;
 781			m[2] |= gx->activeEntries[2].params[3] << 24;
 782			switch (gx->mtxMode) {
 783			case 0:
 784				DSGXMtxTranslate(&gx->projMatrix, m);
 785				break;
 786			case 2:
 787				DSGXMtxTranslate(&gx->vecMatrix, m);
 788				// Fall through
 789			case 1:
 790				DSGXMtxTranslate(&gx->posMatrix, m);
 791				break;
 792			case 3:
 793				DSGXMtxTranslate(&gx->texMatrix, m);
 794				break;
 795			}
 796			_updateClipMatrix(gx);
 797			break;
 798		}
 799		case DS_GX_CMD_MTX_SCALE: {
 800			int32_t m[3];
 801			m[0] = gx->activeEntries[0].params[0];
 802			m[0] |= gx->activeEntries[0].params[1] << 8;
 803			m[0] |= gx->activeEntries[0].params[2] << 16;
 804			m[0] |= gx->activeEntries[0].params[3] << 24;
 805			m[1] = gx->activeEntries[1].params[0];
 806			m[1] |= gx->activeEntries[1].params[1] << 8;
 807			m[1] |= gx->activeEntries[1].params[2] << 16;
 808			m[1] |= gx->activeEntries[1].params[3] << 24;
 809			m[2] = gx->activeEntries[2].params[0];
 810			m[2] |= gx->activeEntries[2].params[1] << 8;
 811			m[2] |= gx->activeEntries[2].params[2] << 16;
 812			m[2] |= gx->activeEntries[2].params[3] << 24;
 813			switch (gx->mtxMode) {
 814			case 0:
 815				DSGXMtxScale(&gx->projMatrix, m);
 816				break;
 817			case 1:
 818			case 2:
 819				DSGXMtxScale(&gx->posMatrix, m);
 820				break;
 821			case 3:
 822				DSGXMtxScale(&gx->texMatrix, m);
 823				break;
 824			}
 825			_updateClipMatrix(gx);
 826			break;
 827		}
 828		case DS_GX_CMD_COLOR:
 829			gx->currentVertex.color = entry.params[0];
 830			gx->currentVertex.color |= entry.params[1] << 8;
 831			break;
 832		case DS_GX_CMD_TEXCOORD:
 833			gx->currentVertex.s = entry.params[0];
 834			gx->currentVertex.s |= entry.params[1] << 8;
 835			gx->currentVertex.t = entry.params[2];
 836			gx->currentVertex.t |= entry.params[3] << 8;
 837			break;
 838		case DS_GX_CMD_VTX_16: {
 839			int16_t x = gx->activeEntries[0].params[0];
 840			x |= gx->activeEntries[0].params[1] << 8;
 841			int16_t y = gx->activeEntries[0].params[2];
 842			y |= gx->activeEntries[0].params[3] << 8;
 843			int16_t z = gx->activeEntries[1].params[0];
 844			z |= gx->activeEntries[1].params[1] << 8;
 845			_emitVertex(gx, x, y, z);
 846			break;
 847		}
 848		case DS_GX_CMD_VTX_10: {
 849			int32_t xyz = entry.params[0];
 850			xyz |= entry.params[1] << 8;
 851			xyz |= entry.params[2] << 16;
 852			xyz |= entry.params[3] << 24;
 853			int16_t x = (xyz << 6) & 0xFFC0;
 854			int16_t y = (xyz >> 4) & 0xFFC0;
 855			int16_t z = (xyz >> 14) & 0xFFC0;
 856			_emitVertex(gx, x, y, z);
 857			break;
 858		}
 859		case DS_GX_CMD_VTX_XY: {
 860			int16_t x = entry.params[0];
 861			x |= entry.params[1] << 8;
 862			int16_t y = entry.params[2];
 863			y |= entry.params[3] << 8;
 864			_emitVertex(gx, x, y, gx->currentVertex.z);
 865			break;
 866		}
 867		case DS_GX_CMD_VTX_XZ: {
 868			int16_t x = entry.params[0];
 869			x |= entry.params[1] << 8;
 870			int16_t z = entry.params[2];
 871			z |= entry.params[3] << 8;
 872			_emitVertex(gx, x, gx->currentVertex.y, z);
 873			break;
 874		}
 875		case DS_GX_CMD_VTX_YZ: {
 876			int16_t y = entry.params[0];
 877			y |= entry.params[1] << 8;
 878			int16_t z = entry.params[2];
 879			z |= entry.params[3] << 8;
 880			_emitVertex(gx, gx->currentVertex.x, y, z);
 881			break;
 882		}
 883		case DS_GX_CMD_VTX_DIFF: {
 884			int32_t xyz = entry.params[0];
 885			xyz |= entry.params[1] << 8;
 886			xyz |= entry.params[2] << 16;
 887			xyz |= entry.params[3] << 24;
 888			int16_t x = (xyz << 6) & 0xFFC0;
 889			int16_t y = (xyz >> 4) & 0xFFC0;
 890			int16_t z = (xyz >> 14) & 0xFFC0;
 891			_emitVertex(gx, gx->currentVertex.x + (x >> 6), gx->currentVertex.y + (y >> 6), gx->currentVertex.z + (z >> 6));
 892			break;
 893		}
 894		case DS_GX_CMD_POLYGON_ATTR:
 895			gx->nextPoly.polyParams = entry.params[0];
 896			gx->nextPoly.polyParams |= entry.params[1] << 8;
 897			gx->nextPoly.polyParams |= entry.params[2] << 16;
 898			gx->nextPoly.polyParams |= entry.params[3] << 24;
 899			break;
 900		case DS_GX_CMD_TEXIMAGE_PARAM:
 901			gx->nextPoly.texParams = entry.params[0];
 902			gx->nextPoly.texParams |= entry.params[1] << 8;
 903			gx->nextPoly.texParams |= entry.params[2] << 16;
 904			gx->nextPoly.texParams |= entry.params[3] << 24;
 905			break;
 906		case DS_GX_CMD_PLTT_BASE:
 907			gx->nextPoly.palBase = entry.params[0];
 908			gx->nextPoly.palBase |= entry.params[1] << 8;
 909			gx->nextPoly.palBase |= entry.params[2] << 16;
 910			gx->nextPoly.palBase |= entry.params[3] << 24;
 911			break;
 912		case DS_GX_CMD_BEGIN_VTXS:
 913			gx->vertexMode = entry.params[0] & 3;
 914			gx->currentPoly = gx->nextPoly;
 915			break;
 916		case DS_GX_CMD_END_VTXS:
 917			gx->vertexMode = -1;
 918			break;
 919		case DS_GX_CMD_SWAP_BUFFERS:
 920			gx->swapBuffers = true;
 921			gx->wSort = entry.params[0] & 2;
 922			memset(&gx->currentVertex, 0, sizeof(gx->currentVertex));
 923			memset(&gx->nextPoly, 0, sizeof(gx-> nextPoly));
 924			gx->currentVertex.color = 0x7FFF;
 925			break;
 926		case DS_GX_CMD_VIEWPORT:
 927			gx->viewportX1 = (uint8_t) entry.params[0];
 928			gx->viewportY1 = (uint8_t) entry.params[1];
 929			gx->viewportX2 = (uint8_t) entry.params[2];
 930			gx->viewportY2 = (uint8_t) entry.params[3];
 931			gx->viewportWidth = gx->viewportX2 - gx->viewportX1;
 932			gx->viewportHeight = gx->viewportY2 - gx->viewportY1;
 933			break;
 934		case DS_GX_CMD_BOX_TEST:
 935			gxstat = DSRegGXSTATClearTestBusy(gxstat);
 936			gxstat = DSRegGXSTATTestFillBoxTestResult(gxstat, _boxTest(gx));
 937			break;
 938		default:
 939			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]);
 940			break;
 941		}
 942
 943		gxstat = DSRegGXSTATSetPVMatrixStackLevel(gxstat, gx->pvMatrixPointer);
 944		gxstat = DSRegGXSTATSetProjMatrixStackLevel(gxstat, projMatrixPointer);
 945		gxstat = DSRegGXSTATTestFillMatrixStackError(gxstat, projMatrixPointer || gx->pvMatrixPointer >= 0x1F);
 946		gx->p->memory.io9[DS9_REG_GXSTAT_LO >> 1] = gxstat;
 947
 948		if (cyclesLate >= cycles) {
 949			cyclesLate -= cycles;
 950		} else {
 951			break;
 952		}
 953	}
 954	if (cycles && !gx->swapBuffers) {
 955		mTimingSchedule(timing, &gx->fifoEvent, cycles - cyclesLate);
 956	}
 957	if (CircleBufferSize(&gx->fifo) < (DS_GX_FIFO_SIZE * sizeof(struct DSGXEntry))) {
 958		_flushOutstanding(gx);
 959	}
 960	DSGXUpdateGXSTAT(gx);
 961}
 962
 963void DSGXInit(struct DSGX* gx) {
 964	gx->renderer = &dummyRenderer;
 965	CircleBufferInit(&gx->fifo, sizeof(struct DSGXEntry) * DS_GX_FIFO_SIZE);
 966	CircleBufferInit(&gx->pipe, sizeof(struct DSGXEntry) * DS_GX_PIPE_SIZE);
 967	gx->vertexBuffer[0] = malloc(sizeof(struct DSGXVertex) * DS_GX_VERTEX_BUFFER_SIZE);
 968	gx->vertexBuffer[1] = malloc(sizeof(struct DSGXVertex) * DS_GX_VERTEX_BUFFER_SIZE);
 969	gx->polygonBuffer[0] = malloc(sizeof(struct DSGXPolygon) * DS_GX_POLYGON_BUFFER_SIZE);
 970	gx->polygonBuffer[1] = malloc(sizeof(struct DSGXPolygon) * DS_GX_POLYGON_BUFFER_SIZE);
 971	gx->fifoEvent.name = "DS GX FIFO";
 972	gx->fifoEvent.priority = 0xC;
 973	gx->fifoEvent.context = gx;
 974	gx->fifoEvent.callback = _fifoRun;
 975}
 976
 977void DSGXDeinit(struct DSGX* gx) {
 978	DSGXAssociateRenderer(gx, &dummyRenderer);
 979	CircleBufferDeinit(&gx->fifo);
 980	CircleBufferDeinit(&gx->pipe);
 981	free(gx->vertexBuffer[0]);
 982	free(gx->vertexBuffer[1]);
 983	free(gx->polygonBuffer[0]);
 984	free(gx->polygonBuffer[1]);
 985}
 986
 987void DSGXReset(struct DSGX* gx) {
 988	CircleBufferClear(&gx->fifo);
 989	CircleBufferClear(&gx->pipe);
 990	DSGXMtxIdentity(&gx->projMatrix);
 991	DSGXMtxIdentity(&gx->texMatrix);
 992	DSGXMtxIdentity(&gx->posMatrix);
 993	DSGXMtxIdentity(&gx->vecMatrix);
 994
 995	DSGXMtxIdentity(&gx->clipMatrix);
 996	DSGXMtxIdentity(&gx->projMatrixStack);
 997	DSGXMtxIdentity(&gx->texMatrixStack);
 998	int i;
 999	for (i = 0; i < 32; ++i) {
1000		DSGXMtxIdentity(&gx->posMatrixStack[i]);
1001		DSGXMtxIdentity(&gx->vecMatrixStack[i]);
1002	}
1003	gx->swapBuffers = false;
1004	gx->bufferIndex = 0;
1005	gx->vertexIndex = 0;
1006	gx->polygonIndex = 0;
1007	gx->mtxMode = 0;
1008	gx->pvMatrixPointer = 0;
1009	gx->vertexMode = -1;
1010
1011	gx->viewportX1 = 0;
1012	gx->viewportY1 = 0;
1013	gx->viewportX2 = DS_VIDEO_HORIZONTAL_PIXELS - 1;
1014	gx->viewportY2 = DS_VIDEO_VERTICAL_PIXELS - 1;
1015	gx->viewportWidth = gx->viewportX2 - gx->viewportX1;
1016	gx->viewportHeight = gx->viewportY2 - gx->viewportY1;
1017
1018	memset(gx->outstandingParams, 0, sizeof(gx->outstandingParams));
1019	memset(gx->outstandingCommand, 0, sizeof(gx->outstandingCommand));
1020	memset(&gx->outstandingEntry, 0, sizeof(gx->outstandingEntry));
1021	gx->activeParams = 0;
1022	memset(&gx->currentVertex, 0, sizeof(gx->currentVertex));
1023	memset(&gx->nextPoly, 0, sizeof(gx-> nextPoly));
1024	gx->currentVertex.color = 0x7FFF;
1025	gx->dmaSource = -1;
1026}
1027
1028void DSGXAssociateRenderer(struct DSGX* gx, struct DSGXRenderer* renderer) {
1029	gx->renderer->deinit(gx->renderer);
1030	gx->renderer = renderer;
1031	memcpy(gx->renderer->tex, gx->tex, sizeof(gx->renderer->tex));
1032	memcpy(gx->renderer->texPal, gx->texPal, sizeof(gx->renderer->texPal));
1033	gx->renderer->init(gx->renderer);
1034}
1035
1036void DSGXUpdateGXSTAT(struct DSGX* gx) {
1037	uint32_t value = gx->p->memory.io9[DS9_REG_GXSTAT_HI >> 1] << 16;
1038	value = DSRegGXSTATIsDoIRQ(value);
1039
1040	size_t entries = CircleBufferSize(&gx->fifo) / sizeof(struct DSGXEntry);
1041	// XXX
1042	if (gx->swapBuffers) {
1043		entries++;
1044	}
1045	value = DSRegGXSTATSetFIFOEntries(value, entries);
1046	value = DSRegGXSTATSetFIFOLtHalf(value, entries < (DS_GX_FIFO_SIZE / 2));
1047	value = DSRegGXSTATSetFIFOEmpty(value, entries == 0);
1048
1049	if ((DSRegGXSTATGetDoIRQ(value) == 1 && entries < (DS_GX_FIFO_SIZE / 2)) ||
1050		(DSRegGXSTATGetDoIRQ(value) == 2 && entries == 0)) {
1051		DSRaiseIRQ(gx->p->ds9.cpu, gx->p->ds9.memory.io, DS_IRQ_GEOM_FIFO);
1052	}
1053
1054	value = DSRegGXSTATSetBusy(value, mTimingIsScheduled(&gx->p->ds9.timing, &gx->fifoEvent) || gx->swapBuffers);
1055
1056	gx->p->memory.io9[DS9_REG_GXSTAT_HI >> 1] = value >> 16;
1057
1058	struct GBADMA* dma = NULL;
1059	if (gx->dmaSource >= 0) {
1060		dma = &gx->p->ds9.memory.dma[gx->dmaSource];
1061		if (GBADMARegisterGetTiming9(dma->reg) != DS_DMA_TIMING_GEOM_FIFO) {
1062			gx->dmaSource = -1;
1063		} else if (GBADMARegisterIsEnable(dma->reg) && entries < (DS_GX_FIFO_SIZE / 2) && !dma->nextCount) {
1064			dma->nextCount = dma->count;
1065			if (dma->count > 112) {
1066				dma->nextCount = 112;
1067			}
1068			dma->when = mTimingCurrentTime(&gx->p->ds9.timing);
1069			DSDMAUpdate(&gx->p->ds9);
1070		}
1071	}
1072}
1073
1074static void DSGXUnpackCommand(struct DSGX* gx, uint32_t command) {
1075	gx->outstandingCommand[0] = command;
1076	gx->outstandingCommand[1] = command >> 8;
1077	gx->outstandingCommand[2] = command >> 16;
1078	gx->outstandingCommand[3] = command >> 24;
1079	if (gx->outstandingCommand[0] >= DS_GX_CMD_MAX) {
1080		gx->outstandingCommand[0] = 0;
1081	}
1082	if (gx->outstandingCommand[1] >= DS_GX_CMD_MAX) {
1083		gx->outstandingCommand[1] = 0;
1084	}
1085	if (gx->outstandingCommand[2] >= DS_GX_CMD_MAX) {
1086		gx->outstandingCommand[2] = 0;
1087	}
1088	if (gx->outstandingCommand[3] >= DS_GX_CMD_MAX) {
1089		gx->outstandingCommand[3] = 0;
1090	}
1091	gx->outstandingParams[0] = _gxCommandParams[gx->outstandingCommand[0]];
1092	gx->outstandingParams[1] = _gxCommandParams[gx->outstandingCommand[1]];
1093	gx->outstandingParams[2] = _gxCommandParams[gx->outstandingCommand[2]];
1094	gx->outstandingParams[3] = _gxCommandParams[gx->outstandingCommand[3]];
1095	_flushOutstanding(gx);
1096	DSGXUpdateGXSTAT(gx);
1097}
1098
1099static void DSGXWriteFIFO(struct DSGX* gx, struct DSGXEntry entry) {
1100	if (CircleBufferSize(&gx->fifo) == (DS_GX_FIFO_SIZE * sizeof(entry))) {
1101		mLOG(DS_GX, INFO, "FIFO full");
1102		if (gx->p->cpuBlocked & DS_CPU_BLOCK_GX) {
1103			abort();
1104		}
1105		gx->p->cpuBlocked |= DS_CPU_BLOCK_GX;
1106		gx->outstandingEntry = entry;
1107		gx->p->ds9.cpu->nextEvent = 0;
1108		return;
1109	}
1110	if (gx->outstandingCommand[0]) {
1111		entry.command = gx->outstandingCommand[0];
1112		if (gx->outstandingParams[0]) {
1113			--gx->outstandingParams[0];
1114		}
1115		if (!gx->outstandingParams[0]) {
1116			// TODO: improve this
1117			memmove(&gx->outstandingParams[0], &gx->outstandingParams[1], sizeof(gx->outstandingParams[0]) * 3);
1118			memmove(&gx->outstandingCommand[0], &gx->outstandingCommand[1], sizeof(gx->outstandingCommand[0]) * 3);
1119			gx->outstandingParams[3] = 0;
1120			gx->outstandingCommand[3] = 0;
1121		}
1122	} else {
1123		gx->outstandingParams[0] = _gxCommandParams[entry.command];
1124		if (gx->outstandingParams[0]) {
1125			--gx->outstandingParams[0];
1126		}
1127		if (gx->outstandingParams[0]) {
1128			gx->outstandingCommand[0] = entry.command;
1129		}
1130	}
1131	uint32_t cycles = _gxCommandCycleBase[entry.command];
1132	if (!cycles) {
1133		return;
1134	}
1135	if (CircleBufferSize(&gx->fifo) == 0 && CircleBufferSize(&gx->pipe) < (DS_GX_PIPE_SIZE * sizeof(entry))) {
1136		CircleBufferWrite8(&gx->pipe, entry.command);
1137		CircleBufferWrite8(&gx->pipe, entry.params[0]);
1138		CircleBufferWrite8(&gx->pipe, entry.params[1]);
1139		CircleBufferWrite8(&gx->pipe, entry.params[2]);
1140		CircleBufferWrite8(&gx->pipe, entry.params[3]);
1141	} else if (CircleBufferSize(&gx->fifo) < (DS_GX_FIFO_SIZE * sizeof(entry))) {
1142		CircleBufferWrite8(&gx->fifo, entry.command);
1143		CircleBufferWrite8(&gx->fifo, entry.params[0]);
1144		CircleBufferWrite8(&gx->fifo, entry.params[1]);
1145		CircleBufferWrite8(&gx->fifo, entry.params[2]);
1146		CircleBufferWrite8(&gx->fifo, entry.params[3]);
1147	}
1148	if (entry.command == DS_GX_CMD_BOX_TEST) {
1149		DSRegGXSTAT gxstat = gx->p->memory.io9[DS9_REG_GXSTAT_LO >> 1];
1150		gxstat = DSRegGXSTATFillTestBusy(gxstat);
1151		gxstat = DSRegGXSTATClearBoxTestResult(gxstat);
1152		gx->p->memory.io9[DS9_REG_GXSTAT_LO >> 1] = gxstat;
1153	}
1154	if (!gx->swapBuffers && !mTimingIsScheduled(&gx->p->ds9.timing, &gx->fifoEvent)) {
1155		mTimingSchedule(&gx->p->ds9.timing, &gx->fifoEvent, cycles);
1156	}
1157
1158	_flushOutstanding(gx);
1159}
1160
1161uint16_t DSGXWriteRegister(struct DSGX* gx, uint32_t address, uint16_t value) {
1162	uint16_t oldValue = gx->p->memory.io9[address >> 1];
1163	switch (address) {
1164	case DS9_REG_DISP3DCNT:
1165		mLOG(DS_GX, STUB, "Unimplemented GX write %03X:%04X", address, value);
1166		break;
1167	case DS9_REG_GXSTAT_LO:
1168		value = DSRegGXSTATIsMatrixStackError(value);
1169		if (value) {
1170			oldValue = DSRegGXSTATClearMatrixStackError(oldValue);
1171			oldValue = DSRegGXSTATClearProjMatrixStackLevel(oldValue);
1172		}
1173		value = oldValue;
1174		break;
1175	case DS9_REG_GXSTAT_HI:
1176		value = DSRegGXSTATIsDoIRQ(value << 16) >> 16;
1177		gx->p->memory.io9[address >> 1] = value;
1178		DSGXUpdateGXSTAT(gx);
1179		value = gx->p->memory.io9[address >> 1];
1180		break;
1181	default:
1182		if (address < DS9_REG_GXFIFO_00) {
1183			mLOG(DS_GX, STUB, "Unimplemented GX write %03X:%04X", address, value);
1184		} else if (address <= DS9_REG_GXFIFO_1F) {
1185			mLOG(DS_GX, STUB, "Unimplemented GX write %03X:%04X", address, value);
1186		} else if (address < DS9_REG_GXSTAT_LO) {
1187			struct DSGXEntry entry = {
1188				.command = (address & 0x1FC) >> 2,
1189				.params = {
1190					value,
1191					value >> 8,
1192				}
1193			};
1194			if (entry.command < DS_GX_CMD_MAX) {
1195				DSGXWriteFIFO(gx, entry);
1196			}
1197		} else {
1198			mLOG(DS_GX, STUB, "Unimplemented GX write %03X:%04X", address, value);
1199		}
1200		break;
1201	}
1202	return value;
1203}
1204
1205uint32_t DSGXWriteRegister32(struct DSGX* gx, uint32_t address, uint32_t value) {
1206	switch (address) {
1207	case DS9_REG_DISP3DCNT:
1208		mLOG(DS_GX, STUB, "Unimplemented GX write %03X:%08X", address, value);
1209		break;
1210	case DS9_REG_GXSTAT_LO:
1211		value = (value & 0xFFFF0000) | DSGXWriteRegister(gx, DS9_REG_GXSTAT_LO, value);
1212		value = (value & 0x0000FFFF) | (DSGXWriteRegister(gx, DS9_REG_GXSTAT_HI, value >> 16) << 16);
1213		break;
1214	default:
1215		if (address < DS9_REG_GXFIFO_00) {
1216			mLOG(DS_GX, STUB, "Unimplemented GX write %03X:%08X", address, value);
1217		} else if (address <= DS9_REG_GXFIFO_1F) {
1218			if (gx->outstandingParams[0]) {
1219				struct DSGXEntry entry = {
1220					.command = gx->outstandingCommand[0],
1221					.params = {
1222						value,
1223						value >> 8,
1224						value >> 16,
1225						value >> 24
1226					}
1227				};
1228				DSGXWriteFIFO(gx, entry);
1229			} else {
1230				DSGXUnpackCommand(gx, value);
1231			}
1232		} else if (address < DS9_REG_GXSTAT_LO) {
1233			struct DSGXEntry entry = {
1234				.command = (address & 0x1FC) >> 2,
1235				.params = {
1236					value,
1237					value >> 8,
1238					value >> 16,
1239					value >> 24
1240				}
1241			};
1242			DSGXWriteFIFO(gx, entry);
1243		} else {
1244			mLOG(DS_GX, STUB, "Unimplemented GX write %03X:%08X", address, value);
1245		}
1246		break;
1247	}
1248	return value;
1249}
1250
1251void DSGXFlush(struct DSGX* gx) {
1252	if (gx->swapBuffers) {
1253		gx->renderer->setRAM(gx->renderer, gx->vertexBuffer[gx->bufferIndex], gx->polygonBuffer[gx->bufferIndex], gx->polygonIndex, gx->wSort);
1254		gx->swapBuffers = false;
1255		gx->bufferIndex ^= 1;
1256		gx->vertexIndex = 0;
1257		gx->polygonIndex = 0;
1258		if (CircleBufferSize(&gx->fifo)) {
1259			mTimingSchedule(&gx->p->ds9.timing, &gx->fifoEvent, 0);
1260		}
1261	}
1262
1263	DSGXUpdateGXSTAT(gx);
1264}
1265
1266void DSGXScheduleDMA(struct DSCommon* dscore, int number, struct GBADMA* info) {
1267	UNUSED(info);
1268	dscore->p->gx.dmaSource = number;
1269}
1270
1271static void DSGXDummyRendererInit(struct DSGXRenderer* renderer) {
1272	UNUSED(renderer);
1273	// Nothing to do
1274}
1275
1276static void DSGXDummyRendererReset(struct DSGXRenderer* renderer) {
1277	UNUSED(renderer);
1278	// Nothing to do
1279}
1280
1281static void DSGXDummyRendererDeinit(struct DSGXRenderer* renderer) {
1282	UNUSED(renderer);
1283	// Nothing to do
1284}
1285
1286static void DSGXDummyRendererInvalidateTex(struct DSGXRenderer* renderer, int slot) {
1287	UNUSED(renderer);
1288	UNUSED(slot);
1289	// Nothing to do
1290}
1291
1292static void DSGXDummyRendererSetRAM(struct DSGXRenderer* renderer, struct DSGXVertex* verts, struct DSGXPolygon* polys, unsigned polyCount, bool wSort) {
1293	UNUSED(renderer);
1294	UNUSED(verts);
1295	UNUSED(polys);
1296	UNUSED(polyCount);
1297	UNUSED(wSort);
1298	// Nothing to do
1299}
1300
1301static void DSGXDummyRendererDrawScanline(struct DSGXRenderer* renderer, int y) {
1302	UNUSED(renderer);
1303	UNUSED(y);
1304	// Nothing to do
1305}
1306
1307static void DSGXDummyRendererGetScanline(struct DSGXRenderer* renderer, int y, const color_t** output) {
1308	UNUSED(renderer);
1309	UNUSED(y);
1310	*output = NULL;
1311	// Nothing to do
1312}