all repos — mgba @ b99d8164ddba2e6f8fc16de941d1551674974e4c

mGBA Game Boy Advance Emulator

include/mgba/internal/gba/video.h (view raw)

  1/* Copyright (c) 2013-2015 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#ifndef GBA_VIDEO_H
  7#define GBA_VIDEO_H
  8
  9#include <mgba-util/common.h>
 10
 11CXX_GUARD_START
 12
 13#include <mgba/core/log.h>
 14#include <mgba/core/timing.h>
 15#include <mgba/gba/interface.h>
 16
 17mLOG_DECLARE_CATEGORY(GBA_VIDEO);
 18
 19enum {
 20	VIDEO_HBLANK_PIXELS = 68,
 21	VIDEO_HDRAW_LENGTH = 1006,
 22	VIDEO_HBLANK_LENGTH = 226,
 23	VIDEO_HORIZONTAL_LENGTH = 1232,
 24
 25	VIDEO_VBLANK_PIXELS = 68,
 26	VIDEO_VERTICAL_TOTAL_PIXELS = 228,
 27
 28	VIDEO_TOTAL_LENGTH = 280896,
 29
 30	OBJ_HBLANK_FREE_LENGTH = 954,
 31	OBJ_LENGTH = 1210,
 32
 33	BASE_TILE = 0x00010000
 34};
 35
 36enum GBAVideoObjMode {
 37	OBJ_MODE_NORMAL = 0,
 38	OBJ_MODE_SEMITRANSPARENT = 1,
 39	OBJ_MODE_OBJWIN = 2
 40};
 41
 42enum GBAVideoObjShape {
 43	OBJ_SHAPE_SQUARE = 0,
 44	OBJ_SHAPE_HORIZONTAL = 1,
 45	OBJ_SHAPE_VERTICAL = 2
 46};
 47
 48enum GBAVideoBlendEffect {
 49	BLEND_NONE = 0,
 50	BLEND_ALPHA = 1,
 51	BLEND_BRIGHTEN = 2,
 52	BLEND_DARKEN = 3
 53};
 54
 55DECL_BITFIELD(GBAObjAttributesA, uint16_t);
 56DECL_BITS(GBAObjAttributesA, Y, 0, 8);
 57DECL_BIT(GBAObjAttributesA, Transformed, 8);
 58DECL_BIT(GBAObjAttributesA, Disable, 9);
 59DECL_BIT(GBAObjAttributesA, DoubleSize, 9);
 60DECL_BITS(GBAObjAttributesA, Mode, 10, 2);
 61DECL_BIT(GBAObjAttributesA, Mosaic, 12);
 62DECL_BIT(GBAObjAttributesA, 256Color, 13);
 63DECL_BITS(GBAObjAttributesA, Shape, 14, 2);
 64
 65DECL_BITFIELD(GBAObjAttributesB, uint16_t);
 66DECL_BITS(GBAObjAttributesB, X, 0, 9);
 67DECL_BITS(GBAObjAttributesB, MatIndex, 9, 5);
 68DECL_BIT(GBAObjAttributesB, HFlip, 12);
 69DECL_BIT(GBAObjAttributesB, VFlip, 13);
 70DECL_BITS(GBAObjAttributesB, Size, 14, 2);
 71
 72DECL_BITFIELD(GBAObjAttributesC, uint16_t);
 73DECL_BITS(GBAObjAttributesC, Tile, 0, 10);
 74DECL_BITS(GBAObjAttributesC, Priority, 10, 2);
 75DECL_BITS(GBAObjAttributesC, Palette, 12, 4);
 76
 77struct GBAObj {
 78	GBAObjAttributesA a;
 79	GBAObjAttributesB b;
 80	GBAObjAttributesC c;
 81	uint16_t d;
 82};
 83
 84struct GBAOAMMatrix {
 85	int16_t padding0[3];
 86	int16_t a;
 87	int16_t padding1[3];
 88	int16_t b;
 89	int16_t padding2[3];
 90	int16_t c;
 91	int16_t padding3[3];
 92	int16_t d;
 93};
 94
 95union GBAOAM {
 96	struct GBAObj obj[128];
 97	struct GBAOAMMatrix mat[32];
 98	uint16_t raw[512];
 99};
100
101struct GBAVideoWindowRegion {
102	uint8_t end;
103	uint8_t start;
104};
105
106#define GBA_TEXT_MAP_TILE(MAP) ((MAP) & 0x03FF)
107#define GBA_TEXT_MAP_HFLIP(MAP) ((MAP) & 0x0400)
108#define GBA_TEXT_MAP_VFLIP(MAP) ((MAP) & 0x0800)
109#define GBA_TEXT_MAP_PALETTE(MAP) (((MAP) & 0xF000) >> 12)
110
111DECL_BITFIELD(GBARegisterDISPCNT, uint16_t);
112DECL_BITS(GBARegisterDISPCNT, Mode, 0, 3);
113DECL_BIT(GBARegisterDISPCNT, Cgb, 3);
114DECL_BIT(GBARegisterDISPCNT, FrameSelect, 4);
115DECL_BIT(GBARegisterDISPCNT, HblankIntervalFree, 5);
116DECL_BIT(GBARegisterDISPCNT, ObjCharacterMapping, 6);
117DECL_BIT(GBARegisterDISPCNT, ForcedBlank, 7);
118DECL_BIT(GBARegisterDISPCNT, Bg0Enable, 8);
119DECL_BIT(GBARegisterDISPCNT, Bg1Enable, 9);
120DECL_BIT(GBARegisterDISPCNT, Bg2Enable, 10);
121DECL_BIT(GBARegisterDISPCNT, Bg3Enable, 11);
122DECL_BIT(GBARegisterDISPCNT, ObjEnable, 12);
123DECL_BIT(GBARegisterDISPCNT, Win0Enable, 13);
124DECL_BIT(GBARegisterDISPCNT, Win1Enable, 14);
125DECL_BIT(GBARegisterDISPCNT, ObjwinEnable, 15);
126
127DECL_BITFIELD(GBARegisterDISPSTAT, uint16_t);
128DECL_BIT(GBARegisterDISPSTAT, InVblank, 0);
129DECL_BIT(GBARegisterDISPSTAT, InHblank, 1);
130DECL_BIT(GBARegisterDISPSTAT, Vcounter, 2);
131DECL_BIT(GBARegisterDISPSTAT, VblankIRQ, 3);
132DECL_BIT(GBARegisterDISPSTAT, HblankIRQ, 4);
133DECL_BIT(GBARegisterDISPSTAT, VcounterIRQ, 5);
134DECL_BITS(GBARegisterDISPSTAT, VcountSetting, 8, 8);
135
136DECL_BITFIELD(GBARegisterBGCNT, uint16_t);
137DECL_BITS(GBARegisterBGCNT, Priority, 0, 2);
138DECL_BITS(GBARegisterBGCNT, CharBase, 2, 2);
139DECL_BIT(GBARegisterBGCNT, Mosaic, 6);
140DECL_BIT(GBARegisterBGCNT, 256Color, 7);
141DECL_BITS(GBARegisterBGCNT, ScreenBase, 8, 5);
142DECL_BIT(GBARegisterBGCNT, Overflow, 13);
143DECL_BITS(GBARegisterBGCNT, Size, 14, 2);
144
145DECL_BITFIELD(GBARegisterBLDCNT, uint16_t);
146DECL_BIT(GBARegisterBLDCNT, Target1Bg0, 0);
147DECL_BIT(GBARegisterBLDCNT, Target1Bg1, 1);
148DECL_BIT(GBARegisterBLDCNT, Target1Bg2, 2);
149DECL_BIT(GBARegisterBLDCNT, Target1Bg3, 3);
150DECL_BIT(GBARegisterBLDCNT, Target1Obj, 4);
151DECL_BIT(GBARegisterBLDCNT, Target1Bd, 5);
152DECL_BITS(GBARegisterBLDCNT, Effect, 6, 2);
153DECL_BIT(GBARegisterBLDCNT, Target2Bg0, 8);
154DECL_BIT(GBARegisterBLDCNT, Target2Bg1, 9);
155DECL_BIT(GBARegisterBLDCNT, Target2Bg2, 10);
156DECL_BIT(GBARegisterBLDCNT, Target2Bg3, 11);
157DECL_BIT(GBARegisterBLDCNT, Target2Obj, 12);
158DECL_BIT(GBARegisterBLDCNT, Target2Bd, 13);
159
160DECL_BITFIELD(GBAWindowControl, uint8_t);
161DECL_BIT(GBAWindowControl, Bg0Enable, 0);
162DECL_BIT(GBAWindowControl, Bg1Enable, 1);
163DECL_BIT(GBAWindowControl, Bg2Enable, 2);
164DECL_BIT(GBAWindowControl, Bg3Enable, 3);
165DECL_BIT(GBAWindowControl, ObjEnable, 4);
166DECL_BIT(GBAWindowControl, BlendEnable, 5);
167
168DECL_BITFIELD(GBAMosaicControl, uint16_t);
169DECL_BITS(GBAMosaicControl, BgH, 0, 4);
170DECL_BITS(GBAMosaicControl, BgV, 4, 4);
171DECL_BITS(GBAMosaicControl, ObjH, 8, 4);
172DECL_BITS(GBAMosaicControl, ObjV, 12, 4);
173
174struct GBAVideoRenderer {
175	void (*init)(struct GBAVideoRenderer* renderer);
176	void (*reset)(struct GBAVideoRenderer* renderer);
177	void (*deinit)(struct GBAVideoRenderer* renderer);
178
179	uint16_t (*writeVideoRegister)(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
180	void (*writeVRAM)(struct GBAVideoRenderer* renderer, uint32_t address);
181	void (*writePalette)(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
182	void (*writeOAM)(struct GBAVideoRenderer* renderer, uint32_t oam);
183	void (*drawScanline)(struct GBAVideoRenderer* renderer, int y);
184	void (*finishFrame)(struct GBAVideoRenderer* renderer);
185
186	void (*getPixels)(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels);
187	void (*putPixels)(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels);
188
189	uint16_t* palette;
190	uint16_t* vram;
191	union GBAOAM* oam;
192	struct mCacheSet* cache;
193
194	bool disableBG[4];
195	bool disableOBJ;
196
197	bool highlightBG[4];
198	bool highlightOBJ[128];
199	color_t highlightColor;
200	uint8_t highlightAmount;
201};
202
203struct GBAVideo {
204	struct GBA* p;
205	struct GBAVideoRenderer* renderer;
206	struct mTimingEvent event;
207
208	// VCOUNT
209	int vcount;
210
211	uint16_t palette[512];
212	uint16_t* vram;
213	union GBAOAM oam;
214
215	int32_t frameCounter;
216	int frameskip;
217	int frameskipCounter;
218};
219
220void GBAVideoInit(struct GBAVideo* video);
221void GBAVideoReset(struct GBAVideo* video);
222void GBAVideoDeinit(struct GBAVideo* video);
223void GBAVideoAssociateRenderer(struct GBAVideo* video, struct GBAVideoRenderer* renderer);
224
225void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value);
226
227struct GBASerializedState;
228void GBAVideoSerialize(const struct GBAVideo* video, struct GBASerializedState* state);
229void GBAVideoDeserialize(struct GBAVideo* video, const struct GBASerializedState* state);
230
231extern const int GBAVideoObjSizes[16][2];
232
233CXX_GUARD_END
234
235#endif