all repos — mgba @ 5a1a04a353c1b7a93791a7bf0dfc471d645efdaf

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 "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
 49union GBAOAM {
 50	struct GBAObj {
 51		unsigned y : 8;
 52		unsigned transformed : 1;
 53		unsigned disable : 1;
 54		enum ObjMode mode : 2;
 55		unsigned mosaic : 1;
 56		unsigned multipalette : 1;
 57		enum ObjShape shape : 2;
 58
 59		int x : 9;
 60		int : 3;
 61		unsigned hflip : 1;
 62		unsigned vflip : 1;
 63		unsigned size : 2;
 64
 65		unsigned tile : 10;
 66		unsigned priority : 2;
 67		unsigned palette : 4;
 68
 69		int : 16;
 70	} obj[128];
 71
 72	struct GBATransformedObj {
 73		unsigned y : 8;
 74		unsigned transformed : 1;
 75		unsigned doublesize : 1;
 76		enum ObjMode mode : 2;
 77		unsigned mosaic : 1;
 78		unsigned multipalette : 1;
 79		enum ObjShape shape : 2;
 80
 81		int x : 9;
 82		unsigned matIndex : 5;
 83		unsigned size : 2;
 84
 85		unsigned tile : 10;
 86		unsigned priority : 2;
 87		unsigned palette : 4;
 88
 89		int : 16;
 90	} tobj[128];
 91
 92	struct GBAOAMMatrix {
 93		int : 16;
 94		int : 16;
 95		int : 16;
 96		int a : 16;
 97		int : 16;
 98		int : 16;
 99		int : 16;
100		int b : 16;
101		int : 16;
102		int : 16;
103		int : 16;
104		int c : 16;
105		int : 16;
106		int : 16;
107		int : 16;
108		int d : 16;
109	} mat[32];
110
111	uint16_t raw[512];
112};
113
114#define GBA_TEXT_MAP_TILE(MAP) ((MAP) & 0x03FF)
115#define GBA_TEXT_MAP_HFLIP(MAP) ((MAP) & 0x0400)
116#define GBA_TEXT_MAP_VFLIP(MAP) ((MAP) & 0x0800)
117#define GBA_TEXT_MAP_PALETTE(MAP) (((MAP) & 0xF000) >> 12)
118
119union GBARegisterDISPCNT {
120	struct {
121		unsigned mode : 3;
122		unsigned cgb : 1;
123		unsigned frameSelect : 1;
124		unsigned hblankIntervalFree : 1;
125		unsigned objCharacterMapping : 1;
126		unsigned forcedBlank : 1;
127		unsigned bg0Enable : 1;
128		unsigned bg1Enable : 1;
129		unsigned bg2Enable : 1;
130		unsigned bg3Enable : 1;
131		unsigned objEnable : 1;
132		unsigned win0Enable : 1;
133		unsigned win1Enable : 1;
134		unsigned objwinEnable : 1;
135	};
136	uint16_t packed;
137};
138
139union GBARegisterBGCNT {
140	struct {
141		unsigned priority : 2;
142		unsigned charBase : 2;
143		unsigned : 2;
144		unsigned mosaic : 1;
145		unsigned multipalette : 1;
146		unsigned screenBase : 5;
147		unsigned overflow : 1;
148		unsigned size : 2;
149	};
150	uint16_t packed;
151};
152
153struct GBAVideoRenderer {
154	void (*init)(struct GBAVideoRenderer* renderer);
155	void (*deinit)(struct GBAVideoRenderer* renderer);
156
157	uint16_t (*writeVideoRegister)(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
158	void (*writePalette)(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
159	void (*writeOAM)(struct GBAVideoRenderer* renderer, uint32_t oam);
160	void (*drawScanline)(struct GBAVideoRenderer* renderer, int y);
161	void (*finishFrame)(struct GBAVideoRenderer* renderer);
162
163	uint16_t* palette;
164	uint16_t* vram;
165	union GBAOAM* oam;
166};
167
168struct GBAVideo {
169	struct GBA* p;
170	struct GBAVideoRenderer* renderer;
171
172	// DISPSTAT
173	int inHblank;
174	int inVblank;
175	int vcounter;
176	int vblankIRQ;
177	int hblankIRQ;
178	int vcounterIRQ;
179	int vcountSetting;
180
181	// VCOUNT
182	int vcount;
183
184	int32_t lastHblank;
185	int32_t nextHblank;
186	int32_t nextEvent;
187	int32_t eventDiff;
188
189	int32_t nextHblankIRQ;
190	int32_t nextVblankIRQ;
191	int32_t nextVcounterIRQ;
192
193	uint16_t palette[SIZE_PALETTE_RAM >> 1];
194	uint16_t* vram;
195	union GBAOAM oam;
196};
197
198void GBAVideoInit(struct GBAVideo* video);
199void GBAVideoDeinit(struct GBAVideo* video);
200void GBAVideoAssociateRenderer(struct GBAVideo* video, struct GBAVideoRenderer* renderer);
201int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles);
202
203void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value);
204uint16_t GBAVideoReadDISPSTAT(struct GBAVideo* video);
205
206struct GBASerializedState;
207void GBAVideoSerialize(struct GBAVideo* video, struct GBASerializedState* state);
208void GBAVideoDeserialize(struct GBAVideo* video, struct GBASerializedState* state);
209
210#endif