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