src/gba/gba-video.h (view raw)
1#ifndef GBA_VIDEO_H
2#define GBA_VIDEO_H
3
4#include "gba-memory.h"
5
6#include <stdint.h>
7
8enum {
9 VIDEO_CYCLES_PER_PIXEL = 4,
10
11 VIDEO_HORIZONTAL_PIXELS = 240,
12 VIDEO_HBLANK_PIXELS = 68,
13 VIDEO_HDRAW_LENGTH = 1006,
14 VIDEO_HBLANK_LENGTH = 226,
15 VIDEO_HORIZONTAL_LENGTH = 1232,
16
17 VIDEO_VERTICAL_PIXELS = 160,
18 VIDEO_VBLANK_PIXELS = 68,
19 VIDEO_VERTICAL_TOTAL_PIXELS = 228,
20
21 VIDEO_TOTAL_LENGTH = 280896,
22
23 REG_DISPSTAT_MASK = 0xFF38,
24
25 BASE_TILE = 0x00010000
26};
27
28enum ObjMode {
29 OBJ_MODE_NORMAL = 0,
30 OBJ_MODE_SEMITRANSPARENT = 1,
31 OBJ_MODE_OBJWIN = 2
32};
33
34enum ObjShape {
35 OBJ_SHAPE_SQUARE = 0,
36 OBJ_SHAPE_HORIZONTAL = 1,
37 OBJ_SHAPE_VERTICAL = 2
38};
39
40union GBAColor {
41 struct {
42 unsigned r : 5;
43 unsigned g : 5;
44 unsigned b : 5;
45 };
46 uint16_t packed;
47};
48
49struct GBAObj {
50 unsigned y : 8;
51 unsigned transformed : 1;
52 unsigned disable : 1;
53 enum ObjMode mode : 2;
54 unsigned mosaic : 1;
55 unsigned multipalette : 1;
56 enum ObjShape shape : 2;
57
58 int x : 9;
59 int : 3;
60 unsigned hflip : 1;
61 unsigned vflip : 1;
62 unsigned size : 2;
63
64 unsigned tile : 10;
65 unsigned priority : 2;
66 unsigned palette : 4;
67
68 int : 16;
69};
70
71struct GBATransformedObj {
72 unsigned y : 8;
73 unsigned transformed : 1;
74 unsigned doublesize : 1;
75 enum ObjMode mode : 2;
76 unsigned mosaic : 1;
77 unsigned multipalette : 1;
78 enum ObjShape shape : 2;
79
80 int x : 9;
81 unsigned matIndex : 5;
82 unsigned size : 2;
83
84 unsigned tile : 10;
85 unsigned priority : 2;
86 unsigned palette : 4;
87
88 int : 16;
89};
90
91union GBAOAM {
92 struct GBAObj obj[128];
93 struct GBATransformedObj tobj[128];
94
95 struct GBAOAMMatrix {
96 int : 16;
97 int : 16;
98 int : 16;
99 int a : 16;
100 int : 16;
101 int : 16;
102 int : 16;
103 int b : 16;
104 int : 16;
105 int : 16;
106 int : 16;
107 int c : 16;
108 int : 16;
109 int : 16;
110 int : 16;
111 int d : 16;
112 } mat[32];
113
114 uint16_t raw[512];
115};
116
117#define GBA_TEXT_MAP_TILE(MAP) ((MAP) & 0x03FF)
118#define GBA_TEXT_MAP_HFLIP(MAP) ((MAP) & 0x0400)
119#define GBA_TEXT_MAP_VFLIP(MAP) ((MAP) & 0x0800)
120#define GBA_TEXT_MAP_PALETTE(MAP) (((MAP) & 0xF000) >> 12)
121
122union GBARegisterDISPCNT {
123 struct {
124 unsigned mode : 3;
125 unsigned cgb : 1;
126 unsigned frameSelect : 1;
127 unsigned hblankIntervalFree : 1;
128 unsigned objCharacterMapping : 1;
129 unsigned forcedBlank : 1;
130 unsigned bg0Enable : 1;
131 unsigned bg1Enable : 1;
132 unsigned bg2Enable : 1;
133 unsigned bg3Enable : 1;
134 unsigned objEnable : 1;
135 unsigned win0Enable : 1;
136 unsigned win1Enable : 1;
137 unsigned objwinEnable : 1;
138 };
139 uint16_t packed;
140};
141
142union GBARegisterDISPSTAT {
143 struct {
144 unsigned inVblank : 1;
145 unsigned inHblank : 1;
146 unsigned vcounter : 1;
147 unsigned vblankIRQ : 1;
148 unsigned hblankIRQ : 1;
149 unsigned vcounterIRQ : 1;
150 unsigned : 2;
151 unsigned vcountSetting : 8;
152 };
153 uint32_t packed;
154};
155
156union GBARegisterBGCNT {
157 struct {
158 unsigned priority : 2;
159 unsigned charBase : 2;
160 unsigned : 2;
161 unsigned mosaic : 1;
162 unsigned multipalette : 1;
163 unsigned screenBase : 5;
164 unsigned overflow : 1;
165 unsigned size : 2;
166 };
167 uint16_t packed;
168};
169
170struct GBAVideoRenderer {
171 void (*init)(struct GBAVideoRenderer* renderer);
172 void (*deinit)(struct GBAVideoRenderer* renderer);
173
174 uint16_t (*writeVideoRegister)(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
175 void (*writePalette)(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
176 void (*writeOAM)(struct GBAVideoRenderer* renderer, uint32_t oam);
177 void (*drawScanline)(struct GBAVideoRenderer* renderer, int y);
178 void (*finishFrame)(struct GBAVideoRenderer* renderer);
179
180 uint16_t* palette;
181 uint16_t* vram;
182 union GBAOAM* oam;
183};
184
185struct GBAVideo {
186 struct GBA* p;
187 struct GBAVideoRenderer* renderer;
188
189 // DISPSTAT
190 int inHblank;
191 int inVblank;
192 int vcounter;
193 int vblankIRQ;
194 int hblankIRQ;
195 int vcounterIRQ;
196 int vcountSetting;
197
198 // VCOUNT
199 int vcount;
200
201 int32_t lastHblank;
202 int32_t nextHblank;
203 int32_t nextEvent;
204 int32_t eventDiff;
205
206 int32_t nextHblankIRQ;
207 int32_t nextVblankIRQ;
208 int32_t nextVcounterIRQ;
209
210 uint16_t palette[SIZE_PALETTE_RAM >> 1];
211 uint16_t* vram;
212 union GBAOAM oam;
213};
214
215void GBAVideoInit(struct GBAVideo* video);
216void GBAVideoDeinit(struct GBAVideo* video);
217void GBAVideoAssociateRenderer(struct GBAVideo* video, struct GBAVideoRenderer* renderer);
218int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles);
219
220void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value);
221uint16_t GBAVideoReadDISPSTAT(struct GBAVideo* video);
222
223struct GBASerializedState;
224void GBAVideoSerialize(struct GBAVideo* video, struct GBASerializedState* state);
225void GBAVideoDeserialize(struct GBAVideo* video, struct GBASerializedState* state);
226
227#endif