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);
55// TODO
56DECL_BITS(DSGXPolygonAttrs, Alpha, 16, 5);
57
58enum DSGXCommand {
59 DS_GX_CMD_NOP = 0,
60 DS_GX_CMD_MTX_MODE = 0x10,
61 DS_GX_CMD_MTX_PUSH = 0x11,
62 DS_GX_CMD_MTX_POP = 0x12,
63 DS_GX_CMD_MTX_STORE = 0x13,
64 DS_GX_CMD_MTX_RESTORE = 0x14,
65 DS_GX_CMD_MTX_IDENTITY = 0x15,
66 DS_GX_CMD_MTX_LOAD_4x4 = 0x16,
67 DS_GX_CMD_MTX_LOAD_4x3 = 0x17,
68 DS_GX_CMD_MTX_MULT_4x4 = 0x18,
69 DS_GX_CMD_MTX_MULT_4x3 = 0x19,
70 DS_GX_CMD_MTX_MULT_3x3 = 0x1A,
71 DS_GX_CMD_MTX_SCALE = 0x1B,
72 DS_GX_CMD_MTX_TRANS = 0x1C,
73 DS_GX_CMD_COLOR = 0x20,
74 DS_GX_CMD_NORMAL = 0x21,
75 DS_GX_CMD_TEXCOORD = 0x22,
76 DS_GX_CMD_VTX_16 = 0x23,
77 DS_GX_CMD_VTX_10 = 0x24,
78 DS_GX_CMD_VTX_XY = 0x25,
79 DS_GX_CMD_VTX_XZ = 0x26,
80 DS_GX_CMD_VTX_YZ = 0x27,
81 DS_GX_CMD_VTX_DIFF = 0x28,
82 DS_GX_CMD_POLYGON_ATTR = 0x29,
83 DS_GX_CMD_TEXIMAGE_PARAM = 0x2A,
84 DS_GX_CMD_PLTT_BASE = 0x2B,
85 DS_GX_CMD_DIF_AMB = 0x30,
86 DS_GX_CMD_SPE_EMI = 0x31,
87 DS_GX_CMD_LIGHT_VECTOR = 0x32,
88 DS_GX_CMD_LIGHT_COLOR = 0x33,
89 DS_GX_CMD_SHININESS = 0x34,
90 DS_GX_CMD_BEGIN_VTXS = 0x40,
91 DS_GX_CMD_END_VTXS = 0x41,
92 DS_GX_CMD_SWAP_BUFFERS = 0x50,
93 DS_GX_CMD_VIEWPORT = 0x60,
94 DS_GX_CMD_BOX_TEST = 0x70,
95 DS_GX_CMD_POS_TEST = 0x71,
96 DS_GX_CMD_VEC_TEST = 0x72,
97
98 DS_GX_CMD_MAX
99};
100
101#pragma pack(push, 1)
102struct DSGXEntry {
103 uint8_t command;
104 uint8_t params[4];
105};
106#pragma pack(pop)
107
108struct DSGXVertex {
109 // World coords
110 int16_t x; // 4.12
111 int16_t y; // 4.12
112 int16_t z; // 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 vx;
121 int32_t vy;
122 int32_t vz;
123 int32_t vw;
124 int16_t vs; // 12.4
125 int16_t vt; // 12.4
126};
127
128struct DSGXPolygon {
129 uint32_t polyParams;
130 DSGXTexParams texParams;
131 uint32_t palBase;
132 int verts;
133 unsigned vertIds[8];
134};
135
136struct DSGXRenderer {
137 void (*init)(struct DSGXRenderer* renderer);
138 void (*reset)(struct DSGXRenderer* renderer);
139 void (*deinit)(struct DSGXRenderer* renderer);
140
141 void (*invalidateTex)(struct DSGXRenderer* renderer, int slot);
142 void (*setRAM)(struct DSGXRenderer* renderer, struct DSGXVertex* verts, struct DSGXPolygon* polys, unsigned polyCount, bool wSort);
143 void (*drawScanline)(struct DSGXRenderer* renderer, int y);
144 void (*getScanline)(struct DSGXRenderer* renderer, int y, const color_t** output);
145
146 uint16_t* tex[4];
147 uint16_t* texPal[6];
148
149 int viewportX;
150 int viewportY;
151 int viewportWidth;
152 int viewportHeight;
153};
154
155struct DSGXLight {
156 int16_t color;
157 int16_t x;
158 int16_t y;
159 int16_t z;
160};
161
162struct DS;
163struct DSGX {
164 struct DS* p;
165 struct DSGXRenderer* renderer;
166 struct CircleBuffer fifo;
167 struct CircleBuffer pipe;
168
169 struct mTimingEvent fifoEvent;
170 int dmaSource;
171
172 int outstandingParams[4];
173 uint8_t outstandingCommand[4];
174 struct DSGXEntry outstandingEntry;
175
176 int activeParams;
177 struct DSGXEntry activeEntries[32];
178
179 bool swapBuffers;
180 bool wSort;
181 int bufferIndex;
182 int vertexIndex;
183 int pendingVertexIndex;
184 int polygonIndex;
185 struct DSGXVertex* vertexBuffer[2];
186 struct DSGXPolygon* polygonBuffer[2];
187 struct DSGXVertex pendingVertices[8];
188 int pendingVertexIds[8];
189 bool reverseWinding;
190
191 uint16_t* tex[4];
192 uint16_t* texPal[6];
193
194 int mtxMode;
195 int pvMatrixPointer;
196 struct DSGXMatrix projMatrixStack;
197 struct DSGXMatrix texMatrixStack;
198 struct DSGXMatrix posMatrixStack[32];
199 struct DSGXMatrix vecMatrixStack[32];
200
201 struct DSGXMatrix projMatrix;
202 struct DSGXMatrix texMatrix;
203 struct DSGXMatrix posMatrix;
204 struct DSGXMatrix vecMatrix;
205
206 struct DSGXMatrix clipMatrix;
207
208 struct DSGXLight lights[4];
209 int16_t diffuse;
210 int16_t ambient;
211 int16_t specular;
212 int16_t emit;
213
214 int viewportX1;
215 int viewportY1;
216 int viewportX2;
217 int viewportY2;
218 int viewportWidth;
219 int viewportHeight;
220
221 int vertexMode;
222 struct DSGXVertex currentVertex;
223 struct DSGXPolygon nextPoly;
224 struct DSGXPolygon currentPoly;
225};
226
227void DSGXInit(struct DSGX*);
228void DSGXDeinit(struct DSGX*);
229void DSGXReset(struct DSGX*);
230void DSGXAssociateRenderer(struct DSGX* video, struct DSGXRenderer* renderer);
231
232uint16_t DSGXWriteRegister(struct DSGX*, uint32_t address, uint16_t value);
233uint32_t DSGXWriteRegister32(struct DSGX*, uint32_t address, uint32_t value);
234
235void DSGXFlush(struct DSGX*);
236void DSGXUpdateGXSTAT(struct DSGX*);
237
238struct GBADMA;
239struct DSCommon;
240void DSGXScheduleDMA(struct DSCommon* dscore, int number, struct GBADMA* info);
241
242CXX_GUARD_END
243
244#endif