-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathCoolGirl_mappers.vh
1509 lines (1438 loc) · 62.1 KB
/
CoolGirl_mappers.vh
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
reg [26:14] prg_base = 0;
reg [20:14] prg_mask = 7'b1111000;
reg [18:13] chr_mask = 0;
reg [2:0] prg_mode = 0;
reg map_rom_on_6000 = 0;
reg [7:0] prg_bank_6000 = 0;
reg [7:0] prg_bank_a = 0;
reg [7:0] prg_bank_b = 1;
reg [7:0] prg_bank_c = 8'b11111110;
reg [7:0] prg_bank_d = 8'b11111111;
reg [2:0] chr_mode = 0;
reg [8:0] chr_bank_a = 0;
reg [8:0] chr_bank_b = 1;
reg [8:0] chr_bank_c = 2;
reg [8:0] chr_bank_d = 3;
reg [8:0] chr_bank_e = 4;
reg [8:0] chr_bank_f = 5;
reg [8:0] chr_bank_g = 6;
reg [8:0] chr_bank_h = 7;
reg [5:0] mapper = 0;
reg [2:0] flags = 0;
reg sram_enabled = 0;
reg [1:0] sram_page = 0;
reg chr_write_enabled = 0;
reg prg_write_enabled = 0;
reg [1:0] mirroring = 0;
reg four_screen = 0;
reg lockout = 0;
reg [8:0] controller_data = 0;
reg [1:0] reset_state = 0;
// Multiplier
reg [7:0] mul1 = 0;
reg [7:0] mul2 = 0;
wire [15:0] mul = mul1*mul2;
// IRQ stuff
assign irq = (
mmc3_irq_out |
mmc5_irq_out |
mapper18_irq_out |
mapper65_irq_out |
vrc4_irq_out |
vrc3_irq_out |
mapper69_irq_out |
mapper42_irq_out |
mapper83_irq_out |
mapper90_irq_out |
mapper67_irq_out) ? 1'b0 : 1'bZ;
// for MMC3 scanline-based interrupts, counts A12 rises after long A12 falls
reg mmc3_irq_enabled = 0; // register to enable/disable counter
reg [7:0] mmc3_irq_latch = 0; // stores counter reload latch value
reg [7:0] mmc3_irq_counter = 0; // counter itself (downcounting)
reg [1:0] a12_low_time = 0; // counts how long A12 is low
reg mmc3_irq_reload = 0; // flag to reload counter from latch
reg mmc3_irq_reload_clear = 0; // flag to clear reload flag
reg mmc3_irq_ready = 0; // stores 1 when IRQ is ready (enabled and non-zero)
reg mmc3_irq_out = 0; // stores 1 when IRQ is triggered
// scanline counter, counts dummy PPU reads, detects v-blank automatically
reg [3:0] ppu_rd_hi_time = 0; // counts how long there is no reads from PPU to detect v-blank
reg [1:0] ppu_nt_read_count; // nametable read counter
reg [7:0] scanline = 0; // current scanline
reg new_screen = 0; // stores 1 when v-blank detected ("in frame" flag)
reg new_screen_clear = 0; // flag to clear new_screen flag
// for MMC5 scanline-based interrupts, counts dummy PPU reads
reg mmc5_irq_enabled = 0; // register to enable/disable counter
reg [7:0] mmc5_irq_line = 0; // scanline on which IRQ will be triggered
reg mmc5_irq_ack = 0; // flag to acknowledge IRQ
reg mmc5_irq_out = 0; // stores 1 when IRQ is triggered
// for mapper #18
reg [15:0] mapper18_irq_value = 0; // counter itself (downcounting)
reg [3:0] mapper18_irq_control = 0; // IRQ settings
reg [15:0] mapper18_irq_latch = 0; // stores counter reload latch value
reg mapper18_irq_out = 0;
// for mapper #65
reg mapper65_irq_enabled = 0; // register to enable/disable IRQ
reg [15:0] mapper65_irq_value = 0; // counter itself (downcounting)
reg [15:0] mapper65_irq_latch = 0; // stores counter reload latch value
reg mapper65_irq_out = 0;
// for Sunsoft FME-7
reg mapper69_irq_enabled = 0; // register to enable/disable IRQ
reg mapper69_counter_enabled = 0; // register to enable/disable counter
reg [15:0] mapper69_irq_value = 0; // counter itself (downcounting)
reg mapper69_irq_out = 0; // stores 1 when IRQ is triggered
// for VRC4, CPU-based interrupts
reg [7:0] vrc4_irq_value = 0; // counter itself (upcounting)
reg [2:0] vrc4_irq_control = 0; // IRQ settings
reg [7:0] vrc4_irq_latch = 0; // stores counter reload latch value
reg [6:0] vrc4_irq_prescaler = 0; // prescaler counter for VRC4
reg [1:0] vrc4_irq_prescaler_counter = 0; // prescaler cicles counter for VRC4
reg vrc4_irq_out = 0; // stores 1 when IRQ is triggered
// for VRC3, CPU-based interrupts
reg [15:0] vrc3_irq_value = 0; // counter itself (upcounting)
reg [3:0] vrc3_irq_control = 0; // IRQ settings
reg [15:0] vrc3_irq_latch = 0; // stores counter reload latch value
reg vrc3_irq_out = 0; // stores 1 when IRQ is triggered
// for mapper #42 (only Baby Mario)
reg mapper42_irq_enabled = 0; // register to enable/disable counter
reg [14:0] mapper42_irq_value = 0; // counter itself (upcounting)
wire mapper42_irq_out = mapper42_irq_value[14] & mapper42_irq_value[13];
// for mapper #83, CPU-based interrupts
reg mapper83_irq_enabled_latch = 0;
reg mapper83_irq_enabled = 0;
reg [15:0] mapper83_irq_counter = 0;
reg mapper83_irq_out = 0;
// for mapper #90, unfiltered PPU A12 counter
reg mapper90_irq_enabled = 0; // register to enable/disable counter
reg [7:0] mapper90_xor; // XOR register (is not used actually)
reg [10:0] mapper90_irq_latch = 0; // stores counter reload latch value
reg [10:0] mapper90_irq_counter = 0; // counter itself (downcounting)
reg mapper90_irq_reload = 0; // flag to reload counter and prescaler from latch
reg mapper90_irq_reload_clear = 0; // flag to clear reload flag
reg mapper90_irq_pending = 0; // flag of pending IRQ
reg mapper90_irq_out = 0; // stores 1 when IRQ is triggered
reg mapper90_irq_out_clear = 0; // flag to clear pending flag
// for mapper #67, CPU-based interrupts
reg mapper67_irq_enabled = 0;
reg mapper67_irq_latch = 0;
reg [15:0] mapper67_irq_counter = 0;
reg mapper67_irq_out = 0;
// Mapper specific stuff
// for MMC2/MMC4
reg ppu_latch0 = 0;
reg ppu_latch1 = 0;
// for MMC1
reg [5:0] mmc1_load_register = 0;
// for MMC3
reg [2:0] mmc3_internal = 0;
// for mapper #69
reg [3:0] mapper69_internal = 0;
// for mapper #112
reg [2:0] mapper112_internal = 0;
// for mapper #163
reg mapper_163_latch = 0;
reg [7:0] mapper163_r0 = 0;
reg [7:0] mapper163_r1 = 0;
reg [7:0] mapper163_r2 = 0;
reg [7:0] mapper163_r3 = 0;
reg [7:0] mapper163_r4 = 0;
reg [7:0] mapper163_r5 = 0;
// to block two writes in a row
reg writed;
// for VRC
wire shift_chr_right = ENABLE_MAPPER_021_022_023_025 && ENABLE_MAPPER_022 && (mapper == 6'b011000) && flags[1];
wire shift_chr_left = 0;
wire vrc_2b_hi =
(!flags[0] && !flags[2]) ?
(cpu_addr_in[7] | cpu_addr_in[2]) // mapper #21
: (flags[0] && !flags[2]) ?
(cpu_addr_in[0]) // mapper #22
: (!flags[0] && flags[2]) ?
(cpu_addr_in[5] | cpu_addr_in[3] | cpu_addr_in[1]) // mapper #23
: (cpu_addr_in[2] | cpu_addr_in[0]); // mapper #25
wire vrc_2b_low =
(!flags[0] && !flags[2]) ?
(cpu_addr_in[6] | cpu_addr_in[1]) // mapper #21
: (flags[0] && !flags[2]) ?
(cpu_addr_in[1]) // mapper #22
: (!flags[0] && flags[2]) ?
(cpu_addr_in[4] | cpu_addr_in[2] | cpu_addr_in[0]) // mapper #23
: (cpu_addr_in[3] | cpu_addr_in[1]); // mapper #25
wire cpu_data_out_enabled;
wire [7:0] cpu_data_out;
assign {cpu_data_out_enabled, cpu_data_out} =
(cpu_rw_in && m2) ? // reading?
(
((RESET_COMBINATION != 0) && (reset_state != 0)) // resetting?
? ( // resetting
// jmp [$FFFC]
reset_state == 1 ?
{1'b1, 8'h6C}
: reset_state == 2 ?
{1'b1, 8'hFC}
:
{1'b1, 8'hFF}
)
: ( // not resetting
romsel ? // reading $0000-$7FFF?
(
((mapper == 0) && (cpu_addr_in[14:12] == 3'b101)) ? {8'b10000000, new_dendy} : // $5000 - $5FFF - new dendy flag
(ENABLE_MAPPER_163 && (mapper == 6'b000110) && ({cpu_addr_in[14:12],cpu_addr_in[10:8]} == 6'b101001)) ?
{1'b1, mapper163_r2 | mapper163_r0 | mapper163_r1 | ~mapper163_r3} :
(ENABLE_MAPPER_163 && (mapper == 6'b000110) && ({cpu_addr_in[14:12],cpu_addr_in[10:8]} == 6'b101101)) ?
{1'b1, mapper163_r5[0] ? mapper163_r2 : mapper163_r1} :
(ENABLE_MAPPER_005 && (mapper == 6'b001111) && (cpu_addr_in[14:0] == 15'h5204)) ?
{1'b1, mmc5_irq_out, ~new_screen, 6'b000000} :
(ENABLE_MAPPER_036 && mapper == 6'b011101 && {cpu_addr_in[14:13], cpu_addr_in[8]} == 3'b101) ? // Need by Strike Wolf, being simplified mapper, this cart still uses some TCX mapper features andrely on it
{1'b1, 2'b00, prg_bank_a[3:2], 4'b00} :
(ENABLE_MAPPER_083 && mapper == 6'b100011 && {cpu_addr_in[14:12]} == 3'b101) ? // $5000 - DIP switches
{1'b1, 6'b000000, flags[1:0]} :
(ENABLE_MAPPER_090_MULTIPLIER && (mapper == 6'b001101) && (cpu_addr_in[14:0] == 15'h5800)) ? {1'b1, mul[7:0]} :
(ENABLE_MAPPER_090_MULTIPLIER && (mapper == 6'b001101) && (cpu_addr_in[14:0] == 15'h5801)) ? {1'b1, mul[15:8]} :
9'b00000000
): 9'b00000000
)
) : 9'b00000000;
// Mirroring: 00=vertical, 01=horizontal, 10=1Sa, 11=1Sb
assign ppu_ciram_a10 = (ENABLE_MAPPER_118 & (mapper == 6'b010100) & flags[0]) ? chr_addr_mapped[17] :
(mirroring[1] ? mirroring[0] : (mirroring[0] ? ppu_addr_in[11] : ppu_addr_in[10]));
wire [20:13] prg_addr_mapped = (map_rom_on_6000 & romsel & m2) ? prg_bank_6000 :
(
prg_mode[2] ? (
prg_mode[1] ? (
prg_mode[0] ? (
// 111 - 0x8000(A)
{prg_bank_a[7:2], cpu_addr_in[14:13]}
) : (
// 110 - 0x8000(B)
{prg_bank_b[7:2], cpu_addr_in[14:13]}
)
) : ( // prg_mode[1]
prg_mode[0] ? (
// 101 - 0x2000(C)+0x2000(B)+0x2000(A)+0x2000(D)
cpu_addr_in[14] ? (cpu_addr_in[13] ? prg_bank_d : prg_bank_a) : (cpu_addr_in[13] ? prg_bank_b : prg_bank_c)
) : ( // prg_mode[0]
// 100 - 0x2000(A)+0x2000(B)+0x2000(C)+0x2000(D)
cpu_addr_in[14] ? (cpu_addr_in[13] ? prg_bank_d : prg_bank_c) : (cpu_addr_in[13] ? prg_bank_b : prg_bank_a)
)
)
) : ( // prg_mode[2]
prg_mode[0] ? (
// 0x1 - 0x4000(C) + 0x4000 (A)
{cpu_addr_in[14] ? prg_bank_a[7:1] : prg_bank_c[7:1], cpu_addr_in[13]}
) : ( // prg_mode[0]
// 0x0 - 0x4000(A) + 0x4000 (С)
{cpu_addr_in[14] ? prg_bank_c[7:1] : prg_bank_a[7:1], cpu_addr_in[13]}
)
)
);
wire [18:10] chr_addr_mapped = (
chr_mode[2] ? (
chr_mode[1] ? (
chr_mode[0] ? (
// 111 - 0x400(A)+0x400(B)+0x400(C)+0x400(D)+0x400(E)+0x400(F)+0x400(G)+0x400(H)
ppu_addr_in[12] ?
(ppu_addr_in[11] ? (ppu_addr_in[10] ? chr_bank_h : chr_bank_g) :
(ppu_addr_in[10] ? chr_bank_f : chr_bank_e)) : (ppu_addr_in[11] ? (ppu_addr_in[10] ? chr_bank_d : chr_bank_c) : (ppu_addr_in[10] ? chr_bank_b : chr_bank_a))
) : ( // chr_mode[0]
// 110 - 0x800(A)+0x800(C)+0x800(E)+0x800(G)
{ppu_addr_in[12] ?
(ppu_addr_in[11] ? chr_bank_g[8:1] : chr_bank_e[8:1]) :
(ppu_addr_in[11] ? chr_bank_c[8:1] : chr_bank_a[8:1]), ppu_addr_in[10]}
)
) : ( // chr_mode[1]
// 100 - 0x1000(A) + 0x1000(E)
// 101 - 0x1000(A/B) + 0x1000(E/F) - MMC2 and MMC4
{ppu_addr_in[12] ?
(((ENABLE_MAPPER_009_010) && chr_mode[0] && ppu_latch1) ? chr_bank_f[8:2] : chr_bank_e[8:2]) :
(((ENABLE_MAPPER_009_010) && chr_mode[0] && ppu_latch0) ? chr_bank_b[8:2] : chr_bank_a[8:2]),
ppu_addr_in[11:10]}
)
) : ( // chr_mode[2]
chr_mode[1] ? (
// 010 - 0x800(A)+0x800(C)+0x400(E)+0x400(F)+0x400(G)+0x400(H)
// 011 - 0x400(E)+0x400(F)+0x400(G)+0x400(H)+0x800(A)+0x800(С)
(ppu_addr_in[12]^chr_mode[0]) ?
(ppu_addr_in[11] ?
(ppu_addr_in[10] ? chr_bank_h : chr_bank_g) :
(ppu_addr_in[10] ? chr_bank_f : chr_bank_e)
) : (
ppu_addr_in[11] ? {chr_bank_c[8:1],ppu_addr_in[10]} : {chr_bank_a[8:1],ppu_addr_in[10]}
)
) : ( // chr_mode[1]
(ENABLE_MAPPER_163 && chr_mode[0]) ? (
// 001 - Mapper #163 special
{mapper_163_latch, ppu_addr_in[11:10]}
) : (
// 000 - 0x2000(A)
{chr_bank_a[8:3], ppu_addr_in[12:10]}
)
)
)
) >> shift_chr_right << shift_chr_left;
always @ (negedge m2)
begin
// for MMC3
if (mmc3_irq_reload_clear)
mmc3_irq_reload <= 0;
// IRQ for VRC4
if (ENABLE_MAPPER_021_022_023_025 & ENABLE_VRC4_INTERRUPTS & (vrc4_irq_control[1]))
begin
reg carry;
// Cycle mode without prescaler is not used by any games? It's missed in fceux source code.
/*
if (vrc4_irq_control[2]) // cycle mode
begin
{carry, vrc4_irq_value[7:0]} = vrc4_irq_value[7:0] + 1'b1; // just count IRQ value
if (carry)
begin
vrc4_irq_out = 1;
vrc4_irq_value[7:0] = vrc4_irq_latch[7:0];
end
end else
*/
begin // scanline mode
vrc4_irq_prescaler = vrc4_irq_prescaler + 1'b1; // count prescaler
if ((vrc4_irq_prescaler_counter[1] == 0 && vrc4_irq_prescaler == 114)
|| (vrc4_irq_prescaler_counter[1] == 1 && vrc4_irq_prescaler == 113)) // 114, 114, 113
begin
vrc4_irq_prescaler = 0;
vrc4_irq_prescaler_counter = vrc4_irq_prescaler_counter + 1'b1;
if (vrc4_irq_prescaler_counter == 2'b11) vrc4_irq_prescaler_counter = 2'b00;
{carry, vrc4_irq_value[7:0]} = vrc4_irq_value[7:0] + 1'b1;
if (carry)
begin
vrc4_irq_out = 1;
vrc4_irq_value[7:0] = vrc4_irq_latch[7:0];
end
end
end
end
// IRQ for VRC3
if (ENABLE_MAPPER_073 & (vrc3_irq_control[1]))
begin
if (vrc3_irq_control[2])
begin // 8-bit mode
vrc3_irq_value[7:0] = vrc3_irq_value[7:0] + 1'b1;
if (vrc3_irq_value[7:0] == 0)
begin
vrc3_irq_out <= 1;
vrc3_irq_value[7:0] = vrc3_irq_latch[7:0];
end
end else begin // 16-bit mode
vrc3_irq_value[15:0] = vrc3_irq_value[15:0] + 1'b1;
if (vrc3_irq_value[15:0] == 0)
begin
vrc3_irq_out <= 1;
vrc3_irq_value[15:0] = vrc3_irq_latch[15:0];
end
end
end
// IRQ for Sunsoft FME-7
if (ENABLE_MAPPER_069 & mapper69_counter_enabled)
begin
reg carry;
{carry, mapper69_irq_value[15:0]} <= {1'b0, mapper69_irq_value[15:0]} - 1'b1;
if (mapper69_irq_enabled && carry) mapper69_irq_out <= 1;
end
// Mapper #18 - Sunsoft-2
if (ENABLE_MAPPER_018)
begin
if (mapper18_irq_control[0])
begin
reg carry;
{carry, mapper18_irq_value[3:0]} = mapper18_irq_value[3:0] - 1'b1;
if (mapper18_irq_control[3] == 1'b0)
{carry, mapper18_irq_value[7:4]} = mapper18_irq_value[7:4] - carry;
if (mapper18_irq_control[3:2] == 2'b00)
{carry, mapper18_irq_value[11:8]} = mapper18_irq_value[11:8] - carry;
if (mapper18_irq_control[3:1] == 3'b000)
{carry, mapper18_irq_value[15:12]} = mapper18_irq_value[15:12] - carry;
mapper18_irq_out <= mapper18_irq_out | carry;
end
end
// Mapper #65 - Irem's H3001
if (ENABLE_MAPPER_065)
begin
if (mapper65_irq_enabled)
begin
if (mapper65_irq_value[15:0] != 0)
begin
mapper65_irq_value[15:0] = mapper65_irq_value[15:0] - 1'b1;
if (mapper65_irq_value[15:0] == 0) mapper65_irq_out <= 1;
end
end
end
// IRQ for mapper #42
if (ENABLE_MAPPER_042 & ENABLE_MAPPER_042_INTERRUPTS & mapper42_irq_enabled)
begin
mapper42_irq_value[14:0] <= mapper42_irq_value[14:0] + 1'b1;
end
// for mapper #83
if (ENABLE_MAPPER_083)
begin
if (mapper83_irq_enabled)
begin
if (mapper83_irq_counter == 0)
mapper83_irq_out <= 1;
mapper83_irq_counter = mapper83_irq_counter - 1'b1;
end
end
// for mapper #90
if (mapper90_irq_pending & !mapper90_irq_out_clear)
begin
mapper90_irq_out <= 1;
mapper90_irq_out_clear <= 1;
end else if (!mapper90_irq_pending) begin
mapper90_irq_out_clear <= 0;
end
if (mapper90_irq_reload_clear)
mapper90_irq_reload <= 0;
// for mapper #67
if (ENABLE_MAPPER_067 && mapper67_irq_enabled)
begin
mapper67_irq_counter = mapper67_irq_counter - 1'b1;
if (mapper67_irq_counter == 16'hFFFF)
begin
mapper67_irq_out <= 1; // fire IRQ
mapper67_irq_enabled <= 0; // disable IRQ
end
end
if (cpu_rw_in == 1) // read
begin
// block two writes in a row (RMW) for games like Snow Bros. and Bill & Ted's Excellent Adventure
// also you can remove this check and just patch those games, lol
writed <= 0;
if ((RESET_COMBINATION != 0) && (reset_state != 0))
begin
// increase reset state after each read
reset_state = reset_state + 1'b1;
end
// Reading controller
if ((RESET_COMBINATION != 0) && (cpu_addr_in == 15'h4016) && lockout)
begin
controller_data[8:0] = {controller_data[7:0], cpu_data_in[0]};
if (controller_data[8:0] == {1'b1, RESET_COMBINATION})
begin
// Reset combination pressed. Reset!
// Revert registers to original state
prg_base <= 0;
prg_mask <= 7'b1111000;
chr_mask <= 0;
prg_mode <= 0;
map_rom_on_6000 <= 0;
prg_bank_6000 <= 0;
prg_bank_a <= 0;
prg_bank_b <= 1;
prg_bank_c <= 8'b11111110;
prg_bank_d <= 8'b11111111;
chr_mode <= 0;
chr_bank_a <= 0;
chr_bank_b <= 1;
chr_bank_c <= 2;
chr_bank_d <= 3;
chr_bank_e <= 4;
chr_bank_f <= 5;
chr_bank_g <= 6;
chr_bank_h <= 7;
mapper <= 0;
flags <= 0;
sram_enabled <= 0;
sram_page <= 0;
chr_write_enabled <= 0;
prg_write_enabled <= 0;
mirroring <= 0;
four_screen <= 0;
lockout <= 0;
// Disable IRQs
mmc3_irq_enabled <= 0;
mmc5_irq_enabled <= 0;
mapper18_irq_control <= 0;
mapper65_irq_enabled <= 0;
mapper69_irq_enabled <= 0;
vrc4_irq_control <= 0;
vrc3_irq_control <= 0;
mapper42_irq_enabled <= 0;
mapper83_irq_enabled <= 0;
mapper90_irq_enabled <= 0;
mapper67_irq_enabled <= 0;
// Acknowledge IRQs
mmc5_irq_ack <= 1;
mapper18_irq_out <= 0;
mapper65_irq_out <= 0;
vrc4_irq_out <= 0;
vrc3_irq_out <= 0;
mapper69_irq_out <= 0;
mapper42_irq_value <= 0;
mapper83_irq_out <= 0;
mapper90_irq_out <= 0;
mapper67_irq_out <= 0;
// Start reset sequence
reset_state = 1;
end
end
end else if (cpu_rw_in == 0 && !writed) // write
begin
writed <= 1;
if (romsel) // $0000-$7FFF
begin
if ((cpu_addr_in[14:12] == 3'b101) && (lockout == 0)) // $5000-5FFF & lockout is off
begin
case (cpu_addr_in[2:0])
3'b000: // $5xx0
{prg_base[26:22]} <= cpu_data_in[4:0]; // CPU base address A26-A22
3'b001: // $5xx1
prg_base[21:14] <= cpu_data_in[7:0]; // CPU base address A21-A14
3'b010: // $5xx2
{chr_mask[18], prg_mask[20:14]} <= cpu_data_in[7:0]; // CHR mask A18, CPU mask A20-A14
3'b011: // $5xx3
{prg_mode[2:0], chr_bank_a[7:3]} <= cpu_data_in[7:0]; // PRG mode, direct chr_bank_a access
3'b100: // $5xx4
{chr_mode[2:0], chr_mask[17:13]} <= cpu_data_in[7:0]; // CHR mode, CHR mask A17-A13
3'b101: // $5xx5
{chr_bank_a[8], prg_bank_a[5:1], sram_page[1:0]} <= cpu_data_in[7:0]; // direct prg_bank_a access, current SRAM page 0-3
3'b110: // $5xx6
{flags[2:0], mapper[4:0]} <= cpu_data_in[7:0]; // some flags, mapper
3'b111: // $5xx7
// some other parameters
begin
{lockout, mapper[5], four_screen, mirroring[1:0], prg_write_enabled, chr_write_enabled, sram_enabled} <= cpu_data_in[7:0];
// some unusual init stuff
if (ENABLE_MAPPER_009_010 && mapper == 6'b010001) prg_bank_b <= 8'b11111101;
if (ENABLE_MAPPER_042 && (mapper == 6'b010111)) map_rom_on_6000 <= 1;
if (ENABLE_MAPPER_065 && mapper == 6'b001110) prg_bank_b <= 1;
end
endcase
end
if ((RESET_COMBINATION != 0) && (cpu_addr_in == 15'h4016))
begin
controller_data = 1;
end
// Mapper #163
if (ENABLE_MAPPER_163 && (mapper == 6'b000110))
begin
if (cpu_addr_in[14:0] == 15'h5101)
begin
if ((mapper163_r4 != 0) && (cpu_data_in == 0))
mapper163_r5[0] <= ~mapper163_r5[0];
mapper163_r4 <= cpu_data_in;
end else if ((cpu_addr_in[14:0] == 15'h5100) && (cpu_data_in == 6))
begin
prg_mode[0] <= 0;
prg_bank_b <= 4'b1100;
end else if (cpu_addr_in[14:12] == 3'b101) begin
case (cpu_addr_in[9:8])
2'b10: begin
prg_mode[0] <= 1;
prg_bank_a[7:6] <= cpu_data_in[1:0];
mapper163_r0 <= cpu_data_in;
end
2'b00: begin
prg_mode[0] <= 1;
prg_bank_a[5:2] <= cpu_data_in[3:0];
chr_mode[0] <= cpu_data_in[7];
mapper163_r1 <= cpu_data_in;
end
2'b11: mapper163_r2 <= cpu_data_in;
2'b01: mapper163_r3 <= cpu_data_in;
endcase
end
end
// Mapper #87
if (ENABLE_MAPPER_087 && (mapper == 6'b001100))
begin
if (cpu_addr_in[14:13] == 2'b11) // $6000-$7FFF
begin
chr_bank_a[4:3] <= {cpu_data_in[0], cpu_data_in[1]};
end
end
// Mapper #90 - JY
if (ENABLE_MAPPER_090_MULTIPLIER && (mapper == 6'b001101))
begin
if (cpu_addr_in[14:0] == 15'h5800)
mul1 <= cpu_data_in;
if (cpu_addr_in[14:0] == 15'h5801)
mul2 <= cpu_data_in;
end
// MMC5
if (ENABLE_MAPPER_005 && (mapper == 6'b001111))
begin
// just workaround for Castlevania 3, not real MMC5
case (cpu_addr_in[14:0])
15'h5105: begin // mirroring
if (cpu_data_in == 8'b11111111)
four_screen <= 1;
else begin
four_screen <= 0;
case ({cpu_data_in[4], cpu_data_in[2]})
2'b00: mirroring <= 2'b10;
2'b01: mirroring <= 2'b00;
2'b10: mirroring <= 2'b01;
2'b11: mirroring <= 2'b11;
endcase
end
end
15'h5115: begin
prg_bank_a[4:0] <= {cpu_data_in[4:1], 1'b0};
prg_bank_b[4:0] <= {cpu_data_in[4:1], 1'b1};
end
15'h5116: prg_bank_c[4:0] <= cpu_data_in[4:0];
15'h5117: prg_bank_d[4:0] <= cpu_data_in[4:0];
15'h5120: chr_bank_a[7:0] <= cpu_data_in[7:0];
15'h5121: chr_bank_b[7:0] <= cpu_data_in[7:0];
15'h5122: chr_bank_c[7:0] <= cpu_data_in[7:0];
15'h5123: chr_bank_d[7:0] <= cpu_data_in[7:0];
15'h5128: chr_bank_e[7:0] <= cpu_data_in[7:0];
15'h5129: chr_bank_f[7:0] <= cpu_data_in[7:0];
15'h512A: chr_bank_g[7:0] <= cpu_data_in[7:0];
15'h512B: chr_bank_h[7:0] <= cpu_data_in[7:0];
15'h5203: begin
mmc5_irq_ack <= 1;
mmc5_irq_line[7:0] <= cpu_data_in[7:0];
end
15'h5204: begin
mmc5_irq_ack <= 1;
mmc5_irq_enabled <= cpu_data_in[7];
end
endcase
end
// Mapper #189
// It's MMC3 with flag1
if (ENABLE_MAPPER_189 & flags[1] & (mapper == 6'b010100))
begin
if (cpu_addr_in[14:0] >= 15'h4120) // $4120-$7FFF
begin
prg_bank_a[5:2] <= cpu_data_in[3:0] | cpu_data_in[7:4];
end
end
// Mappers #79 and #146 - NINA-03/06 and Sachen 3015: (flag0 = 1)
if (ENABLE_MAPPER_079_146 && (mapper == 6'b011011))
begin
if ({cpu_addr_in[14:13], cpu_addr_in[8]} == 3'b101)
begin
// That is, $4100-$41FF, $4300-$43FF, $45xx, $47xx, ..., $5Dxx, and $5Fxx.
chr_bank_a[5:3] <= cpu_data_in[2:0];
prg_bank_a[2] <= cpu_data_in[3];
end
end
// Mapper #133
if (ENABLE_MAPPER_133 && (mapper == 6'b011100))
begin
if ({cpu_addr_in[14:13], cpu_addr_in[8]} == 3'b101)
begin
chr_bank_a[4:3] <= cpu_data_in[1:0];
prg_bank_a[2] <= cpu_data_in[2];
end
end
// Mapper #184
if (ENABLE_MAPPER_184 && (mapper == 6'b011111))
begin
if (cpu_addr_in[14:13] == 2'b11)
begin
chr_bank_a[4:2] <= cpu_data_in[2:0];
chr_bank_e[4:2] <= {1'b1, cpu_data_in[5:4]};
end
end
// Mapper #38
if (ENABLE_MAPPER_038 && (mapper == 6'b100000))
begin
if (cpu_addr_in[14:12] == 3'b111)
begin
prg_bank_a[3:2] <= cpu_data_in[1:0];
chr_bank_a[4:3] <= cpu_data_in[3:2];
end
end
end else begin // $8000-$FFFF
// Mapper #2 - UxROM
// flag0 - mapper #71 - for Fire Hawk only.
// other mapper-#71 games are UxROM
if (mapper == 6'b000001)
begin
if (!ENABLE_MAPPER_071 | ~flags[0] | (cpu_addr_in[14:12] != 3'b001))
begin
prg_bank_a[UxROM_BITSIZE+1:1] <= cpu_data_in[UxROM_BITSIZE:0];
// Mapper #30 - UNROM 512
if (ENABLE_MAPPER_030 && flags[1])
begin
// One screen mirroring select, CHR RAM bank
mirroring[1:0] <= {1'b1, cpu_data_in[7]};
chr_bank_a[1:0] <= cpu_data_in[6:5];
end
end else begin // CodeMasters, blah. Mirroring control used only by Fire Hawk
mirroring[1:0] <= {1'b1, cpu_data_in[4]};
end
end
// Mapper #3 - CNROM
if (mapper == 6'b000010)
begin
chr_bank_a[7:3] <= cpu_data_in[4:0];
end
// Mapper #78 - Holy Diver
if (ENABLE_MAPPER_078 && (mapper == 6'b000011))
begin
prg_bank_a[3:1] <= cpu_data_in[2:0];
chr_bank_a[6:3] <= cpu_data_in[7:4];
mirroring <= {1'b0, ~cpu_data_in[3]};
end
// Mapper #97 - Irem's TAM-S1
if (ENABLE_MAPPER_097 && (mapper == 6'b000100))
begin
prg_bank_a[5:1] <= cpu_data_in[4:0];
mirroring <= {1'b0, ~cpu_data_in[7]};
end
// Mapper #93 - Sunsoft-2
if (ENABLE_MAPPER_093 && (mapper == 6'b000101))
begin
prg_bank_a[3:1] <= {cpu_data_in[6:4]};
chr_write_enabled <= cpu_data_in[0];
end
// Mapper #18 - Jaleco SS 88006
if (ENABLE_MAPPER_018 && (mapper == 6'b000111))
begin
case ({cpu_addr_in[14:12], cpu_addr_in[1:0]})
5'b00000: prg_bank_a[3:0] <= cpu_data_in[3:0]; // $8000
5'b00001: prg_bank_a[7:4] <= cpu_data_in[3:0]; // $8001
5'b00010: prg_bank_b[3:0] <= cpu_data_in[3:0]; // $8002
5'b00011: prg_bank_b[7:4] <= cpu_data_in[3:0]; // $8003
5'b00100: prg_bank_c[3:0] <= cpu_data_in[3:0]; // $9000
5'b00101: prg_bank_c[7:4] <= cpu_data_in[3:0]; // $9001
5'b00110: ; // $9002
5'b00111: ; // $9003
5'b01000: chr_bank_a[3:0] <= cpu_data_in[3:0]; // $A000
5'b01001: chr_bank_a[7:4] <= cpu_data_in[3:0]; // $A001
5'b01010: chr_bank_b[3:0] <= cpu_data_in[3:0]; // $A002
5'b01011: chr_bank_b[7:4] <= cpu_data_in[3:0]; // $A003
5'b01100: chr_bank_c[3:0] <= cpu_data_in[3:0]; // $B000
5'b01101: chr_bank_c[7:4] <= cpu_data_in[3:0]; // $B001
5'b01110: chr_bank_d[3:0] <= cpu_data_in[3:0]; // $B002
5'b01111: chr_bank_d[7:4] <= cpu_data_in[3:0]; // $B003
5'b10000: chr_bank_e[3:0] <= cpu_data_in[3:0]; // $C000
5'b10001: chr_bank_e[7:4] <= cpu_data_in[3:0]; // $C001
5'b10010: chr_bank_f[3:0] <= cpu_data_in[3:0]; // $C002
5'b10011: chr_bank_f[7:4] <= cpu_data_in[3:0]; // $C003
5'b10100: chr_bank_g[3:0] <= cpu_data_in[3:0]; // $D000
5'b10101: chr_bank_g[7:4] <= cpu_data_in[3:0]; // $D001
5'b10110: chr_bank_h[3:0] <= cpu_data_in[3:0]; // $D002
5'b10111: chr_bank_h[7:4] <= cpu_data_in[3:0]; // $D003
5'b11000: mapper18_irq_latch[3:0] <= cpu_data_in[3:0]; // $E000
5'b11001: mapper18_irq_latch[7:4] <= cpu_data_in[3:0]; // $E001
5'b11010: mapper18_irq_latch[11:8] <= cpu_data_in[3:0]; // $E002
5'b11011: mapper18_irq_latch[15:12] <= cpu_data_in[3:0]; // $E003
5'b11100: begin // $F000
mapper18_irq_value[15:0] <= mapper18_irq_latch[15:0];
mapper18_irq_out <= 0; // ack
end
5'b11101: begin // $F001
mapper18_irq_control[3:0] <= cpu_data_in[3:0];
mapper18_irq_out <= 0;
end
5'b11110:
case (cpu_data_in[1:0])
2'b00: mirroring <= 2'b01; // Horz
2'b01: mirroring <= 2'b00; // Vert
2'b10: mirroring <= 2'b10; // 1SsA
2'b11: mirroring <= 2'b11; // 1SsB
endcase
5'b11111: ; // $F003 - sound
endcase
end
// Mapper #7 - AxROM
if (mapper == 6'b001000)
begin
prg_bank_a[AxROM_BxROM_BITSIZE+2:2] <= cpu_data_in[AxROM_BxROM_BITSIZE:0];
if (!ENABLE_MAPPER_034_BNROM || !flags[0]) // BNROM?
mirroring <= {1'b1, cpu_data_in[4]};
end
// Mapper #228 - Cheetahmen II
if (ENABLE_MAPPER_228 && (mapper == 6'b001001))
begin
prg_bank_a[5:2] <= cpu_addr_in[10:7];
chr_bank_a[7:3] <= {cpu_addr_in[2:0], cpu_data_in[1:0]}; // only 256k, sorry
mirroring <= {1'b0, cpu_addr_in[13]};
end
// Mapper #11 - ColorDreams
if (ENABLE_MAPPER_011 && (mapper == 6'b001010))
begin
prg_bank_a[3:2] <= cpu_data_in[1:0];
chr_bank_a[6:3] <= cpu_data_in[7:4];
end
// Mapper #66 - GxROM
if (ENABLE_MAPPER_066 && (mapper == 6'b001011))
begin
prg_bank_a[3:2] <= cpu_data_in[5:4];
chr_bank_a[4:3] <= cpu_data_in[1:0];
end
// Mapper #90 - JY
if (ENABLE_MAPPER_090 && (mapper == 6'b001101))
begin
if (cpu_addr_in[14:12] == 3'b000) // $800x
begin
case (cpu_addr_in[1:0])
2'b00: prg_bank_a[5:0] <= cpu_data_in[5:0];
2'b01: prg_bank_b[5:0] <= cpu_data_in[5:0];
2'b10: prg_bank_c[5:0] <= cpu_data_in[5:0];
2'b11: prg_bank_d[5:0] <= cpu_data_in[5:0];
endcase
end
if (cpu_addr_in[14:12] == 3'b001) // $900x
begin
case (cpu_addr_in[2:0])
3'b000: chr_bank_a[7:0] <= cpu_data_in[7:0]; // $9000
3'b001: chr_bank_b[7:0] <= cpu_data_in[7:0]; // $9001
3'b010: chr_bank_c[7:0] <= cpu_data_in[7:0]; // $9002
3'b011: chr_bank_d[7:0] <= cpu_data_in[7:0]; // $9003
3'b100: chr_bank_e[7:0] <= cpu_data_in[7:0]; // $9004
3'b101: chr_bank_f[7:0] <= cpu_data_in[7:0]; // $9005
3'b110: chr_bank_g[7:0] <= cpu_data_in[7:0]; // $9006
3'b111: chr_bank_h[7:0] <= cpu_data_in[7:0]; // $9007
endcase
end
if ({cpu_addr_in[14:12], cpu_addr_in[1:0]} == 5'b10101) // $D001
mirroring <= cpu_data_in[1:0];
if (ENABLE_MAPPER_090_ACCURATE_IRQ)
begin
if (cpu_addr_in[14:12] == 3'b100) // $C00x
begin
case (cpu_addr_in[2:0])
3'b000: mapper90_irq_enabled <= cpu_data_in[0];
3'b001: ;
3'b010: mapper90_irq_enabled <= 0;
3'b011: mapper90_irq_enabled <= 1;
3'b100: begin
mapper90_irq_latch[2:0] <= cpu_data_in[2:0] ^ mapper90_xor[2:0];
mapper90_irq_reload <= 1;
end
3'b101: begin
mapper90_irq_latch[10:3] <= cpu_data_in ^ mapper90_xor;
mapper90_irq_reload <= 1;
end
3'b110: mapper90_xor <= cpu_data_in;
3'b111: ;
endcase
end
end else begin
// use MMC3's IRQs
if (cpu_addr_in[14:12] == 3'b100) // $C00x
begin
case (cpu_addr_in[2:0])
3'b000: mmc3_irq_enabled <= cpu_data_in[0];
3'b001: ;
3'b010: mmc3_irq_enabled <= 0;
3'b011: mmc3_irq_enabled <= 1;
3'b100: ;
3'b101: begin
mmc3_irq_latch <= cpu_data_in ^ mapper90_xor;
mmc3_irq_reload <= 1;
end
3'b110: mapper90_xor = cpu_data_in;
3'b111: ;
endcase
end
end
end
// Mapper #65 - Irem's H3001
if (ENABLE_MAPPER_065 && (mapper == 6'b001110))
begin
case ({cpu_addr_in[14:12], cpu_addr_in[2:0]})
6'b000000: prg_bank_a[5:0] <= cpu_data_in[5:0]; // $8000
6'b001001: mirroring <= {1'b0, cpu_data_in[7]}; // $9001, mirroring
6'b001011: begin
mapper65_irq_out <= 0; // ack
mapper65_irq_enabled <= cpu_data_in[7]; // $9003, enable IRQ
end
6'b001100: begin
mapper65_irq_out <= 0; // ack
mapper65_irq_value <= mapper65_irq_latch; // $9004, IRQ reload
end
6'b001101: mapper65_irq_latch[15:8] <= cpu_data_in; // $9005, IRQ high value
6'b001110: mapper65_irq_latch[7:0] <= cpu_data_in; // $9006, IRQ low value
6'b010000: prg_bank_b[5:0] <= cpu_data_in[5:0]; // $A000
6'b011000: chr_bank_a[7:0] <= cpu_data_in[7:0]; // $B000
6'b011001: chr_bank_b[7:0] <= cpu_data_in[7:0]; // $B001
6'b011010: chr_bank_c[7:0] <= cpu_data_in[7:0]; // $B002
6'b011011: chr_bank_d[7:0] <= cpu_data_in[7:0]; // $B003
6'b011100: chr_bank_e[7:0] <= cpu_data_in[7:0]; // $B004
6'b011101: chr_bank_f[7:0] <= cpu_data_in[7:0]; // $B005
6'b011110: chr_bank_g[7:0] <= cpu_data_in[7:0]; // $B006
6'b011111: chr_bank_h[7:0] <= cpu_data_in[7:0]; // $B007
6'b100000: prg_bank_c[5:0] <= cpu_data_in[5:0]; // $C000
endcase
end
// Mapper #1 - MMC1
/*
flag0 - 16KB of PRG RAM (SOROM)
*/
if (mapper == 6'b010000)
begin
if (cpu_data_in[7] == 1) // reset
begin
mmc1_load_register[5:0] = 6'b100000;
prg_mode <= 3'b000; // 0x4000 (A) + fixed last (C)
prg_bank_c[4:0] <= 5'b11110;
end else begin
mmc1_load_register[5:0] = {cpu_data_in[0], mmc1_load_register[5:1]};
if (mmc1_load_register[0] == 1)
begin
case (cpu_addr_in[14:13])
2'b00: begin // $8000-$9FFF
if (mmc1_load_register[4:3] == 2'b11)
begin
prg_mode = 3'b000; // 0x4000 (A) + fixed last (C)
prg_bank_c[4:1] = 4'b1111;
end else if (mmc1_load_register[4:3] == 2'b10)
begin
prg_mode = 3'b001; // fixed first (C) + 0x4000 (A)
prg_bank_c[4:1] = 4'b0000;
end else
prg_mode = 3'b111; // 0x8000 (A)
if (mmc1_load_register[5])
chr_mode = 3'b100;
else
chr_mode = 3'b000;
mirroring[1:0] = mmc1_load_register[2:1] ^ 2'b10;
end
2'b01: begin // $A000-$BFFF
chr_bank_a[6:2] = mmc1_load_register[5:1];
if (flags[0]) // 16KB of PRG RAM
begin
sram_page = {1'b1, ~mmc1_load_register[4]}; // PRG RAM page #2 is "battery" backed
end
prg_bank_a[5] = mmc1_load_register[5]; // for SUROM, 512k PRG support
prg_bank_c[5] = mmc1_load_register[5]; // for SUROM, 512k PRG support
end
2'b10: chr_bank_e[6:2] = mmc1_load_register[5:1]; // $C000-$DFFF
2'b11: begin
prg_bank_a[4:1] = mmc1_load_register[4:1]; // $E000-$FFFF
sram_enabled = ~mmc1_load_register[5];
end
endcase
mmc1_load_register[5:0] = 6'b100000;
end
end
end
// Mapper #9 and #10 - MMC2 and MMC4
// flag0 - 0=MMC2, 1=MMC4
if (ENABLE_MAPPER_009_010 && (mapper == 6'b010001))
begin
case (cpu_addr_in[14:12])
3'b010: if (~flags[0]) // $A000-$AFFF
prg_bank_a[3:0] <= cpu_data_in[3:0];
else
prg_bank_a[4:1] <= cpu_data_in[3:0];
3'b011: chr_bank_a[6:2] <= cpu_data_in[4:0]; // $B000-$BFFF
3'b100: chr_bank_b[6:2] <= cpu_data_in[4:0]; // $C000-$CFFF
3'b101: chr_bank_e[6:2] <= cpu_data_in[4:0]; // $D000-$DFFF
3'b110: chr_bank_f[6:2] <= cpu_data_in[4:0]; // $E000-$EFFF
3'b111: mirroring <= {1'b0, cpu_data_in[0]}; // $F000-$FFFF
endcase
end
// Mapper #70
// Mapper #152
if (ENABLE_MAPPER_070_152 && (mapper == 6'b010010))
begin
chr_bank_a[6:3] <= cpu_data_in[3:0];
prg_bank_a[3:1] <= cpu_data_in[6:4];
if (flags[0])
mirroring <= {1'b1, cpu_data_in[7]}; // Mapper #152
else
prg_bank_a[4] <= cpu_data_in[7]; // Mapper #70
end
// Mapper #73 - VRC3
if (ENABLE_MAPPER_073 && (mapper == 6'b010011))
begin
case (cpu_addr_in[14:12])
3'b000: vrc3_irq_latch[3:0] <= cpu_data_in[3:0]; // $8000-$8FFF
3'b001: vrc3_irq_latch[7:4] <= cpu_data_in[3:0]; // $9000-$9FFF
3'b010: vrc3_irq_latch[11:8] <= cpu_data_in[3:0]; // $A000-$AFFF
3'b011: vrc3_irq_latch[15:12] <= cpu_data_in[3:0]; // $B000-$BFFF
3'b100: begin // $C000-$CFFF
vrc3_irq_out <= 0; // ack
vrc3_irq_control[2:0] <= cpu_data_in[2:0]; // mode, enabled, enabled after ack
if (vrc3_irq_control[1]) // if E is set
vrc3_irq_value[15:0] <= vrc3_irq_latch[15:0]; // reload with latch
end