-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graphics.cpp
287 lines (255 loc) · 9.33 KB
/
Graphics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "Graphics.h"
#include "Memory.h"
#include "CPU.h"
#include "GB.h"
#include "Serializer.h"
Graphics::Graphics(GB *gb, Memory &MMU, CPU &Cpu) :
MMU(MMU),
Cpu(Cpu),
gb(gb),
currentCycles(0) {
display = new byte[144 * 160 * 4];
Reset();
}
Graphics::~Graphics() {
delete[] display;
}
void Graphics::Update(int cycles) {
byte LCDStat = MMU.ReadByte(STAT);
byte currentMode = LCDStat & 0x3;
byte currentScanline = MMU.ReadByte(LY);
bool previousSTAT = STATInterrupt;
STATInterrupt = false;
if (!LCDEnabled()) {
SetMode(1);
MMU.WriteByteDirect(LY, 0);
return;
}
currentCycles += cycles;
if (currentMode == 2 && currentCycles >= 80) {
SetMode(3);
}
if (currentMode == 3 && currentCycles >= 252) {
SetMode(0);
MMU.HandleHBlank();
if (LCDStat & 0x8) {
STATInterrupt = true;
}
}
if (currentMode == 0 && currentCycles >= 456) {
currentCycles -= 456;
DrawScanline();
MMU.WriteByteDirect(LY, ++currentScanline);
if (currentScanline == 144) {
SetMode(1);
Cpu.RequestInterrupt(0);
if (LCDStat & 0x10) {
STATInterrupt = true;
}
}
else {
SetMode(2);
if (LCDStat & 0x20) {
STATInterrupt = true;
}
}
}
if (currentMode == 1 && currentCycles >= 456) {
currentCycles -= 456;
MMU.WriteByteDirect(LY, ++currentScanline);
if (currentScanline == 154) {
if (vBlankCallback != nullptr) vBlankCallback();
SetMode(2);
currentScanline = 0;
MMU.WriteByteDirect(LY, 0);
if (LCDStat & 0x20) {
STATInterrupt = true;
}
}
}
if (currentScanline == MMU.ReadByte(LYC)) {
MMU.WriteByte(STAT, MMU.ReadByte(STAT) | 4);
if (LCDStat & 0x40) {
STATInterrupt = true;
}
}
else MMU.WriteByte(STAT, MMU.ReadByte(STAT) & ~4);
if (STATInterrupt && !previousSTAT) {
Cpu.RequestInterrupt(1);
}
}
void Graphics::SetMode(int mode) {
MMU.WriteByte(STAT, (MMU.ReadByte(STAT) & 0xFC) | mode);
}
bool Graphics::LCDEnabled() {
return !!(MMU.ReadByte(LCDC) & 0x80);
}
void Graphics::DrawScanline() {
byte LCDControl = MMU.ReadByte(LCDC);
bool backgroundEnabled = LCDControl & 1;
bool windowEnabled = LCDControl & 0x20;
bool spritesEnabled = LCDControl & 2;
if (backgroundEnabled) DrawBackgroundTiles();
if (windowEnabled) DrawWindowTiles();
if (spritesEnabled) DrawSprites();
}
void Graphics::DrawBackgroundTiles() {
byte LCDControl = MMU.ReadByte(LCDC);
word backgroundTilesAddr = (LCDControl & (1 << 3)) ? 0x9C00 : 0x9800;
word tilesDataAddr = (LCDControl & (1 << 4)) ? 0x8000 : 0x8800;
byte y = MMU.ReadByte(LY);
byte lineY = y + MMU.ReadByte(SCROLL_Y);
byte tileY = (lineY / 8);
for (int x = 0; x < 160; x++) {
byte lineX = x + MMU.ReadByte(SCROLL_X);
byte tileX = (lineX / 8);
word tileIdAddr = backgroundTilesAddr + tileY * 32 + tileX;
byte tileId = MMU.ReadVRAM(tileIdAddr, 0);
byte tileAttrs = gb->CGBModeEnabled() ? MMU.ReadVRAM(tileIdAddr, 1) : 0;
int tileBankNumber = (tileAttrs & (1 << 3)) ? 1 : 0;
word tileDataAddr = 0;
if (tilesDataAddr == 0x8000) tileDataAddr = tilesDataAddr + tileId * 0x10;
else tileDataAddr = tilesDataAddr + ((sbyte)tileId + 128) * 0x10;
byte lineYOffset = (tileAttrs & (1 << 6)) ? (7 - (lineY % 8)) : (lineY % 8);
byte lineData1 = MMU.ReadVRAM(tileDataAddr + lineYOffset * 2, tileBankNumber);
byte lineData2 = MMU.ReadVRAM(tileDataAddr + lineYOffset * 2 + 1, tileBankNumber);
int colorBit = (tileAttrs & (1 << 5)) ? (lineX % 8) : (7 - (lineX % 8));
int colorData = 0;
if (lineData1 & (1 << colorBit)) colorData |= 1;
if (lineData2 & (1 << colorBit)) colorData |= 2;
Color color = GetColor(colorData, tileAttrs, false, 0);
if (y < 0 || y >= 144 || x < 0 || x >= 160) return;
int idx = y * 160 * 4 + x * 4;
display[idx + 0] = 0xFF;
display[idx + 1] = color.R;
display[idx + 2] = color.G;
display[idx + 3] = color.B;
}
}
void Graphics::DrawWindowTiles() {
byte LCDControl = MMU.ReadByte(LCDC);
word windowTilesAddr = (LCDControl & (1 << 6)) ? 0x9C00 : 0x9800;
word tilesDataAddr = (LCDControl & (1 << 4)) ? 0x8000 : 0x8800;
byte y = MMU.ReadByte(LY);
int lineY = y - MMU.ReadByte(WINDOW_Y);
if (lineY < 0) return;
byte tileY = lineY / 8;
for (int x = 0; x < 160; x++) {
int lineX = x - MMU.ReadByte(WINDOW_X) + 7;
if (lineX < 0) continue;
byte tileX = lineX / 8;
word tileIdAddr = windowTilesAddr + tileY * 32 + tileX;
byte tileId = MMU.ReadVRAM(tileIdAddr, 0);
byte tileAttrs = gb->CGBModeEnabled() ? MMU.ReadVRAM(tileIdAddr, 1) : 0;
int tileBankNumber = (tileAttrs & (1 << 3)) ? 1 : 0;
word tileDataAddr = 0;
if (tilesDataAddr == 0x8000) tileDataAddr = tilesDataAddr + tileId * 0x10;
else tileDataAddr = tilesDataAddr + ((sbyte)tileId + 128) * 0x10;
byte lineYOffset = (tileAttrs & (1 << 6)) ? (7 - (lineY % 8)) : (lineY % 8);
byte lineData1 = MMU.ReadVRAM(tileDataAddr + lineYOffset * 2, tileBankNumber);
byte lineData2 = MMU.ReadVRAM(tileDataAddr + lineYOffset * 2 + 1, tileBankNumber);
int colorBit = (tileAttrs & (1 << 5)) ? (lineX % 8) : (7 - (lineX % 8));
int colorData = 0;
if (lineData1 & (1 << colorBit)) colorData |= 1;
if (lineData2 & (1 << colorBit)) colorData |= 2;
Color color = GetColor(colorData, tileAttrs, false, 0);
if (y < 0 || y >= 144 || x < 0 || x >= 160) return;
int idx = y * 160 * 4 + x * 4;
display[idx + 0] = 0xFF;
display[idx + 1] = color.R;
display[idx + 2] = color.G;
display[idx + 3] = color.B;
}
}
void Graphics::DrawSprites() {
for (int sprite = 39; sprite >= 0; sprite--) {
int idx = sprite * 4;
byte y = MMU.ReadByte(0xFE00 + idx) - 16;
byte x = MMU.ReadByte(0xFE00 + idx + 1) - 8;
byte tileId = MMU.ReadByte(0xFE00 + idx + 2);
byte attrs = MMU.ReadByte(0xFE00 + idx + 3);
int currentScanline = MMU.ReadByte(LY);
int spriteSize = (MMU.ReadByte(LCDC) & (1 << 2)) ? 16 : 8;
if (currentScanline < y || currentScanline >= y + spriteSize) continue;
int line = currentScanline - y;
if (attrs & (1 << 6)) line = spriteSize - 1 - line;
if (spriteSize == 16) tileId &= 0xFE;
int tileBankNumber = (attrs & (1 << 3)) ? 1 : 0;
word tileDataAddr = 0x8000 + tileId * 16 + line * 2;
byte tileData1 = MMU.ReadVRAM(tileDataAddr, tileBankNumber);
byte tileData2 = MMU.ReadVRAM(tileDataAddr + 1, tileBankNumber);
for (int xLine = 0; xLine < 8; xLine++) {
int xFinal = x + xLine;
if (xFinal >= 160 || xFinal < 0) continue;
int colorBit = (attrs & (1 << 5)) ? xLine : (7 - xLine);
int colorData = 0;
if (tileData1 & (1 << colorBit)) colorData |= 1;
if (tileData2 & (1 << colorBit)) colorData |= 2;
if (colorData == 0) continue;
Color color = GetColor(colorData, 0, true, attrs);
// If LCDC bit 0 is set in CGB mode, sprites will always be displayed
// on top of the background and window.
// Note: if bit 0 is cleared, both window and background will not be rendered on a CGB.
// This could lead to possible compatibility problems with monochrome GB games
// if they choose to only display the window, so I chose not to emulate it here.
byte LCDControl = MMU.ReadByte(LCDC);
bool spriteMasterPriority = gb->CGBModeEnabled() && !(LCDControl & 1);
if (!(attrs & (1 << 7)) || spriteMasterPriority) {
int idx = currentScanline * 160 * 4 + xFinal * 4;
display[idx + 0] = 0xFF;
display[idx + 1] = color.R;
display[idx + 2] = color.G;
display[idx + 3] = color.B;
}
}
}
}
Color Graphics::GetColor(int colorId, byte bgAttrs, bool isObj, byte objAttrs) {
Color result;
if (!gb->CGBModeEnabled()) {
word paletteAddr = 0xFF47;
if (isObj) paletteAddr = (objAttrs & (1 << 4)) ? 0xFF49 : 0xFF48;
byte paletteData = MMU.ReadByte(paletteAddr);
byte palette[4] = {
static_cast<byte>(paletteData & 3),
static_cast<byte>((paletteData >> 2) & 3),
static_cast<byte>((paletteData >> 4) & 3),
static_cast<byte>((paletteData >> 6) & 3)
};
switch (palette[colorId]) {
case 0: result.R = 255; result.G = 255; result.B = 255; break;
case 1: result.R = 200; result.G = 200; result.B = 200; break;
case 2: result.R = 100; result.G = 100; result.B = 100; break;
case 3: result.R = 0; result.G = 0; result.B = 0; break;
}
}
else {
int paletteNumber = isObj ? (objAttrs & 0x7) : (bgAttrs & 0x7);
word colorAddr = paletteNumber * 8 + colorId * 2;
if (isObj) colorAddr += 0x40;
word colorData = MMU.GetPaletteData(colorAddr);
// Convert from RGB555 to RGB888. Right shift the 5-bit value and OR it with its
// most significant bits.
result.B = ((colorData & 0x1F) << 3) | ((colorData & 0x1C) >> 2);
result.G = (((colorData & 0x3E0) >> 5) << 3) | ((colorData & 0x380) >> 7);
result.R = (((colorData & 0x7C00) >> 10) << 3) | ((colorData & 0x7000) >> 12);
}
return result;
}
byte* Graphics::GetDisplayPixels() {
return display;
}
void Graphics::SetVblankCallback(std::function<void(void)> cb) {
vBlankCallback = cb;
}
void Graphics::Reset() {
memset(display, 0xFF, 144 * 160 * 4);
}
void Graphics::Serialize(Serializer & s) {
s.Serialize<int>(currentCycles);
s.Serialize<bool>(STATInterrupt);
}
void Graphics::Deserialize(Serializer & s) {
currentCycles = s.Deserialize<int>();
STATInterrupt = s.Deserialize<bool>();
}