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 // Viewport coords
87 int32_t x; // 16.16
88 int32_t y; // 16.16
89 int32_t z; // 16.16
90 uint16_t color;
91 // Texcoords
92 int16_t s; // 12.4
93 int16_t t; // 12.4
94};
95
96struct DSGXPolygon {
97 int verts;
98 unsigned vertIds[4];
99};
100
101struct DSGXRenderer {
102 void (*init)(struct DSGXRenderer* renderer);
103 void (*reset)(struct DSGXRenderer* renderer);
104 void (*deinit)(struct DSGXRenderer* renderer);
105
106 void (*setRAM)(struct DSGXRenderer* renderer, struct DSGXVertex* verts, struct DSGXPolygon* polys, unsigned polyCount);
107 void (*drawScanline)(struct DSGXRenderer* renderer, int y);
108 void (*getScanline)(struct DSGXRenderer* renderer, int y, color_t** output);
109};
110
111struct DS;
112struct DSGX {
113 struct DS* p;
114 struct DSGXRenderer* renderer;
115 struct CircleBuffer fifo;
116 struct CircleBuffer pipe;
117
118 struct mTimingEvent fifoEvent;
119
120 int outstandingParams[4];
121 uint8_t outstandingCommand[4];
122
123 int activeParams;
124 struct DSGXEntry activeEntries[32];
125
126 bool swapBuffers;
127 int bufferIndex;
128 struct DSGXVertex* vertexBuffer[2];
129 struct DSGXPolygon* polygonBuffer[2];
130
131 int mtxMode;
132 int pvMatrixPointer;
133 struct DSGXMatrix projMatrixStack;
134 struct DSGXMatrix texMatrixStack;
135 struct DSGXMatrix posMatrixStack[32];
136 struct DSGXMatrix vecMatrixStack[32];
137
138 struct DSGXMatrix projMatrix;
139 struct DSGXMatrix texMatrix;
140 struct DSGXMatrix posMatrix;
141 struct DSGXMatrix vecMatrix;
142};
143
144void DSGXInit(struct DSGX*);
145void DSGXDeinit(struct DSGX*);
146void DSGXReset(struct DSGX*);
147void DSGXAssociateRenderer(struct DSGX* video, struct DSGXRenderer* renderer);
148
149uint16_t DSGXWriteRegister(struct DSGX*, uint32_t address, uint16_t value);
150uint32_t DSGXWriteRegister32(struct DSGX*, uint32_t address, uint32_t value);
151
152void DSGXSwapBuffers(struct DSGX*);
153void DSGXUpdateGXSTAT(struct DSGX*);
154
155CXX_GUARD_END
156
157#endif