-
Notifications
You must be signed in to change notification settings - Fork 16
/
ppu.c
2124 lines (2048 loc) · 76.6 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
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2017 - 2020 FIX94
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
#include <stdio.h>
#include <stdbool.h>
#include <inttypes.h>
#include <string.h>
#include "mapper.h"
#include "mem.h"
#include "cpu.h"
#include "ppu.h"
//certain optimizations were taken from nestopias ppu code,
//thanks to the people from there for all that
//sprite byte 2
#define PPU_SPRITE_PRIO (1<<5)
#define PPU_SPRITE_FLIP_X (1<<6)
#define PPU_SPRITE_FLIP_Y (1<<7)
//2000
#define PPU_INC_AMOUNT (1<<2)
#define PPU_SPRITE_ADDR (1<<3)
#define PPU_BACKGROUND_ADDR (1<<4)
#define PPU_SPRITE_8_16 (1<<5)
#define PPU_FLAG_NMI (1<<7)
//2001
#define PPU_GRAY (1<<0)
#define PPU_BG_8PX (1<<1)
#define PPU_SPRITE_8PX (1<<2)
#define PPU_BG_ENABLE (1<<3)
#define PPU_SPRITE_ENABLE (1<<4)
//2002
#define PPU_FLAG_OVERFLOW (1<<5)
#define PPU_FLAG_SPRITEZERO (1<<6)
#define PPU_FLAG_VBLANK (1<<7)
#define PPU_VRAM_HORIZONTAL_MASK 0x41F
#define PPU_VRAM_VERTICAL_MASK (~PPU_VRAM_HORIZONTAL_MASK)
#define PPU_DEBUG_ULTRA 0
#define PPU_DEBUG_VSYNC 0
//set or used externally
bool ppu4Screen = false;
//all for mmc5
bool ppuMapper5 = false;
bool ppu816Sprite = false;
bool ppuInFrame = false;
//from main.c
#ifdef COL_32BIT
extern uint32_t textureImage[TEX_DOTS*TEX_LINES];
#define TEXCOL_WHITE 0xFFFFFFFF
#ifdef COL_TEX_BSWAP
#define TEXCOL_BLACK 0xFF000000
#define COL_LS1 0
#define COL_LS2 8
#define COL_LS3 16
#define COL_LS4 24
#else //case COL_TEX_NORMAL
#define TEXCOL_BLACK 0x000000FF
#define COL_LS1 24
#define COL_LS2 16
#define COL_LS3 8
#define COL_LS4 0
#endif //end COL_TEX_BSWAP
#else //COL_16BIT
extern uint16_t textureImage[TEX_DOTS*TEX_LINES];
#define TEXCOL_WHITE 0xFFFF
#define TEXCOL_BLACK 0x0000
#ifdef COL_TEX_BSWAP
#define COL_LS1 0
#define COL_LS2 5
#define COL_LS3 11
#else //case COL_TEX_NORMAL
#define COL_LS1 11
#define COL_LS2 5
#define COL_LS3 0
#endif //end COL_TEX_BSWAP
#endif //end COL_32BIT
extern bool nesPause;
extern bool ppuDebugPauseFrame;
extern bool doOverscan;
static uint8_t ppuDoSprites(uint8_t color, uint16_t dot);
// BMF Final 2
static const uint8_t PPU_Pal[192] =
{
0x52, 0x52, 0x52, 0x00, 0x00, 0x80, 0x08, 0x00, 0x8A, 0x2C, 0x00, 0x7E, 0x4A, 0x00, 0x4E, 0x50, 0x00, 0x06, 0x44, 0x00, 0x00, 0x26, 0x08, 0x00,
0x0A, 0x20, 0x00, 0x00, 0x2E, 0x00, 0x00, 0x32, 0x00, 0x00, 0x26, 0x0A, 0x00, 0x1C, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xA4, 0xA4, 0xA4, 0x00, 0x38, 0xCE, 0x34, 0x16, 0xEC, 0x5E, 0x04, 0xDC, 0x8C, 0x00, 0xB0, 0x9A, 0x00, 0x4C, 0x90, 0x18, 0x00, 0x70, 0x36, 0x00,
0x4C, 0x54, 0x00, 0x0E, 0x6C, 0x00, 0x00, 0x74, 0x00, 0x00, 0x6C, 0x2C, 0x00, 0x5E, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF, 0x4C, 0x9C, 0xFF, 0x7C, 0x78, 0xFF, 0xA6, 0x64, 0xFF, 0xDA, 0x5A, 0xFF, 0xF0, 0x54, 0xC0, 0xF0, 0x6A, 0x56, 0xD6, 0x86, 0x10,
0xBA, 0xA4, 0x00, 0x76, 0xC0, 0x00, 0x46, 0xCC, 0x1A, 0x2E, 0xC8, 0x66, 0x34, 0xC2, 0xBE, 0x3A, 0x3A, 0x3A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF, 0xB6, 0xDA, 0xFF, 0xC8, 0xCA, 0xFF, 0xDA, 0xC2, 0xFF, 0xF0, 0xBE, 0xFF, 0xFC, 0xBC, 0xEE, 0xFA, 0xC2, 0xC0, 0xF2, 0xCC, 0xA2,
0xE6, 0xDA, 0x92, 0xCC, 0xE6, 0x8E, 0xB8, 0xEE, 0xA2, 0xAE, 0xEA, 0xBE, 0xAE, 0xE8, 0xE2, 0xB0, 0xB0, 0xB0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
} ;
static const uint8_t ppuOddArrNTSC[2] = { 0, 1 };
static const uint8_t ppuOddArrPAL[2] = { 0, 0 };
typedef void (*spriteEvalBFunc)(uint16_t line);
static struct
{
const uint8_t *Pal;
const uint8_t *OddArr;
spriteEvalBFunc spriteEvalB;
uint8_t SprLUT[0x100];
#ifdef COL_32BIT
uint32_t BGRLUT[0x200];
#ifdef DISPLAY_PPUWRITES
uint32_t highlightcolor;
#endif
#else //COL_16BIT
uint16_t BGRLUT[0x200];
#ifdef DISPLAY_PPUWRITES
uint16_t highlightcolor;
#endif
#endif //end COL_32BIT
uint8_t TILELUT[0x400][4];
uint16_t PALRAM2[0x20];
uint16_t NameTbl[4];
uint8_t VRAM[0x1000];
uint8_t *VRAMBank0Ptr, *VRAMBank1Ptr,
*VRAMBank2Ptr, *VRAMBank3Ptr;
uint8_t OAM[0x100];
uint8_t OAM2[0x20];
uint8_t *OAM2Ptr;
uint8_t PALRAM[0x20];
uint8_t BGTiles[16];
uint8_t Reg[8];
uint8_t OddNum;
uint16_t RunCycles;
uint16_t curLine;
uint16_t LinesTotal;
uint16_t PreRenderLine;
uint16_t curDot;
uint16_t VramAddr;
uint16_t TmpVramAddr;
uint16_t NextAddr;
uint16_t NextTile;
uint8_t NextByte;
uint8_t VramReadBuf;
uint8_t OAMpos;
uint8_t OAM2pos;
uint8_t OAMcpPos;
uint8_t OAMzSpritePos;
uint8_t FineXScroll;
uint8_t SpriteOAM2Pos;
uint8_t BGRegA;
uint8_t BGRegB;
uint8_t BGAttribReg;
uint8_t BGIndex;
uint8_t lastVal;
uint8_t CurSpriteLn;
uint8_t CurSpriteIndex;
uint8_t CurSpriteByte2;
uint8_t CurSpriteByte3;
uint8_t TmpOAMVal;
uint8_t sprEvalNumLines;
uint8_t sprEvalLineAnd;
bool NextHasZSprite;
bool FrameDone;
bool TmpWrite;
bool NMIallowed;
bool NMITriggered;
bool VBlankFlagCleared;
bool VBlankClearCycle;
bool CurNMIStat;
bool OddFrame;
bool ReadReg2;
bool DoOverscan;
bool BGEnable;
bool SprEnable;
bool DoOAMBug;
bool reqReset;
#ifdef DISPLAY_PPUWRITES
bool ppuwrite;
#endif
} ppu;
static void ppuSetVRAMBankPtr()
{
ppu.VRAMBank0Ptr = ppu.VRAM+ppu.NameTbl[0];
ppu.VRAMBank1Ptr = ppu.VRAM+ppu.NameTbl[1];
ppu.VRAMBank2Ptr = ppu.VRAM+ppu.NameTbl[2];
ppu.VRAMBank3Ptr = ppu.VRAM+ppu.NameTbl[3];
}
//normal drawing dot 0-340
#define PPU_OFF (DOTS*1) //drawing off dot 341-681
#define PPU_PRE (DOTS*2) //pre-render dot 682-1022
#define PPU_PRE_OFF (DOTS*3) //pre-render off dot 1023-1363
#define PPU_L241 (DOTS*4) //line 241 dot 1364-1704
#define PPU_IDLE (DOTS*5) //idle dot 1705-2045
extern bool nesEmuNSFPlayback;
static uint16_t ppuSetDotType(uint16_t dot, uint16_t line)
{
dot %= DOTS;
if(line < VISIBLE_LINES)
{
if(nesEmuNSFPlayback)
dot += PPU_IDLE;
else if(ppu.Reg[1] & (PPU_BG_ENABLE | PPU_SPRITE_ENABLE))
dot += 0; //unchanged
else
dot += PPU_OFF;
}
else if(line == ppu.PreRenderLine)
{
if(nesEmuNSFPlayback)
dot += PPU_PRE_OFF;
else if(ppu.Reg[1] & (PPU_BG_ENABLE | PPU_SPRITE_ENABLE))
dot += PPU_PRE;
else
dot += PPU_PRE_OFF;
}
else if(line == 241)
dot += PPU_L241;
else
dot += PPU_IDLE;
return dot;
}
static void ppuSetSprite816vals()
{
if(ppu.Reg[0] & PPU_SPRITE_8_16)
{
ppu.sprEvalNumLines = 16;
ppu.sprEvalLineAnd = 15;
ppu816Sprite = true;
}
else
{
ppu.sprEvalNumLines = 8;
ppu.sprEvalLineAnd = 7;
ppu816Sprite = false;
}
}
static void spriteEvalB_P1(uint16_t line);
static void spriteEvalB_P2(uint16_t line);
static void spriteEvalB_P3(uint16_t line);
static void spriteEvalB_P4(uint16_t line);
static void spriteEvalB_P5(uint16_t line);
static void spriteEvalB_P6(uint16_t line);
static void spriteEvalB_P7(uint16_t line);
static void spriteEvalB_P8(uint16_t line);
static void spriteEvalB_P9(uint16_t line);
extern bool nesPAL;
void ppuInit()
{
memset(ppu.PALRAM2,0,0x40);
memset(ppu.NameTbl,0,8);
memset(ppu.VRAM,0,0x1000);
memset(ppu.SprLUT,0,0x100);
ppuSetVRAMBankPtr();
memset(ppu.OAM,0,0x100);
memset(ppu.OAM2,0xFF,0x20);
ppu.OAM2Ptr = ppu.OAM2;
memset(ppu.PALRAM,0,0x20);
memset(ppu.BGTiles,0,0x10);
memset(ppu.Reg,0,8);
ppuSetSprite816vals();
ppu.Pal = PPU_Pal;
//ppuCycles = 0;
//start out being in vblank
ppu.Reg[2] |= PPU_FLAG_VBLANK;
ppu.spriteEvalB = spriteEvalB_P1;
ppu.LinesTotal = nesPAL ? LINES_PAL : LINES_NTSC;
ppu.PreRenderLine = ppu.LinesTotal - 1;
ppu.curLine = ppu.LinesTotal - 11;
ppu.curDot = ppuSetDotType(0, ppu.curLine);
ppu.VramAddr = 0;
ppu.TmpVramAddr = 0;
ppu.NextAddr = 0;
ppu.NextTile = 0;
ppu.NextByte = 0;
ppu.VramReadBuf = 0;
ppu.OAMpos = 0;
ppu.OAM2pos = 0;
ppu.OAMcpPos = 0;
ppu.OAMzSpritePos = 0;
ppu.FineXScroll = 0;
ppu.BGRegA = 0;
ppu.BGRegB = 0;
ppu.BGAttribReg = 0;
ppu.BGIndex = 0;
ppu.lastVal = 0;
ppu.CurSpriteLn = 0;
ppu.CurSpriteIndex = 0;
ppu.CurSpriteByte2 = 0;
ppu.CurSpriteByte3 = 0;
ppu.TmpOAMVal = 0xFF;
ppu.NextHasZSprite = false;
ppu.FrameDone = false;
ppu.TmpWrite = false;
ppu.NMIallowed = false;
ppu.NMITriggered = false;
ppu.VBlankFlagCleared = false;
ppu.VBlankClearCycle = false;
ppu.CurNMIStat = false;
ppu.OddFrame = false;
ppu.ReadReg2 = false;
ppu.DoOverscan = false;
ppu.BGEnable = false;
ppu.SprEnable = false;
ppu.reqReset = false;
#ifdef DISPLAY_PPUWRITES
ppu.ppuwrite = false;
#endif
//only do OAM Bug on NTSC NES games
ppu.DoOAMBug = !nesEmuNSFPlayback && !nesPAL;
//generate full BGR LUT
uint8_t rtint = nesPAL ? (1<<6) : (1<<5);
uint8_t gtint = nesPAL ? (1<<5) : (1<<6);
uint8_t btint = (1<<7);
int32_t i;
for(i = 0; i < 0x200; i++)
{
uint8_t palpos = (i&0x3F)*3;
#ifdef COL_32BIT
uint8_t r = ppu.Pal[palpos];
uint8_t g = ppu.Pal[palpos+1];
uint8_t b = ppu.Pal[palpos+2];
#else //COL_16BIT
uint8_t r = ppu.Pal[palpos]>>3;
uint8_t g = ppu.Pal[palpos+1]>>2;
uint8_t b = ppu.Pal[palpos+2]>>3;
#endif
if((i & 0xF) <= 0xD)
{
//reduce red
if((i>>1) & btint) r = (uint8_t)(((float)r)*0.75f);
if((i>>1) & gtint) r = (uint8_t)(((float)r)*0.75f);
//reduce green
if((i>>1) & rtint) g = (uint8_t)(((float)g)*0.75f);
if((i>>1) & btint) g = (uint8_t)(((float)g)*0.75f);
//reduce blue
if((i>>1) & rtint) b = (uint8_t)(((float)b)*0.75f);
if((i>>1) & gtint) b = (uint8_t)(((float)b)*0.75f);
}
//save new color into LUT
ppu.BGRLUT[i] =
#ifdef COL_32BIT
#ifdef COL_BGRA
(b<<COL_LS1) //Blue
| (g<<COL_LS2) //Green
| (r<<COL_LS3) //Red
| (0xFF<<COL_LS4); //Alpha
#ifdef DISPLAY_PPUWRITES
ppu.highlightcolor = (0xFF<<COL_LS3)|(0xFF<<COL_LS4);
#endif
#else //RGBA
(r<<COL_LS1) //Red
| (g<<COL_LS2) //Green
| (b<<COL_LS3) //Blue
| (0xFF<<COL_LS4); //Alpha
#ifdef DISPLAY_PPUWRITES
ppu.highlightcolor = (0xFF<<COL_LS1)|(0xFF<<COL_LS4);
#endif
#endif
#else //COL_16BIT
(r<<COL_LS1) //Red
| (g<<COL_LS2) //Green
| (b<<COL_LS3); //Blue
#ifdef DISPLAY_PPUWRITES
ppu.highlightcolor = (0x1F<<COL_LS1);
#endif
#endif
}
//tile LUT from nestopia
for (i = 0; i < 0x400; ++i)
{
ppu.TILELUT[i][0] = (i & 0xC0) ? (i >> 6 & 0xC) | (i >> 6 & 0x3) : 0;
ppu.TILELUT[i][1] = (i & 0x30) ? (i >> 6 & 0xC) | (i >> 4 & 0x3) : 0;
ppu.TILELUT[i][2] = (i & 0x0C) ? (i >> 6 & 0xC) | (i >> 2 & 0x3) : 0;
ppu.TILELUT[i][3] = (i & 0x03) ? (i >> 6 & 0xC) | (i >> 0 & 0x3) : 0;
}
ppu.RunCycles = nesPAL ? 0x46DB : 0x36DB;
ppu.OddNum = 0;
ppu.OddArr = nesPAL ? ppuOddArrPAL : ppuOddArrNTSC;
ppu4Screen = false; //reg reset
ppuInFrame = false; //we're in vsync
}
//tile load from nestopia
static void loadTiles()
{
uint8_t* src[] =
{
ppu.TILELUT[ppu.BGRegA | (ppu.BGAttribReg & 3) << 8],
ppu.TILELUT[ppu.BGRegB | (ppu.BGAttribReg & 3) << 8]
};
uint8_t* dst = ppu.BGTiles+ppu.BGIndex;
ppu.BGIndex ^= 8;
dst[0] = src[0][0];
dst[1] = src[1][0];
dst[2] = src[0][1];
dst[3] = src[1][1];
dst[4] = src[0][2];
dst[5] = src[1][2];
dst[6] = src[0][3];
dst[7] = src[1][3];
}
static void setNextSpriteTileAddr(uint16_t line)
{
uint8_t cSprLn = ppu.OAM2Ptr[0], cSprInd = ppu.OAM2Ptr[1], cSprB2 = ppu.OAM2Ptr[2];
uint8_t cSpriteAdd = 0; //used to select which 8 by 16 tile
uint8_t cSpriteY = (line - cSprLn)&ppu.sprEvalLineAnd;
if(cSpriteY > 7) //8 by 16 select
{
cSpriteAdd = 16;
cSpriteY &= 7;
}
uint16_t chrROMSpriteAdd = 0;
if(ppu816Sprite)
{
chrROMSpriteAdd = ((cSprInd & 1) << 12);
cSprInd &= ~1;
}
else if(ppu.Reg[0] & PPU_SPRITE_ADDR)
chrROMSpriteAdd = 0x1000;
if(cSprB2 & PPU_SPRITE_FLIP_Y)
{
cSpriteY ^= 7;
if(ppu816Sprite)
cSpriteAdd ^= 16; //8 by 16 select
}
ppu.NextTile = ((chrROMSpriteAdd+(cSprInd<<4)+cSpriteY+cSpriteAdd)&0xFFF) | chrROMSpriteAdd;
}
static void saveSprite(uint8_t p0, uint8_t p1)
{
/* write processed values into internal draw buffer */
if ((p0 | p1) && (ppu.OAM2Ptr[0] < VISIBLE_LINES)) //sprite contains data and is on a valid line, so process
{
uint8_t cSprB2 = ppu.OAM2Ptr[2];
uint8_t cSprB3 = ppu.OAM2Ptr[3];
//pixels
uint8_t a = (cSprB2 & PPU_SPRITE_FLIP_X) ? 7 : 0;
uint16_t p = (p0 >> 1 & 0x0055) | (p1 << 0 & 0x00AA) | (p0 << 8 & 0x5500) | (p1 << 9 & 0xAA00);
uint8_t tmpCol[8];
tmpCol[( a^=6 )] = ( p ) & 0x3;
tmpCol[( a^=2 )] = ( p >>= 2 ) & 0x3;
tmpCol[( a^=6 )] = ( p >>= 2 ) & 0x3;
tmpCol[( a^=2 )] = ( p >>= 2 ) & 0x3;
tmpCol[( a^=7 )] = ( p >>= 2 ) & 0x3;
tmpCol[( a^=2 )] = ( p >>= 2 ) & 0x3;
tmpCol[( a^=6 )] = ( p >>= 2 ) & 0x3;
tmpCol[( a^=2 )] = ( p >>= 2 );
uint8_t tmpSpr = ((ppu.OAM2Ptr == ppu.OAM2 && ppu.NextHasZSprite) ? 0x20 : 0) | //is zero sprite
((cSprB2 & PPU_SPRITE_PRIO) ? 0x40 : 0) | //priority
(0x10 | (cSprB2&3)<<2); //palette
//set to draw
uint16_t i;
for(i = 0; i < 8; i++)
{
uint16_t dstDot = cSprB3+i;
if(dstDot > 0xFF) break;
uint8_t col = tmpCol[i];
if(col && !(ppu.SprLUT[dstDot]&0x80))
ppu.SprLUT[dstDot] = 0x80 | tmpSpr | col;
}
}
//move to next sprite
ppu.OAM2Ptr += 4;
}
FIXNES_ALWAYSINLINE inline static void updateBGTileAddress()
{
if((ppu.VramAddr & 0x1F) == 0x1F)
ppu.VramAddr ^= 0x41F;
else
ppu.VramAddr++;
}
FIXNES_ALWAYSINLINE inline static void updateBGHoriAddress()
{
ppu.VramAddr = (ppu.VramAddr & (~PPU_VRAM_HORIZONTAL_MASK)) | (ppu.TmpVramAddr & PPU_VRAM_HORIZONTAL_MASK);
}
FIXNES_ALWAYSINLINE inline static void updateBGVertAddress()
{
ppu.VramAddr = (ppu.VramAddr & (~PPU_VRAM_VERTICAL_MASK)) | (ppu.TmpVramAddr & PPU_VRAM_VERTICAL_MASK);
}
FIXNES_ALWAYSINLINE inline static void updateBGYAddress()
{
/* update Y position for writes */
if((ppu.VramAddr & 0x7000) != (7<<12))
ppu.VramAddr += (1<<12);
else switch(ppu.VramAddr & 0x3E0)
{
default: ppu.VramAddr = (ppu.VramAddr & 0xFFF) + (1 << 5); break;
case (29<<5): ppu.VramAddr ^= 0x800; /* Falls through. */
case (31<<5): ppu.VramAddr &= 0xC1F; break;
}
}
FIXNES_ALWAYSINLINE inline static void setNTAddr()
{
ppu.NextAddr = (ppu.VramAddr & 0xFFF) | 0x2000;
}
extern void mmc5setTile(uint16_t dot);
FIXNES_ALWAYSINLINE inline static void getNTAddr(uint16_t dot)
{
/* MMC5 Scanline/Scroll Related */
if(ppuMapper5) mmc5setTile(dot);
uint8_t ntByte = memPPUGet8(ppu.NextAddr);
uint16_t chrROMBG = (ppu.Reg[0] & PPU_BACKGROUND_ADDR) ? 0x1000 : 0;
uint8_t curTileY = (ppu.VramAddr>>12)&7;
ppu.NextTile = chrROMBG+(ntByte<<4)+curTileY;
}
FIXNES_ALWAYSINLINE inline static void setATAddr()
{
ppu.NextAddr = (ppu.VramAddr & 0xC00) | 0x23C0 | ((ppu.VramAddr>>4)&0x38) | ((ppu.VramAddr>>2)&7);
}
FIXNES_ALWAYSINLINE inline static void getATAddr()
{
/* Select new BG Background Attribute */
ppu.BGAttribReg = memPPUGet8(ppu.NextAddr) >> ((ppu.VramAddr & 0x2) | (ppu.VramAddr >> 4 & 0x4));
}
FIXNES_ALWAYSINLINE inline static void setTileAddr(uint8_t add)
{
ppu.NextAddr = ppu.NextTile+add;
}
FIXNES_ALWAYSINLINE inline static void getBGTileAddrLow()
{
mapperChrMode = 0;
uint8_t tmp = memPPUGet8(ppu.NextAddr);
ppu.BGRegB = tmp >> 0 & 0x55; ppu.BGRegA = tmp >> 1 & 0x55;
}
FIXNES_ALWAYSINLINE inline static void getBGTileAddrHigh()
{
mapperChrMode = 0;
uint8_t tmp = memPPUGet8(ppu.NextAddr);
ppu.BGRegA |= tmp << 0 & 0xAA; ppu.BGRegB |= tmp << 1 & 0xAA;
}
FIXNES_ALWAYSINLINE inline static uint8_t getSpriteTileAddr()
{
mapperChrMode = 1;
return memPPUGet8(ppu.NextAddr);
}
FIXNES_ALWAYSINLINE inline static void spriteEvalInit()
{
ppu.OAMzSpritePos = ppu.OAMpos;
ppu.OAM2pos = 0;
ppu.OAMcpPos = 0;
ppu.spriteEvalB = spriteEvalB_P1;
}
FIXNES_ALWAYSINLINE inline static void spriteEvalA()
{
ppu.TmpOAMVal = ppu.OAM[(ppu.OAMpos+ppu.OAMcpPos)&0xFF];
//printf("%i %i %i %02x\n", ppu.OAMpos, ppu.OAMcpPos, (ppu.OAMpos+ppu.OAMcpPos)&0xFF, ppu.TmpOAMVal);
}
static void spriteEvalB_P1(uint16_t line)
{
uint8_t cSpriteLn = ppu.TmpOAMVal;
if(cSpriteLn <= line && (cSpriteLn+ppu.sprEvalNumLines) > line)
{
ppu.spriteEvalB = spriteEvalB_P2; //move into sprite copy
ppu.OAM2[ppu.OAM2pos+0] = ppu.TmpOAMVal;
//printf("Copying sprite with line %i at line %i pos oam %i oam2 %i\n", cSpriteLn, ppu.curLine, ppu.OAMpos, ppu.OAM2pos);
if(ppu.OAM2pos == 0 && ppu.OAMpos == ppu.OAMzSpritePos)
ppu.NextHasZSprite = true;
ppu.OAMcpPos++;
}
else //no matching sprite, increase N
{
ppu.OAMpos += 4;
if(ppu.OAMpos == 0) //wrapped, end eval
{
ppu.OAMcpPos = 0;
ppu.spriteEvalB = spriteEvalB_P9;
}
}
}
static void spriteEvalB_P2(uint16_t line)
{
(void)line;
ppu.OAM2[ppu.OAM2pos+1] = ppu.TmpOAMVal;
ppu.OAMcpPos++;
ppu.spriteEvalB = spriteEvalB_P3;
}
static void spriteEvalB_P3(uint16_t line)
{
(void)line;
ppu.OAM2[ppu.OAM2pos+2] = ppu.TmpOAMVal;
ppu.OAMcpPos++;
ppu.spriteEvalB = spriteEvalB_P4;
}
static void spriteEvalB_P4(uint16_t line)
{
(void)line;
ppu.OAM2[ppu.OAM2pos+3] = ppu.TmpOAMVal;
ppu.OAMcpPos = 0;
ppu.OAMpos += 4;
if(ppu.OAMpos == 0) //wrapped, end eval
{
ppu.OAMcpPos = 0;
ppu.spriteEvalB = spriteEvalB_P9;
}
else
{
ppu.OAM2pos += 4;
if(ppu.OAM2pos == 0x20) //time for buggy sprite overflow
ppu.spriteEvalB = spriteEvalB_P5;
else //go to next sprite
ppu.spriteEvalB = spriteEvalB_P1;
}
}
static void spriteEvalB_P5(uint16_t line)
{
uint8_t cSpriteLn = ppu.TmpOAMVal;
if(cSpriteLn <= line && (cSpriteLn+ppu.sprEvalNumLines) > line)
{
//end byte 0 done
ppu.spriteEvalB = spriteEvalB_P6;
//if(!(PPU_Reg[2] & PPU_FLAG_OVERFLOW) && !ppu.SpriteOverflow)
// printf("Overflow with line %i at line %i pos oam %i oam2 %i\n", cSpriteLn, ppu.curLine, ppu.OAMpos, ppu.OAM2pos);
ppu.Reg[2] |= PPU_FLAG_OVERFLOW;
if(ppu.OAMcpPos < 3)
ppu.OAMcpPos++;
else
{
ppu.OAMcpPos = 0;
//OAMpos increment if found
ppu.OAMpos += 4;
}
}
else //no matching sprite, increase N
{
ppu.OAMpos += 4;
if(ppu.OAMpos == 0) //wrapped, end eval
{
ppu.OAMcpPos = 0;
ppu.spriteEvalB = spriteEvalB_P9;
}
else
{
if(ppu.OAMcpPos < 3)
ppu.OAMcpPos++;
else //no OAMpos increment if not found
ppu.OAMcpPos = 0;
}
}
}
static void spriteEvalB_P6(uint16_t line)
{
(void)line;
//end byte 1 done
ppu.spriteEvalB = spriteEvalB_P7;
if(ppu.OAMcpPos < 3)
ppu.OAMcpPos++;
else
{
ppu.OAMcpPos = 0;
//OAMpos increment if found
ppu.OAMpos += 4;
}
}
static void spriteEvalB_P7(uint16_t line)
{
(void)line;
//end byte 2 done
ppu.spriteEvalB = spriteEvalB_P8;
if(ppu.OAMcpPos < 3)
ppu.OAMcpPos++;
else
{
ppu.OAMcpPos = 0;
//OAMpos increment if found
ppu.OAMpos += 4;
}
}
static void spriteEvalB_P8(uint16_t line)
{
(void)line;
//end byte 3 done
ppu.spriteEvalB = spriteEvalB_P9;
if(ppu.OAMcpPos == 3) //OAMpos increment if found
ppu.OAMpos += 4;
ppu.OAMcpPos = 0;
}
static void spriteEvalB_P9(uint16_t line)
{
(void)line;
ppu.OAMpos += 4;
}
FIXNES_ALWAYSINLINE inline static void initSpriteTileFetch()
{
ppu.OAM2Ptr = ppu.OAM2;
}
FIXNES_ALWAYSINLINE inline static void resetOAMPos()
{
ppu.OAMpos = 0;
}
FIXNES_ALWAYSINLINE inline static void ClearOAM2Byte()
{
ppu.OAM2[ppu.OAM2pos++] = 0xFF;
}
static uint16_t ppuLastDot(uint16_t line)
{
line++;
if(line == 241) /* done drawing this frame */
{
ppu.FrameDone = true;
/* For MMC5 Scanline Detect */
ppuInFrame = false;
}
else if(line == ppu.PreRenderLine) /* OAM Bug in 2C02 */
{
if((ppu.OAMpos & 0xF8) && ppu.DoOAMBug && (ppu.Reg[1] & (PPU_BG_ENABLE | PPU_SPRITE_ENABLE)))
memcpy(ppu.OAM,ppu.OAM + (ppu.OAMpos & 0xF8), 8);
}
else if(line == ppu.LinesTotal) /* Wrap back down after pre-render line */
line = 0;
ppu.curLine = line;
ppu.DoOverscan = (doOverscan && (line < 8 || line >= 232));
ppu.NextHasZSprite = false; //reset
ppu.OAMzSpritePos = 0; // reset
//important for ClearOAM2Byte
ppu.OAM2pos = 0; //reset
ppu.TmpOAMVal = 0xFF;
//printf("Line done\n");
return line;
}
#ifdef DO_4_3_ASPECT //4x horizontal and vertical scaling for interpolation
#define TEX_AT_POS textureImage[drawPos]=textureImage[drawPos+1]= \
textureImage[drawPos+2]=textureImage[drawPos+3]= \
textureImage[drawPos+TEX_DOTS]=textureImage[drawPos+TEX_DOTS+1]= \
textureImage[drawPos+TEX_DOTS+2]=textureImage[drawPos+TEX_DOTS+3]= \
textureImage[drawPos+(TEX_DOTS*2)]=textureImage[drawPos+(TEX_DOTS*2)+1]= \
textureImage[drawPos+(TEX_DOTS*2)+2]=textureImage[drawPos+(TEX_DOTS*2)+3]= \
textureImage[drawPos+(TEX_DOTS*3)]=textureImage[drawPos+(TEX_DOTS*3)+1]= \
textureImage[drawPos+(TEX_DOTS*3)+2]=textureImage[drawPos+(TEX_DOTS*3)+3]
#else //1x horizontal and vertical scaling for pure integer scale display
#define TEX_AT_POS textureImage[drawPos]
#endif
FIXNES_ALWAYSINLINE void ppuCycle()
{
uint16_t dot = ppu.curDot, line = ppu.curLine;
#if defined(DISPLAY_PPUWRITES) || defined(DO_4_3_ASPECT)
uint32_t drawPos;
#else
uint16_t drawPos;
#endif
uint8_t curCol;
//these 2 stats are set by cpu, no need to have them in loop
ppu.CurNMIStat = !!(ppu.Reg[0] & PPU_FLAG_NMI);
ppu.VBlankClearCycle = false;
//get value of how often ppu has to run
uint16_t rc = ppu.RunCycles;
uint8_t ppuLoop = rc&7;
ppu.RunCycles = (rc>>3)|(ppuLoop<<12);
while(ppuLoop--)
{
switch(dot)
{
//line 0-239 render on
/*case 0: case 8:*/ case 16: case 24:
case 32: case 40: case 48: case 56:
setNTAddr();
loadTiles();
do_render_pixel:
/* Grab color to render from BG and Sprites */
curCol = ppu.BGEnable ? ppu.BGTiles[(dot + ppu.FineXScroll) & 15] : 0;
if(ppu.SprEnable) curCol = ppuDoSprites(curCol, dot);
ppu.SprLUT[dot] = 0; //always clear for next sprite eval
/* Draw current dot on screen */
#ifdef DISPLAY_PPUWRITES
drawPos = (dot)+(line*TEX_DOTS);
#elif defined(DO_4_3_ASPECT)
drawPos = (dot<<2)+(line<<12);
#else
drawPos = (dot)+(line<<8);
#endif
if(ppu.DoOverscan) /* Draw clipped area as black */
TEX_AT_POS = TEXCOL_BLACK;
else
{
//use color from bg or sprite input
TEX_AT_POS = ppu.BGRLUT[ppu.PALRAM2[curCol&0x1F]];
}
goto add_dot;
case 0:
setNTAddr();
loadTiles();
ppu.BGEnable = (ppu.Reg[1] & PPU_BG_8PX) && (ppu.Reg[1] & PPU_BG_ENABLE);
ppu.SprEnable = (ppu.Reg[1] & PPU_SPRITE_8PX) && (ppu.Reg[1] & PPU_SPRITE_ENABLE);
goto do_render_pixel;
case 8:
setNTAddr();
loadTiles();
ppu.BGEnable = (ppu.Reg[1] & PPU_BG_ENABLE);
ppu.SprEnable = (ppu.Reg[1] & PPU_SPRITE_ENABLE);
goto do_render_pixel;
case 1: case 9: case 17: case 25:
case 33: case 41: case 49: case 57:
getNTAddr(dot);
ClearOAM2Byte();
goto do_render_pixel;
case 2: case 10: case 18: case 26:
case 34: case 42: case 50: case 58:
setATAddr();
goto do_render_pixel;
case 3: case 11: case 19: case 27:
case 35: case 43: case 51: case 59:
getATAddr();
updateBGTileAddress();
ClearOAM2Byte();
goto do_render_pixel;
case 4: case 12: case 20: case 28:
case 36: case 44: case 52: case 60:
setTileAddr(0);
goto do_render_pixel;
case 5: case 13: case 21: case 29:
case 37: case 45: case 53: case 61:
getBGTileAddrLow();
ClearOAM2Byte();
goto do_render_pixel;
case 6: case 14: case 22: case 30:
case 38: case 46: case 54: case 62:
setTileAddr(8);
goto do_render_pixel;
case 7: case 15: case 23: case 31:
case 39: case 47: case 55: case 63:
getBGTileAddrHigh();
ClearOAM2Byte();
goto do_render_pixel;
/*case 64:*/ case 72: case 80: case 88:
case 96: case 104: case 112: case 120:
case 128: case 136: case 144: case 152:
case 160: case 168: case 176: case 184:
case 192: case 200: case 208: case 216:
case 224: case 232: case 240: case 248:
setNTAddr();
loadTiles();
goto do_sprite_eval_a;
case 64: //special case
setNTAddr();
loadTiles();
spriteEvalInit();
goto do_sprite_eval_a;
case 65: case 73: case 81: case 89:
case 97: case 105: case 113: case 121:
case 129: case 137: case 145: case 153:
case 161: case 169: case 177: case 185:
case 193: case 201: case 209: case 217:
case 225: case 233: case 241: case 249:
getNTAddr(dot);
do_sprite_eval_b:
ppu.spriteEvalB(line);
goto do_render_pixel;
case 66: case 74: case 82: case 90:
case 98: case 106: case 114: case 122:
case 130: case 138: case 146: case 154:
case 162: case 170: case 178: case 186:
case 194: case 202: case 210: case 218:
case 226: case 234: case 242: case 250:
setATAddr();
do_sprite_eval_a:
spriteEvalA();
goto do_render_pixel;
case 67: case 75: case 83: case 91:
case 99: case 107: case 115: case 123:
case 131: case 139: case 147: case 155:
case 163: case 171: case 179: case 187:
case 195: case 203: case 211: case 219:
case 227: case 235: case 243: /*case 251:*/
getATAddr();
updateBGTileAddress();
goto do_sprite_eval_b;
case 251: //special case
getATAddr();
updateBGTileAddress();
updateBGYAddress();
goto do_sprite_eval_b;
case 68: case 76: case 84: case 92:
case 100: case 108: case 116: case 124:
case 132: case 140: case 148: case 156:
case 164: case 172: case 180: case 188:
case 196: case 204: case 212: case 220:
case 228: case 236: case 244: case 252:
setTileAddr(0);
goto do_sprite_eval_a;
case 69: case 77: case 85: case 93:
case 101: case 109: case 117: case 125:
case 133: case 141: case 149: case 157:
case 165: case 173: case 181: case 189:
case 197: case 205: case 213: case 221:
case 229: case 237: case 245: case 253:
getBGTileAddrLow();
goto do_sprite_eval_b;
case 70: case 78: case 86: case 94:
case 102: case 110: case 118: case 126:
case 134: case 142: case 150: case 158:
case 166: case 174: case 182: case 190:
case 198: case 206: case 214: case 222:
case 230: case 238: case 246: case 254:
setTileAddr(8);
goto do_sprite_eval_a;
case 71: case 79: case 87: case 95:
case 103: case 111: case 119: case 127:
case 135: case 143: case 151: case 159:
case 167: case 175: case 183: case 191:
case 199: case 207: case 215: case 223:
case 231: case 239: case 247: case 255:
getBGTileAddrHigh();
goto do_sprite_eval_b;
case 256:
updateBGHoriAddress();
ppu.TmpOAMVal = 0xFF;
initSpriteTileFetch();
goto do_reset_oam_pos;
case 257: case 258: case 259:
case 280: case 281: case 282: case 283:
case 288: case 289: case 290: case 291:
case 296: case 297: case 298: case 299:
case 304: case 305: case 306: case 307:
case 312: case 313: case 314: case 315:
do_reset_oam_pos:
resetOAMPos();
goto add_dot;
case 260: case 268: case 276: case 284:
case 292: case 300: case 308: case 316:
setNextSpriteTileAddr(line);
setTileAddr(0);
goto do_reset_oam_pos;
case 261: case 269: case 277: case 285:
case 293: case 301: case 309: case 317:
ppu.NextByte = getSpriteTileAddr();
goto do_reset_oam_pos;
case 262: case 270: case 278: case 286:
case 294: case 302: case 310: case 318:
setTileAddr(8);
goto do_reset_oam_pos;
case 263: case 271: case 279: case 287:
case 295: case 303: case 311: case 319:
saveSprite(ppu.NextByte, getSpriteTileAddr());
goto do_reset_oam_pos;
case 264: case 265: case 266: case 267:
case 272: case 273: case 274: case 275:
//garbage table fetches, not here yet
goto add_dot;
case 320:
setNTAddr();
ppu.TmpOAMVal = ppu.OAM2[0];
goto add_dot;
case 328:
setNTAddr();
ppu.BGIndex = 0;
loadTiles();
goto add_dot;
case 336: case 338:
setNTAddr();
goto add_dot;
case 321: case 329: case 337: case 339:
getNTAddr(dot);
goto add_dot;
case 322: case 330:
setATAddr();
goto add_dot;
case 323: case 331:
getATAddr();
updateBGTileAddress();