-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathppu.c
296 lines (235 loc) · 12.1 KB
/
ppu.c
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
#include "ppu.h"
#include <stdlib.h>
#include <stdio.h>
#include <raylib.h>
#define SET_LCD_Y_COORD_REG(a) bus_write(ppu->bus, 0xFF44, a);
Ppu_t* ppu_create(Bus_t* bus)
{
Ppu_t* ppu = malloc(sizeof(Ppu_t));
ppu->bus = bus;
ppu->currentLine = 0;
ppu->clocksElapsed = 0;
ppu->screenBufferLocked = 0;
for (int y = 0; y < 160; y++)
for (int x = 0; x < 144; x++)
{
ppu->screenBuffer[x][y] = 0;
}
return ppu;
}
void ppu_clock(Ppu_t* ppu)
{
LCDControlReg_t* lcdControl = ((LCDControlReg_t*)&ppu->bus->data[0xFF40]);
LCDStatReg_t* lcdStat = ((LCDStatReg_t*)&ppu->bus->data[0xFF41]);
ppu->clocksElapsed++;
switch(lcdStat->ppuMode)
{
case MODE_OAMSCAN:
if (ppu->clocksElapsed >= 160)
{
lcdStat->ppuMode = MODE_DRAWING;
//Sprites
if (lcdControl->sprEnable)
{
for (int i = 0; i < 40; i++)
{
uint8_t yPos = bus_read(ppu->bus, 0xFE00 + (i * 4));
uint8_t xPos = bus_read(ppu->bus, 0xFE00 + (i * 4) + 1);
uint8_t tileIndex = bus_read(ppu->bus, 0xFE00 + (i * 4) + 2);
uint8_t attributes = bus_read(ppu->bus, 0xFE00 + (i * 4) + 3);
uint8_t xFlip = (attributes >> 5) & 1;
uint8_t yFlip = (attributes >> 6) & 1;
uint8_t palette = (attributes >> 4) & 1;
if (xPos > 0 || yPos)
{
uint16_t yBase = yPos - 16;
if (ppu->currentLine >= yBase && ppu->currentLine < yBase + (lcdControl->sprSize ? 16 : 8))
{
uint8_t y = ppu->currentLine - yBase;
uint8_t byteA = bus_read(ppu->bus, 0x8000 + ((uint8_t)tileIndex * 16) + y * 2);
uint8_t byteB = bus_read(ppu->bus, 0x8000 + ((uint8_t)tileIndex * 16) + y * 2 + 1);
for (int x = 0; x < 8; x++)
{
uint8_t bitA = (byteA >> (xFlip ? x : 7 - x)) & 1;
uint8_t bitB = (byteB >> (xFlip ? x : 7 - x)) & 1;
uint8_t color = 0;
if (bitA == 0 && bitB == 0) color = palette == 1 ? 0 : 4;
if (bitA == 0 && bitB == 1) color = palette == 1 ? 1 : 5;
if (bitA == 1 && bitB == 0) color = palette == 1 ? 2 : 6;
if (bitA == 1 && bitB == 1) color = palette == 1 ? 3 : 7;
uint16_t X = xPos + x - 8;
uint16_t Y = yFlip ? yPos - (y - 16) - (lcdControl->sprSize ? 16 : 8) : yPos + y - 16;
if (color != 0 && color != 4 && X >= 0 && X < 160 && Y >= 0 && Y < 144)
{
ppu->backBuffer[X][Y] = color;
}
}
}
}
}
}
if (lcdControl->bgWinEnable)
{
//Window
if (lcdControl->winEnable)
{
for (int i = 0; i < 32; i++)
{
uint8_t winX = bus_read(ppu->bus, 0xFF4B);
uint8_t winY = bus_read(ppu->bus, 0xFF4A);
//Fetch tile ID
uint16_t tileIDY = (ppu->currentLine / 8);
uint16_t tileIDX = i;
uint16_t tileIDAddr = tileIDX + ((tileIDY) * 32);
uint16_t tileID = bus_read(ppu->bus, (lcdControl->winTileMapSelect ? 0x9C00 : 0x9800) + tileIDAddr);
uint8_t tileY = ppu->currentLine % 8;
uint8_t tileByteA = bus_read(ppu->bus, lcdControl->tileDataSelect ? 0x8000 + ((uint8_t)tileID * 16) + tileY * 2 : 0x9000 + ((int8_t)tileID * 16) + tileY * 2);
uint8_t tileByteB = bus_read(ppu->bus, lcdControl->tileDataSelect ? 0x8000 + ((uint8_t)tileID * 16) + tileY * 2 + 1 : 0x9000 + ((int8_t)tileID * 16) + tileY * 2 + 1);
uint8_t paletteSelect = bus_read(ppu->bus, 0xFF47);
uint8_t pal0 = paletteSelect >> 6;
uint8_t pal1 = (paletteSelect >> 4) & 3;
uint8_t pal2 = (paletteSelect >> 2) & 3;
uint8_t pal3 = paletteSelect & 3;
for (int j = 0; j < 8; j++)
{
uint8_t bitA = (tileByteA >> (7 - j)) & 1;
uint8_t bitB = (tileByteB >> (7 - j)) & 1;
uint8_t color = 0;
if (bitA == 0 && bitB == 0) color = pal3;//Pal0 = black
if (bitA == 0 && bitB == 1) color = pal1;//Pal1 = white
if (bitA == 1 && bitB == 0) color = pal2;//Pal2 = gray
if (bitA == 1 && bitB == 1) color = pal0;//Pal3 = dark gray
uint16_t X = (i*8 + j) + winX - 7;
uint16_t Y = ppu->currentLine + winY;
if (X >= 0 && X < 160 && Y >= 0 && Y < 144)
{
ppu->backBuffer[X][Y] = color;
}
}
}
}
//Background
for (int i = 0; i < 32; i++)
{
uint16_t scrollX = bus_read(ppu->bus, 0xFF43);
uint16_t scrollY = bus_read(ppu->bus, 0xFF42);
//Fetch tile ID
//uint16_t tileIDY = ((ppu->currentLine) / 8);
//uint16_t tileIDX = (i);
uint16_t tileIDY = ((ppu->currentLine / 8) + scrollY / 8 & 0x1F);
uint16_t tileIDX = (i + scrollX / 8) & 0x1F;
uint16_t tileIDAddr = tileIDX + ((tileIDY) * 32);
uint16_t tileID = bus_read(ppu->bus, (lcdControl->bgTileMapSelect ? 0x9C00 : 0x9800) + tileIDAddr);
uint8_t tileY = ppu->currentLine % 8;
uint8_t tileByteA = bus_read(ppu->bus, lcdControl->tileDataSelect ? 0x8000 + ((uint8_t)tileID * 16) + tileY * 2 : 0x9000 + ((int8_t)tileID * 16) + tileY * 2);
uint8_t tileByteB = bus_read(ppu->bus, lcdControl->tileDataSelect ? 0x8000 + ((uint8_t)tileID * 16) + tileY * 2 + 1 : 0x9000 + ((int8_t)tileID * 16) + tileY * 2 + 1);
uint8_t paletteSelect = bus_read(ppu->bus, 0xFF47);
uint8_t pal0 = paletteSelect >> 6;
uint8_t pal1 = (paletteSelect >> 4) & 3;
uint8_t pal2 = (paletteSelect >> 2) & 3;
uint8_t pal3 = paletteSelect & 3;
for (int j = 0; j < 8; j++)
{
uint8_t bitA = (tileByteA >> (7 - j)) & 1;
uint8_t bitB = (tileByteB >> (7 - j)) & 1;
uint8_t color = 0;
if (bitA == 0 && bitB == 0) color = pal3;//Pal0 = black
if (bitA == 0 && bitB == 1) color = pal1;//Pal1 = white
if (bitA == 1 && bitB == 0) color = pal2;//Pal2 = gray
if (bitA == 1 && bitB == 1) color = pal0;//Pal3 = dark gray
uint16_t X = (i*8 + j) - scrollX % 8;
uint16_t Y = ppu->currentLine - scrollY % 8;
if (X >= 0 && X < 160 && Y >= 0 && Y < 144 && ppu->backBuffer[X][Y] == 255)
{
ppu->backBuffer[X][Y] = color;
}
}
}
}
}
break;
case MODE_DRAWING:
if (ppu->clocksElapsed >= 168)
{
lcdStat->ppuMode = MODE_HBLANK;
if (lcdStat->mode0IntEnable)
{
//Request LCD interrupt
bus_write(ppu->bus, 0xFF0F, bus_read(ppu->bus, 0xFF0F) | (1 << 1));
//lcdStat->mode0IntEnable = 0;
}
//Coincidence interrupt
lcdStat->coincidence = ppu->currentLine == bus_read(ppu->bus, 0xFF45);
if (lcdStat->lyIntEnable == 1)
{
if (lcdStat->coincidence)
{
//Request LCD interrupt
bus_write(ppu->bus, 0xFF0F, bus_read(ppu->bus, 0xFF0F) | (1 << 1));
printf("Coincidence!\n");
lcdStat->coincidence = 0;
}
}
ppu->currentLine++;
bus_write(ppu->bus, 0xFF44, ppu->currentLine);
}
break;
case MODE_HBLANK:
if (ppu->currentLine > 144)
{
if (lcdStat->mode1IntEnable)
{
//Request LCD interrupt
bus_write(ppu->bus, 0xFF0F, bus_read(ppu->bus, 0xFF0F) | (1 << 1));
}
//Request VBlank interrupt
bus_write(ppu->bus, 0xFF0F, bus_read(ppu->bus, 0xFF0F) | 1);
lcdStat->ppuMode = MODE_VBLANK;
ppu->currentLine = 0x94;
ppu->clocksElapsed = 0;
for (int y = 0; y < 144; y++)
{
for (int x = 0; x < 160; x++)
{
ppu->screenBuffer[x][y] = ppu->backBuffer[x][y];
}
}
ppu_clearScreen(ppu);
}
else if (ppu->clocksElapsed >= 456)
{
ppu->clocksElapsed = 0;
lcdStat->ppuMode = MODE_OAMSCAN;
if (lcdStat->mode2IntEnable)
{
//Request LCD interrupt
bus_write(ppu->bus, 0xFF0F, bus_read(ppu->bus, 0xFF0F) | (1 << 1));
}
}
break;
case MODE_VBLANK:
if (ppu->clocksElapsed >= 4560)
{
lcdStat->ppuMode = MODE_OAMSCAN;
if (lcdStat->mode2IntEnable)
{
//Request LCD interrupt
bus_write(ppu->bus, 0xFF0F, bus_read(ppu->bus, 0xFF0F) | (1 << 1));
}
//ppu->currentLine
ppu->clocksElapsed = 0;
ppu->currentLine = 0;
}
break;
}
}
void ppu_clearScreen(Ppu_t* ppu)
{
for (int y = 0; y < 144; y++)
{
for (int x = 0; x < 160; x++)
{
ppu->backBuffer[x][y] = 255;
}
}
}