-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPPU.cpp
344 lines (288 loc) · 7.91 KB
/
PPU.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "PPU.h"
#include "Bus.h"
#include "GUI/MainCanvas.h"
#include <SFML/Graphics.hpp>
#include <vector>
Display::Color Display::GetColor(uint8_t data) {
switch (data) {
case 0b00: return Color(0xFF, 0xFF, 0xFF, 0xFF);
case 0b01: return Color(0xA9, 0xA9, 0xA9, 0xFF);
case 0b10: return Color(0x54, 0x54, 0x54, 0xFF);
case 0b11: return Color(0x00, 0x00, 0x00, 0xFF);
default: return Color(0x00, 0x00, 0x00, 0x00);
}
}
Display::Color Display::GetColor(Display::Palette p)
{
return GetColor(static_cast<uint8_t>(p));
}
PPU::PPU(Bus* b)
: bus(b)
{
Reset();
}
void PPU::Reset()
{
scanline_counter = 456;
frameBuffer = new FrameBuffer();
frameBuffer->fill(0xFF);
MainCanvas::Paint();
}
void PPU::UpdateScreen(uint8_t cycles) {
if (!bus->regs.lcd.lcdc.lcd_on) {
return;
}
scanline_counter -= cycles * 4;
if (scanline_counter > 0) {
return;
}
scanline_counter = 456;
if (bus->regs.lcd.ly < 144)
{
DrawLine();
bus->regs.lcd.stat.match_flag = STAT_HBLANK;
}
else if (bus->regs.lcd.ly == 144) {
//TODO: LCDC should be off during vblank
BIT_SET(bus->regs.int_request, INT_VBLANK);
if (bus->regs.lcd.stat.vblank_check)
{
BIT_SET(bus->regs.int_request, INT_LCDC);
}
bus->regs.lcd.stat.match_flag = STAT_VBLANK;
//TODO: Only draw canvas during vblank?
MainCanvas::UpdateCanvas(frameBuffer);
MainCanvas::Paint();
}
else if (bus->regs.lcd.ly > 153) {
bus->regs.lcd.ly = 0;
bus->regs.lcd.stat.match_flag = STAT_HBLANK;
return;
}
bus->regs.lcd.ly++;
if (bus->regs.lcd.lyc == bus->regs.lcd.ly)
{
bus->regs.lcd.stat.match_flag = 1;
if (bus->regs.lcd.stat.lyc_check)
{
BIT_SET(bus->regs.int_request, INT_LCDC);
}
}
}
void PPU::DrawLine()
{
Memory::Registers::LCD& lcd = bus->regs.lcd;
// Draw bg
if (lcd.lcdc.bg_on)
{
uint16_t bgCodeArea = (lcd.lcdc.bg_area_flag) ? 0x9C00 : 0x9800;
uint16_t bgCharArea = (lcd.lcdc.bg_data_flag) ? 0x8000 : 0x9000;
for (uint8_t block = 0; block < 20; block++)
{
//TODO: Handle cases where scy is not divisble by 8
uint16_t y_blockOffset = (((lcd.ly + lcd.scy) / 8) * 32);
uint16_t x_blockOffset = (block + (lcd.scx / 8)) % 32;
uint16_t bgCodeOffset = (y_blockOffset + x_blockOffset) % 1024;
uint16_t bgCharOffset;
// When 0x9000 is base ptr, addressing is signed
if (bgCharArea == 0x8000)
{
uint8_t charCode = bus->Read(bgCodeArea + bgCodeOffset);
bgCharOffset = (charCode * 0x10) + ((lcd.ly % 8) * 2);
}
else
{
int8_t charCode = bus->Read(bgCodeArea + bgCodeOffset);
bgCharOffset = (charCode * 0x10) + ((lcd.ly % 8) * 2);
}
uint8_t upperLine = bus->Read(bgCharArea + bgCharOffset);
uint8_t lowerLine = bus->Read(bgCharArea + bgCharOffset + 1);
for (uint8_t i = 0; i < 8; i++)
{
bool upperBit = upperLine & 128;
bool lowerBit = lowerLine & 128;
Display::Color color(0x00, 0x00, 0x00, 0x00);
if (upperBit && lowerBit)
{
color = Display::GetColor(lcd.bgp.palette11);
}
else if (upperBit && !lowerBit)
{
color = Display::GetColor(lcd.bgp.palette01);
}
else if (!upperBit && lowerBit)
{
color = Display::GetColor(lcd.bgp.palette10);
}
else
{
color = Display::GetColor(lcd.bgp.palette00);
}
// Used to fix x position when scx is not divisble by 8
int8_t x_pos = 0;
if ((block + (lcd.scx / 8)) >= 32)
{
x_pos -= lcd.scx % 8;
}
else
{
x_pos += lcd.scx % 8;
}
x_pos = (i + x_pos) % 8;
frameBuffer->at(lcd.ly * 160 * 4 + block * 32 + x_pos * 4) = color.r;
frameBuffer->at(lcd.ly * 160 * 4 + block * 32 + x_pos * 4 + 1) = color.g;
frameBuffer->at(lcd.ly * 160 * 4 + block * 32 + x_pos * 4 + 2) = color.b;
frameBuffer->at(lcd.ly * 160 * 4 + block * 32 + x_pos * 4 + 3) = color.a;
upperLine <<= 1;
lowerLine <<= 1;
}
}
}
// Draw objs
if (lcd.lcdc.obj_on)
{
uint8_t objHeight = (lcd.lcdc.obj_flag) ? 16 : 8;
uint16_t oamArea = 0xFE00;
uint16_t objCharArea = 0x8000;
std::vector<Obj> objs;
for (uint8_t i = 0; i < 40; i++)
{
Obj obj;
obj.y = bus->Read(oamArea + i * 4);
obj.x = bus->Read(oamArea + i * 4 + 1);
obj.chr = bus->Read(oamArea + i * 4 + 2);
obj.attr.raw = bus->Read(oamArea + i * 4 + 3);
int16_t y_pos = obj.y - 16;
int16_t x_pos = obj.x - 8;
if (x_pos >= 0
&& x_pos < 160
&& y_pos >= 0
&& (lcd.ly / 8) == (y_pos / 8))
{
objs.push_back(obj);
}
// In 8 x 16 mode, check for the second half of the sprite
if (objHeight == 16)
{
if (x_pos >= 0
&& x_pos < 160
&& y_pos >= 0
&& (lcd.ly / 8) == ((y_pos + 8) / 8))
{
objs.push_back(obj);
}
}
// 10 objects per line limit
if (objs.size() == 10)
{
break;
}
}
// Stores the x positions of the sprites to be drawn
// A sprite which was drawn first has priority over
// another sprite if they share the same x position
std::vector<uint8_t> xOfObjs;
for (auto obj : objs)
{
int16_t y_pos = obj.y - 16;
int16_t x_pos = obj.x - 8;
// Check if sprite has same x position as another drawn sprite
bool sameX = false;
for (auto x : xOfObjs)
{
if (x == obj.x)
{
sameX = true;
break;
}
}
if (sameX == true)
{
continue;
}
else
{
xOfObjs.push_back(obj.x);
}
//TODO: Check Object Priority for lower x position
// A sprite can't draw over another sprite with a lower x position
uint16_t objCharOffset = (obj.chr * 0x10);
if (obj.attr.vertical_flip && objHeight == 8)
{
objCharOffset += ((objHeight - (lcd.ly % objHeight)) * 2) - 2;
}
else if(objHeight == 8)
{
objCharOffset += ((lcd.ly % objHeight)) * 2;
}
// To flip 8 x 16 is the reverse process of flipping 8 x 8
else if (obj.attr.vertical_flip && objHeight == 16)
{
objCharOffset += ((lcd.ly % objHeight)) * 2;
}
else if(objHeight == 16)
{
objCharOffset += ((objHeight - (lcd.ly % objHeight)) * 2) - 2;
}
uint8_t upperLine = bus->Read(objCharArea + objCharOffset);
uint8_t lowerLine = bus->Read(objCharArea + objCharOffset + 1);
// OBJ Palette Data
Memory::Registers::LCD::OBP obp = (obj.attr.palette) ? lcd.obp1 : lcd.obp0;
for (uint8_t i = 0; i < 8; i++)
{
bool upperBit;
bool lowerBit;
if (obj.attr.horizontal_flip)
{
upperBit = upperLine & (1 << i);
lowerBit = lowerLine & (1 << i);
}
else
{
upperBit = upperLine & (128 >> i);
lowerBit = lowerLine & (128 >> i);
}
Display::Color color(0x00, 0x00, 0x00, 0x00);
if (upperBit && lowerBit)
{
color = Display::GetColor(obp.palette11);
}
else if (upperBit && !lowerBit)
{
color = Display::GetColor(obp.palette01);
}
else if (!upperBit && lowerBit)
{
color = Display::GetColor(obp.palette10);
}
else
{
// Transparent pixels don't need to be drawn
color = Display::GetColor(Display::Palette::Transparent);
continue;
}
//TODO: Align sprites horizontally when obj.x is not divisible by 8
// BG has priority
if (obj.attr.priority)
{
// Sprites can still be visible when BG has priority
// if the BG color is 0
//TODO: Proper check to see if color is 0
if (frameBuffer->at(lcd.ly * 160 * 4 + (x_pos / 8) * 32 + i * 4) == 0xFF)
{
frameBuffer->at(lcd.ly * 160 * 4 + (x_pos / 8) * 32 + i * 4) = color.r;
frameBuffer->at(lcd.ly * 160 * 4 + (x_pos / 8) * 32 + i * 4 + 1) = color.g;
frameBuffer->at(lcd.ly * 160 * 4 + (x_pos / 8) * 32 + i * 4 + 2) = color.b;
frameBuffer->at(lcd.ly * 160 * 4 + (x_pos / 8) * 32 + i * 4 + 3) = color.a;
}
continue;
}
frameBuffer->at(lcd.ly * 160 * 4 + (x_pos / 8) * 32 + i * 4) = color.r;
frameBuffer->at(lcd.ly * 160 * 4 + (x_pos / 8) * 32 + i * 4 + 1) = color.g;
frameBuffer->at(lcd.ly * 160 * 4 + (x_pos / 8) * 32 + i * 4 + 2) = color.b;
frameBuffer->at(lcd.ly * 160 * 4 + (x_pos / 8) * 32 + i * 4 + 3) = color.a;
}
}
}
//TODO: Draw window
}