forked from snes9xgit/snes9x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsx.cpp
1211 lines (995 loc) · 25.7 KB
/
bsx.cpp
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
/***********************************************************************************
Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
(c) Copyright 1996 - 2002 Gary Henderson ([email protected]),
Jerremy Koot ([email protected])
(c) Copyright 2002 - 2004 Matthew Kendora
(c) Copyright 2002 - 2005 Peter Bortas ([email protected])
(c) Copyright 2004 - 2005 Joel Yliluoma (http://iki.fi/bisqwit/)
(c) Copyright 2001 - 2006 John Weidman ([email protected])
(c) Copyright 2002 - 2006 funkyass ([email protected]),
Kris Bleakley ([email protected])
(c) Copyright 2002 - 2010 Brad Jorsch ([email protected]),
Nach ([email protected]),
(c) Copyright 2002 - 2011 zones ([email protected])
(c) Copyright 2006 - 2007 nitsuja
(c) Copyright 2009 - 2011 BearOso,
OV2
BS-X C emulator code
(c) Copyright 2005 - 2006 Dreamer Nom,
zones
C4 x86 assembler and some C emulation code
(c) Copyright 2000 - 2003 _Demo_ ([email protected]),
Nach,
zsKnight ([email protected])
C4 C++ code
(c) Copyright 2003 - 2006 Brad Jorsch,
Nach
DSP-1 emulator code
(c) Copyright 1998 - 2006 _Demo_,
Andreas Naive ([email protected]),
Gary Henderson,
Ivar ([email protected]),
John Weidman,
Kris Bleakley,
Matthew Kendora,
Nach,
neviksti ([email protected])
DSP-2 emulator code
(c) Copyright 2003 John Weidman,
Kris Bleakley,
Lord Nightmare ([email protected]),
Matthew Kendora,
neviksti
DSP-3 emulator code
(c) Copyright 2003 - 2006 John Weidman,
Kris Bleakley,
Lancer,
z80 gaiden
DSP-4 emulator code
(c) Copyright 2004 - 2006 Dreamer Nom,
John Weidman,
Kris Bleakley,
Nach,
z80 gaiden
OBC1 emulator code
(c) Copyright 2001 - 2004 zsKnight,
pagefault ([email protected]),
Kris Bleakley
Ported from x86 assembler to C by sanmaiwashi
SPC7110 and RTC C++ emulator code used in 1.39-1.51
(c) Copyright 2002 Matthew Kendora with research by
zsKnight,
John Weidman,
Dark Force
SPC7110 and RTC C++ emulator code used in 1.52+
(c) Copyright 2009 byuu,
neviksti
S-DD1 C emulator code
(c) Copyright 2003 Brad Jorsch with research by
Andreas Naive,
John Weidman
S-RTC C emulator code
(c) Copyright 2001 - 2006 byuu,
John Weidman
ST010 C++ emulator code
(c) Copyright 2003 Feather,
John Weidman,
Kris Bleakley,
Matthew Kendora
Super FX x86 assembler emulator code
(c) Copyright 1998 - 2003 _Demo_,
pagefault,
zsKnight
Super FX C emulator code
(c) Copyright 1997 - 1999 Ivar,
Gary Henderson,
John Weidman
Sound emulator code used in 1.5-1.51
(c) Copyright 1998 - 2003 Brad Martin
(c) Copyright 1998 - 2006 Charles Bilyue'
Sound emulator code used in 1.52+
(c) Copyright 2004 - 2007 Shay Green ([email protected])
SH assembler code partly based on x86 assembler code
(c) Copyright 2002 - 2004 Marcus Comstedt ([email protected])
2xSaI filter
(c) Copyright 1999 - 2001 Derek Liauw Kie Fa
HQ2x, HQ3x, HQ4x filters
(c) Copyright 2003 Maxim Stepin ([email protected])
NTSC filter
(c) Copyright 2006 - 2007 Shay Green
GTK+ GUI code
(c) Copyright 2004 - 2011 BearOso
Win32 GUI code
(c) Copyright 2003 - 2006 blip,
funkyass,
Matthew Kendora,
Nach,
nitsuja
(c) Copyright 2009 - 2011 OV2
Mac OS GUI code
(c) Copyright 1998 - 2001 John Stiles
(c) Copyright 2001 - 2011 zones
Specific ports contains the works of other authors. See headers in
individual files.
Snes9x homepage: http://www.snes9x.com/
Permission to use, copy, modify and/or distribute Snes9x in both binary
and source form, for non-commercial purposes, is hereby granted without
fee, providing that this license information and copyright notice appear
with all copies and any derived work.
This software is provided 'as-is', without any express or implied
warranty. In no event shall the authors be held liable for any damages
arising from the use of this software or it's derivatives.
Snes9x is freeware for PERSONAL USE only. Commercial users should
seek permission of the copyright holders first. Commercial use includes,
but is not limited to, charging money for Snes9x or software derived from
Snes9x, including Snes9x or derivatives in commercial game bundles, and/or
using Snes9x as a promotion for your commercial product.
The copyright holders request that bug fixes and improvements to the code
should be forwarded to them so everyone can benefit from the modifications
in future versions.
Super NES and Super Nintendo Entertainment System are trademarks of
Nintendo Co., Limited and its subsidiary companies.
***********************************************************************************/
// Dreamer Nom wrote:
// Large thanks to John Weidman for all his initial research
// Thanks to Seph3 for his modem notes
#include "snes9x.h"
#include "memmap.h"
#include "display.h"
//#define BSX_DEBUG
#define BIOS_SIZE 0x100000
#define FLASH_SIZE 0x200000
#define PSRAM_SIZE 0x80000
#define Map Memory.Map
#define BlockIsRAM Memory.BlockIsRAM
#define BlockIsROM Memory.BlockIsROM
#define RAM Memory.RAM
#define SRAM Memory.SRAM
#define PSRAM Memory.BSRAM
#define BIOSROM Memory.BIOSROM
#define MAP_BSX Memory.MAP_BSX
#define MAP_CPU Memory.MAP_CPU
#define MAP_PPU Memory.MAP_PPU
#define MAP_NONE Memory.MAP_NONE
#define BSXPPUBASE 0x2180
struct SBSX_RTC
{
int hours;
int minutes;
int seconds;
int ticks;
};
static struct SBSX_RTC BSX_RTC;
// flash card vendor information
static const uint8 flashcard[20] =
{
0x4D, 0x00, 0x50, 0x00, // vendor id
0x00, 0x00, // ?
0x2B, 0x00, // 2MB Flash (1MB = 0x2A)
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
static const uint8 init2192[32] = // FIXME
{
00, 00, 00, 00, 00, // unknown
01, 01, 00, 00, 00,
00, // seconds (?)
00, // minutes
00, // hours
10, 10, 10, 10, 10, // unknown
10, 10, 10, 10, 10, // dummy
00, 00, 00, 00, 00, 00, 00, 00, 00
};
static bool8 FlashMode;
static uint32 FlashSize;
static uint8 *MapROM, *FlashROM;
static void BSX_Map_SNES (void);
static void BSX_Map_LoROM (void);
static void BSX_Map_HiROM (void);
static void BSX_Map_MMC (void);
static void BSX_Map_FlashIO (void);
static void BSX_Map_SRAM (void);
static void BSX_Map_PSRAM (void);
static void BSX_Map_BIOS (void);
static void BSX_Map_RAM (void);
static void BSX_Map_Dirty (void);
static void BSX_Map (void);
static void BSX_Set_Bypass_FlashIO (uint16, uint8);
static uint8 BSX_Get_Bypass_FlashIO (uint16);
static bool8 BSX_LoadBIOS (void);
static void map_psram_mirror_sub (uint32);
static int is_bsx (unsigned char *);
static void BSX_Map_SNES (void)
{
// These maps will be partially overwritten
int c;
// Banks 00->3F and 80->BF
for (c = 0; c < 0x400; c += 16)
{
Map[c + 0] = Map[c + 0x800] = RAM;
Map[c + 1] = Map[c + 0x801] = RAM;
BlockIsRAM[c + 0] = BlockIsRAM[c + 0x800] = TRUE;
BlockIsRAM[c + 1] = BlockIsRAM[c + 0x801] = TRUE;
Map[c + 2] = Map[c + 0x802] = (uint8 *) MAP_PPU;
Map[c + 3] = Map[c + 0x803] = (uint8 *) MAP_PPU;
Map[c + 4] = Map[c + 0x804] = (uint8 *) MAP_CPU;
Map[c + 5] = Map[c + 0x805] = (uint8 *) MAP_CPU;
Map[c + 6] = Map[c + 0x806] = (uint8 *) MAP_NONE;
Map[c + 7] = Map[c + 0x807] = (uint8 *) MAP_NONE;
}
}
static void BSX_Map_LoROM (void)
{
// These maps will be partially overwritten
int i, c;
// Banks 00->3F and 80->BF
for (c = 0; c < 0x400; c += 16)
{
for (i = c + 8; i < c + 16; i++)
{
Map[i] = Map[i + 0x800] = &MapROM[(c << 11) % FlashSize] - 0x8000;
BlockIsRAM[i] = BlockIsRAM[i + 0x800] = BSX.write_enable;
BlockIsROM[i] = BlockIsROM[i + 0x800] = !BSX.write_enable;
}
}
// Banks 40->7F and C0->FF
for (c = 0; c < 0x400; c += 16)
{
for (i = c; i < c + 8; i++)
Map[i + 0x400] = Map[i + 0xC00] = &MapROM[(c << 11) % FlashSize];
for (i = c + 8; i < c + 16; i++)
Map[i + 0x400] = Map[i + 0xC00] = &MapROM[(c << 11) % FlashSize] - 0x8000;
for (i = c; i < c + 16; i++)
{
BlockIsRAM[i + 0x400] = BlockIsRAM[i + 0xC00] = BSX.write_enable;
BlockIsROM[i + 0x400] = BlockIsROM[i + 0xC00] = !BSX.write_enable;
}
}
}
static void BSX_Map_HiROM (void)
{
// These maps will be partially overwritten
int i, c;
// Banks 00->3F and 80->BF
for (c = 0; c < 0x400; c += 16)
{
for (i = c + 8; i < c + 16; i++)
{
Map[i] = Map[i + 0x800] = &MapROM[(c << 12) % FlashSize];
BlockIsRAM[i] = BlockIsRAM[i + 0x800] = BSX.write_enable;
BlockIsROM[i] = BlockIsROM[i + 0x800] = !BSX.write_enable;
}
}
// Banks 40->7F and C0->FF
for (c = 0; c < 0x400; c += 16)
{
for (i = c; i < c + 16; i++)
{
Map[i + 0x400] = Map[i + 0xC00] = &MapROM[(c << 12) % FlashSize];
BlockIsRAM[i + 0x400] = BlockIsRAM[i + 0xC00] = BSX.write_enable;
BlockIsROM[i + 0x400] = BlockIsROM[i + 0xC00] = !BSX.write_enable;
}
}
}
static void BSX_Map_MMC (void)
{
int c;
// Banks 01->0E:5000-5FFF
for (c = 0x010; c < 0x0F0; c += 16)
{
Map[c + 5] = (uint8 *) MAP_BSX;
BlockIsRAM[c + 5] = BlockIsROM[c + 5] = FALSE;
}
}
static void BSX_Map_FlashIO (void)
{
int c;
if (BSX.MMC[0x0C] || BSX.MMC[0x0D])
{
// Bank C0:0000, 2AAA, 5555, FF00-FF1F
for (c = 0; c < 16; c++)
{
Map[c + 0xC00] = (uint8 *) MAP_BSX;
BlockIsRAM[c + 0xC00] = TRUE;
BlockIsROM[c + 0xC00] = FALSE;
}
}
}
static void BSX_Map_SRAM (void)
{
int c;
// Banks 10->17:5000-5FFF
for (c = 0x100; c < 0x180; c += 16)
{
Map[c + 5] = (uint8 *) SRAM + ((c & 0x70) << 8) - 0x5000;
BlockIsRAM[c + 5] = TRUE;
BlockIsROM[c + 5] = FALSE;
}
}
static void map_psram_mirror_sub (uint32 bank)
{
int i, c;
bank <<= 4;
if (BSX.MMC[0x02])
{
for (c = 0; c < 0x100; c += 16)
{
for (i = c; i < c + 16; i++)
{
Map[i + bank] = &PSRAM[(c << 12) % PSRAM_SIZE];
BlockIsRAM[i + bank] = TRUE;
BlockIsROM[i + bank] = FALSE;
}
}
}
else
{
for (c = 0; c < 0x100; c += 16)
{
for (i = c; i < c + 8; i++)
Map[i + bank] = &PSRAM[(c << 11) % PSRAM_SIZE];
for (i = c + 8; i < c + 16; i++)
Map[i + bank] = &PSRAM[(c << 11) % PSRAM_SIZE] - 0x8000;
for (i = c; i < c + 16; i++)
{
BlockIsRAM[i + bank] = TRUE;
BlockIsROM[i + bank] = FALSE;
}
}
}
}
static void BSX_Map_PSRAM (void)
{
int c;
// Banks 70->77:0000-FFFF
// FIXME: could be toggled by $03
for (c = 0; c < 0x80; c++)
{
Map[c + 0x700] = &PSRAM[((c & 0x70) << 12) % PSRAM_SIZE];
BlockIsRAM[c + 0x700] = TRUE;
BlockIsROM[c + 0x700] = FALSE;
}
// Banks 20->3F:6000-7FFF mirrors 70->77:6000-7FFF
for (c = 0x200; c < 0x400; c += 16)
{
Map[c + 6] = &PSRAM[((c & 0x70) << 12) % PSRAM_SIZE];
Map[c + 7] = &PSRAM[((c & 0x70) << 12) % PSRAM_SIZE];
BlockIsRAM[c + 6] = TRUE;
BlockIsRAM[c + 7] = TRUE;
BlockIsROM[c + 6] = FALSE;
BlockIsROM[c + 7] = FALSE;
}
if (!BSX.MMC[0x05])
// Banks 40->4F:0000-FFFF mirrors 70->77:0000-7FFF
map_psram_mirror_sub(0x40);
if (!BSX.MMC[0x06])
// Banks 50->5F:0000-FFFF mirrors 70->77:0000-7FFF
map_psram_mirror_sub(0x50);
// FIXME
if (!BSX.MMC[0x03])
// Banks 60->6F:0000-FFFF mirrors 70->77:0000-7FFF (?)
map_psram_mirror_sub(0x60);
}
static void BSX_Map_BIOS (void)
{
int i,c;
// Banks 00->1F:8000-FFFF
if (BSX.MMC[0x07])
{
for (c = 0; c < 0x200; c += 16)
{
for (i = c + 8; i < c + 16; i++)
{
Map[i] = &BIOSROM[(c << 11) % BIOS_SIZE] - 0x8000;
BlockIsRAM[i] = FALSE;
BlockIsROM[i] = TRUE;
}
}
}
// Banks 80->9F:8000-FFFF
if (BSX.MMC[0x08])
{
for (c = 0; c < 0x200; c += 16)
{
for (i = c + 8; i < c + 16; i++)
{
Map[i + 0x800] = &BIOSROM[(c << 11) % BIOS_SIZE] - 0x8000;
BlockIsRAM[i + 0x800] = FALSE;
BlockIsROM[i + 0x800] = TRUE;
}
}
}
}
static void BSX_Map_RAM (void)
{
int c;
// Banks 7E->7F
for (c = 0; c < 16; c++)
{
Map[c + 0x7E0] = RAM;
Map[c + 0x7F0] = RAM + 0x10000;
BlockIsRAM[c + 0x7E0] = TRUE;
BlockIsRAM[c + 0x7F0] = TRUE;
BlockIsROM[c + 0x7E0] = FALSE;
BlockIsROM[c + 0x7F0] = FALSE;
}
}
static void BSX_Map_Dirty (void)
{
// for the quick bank change
int i, c;
// Banks 00->1F and 80->9F:8000-FFFF
if (BSX.MMC[0x02])
{
for (c = 0; c < 0x200; c += 16)
{
for (i = c + 8; i < c + 16; i++)
{
Map[i] = Map[i + 0x800] = &MapROM[(c << 12) % FlashSize];
BlockIsRAM[i] = BlockIsRAM[i + 0x800] = BSX.write_enable;
BlockIsROM[i] = BlockIsROM[i + 0x800] = !BSX.write_enable;
}
}
}
else
{
for (c = 0; c < 0x200; c += 16)
{
for (i = c + 8; i < c + 16; i++)
{
Map[i] = Map[i + 0x800] = &MapROM[(c << 11) % FlashSize] - 0x8000;
BlockIsRAM[i] = BlockIsRAM[i + 0x800] = BSX.write_enable;
BlockIsROM[i] = BlockIsROM[i + 0x800] = !BSX.write_enable;
}
}
}
}
static void BSX_Map (void)
{
#ifdef BSX_DEBUG
printf("BS: Remapping\n");
for (int i = 0; i < 32; i++)
printf("BS: MMC %02X: %d\n", i, BSX.MMC[i]);
#endif
memcpy(BSX.prevMMC, BSX.MMC, sizeof(BSX.MMC));
// Do a quick bank change
if (BSX.dirty2 && !BSX.dirty)
{
BSX_Map_Dirty();
BSX_Map_BIOS();
BSX.dirty2 = FALSE;
Memory.map_WriteProtectROM();
return;
}
if (BSX.MMC[0x01])
{
MapROM = PSRAM;
FlashSize = PSRAM_SIZE;
}
else
{
MapROM = FlashROM;
FlashSize = FLASH_SIZE;
}
BSX_Map_SNES();
if (BSX.MMC[0x02])
BSX_Map_HiROM();
else
BSX_Map_LoROM();
BSX_Map_PSRAM();
BSX_Map_SRAM();
BSX_Map_RAM();
BSX_Map_BIOS();
BSX_Map_FlashIO();
BSX_Map_MMC();
// Monitor new register changes
BSX.dirty = FALSE;
BSX.dirty2 = FALSE;
Memory.map_WriteProtectROM();
}
static uint8 BSX_Get_Bypass_FlashIO (uint16 offset)
{
if (BSX.MMC[0x02])
return (MapROM[offset]);
else
{
if (offset < 0x8000)
return (MapROM[offset]);
else
return (MapROM[offset - 0x8000]);
}
}
static void BSX_Set_Bypass_FlashIO (uint16 offset, uint8 byte)
{
if (BSX.MMC[0x02])
MapROM[offset] = byte;
else
{
if (offset < 0x8000)
MapROM[offset] = byte;
else
MapROM[offset - 0x8000] = byte;
}
}
uint8 S9xGetBSX (uint32 address)
{
uint8 bank = (address >> 16) & 0xFF;
uint16 offset = address & 0xFFFF;
uint8 t = 0;
// MMC
if ((bank >= 0x01 && bank <= 0x0E) && (offset == 0x5000))
return (BSX.MMC[bank]);
// Flash IO
if (bank == 0xC0)
{
// default: read-through mode
t = BSX_Get_Bypass_FlashIO(offset);
// note: may be more registers, purposes unknown
switch (offset)
{
case 0x0002:
if (BSX.flash_enable)
t = 0x80; // status register?
break;
case 0x5555:
if (BSX.flash_enable)
t = 0x80; // ???
break;
case 0xFF00:
case 0xFF02:
case 0xFF04:
case 0xFF06:
case 0xFF08:
case 0xFF0A:
case 0xFF0C:
case 0xFF0E:
case 0xFF10:
case 0xFF12:
// return flash vendor information
if (BSX.read_enable)
t = flashcard[offset - 0xFF00];
break;
}
}
return (t);
}
void S9xSetBSX (uint8 byte, uint32 address)
{
uint8 bank = (address >> 16) & 0xFF;
uint16 offset = address & 0xFFFF;
// MMC
if ((bank >= 0x01 && bank <= 0x0E) && (offset == 0x5000))
{
switch (bank)
{
case 0x01:
case 0x02:
case 0x03:
case 0x04:
case 0x05:
case 0x06:
case 0x09:
case 0x0A:
case 0x0B:
case 0x0C:
case 0x0D:
if (BSX.MMC[bank] != byte)
{
BSX.MMC[bank] = byte;
BSX.dirty = TRUE;
}
break;
case 0x07:
case 0x08:
if (BSX.MMC[bank] != byte)
{
BSX.MMC[bank] = byte;
BSX.dirty2 = TRUE;
}
break;
case 0x0E:
BSX.MMC[bank] = byte;
if (byte && (BSX.dirty || BSX.dirty2))
BSX_Map();
break;
}
}
// Flash IO
if (bank == 0xC0)
{
BSX.old_write = BSX.new_write;
BSX.new_write = address;
// ???: double writes to the desired address will bypass
// flash registers
if (BSX.old_write == BSX.new_write && BSX.write_enable)
{
BSX_Set_Bypass_FlashIO(offset, byte);
return;
}
// flash command handling
// note: incomplete
switch (offset)
{
case 0x0000:
BSX.flash_command <<= 8;
BSX.flash_command |= byte;
if ((BSX.flash_command & 0xFFFF) == 0x38D0)
{
// retrieve information about the flash card
BSX.flash_enable = TRUE;
BSX.read_enable = TRUE;
}
break;
case 0x2AAA:
BSX.flash_command <<= 8;
BSX.flash_command |= byte;
break;
case 0x5555:
BSX.flash_command <<= 8;
BSX.flash_command |= byte;
switch (BSX.flash_command & 0xFFFFFF)
{
case 0xAA55F0:
// turn off flash i/o
BSX.flash_enable = FALSE;
BSX.write_enable = FALSE;
BSX.read_enable = FALSE;
break;
case 0xAA55A0:
// enable writing to flash
BSX.old_write = 0;
BSX.new_write = 0;
BSX.flash_enable = TRUE;
BSX.write_enable = TRUE;
BSX_Map();
break;
case 0xAA5570:
// turn on write-protection
BSX.write_enable = FALSE;
BSX_Map();
break;
case 0xAA5580:
case 0xAA5510:
// ???
break;
}
break;
}
}
}
uint8 S9xGetBSXPPU (uint16 address)
{
uint8 t;
// known read registers
switch (address)
{
// Test register low? (r/w)
case 0x2188:
t = BSX.PPU[0x2188 - BSXPPUBASE];
break;
// Test register high? (r/w)
case 0x2189:
t = BSX.PPU[0x2189 - BSXPPUBASE];
break;
case 0x218A:
t = BSX.PPU[0x218A - BSXPPUBASE];
break;
case 0x218C:
t = BSX.PPU[0x218C - BSXPPUBASE];
break;
// Transmission number low? (r/w)
case 0x218E:
t = BSX.PPU[0x218E - BSXPPUBASE];
break;
// Transmission number high? (r/w)
case 0x218F:
t = BSX.PPU[0x218F - BSXPPUBASE];
break;
// Status register? (r)
case 0x2190:
t = BSX.PPU[0x2190 - BSXPPUBASE];
break;
// Data register? (r/w)
case 0x2192:
t = BSX.PPU[0x2192 - BSXPPUBASE];
// test
t = BSX.test2192[BSX.out_index++];
if (BSX.out_index == 32)
BSX.out_index = 0;
BSX_RTC.ticks++;
if (BSX_RTC.ticks >= 1000)
{
BSX_RTC.ticks = 0;
BSX_RTC.seconds++;
}
if (BSX_RTC.seconds >= 60)
{
BSX_RTC.seconds = 0;
BSX_RTC.minutes++;
}
if (BSX_RTC.minutes >= 60)
{
BSX_RTC.minutes = 0;
BSX_RTC.hours++;
}
if (BSX_RTC.hours >= 24)
BSX_RTC.hours = 0;
BSX.test2192[10] = BSX_RTC.seconds;
BSX.test2192[11] = BSX_RTC.minutes;
BSX.test2192[12] = BSX_RTC.hours;
break;
// Transmission status? (r/w)
case 0x2193:
// Data ready when bits 2/3 clear?
t = BSX.PPU[0x2193 - BSXPPUBASE] & ~0x0C;
break;
// Reset? (r/w)
case 0x2194:
t = BSX.PPU[0x2194 - BSXPPUBASE];
break;
// Unknown (r)
case 0x2196:
t = BSX.PPU[0x2196 - BSXPPUBASE];
break;
// Unknown (r/w)
case 0x2197:
t = BSX.PPU[0x2197 - BSXPPUBASE];
break;
// Modem protocol? (r/w)
case 0x2199:
t = BSX.PPU[0x2199 - BSXPPUBASE];
break;
default:
t = OpenBus;
break;
}
return (t);
}
void S9xSetBSXPPU (uint8 byte, uint16 address)
{
// known write registers
switch (address)
{
// Test register low? (r/w)
case 0x2188:
BSX.PPU[0x2188 - BSXPPUBASE] = byte;
break;
// Test register high? (r/w)
case 0x2189:
BSX.PPU[0x2189 - BSXPPUBASE] = byte;
break;
case 0x218A:
BSX.PPU[0x218A - BSXPPUBASE] = byte;
break;
case 0x218B:
BSX.PPU[0x218B - BSXPPUBASE] = byte;
break;
case 0x218C:
BSX.PPU[0x218C - BSXPPUBASE] = byte;
break;
// Transmission number low? (r/w)
case 0x218E:
BSX.PPU[0x218E - BSXPPUBASE] = byte;
break;
// Transmission number high? (r/w)
case 0x218F:
BSX.PPU[0x218F - BSXPPUBASE] = byte;
// ?
BSX.PPU[0x218E - BSXPPUBASE] >>= 1;
BSX.PPU[0x218E - BSXPPUBASE] = BSX.PPU[0x218F - BSXPPUBASE] - BSX.PPU[0x218E - BSXPPUBASE];
BSX.PPU[0x218F - BSXPPUBASE] >>= 1;
BSX.PPU[0x2190 - BSXPPUBASE] = 0x80; // ?
break;
// Strobe assert? (w)
case 0x2191:
BSX.PPU[0x2191 - BSXPPUBASE] = byte;
BSX.out_index = 0;
break;
// Data register? (r/w)
case 0x2192:
BSX.PPU[0x2192 - BSXPPUBASE] = 0x01; // ?
BSX.PPU[0x2190 - BSXPPUBASE] = 0x80; // ?
break;
// Transmission status? (r/w)
case 0x2193:
BSX.PPU[0x2193 - BSXPPUBASE] = byte;
break;
// Reset? (r/w)
case 0x2194:
BSX.PPU[0x2194 - BSXPPUBASE] = byte;
break;
// Unknown (r/w)
case 0x2197:
BSX.PPU[0x2197 - BSXPPUBASE] = byte;
break;
// Modem protocol? (r/w)
case 0x2199:
// Lots of modem strings written here when
// connection is lost or no uplink established
BSX.PPU[0x2199 - BSXPPUBASE] = byte;
break;
}
}
uint8 * S9xGetBasePointerBSX (uint32 address)
{
return (MapROM);
}
static bool8 BSX_LoadBIOS (void)
{
FILE *fp;
char path[PATH_MAX + 1], name[PATH_MAX + 1];
bool8 r = FALSE;
strcpy(path, S9xGetDirectory(BIOS_DIR));
strcat(path, SLASH_STR);
strcpy(name, path);
strcat(name, "BS-X.bin");
fp = fopen(name, "rb");
if (!fp)
{