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