all repos — mgba @ 3202811e416e8b7c6ddc8a68442536510aa2b85e

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