all repos — mgba @ 4edd7286f39fe940a890394626567211e072badb

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
 11#include "gba/memory.h"
 12#include "macros.h"
 13
 14#ifdef COLOR_16_BIT
 15#define BYTES_PER_PIXEL 2
 16#else
 17#define BYTES_PER_PIXEL 4
 18#endif
 19
 20#define GBA_R5(X) ((X) & 0x1F)
 21#define GBA_G5(X) (((X) >> 5) & 0x1F)
 22#define GBA_B5(X) (((X) >> 10) & 0x1F)
 23
 24#define GBA_R8(X) (((X) << 3) & 0xF8)
 25#define GBA_G8(X) (((X) >> 2) & 0xF8)
 26#define GBA_B8(X) (((X) >> 7) & 0xF8)
 27
 28enum {
 29	VIDEO_HORIZONTAL_PIXELS = 240,
 30	VIDEO_HBLANK_PIXELS = 68,
 31	VIDEO_HDRAW_LENGTH = 1006,
 32	VIDEO_HBLANK_LENGTH = 226,
 33	VIDEO_HORIZONTAL_LENGTH = VIDEO_HDRAW_LENGTH + VIDEO_HBLANK_LENGTH,
 34
 35	VIDEO_VERTICAL_PIXELS = 160,
 36	VIDEO_VBLANK_PIXELS = 68,
 37	VIDEO_VERTICAL_TOTAL_PIXELS = VIDEO_VERTICAL_PIXELS + VIDEO_VBLANK_PIXELS,
 38
 39	VIDEO_TOTAL_LENGTH = VIDEO_HORIZONTAL_LENGTH * VIDEO_VERTICAL_TOTAL_PIXELS,
 40
 41	OBJ_HBLANK_FREE_LENGTH = 954,
 42	OBJ_LENGTH = 1210,
 43
 44	BASE_TILE = 0x00010000
 45};
 46
 47enum ObjMode {
 48	OBJ_MODE_NORMAL = 0,
 49	OBJ_MODE_SEMITRANSPARENT = 1,
 50	OBJ_MODE_OBJWIN = 2
 51};
 52
 53enum ObjShape {
 54	OBJ_SHAPE_SQUARE = 0,
 55	OBJ_SHAPE_HORIZONTAL = 1,
 56	OBJ_SHAPE_VERTICAL = 2
 57};
 58
 59DECL_BITFIELD(GBAObjAttributesA, uint16_t);
 60DECL_BITS(GBAObjAttributesA, Y, 0, 8);
 61DECL_BIT(GBAObjAttributesA, Transformed, 8);
 62DECL_BIT(GBAObjAttributesA, Disable, 9);
 63DECL_BIT(GBAObjAttributesA, DoubleSize, 9);
 64DECL_BITS(GBAObjAttributesA, Mode, 10, 2);
 65DECL_BIT(GBAObjAttributesA, Mosaic, 12);
 66DECL_BIT(GBAObjAttributesA, 256Color, 13);
 67DECL_BITS(GBAObjAttributesA, Shape, 14, 2);
 68
 69DECL_BITFIELD(GBAObjAttributesB, uint16_t);
 70DECL_BITS(GBAObjAttributesB, X, 0, 9);
 71DECL_BITS(GBAObjAttributesB, MatIndex, 9, 5);
 72DECL_BIT(GBAObjAttributesB, HFlip, 12);
 73DECL_BIT(GBAObjAttributesB, VFlip, 13);
 74DECL_BITS(GBAObjAttributesB, Size, 14, 2);
 75
 76DECL_BITFIELD(GBAObjAttributesC, uint16_t);
 77DECL_BITS(GBAObjAttributesC, Tile, 0, 10);
 78DECL_BITS(GBAObjAttributesC, Priority, 10, 2);
 79DECL_BITS(GBAObjAttributesC, Palette, 12, 4);
 80
 81struct GBAObj {
 82	GBAObjAttributesA a;
 83	GBAObjAttributesB b;
 84	GBAObjAttributesC c;
 85	uint16_t d;
 86};
 87
 88union GBAOAM {
 89	struct GBAObj obj[128];
 90
 91	struct GBAOAMMatrix {
 92		int16_t padding0[3];
 93		int16_t a;
 94		int16_t padding1[3];
 95		int16_t b;
 96		int16_t padding2[3];
 97		int16_t c;
 98		int16_t padding3[3];
 99		int16_t d;
100	} mat[32];
101
102	uint16_t raw[512];
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
159struct GBAVideoRenderer {
160	void (*init)(struct GBAVideoRenderer* renderer);
161	void (*reset)(struct GBAVideoRenderer* renderer);
162	void (*deinit)(struct GBAVideoRenderer* renderer);
163
164	uint16_t (*writeVideoRegister)(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
165	void (*writeVRAM)(struct GBAVideoRenderer* renderer, uint32_t address);
166	void (*writePalette)(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
167	void (*writeOAM)(struct GBAVideoRenderer* renderer, uint32_t oam);
168	void (*drawScanline)(struct GBAVideoRenderer* renderer, int y);
169	void (*finishFrame)(struct GBAVideoRenderer* renderer);
170
171	void (*getPixels)(struct GBAVideoRenderer* renderer, unsigned* stride, const void** pixels);
172	void (*putPixels)(struct GBAVideoRenderer* renderer, unsigned stride, void* pixels);
173
174	uint16_t* palette;
175	uint16_t* vram;
176	union GBAOAM* oam;
177
178	bool disableBG[4];
179	bool disableOBJ;
180};
181
182struct GBAVideo {
183	struct GBA* p;
184	struct GBAVideoRenderer* renderer;
185
186	// VCOUNT
187	int vcount;
188
189	int32_t nextHblank;
190	int32_t nextEvent;
191	int32_t eventDiff;
192
193	int32_t nextHblankIRQ;
194	int32_t nextVblankIRQ;
195	int32_t nextVcounterIRQ;
196
197	uint16_t palette[SIZE_PALETTE_RAM >> 1];
198	uint16_t* vram;
199	union GBAOAM oam;
200
201	int32_t frameCounter;
202	int frameskip;
203	int frameskipCounter;
204};
205
206void GBAVideoInit(struct GBAVideo* video);
207void GBAVideoReset(struct GBAVideo* video);
208void GBAVideoDeinit(struct GBAVideo* video);
209void GBAVideoAssociateRenderer(struct GBAVideo* video, struct GBAVideoRenderer* renderer);
210int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles);
211
212void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value);
213
214struct GBASerializedState;
215void GBAVideoSerialize(const struct GBAVideo* video, struct GBASerializedState* state);
216void GBAVideoDeserialize(struct GBAVideo* video, const struct GBASerializedState* state);
217
218extern const int GBAVideoObjSizes[16][2];
219
220#endif