all repos — mgba @ 570f2c5f380464ae7f6d468242151d520cb00c15

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