/
ppu.go
executable file
·1397 lines (1257 loc) · 24.5 KB
/
ppu.go
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
package rp2cgo2
import (
"bufio"
"fmt"
"image"
"image/color"
"image/jpeg"
"os"
"github.com/nwidger/m65go2"
"github.com/nwidger/rp2ago3"
)
type Mirroring uint8
const (
Horizontal Mirroring = iota
Vertical
FourScreen
)
func (m Mirroring) String() string {
switch m {
case Horizontal:
return "Horizontal"
case Vertical:
return "Vertical"
case FourScreen:
return "FourScreen"
}
return "Unknown"
}
type ControllerFlag uint8
const (
BaseNametableAddress ControllerFlag = 1 << iota
_
VRAMAddressIncrement
SpritePatternAddress
BackgroundPatternAddress
SpriteSize
_
NMIOnVBlank
)
type MaskFlag uint8
const (
Grayscale MaskFlag = 1 << iota
ShowBackgroundLeft
ShowSpritesLeft
ShowBackground
ShowSprites
IntensifyReds
IntensifyGreens
IntensifyBlues
)
type StatusFlag uint8
const (
_ StatusFlag = 1 << iota
_
_
_
_
SpriteOverflow
Sprite0Hit
VBlankStarted
)
type AddressFlag uint16
const (
CoarseXScroll AddressFlag = 1 << iota
_
_
_
_
CoarseYScroll
_
_
_
_
NametableSelect
_
FineYScroll
_
_
_
)
type SpriteFlag uint32
const (
// byte 0
YPosition SpriteFlag = 1 << iota
_
_
_
_
_
_
_
// byte 1
TileBank
TileNumber
_
_
_
_
_
_
// byte 2
SpritePalette
_
_
_
_
Priority
FlipHorizontally
FlipVertically
// byte 3
XPosition
_
_
_
_
_
_
_
)
type Registers struct {
Controller uint8
Mask uint8
Status uint8
OAMAddress uint8
Scroll uint16
Address uint16
Data uint8
}
func (reg *Registers) Reset() {
reg.Controller = 0x00
reg.Mask = 0x00
reg.Status = 0x00
reg.OAMAddress = 0x00
reg.Scroll = 0x00
reg.Address = 0x00
reg.Data = 0x00
}
const (
CYCLES_PER_SCANLINE uint16 = 341
NUM_SCANLINES = 262
POWERUP_SCANLINE = 241
)
type Sprite struct {
TileLow uint8
TileHigh uint8
Sprite uint32
XPosition uint8
}
type RP2C02 struct {
latch bool
latchAddress uint16
Output chan []uint8
colors []uint8
Registers Registers
Memory *rp2ago3.MappedMemory
Interrupt func(state bool)
oam *OAM
frame uint16
scanline uint16
cycle uint16
patternAddress uint16
attributeLatch uint8
attributes uint16
tilesLatch uint16
tilesLow uint16
tilesHigh uint16
Cycles chan uint16
quota uint16
sprites [8]Sprite
}
func NewRP2C02(interrupt func(bool)) *RP2C02 {
mem := rp2ago3.NewMappedMemory(m65go2.NewBasicMemory(m65go2.DEFAULT_MEMORY_SIZE))
mirrors := make(map[uint16]uint16)
// Mirrored nametables
for i := uint16(0x3000); i <= 0x3eff; i++ {
mirrors[i] = i - 0x1000
}
// Mirrored palette
for _, i := range []uint16{0x3f10, 0x3f14, 0x3f18, 0x3f1c} {
mirrors[i] = i - 0x0010
}
for i := uint16(0x3f20); i <= 0x3fff; i++ {
mirrors[i] = i - 0x0020
}
mem.AddMirrors(mirrors)
return &RP2C02{
Output: make(chan []uint8),
Memory: mem,
Interrupt: interrupt,
oam: NewOAM(),
Cycles: make(chan uint16),
}
}
func (ppu *RP2C02) Reset() {
ppu.latch = false
ppu.Registers.Reset()
ppu.Memory.Reset()
ppu.frame = 0
ppu.cycle = 0
ppu.scanline = POWERUP_SCANLINE
ppu.quota = 0
}
func (ppu *RP2C02) controller(flag ControllerFlag) (value uint16) {
byte := ppu.Registers.Controller
bit := byte & uint8(flag)
switch flag {
case BaseNametableAddress:
// 0x2000 | 0x2400 | 0x2800 | 0x2c00
value = 0x2000 | (uint16(byte&0x03) << 10)
case VRAMAddressIncrement:
switch bit {
case 0:
value = 1
default:
value = 32
}
case SpritePatternAddress:
// 0x0000 | 0x1000
switch bit {
case 0:
value = 0x0000
default:
value = 0x1000
}
case BackgroundPatternAddress:
// 0x0000 | 0x1000
switch bit {
case 0:
value = 0x0000
default:
value = 0x1000
}
case SpriteSize:
// 8x8 | 8x16
switch bit {
case 0:
value = 8
default:
value = 16
}
case NMIOnVBlank:
switch bit {
case 0:
value = 0
default:
value = 1
}
}
return
}
func (ppu *RP2C02) mask(flag MaskFlag) (value bool) {
if ppu.Registers.Mask&uint8(flag) != 0 {
value = true
}
return
}
func (ppu *RP2C02) status(flag StatusFlag) (value bool) {
if ppu.Registers.Status&uint8(flag) != 0 {
value = true
}
return
}
func (ppu *RP2C02) address(flag AddressFlag) (value uint16) {
word := ppu.Registers.Address
switch flag {
case CoarseXScroll:
value = word & 0x001f
case CoarseYScroll:
value = (word & 0x03e0) >> 5
case NametableSelect:
value = (word & 0x0c00) >> 10
case FineYScroll:
value = (word & 0x7000) >> 12
}
return
}
func (ppu *RP2C02) sprite(sprite uint32, flag SpriteFlag) (value uint8) {
switch flag {
case YPosition:
value = uint8(sprite >> 24)
case TileBank:
value = uint8((sprite & 0x00010000) >> 16)
case TileNumber:
value = uint8((sprite & 0x00ff0000) >> 16)
if ppu.controller(SpriteSize) == 16 {
value >>= 1
}
case SpritePalette:
value = uint8((sprite & 0x00000300) >> 8)
case Priority:
value = uint8((sprite & 0x00002000) >> 13)
case FlipHorizontally:
value = uint8((sprite & 0x00004000) >> 14)
case FlipVertically:
value = uint8((sprite & 0x00008000) >> 15)
case XPosition:
value = uint8(sprite)
}
return
}
func (ppu *RP2C02) Mappings(which rp2ago3.Mapping) (fetch, store []uint16) {
fetch = []uint16{}
store = []uint16{}
switch which {
case rp2ago3.CPU:
for i := uint16(0x2000); i <= 0x2007; i++ {
switch i {
case 0x2000:
store = append(store, i)
case 0x2001:
store = append(store, i)
case 0x2002:
fetch = append(fetch, i)
case 0x2003:
store = append(store, i)
case 0x2004:
fetch = append(fetch, i)
store = append(store, i)
case 0x2005:
store = append(store, i)
case 0x2006:
store = append(store, i)
case 0x2007:
fetch = append(fetch, i)
store = append(store, i)
}
}
}
return
}
func (ppu *RP2C02) Fetch(address uint16) (value uint8) {
switch address {
// Status
case 0x2002:
value = ppu.Registers.Status
ppu.Registers.Status &^= uint8(VBlankStarted)
ppu.latch = false
// OAMData
case 0x2004:
value = ppu.oam.Fetch(uint16(ppu.Registers.OAMAddress))
// Data
case 0x2007:
value = ppu.Registers.Data
vramAddress := ppu.Registers.Address & 0x3fff
ppu.Registers.Data = ppu.Memory.Fetch(vramAddress)
if vramAddress&0x3f00 == 0x3f00 {
value = ppu.Registers.Data
}
ppu.incrementAddress()
}
return
}
func (ppu *RP2C02) Store(address uint16, value uint8) (oldValue uint8) {
switch address {
// Controller
case 0x2000:
// t: ...BA.. ........ = d: ......BA
oldValue = ppu.Registers.Controller
ppu.Registers.Controller = value
ppu.latchAddress = (ppu.latchAddress & 0x73ff) | uint16(value&0x03)<<10
// Mask
case 0x2001:
oldValue = ppu.Registers.Mask
ppu.Registers.Mask = value
// OAMAddress
case 0x2003:
oldValue = ppu.Registers.OAMAddress
ppu.Registers.OAMAddress = value
// OAMData
case 0x2004:
if !ppu.rendering() || ppu.scanline == 240 {
oldValue = ppu.oam.Fetch(uint16(ppu.Registers.OAMAddress))
ppu.oam.Store(uint16(ppu.Registers.OAMAddress), value)
ppu.Registers.OAMAddress++
}
// Scroll
case 0x2005:
if !ppu.latch {
// t: ....... ...HGFED = d: HGFED...
// x: CBA = d: .....CBA
ppu.latchAddress = (ppu.latchAddress & 0x7fe0) | uint16(value>>3)
ppu.Registers.Scroll = uint16(value & 0x07)
} else {
// t: CBA..HG FED..... = d: HGFEDCBA
ppu.latchAddress = (ppu.latchAddress & 0x0c1f) | ((uint16(value)<<2 | uint16(value)<<12) & 0x73e0)
}
ppu.latch = !ppu.latch
// Address
case 0x2006:
if !ppu.latch {
// t: .FEDCBA ........ = d: ..FEDCBA
// t: X...... ........ = 0
ppu.latchAddress = (ppu.latchAddress & 0x00ff) | (uint16(value&0x3f) << 8)
} else {
// t: ....... HGFEDCBA = d: HGFEDCBA
// v = t
ppu.latchAddress = (ppu.latchAddress & 0x7f00) | uint16(value)
ppu.Registers.Address = ppu.latchAddress
}
ppu.latch = !ppu.latch
// Data
case 0x2007:
oldValue = ppu.Registers.Data
ppu.Memory.Store(ppu.Registers.Address&0x3fff, value)
ppu.incrementAddress()
}
return
}
func (ppu *RP2C02) transferX() {
// v: ....F.. ...EDCBA = t: ....F.. ...EDCBA
ppu.Registers.Address = (ppu.Registers.Address & 0x7be0) | (ppu.latchAddress & 0x041f)
}
func (ppu *RP2C02) transferY() {
// v: IHGF.ED CBA..... = t: IHGF.ED CBA.....
ppu.Registers.Address = (ppu.Registers.Address & 0x041f) | (ppu.latchAddress & 0x7be0)
}
func (ppu *RP2C02) incrementX() {
// v: .yyy NN YYYYY XXXXX
// ||| || ||||| +++++-- coarse X scroll
// ||| || +++++-------- coarse Y scroll
// ||| ++-------------- nametable select
// +++----------------- fine Y scroll
v := ppu.Registers.Address
switch v & 0x001f {
case 0x001f: // coarse X == 31
v ^= 0x041f // coarse X = 0, switch horizontal nametable
default:
v++ // increment coarse X
}
ppu.Registers.Address = v
}
func (ppu *RP2C02) incrementY() {
// v: .yyy NN YYYYY XXXXX
// ||| || ||||| +++++-- coarse X scroll
// ||| || +++++-------- coarse Y scroll
// ||| ++-------------- nametable select
// +++----------------- fine Y scroll
v := ppu.Registers.Address
if (v & 0x7000) != 0x7000 { // if fine Y < 7
v += 0x1000 // increment fine Y
} else {
v &= 0x0fff
switch v & 0x03e0 {
case 0x03a0: // coarse Y = 29
v ^= 0x0800 // switch vertical nametable
case 0x03e0: // coarse Y = 31
v &= 0x7c1f // coarse Y = 0, nametable not switched
default:
v += 0x0020 // increment coarse Y
}
}
ppu.Registers.Address = v
}
func (ppu *RP2C02) incrementAddress() {
if !ppu.rendering() || ppu.scanline == 240 {
ppu.Registers.Address =
(ppu.Registers.Address + ppu.controller(VRAMAddressIncrement)) & 0x7fff
} else {
if ppu.controller(VRAMAddressIncrement) == 32 {
ppu.incrementY()
} else {
ppu.Registers.Address++
}
}
}
func (ppu *RP2C02) reloadBackgroundTiles() {
switch ppu.cycle {
case 9:
fallthrough
case 17:
fallthrough
case 25:
fallthrough
case 33:
fallthrough
case 41:
fallthrough
case 49:
fallthrough
case 57:
fallthrough
case 65:
fallthrough
case 73:
fallthrough
case 81:
fallthrough
case 89:
fallthrough
case 97:
fallthrough
case 105:
fallthrough
case 113:
fallthrough
case 121:
fallthrough
case 129:
fallthrough
case 137:
fallthrough
case 145:
fallthrough
case 153:
fallthrough
case 161:
fallthrough
case 169:
fallthrough
case 177:
fallthrough
case 185:
fallthrough
case 193:
fallthrough
case 201:
fallthrough
case 209:
fallthrough
case 217:
fallthrough
case 225:
fallthrough
case 233:
fallthrough
case 241:
fallthrough
case 249:
fallthrough
case 257:
fallthrough
case 329:
fallthrough
case 337:
if ppu.rendering() {
ppu.tilesLow = (ppu.tilesLow & 0xff00) | (ppu.tilesLatch & 0x00ff)
ppu.tilesHigh = (ppu.tilesHigh & 0xff00) | ((ppu.tilesLatch >> 8) & 0x00ff)
}
}
}
func (ppu *RP2C02) shiftBackgroundTiles() {
if (ppu.cycle >= 2 && ppu.cycle <= 257) || (ppu.cycle >= 322 && ppu.cycle <= 337) {
ppu.tilesLow <<= 1
ppu.tilesHigh <<= 1
ppu.attributes = (ppu.attributes >> 2) | (uint16(ppu.attributeLatch) << 14)
}
}
func (ppu *RP2C02) loadSprites() {
index := uint8(0)
switch ppu.cycle {
case 319:
index++
fallthrough
case 311:
index++
fallthrough
case 303:
index++
fallthrough
case 295:
index++
fallthrough
case 287:
index++
fallthrough
case 279:
index++
fallthrough
case 272:
index++
fallthrough
case 257:
sprite := ppu.oam.Sprite(index)
ppu.sprites[index].Sprite = sprite
ppu.sprites[index].XPosition = ppu.sprite(sprite, XPosition)
ppu.sprites[index].TileLow = 0x00
ppu.sprites[index].TileHigh = 0x00
if sprite != 0xffffffff {
address := ppu.spriteAddress(sprite)
ppu.sprites[index].TileLow = ppu.Memory.Fetch(address)
ppu.sprites[index].TileHigh = ppu.Memory.Fetch(address | 0x0008)
reverse := func(x uint8) uint8 {
x = (x&0x55)<<1 | (x&0xAA)>>1
x = (x&0x33)<<2 | (x&0xCC)>>2
x = (x&0x0F)<<4 | (x&0xF0)>>4
return x
}
if ppu.sprite(sprite, FlipHorizontally) != 0 {
ppu.sprites[index].TileLow = reverse(ppu.sprites[index].TileLow)
ppu.sprites[index].TileHigh = reverse(ppu.sprites[index].TileHigh)
}
}
}
}
func (ppu *RP2C02) decrementSprites() {
for i := range ppu.sprites {
if ppu.sprites[i].XPosition > 0 {
ppu.sprites[i].XPosition--
}
}
}
func (ppu *RP2C02) shiftSprites() {
for i := range ppu.sprites {
if ppu.sprites[i].XPosition == 0 {
ppu.sprites[i].TileLow <<= 1
ppu.sprites[i].TileHigh <<= 1
}
}
}
func (ppu *RP2C02) rendering() bool {
return ppu.mask(ShowBackground) || ppu.mask(ShowSprites)
}
func (ppu *RP2C02) fetchName(address uint16) (value uint8) {
// NNii iiii iiii
// 0x2000 = 0010 0000 0000 0000
// 0x2400 = 0010 0100 0000 0000
// 0x2800 = 0010 1000 0000 0000
// 0x2c00 = 0010 1100 0000 0000
value = ppu.Memory.Fetch(0x2000 | address&0x0fff)
return
}
func (ppu *RP2C02) fetchAttribute(address uint16) (value uint8) {
// 0x23c0 = 0010 0011 1100 0000
// NN = 0x0c00
// ii i = 0x0038
// jjj = 0x0007
value = ppu.Memory.Fetch(0x23c0 | (address & 0x0c00) | (address >> 4 & 0x0038) | (address >> 2 & 0x0007))
return
}
func (ppu *RP2C02) spriteAddress(sprite uint32) (address uint16) {
comparitor := (ppu.scanline - uint16(ppu.sprite(sprite, YPosition)))
if ppu.sprite(sprite, FlipVertically) != 0 {
comparitor ^= 0x000f
}
switch ppu.controller(SpriteSize) {
case 8:
address = ppu.controller(SpritePatternAddress) |
(uint16(ppu.sprite(sprite, TileNumber)) << 4)
case 16:
address = (uint16(ppu.sprite(sprite, TileBank)) << 12) |
(uint16(ppu.sprite(sprite, TileNumber)) << 5) |
((comparitor & 0x08) << 1)
}
address |= comparitor & 0xf
return
}
func (ppu *RP2C02) priorityMultiplexer(bgAddress, spriteAddress uint16, spritePriority uint8) (address uint16) {
bgIndex := bgAddress & 0x0003
spriteIndex := spriteAddress & 0x0003
switch bgIndex {
case 0:
switch spriteIndex {
case 0:
address = uint16(0x3f00)
default:
address = spriteAddress
}
default:
switch spriteIndex {
case 0:
address = bgAddress
default:
switch spritePriority {
case 0:
address = spriteAddress
case 1:
address = bgAddress
}
}
}
return
}
func (ppu *RP2C02) renderVisibleScanline() {
ppu.reloadBackgroundTiles()
switch ppu.cycle {
// skipped on BG+odd
case 0:
if ppu.scanline == 0 && ppu.rendering() && ppu.frame&0x1 != 0 {
ppu.quota++
}
// NT byte
case 1:
if ppu.scanline == 261 {
ppu.Registers.Status &^= uint8(VBlankStarted | Sprite0Hit | SpriteOverflow)
}
fallthrough
case 9:
fallthrough
case 17:
fallthrough
case 25:
fallthrough
case 33:
fallthrough
case 41:
fallthrough
case 49:
fallthrough
case 57:
fallthrough
case 65:
fallthrough
case 73:
fallthrough
case 81:
fallthrough
case 89:
fallthrough
case 97:
fallthrough
case 105:
fallthrough
case 113:
fallthrough
case 121:
fallthrough
case 129:
fallthrough
case 137:
fallthrough
case 145:
fallthrough
case 153:
fallthrough
case 161:
fallthrough
case 169:
fallthrough
case 177:
fallthrough
case 185:
fallthrough
case 193:
fallthrough
case 201:
fallthrough
case 209:
fallthrough
case 217:
fallthrough
case 225:
fallthrough
case 233:
fallthrough
case 241:
fallthrough
case 249:
fallthrough
case 321:
fallthrough
case 329:
// 000p NNNN NNNN vvvv
if ppu.rendering() {
ppu.patternAddress = ppu.controller(BackgroundPatternAddress) |
uint16(ppu.fetchName(ppu.Registers.Address))<<4 |
ppu.address(FineYScroll)
}
// AT byte
case 3:
fallthrough
case 11:
fallthrough
case 19:
fallthrough
case 27:
fallthrough
case 35:
fallthrough
case 43:
fallthrough
case 51:
fallthrough
case 59:
fallthrough
case 67:
fallthrough
case 75:
fallthrough
case 83:
fallthrough
case 91:
fallthrough
case 99:
fallthrough
case 107:
fallthrough
case 115:
fallthrough
case 123:
fallthrough
case 131:
fallthrough
case 139:
fallthrough
case 147:
fallthrough
case 155:
fallthrough
case 163:
fallthrough
case 171:
fallthrough
case 179:
fallthrough
case 187:
fallthrough
case 195:
fallthrough
case 203:
fallthrough
case 211:
fallthrough
case 219:
fallthrough
case 227:
fallthrough
case 235:
fallthrough
case 243:
fallthrough
case 251:
fallthrough
case 323:
fallthrough
case 331:
// combine 2nd X- and Y-bit of loopy_v to
// determine which 2-bits of AT byte to use:
//
// value = (topleft << 0) | (topright << 2) | (bottomleft << 4) | (bottomright << 6)
//
// v: .yyy NNYY YYYX XXXX|
// .... .... .... ..X.|
// v >> 4: .... .>>> >Y..|....
// .X. = 000 = 0
// Y.. 010 = 2
// 100 = 4
// 110 = 6
if ppu.rendering() {
ppu.attributeLatch = (ppu.fetchAttribute(ppu.Registers.Address) >>
((ppu.Registers.Address & 0x2) | (ppu.Registers.Address >> 4 & 0x4))) & 0x03
}
// Low BG tile byte (color bit 0)
case 5:
fallthrough
case 13:
fallthrough
case 21:
fallthrough
case 29:
fallthrough
case 37:
fallthrough
case 45:
fallthrough
case 53:
fallthrough
case 61:
fallthrough
case 69:
fallthrough
case 77:
fallthrough
case 85:
fallthrough
case 93:
fallthrough
case 101:
fallthrough
case 109:
fallthrough
case 117:
fallthrough
case 125:
fallthrough
case 133:
fallthrough
case 141:
fallthrough
case 149:
fallthrough
case 157:
fallthrough
case 165:
fallthrough
case 173:
fallthrough
case 181:
fallthrough
case 189:
fallthrough
case 197:
fallthrough
case 205:
fallthrough
case 213:
fallthrough
case 221:
fallthrough
case 229:
fallthrough
case 237:
fallthrough
case 245:
fallthrough