all repos — mgba @ 89bb9c7eac58f3b7ded18b108b69e51952a14697

mGBA Game Boy Advance Emulator

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
  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 (*reset)(struct GBAVideoRenderer* renderer);
173	void (*deinit)(struct GBAVideoRenderer* renderer);
174
175	uint16_t (*writeVideoRegister)(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
176	void (*writePalette)(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
177	void (*writeOAM)(struct GBAVideoRenderer* renderer, uint32_t oam);
178	void (*drawScanline)(struct GBAVideoRenderer* renderer, int y);
179	void (*finishFrame)(struct GBAVideoRenderer* renderer);
180
181	void (*getPixels)(struct GBAVideoRenderer* renderer, unsigned* stride, void** pixels);
182
183	uint16_t* palette;
184	uint16_t* vram;
185	union GBAOAM* oam;
186};
187
188struct GBAVideo {
189	struct GBA* p;
190	struct GBAVideoRenderer* renderer;
191
192	// DISPSTAT
193	int inHblank;
194	int inVblank;
195	int vcounter;
196	int vblankIRQ;
197	int hblankIRQ;
198	int vcounterIRQ;
199	int vcountSetting;
200
201	// VCOUNT
202	int vcount;
203
204	int32_t lastHblank;
205	int32_t nextHblank;
206	int32_t nextEvent;
207	int32_t eventDiff;
208
209	int32_t nextHblankIRQ;
210	int32_t nextVblankIRQ;
211	int32_t nextVcounterIRQ;
212
213	uint16_t palette[SIZE_PALETTE_RAM >> 1];
214	uint16_t* vram;
215	union GBAOAM oam;
216};
217
218void GBAVideoInit(struct GBAVideo* video);
219void GBAVideoReset(struct GBAVideo* video);
220void GBAVideoDeinit(struct GBAVideo* video);
221void GBAVideoAssociateRenderer(struct GBAVideo* video, struct GBAVideoRenderer* renderer);
222int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles);
223
224void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value);
225
226struct GBASerializedState;
227void GBAVideoSerialize(struct GBAVideo* video, struct GBASerializedState* state);
228void GBAVideoDeserialize(struct GBAVideo* video, struct GBASerializedState* state);
229
230#endif