all repos — mgba @ 2b6ed2661e880a41bbd9d064794a59b9e44ef353

mGBA Game Boy Advance Emulator

include/mgba/internal/ds/gx.h (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#ifndef DS_GX_H
  7#define DS_GX_H
  8
  9#include <mgba-util/common.h>
 10
 11CXX_GUARD_START
 12
 13#include <mgba/core/interface.h>
 14#include <mgba/core/log.h>
 15#include <mgba/core/timing.h>
 16#include <mgba/internal/ds/matrix.h>
 17#include <mgba-util/circle-buffer.h>
 18
 19mLOG_DECLARE_CATEGORY(DS_GX);
 20
 21#define DS_GX_POLYGON_BUFFER_SIZE 2048
 22#define DS_GX_VERTEX_BUFFER_SIZE 6144
 23
 24DECL_BITFIELD(DSRegGXSTAT, uint32_t);
 25DECL_BIT(DSRegGXSTAT, TestBusy, 0);
 26DECL_BIT(DSRegGXSTAT, BoxTestResult, 1);
 27DECL_BITS(DSRegGXSTAT, PVMatrixStackLevel, 8, 5);
 28DECL_BIT(DSRegGXSTAT, ProjMatrixStackLevel, 13);
 29DECL_BIT(DSRegGXSTAT, MatrixStackBusy, 14);
 30DECL_BIT(DSRegGXSTAT, MatrixStackError, 15);
 31DECL_BITS(DSRegGXSTAT, FIFOEntries, 16, 9);
 32DECL_BIT(DSRegGXSTAT, FIFOFull, 24);
 33DECL_BIT(DSRegGXSTAT, FIFOLtHalf, 25);
 34DECL_BIT(DSRegGXSTAT, FIFOEmpty, 26);
 35DECL_BIT(DSRegGXSTAT, Busy, 27);
 36DECL_BITS(DSRegGXSTAT, DoIRQ, 30, 2);
 37
 38DECL_BITFIELD(DSGXTexParams, uint32_t);
 39DECL_BITS(DSGXTexParams, VRAMBase, 0, 16);
 40DECL_BIT(DSGXTexParams, SRepeat, 16);
 41DECL_BIT(DSGXTexParams, TRepeat, 17);
 42DECL_BIT(DSGXTexParams, SMirror, 18);
 43DECL_BIT(DSGXTexParams, TMirror, 19);
 44DECL_BITS(DSGXTexParams, SSize, 20, 3);
 45DECL_BITS(DSGXTexParams, TSize, 23, 3);
 46DECL_BITS(DSGXTexParams, Format, 26, 3);
 47DECL_BIT(DSGXTexParams, 0Transparent, 29);
 48DECL_BITS(DSGXTexParams, CoordTfMode, 30, 2);
 49
 50DECL_BITFIELD(DSGXPolygonAttrs, uint32_t);
 51DECL_BITS(DSGXPolygonAttrs, Lights, 0, 4);
 52DECL_BITS(DSGXPolygonAttrs, Mode, 4, 2);
 53DECL_BIT(DSGXPolygonAttrs, FrontFace, 6);
 54DECL_BIT(DSGXPolygonAttrs, BackFace, 7);
 55DECL_BIT(DSGXPolygonAttrs, UpdateDepth, 11);
 56// TODO
 57DECL_BITS(DSGXPolygonAttrs, Alpha, 16, 5);
 58DECL_BITS(DSGXPolygonAttrs, Id, 24, 6);
 59
 60enum DSGXCommand {
 61	DS_GX_CMD_NOP = 0,
 62	DS_GX_CMD_MTX_MODE = 0x10,
 63	DS_GX_CMD_MTX_PUSH = 0x11,
 64	DS_GX_CMD_MTX_POP = 0x12,
 65	DS_GX_CMD_MTX_STORE = 0x13,
 66	DS_GX_CMD_MTX_RESTORE = 0x14,
 67	DS_GX_CMD_MTX_IDENTITY = 0x15,
 68	DS_GX_CMD_MTX_LOAD_4x4 = 0x16,
 69	DS_GX_CMD_MTX_LOAD_4x3 = 0x17,
 70	DS_GX_CMD_MTX_MULT_4x4 = 0x18,
 71	DS_GX_CMD_MTX_MULT_4x3 = 0x19,
 72	DS_GX_CMD_MTX_MULT_3x3 = 0x1A,
 73	DS_GX_CMD_MTX_SCALE = 0x1B,
 74	DS_GX_CMD_MTX_TRANS = 0x1C,
 75	DS_GX_CMD_COLOR = 0x20,
 76	DS_GX_CMD_NORMAL = 0x21,
 77	DS_GX_CMD_TEXCOORD = 0x22,
 78	DS_GX_CMD_VTX_16 = 0x23,
 79	DS_GX_CMD_VTX_10 = 0x24,
 80	DS_GX_CMD_VTX_XY = 0x25,
 81	DS_GX_CMD_VTX_XZ = 0x26,
 82	DS_GX_CMD_VTX_YZ = 0x27,
 83	DS_GX_CMD_VTX_DIFF = 0x28,
 84	DS_GX_CMD_POLYGON_ATTR = 0x29,
 85	DS_GX_CMD_TEXIMAGE_PARAM = 0x2A,
 86	DS_GX_CMD_PLTT_BASE = 0x2B,
 87	DS_GX_CMD_DIF_AMB = 0x30,
 88	DS_GX_CMD_SPE_EMI = 0x31,
 89	DS_GX_CMD_LIGHT_VECTOR = 0x32,
 90	DS_GX_CMD_LIGHT_COLOR = 0x33,
 91	DS_GX_CMD_SHININESS = 0x34,
 92	DS_GX_CMD_BEGIN_VTXS = 0x40,
 93	DS_GX_CMD_END_VTXS = 0x41,
 94	DS_GX_CMD_SWAP_BUFFERS = 0x50,
 95	DS_GX_CMD_VIEWPORT = 0x60,
 96	DS_GX_CMD_BOX_TEST = 0x70,
 97	DS_GX_CMD_POS_TEST = 0x71,
 98	DS_GX_CMD_VEC_TEST = 0x72,
 99
100	DS_GX_CMD_MAX
101};
102
103#pragma pack(push, 1)
104struct DSGXEntry {
105	uint8_t command;
106	uint8_t params[4];
107};
108#pragma pack(pop)
109
110struct DSGXVertex {
111	// World coords
112	int16_t coord[3]; // 4.12
113
114	// Color/Texcoords
115	uint16_t color; // 5.5.5
116	int16_t s; // 12.4
117	int16_t t; // 12.4
118
119	// Viewport coords
120	int32_t viewCoord[4];
121	int16_t vs; // 12.4
122	int16_t vt; // 12.4
123};
124
125struct DSGXPolygon {
126	uint32_t polyParams;
127	DSGXTexParams texParams;
128	uint32_t palBase;
129	int verts;
130	unsigned vertIds[10];
131};
132
133struct DSGXRenderer {
134	void (*init)(struct DSGXRenderer* renderer);
135	void (*reset)(struct DSGXRenderer* renderer);
136	void (*deinit)(struct DSGXRenderer* renderer);
137
138	void (*invalidateTex)(struct DSGXRenderer* renderer, int slot);
139	void (*setRAM)(struct DSGXRenderer* renderer, struct DSGXVertex* verts, struct DSGXPolygon* polys, unsigned polyCount, bool wSort);
140	void (*drawScanline)(struct DSGXRenderer* renderer, int y);
141	void (*getScanline)(struct DSGXRenderer* renderer, int y, const color_t** output);
142
143	uint16_t* tex[4];
144	uint16_t* texPal[6];
145
146	int viewportX;
147	int viewportY;
148	int viewportWidth;
149	int viewportHeight;
150};
151
152struct DSGXLight {
153	int16_t color;
154	int16_t x;
155	int16_t y;
156	int16_t z;
157};
158
159struct DS;
160struct DSGX {
161	struct DS* p;
162	struct DSGXRenderer* renderer;
163	struct CircleBuffer fifo;
164	struct CircleBuffer pipe;
165
166	struct mTimingEvent fifoEvent;
167	int dmaSource;
168
169	int outstandingParams[4];
170	uint8_t outstandingCommand[4];
171	struct DSGXEntry outstandingEntry;
172
173	int activeParams;
174	struct DSGXEntry activeEntries[32];
175
176	bool swapBuffers;
177	bool wSort;
178	int bufferIndex;
179	int vertexIndex;
180	int pendingVertexIndex;
181	int polygonIndex;
182	struct DSGXVertex* vertexBuffer[2];
183	struct DSGXPolygon* polygonBuffer[2];
184	struct DSGXVertex pendingVertices[8];
185	int pendingVertexIds[8];
186	bool reverseWinding;
187
188	uint16_t* tex[4];
189	uint16_t* texPal[6];
190
191	int mtxMode;
192	int pvMatrixPointer;
193	struct DSGXMatrix projMatrixStack;
194	struct DSGXMatrix texMatrixStack;
195	struct DSGXMatrix posMatrixStack[32];
196	struct DSGXMatrix vecMatrixStack[32];
197
198	struct DSGXMatrix projMatrix;
199	struct DSGXMatrix texMatrix;
200	struct DSGXMatrix posMatrix;
201	struct DSGXMatrix vecMatrix;
202
203	struct DSGXMatrix clipMatrix;
204
205	struct DSGXLight lights[4];
206	int16_t diffuse;
207	int16_t ambient;
208	int16_t specular;
209	int16_t emit;
210
211	int viewportX1;
212	int viewportY1;
213	int viewportX2;
214	int viewportY2;
215	int viewportWidth;
216	int viewportHeight;
217
218	int vertexMode;
219	struct DSGXVertex currentVertex;
220	struct DSGXPolygon nextPoly;
221	struct DSGXPolygon currentPoly;
222};
223
224void DSGXInit(struct DSGX*);
225void DSGXDeinit(struct DSGX*);
226void DSGXReset(struct DSGX*);
227void DSGXAssociateRenderer(struct DSGX* video, struct DSGXRenderer* renderer);
228
229uint16_t DSGXWriteRegister(struct DSGX*, uint32_t address, uint16_t value);
230uint32_t DSGXWriteRegister32(struct DSGX*, uint32_t address, uint32_t value);
231
232void DSGXFlush(struct DSGX*);
233void DSGXUpdateGXSTAT(struct DSGX*);
234
235struct GBADMA;
236struct DSCommon;
237void DSGXScheduleDMA(struct DSCommon* dscore, int number, struct GBADMA* info);
238
239CXX_GUARD_END
240
241#endif