all repos — mgba @ fc69cdce60f9aee4782e1b21d75fd7b6c2dfa19b

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