src/gba/gba-video.h (view raw)
1#ifndef GBA_VIDEO_H
2#define GBA_VIDEO_H
3
4#include "common.h"
5
6#include "gba-memory.h"
7
8#ifdef COLOR_16_BIT
9#define BYTES_PER_PIXEL 2
10#else
11#define BYTES_PER_PIXEL 4
12#endif
13
14enum {
15 VIDEO_CYCLES_PER_PIXEL = 4,
16
17 VIDEO_HORIZONTAL_PIXELS = 240,
18 VIDEO_HBLANK_PIXELS = 68,
19 VIDEO_HDRAW_LENGTH = 1006,
20 VIDEO_HBLANK_LENGTH = 226,
21 VIDEO_HORIZONTAL_LENGTH = 1232,
22
23 VIDEO_VERTICAL_PIXELS = 160,
24 VIDEO_VBLANK_PIXELS = 68,
25 VIDEO_VERTICAL_TOTAL_PIXELS = 228,
26
27 VIDEO_TOTAL_LENGTH = 280896,
28
29 REG_DISPSTAT_MASK = 0xFF38,
30
31 BASE_TILE = 0x00010000
32};
33
34enum ObjMode {
35 OBJ_MODE_NORMAL = 0,
36 OBJ_MODE_SEMITRANSPARENT = 1,
37 OBJ_MODE_OBJWIN = 2
38};
39
40enum ObjShape {
41 OBJ_SHAPE_SQUARE = 0,
42 OBJ_SHAPE_HORIZONTAL = 1,
43 OBJ_SHAPE_VERTICAL = 2
44};
45
46DECL_BITFIELD(GBAObjAttributesA, uint16_t);
47DECL_BITS(GBAObjAttributesA, Y, 0, 8);
48DECL_BIT(GBAObjAttributesA, Transformed, 8);
49DECL_BIT(GBAObjAttributesA, Disable, 9);
50DECL_BIT(GBAObjAttributesA, DoubleSize, 9);
51DECL_BITS(GBAObjAttributesA, Mode, 10, 2);
52DECL_BIT(GBAObjAttributesA, Mosaic, 12);
53DECL_BIT(GBAObjAttributesA, 256Color, 13);
54DECL_BITS(GBAObjAttributesA, Shape, 14, 2);
55
56
57DECL_BITFIELD(GBAObjAttributesB, uint16_t);
58DECL_BITS(GBAObjAttributesB, X, 0, 9);
59DECL_BITS(GBAObjAttributesB, MatIndex, 9, 5);
60DECL_BIT(GBAObjAttributesB, HFlip, 12);
61DECL_BIT(GBAObjAttributesB, VFlip, 13);
62DECL_BITS(GBAObjAttributesB, Size, 14, 2);
63
64DECL_BITFIELD(GBAObjAttributesC, uint16_t);
65DECL_BITS(GBAObjAttributesC, Tile, 0, 10);
66DECL_BITS(GBAObjAttributesC, Priority, 10, 2);
67DECL_BITS(GBAObjAttributesC, Palette, 12, 4);
68
69struct GBAObj {
70 GBAObjAttributesA a;
71 GBAObjAttributesB b;
72 GBAObjAttributesC c;
73 uint16_t d;
74};
75
76union GBAOAM {
77 struct GBAObj obj[128];
78
79 struct GBAOAMMatrix {
80 int16_t padding0[3];
81 int16_t a;
82 int16_t padding1[3];
83 int16_t b;
84 int16_t padding2[3];
85 int16_t c;
86 int16_t padding3[3];
87 int16_t d;
88 } mat[32];
89
90 uint16_t raw[512];
91};
92
93#define GBA_TEXT_MAP_TILE(MAP) ((MAP) & 0x03FF)
94#define GBA_TEXT_MAP_HFLIP(MAP) ((MAP) & 0x0400)
95#define GBA_TEXT_MAP_VFLIP(MAP) ((MAP) & 0x0800)
96#define GBA_TEXT_MAP_PALETTE(MAP) (((MAP) & 0xF000) >> 12)
97
98DECL_BITFIELD(GBARegisterDISPCNT, uint16_t);
99DECL_BITS(GBARegisterDISPCNT, Mode, 0, 3);
100DECL_BIT(GBARegisterDISPCNT, Cgb, 3);
101DECL_BIT(GBARegisterDISPCNT, FrameSelect, 4);
102DECL_BIT(GBARegisterDISPCNT, HblankIntervalFree, 5);
103DECL_BIT(GBARegisterDISPCNT, ObjCharacterMapping, 6);
104DECL_BIT(GBARegisterDISPCNT, ForcedBlank, 7);
105DECL_BIT(GBARegisterDISPCNT, Bg0Enable, 8);
106DECL_BIT(GBARegisterDISPCNT, Bg1Enable, 9);
107DECL_BIT(GBARegisterDISPCNT, Bg2Enable, 10);
108DECL_BIT(GBARegisterDISPCNT, Bg3Enable, 11);
109DECL_BIT(GBARegisterDISPCNT, ObjEnable, 12);
110DECL_BIT(GBARegisterDISPCNT, Win0Enable, 13);
111DECL_BIT(GBARegisterDISPCNT, Win1Enable, 14);
112DECL_BIT(GBARegisterDISPCNT, ObjwinEnable, 15);
113
114DECL_BITFIELD(GBARegisterDISPSTAT, uint16_t);
115DECL_BIT(GBARegisterDISPSTAT, InVblank, 0);
116DECL_BIT(GBARegisterDISPSTAT, InHblank, 1);
117DECL_BIT(GBARegisterDISPSTAT, Vcounter, 2);
118DECL_BIT(GBARegisterDISPSTAT, VblankIRQ, 3);
119DECL_BIT(GBARegisterDISPSTAT, HblankIRQ, 4);
120DECL_BIT(GBARegisterDISPSTAT, VcounterIRQ, 5);
121DECL_BITS(GBARegisterDISPSTAT, VcountSetting, 8, 8);
122
123DECL_BITFIELD(GBARegisterBGCNT, uint16_t);
124DECL_BITS(GBARegisterBGCNT, Priority, 0, 2);
125DECL_BITS(GBARegisterBGCNT, CharBase, 2, 2);
126DECL_BIT(GBARegisterBGCNT, Mosaic, 6);
127DECL_BIT(GBARegisterBGCNT, 256Color, 7);
128DECL_BITS(GBARegisterBGCNT, ScreenBase, 8, 5);
129DECL_BIT(GBARegisterBGCNT, Overflow, 13);
130DECL_BITS(GBARegisterBGCNT, Size, 14, 2);
131
132struct GBAVideoRenderer {
133 void (*init)(struct GBAVideoRenderer* renderer);
134 void (*reset)(struct GBAVideoRenderer* renderer);
135 void (*deinit)(struct GBAVideoRenderer* renderer);
136
137 uint16_t (*writeVideoRegister)(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
138 void (*writePalette)(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
139 void (*writeOAM)(struct GBAVideoRenderer* renderer, uint32_t oam);
140 void (*drawScanline)(struct GBAVideoRenderer* renderer, int y);
141 void (*finishFrame)(struct GBAVideoRenderer* renderer);
142
143 void (*getPixels)(struct GBAVideoRenderer* renderer, unsigned* stride, void** pixels);
144 void (*putPixels)(struct GBAVideoRenderer* renderer, unsigned stride, void* pixels);
145
146 uint16_t* palette;
147 uint16_t* vram;
148 union GBAOAM* oam;
149};
150
151struct GBAVideo {
152 struct GBA* p;
153 struct GBAVideoRenderer* renderer;
154
155 GBARegisterDISPSTAT dispstat;
156
157 // VCOUNT
158 int vcount;
159
160 int32_t lastHblank;
161 int32_t nextHblank;
162 int32_t nextEvent;
163 int32_t eventDiff;
164
165 int32_t nextHblankIRQ;
166 int32_t nextVblankIRQ;
167 int32_t nextVcounterIRQ;
168
169 uint16_t palette[SIZE_PALETTE_RAM >> 1];
170 uint16_t* vram;
171 union GBAOAM oam;
172};
173
174void GBAVideoInit(struct GBAVideo* video);
175void GBAVideoReset(struct GBAVideo* video);
176void GBAVideoDeinit(struct GBAVideo* video);
177void GBAVideoAssociateRenderer(struct GBAVideo* video, struct GBAVideoRenderer* renderer);
178int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles);
179
180void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value);
181
182struct GBASerializedState;
183void GBAVideoSerialize(struct GBAVideo* video, struct GBASerializedState* state);
184void GBAVideoDeserialize(struct GBAVideo* video, struct GBASerializedState* state);
185
186#endif