-
Notifications
You must be signed in to change notification settings - Fork 1
/
mappers.c
1485 lines (1291 loc) · 36.5 KB
/
mappers.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
/*
* mappers.c
*
* mapper emulation routines
*/
/* $Id: mappers.c,v 1.98 2000/12/14 21:47:07 nyef Exp $ */
#include <stdlib.h>
#include <stdio.h>
#include "mappers.h"
#include "ui.h"
#include "tool.h"
#include "nes_ppu.h"
#include "nes.h"
#include "cal.h"
/* defines for mapper interfaces */
typedef nes_mapper (* mapinit_t)(nes_ppu ppu, nes_rom romfile);
nes_mapper map0_init(nes_ppu ppu, nes_rom romfile);
nes_mapper map1_init(nes_ppu ppu, nes_rom romfile);
nes_mapper map2_init(nes_ppu ppu, nes_rom romfile);
nes_mapper map3_init(nes_ppu ppu, nes_rom romfile);
nes_mapper map4_init(nes_ppu ppu, nes_rom romfile);
nes_mapper map7_init(nes_ppu ppu, nes_rom romfile);
nes_mapper map9_init(nes_ppu ppu, nes_rom romfile);
nes_mapper mapA_init(nes_ppu ppu, nes_rom romfile);
nes_mapper mapB_init(nes_ppu ppu, nes_rom romfile);
nes_mapper mapF_init(nes_ppu ppu, nes_rom romfile);
nes_mapper map14_init(nes_ppu ppu, nes_rom romfile);
nes_mapper map17_init(nes_ppu ppu, nes_rom romfile);
nes_mapper map18_init(nes_ppu ppu, nes_rom romfile);
nes_mapper map20_init(nes_ppu ppu, nes_rom romfile);
nes_mapper map42_init(nes_ppu ppu, nes_rom romfile);
nes_mapper map44_init(nes_ppu ppu, nes_rom romfile);
nes_mapper map4E_init(nes_ppu ppu, nes_rom romfile);
struct mapper_support {
int mapper;
const char *name;
mapinit_t create;
int support; /* mapper support. 0: none. 1: partial. 2: full. */
};
struct mapper_support mappers[] = {
{0, "None", &map0_init, 2},
{1, "MMC1", &map1_init, 2},
{2, "UNROM", &map2_init, 2},
{3, "CNROM", &map3_init, 2},
{4, "MMC3", &map4_init, 1},
{7, "AOROM", &map7_init, 2},
{9, "MMC2", &map9_init, 2},
{10, "MMC4", &mapA_init, 2},
{11, "Color Dreams", &mapB_init, 2},
#if 0
{15, "100-in-1", &mapF_init, 2},
#endif
{20, "Famicom DiskSystem", &map14_init, 1},
{23, "Konami VRC2 type B", &map17_init, 1},
{24, "Konami VRC6", &map18_init, 1},
#if 0
{32, "Irem G-101", &map20_init, 1},
#endif
{66, "GNROM", &map42_init, 1},
{68, "Sunsoft Mapper 4", &map44_init, 2},
{78, "Irem 74HC161/32", &map4E_init, 1},
{-1, NULL, NULL, 0},
};
nes_mapper create_mapper(nes_ppu ppu, nes_rom romfile)
{
int i;
for (i = 0; mappers[i].mapper != -1; i++) {
if ((mappers[i].mapper == romfile->mapper) && (mappers[i].support)) {
deb_printf("mappers: initializing%s completed mapper %d (%s).\n",
(mappers[i].support == 1)? " partially": "",
romfile->mapper, mappers[i].name);
return mappers[i].create(ppu, romfile);
}
}
return NULL;
}
int mapper_supported(int mapper)
{
int i;
for (i = 0; mappers[i].mapper != -1; i++) {
if (mappers[i].mapper == mapper) {
return mappers[i].support;
}
}
return 0;
}
/* dummy routines for when a mapper doesn't need it. */
void dummy_hsync(nes_mapper mapper, cal_cpu cpu, int display_active)
{
}
/* MAPPER 0 Implementation */
void map0_write(nes_mapper mapper, unsigned short address, unsigned char value)
{
}
nes_mapper map0_init(nes_ppu ppu, nes_rom romfile)
{
nes_mapper retval;
retval = malloc(sizeof(struct nes_mapper));
if (retval) {
retval->write = map0_write;
retval->hsync = dummy_hsync;
ppu_latchfunc = NULL;
nesppu_map_8k(ppu, 0, 0);
nesprg_map_16k(0, 0);
nesprg_map_16k(1, -1);
}
return retval;
}
/* MAPPER 1 Implementation */
struct mapper_1 {
struct nes_mapper interface;
nes_ppu ppu;
nes_rom romfile;
int sequence;
int accumulator;
int data[4];
};
void map1_write(struct mapper_1 *mapper, unsigned short address, unsigned char value)
{
int bank_select;
if (value & 0x80) {
mapper->data[0] |= 0x0c;
mapper->accumulator = mapper->data[(address >> 13) & 3];
mapper->sequence = 5;
} else {
mapper->accumulator |= ((value & 1) << (mapper->sequence));
mapper->sequence++;
}
if ((mapper->sequence) == 5) {
mapper->data[(address >> 13) & 3] = mapper->accumulator;
mapper->sequence = 0;
mapper->accumulator = 0;
switch (mapper->data[0] & 3) {
case 0:
PPU_mirror_one_low();
break;
case 1:
PPU_mirror_one_high();
break;
case 2:
PPU_mirror_vertical();
break;
case 3:
PPU_mirror_horizontal();
break;
}
if (mapper->romfile->prg_size == 0x20) { /* 512k cart */
bank_select = mapper->data[1] & 0x10;
} else { /* other carts */
bank_select = 0;
}
if (!(mapper->data[0] & 8)) {
nesprg_map_32k(0, (mapper->data[3] & 15) + (bank_select >> 1));
} else if (mapper->data[0] & 4) {
nesprg_map_16k(0, (mapper->data[3] & 15) + bank_select);
nesprg_map_16k(1, -1);
} else {
nesprg_map_16k(0, 0);
nesprg_map_16k(1, (mapper->data[3] & 15) + bank_select);
}
if (mapper->data[0] & 0x10) {
nesppu_map_4k(mapper->ppu, 0, mapper->data[1]);
nesppu_map_4k(mapper->ppu, 1, mapper->data[2]);
} else {
nesppu_map_8k(mapper->ppu, 0, mapper->data[1] >> 1);
}
}
}
nes_mapper map1_init(nes_ppu ppu, nes_rom romfile)
{
struct mapper_1 *retval;
retval = malloc(sizeof(struct mapper_1));
if (retval) {
retval->interface.write = (mapwrite_t)map1_write;
retval->interface.hsync = dummy_hsync;
retval->ppu = ppu;
retval->romfile = romfile;
ppu_latchfunc = NULL;
retval->data[0] = 0x1f; /* FIXME: may be incorrect */
retval->data[3] = 0x00;
/* FIXME: may want to set consistent internal state */
/* NOTE: does nothing when not using CHR-ROM */
nesppu_map_8k(ppu, 0, 0);
nesprg_map_16k(0, 0);
nesprg_map_16k(1, -1);
}
return (nes_mapper)retval;
}
/* MAPPER 2 Implementation */
void map2_write(nes_mapper mapper, unsigned short address, unsigned char value)
{
nesprg_map_16k(0, value);
}
nes_mapper map2_init(nes_ppu ppu, nes_rom romfile)
{
nes_mapper retval;
retval = malloc(sizeof(struct nes_mapper));
if (retval) {
retval->write = map2_write;
retval->hsync = dummy_hsync;
/* NOTE: does nothing when not using CHR-ROM */
nesppu_map_8k(ppu, 0, 0);
ppu_latchfunc = NULL;
nesprg_map_16k(0, 0);
nesprg_map_16k(1, -1);
}
return retval;
}
/* MAPPER 3 Implementation */
struct mapper_3 {
struct nes_mapper interface;
nes_ppu ppu;
};
void map3_write(struct mapper_3 *mapper, unsigned short address, unsigned char value)
{
nesppu_map_8k(mapper->ppu, 0, value);
}
nes_mapper map3_init(nes_ppu ppu, nes_rom romfile)
{
struct mapper_3 *retval;
retval = malloc(sizeof(struct mapper_3));
if (retval) {
retval->interface.write = (mapwrite_t)map3_write;
retval->interface.hsync = dummy_hsync;
retval->ppu = ppu;
ppu_latchfunc = NULL;
map3_write(retval, 0, 0);
nesprg_map_16k(0, 0);
nesprg_map_16k(1, -1);
}
return (nes_mapper)retval;
}
/* MAPPER 4 Implementation */
struct mapper_4 {
struct nes_mapper interface;
nes_ppu ppu;
nes_rom romfile;
unsigned char command;
unsigned char irq_count;
unsigned char irq_latch;
unsigned char irq_state;
unsigned char chr_switch[6];
unsigned char prg_switch[2];
};
void map4_sync_prg(struct mapper_4 *mapper)
{
if (mapper->command & 0x40) {
nesprg_map_8k(0, 0xfe);
nesprg_map_8k(1, mapper->prg_switch[1]);
nesprg_map_8k(2, mapper->prg_switch[0]);
nesprg_map_8k(3, 0xff);
} else {
nesprg_map_8k(0, mapper->prg_switch[0]);
nesprg_map_8k(1, mapper->prg_switch[1]);
nesprg_map_16k(1, -1);
}
}
void map4_sync_chr(struct mapper_4 *mapper)
{
if (mapper->command & 0x80) {
nesppu_map_1k(mapper->ppu, 0, mapper->chr_switch[2]);
nesppu_map_1k(mapper->ppu, 1, mapper->chr_switch[3]);
nesppu_map_1k(mapper->ppu, 2, mapper->chr_switch[4]);
nesppu_map_1k(mapper->ppu, 3, mapper->chr_switch[5]);
nesppu_map_1k(mapper->ppu, 4, mapper->chr_switch[0]);
nesppu_map_1k(mapper->ppu, 5, mapper->chr_switch[0] + 1);
nesppu_map_1k(mapper->ppu, 6, mapper->chr_switch[1]);
nesppu_map_1k(mapper->ppu, 7, mapper->chr_switch[1] + 1);
} else {
nesppu_map_1k(mapper->ppu, 0, mapper->chr_switch[0]);
nesppu_map_1k(mapper->ppu, 1, mapper->chr_switch[0] + 1);
nesppu_map_1k(mapper->ppu, 2, mapper->chr_switch[1]);
nesppu_map_1k(mapper->ppu, 3, mapper->chr_switch[1] + 1);
nesppu_map_1k(mapper->ppu, 4, mapper->chr_switch[2]);
nesppu_map_1k(mapper->ppu, 5, mapper->chr_switch[3]);
nesppu_map_1k(mapper->ppu, 6, mapper->chr_switch[4]);
nesppu_map_1k(mapper->ppu, 7, mapper->chr_switch[5]);
}
}
void map4_write(struct mapper_4 *mapper, unsigned short address, unsigned char value)
{
if (address == 0x8000) {
mapper->command = value;
map4_sync_chr(mapper);
map4_sync_prg(mapper);
} else if (address == 0x8001) {
switch (mapper->command & 7) {
case 0:
mapper->chr_switch[0] = value;
break;
case 1:
mapper->chr_switch[1] = value;
break;
case 2:
mapper->chr_switch[2] = value;
break;
case 3:
mapper->chr_switch[3] = value;
break;
case 4:
mapper->chr_switch[4] = value;
break;
case 5:
mapper->chr_switch[5] = value;
break;
case 6:
mapper->prg_switch[0] = value;
break;
case 7:
mapper->prg_switch[1] = value;
break;
}
map4_sync_chr(mapper);
map4_sync_prg(mapper);
} else if (address == 0xa000) {
if (value & 0x40) {
PPU_mirror_one_high();
} else {
if (value & 0x01) {
PPU_mirror_horizontal();
} else {
PPU_mirror_vertical();
}
}
} else if (address == 0xc000) {
mapper->irq_count = value;
} else if (address == 0xc001) {
mapper->irq_latch = value;
} else if (address == 0xe000) {
mapper->irq_state = 0;
} else if (address == 0xe001) {
mapper->irq_state = 1;
} else {
deb_printf("map4_write: unknown address. (0x%04hx, 0x%02x)\n", address, value);
}
}
void map4_hsync(struct mapper_4 *mapper, cal_cpu cpu, int display_active)
{
if (display_active && mapper->irq_state && (mapper->irq_count-- == 0)) {
mapper->irq_count = mapper->irq_latch;
cpu->irq(cpu, 0);
}
}
nes_mapper map4_init(nes_ppu ppu, nes_rom romfile)
{
struct mapper_4 *retval;
retval = malloc(sizeof(struct mapper_4));
if (retval) {
retval->interface.write = (mapwrite_t)map4_write;
retval->interface.hsync = (maphsync_t)map4_hsync;
retval->ppu = ppu;
retval->romfile = romfile;
ppu_latchfunc = NULL;
retval->command = 0;
retval->prg_switch[0] = 0;
retval->prg_switch[1] = 1;
map4_sync_prg(retval);
retval->chr_switch[0] = 0;
retval->chr_switch[1] = 2;
retval->chr_switch[2] = 4;
retval->chr_switch[3] = 5;
retval->chr_switch[4] = 6;
retval->chr_switch[5] = 7;
map4_sync_chr(retval);
}
return (nes_mapper)retval;
}
/* MAPPER 7 Implementation */
struct mapper_7 {
struct nes_mapper interface;
nes_ppu ppu;
};
void map7_write(struct mapper_7 *mapper, unsigned short address, unsigned char value)
{
if (value & 0x10) {
PPU_mirror_one_high();
} else {
PPU_mirror_one_low();
}
nesprg_map_32k(0, value & 0x0f);
}
nes_mapper map7_init(nes_ppu ppu, nes_rom romfile)
{
struct mapper_7 *retval;
retval = malloc(sizeof(struct mapper_7));
if (retval) {
retval->interface.write = (mapwrite_t)map7_write;
retval->interface.hsync = dummy_hsync;
retval->ppu = ppu;
ppu_latchfunc = NULL;
nesprg_map_32k(0, 0);
}
return (nes_mapper)retval;
}
/* MAPPER 9 Implementation */
struct map9_latch {
unsigned char lo_bank;
unsigned char hi_bank;
unsigned char state;
};
struct mapper_9 {
struct nes_mapper interface;
nes_ppu ppu;
struct map9_latch latch1;
struct map9_latch latch2;
};
void map9_latchfunc(struct mapper_9 *mapper, unsigned short address)
{
if ((address & 0x3ff0) == 0x0fd0) {
mapper->latch1.state = 0;
nesppu_map_4k(mapper->ppu, 0, mapper->latch1.lo_bank);
} else if ((address & 0x3ff0) == 0x0fe0) {
mapper->latch1.state = 1;
nesppu_map_4k(mapper->ppu, 0, mapper->latch1.hi_bank);
} else if ((address & 0x3ff0) == 0x1fd0) {
mapper->latch2.state = 0;
nesppu_map_4k(mapper->ppu, 1, mapper->latch2.lo_bank);
} else if ((address & 0x3ff0) == 0x1fe0) {
mapper->latch2.state = 1;
nesppu_map_4k(mapper->ppu, 1, mapper->latch2.hi_bank);
}
}
void map9_write(struct mapper_9 *mapper, unsigned short address, unsigned char value)
{
if (address < 0xa000) {
/* Ignore it. It's not in spec. */
} else if (address < 0xb000) {
nesprg_map_8k(0, value);
} else if (address < 0xc000) {
mapper->latch1.lo_bank = value;
if (mapper->latch1.state == 0) {
nesppu_map_4k(mapper->ppu, 0, value);
}
} else if (address < 0xd000) {
mapper->latch1.hi_bank = value;
if (mapper->latch1.state == 1) {
nesppu_map_4k(mapper->ppu, 0, value);
}
} else if (address < 0xe000) {
mapper->latch2.lo_bank = value;
if (mapper->latch2.state == 0) {
nesppu_map_4k(mapper->ppu, 1, value);
}
} else if (address < 0xf000) {
mapper->latch2.hi_bank = value;
if (mapper->latch2.state == 1) {
nesppu_map_4k(mapper->ppu, 1, value);
}
} else {
if (value & 1) {
PPU_mirror_horizontal();
} else {
PPU_mirror_vertical();
}
}
}
nes_mapper map9_init(nes_ppu ppu, nes_rom romfile)
{
struct mapper_9 *retval;
retval = malloc(sizeof(struct mapper_9));
if (retval) {
retval->interface.write = (mapwrite_t)map9_write;
retval->interface.hsync = dummy_hsync;
retval->ppu = ppu;
retval->latch1.lo_bank = 0;
retval->latch1.hi_bank = 0;
retval->latch1.state = 0;
retval->latch2.lo_bank = 0;
retval->latch2.hi_bank = 0;
retval->latch2.state = 0;
ppu_latchfunc = (ppulatch_t)map9_latchfunc;
nesprg_map_8k(0, 0);
nesprg_map_8k(1, 0xfd);
nesprg_map_8k(2, 0xfe);
nesprg_map_8k(3, 0xff);
}
return (nes_mapper)retval;
}
/* MAPPER A (10) Implementation */
struct mapA_latch {
unsigned char lo_bank;
unsigned char hi_bank;
unsigned char state;
};
struct mapper_A {
struct nes_mapper interface;
nes_ppu ppu;
struct mapA_latch latch1;
struct mapA_latch latch2;
};
void mapA_latchfunc(struct mapper_A *mapper, unsigned short address)
{
if ((address & 0x3ff0) == 0x0fd0) {
mapper->latch1.state = 0;
nesppu_map_4k(mapper->ppu, 0, mapper->latch1.lo_bank);
} else if ((address & 0x3ff0) == 0x0fe0) {
mapper->latch1.state = 1;
nesppu_map_4k(mapper->ppu, 0, mapper->latch1.hi_bank);
} else if ((address & 0x3ff0) == 0x1fd0) {
mapper->latch2.state = 0;
nesppu_map_4k(mapper->ppu, 1, mapper->latch2.lo_bank);
} else if ((address & 0x3ff0) == 0x1fe0) {
mapper->latch2.state = 1;
nesppu_map_4k(mapper->ppu, 1, mapper->latch2.hi_bank);
}
}
void mapA_write(struct mapper_A *mapper, unsigned short address, unsigned char value)
{
if (address < 0xa000) {
/* Ignore it. It's not in spec. */
} else if (address < 0xb000) {
nesprg_map_16k(0, value);
} else if (address < 0xc000) {
mapper->latch1.lo_bank = value;
if (mapper->latch1.state == 0) {
nesppu_map_4k(mapper->ppu, 0, value);
}
} else if (address < 0xd000) {
mapper->latch1.hi_bank = value;
if (mapper->latch1.state == 1) {
nesppu_map_4k(mapper->ppu, 0, value);
}
} else if (address < 0xe000) {
mapper->latch2.lo_bank = value;
if (mapper->latch2.state == 0) {
nesppu_map_4k(mapper->ppu, 1, value);
}
} else if (address < 0xf000) {
mapper->latch2.hi_bank = value;
if (mapper->latch2.state == 1) {
nesppu_map_4k(mapper->ppu, 1, value);
}
} else {
if (value & 1) {
PPU_mirror_horizontal();
} else {
PPU_mirror_vertical();
}
}
}
nes_mapper mapA_init(nes_ppu ppu, nes_rom romfile)
{
struct mapper_A *retval;
retval = malloc(sizeof(struct mapper_A));
if (retval) {
retval->interface.write = (mapwrite_t)mapA_write;
retval->interface.hsync = dummy_hsync;
retval->ppu = ppu;
retval->latch1.lo_bank = 0;
retval->latch1.hi_bank = 0;
retval->latch1.state = 0;
retval->latch2.lo_bank = 0;
retval->latch2.hi_bank = 0;
retval->latch2.state = 0;
ppu_latchfunc = (ppulatch_t)mapA_latchfunc;
nesprg_map_16k(0, 0);
nesprg_map_16k(1, -1);
}
return (nes_mapper)retval;
}
/* MAPPER B (11) Implementation */
struct mapper_B {
struct nes_mapper interface;
nes_ppu ppu;
};
void mapB_write(struct mapper_B *mapper, unsigned short address, unsigned char value)
{
unsigned char vromptr;
unsigned char romptr;
vromptr = (value >> 4) & 0x0f;
romptr = value & 0x0f;
nesppu_map_8k(mapper->ppu, 0, vromptr);
nesprg_map_32k(0, romptr);
}
nes_mapper mapB_init(nes_ppu ppu, nes_rom romfile)
{
struct mapper_B *retval;
retval = malloc(sizeof(struct mapper_B));
if (retval) {
retval->interface.write = (mapwrite_t)mapB_write;
retval->interface.hsync = dummy_hsync;
retval->ppu = ppu;
ppu_latchfunc = NULL;
mapB_write(retval, 0, 0);
}
return (nes_mapper)retval;
}
#if 0
/* MAPPER F (15) Implementation */
void mapF_write(nes_mapper mapper, unsigned short address, unsigned char value)
{
int bank = (value << 1);
if (address == 0x8000) {
bank8_reg = bank;
bankA_reg = bank + 1;
bankC_reg = bank + 2;
bankE_reg = bank + 3;
sync_banks();
if (value & 64) {
PPU_mirror_horizontal();
} else {
PPU_mirror_vertical();
}
} else if (address == 0x8001) {
bankC_reg = bank;
bankE_reg = bank + 1;
sync_banks();
} else if (address == 0x8002) {
bank8_reg = bank + ((value & 0x80)? 1 : 0);
bankA_reg = bank8_reg;
bankC_reg = bank8_reg;
bankE_reg = bank8_reg;
sync_banks();
} else if (address == 0x8003) {
bankC_reg = bank;
bankE_reg = bank + 1;
sync_banks();
if (value & 64) {
PPU_mirror_horizontal();
} else {
PPU_mirror_vertical();
}
} else {
/* FIXME: what about other addresses? */
}
}
nes_mapper mapF_init(nes_ppu ppu)
{
nes_mapper retval;
retval = malloc(sizeof(struct nes_mapper));
if (retval) {
retval->write = mapF_write;
retval->hsync = dummy_hsync;
ppu_latchfunc = NULL;
mapF_write(retval, 0, 0);
bank8_reg = 0;
bankA_reg = 1;
bankC_reg = 2;
bankE_reg = 3;
sync_banks();
}
return retval;
}
#endif
/* MAPPER 17 (23) Implementation */
struct mapper_17 {
struct nes_mapper interface;
nes_ppu ppu;
unsigned char bank_regs[16];
};
void map17_sync_vrom(struct mapper_17 *mapper, int bank)
{
unsigned char value;
value = mapper->bank_regs[bank];
value |= mapper->bank_regs[bank + 1] << 4;
nesppu_map_1k(mapper->ppu, bank >> 1, value);
}
void map17_write(struct mapper_17 *mapper, unsigned short address, unsigned char value)
{
if (address == 0x8000) {
nesprg_map_8k(0, value);
} else if (address == 0x9000) {
switch (value & 3) {
case 0:
PPU_mirror_vertical();
break;
case 1:
PPU_mirror_horizontal();
break;
case 2:
PPU_mirror_one_low();
break;
case 3:
PPU_mirror_one_high();
break;
}
} else if (address == 0xa000) {
nesprg_map_8k(1, value);
} else if (((address & 0x8ffc) == 0x8000)
&& (address >= 0xb000)
&& (address < 0xf000)) {
u16 base;
base = (address >> 10) - 0x2c;
mapper->bank_regs[base + (address & 3)] = value & 15;
map17_sync_vrom(mapper, base + (address & 2));
} else {
deb_printf("map23: unknown addr 0x%04hx = 0x%02x.\n", address, value);
}
}
nes_mapper map17_init(nes_ppu ppu, nes_rom romfile)
{
struct mapper_17 *retval;
retval = malloc(sizeof(struct mapper_17));
if (retval) {
retval->interface.write = (mapwrite_t)map17_write;
retval->interface.hsync = dummy_hsync;
retval->ppu = ppu;
ppu_latchfunc = NULL;
#if 0
nesppu_map_8k(ppu, 0, 0);
#endif
nesprg_map_16k(0, 0);
nesprg_map_16k(1, -1);
}
return (nes_mapper)retval;
}
/* MAPPER 18 (24) Implementation */
struct mapper_18 {
struct nes_mapper interface;
nes_ppu ppu;
unsigned char irq_latch;
unsigned char irq_count;
unsigned char irq_state;
};
void map18_write(struct mapper_18 *mapper, unsigned short address, unsigned char value)
{
if (address == 0x8000) {
nesprg_map_16k(0, value);
} else if (address < 0x9000) {
deb_printf("map18_write: unknown write in 0x8001-0x8fff range.\n");
} else if (address == 0xb003) {
switch (value & 0x0c) {
case 0x00:
PPU_mirror_vertical();
break;
case 0x04:
PPU_mirror_horizontal();
break;
case 0x08:
PPU_mirror_one_low();
break;
case 0x0c:
PPU_mirror_one_high();
break;
}
} else if ((address & 0xfffc) == 0xd000) {
nesppu_map_1k(mapper->ppu, address & 3, value);
} else if ((address & 0xfffc) == 0xe000) {
nesppu_map_1k(mapper->ppu, 4 + (address & 3), value);
} else if ((address & 0xfffc) == 0xf000) {
switch (address & 3) {
case 0:
mapper->irq_latch = value;
break;
case 1:
mapper->irq_state = value;
mapper->irq_count = mapper->irq_latch;
break;
case 2:
mapper->irq_state = (mapper->irq_state << 1) | (mapper->irq_state & 1);
break;
case 3:
deb_printf("map18: unknown write 0x%02x to 0xf003.\n", value);
break;
}
}
}
void map18_hsync(struct mapper_18 *mapper, cal_cpu cpu, int display_active)
{
if ((mapper->irq_state & 2) && (++mapper->irq_count == 0)) {
cpu->irq(cpu, 0);
mapper->irq_count = mapper->irq_latch;
}
}
nes_mapper map18_init(nes_ppu ppu, nes_rom romfile)
{
struct mapper_18 *retval;
retval = malloc(sizeof(struct mapper_18));
if (retval) {
retval->interface.write = (mapwrite_t)map18_write;
retval->interface.hsync = (maphsync_t)map18_hsync;
retval->ppu = ppu;
ppu_latchfunc = NULL;
nesprg_map_16k(0, 0);
nesprg_map_16k(1, -1);
}
return (nes_mapper)retval;
}
#if 0 /* doesn't work, not high priority */
/* MAPPER 20 (32) Implementation */
unsigned char map20_saved_9fff;
void map20_write(unsigned short address, unsigned char value)
{
if (address == 0x8fff) {
if (map20_saved_9fff & 0x02) {
bankC_reg = value;
} else {
bank8_reg = value;
}
sync_banks();
} else if (address == 0x9fff) {
map20_saved_9fff = value;
if (value & 1) {
PPU_mirror_horizontal();
} else {
PPU_mirror_vertical();
}
} else if (address == 0xafff) {
bankA_reg = value;
sync_banks();
} else if ((address & 0xfff8) == 0xbff0) {
nesppu_map_1k(mapper->ppu, address & 7, value);
} else {
deb_printf("map32: unknown address 0x%04hx = 0x%02x.\n", address, value);
}
}
void map20_init(void)
{
ppu_latchfunc = NULL;
bank8_reg = 0;
bankA_reg = 0;
bankC_reg = 0xfe;
bankE_reg = 0xff;
sync_banks();
}
mapper_iface_t map20_iface = {map20_init, map20_write, dummy_hsync, dummy_vsync};
#endif
/* MAPPER 42 (66) Implementation */
struct mapper_42 {
struct nes_mapper interface;
nes_ppu ppu;
};
void map42_write(struct mapper_42 *mapper, unsigned short address, unsigned char value)
{
unsigned char vromptr;
unsigned char romptr;
vromptr = value & 0x03;
romptr = (value >> 4) & 0x03;
nesppu_map_8k(mapper->ppu, 0, vromptr);
nesprg_map_32k(0, romptr);
}
nes_mapper map42_init(nes_ppu ppu, nes_rom romfile)
{
struct mapper_42 *retval;