-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmc.go
1034 lines (961 loc) · 27.4 KB
/
mmc.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 famigo
import (
"encoding/json"
"fmt"
)
func makeMMC(cartInfo *CartInfo) mmc {
mapperNum := cartInfo.GetMapperNumber()
switch mapperNum {
case 0:
return &mapper000{
VramMirroring: cartInfo.GetMirrorInfo(),
IsChrRAM: cartInfo.IsChrRAM(),
}
case 1:
return &mapper001{
VramMirroring: cartInfo.GetMirrorInfo(),
IsChrRAM: cartInfo.IsChrRAM(),
}
case 2:
return &mapper002{
VramMirroring: cartInfo.GetMirrorInfo(),
IsChrRAM: cartInfo.IsChrRAM(),
}
case 3:
return &mapper003{
VramMirroring: cartInfo.GetMirrorInfo(),
IsChrRAM: cartInfo.IsChrRAM(),
}
case 4:
return &mapper004{}
case 7:
return &mapper007{}
case 31:
return &mapper031{
VramMirroring: cartInfo.GetMirrorInfo(),
IsChrRAM: cartInfo.IsChrRAM(),
}
default:
emuErr(fmt.Sprintf("makeMMC: unimplemented mapper number %v", mapperNum))
panic("unreachable code")
}
}
type mmc interface {
Init(mem *mem)
Read(mem *mem, addr uint16) byte
Write(mem *mem, addr uint16, val byte)
ReadVRAM(mem *mem, addr uint16) byte
WriteVRAM(mem *mem, addr uint16, val byte)
RunCycle(emu *emuState)
Marshal() marshalledMMC
}
func unmarshalMMC(m marshalledMMC) (mmc, error) {
var mmc mmc
switch m.Number {
case 0:
mmc = &mapper000{}
case 1:
mmc = &mapper001{}
case 2:
mmc = &mapper002{}
case 3:
mmc = &mapper003{}
case 4:
mmc = &mapper004{}
case 7:
mmc = &mapper007{}
case 31:
mmc = &mapper031{}
default:
return nil, fmt.Errorf("state contained unknown mapper number %v", m.Number)
}
if err := json.Unmarshal(m.Data, &mmc); err != nil {
return nil, err
}
return mmc, nil
}
type marshalledMMC struct {
Number uint32
Data []byte
}
func marshalMMC(number uint32, mmc mmc) marshalledMMC {
rawJSON, err := json.Marshal(mmc)
if err != nil {
panic(err)
}
return marshalledMMC{
Number: number,
Data: rawJSON,
}
}
func vertMirrorVRAMAddr(addr uint16) uint16 {
return (addr - 0x2000) & 0x07ff
}
func horizMirrorVRAMAddr(addr uint16) uint16 {
// can surely do this in one shot
if addr < 0x2800 {
return (addr & 0x23ff) - 0x2000
}
return (addr & 0x2bff) - 0x2400
}
func oneScreenLowerVRAMAddr(addr uint16) uint16 {
return (addr - 0x2000) & 0x03ff
}
func oneScreenUpperVRAMAddr(addr uint16) uint16 {
return ((addr - 0x2000) & 0x03ff) + 0x0400
}
type mapper000 struct {
VramMirroring MirrorInfo
IsChrRAM bool
}
func (m *mapper000) Init(mem *mem) {}
func (m *mapper000) RunCycle(emu *emuState) {}
func (m *mapper000) Marshal() marshalledMMC { return marshalMMC(0, m) }
func (m *mapper000) Read(mem *mem, addr uint16) byte {
if addr >= 0x6000 && addr < 0x8000 {
// will crash if no RAM, but should be fine
return mem.PrgRAM[(int(addr)-0x6000)&(len(mem.PrgRAM)-1)]
}
if addr >= 0x8000 {
return mem.prgROM[(int(addr)-0x8000)&(len(mem.prgROM)-1)]
}
return 0xff
}
func (m *mapper000) Write(mem *mem, addr uint16, val byte) {
if addr >= 0x6000 && addr < 0x8000 {
realAddr := (int(addr) - 0x6000) & (len(mem.PrgRAM) - 1)
if realAddr < len(mem.PrgRAM) {
mem.PrgRAM[realAddr] = val
}
}
if addr >= 0x8000 {
// It's ROM: nop
}
}
func (m *mapper000) ReadVRAM(mem *mem, addr uint16) byte {
var val byte
switch {
case addr < 0x2000:
val = mem.chrROM[addr]
case addr >= 0x2000 && addr < 0x3000:
var realAddr uint16
if m.VramMirroring == VerticalMirroring {
realAddr = vertMirrorVRAMAddr(addr)
} else if m.VramMirroring == HorizontalMirroring {
realAddr = horizMirrorVRAMAddr(addr)
} else {
emuErr(fmt.Sprintf("mapper000: unimplemented vram mirroring: %v at read(%04x, %02x)", m.VramMirroring, addr, val))
}
val = mem.InternalVRAM[realAddr]
default:
emuErr(fmt.Sprintf("mapper000: unimplemented vram access: read(%04x, %02x)", addr, val))
}
return val
}
func (m *mapper000) WriteVRAM(mem *mem, addr uint16, val byte) {
switch {
case addr < 0x2000:
if m.IsChrRAM {
mem.chrROM[addr] = val
}
case addr >= 0x2000 && addr < 0x3000:
var realAddr uint16
if m.VramMirroring == VerticalMirroring {
realAddr = vertMirrorVRAMAddr(addr)
} else if m.VramMirroring == HorizontalMirroring {
realAddr = horizMirrorVRAMAddr(addr)
} else {
emuErr(fmt.Sprintf("mapper000: unimplemented vram mirroring: write(%04x, %02x)", addr, val))
}
mem.InternalVRAM[realAddr] = val
default:
emuErr(fmt.Sprintf("mapper000: unimplemented vram access: write(%04x, %02x)", addr, val))
}
}
type mapper001 struct {
VramMirroring MirrorInfo
ShiftReg byte
ShiftRegWriteCounter byte
PrgBankMode mapper001PRGBankMode
ChrBankMode mapper001CHRBankMode
PrgBankNumber int
PrgBankNumber256 int
ChrBank0Number int
ChrBank1Number int
PrgRAMBankNumber int
RAMEnabled bool
IsChrRAM bool
}
type mapper001PRGBankMode int
const (
oneBigBank mapper001PRGBankMode = iota
firstBankFixed
lastBankFixed
)
type mapper001CHRBankMode int
const (
oneBank mapper001CHRBankMode = iota
twoBanks
)
func (m *mapper001) Init(mem *mem) {
m.PrgBankMode = lastBankFixed
}
func (m *mapper001) RunCycle(emu *emuState) {}
func (m *mapper001) Marshal() marshalledMMC { return marshalMMC(1, m) }
func (m *mapper001) Read(mem *mem, addr uint16) byte {
if addr >= 0x6000 && addr < 0x8000 {
// FIXME: not complete for SOROM and SXROM
// will crash if no RAM, but should be fine
realAddr := 8*1024*m.PrgRAMBankNumber + int(addr-0x6000)
return mem.PrgRAM[realAddr&(len(mem.PrgRAM)-1)]
}
if addr >= 0x8000 {
switch m.PrgBankMode {
case oneBigBank:
realAddr := 256*1024*m.PrgBankNumber256 + 16*1024*m.PrgBankNumber + int(addr-0x8000)
return mem.prgROM[realAddr]
case firstBankFixed:
if addr < 0xc000 {
realAddr := 256*1024*m.PrgBankNumber256 + int(addr-0x8000)
return mem.prgROM[realAddr]
}
realAddr := 256*1024*m.PrgBankNumber256 + 16*1024*m.PrgBankNumber + int(addr-0xc000)
return mem.prgROM[realAddr]
case lastBankFixed:
if addr >= 0xc000 {
var lastBankStart int
if len(mem.prgROM) > 256*1024 && m.PrgBankNumber256 == 0 {
lastBankStart = 256*1024 - 16*1024
} else {
lastBankStart = len(mem.prgROM) - (16 * 1024)
}
return mem.prgROM[lastBankStart+int(addr-0xc000)]
}
realAddr := 256*1024*m.PrgBankNumber256 + 16*1024*m.PrgBankNumber + int(addr-0x8000)
return mem.prgROM[realAddr]
}
}
return 0xff
}
func (m *mapper001) Write(mem *mem, addr uint16, val byte) {
if addr >= 0x6000 && addr < 0x8000 {
// FIXME: not complete for SOROM and SXROM
// will crash if no RAM, but should be fine
realAddr := 8*1024*m.PrgRAMBankNumber + int(addr-0x6000)
realAddr &= len(mem.PrgRAM) - 1
if realAddr < len(mem.PrgRAM) {
mem.PrgRAM[realAddr] = val
}
} else if addr >= 0x8000 {
if val&0x80 == 0x80 {
m.ShiftRegWriteCounter = 0
m.ShiftReg = 0
m.PrgBankMode = lastBankFixed
} else {
m.ShiftReg |= ((val & 0x01) << 4)
m.ShiftRegWriteCounter++
if m.ShiftRegWriteCounter == 5 {
m.writeReg(mem, addr, m.ShiftReg)
m.ShiftRegWriteCounter = 0
m.ShiftReg = 0
} else {
m.ShiftReg >>= 1
}
}
}
}
func (m *mapper001) writeReg(mem *mem, addr uint16, val byte) {
switch {
case addr >= 0x8000 && addr < 0xa000:
switch val & 0x03 {
case 0:
m.VramMirroring = OneScreenLowerMirroring
case 1:
m.VramMirroring = OneScreenUpperMirroring
case 2:
m.VramMirroring = VerticalMirroring
case 3:
m.VramMirroring = HorizontalMirroring
default:
emuErr(fmt.Sprintf("mapper001: mirroring style not yet implemented: %v", val&0x03))
}
switch (val >> 2) & 0x03 {
case 0, 1:
m.PrgBankMode = oneBigBank
case 2:
m.PrgBankMode = firstBankFixed
case 3:
m.PrgBankMode = lastBankFixed
}
switch (val >> 4) & 0x01 {
case 0:
m.ChrBankMode = oneBank
case 1:
m.ChrBankMode = twoBanks
}
case addr >= 0xa000 && addr < 0xc000:
if m.ChrBankMode == oneBank {
val &^= 0x01
}
chrBankNum := int(val)
if len(mem.prgROM) > 256*1024 {
m.PrgBankNumber256 = int(val>>4) & 0x01
chrBankNum &= 0x0f
}
if len(mem.chrROM) == 8*1024 {
chrBankNum &= 0x01
if len(mem.prgROM) <= 256*1024 {
// impl ram disable here, if desired
}
m.PrgRAMBankNumber = int(val>>2) & 0x03
}
m.ChrBank0Number = chrBankNum & (len(mem.chrROM)/(4*1024) - 1)
case addr >= 0xc000 && addr < 0xe000:
if m.ChrBankMode != oneBank {
chrBankNum := int(val)
if len(mem.prgROM) > 256*1024 {
m.PrgBankNumber256 = int(val>>4) & 0x01
chrBankNum &= 0x0f
}
if len(mem.chrROM) == 8*1024 {
chrBankNum &= 0x01
if len(mem.prgROM) <= 256*1024 {
// impl ram disable here, if desired
}
m.PrgRAMBankNumber = int(val>>2) & 0x03
}
m.ChrBank1Number = chrBankNum & (len(mem.chrROM)/(4*1024) - 1)
}
case addr >= 0xe000:
m.RAMEnabled = val&0x10 == 0x10
if m.PrgBankMode == oneBigBank {
val &^= 0x01
}
m.PrgBankNumber = int(val&0x0f) & (len(mem.prgROM)/(16*1024) - 1)
}
}
func (m *mapper001) getchrROMAddr(addr uint16) int {
if m.ChrBankMode == oneBank || addr < 0x1000 {
return int(m.ChrBank0Number)*1024*4 + int(addr)
}
return int(m.ChrBank1Number)*1024*4 + int(addr-0x1000)
}
func (m *mapper001) ReadVRAM(mem *mem, addr uint16) byte {
var val byte
switch {
case addr < 0x2000:
realAddr := m.getchrROMAddr(addr)
val = mem.chrROM[realAddr]
case addr >= 0x2000 && addr < 0x3000:
switch m.VramMirroring {
case OneScreenLowerMirroring:
val = mem.InternalVRAM[oneScreenLowerVRAMAddr(addr)]
case OneScreenUpperMirroring:
val = mem.InternalVRAM[oneScreenUpperVRAMAddr(addr)]
case VerticalMirroring:
val = mem.InternalVRAM[vertMirrorVRAMAddr(addr)]
case HorizontalMirroring:
val = mem.InternalVRAM[horizMirrorVRAMAddr(addr)]
default:
emuErr(fmt.Sprintf("mapper001: unimplemented vram mirroring %v: read(%04x)", m.VramMirroring, addr))
}
default:
emuErr(fmt.Sprintf("mapper001: unimplemented vram access: read(%04x)", addr))
}
return val
}
func (m *mapper001) WriteVRAM(mem *mem, addr uint16, val byte) {
switch {
case addr < 0x2000:
// NOTE: this ignores the difference between CHR ROM and CHR RAM
if m.IsChrRAM {
mem.chrROM[m.getchrROMAddr(addr)] = val
}
case addr >= 0x2000 && addr < 0x3000:
switch m.VramMirroring {
case OneScreenLowerMirroring:
mem.InternalVRAM[oneScreenLowerVRAMAddr(addr)] = val
case OneScreenUpperMirroring:
mem.InternalVRAM[oneScreenUpperVRAMAddr(addr)] = val
case VerticalMirroring:
mem.InternalVRAM[vertMirrorVRAMAddr(addr)] = val
case HorizontalMirroring:
mem.InternalVRAM[horizMirrorVRAMAddr(addr)] = val
default:
emuErr(fmt.Sprintf("mapper001: unimplemented vram mirroring %v: write(%04x, %02x)", m.VramMirroring, addr, val))
}
default:
emuErr(fmt.Sprintf("mapper001: unimplemented vram access: write(%04x, %02x)", addr, val))
}
}
type mapper002 struct {
VramMirroring MirrorInfo
PrgBankNumber int
IsChrRAM bool
}
func (m *mapper002) Init(mem *mem) {}
func (m *mapper002) RunCycle(emu *emuState) {}
func (m *mapper002) Marshal() marshalledMMC { return marshalMMC(2, m) }
func (m *mapper002) Read(mem *mem, addr uint16) byte {
if addr >= 0x6000 && addr < 0x8000 {
// will crash if no RAM, but should be fine
return mem.PrgRAM[(int(addr)-0x6000)&(len(mem.PrgRAM)-1)]
}
if addr >= 0x8000 && addr < 0xc000 {
return mem.prgROM[m.PrgBankNumber*16*1024+int(addr-0x8000)]
}
return mem.prgROM[(len(mem.prgROM)-16*1024)+int(addr-0xc000)]
}
func (m *mapper002) Write(mem *mem, addr uint16, val byte) {
if addr >= 0x6000 && addr < 0x8000 {
realAddr := (int(addr) - 0x6000) & (len(mem.PrgRAM) - 1)
if realAddr < len(mem.PrgRAM) {
mem.PrgRAM[realAddr] = val
}
}
if addr >= 0x8000 {
m.PrgBankNumber = int(val)
m.PrgBankNumber &= len(mem.prgROM)/(16*1024) - 1
}
}
func (m *mapper002) ReadVRAM(mem *mem, addr uint16) byte {
var val byte
switch {
case addr < 0x2000:
val = mem.chrROM[addr]
case addr >= 0x2000 && addr < 0x3000:
var realAddr uint16
if m.VramMirroring == VerticalMirroring {
realAddr = vertMirrorVRAMAddr(addr)
} else if m.VramMirroring == HorizontalMirroring {
realAddr = horizMirrorVRAMAddr(addr)
} else {
emuErr(fmt.Sprintf("mapper002: unimplemented vram mirroring: %v at read(%04x, %02x)", m.VramMirroring, addr, val))
}
val = mem.InternalVRAM[realAddr]
default:
emuErr(fmt.Sprintf("mapper002: unimplemented vram access: read(%04x, %02x)", addr, val))
}
return val
}
func (m *mapper002) WriteVRAM(mem *mem, addr uint16, val byte) {
switch {
case addr < 0x2000:
if m.IsChrRAM {
mem.chrROM[addr] = val
}
case addr >= 0x2000 && addr < 0x3000:
var realAddr uint16
if m.VramMirroring == VerticalMirroring {
realAddr = vertMirrorVRAMAddr(addr)
} else if m.VramMirroring == HorizontalMirroring {
realAddr = horizMirrorVRAMAddr(addr)
} else {
emuErr(fmt.Sprintf("mapper002: unimplemented vram mirroring: write(%04x, %02x)", addr, val))
}
mem.InternalVRAM[realAddr] = val
default:
emuErr(fmt.Sprintf("mapper002: unimplemented vram access: write(%04x, %02x)", addr, val))
}
}
type mapper003 struct {
VramMirroring MirrorInfo
IsChrRAM bool
ChrBankNumber int
}
func (m *mapper003) Init(mem *mem) {}
func (m *mapper003) RunCycle(emu *emuState) {}
func (m *mapper003) Marshal() marshalledMMC { return marshalMMC(3, m) }
func (m *mapper003) Read(mem *mem, addr uint16) byte {
if addr >= 0x6000 && addr < 0x8000 {
// will crash if no RAM, but should be fine
return mem.PrgRAM[(int(addr)-0x6000)&(len(mem.PrgRAM)-1)]
}
if addr >= 0x8000 {
return mem.prgROM[(int(addr)-0x8000)&(len(mem.prgROM)-1)]
}
return 0xff
}
func (m *mapper003) Write(mem *mem, addr uint16, val byte) {
if addr >= 0x6000 && addr < 0x8000 {
realAddr := (int(addr) - 0x6000) & (len(mem.PrgRAM) - 1)
if realAddr < len(mem.PrgRAM) {
mem.PrgRAM[realAddr] = val
}
}
if addr >= 0x8000 {
m.ChrBankNumber = int(val)
m.ChrBankNumber &= len(mem.chrROM)/(8*1024) - 1
}
}
func (m *mapper003) ReadVRAM(mem *mem, addr uint16) byte {
var val byte
switch {
case addr < 0x2000:
realAddr := int(m.ChrBankNumber)*1024*8 + int(addr)
val = mem.chrROM[realAddr]
case addr >= 0x2000 && addr < 0x3000:
var realAddr uint16
if m.VramMirroring == VerticalMirroring {
realAddr = vertMirrorVRAMAddr(addr)
} else if m.VramMirroring == HorizontalMirroring {
realAddr = horizMirrorVRAMAddr(addr)
} else {
emuErr(fmt.Sprintf("mapper003: unimplemented vram mirroring: %v at read(%04x, %02x)", m.VramMirroring, addr, val))
}
val = mem.InternalVRAM[realAddr]
default:
emuErr(fmt.Sprintf("mapper003: unimplemented vram access: read(%04x, %02x)", addr, val))
}
return val
}
func (m *mapper003) WriteVRAM(mem *mem, addr uint16, val byte) {
switch {
case addr < 0x2000:
if m.IsChrRAM {
realAddr := int(m.ChrBankNumber)*1024*8 + int(addr)
mem.chrROM[realAddr] = val
}
case addr >= 0x2000 && addr < 0x3000:
var realAddr uint16
if m.VramMirroring == VerticalMirroring {
realAddr = vertMirrorVRAMAddr(addr)
} else if m.VramMirroring == HorizontalMirroring {
realAddr = horizMirrorVRAMAddr(addr)
} else {
emuErr(fmt.Sprintf("mapper003: unimplemented vram mirroring: write(%04x, %02x)", addr, val))
}
mem.InternalVRAM[realAddr] = val
default:
emuErr(fmt.Sprintf("mapper003: unimplemented vram access: write(%04x, %02x)", addr, val))
}
}
type mapper004 struct {
VramMirroring MirrorInfo
BankWriteSelector byte
PrgLowerBankIsLocked bool
PrgBank0Number int
PrgBank1Number int
ChrUpperBanksAreBigger bool
ChrBank0Number int
ChrBank1Number int
ChrBank2Number int
ChrBank3Number int
ChrBank4Number int
ChrBank5Number int
IRQLastPPUCycles int // NOTE: if accuracy demands, change this to track ppu.AddrReg bit 12
IRQCounter byte
IRQCounterReloadValue byte
IRQCounterReloadRequested bool
IRQRequested bool
IRQEnabled bool
}
func (m *mapper004) Init(mem *mem) {}
func (m *mapper004) Marshal() marshalledMMC { return marshalMMC(4, m) }
func (m *mapper004) RunCycle(emu *emuState) {
endOfScanline := 260
isRendering := emu.PPU.ShowBG && emu.PPU.LineY >= -1 && emu.PPU.LineY < 240
if isRendering && m.IRQLastPPUCycles < endOfScanline && emu.PPU.PPUCyclesSinceYInc >= endOfScanline {
if m.IRQCounterReloadRequested {
m.IRQCounterReloadRequested = false
m.IRQCounter = m.IRQCounterReloadValue
}
if m.IRQCounter == 0 {
if m.IRQEnabled {
emu.CPU.IRQ = true
}
m.IRQCounter = m.IRQCounterReloadValue
} else {
m.IRQCounter--
}
}
m.IRQLastPPUCycles = emu.PPU.PPUCyclesSinceYInc
}
func (m *mapper004) Read(mem *mem, addr uint16) byte {
if addr >= 0x6000 && addr < 0x8000 {
return mem.PrgRAM[int(addr-0x6000)&(len(mem.PrgRAM)-1)]
}
if addr >= 0x8000 && addr < 0xa000 {
if m.PrgLowerBankIsLocked {
offset := len(mem.prgROM) - 2*8*1024 // second to last bank
return mem.prgROM[offset+int(addr-0x8000)]
}
return mem.prgROM[1024*8*m.PrgBank0Number+int(addr-0x8000)]
}
if addr >= 0xa000 && addr < 0xc000 {
return mem.prgROM[1024*8*m.PrgBank1Number+int(addr-0xa000)]
}
if addr >= 0xc000 && addr < 0xe000 {
if m.PrgLowerBankIsLocked {
return mem.prgROM[1024*8*m.PrgBank0Number+int(addr-0xc000)]
}
offset := len(mem.prgROM) - 2*8*1024 // second to last bank
return mem.prgROM[offset+int(addr-0xc000)]
}
// addr > 0xe000
offset := len(mem.prgROM) - 8*1024 // last bank
return mem.prgROM[offset+int(addr-0xe000)]
}
func (m *mapper004) Write(mem *mem, addr uint16, val byte) {
if addr >= 0x6000 && addr < 0x8000 {
realAddr := (int(addr) - 0x6000) & (len(mem.PrgRAM) - 1)
if realAddr < len(mem.PrgRAM) {
mem.PrgRAM[realAddr] = val
}
}
if addr >= 0x8000 && addr < 0xa000 {
if addr&0x01 == 0 {
m.ChrUpperBanksAreBigger = val&0x80 == 0x80
m.PrgLowerBankIsLocked = val&0x40 == 0x40
// MM6 ram enable bit here, but let's ignore it for easier compat
m.BankWriteSelector = val & 0x07
} else {
bankNumReg := []*int{
&m.ChrBank0Number,
&m.ChrBank1Number,
&m.ChrBank2Number,
&m.ChrBank3Number,
&m.ChrBank4Number,
&m.ChrBank5Number,
&m.PrgBank0Number,
&m.PrgBank1Number,
}[m.BankWriteSelector]
*bankNumReg = int(val)
if m.BankWriteSelector < 6 {
*bankNumReg &= len(mem.chrROM)/1024 - 1
} else {
*bankNumReg &= len(mem.prgROM)/(8*1024) - 1
}
}
}
if addr >= 0xa000 && addr < 0xc000 {
if addr&0x01 == 0 {
if val&0x01 == 0 {
m.VramMirroring = VerticalMirroring
} else {
m.VramMirroring = HorizontalMirroring
}
} else {
// ram protect; do nothing to improve mmc3/mmc6 compat
}
}
if addr >= 0xc000 && addr < 0xe000 {
if addr&0x01 == 0 {
m.IRQCounterReloadValue = val
} else {
m.IRQCounterReloadRequested = true
}
}
if addr >= 0xe000 {
if addr&0x01 == 0 {
m.IRQRequested = false
m.IRQEnabled = false
} else {
m.IRQEnabled = true
}
}
}
func (m *mapper004) ReadVRAM(mem *mem, addr uint16) byte {
var val byte
switch {
case addr < 0x0400:
var offset int
if m.ChrUpperBanksAreBigger {
offset = m.ChrBank2Number * 1024
} else {
offset = (m.ChrBank0Number &^ 0x01) * 1024
}
val = mem.chrROM[offset+int(addr)]
case addr >= 0x0400 && addr < 0x0800:
var offset int
if m.ChrUpperBanksAreBigger {
offset = m.ChrBank3Number * 1024
} else {
offset = (m.ChrBank0Number | 0x01) * 1024
}
val = mem.chrROM[offset+int(addr-0x0400)]
case addr >= 0x0800 && addr < 0x0c00:
var offset int
if m.ChrUpperBanksAreBigger {
offset = m.ChrBank4Number * 1024
} else {
offset = (m.ChrBank1Number &^ 0x01) * 1024
}
val = mem.chrROM[offset+int(addr-0x0800)]
case addr >= 0x0c00 && addr < 0x1000:
var offset int
if m.ChrUpperBanksAreBigger {
offset = m.ChrBank5Number * 1024
} else {
offset = (m.ChrBank1Number | 0x01) * 1024
}
val = mem.chrROM[offset+int(addr-0x0c00)]
case addr >= 0x1000 && addr < 0x1400:
var offset int
if m.ChrUpperBanksAreBigger {
offset = (m.ChrBank0Number &^ 0x01) * 1024
} else {
offset = m.ChrBank2Number * 1024
}
val = mem.chrROM[offset+int(addr-0x1000)]
case addr >= 0x1400 && addr < 0x1800:
var offset int
if m.ChrUpperBanksAreBigger {
offset = (m.ChrBank0Number | 0x01) * 1024
} else {
offset = m.ChrBank3Number * 1024
}
val = mem.chrROM[offset+int(addr-0x1400)]
case addr >= 0x1800 && addr < 0x1c00:
var offset int
if m.ChrUpperBanksAreBigger {
offset = (m.ChrBank1Number &^ 0x01) * 1024
} else {
offset = m.ChrBank4Number * 1024
}
val = mem.chrROM[offset+int(addr-0x1800)]
case addr >= 0x1c00 && addr < 0x2000:
var offset int
if m.ChrUpperBanksAreBigger {
offset = (m.ChrBank1Number | 0x01) * 1024
} else {
offset = m.ChrBank5Number * 1024
}
val = mem.chrROM[offset+int(addr-0x1c00)]
case addr >= 0x2000 && addr < 0x3000:
var realAddr uint16
if m.VramMirroring == VerticalMirroring {
realAddr = vertMirrorVRAMAddr(addr)
} else if m.VramMirroring == HorizontalMirroring {
realAddr = horizMirrorVRAMAddr(addr)
} else {
emuErr(fmt.Sprintf("mapper004: unimplemented vram mirroring: %v at read(%04x, %02x)", m.VramMirroring, addr, val))
}
val = mem.InternalVRAM[realAddr]
default:
emuErr(fmt.Sprintf("mapper004: unimplemented vram access: read(%04x, %02x)", addr, val))
}
return val
}
func (m *mapper004) WriteVRAM(mem *mem, addr uint16, val byte) {
switch {
case addr < 0x0400:
var offset int
if m.ChrUpperBanksAreBigger {
offset = m.ChrBank2Number * 1024
} else {
offset = (m.ChrBank0Number &^ 0x01) * 1024
}
mem.chrROM[offset+int(addr)] = val
case addr >= 0x0400 && addr < 0x0800:
var offset int
if m.ChrUpperBanksAreBigger {
offset = m.ChrBank3Number * 1024
} else {
offset = (m.ChrBank0Number | 0x01) * 1024
}
mem.chrROM[offset+int(addr-0x0400)] = val
case addr >= 0x0800 && addr < 0x0c00:
var offset int
if m.ChrUpperBanksAreBigger {
offset = m.ChrBank4Number * 1024
} else {
offset = (m.ChrBank1Number &^ 0x01) * 1024
}
mem.chrROM[offset+int(addr-0x0800)] = val
case addr >= 0x0c00 && addr < 0x1000:
var offset int
if m.ChrUpperBanksAreBigger {
offset = m.ChrBank5Number * 1024
} else {
offset = (m.ChrBank1Number | 0x01) * 1024
}
mem.chrROM[offset+int(addr-0x0c00)] = val
case addr >= 0x1000 && addr < 0x1400:
var offset int
if m.ChrUpperBanksAreBigger {
offset = (m.ChrBank0Number &^ 0x01) * 1024
} else {
offset = m.ChrBank2Number * 1024
}
mem.chrROM[offset+int(addr-0x1000)] = val
case addr >= 0x1400 && addr < 0x1800:
var offset int
if m.ChrUpperBanksAreBigger {
offset = (m.ChrBank0Number | 0x01) * 1024
} else {
offset = m.ChrBank3Number * 1024
}
mem.chrROM[offset+int(addr-0x1400)] = val
case addr >= 0x1800 && addr < 0x1c00:
var offset int
if m.ChrUpperBanksAreBigger {
offset = (m.ChrBank1Number &^ 0x01) * 1024
} else {
offset = m.ChrBank4Number * 1024
}
mem.chrROM[offset+int(addr-0x1800)] = val
case addr >= 0x1c00 && addr < 0x2000:
var offset int
if m.ChrUpperBanksAreBigger {
offset = (m.ChrBank1Number | 0x01) * 1024
} else {
offset = m.ChrBank5Number * 1024
}
mem.chrROM[offset+int(addr-0x1c00)] = val
case addr >= 0x2000 && addr < 0x3000:
var realAddr uint16
if m.VramMirroring == VerticalMirroring {
realAddr = vertMirrorVRAMAddr(addr)
} else if m.VramMirroring == HorizontalMirroring {
realAddr = horizMirrorVRAMAddr(addr)
} else {
emuErr(fmt.Sprintf("mapper004: unimplemented vram mirroring: write(%04x, %02x)", addr, val))
}
mem.InternalVRAM[realAddr] = val
default:
emuErr(fmt.Sprintf("mapper004: unimplemented vram access: write(%04x, %02x)", addr, val))
}
}
type mapper007 struct {
VramMirroring MirrorInfo
PrgBankNumber int
}
func (m *mapper007) Init(mem *mem) {
m.VramMirroring = OneScreenLowerMirroring
}
func (m *mapper007) RunCycle(emu *emuState) {}
func (m *mapper007) Marshal() marshalledMMC { return marshalMMC(7, m) }
func (m *mapper007) Read(mem *mem, addr uint16) byte {
if addr >= 0x6000 && addr < 0x8000 {
// no RAM in this mapper
return 0xff
}
if addr >= 0x8000 {
offset := m.PrgBankNumber * 32 * 1024
return mem.prgROM[offset+int(addr-0x8000)]
}
panic("impossible case")
}
func (m *mapper007) Write(mem *mem, addr uint16, val byte) {
if addr >= 0x6000 && addr < 0x8000 {
// no RAM in this mapper
}
if addr >= 0x8000 {
m.PrgBankNumber = int(val & 0x07)
m.PrgBankNumber &= len(mem.prgROM)/(32*1024) - 1
if val&0x10 == 0x10 {
m.VramMirroring = OneScreenUpperMirroring
} else {
m.VramMirroring = OneScreenLowerMirroring
}
}
}
func (m *mapper007) ReadVRAM(mem *mem, addr uint16) byte {
var val byte
switch {
case addr < 0x2000:
val = mem.chrROM[addr]
case addr >= 0x2000 && addr < 0x3000:
switch m.VramMirroring {
case OneScreenLowerMirroring:
val = mem.InternalVRAM[oneScreenLowerVRAMAddr(addr)]
case OneScreenUpperMirroring:
val = mem.InternalVRAM[oneScreenUpperVRAMAddr(addr)]
default:
emuErr(fmt.Sprintf("mapper007: unimplemented vram mirroring %v: read(%04x)", m.VramMirroring, addr))
}
default:
emuErr(fmt.Sprintf("mapper007: unimplemented vram access: read(%04x, %02x)", addr, val))
}
return val
}
func (m *mapper007) WriteVRAM(mem *mem, addr uint16, val byte) {
switch {
case addr < 0x2000:
mem.chrROM[addr] = val
case addr >= 0x2000 && addr < 0x3000:
switch m.VramMirroring {
case OneScreenLowerMirroring:
mem.InternalVRAM[oneScreenLowerVRAMAddr(addr)] = val
case OneScreenUpperMirroring:
mem.InternalVRAM[oneScreenUpperVRAMAddr(addr)] = val
default:
emuErr(fmt.Sprintf("mapper007: unimplemented vram mirroring %v: write(%04x, %02x)", m.VramMirroring, addr, val))
}
default:
emuErr(fmt.Sprintf("mapper007: unimplemented vram access: write(%04x, %02x)", addr, val))
}
}
type mapper031 struct {
VramMirroring MirrorInfo
IsChrRAM bool
bankNumSlots [8]int
}
func (m *mapper031) Init(mem *mem) {
m.bankNumSlots[7] = len(mem.prgROM)/(4*1024) - 1
}
func (m *mapper031) RunCycle(emu *emuState) {}
func (m *mapper031) Marshal() marshalledMMC { return marshalMMC(31, m) }
func (m *mapper031) Read(mem *mem, addr uint16) byte {
if addr >= 0x6000 && addr < 0x8000 {
// no prg RAM in this mapper
return 0xff
}
if addr >= 0x8000 {
slotNum := (addr - 0x8000) >> 12
offset := m.bankNumSlots[slotNum] * (4 * 1024)
strippedAddr := int(addr - 0x8000 - (0x1000 * slotNum))
realAddr := offset + strippedAddr
return mem.prgROM[realAddr]
}
return 0xff
}
func (m *mapper031) Write(mem *mem, addr uint16, val byte) {
if addr >= 0x5000 && addr < 0x6000 {
m.bankNumSlots[addr&0x07] = int(val)
}
if addr >= 0x6000 && addr < 0x8000 {
// no prg RAM in this mapper
}
}
func (m *mapper031) ReadVRAM(mem *mem, addr uint16) byte {
var val byte
switch {
case addr < 0x2000:
val = mem.chrROM[addr]
case addr >= 0x2000 && addr < 0x3000:
var realAddr uint16