all repos — mgba @ e6ea94d2296eae963a48a18d009217a38d92bf9b

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