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