include/mgba/internal/gb/video.h (view raw)
1/* Copyright (c) 2013-2016 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 GB_VIDEO_H
7#define GB_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#include <mgba/gb/interface.h>
16
17mLOG_DECLARE_CATEGORY(GB_VIDEO);
18
19enum {
20 GB_VIDEO_HORIZONTAL_PIXELS = 160,
21 GB_VIDEO_VERTICAL_PIXELS = 144,
22 GB_VIDEO_VBLANK_PIXELS = 10,
23 GB_VIDEO_VERTICAL_TOTAL_PIXELS = 154,
24
25 // TODO: Figure out exact lengths
26 GB_VIDEO_MODE_2_LENGTH = 80,
27 GB_VIDEO_MODE_3_LENGTH_BASE = 172,
28 GB_VIDEO_MODE_0_LENGTH_BASE = 204,
29
30 GB_VIDEO_HORIZONTAL_LENGTH = 456,
31
32 GB_VIDEO_TOTAL_LENGTH = 70224,
33
34 GB_BASE_MAP = 0x1800,
35 GB_SIZE_MAP = 0x0400,
36
37 SGB_SIZE_CHAR_RAM = 0x2000,
38 SGB_SIZE_MAP_RAM = 0x1000,
39 SGB_SIZE_PAL_RAM = 0x1000,
40 SGB_SIZE_ATF_RAM = 0x1000
41};
42
43DECL_BITFIELD(GBObjAttributes, uint8_t);
44DECL_BITS(GBObjAttributes, CGBPalette, 0, 3);
45DECL_BIT(GBObjAttributes, Bank, 3);
46DECL_BIT(GBObjAttributes, Palette, 4);
47DECL_BIT(GBObjAttributes, XFlip, 5);
48DECL_BIT(GBObjAttributes, YFlip, 6);
49DECL_BIT(GBObjAttributes, Priority, 7);
50
51DECL_BITFIELD(SGBBgAttributes, uint16_t);
52DECL_BITS(SGBBgAttributes, Tile, 0, 10);
53DECL_BITS(SGBBgAttributes, Palette, 10, 3);
54DECL_BIT(SGBBgAttributes, Priority, 13);
55DECL_BIT(SGBBgAttributes, XFlip, 14);
56DECL_BIT(SGBBgAttributes, YFlip, 15);
57
58struct GBObj {
59 uint8_t y;
60 uint8_t x;
61 uint8_t tile;
62 GBObjAttributes attr;
63};
64
65union GBOAM {
66 struct GBObj obj[40];
67 uint8_t raw[160];
68};
69
70struct mCacheSet;
71struct GBVideoRenderer {
72 void (*init)(struct GBVideoRenderer* renderer, enum GBModel model, bool borders);
73 void (*deinit)(struct GBVideoRenderer* renderer);
74
75 uint8_t (*writeVideoRegister)(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
76 void (*writeSGBPacket)(struct GBVideoRenderer* renderer, uint8_t* data);
77 void (*writeVRAM)(struct GBVideoRenderer* renderer, uint16_t address);
78 void (*writePalette)(struct GBVideoRenderer* renderer, int index, uint16_t value);
79 void (*writeOAM)(struct GBVideoRenderer* renderer, uint16_t oam);
80 void (*drawRange)(struct GBVideoRenderer* renderer, int startX, int endX, int y);
81 void (*finishScanline)(struct GBVideoRenderer* renderer, int y);
82 void (*finishFrame)(struct GBVideoRenderer* renderer);
83 void (*enableSGBBorder)(struct GBVideoRenderer* renderer, bool enable);
84
85 void (*getPixels)(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels);
86 void (*putPixels)(struct GBVideoRenderer* renderer, size_t stride, const void* pixels);
87
88 uint8_t* vram;
89 union GBOAM* oam;
90 struct mCacheSet* cache;
91
92 uint8_t* sgbCharRam;
93 uint8_t* sgbMapRam;
94 uint8_t* sgbPalRam;
95 int sgbRenderMode;
96 uint8_t* sgbAttributes;
97 uint8_t* sgbAttributeFiles;
98
99 bool disableBG;
100 bool disableOBJ;
101 bool disableWIN;
102};
103
104DECL_BITFIELD(GBRegisterLCDC, uint8_t);
105DECL_BIT(GBRegisterLCDC, BgEnable, 0);
106DECL_BIT(GBRegisterLCDC, ObjEnable, 1);
107DECL_BIT(GBRegisterLCDC, ObjSize, 2);
108DECL_BIT(GBRegisterLCDC, TileMap, 3);
109DECL_BIT(GBRegisterLCDC, TileData, 4);
110DECL_BIT(GBRegisterLCDC, Window, 5);
111DECL_BIT(GBRegisterLCDC, WindowTileMap, 6);
112DECL_BIT(GBRegisterLCDC, Enable, 7);
113
114DECL_BITFIELD(GBRegisterSTAT, uint8_t);
115DECL_BITS(GBRegisterSTAT, Mode, 0, 2);
116DECL_BIT(GBRegisterSTAT, LYC, 2);
117DECL_BIT(GBRegisterSTAT, HblankIRQ, 3);
118DECL_BIT(GBRegisterSTAT, VblankIRQ, 4);
119DECL_BIT(GBRegisterSTAT, OAMIRQ, 5);
120DECL_BIT(GBRegisterSTAT, LYCIRQ, 6);
121
122struct GBVideo {
123 struct GB* p;
124 struct GBVideoRenderer* renderer;
125
126 int x;
127 int ly;
128 GBRegisterSTAT stat;
129
130 int mode;
131
132 struct mTimingEvent modeEvent;
133 struct mTimingEvent frameEvent;
134
135 int32_t dotClock;
136
137 uint8_t* vram;
138 uint8_t* vramBank;
139 int vramCurrentBank;
140
141 union GBOAM oam;
142 int objMax;
143
144 int bcpIndex;
145 bool bcpIncrement;
146 int ocpIndex;
147 bool ocpIncrement;
148 uint8_t sgbCommandHeader;
149 int sgbBufferIndex;
150 uint8_t sgbPacketBuffer[128];
151
152 uint16_t dmgPalette[12];
153 uint16_t palette[64];
154
155 bool sgbBorders;
156
157 int32_t frameCounter;
158 int frameskip;
159 int frameskipCounter;
160};
161
162void GBVideoInit(struct GBVideo* video);
163void GBVideoReset(struct GBVideo* video);
164void GBVideoDeinit(struct GBVideo* video);
165
166void GBVideoDummyRendererCreate(struct GBVideoRenderer*);
167void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer);
168
169void GBVideoSkipBIOS(struct GBVideo* video);
170void GBVideoProcessDots(struct GBVideo* video, uint32_t cyclesLate);
171
172void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value);
173void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value);
174void GBVideoWriteLYC(struct GBVideo* video, uint8_t value);
175void GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value);
176void GBVideoSwitchBank(struct GBVideo* video, uint8_t value);
177
178void GBVideoDisableCGB(struct GBVideo* video);
179void GBVideoSetPalette(struct GBVideo* video, unsigned index, uint32_t color);
180
181void GBVideoWriteSGBPacket(struct GBVideo* video, uint8_t* data);
182
183struct GBSerializedState;
184void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state);
185void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state);
186
187CXX_GUARD_END
188
189#endif