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