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