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