-
Notifications
You must be signed in to change notification settings - Fork 2
/
ppu.cpp
659 lines (544 loc) · 19.9 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
#include <stdint.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include "SDL2/include/SDL.h"
#include "mmu.h"
#include "ppu.h"
#include "wmu.h"
#include "cpu.h"
#ifdef _WIN32
#include <Windows.h>
#include <WinUser.h>
#include "SDL2/include/SDL_syswm.h"
#endif // _WIN32
using namespace::std;
// init PPU
unsigned char *_VRAM[0x4000]; // 16 kbytes
unsigned char pVRAM[0x4000];
unsigned char OAM[0x100]; // 256 bytes
const int FB_SIZE = 256 * 240 * 4;
SDL_Renderer *renderer, *renderer_nt, *renderer_oam;
SDL_Window *window, *window_nt, *window_oam;
SDL_Texture *texture, *texture_nt, *texture_oam;
unsigned char framebuffer[FB_SIZE]; // 4 bytes per pixel, RGBA24
bool fb_bg_alpha[256 * 240 * 4];
bool fb_sp_alpha[256 * 240 * 4];
unsigned char framebuffer_bg[256 * 240 * 3]; // 3 bytes per pixel, RGB24
unsigned char framebuffer_chr[256 * 128 * 3]; // 3 bytes per pixel, RGB24 - CHR / Pattern Table
unsigned char framebuffer_nt[256 * 960 * 3]; // 3 bytes per pixel, RGB24 - Nametables
unsigned char framebuffer_oam[256 * 240 * 3]; // 3 bytes per pixel, RGB24 - OAM / Sprites
uint16_t ppuScanline = 0;
uint16_t ppuCycles = 30;
uint8_t PPUDATA_buffer = 0x00;
uint8_t PPUSCROLL_x, tmp_PPUSCROLL_x, tmp_PPUSCROLL_y, tmp_PPUSCROLL_fine_y, PPUSCROLL_fine_y, PPUSCROLL_pos = 0;
int8_t PPUSCROLL_y;
uint16_t scr_v, scr_t;
uint8_t scr_x;
bool scr_w;
uint8_t nts = 0, nts_v = 0;
bool mmc3 = false;
// Registers
PPUCTRL PPU_CTRL;
PPUSTATUS PPU_STATUS;
PPUMASK PPU_MASK;
uint16_t PPUADDR = 0x0;
uint8_t OAMADDR = 0x0;
void initVRAM(VRAM_MIRRORING m) {
for (int i = 0; i < 0x4000; i++) {
_VRAM[i] = &pVRAM[i];
}
switch (m)
{
case VERTICAL:
for (int i = 0; i < 0x400; i++) {
_VRAM[0x2000 + i] = &pVRAM[0x2000 + i];
_VRAM[0x2800 + i] = &pVRAM[0x2000 + i];
_VRAM[0x2400 + i] = &pVRAM[0x2400 + i];
_VRAM[0x2c00 + i] = &pVRAM[0x2400 + i];
}
break;
case HORIZONTAL:
for (int i = 0; i < 0x400; i++) {
_VRAM[0x2000 + i] = &pVRAM[0x2000 + i];
_VRAM[0x2400 + i] = &pVRAM[0x2000 + i];
_VRAM[0x2800 + i] = &pVRAM[0x2800 + i];
_VRAM[0x2c00 + i] = &pVRAM[0x2800 + i];
}
break;
case NONE:
default:
for (int i = 0; i < 0x400; i++) {
_VRAM[0x2000 + i] = &pVRAM[0x2000 + i];
_VRAM[0x2800 + i] = &pVRAM[0x2000 + i];
_VRAM[0x2400 + i] = &pVRAM[0x2400 + i];
_VRAM[0x2c00 + i] = &pVRAM[0x2400 + i];
}
break;
break;
}
}
void wrV(uint16_t adr, uint8_t val) {
*_VRAM[adr] = val;
}
uint8_t rdV(uint16_t adr) {
return *_VRAM[adr];
}
void isMMC3(bool v) {
mmc3 = v;
}
// DEBUG functions
uint16_t getPPUCycles() {
return ppuCycles;
}
uint16_t getPPUScanlines() {
return ppuScanline;
}
// PPUCTRL write (2000)
void writePPUCTRL(uint8_t val) {
PPU_CTRL.setValue(val);
PPU_STATUS.appendLSB(val);
nts = val & 3;
}
// PPUADDR write (2006)
void writePPUADDR(uint8_t adr) {
if (scr_w == 0) {
PPUADDR = adr << 8;
nts = ((adr >> 2) & 3);
// set temp scroll values
tmp_PPUSCROLL_y &= 0b111;
tmp_PPUSCROLL_y |= (adr & 0b11) << 3;
tmp_PPUSCROLL_fine_y = (adr >> 12) & 0b111;
scr_w = 1;
}
else {
PPUADDR |= adr;
PPU_CTRL.setValue((PPU_CTRL.value & 0xfc) | nts);
nts_v = nts;
// set temp scroll values (also pass to real values)
tmp_PPUSCROLL_y &= 0b11000;
tmp_PPUSCROLL_y |= (adr >> 5) & 0b111;
PPUSCROLL_y = tmp_PPUSCROLL_y - (ppuScanline / 8);
PPUSCROLL_fine_y = tmp_PPUSCROLL_fine_y;
scr_w = 0;
}
PPU_STATUS.appendLSB(adr & 0xff);
}
// PPUDATA write
void writePPUDATA(uint8_t data) {
if (PPUADDR == 0x3f10)
wrV(0x3f00, data);
if (PPUADDR == 0x3f14)
wrV(0x3f04, data);
if (PPUADDR == 0x3f18)
wrV(0x3f08, data);
if (PPUADDR == 0x3f1c)
wrV(0x3f0c, data);
if (PPUADDR == 0x3f00)
wrV(0x3f10, data);
if (PPUADDR == 0x3f04)
wrV(0x3f14, data);
if (PPUADDR == 0x3f08)
wrV(0x3f18, data);
if (PPUADDR == 0x3f0c)
wrV(0x3f1c, data);
wrV(PPUADDR, data);
PPUADDR += PPU_CTRL.ppudata_increment_value;
//PPU_STATUS.appendLSB(data);
}
// PPUDATA read
uint8_t readPPUDATA() {
uint8_t ret = PPUDATA_buffer;
PPUDATA_buffer = rdV(PPUADDR);
PPUADDR += PPU_CTRL.ppudata_increment_value;
return ret;
}
// PPUSTATUS read
uint8_t readPPUSTATUS() {
uint8_t ret = PPU_STATUS.value;
PPU_STATUS.clearVBlank();
PPUADDR += PPU_CTRL.ppudata_increment_value;
scr_w = 0;
return ret;
}
// Load cartridge, map CHR-ROM to CHR-RAM (NROM)
void writeCHRRAM(unsigned char cartridge[], uint32_t offset, uint16_t size, uint16_t target_address) {
for (int i = 0; i < size; i++) {
wrV(i + target_address, cartridge[offset + i]);
}
}
// OAM DMA Transfer - copy sprites to VRAM
void oamDMAtransfer(uint8_t val, unsigned char memory[]) {
for (int i = 0; i < 0x100; i++)
OAM[OAMADDR + i] = memory[(val << 8) + i];
}
// Write OAM Address
void writeOAMADDR(uint8_t val) {
OAMADDR = val;
PPU_STATUS.appendLSB(val);
}
// Write OAM Data
void writeOAMDATA(uint8_t val) {
OAM[OAMADDR] = val;
OAMADDR++;
PPU_STATUS.appendLSB(val);
}
// Read OAM Data
uint8_t readOAMDATA() {
// TODO, only in VBlank / FBlank?
return OAM[OAMADDR];
}
// Write PPUSCROLL (2005)
void writePPUSCROLL(uint8_t val) {
if (scr_w == 0) {
// set temp scroll values
tmp_PPUSCROLL_x = (val & 0x1f);
PPUSCROLL_x = val;
scr_w = 1;
}
else {
// set temp scroll values
tmp_PPUSCROLL_y = (val >> 3) & 0b11111;
tmp_PPUSCROLL_fine_y = val & 0b111;
scr_w = 0;
}
}
// Write PPUMASK
void writePPUMASK(uint8_t val) {
PPU_MASK.setValue(val);
}
/*
FIX PALETTE ADDRESSES
0x3f04, 0x3f08 and 0x3f0c fall back to default color at 0x3f00
*/
uint16_t fixPalette(uint16_t c) {
if ((c & 3) == 0) {
return c & 0xffe0;
}
return c;
}
// NES color palette
uint32_t PALETTE[64] = {
0x7C7C7C, 0x0000FC, 0x0000BC, 0x4428BC, 0x940084, 0xA80020, 0xA81000, 0x881400, 0x503000, 0x007800, 0x006800, 0x005800, 0x004058, 0x000000, 0x000000, 0x000000,
0xBCBCBC, 0x0078F8, 0x0058F8, 0x6844FC, 0xD800CC, 0xE40058, 0xF83800, 0xE45C10, 0xAC7C00, 0x00B800, 0x00A800, 0x00A844, 0x008888, 0x000000, 0x000000, 0x000000,
0xF8F8F8, 0x3CBCFC, 0x6888FC, 0x9878F8, 0xF878F8, 0xF85898, 0xF87858, 0xFCA044, 0xF8B800, 0xB8F818, 0x58D854, 0x58F898, 0x00E8D8, 0x787878, 0x000000, 0x000000,
0xFCFCFC, 0xA4E4FC, 0xB8B8F8, 0xD8B8F8, 0xF8B8F8, 0xF8A4C0, 0xF0D0B0, 0xFCE0A8, 0xF8D878, 0xD8F878, 0xB8F8B8, 0xB8F8D8, 0x00FCFC, 0xF8D8F8, 0x000000, 0x000000
};
void initPPU(string filename) {
/*
MAIN WINDOW
*/
// init and create window and renderer
SDL_Init(SDL_INIT_VIDEO);
//SDL_SetHint(SDL_HINT_RENDER_VSYNC, 0);
SDL_CreateWindowAndRenderer(256, 240, 0, &window, &renderer);
SDL_SetWindowSize(window, 512, 480);
//SDL_RenderSetLogicalSize(renderer, 512, 480);
SDL_SetWindowResizable(window, SDL_TRUE);
// for fast rendering, create a texture
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STREAMING, 256, 240);
/*
INIT WINDOW
*/
initWindow(window, filename);
}
int COLORS[] {
0x00,
0x55,
0xaa,
0xff
};
/*
DRAW FRAME
*/
void drawFrame() {
SDL_UpdateTexture(texture, NULL, framebuffer, 256 * sizeof(unsigned char) * 4);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
}
void drawCHRTable() {
SDL_Renderer* renderer_chr;
SDL_Window* window_chr;
SDL_Texture* texture_chr;
// init and create window and renderer
SDL_CreateWindowAndRenderer(128, 256, 0, &window_chr, &renderer_chr);
SDL_SetWindowSize(window_chr, 256, 512);
SDL_RenderSetLogicalSize(renderer_chr, 256, 512);
SDL_SetWindowResizable(window_chr, SDL_TRUE);
// window decorations
char title[50];
snprintf(title, sizeof title, "[ CHR tables L/R ]");
SDL_SetWindowTitle(window_chr, title);
// for fast rendering, create a texture
texture_chr = SDL_CreateTexture(renderer_chr, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STREAMING, 128, 256);
for (int r = 0; r < 256; r++) {
for (int col = 0; col < 128; col++) {
uint16_t adr = (r / 8 * 0x100) + (r % 8) + (col / 8) * 0x10;
uint8_t pixel = ((rdV(adr) >> (7-(col % 8))) & 1) + ((rdV(adr + 8) >> (7-(col % 8))) & 1) * 2;
framebuffer_chr[(r * 128 * 3) + (col * 3)] = COLORS[pixel];
framebuffer_chr[(r * 128 * 3) + (col * 3) + 1] = COLORS[pixel];
framebuffer_chr[(r * 128 * 3) + (col * 3) + 2] = COLORS[pixel];
}
}
SDL_UpdateTexture(texture_chr, NULL, framebuffer_chr, 128 * sizeof(unsigned char) * 3);
SDL_RenderCopy(renderer_chr, texture_chr, NULL, NULL);
SDL_RenderPresent(renderer_chr);
}
/*
OAM / SPRITES
*/
void drawOAM() {
SDL_CreateWindowAndRenderer(256, 240, 0, &window_oam, &renderer_oam);
SDL_SetWindowSize(window_oam, 256, 240);
SDL_RenderSetLogicalSize(renderer_oam, 256, 240);
SDL_SetWindowResizable(window_oam, SDL_TRUE);
// window decorations
char title[50];
snprintf(title, sizeof title, "[ OAM / Sprites ]");
SDL_SetWindowTitle(window_oam, title);
// for fast rendering, create a texture
texture_oam = SDL_CreateTexture(renderer_oam, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STREAMING, 256, 240);
// clear array
memset(framebuffer_oam, 0xcc, sizeof(framebuffer_oam));
for (int i = 0; i < 64; i++) {
uint8_t Y_Pos = OAM[i * 4];
uint8_t Tile_Index_Nr = OAM[i * 4 + 1];
uint8_t Attributes = OAM[i * 4 + 2];
uint8_t X_Pos = OAM[i * 4 + 3];
uint16_t Palette_Offset = 0x3f10 + ((Attributes & 3) * 4);
// iterate through 8x8 sprite in Pattern Table, with offset of Y_Pos and X_Pos
for (int j = 0; j < 8; j++) {
for (int t = 0; t < 8; t++) {
uint8_t V = 0x00;
switch ((Attributes >> 6) & 3) {
case 0x00: // no flip
V = ((rdV(PPU_CTRL.sprite_pattern_table_adr_value + Tile_Index_Nr * 0x10 + j) >> (7 - (t % 8))) & 1) + ((rdV(PPU_CTRL.sprite_pattern_table_adr_value + Tile_Index_Nr * 0x10 + j + 8) >> (7 - (t % 8))) & 1) * 2;
break;
case 0x01: // horizontal flip
V = ((rdV(PPU_CTRL.sprite_pattern_table_adr_value + Tile_Index_Nr * 0x10 + j) >> (t % 8)) & 1) + ((rdV(PPU_CTRL.sprite_pattern_table_adr_value + Tile_Index_Nr * 0x10 + j + 8) >> (t % 8)) & 1) * 2;
break;
case 0x02: // vertical flip
break;
case 0x03: // horizontal & vertical flip
break;
}
uint8_t R = (PALETTE[rdV(Palette_Offset + V)] >> 16) & 0xff;
uint8_t G = (PALETTE[rdV(Palette_Offset + V)] >> 8) & 0xff;
uint8_t B = PALETTE[rdV(Palette_Offset + V)] & 0xff;
if (V) {
// when drawing "+1" is needed for the Y-Position, it's a quirk of the N64 because of the prep-scanline
framebuffer_oam[((Y_Pos + 1 + j) * 256 * 3) + ((X_Pos + t) * 3)] = R;
framebuffer_oam[((Y_Pos + 1 + j) * 256 * 3) + ((X_Pos + t) * 3) + 1] = G;
framebuffer_oam[((Y_Pos + 1 + j) * 256 * 3) + ((X_Pos + t) * 3) + 2] = B;
}
}
}
}
SDL_UpdateTexture(texture_oam, NULL, framebuffer_oam, 256 * sizeof(unsigned char) * 3);
SDL_RenderCopy(renderer_oam, texture_oam, NULL, NULL);
SDL_RenderPresent(renderer_oam);
}
/*
NAMETABLES
*/
void drawNameTables() {
SDL_CreateWindowAndRenderer(256, 960, 0, &window_nt, &renderer_nt);
SDL_SetWindowSize(window_nt, 256, 960);
SDL_RenderSetLogicalSize(renderer_nt, 256, 960);
SDL_SetWindowResizable(window_nt, SDL_TRUE);
// for fast rendering, create a texture
texture_nt = SDL_CreateTexture(renderer_nt, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STREAMING, 256, 960);
// window decorations
char title[50];
snprintf(title, sizeof title, "[ nametables ]");
SDL_SetWindowTitle(window_nt, title);
for (int r = 0; r < 960; r++) {
for (int col = 0; col < 256; col++) {
uint16_t tile_id = ((r / 8) * 32) + (col / 8); // sequential tile number
uint16_t natural_address = 0x2000 + tile_id;
natural_address += 0x40 * ((natural_address % 0x2000) / 0x3c0);
uint16_t tile_nr = rdV(natural_address); // tile ID at the current address (skip attribute tables)
uint16_t adr = PPU_CTRL.background_pattern_table_adr_value + (tile_nr * 0x10) + (r % 8); // adress of the tile in CHR RAM
// select the correct byte of the attribute table
uint16_t tile_attr_nr = rdV((natural_address & 0xfc00) + 0x03c0 + (((((r + (r / 240) * 16) / 32) * 8) + (col / 32)) % 0x40));
// select the part of the byte that we need (2-bits)
uint16_t attr_shift = (((tile_id % 32) / 2 % 2) + ( (tile_id + (r/240) * 64) / 64 % 2) * 2) * 2;
uint16_t palette_offset = ((tile_attr_nr >> attr_shift) & 0x3) * 4;
uint8_t pixel = ((rdV(adr) >> (7 - (col % 8))) & 1) + (((rdV(adr + 8) >> (7 - (col % 8))) & 1) * 2);
framebuffer_nt[(r * 256 * 3) + (col * 3)] = (PALETTE[rdV(fixPalette(0x3f00 + palette_offset + pixel))] >> 16 ) & 0xff;
framebuffer_nt[(r * 256 * 3) + (col * 3) + 1] = (PALETTE[rdV(fixPalette(0x3f00 + palette_offset + pixel))] >> 8) & 0xff;
framebuffer_nt[(r * 256 * 3) + (col * 3) + 2] = (PALETTE[rdV(fixPalette(0x3f00 + palette_offset + pixel))] ) & 0xff;
}
}
SDL_UpdateTexture(texture_nt, NULL, framebuffer_nt, 256 * sizeof(unsigned char) * 3);
SDL_RenderCopy(renderer_nt, texture_nt, NULL, NULL);
SDL_RenderPresent(renderer_nt);
}
uint16_t getAttribute(uint16_t adr) {
uint16_t base = (adr & 0xfc00) + 0x03c0;
uint16_t diff = adr - (adr & 0xfc00);
uint16_t cell = (diff / 128) * 8 + ((diff % 128) % 32 / 4);
return rdV(base + cell);
}
uint8_t getAttributeTilePart(uint16_t adr) {
uint16_t diff = adr - (adr & 0xfc00);
uint8_t tilePar = (diff / 2) % 2 + ((diff / 64) % 2) * 2;
return tilePar * 2;
}
uint16_t translateScrolledAddress(uint16_t adr, uint8_t scroll_x, int8_t scroll_y, uint8_t x, uint8_t y, uint8_t tile_id) {
uint16_t scrolled_address = adr + (scroll_y) * 0x20;
// appears to be in AT area
if (scrolled_address % 0x400 >= 0x3c0) {
scrolled_address = (scrolled_address & 0x2800 ^ 0x800 ^ 0x400) + ((scrolled_address % 0x400) - 0x3c0);
}
// crossed NT
else if ((adr & 0x400) != (scrolled_address & 0x400)) {
scrolled_address ^= 0x400;
// scroll values above what's vertically visible in the viewport, will NOT make the NT change!
if (scroll_y <= 29) {
scrolled_address ^= 0x800; // XOR the bit, that changes NTs vertically
scrolled_address += 0x40; // skip 64 bytes of attribute table
}
}
uint16_t temp_adr = scrolled_address;
scrolled_address += (scroll_x / 8);
bool x_correct = false;
if (((scrolled_address & 0xffe0) != (temp_adr & 0xffe0)) || ((x > 128) && ((tile_id % 32) == 0))) // check if X-boundary crossed ( AND account for transfer-tile glitch)
{
scrolled_address ^= 0x400; // XOR the bit, that changes NTs horizontally
x_correct = true;
}
if (x_correct) {
scrolled_address -= 0x20; // remove the 'line break'
}
return scrolled_address;
}
/*
CALC SCANLINE ARRAY FOR ROW X
*/
uint16_t tile_id, tile_nr, adr, tile_attr_nr, attr_shift, palette_offset, Palette_Offset;
uint8_t pixel, Y_Pos, Tile_Index_Nr, Attributes, X_Pos;
void renderScanline(uint16_t row) {
memset(fb_sp_alpha, false, sizeof(fb_sp_alpha));
int r = row;
if (PPU_MASK.show_bg) {
for (int col = 0; col < 256; col++) {
tile_id = (((r+PPUSCROLL_fine_y) / 8) * 32) + ((col + PPUSCROLL_x % 8) / 8); // sequential tile number
uint16_t natural_address = (mmc3) ? (PPU_CTRL.base_nametable_address | nts_v) * 0x400 + 0x2000 + tile_id : PPU_CTRL.base_nametable_address_value + tile_id;
uint16_t scrolled_address = translateScrolledAddress(natural_address, PPUSCROLL_x, PPUSCROLL_y, col, r, tile_id);
tile_nr = rdV(scrolled_address); // tile ID at the current address
adr = PPU_CTRL.background_pattern_table_adr_value + (tile_nr * 0x10) + ((r+PPUSCROLL_fine_y) % 8); // adress of the tile in CHR RAM
// select the correct byte of the attribute table
tile_attr_nr = getAttribute(scrolled_address);
// select the part of the byte that we need (2-bits)
attr_shift = getAttributeTilePart(scrolled_address);
palette_offset = ((tile_attr_nr >> attr_shift) & 0x3) * 4;
pixel = ((rdV(adr) >> (7 - ((col + PPUSCROLL_x % 8) % 8))) & 1) + (((rdV(adr + 8) >> (7 - ((col + PPUSCROLL_x % 8) % 8))) & 1) * 2);
framebuffer[(r * 256 * 4) + (col * 4)] = (PALETTE[rdV(fixPalette((0x3f00 + palette_offset + pixel) & 0xff0f))] >> 16) & 0xff;
framebuffer[(r * 256 * 4) + (col * 4) + 1] = (PALETTE[rdV(fixPalette((0x3f00 + palette_offset + pixel) & 0xff0f))] >> 8) & 0xff;
framebuffer[(r * 256 * 4) + (col * 4) + 2] = (PALETTE[rdV(fixPalette((0x3f00 + palette_offset + pixel) & 0xff0f))]) & 0xff;
framebuffer[(r * 256 * 4) + (col * 4) + 3] = 0xff;
fb_bg_alpha[r * 256 * 4 + col * 4] = (pixel) ? true : false;
}
}
if (PPU_MASK.show_sprites) {
for (int i = 63; i >= 0; i--) {
Y_Pos = OAM[i * 4] + 1;
Tile_Index_Nr = OAM[i * 4 + 1];
Attributes = OAM[i * 4 + 2];
X_Pos = OAM[i * 4 + 3];
Palette_Offset = 0x3f10 + ((Attributes & 3) * 4);
if (r >= Y_Pos && (Y_Pos + (PPU_CTRL.sprite_size + 1) * 8) > r && Y_Pos) {
// iterate through 8x8 or 8x16 sprite in Pattern Table, with offset of Y_Pos and X_Pos
int j = (r - Y_Pos) + ((r - Y_Pos) / 8) * 8;
for (int t = 0; t < 8; t++) {
uint16_t V = 0x00;
uint16_t Tile = (PPU_CTRL.sprite_size) ? (Tile_Index_Nr & 1) * 0x1000 | (Tile_Index_Nr / 2) * 0x20 : PPU_CTRL.sprite_pattern_table_adr_value + Tile_Index_Nr * 0x10;
switch ((Attributes >> 6) & 3) {
case 0x00: // no flip
V = ((rdV(Tile + j) >> (7 - (t % 8))) & 1) + ((rdV(Tile + j + 8) >> (7 - (t % 8))) & 1) * 2;
break;
case 0x01: // horizontal flip
V = ((rdV(Tile + j) >> (t % 8)) & 1) + ((rdV(Tile + j + 8) >> (t % 8)) & 1) * 2;
break;
case 0x02: // vertical flip
if(PPU_CTRL.sprite_size)
V = ((rdV(Tile + (23 - j)) >> (7 - (t % 8))) & 1) + ((rdV(Tile + (23 - j) + 8) >> (7 - (t % 8))) & 1) * 2;
else
V = ((rdV(Tile + (7 - j)) >> (7 - (t % 8))) & 1) + ((rdV(Tile + (7 - j) + 8) >> (7 - (t % 8))) & 1) * 2;
break;
case 0x03: // horizontal & vertical flip
if (PPU_CTRL.sprite_size)
V = ((rdV(Tile + (23 - j)) >> (t % 8)) & 1) + ((rdV(Tile + (23 - j) + 8) >> (t % 8)) & 1) * 2;
else
V = ((rdV(Tile + (7 - j)) >> (t % 8)) & 1) + ((rdV(Tile + (7 - j) + 8) >> (t % 8)) & 1) * 2;
break;
}
uint8_t R = (PALETTE[rdV(Palette_Offset + V)] >> 16) & 0xff;
uint8_t G = (PALETTE[rdV(Palette_Offset + V)] >> 8) & 0xff;
uint8_t B = PALETTE[rdV(Palette_Offset + V)] & 0xff;
if (V) {
if ((!((Attributes >> 5) & 1) || !fb_bg_alpha[((Y_Pos + (j%8) + (j / 16) * 8) * 256 * 4) + ((X_Pos + t) * 4)]) && !fb_sp_alpha[((Y_Pos + (j % 8) + (j / 16) * 8) * 256 * 4) + ((X_Pos + t) * 4)]) {
// when drawing "+1" is needed for the Y-Position, it's a quirk of the N64 because of the prep-scanline
framebuffer[((Y_Pos + (j%8) + (j / 16) * 8) * 256 * 4) + ((X_Pos + t) * 4)] = R;
framebuffer[((Y_Pos + (j%8) + (j / 16) * 8) * 256 * 4) + ((X_Pos + t) * 4) + 1] = G;
framebuffer[((Y_Pos + (j%8) + (j / 16) * 8) * 256 * 4) + ((X_Pos + t) * 4) + 2] = B;
framebuffer[((Y_Pos + (j%8) + (j / 16) * 8) * 256 * 4) + ((X_Pos + t) * 4) + 3] = 0xff;
}
else if (((Attributes >> 5) & 1)) {
fb_sp_alpha[((Y_Pos + (j % 8) + (j / 16) * 8) * 256 * 4) + ((X_Pos + t) * 4)] = true;
}
// sprite zero hit
if (!PPU_STATUS.isSpriteZero() && i == 0 && fb_bg_alpha[((Y_Pos + (j % 8) + (j / 16) * 8) * 256 * 4) + ((X_Pos + t) * 4)]) {
PPU_STATUS.setSpriteZero();
}
}
}
}
}
}
}
void stepPPU() {
// stepPPU
ppuCycles++;
if (ppuCycles > 340) {
ppuCycles -= 341;
ppuScanline++;
}
if (ppuScanline >= 0 && ppuScanline <= 239) { // drawing
if (ppuCycles == 230) {
renderScanline(ppuScanline);
}
else if (ppuCycles == 260 && (PPU_MASK.show_bg || PPU_MASK.show_sprites)) {
nextScanline();
}
}
else if (ppuScanline == 241 && ppuCycles == 1) { // VBlank
PPU_STATUS.setVBlank();
if (PPU_CTRL.generate_nmi) {
setNMI(true);
}
}
else if (ppuScanline == 261) {
// VBlank off / pre-render line
if (ppuCycles == 1) {
drawFrame();
PPU_STATUS.clearVBlank();
PPU_STATUS.clearSpriteZero();
}
else if (ppuCycles == 260 && (PPU_MASK.show_bg || PPU_MASK.show_sprites)) {
nextScanline();
nextScanline();
nextScanline();
}
else if (ppuCycles >= 280 && ppuCycles <= 304) {
if (PPU_MASK.show_bg || PPU_MASK.show_sprites) {
PPUSCROLL_y = tmp_PPUSCROLL_y;
PPUSCROLL_fine_y = tmp_PPUSCROLL_fine_y;
}
}
else if (ppuCycles == 340) {
ppuScanline = 0;
}
}
}