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