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