all repos — mgba @ 2f1cb61d0197893cf0c3861e9130ff1aeaefb0b0

mGBA Game Boy Advance Emulator

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