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