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