This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
/
neo_run.cpp
4963 lines (3930 loc) · 141 KB
/
neo_run.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
// NeoGeo CD-WIP - Jan 25, 2019 - present
// mit .bin/.cue & .ccd/.img (trurip) unterstutzung (feb.4.2019)
// known issues:
// Audio Hz rate must be 44100 for proper CDDA speed (auto-handled in fba-ui)
//
// Ninja Commando: glitching "logo" during demo play. This is fixed by
// changing renderbyline mode on. A side-effect is that it makes CD-Loading
// for other games (karnovr, fatalfury3, etc) very slow. Must investigate.
/*
* FB Alpha Neo Geo module
*
* The video frequencies of MVS hardware are:
* hsync: 15625 Hz
* vsync: ~59.18 Hz (264 scanlines make up a single frame)
* The image occupies 224 scanlines, spanning scanlines 24 to 248.
*
* The memory card interface occupies 128KB of memory. There are 2 kinds of
* memory cards that can be used -- 16bit or 8bit. For games, a maximum of 16kB
* data is stored on either, but 16bit cards can be used to transfer bookkeeping
* information. For 8bit cards, only odd bytes are used to address memory and
* the BIOS tests the size of the inserted card by writing to the first byte of
* each 2kB block. The interface used is JEIDA 3.0.
*
* bit 6 of memory location 0x300081 = 0 -> 1/2 slot MVS
* = 1 + bit 5 of 0x320001 = 0 -> 4 slot MVS
* = 1 -> 6 slot MVS
*
* On MVS hardware with multiple slots, address 0x380021 is used to select the
* active slot.
*
* The watchdog timer will reset the system after ~0.13 seconds
*
* On an MV-1F system, the following code was used to test:
* 000100 203C 0001 4F51 MOVE.L #0x14F51,D0
* 000106 13C0 0030 0001 MOVE.B D0,0x300001
* 00010C 5380 SUBQ.L #1,D0
* 00010E 64FC BCC.S *-0x2 [0x10C]
* 000110 13C0 0030 0001 MOVE.B D0,0x300001
* 000116 60F8 BRA.S *-0x6 [0x110]
* This code loops long enough to sometimes cause a reset, sometimes not.
* The move takes 16 cycles, subq 8, bcc 10 if taken and 8 if not taken, so:
* (0x14F51 * 18 + 14) cycles / 12000000 cycles per second = 0.12876 seconds
*
* Newer games force a reset using the following code (this from kof99):
* 009CDA 203C 0003 0D40 MOVE.L #0x30D40,D0
* 009CE0 5380 SUBQ.L #1,D0
* 009CE2 64FC BCC.S *-0x2 [0x9CE0]
* Note however that there is a valid code path after this loop, and indeed
* there are many games that employ the watchdog as a protection device, by
* writing a value to SRAM (usually to 0xD00100) when the watchdog doesn't
* trigger a reset. Later on, the presence of this value can trigger various
* actions, usually a text screen indicating the game is an unlicensed copy.
*
* On AES hardware, reading unmapped memory returns the last value that was on
* the data-bus.
*
* Raster interrupts can be generated by specifying an offset, in pixels.
* This offset can be added to the following (depending on a control register):
* - the position of the electron beam, when setting the offset
* - the position of the electron beam, when interrupt 2 is triggered
* - the position of the electron beam, when interrupt 4 (vblank) is triggered
*
* Note that the offset is *always* relative; when setting the offset from the
* vblank (scanline 248), to trigger an interrupt at scanline 24, you would
* point the offset to 24 scanlines past the screen end at line 264, and set it
* to 15360 (40 scanlines * 384 pixels per scanline).
*
*/
#include "neogeo.h"
#include "cd_interface.h"
#include "burn_ym2610.h"
#include "bitswap.h"
#include "neocdlist.h"
// #undef USE_SPEEDHACKS
// #define LOG_IRQ
// #define LOG_DRAW
#define NEO_HREFRESH (15625.0)
#define NEO_VREFRESH (NEO_HREFRESH / 264.0)
// #define NEO_VREFRESH (NEO_HREFRESH / 312.0)
// If defined, enable emulation of the watchdog timer (timing doesn't match real hardware 100%)
#define EMULATE_WATCHDOG
// If defined, reset the Z80 when switching between the Z80 BIOS/cartridge ROM
//#define Z80_RESET_ON_BANKSWITCH
// If defined, adjust the Z80 speed along with the 68000 when overclocking
#define Z80_SPEED_ADJUST
// If defined, use kludges to better align raster effects in some games (e.g. mosyougi)
#define RASTER_KLUDGE
// If defined, use the bAllowRasters variable to enable/disable raster effects
// #define RASTERS_OPTIONAL
#if defined Z80_SPEED_ADJUST
static INT32 nZ80Clockspeed;
#else
static const INT32 nZ80Clockspeed = 4000000;
#endif
#if defined RASTER_KLUDGE
static UINT16 nScanlineOffset;
#else
// 0xF8 is correct as verified on MVS hardware
static const UINT16 nScanlineOffset = 0xF8;
#endif
#if defined RASTERS_OPTIONAL
static bool bAllowRasters = false;
#endif
// The number of cartridge slots on the emulated MVS hardware (can be 1, 2, 4, or 6)
UINT8 nNeoNumSlots = 1;
UINT32 nNeoActiveSlot = 0;
UINT8 NeoButton1[32] = { 0, };
UINT8 NeoButton2[8] = { 0, };
UINT8 NeoButton3[8] = { 0, };
UINT8 NeoButton4[8] = { 0, };
UINT8 NeoJoy1[8] = { 0, };
UINT8 NeoJoy2[8] = { 0, };
UINT8 NeoJoy3[8] = { 0, };
UINT8 NeoJoy4[8] = { 0, };
UINT16 NeoAxis[2] = { 0, };
UINT8 NeoInput[32] = { 0, };
UINT8 NeoDiag[2] = { 0, };
UINT8 NeoDebugDip[2] = { 0, };
UINT8 NeoReset = 0, NeoSystem = 0;
static UINT8 OldDebugDip[2];
// Which 68K BIOS to use
INT32 nBIOS;
// Joyports are multiplexed
static INT32 nJoyport0[8] = { 0, };
static INT32 nJoyport1[8] = { 0, };
// Ports always mirror the joystick, except when other controllers are hooked up:
//
// input no. corresponds to
//
// Joyport0: 0x00 : trackball X in irrmaze / p1 paddle in popbounc
// 0x01 : trackball Y in irrmaze
// 0x09 : mahjong controller
// 0x12 : mahjong controller (BIOS controls)
// 0x1B : Always has p1 joystick & buttons
// 0x24 : mahjong controller
// 0x20 & 0x21 : selected by irrmaze instead of 0x00 & 0x01 when you lose a life
// (activates air-jets)
// Joyport1: 0x00 : p2 paddle in popbounc
// 0x1B : Always has p2 joystick & buttons
// ----------------------------------------------------------------------------
// Variables that need to be included in savestates
static INT32 nCyclesExtra[2];
static INT32 nPrevBurnCPUSpeedAdjust;
bool bNeoEnableGraphics;
UINT32 nNeo68KROMBank;
static INT32 nIRQAcknowledge;
static INT32 nIRQControl;
static bool bSRAMWritable;
static bool b68KBoardROMBankedIn;
static bool bZ80BoardROMBankedIn;
static INT32 nZ80Bank0, nZ80Bank1, nZ80Bank2, nZ80Bank3;
static UINT8* NeoGraphicsRAMBank;
static UINT16 NeoGraphicsRAMPointer;
static INT32 nNeoGraphicsModulo;
INT32 nNeoSpriteFrame;
static INT32 nSpriteFrameSpeed;
static INT32 nSpriteFrameTimer;
static UINT8 nSoundLatch;
static UINT8 nSoundReply;
static UINT32 nSoundStatus;
#if 1 && defined USE_SPEEDHACKS
static INT32 nSoundPrevReply;
#endif
INT32 ssideki2mode = 0;
INT32 s1945pmode = 0;
INT32 fatfury2mode = 0; // fatfury2 protection active (fatfury2, ssideki)
INT32 vlinermode = 0;
static INT32 nInputSelect;
static UINT8* NeoInputBank;
static UINT32 nAnalogAxis[2];
static UINT32 nuPD4990ATicks;
static UINT32 nIRQOffset;
#define NO_IRQ_PENDING (0x7FFFFFFF)
static INT32 nIRQCycles;
#if defined EMULATE_WATCHDOG
static INT32 nNeoWatchdog;
#endif
bool bDisableNeoWatchdog = false;
static INT32 bNeoCDIRQEnabled = false;
static INT32 nNeoCDIRQVector;
static INT32 nNeoCDIRQVectorAck;
INT32 nNeoScreenWidth;
UINT8 nLEDLatch, nLED[3];
// ----------------------------------------------------------------------------
static bool bMemoryCardInserted, bMemoryCardWritable;
NEO_CALLBACK NeoCallback[MAX_SLOT] = { { NULL, NULL, NULL, NULL, NULL }, };
NEO_CALLBACK* NeoCallbackActive = &NeoCallback[0];
static INT32 nCyclesTotal[2];
static INT32 nCyclesSegment;
static INT32 nCyclesVBlank;
static INT32 nCycles68KSync;
UINT8 *Neo68KROM[MAX_SLOT] = { NULL, }, *Neo68KROMActive = NULL;
UINT8 *NeoVector[MAX_SLOT] = { NULL, }, *NeoVectorActive = NULL;
UINT8 *Neo68KFix[MAX_SLOT] = { NULL, };
UINT8 *NeoZ80ROM[MAX_SLOT] = { NULL, }, *NeoZ80ROMActive = NULL;
static UINT8 *AllRAM = NULL, *RAMEnd = NULL, *AllROM = NULL, *ROMEnd = NULL;
UINT8 *NeoSpriteRAM, *NeoTextRAM;
UINT8 *Neo68KBIOS, *NeoZ80BIOS;
static UINT8 *Neo68KRAM, *NeoZ80RAM, *NeoNVRAM, *NeoNVRAM2, *NeoMemoryCard;
static UINT32 nSpriteSize[MAX_SLOT] = { 0, };
static UINT32 nCodeSize[MAX_SLOT] = { 0, };
UINT8* NeoGraphicsRAM;
UINT8* YM2610ADPCMAROM[MAX_SLOT] = { NULL, };
UINT8* YM2610ADPCMBROM[MAX_SLOT] = { NULL, };
static INT32 nYM2610ADPCMASize[MAX_SLOT] = { 0, };
static INT32 nYM2610ADPCMBSize[MAX_SLOT] = { 0, };
static bool bIRQEnabled;
static INT32 nVBLankIRQ;
static INT32 nScanlineIRQ;
static bool bRenderImage;
static bool bRenderLineByLine;
static bool bForcePartialRender;
static bool bForceUpdateOnStatusRead;
static INT32 nNeoControlConfig;
static INT32 nNeoSystemType;
static bool bZ80BIOS;
static INT32 nNeoCDCyclesIRQ = 0, nNeoCDCyclesIRQPeriod = 0;
#ifdef BUILD_A68K
static bool bUseAsm68KCoreOldValue = false;
#endif
// NeoGeo CD-ROM Stuff
static INT32 nLC8951Register = 0;
static INT32 LC8951RegistersR[16];
static INT32 LC8951RegistersW[16];
static INT32 nActiveTransferArea;
static INT32 nSpriteTransferBank;
static INT32 nADPCMTransferBank;
static UINT8 nTransferWriteEnable;
static bool NeoCDOBJBankUpdate[4];
static bool bNeoCDCommsClock, bNeoCDCommsSend;
static UINT8 NeoCDCommsCommandFIFO[10] = { 0, };
static UINT8 NeoCDCommsStatusFIFO[10] = { 0, };
static INT32 NeoCDCommsWordCount = 0;
static INT32 NeoCDAssyStatus = 0;
static INT32 NeoCDSectorMin = 0;
static INT32 NeoCDSectorSec = 0;
static INT32 NeoCDSectorFrm = 0;
static INT32 NeoCDSectorLBA = 0;
static char NeoCDSectorData[2352];
static bool bNeoCDLoadSector = false;
static INT32 NeoCDDMAAddress1 = 0;
static INT32 NeoCDDMAAddress2 = 0;
static INT32 NeoCDDMAValue1 = 0;
static INT32 NeoCDDMAValue2 = 0;
static INT32 NeoCDDMACount = 0;
static INT32 NeoCDDMAMode = 0;
static INT32 NeoCDVectorSwitch = 0; // 1 ROM(ram), 0 BIOS
static INT32 nNeoCDMode = 0;
static INT32 nff0002 = 0;
static INT32 nff0004 = 0;
bool IsNeoGeoCD() {
return (nNeoSystemType & NEO_SYS_CD);
}
// This function is called once to determine how much memory is needed (RAMEnd-(UINT8 *)0),
// then a second time after the memory is allocated to set up all the pointers.
static INT32 RAMIndex()
{
UINT8* Next = AllRAM;
NeoPalSrc[0] = Next; Next += 0x002000; // Palette RAM Bank 0
NeoPalSrc[1] = Next; Next += 0x002000; // Palette RAM Bank 1
NeoGraphicsRAM = Next; Next += 0x020000; // Graphics controller RAM (2 64KB banks)
if (nNeoSystemType & NEO_SYS_CART) {
Neo68KRAM = Next; Next += 0x010000; // 68K work RAM
NeoZ80RAM = Next; Next += 0x000800; // Z80 RAM
NeoNVRAM = Next; Next += 0x010000; // Battery backed SRAM
}
if ((BurnDrvGetHardwareCode() & HARDWARE_SNK_CONTROLMASK) == HARDWARE_SNK_GAMBLING) {
NeoNVRAM2 = Next; Next += 0x002000; // Extra SRAM for vliner/jockeygp
}
NeoMemoryCard = Next; Next += 0x020000; // Memory card
if (nNeoSystemType & NEO_SYS_CD) {
NeoSpriteRAM = Next; Next += nSpriteSize[0];
NeoTextRAM = Next; Next += nNeoTextROMSize[0];
}
RAMEnd = Next;
return 0;
}
// This function is called once to determine how much memory is needed (ROMEnd-(UINT8*)0),
// then a second time after the memory is allocated to set up all the pointers.
static INT32 ROMIndex()
{
UINT8* Next = AllROM;
NeoZ80BIOS = Next; Next += 0x020000; // Z80 boardROM
NeoZoomROM = Next; Next += 0x020000; // Y Zoom table
NeoTextROMBIOS = Next; Next += 0x020000;
if (nNeoSystemType & NEO_SYS_CART) {
Neo68KBIOS = Next; Next += 0x080000; // 68K boardROM
} else {
Neo68KROM[0] = Next; Next += nCodeSize[0];
NeoVector[0] = Next; Next += 0x000400; // Copy of 68K cartridge ROM with boardROM vector table
Neo68KBIOS = Next; Next += 0x080000; // 68K boardROM
NeoZ80ROM[0] = Next; Next += 0x080000;
NeoSpriteROM[0] = Next; Next += nSpriteSize[0];
NeoTextROM[0] = Next; Next += nNeoTextROMSize[0];
YM2610ADPCMAROM[0] = Next; Next += nYM2610ADPCMASize[0];
YM2610ADPCMBROM[0] = Next; Next += nYM2610ADPCMBSize[0];
}
ROMEnd = Next;
return 0;
}
// -----------------------------------------------------------------------------
// ROM loading
static void NeoSetSystemType()
{
// Neo CD
if (nNeoSystemType & NEO_SYS_CD) {
return;
}
// Dedicated JAMMA PCB
if (nNeoSystemType & NEO_SYS_PCB) {
return;
}
// See if we're emulating MVS or AES hardware
if (nBIOS == -1 || nBIOS == 15 || nBIOS == 16 || nBIOS == 17 || ((NeoSystem & 0x74) == 0x20)) {
nNeoSystemType = NEO_SYS_CART | NEO_SYS_AES;
return;
}
nNeoSystemType = NEO_SYS_CART | NEO_SYS_MVS;
}
static INT32 NeoLoad68KBIOS(INT32 nNewBIOS)
{
// Neo CD
if (nNeoSystemType & NEO_SYS_CD) {
return 0;
}
if ((BurnDrvGetHardwareCode() & HARDWARE_SNK_CONTROLMASK) == HARDWARE_SNK_TRACKBALL) {
nNewBIOS = 34;
}
if ((BurnDrvGetHardwareCode() & HARDWARE_PUBLIC_MASK) == HARDWARE_SNK_DEDICATED_PCB) {
nNewBIOS = 35;
}
// The most recent MVS models doesn't have a Z80 BIOS
bZ80BIOS = (nNewBIOS != 0) ? true : false;
// Check if we need to load a new BIOS
if (nNewBIOS == nBIOS) {
return 0;
}
nBIOS = nNewBIOS;
// Load the BIOS ROMs
if ((BurnDrvGetHardwareCode() & HARDWARE_PUBLIC_MASK) == HARDWARE_SNK_MVS) {
// Load the BIOS ROMs
BurnLoadRom(Neo68KBIOS, 0x00000 + nBIOS, 1);
} else {
if (nBIOS >= 0) {
BurnLoadRom(Neo68KBIOS, 0x00080 + nBIOS, 1);
} else {
BurnLoadRom(Neo68KBIOS, 0x00080 + 0, 1);
}
}
if (!strcmp(BurnDrvGetTextA(DRV_NAME), "kf2k3pcb") || !strcmp(BurnDrvGetTextA(DRV_NAME), "k2k3pcbd")) kf2k3pcb_bios_decode();
NeoUpdateVector();
return 0;
}
static INT32 FindType(const char* pName)
{
INT32 i = 0;
while (pName[i] && pName[i] != '-' && pName[i] != '_') {
i++;
}
return i + 1;
}
static INT32 FindROMs(UINT32 nType, INT32* pOffset, INT32* pNum)
{
INT32 nOffset = -1;
INT32 nNum = -1;
struct BurnRomInfo ri;
ri.nType = 0;
ri.nLen = 0;
// Invalidate indices
if (pOffset) {
*pOffset = -1;
}
if (pNum) {
*pNum = 0;
}
do {
if (BurnDrvGetRomInfo(&ri, ++nOffset)) {
return 1;
}
} while ((ri.nType & 7) != nType && nOffset < 0x80);
if (nOffset >= 0x7F) {
return 1;
}
do {
if (BurnDrvGetRomInfo(&ri, nOffset + ++nNum)) {
break;
}
} while ((ri.nType & 7) == nType && nOffset < 0x80);
if (pOffset) {
*pOffset = nOffset;
}
if (pNum) {
*pNum = nNum >= 0 ? nNum : 0;
}
return 0;
}
static INT32 LoadRoms()
{
NeoGameInfo info;
NeoGameInfo* pInfo = &info;
{
struct BurnRomInfo ri;
ri.nType = 0;
ri.nLen = 0;
// Find 'P' ROMs
FindROMs(1, &pInfo->nCodeOffset, &pInfo->nCodeNum);
// Find 'S' ROM
FindROMs(2, &pInfo->nTextOffset, NULL);
// Find 'C' ROMs
FindROMs(3, &pInfo->nSpriteOffset, &pInfo->nSpriteNum);
// Find 'M' ROM
FindROMs(4, &pInfo->nSoundOffset, NULL);
// Find 'V' ROMs
FindROMs(5, &pInfo->nADPCMOffset, &pInfo->nADPCMANum);
FindROMs(6, NULL, &pInfo->nADPCMBNum);
if (pInfo->nADPCMBNum < 0) {
pInfo->nADPCMBNum = 0;
}
#if 1 && defined FBA_DEBUG
bprintf(PRINT_IMPORTANT, _T(" - P: %i (%i);"), pInfo->nCodeOffset, pInfo->nCodeNum);
if (pInfo->nTextOffset >= 0) {
bprintf(PRINT_IMPORTANT, _T(" S: %i;"), pInfo->nTextOffset);
} else {
bprintf(PRINT_IMPORTANT, _T(" S: unused;"));
}
bprintf(PRINT_IMPORTANT, _T(" C: %i (%i); M: %i"), pInfo->nSpriteOffset, pInfo->nSpriteNum, pInfo->nSoundOffset);
if (pInfo->nADPCMOffset >= 0) {
bprintf(PRINT_IMPORTANT, _T(" V: %i (%i, %i)"), pInfo->nADPCMOffset, pInfo->nADPCMANum, pInfo->nADPCMBNum);
} else {
bprintf(PRINT_IMPORTANT, _T(" V: unused"));
}
bprintf(PRINT_IMPORTANT,_T("\n"));
#endif
nCodeSize[nNeoActiveSlot] = 0;
for (INT32 i = 0; i < pInfo->nCodeNum; i++) {
BurnDrvGetRomInfo(&ri, pInfo->nCodeOffset + i);
nCodeSize[nNeoActiveSlot] += ri.nLen;
}
nCodeSize[nNeoActiveSlot] = (nCodeSize[nNeoActiveSlot] + 0x0FFFFF) & ~0x0FFFFF;
nSpriteSize[nNeoActiveSlot] = 0;
if (BurnDrvGetHardwareCode() & HARDWARE_SNK_SWAPC) {
BurnDrvGetRomInfo(&ri, pInfo->nSpriteOffset);
// for viewpoin, aof, ssideki
if (pInfo->nSpriteNum == 2) {
nSpriteSize[nNeoActiveSlot] = 0x600000;
}
// for kotm2
if (pInfo->nSpriteNum == 4) {
BurnDrvGetRomInfo(&ri, pInfo->nSpriteOffset + 2);
if (ri.nLen == 0x080000) {
nSpriteSize[nNeoActiveSlot] = 0x600000;
}
}
}
if (nSpriteSize[nNeoActiveSlot] == 0) {
// Compute correct size taking gaps into account (kizuna)
for (INT32 i = 0; i < pInfo->nSpriteNum - 2; i += 2) {
BurnDrvGetRomInfo(&ri, pInfo->nSpriteOffset + i);
if (ri.nLen > nSpriteSize[nNeoActiveSlot]) {
nSpriteSize[nNeoActiveSlot] = ri.nLen;
}
}
nSpriteSize[nNeoActiveSlot] *= pInfo->nSpriteNum - 2;
if (!strcmp("kof97oro", BurnDrvGetTextA(DRV_NAME))) nSpriteSize[nNeoActiveSlot] = 0x2400000;
// The final 2 ROMs may have a different size
BurnDrvGetRomInfo(&ri, pInfo->nSpriteOffset + pInfo->nSpriteNum - 2);
nSpriteSize[nNeoActiveSlot] += ri.nLen * 2;
}
{
UINT32 nSize = nSpriteSize[nNeoActiveSlot];
// if (nSize > 0x4000000) {
// nSize = 0x4000000;
// }
for (nNeoTileMask[nNeoActiveSlot] = 1; nNeoTileMask[nNeoActiveSlot] < nSize; nNeoTileMask[nNeoActiveSlot] <<= 1) { }
nNeoTileMask[nNeoActiveSlot] = (nNeoTileMask[nNeoActiveSlot] >> 7) - 1;
nNeoMaxTile[nNeoActiveSlot] = nSize >> 7;
}
if (nNeoTextROMSize[nNeoActiveSlot] == 0) {
if (pInfo->nTextOffset > 0) {
BurnDrvGetRomInfo(&ri, pInfo->nTextOffset);
nNeoTextROMSize[nNeoActiveSlot] = ri.nLen;
} else {
nNeoTextROMSize[nNeoActiveSlot] = 0x080000;
}
}
nYM2610ADPCMASize[nNeoActiveSlot] = nYM2610ADPCMBSize[nNeoActiveSlot] = 0;
if (pInfo->nADPCMOffset >= 0) {
char* pName;
BurnDrvGetRomInfo(&ri, pInfo->nADPCMOffset);
BurnDrvGetRomName(&pName, pInfo->nADPCMOffset, 0);
nYM2610ADPCMASize[nNeoActiveSlot] = ri.nLen;
if (pInfo->nADPCMANum > 1) {
BurnDrvGetRomInfo(&ri, pInfo->nADPCMOffset + pInfo->nADPCMANum - 1);
BurnDrvGetRomName(&pName, pInfo->nADPCMOffset + pInfo->nADPCMANum - 1, 0);
if (pInfo->nADPCMBNum == 0) {
nYM2610ADPCMASize[nNeoActiveSlot] *= pName[FindType(pName) + 1] - '1';
} else {
nYM2610ADPCMASize[nNeoActiveSlot] *= pName[FindType(pName) + 2] - '1';
}
nYM2610ADPCMASize[nNeoActiveSlot] += ri.nLen;
}
if (pInfo->nADPCMBNum) {
BurnDrvGetRomInfo(&ri, pInfo->nADPCMOffset + pInfo->nADPCMANum);
nYM2610ADPCMBSize[nNeoActiveSlot] = ri.nLen * (pInfo->nADPCMBNum - 1);
BurnDrvGetRomInfo(&ri, pInfo->nADPCMOffset + pInfo->nADPCMANum + pInfo->nADPCMBNum - 1);
nYM2610ADPCMBSize[nNeoActiveSlot] += ri.nLen;
}
}
}
if (!strcmp("kof2k4se", BurnDrvGetTextA(DRV_NAME))) nYM2610ADPCMASize[nNeoActiveSlot] += 0x800000;
if (!strcmp("cphd", BurnDrvGetTextA(DRV_NAME))) nYM2610ADPCMASize[nNeoActiveSlot] = 0x4000000;
if (!strcmp("kf2k4pls", BurnDrvGetTextA(DRV_NAME))) nYM2610ADPCMASize[nNeoActiveSlot] += 0x800000;
if (!strcmp("svcboot", BurnDrvGetTextA(DRV_NAME))) nYM2610ADPCMASize[nNeoActiveSlot] += 0x400000;
if (!strcmp("svcplus", BurnDrvGetTextA(DRV_NAME))) nYM2610ADPCMASize[nNeoActiveSlot] += 0x400000;
if (!strcmp("svcplusa", BurnDrvGetTextA(DRV_NAME))) nYM2610ADPCMASize[nNeoActiveSlot] += 0x400000;
if (!strcmp("svcsplus", BurnDrvGetTextA(DRV_NAME))) nYM2610ADPCMASize[nNeoActiveSlot] += 0x400000;
if (!strcmp("pbobblenb", BurnDrvGetTextA(DRV_NAME))) nYM2610ADPCMASize[nNeoActiveSlot] = 0x380000;
if (!strcmp("alpham2p", BurnDrvGetTextA(DRV_NAME))) nYM2610ADPCMASize[nNeoActiveSlot] = 0x200000;
if (!strcmp("burningfp", BurnDrvGetTextA(DRV_NAME))) nYM2610ADPCMASize[nNeoActiveSlot] = 0x180000;
if (!strcmp("burningfpa", BurnDrvGetTextA(DRV_NAME))) nYM2610ADPCMASize[nNeoActiveSlot] = 0x200000;
if (!strcmp("gpilotsp", BurnDrvGetTextA(DRV_NAME))) nYM2610ADPCMASize[nNeoActiveSlot] = 0x180000;
if (!strcmp("lresortp", BurnDrvGetTextA(DRV_NAME))) nYM2610ADPCMASize[nNeoActiveSlot] = 0x200000;
if (!strcmp("kotm2p", BurnDrvGetTextA(DRV_NAME))) nYM2610ADPCMASize[nNeoActiveSlot] = 0x300000;
if (!strcmp("sbp", BurnDrvGetTextA(DRV_NAME))) nYM2610ADPCMASize[nNeoActiveSlot] = 0x800000;
if (!strcmp("lasthope", BurnDrvGetTextA(DRV_NAME))) nYM2610ADPCMASize[nNeoActiveSlot] = 0x600000;
if (!strcmp("mslug5w", BurnDrvGetTextA(DRV_NAME))) nYM2610ADPCMASize[nNeoActiveSlot] = 0x10002f0;
if (!strcmp("kof2k2omg", BurnDrvGetTextA(DRV_NAME))) nYM2610ADPCMASize[nNeoActiveSlot] = 0x1000000;
if (!strcmp("kof2k2omg9b", BurnDrvGetTextA(DRV_NAME))) nYM2610ADPCMASize[nNeoActiveSlot] = 0x1000000;
if (!strcmp("kof2k2omg9", BurnDrvGetTextA(DRV_NAME))) nYM2610ADPCMASize[nNeoActiveSlot] = 0x1000000;
if (!strcmp("kof98pfe", BurnDrvGetTextA(DRV_NAME))) nYM2610ADPCMASize[nNeoActiveSlot] = 0x1000000;
// bprintf(PRINT_NORMAL, _T("%x\n"), nYM2610ADPCMASize[nNeoActiveSlot]);
// The kof2k3 PCB has 96MB of graphics ROM, however the last 16MB are unused, and the protection/decryption hardware does not see them
// if (nSpriteSize[nNeoActiveSlot] > 0x4000000) {
// nSpriteSize[nNeoActiveSlot] = 0x5000000;
// }
NeoSpriteROM[nNeoActiveSlot] = (UINT8*)BurnMalloc(nSpriteSize[nNeoActiveSlot] < (nNeoTileMask[nNeoActiveSlot] << 7) ? ((nNeoTileMask[nNeoActiveSlot] + 1) << 7) : nSpriteSize[nNeoActiveSlot]);
if (NeoSpriteROM[nNeoActiveSlot] == NULL) {
return 1;
}
/* if ((BurnDrvGetHardwareCode() & HARDWARE_PUBLIC_MASK) == HARDWARE_SNK_DEDICATED_PCB) {
BurnSetProgressRange(1.0 / ((double)nSpriteSize[nNeoActiveSlot] / 0x800000 / 12));
} else if (BurnDrvGetHardwareCode() & (HARDWARE_SNK_CMC42 | HARDWARE_SNK_CMC50)) {
BurnSetProgressRange(1.0 / ((double)nSpriteSize[nNeoActiveSlot] / 0x800000 / 9));
} else {
BurnSetProgressRange(1.0 / ((double)nSpriteSize[nNeoActiveSlot] / 0x800000 / 3));
}*/
if (BurnDrvGetHardwareCode() & (HARDWARE_SNK_CMC42 | HARDWARE_SNK_CMC50)) {
double fRange = (double)pInfo->nSpriteNum / 4.0;
if (fRange < 1.5) {
fRange = 1.5;
}
BurnSetProgressRange(1.0 / fRange);
} else {
BurnSetProgressRange(1.0 / pInfo->nSpriteNum);
}
// Load sprite data
NeoLoadSprites(pInfo->nSpriteOffset, pInfo->nSpriteNum, NeoSpriteROM[nNeoActiveSlot], nSpriteSize[nNeoActiveSlot]);
NeoTextROM[nNeoActiveSlot] = (UINT8*)BurnMalloc(nNeoTextROMSize[nNeoActiveSlot]);
if (NeoTextROM[nNeoActiveSlot] == NULL) {
return 1;
}
// Load Text layer tiledata
{
if (pInfo->nTextOffset != -1) {
// Load S ROM data
BurnLoadRom(NeoTextROM[nNeoActiveSlot], pInfo->nTextOffset, 1);
} else {
// Extract data from the end of C ROMS
BurnUpdateProgress(0.0, _T("Decrypting text layer graphics...")/*, BST_DECRYPT_TXT*/, 0);
NeoCMCExtractSData(NeoSpriteROM[nNeoActiveSlot], NeoTextROM[nNeoActiveSlot], nSpriteSize[nNeoActiveSlot], nNeoTextROMSize[nNeoActiveSlot]);
if ((BurnDrvGetHardwareCode() & HARDWARE_PUBLIC_MASK) == HARDWARE_SNK_DEDICATED_PCB) {
for (INT32 i = 0; i < nNeoTextROMSize[nNeoActiveSlot]; i++) {
NeoTextROM[nNeoActiveSlot][i] = BITSWAP08(NeoTextROM[nNeoActiveSlot][i] ^ 0xd2, 4, 0, 7, 2, 5, 1, 6, 3);
}
}
}
}
Neo68KROM[nNeoActiveSlot] = (UINT8*)BurnMalloc(nCodeSize[nNeoActiveSlot]); // 68K cartridge ROM
if (Neo68KROM[nNeoActiveSlot] == NULL) {
return 1;
}
Neo68KROMActive = Neo68KROM[nNeoActiveSlot];
Neo68KFix[nNeoActiveSlot] = Neo68KROM[nNeoActiveSlot];
// Load the roms into memory
if (BurnDrvGetHardwareCode() & HARDWARE_SNK_SMA_PROTECTION) {
BurnLoadRom(Neo68KROMActive + 0x0C0000, 0, 1);
NeoLoadCode(pInfo->nCodeOffset + 1, pInfo->nCodeNum - 1, Neo68KROMActive + 0x100000);
} else {
NeoLoadCode(pInfo->nCodeOffset, pInfo->nCodeNum, Neo68KROMActive);
}
NeoZ80ROM[nNeoActiveSlot] = (UINT8*)BurnMalloc(0x080000); // Z80 cartridge ROM
if (NeoZ80ROM[nNeoActiveSlot] == NULL) {
return 1;
}
NeoZ80ROMActive = NeoZ80ROM[nNeoActiveSlot];
BurnLoadRom(NeoZ80ROMActive, pInfo->nSoundOffset, 1);
if (BurnDrvGetHardwareCode() & HARDWARE_SNK_ENCRYPTED_M1) {
neogeo_cmc50_m1_decrypt();
}
if (NeoCallbackActive && NeoCallbackActive->pInitialise) {
NeoCallbackActive->pInitialise();
}
// Decode text data
BurnUpdateProgress(0.0, _T("Preprocessing text layer graphics...")/*, BST_PROCESS_TXT*/, 0);
NeoDecodeText(0, nNeoTextROMSize[nNeoActiveSlot], NeoTextROM[nNeoActiveSlot], NeoTextROM[nNeoActiveSlot]);
// Decode sprite data
NeoDecodeSprites(NeoSpriteROM[nNeoActiveSlot], nSpriteSize[nNeoActiveSlot]);
if (pInfo->nADPCMANum) {
char* pName;
struct BurnRomInfo ri;
UINT8* pADPCMData;
YM2610ADPCMAROM[nNeoActiveSlot] = (UINT8*)BurnMalloc(nYM2610ADPCMASize[nNeoActiveSlot]);
if (YM2610ADPCMAROM[nNeoActiveSlot] == NULL) {
return 1;
}
ri.nType = 0;
ri.nLen = 0;
BurnDrvGetRomInfo(&ri, pInfo->nADPCMOffset);
BurnDrvGetRomName(&pName, pInfo->nADPCMOffset, 0);
pADPCMData = YM2610ADPCMAROM[nNeoActiveSlot];
if (strcmp(BurnDrvGetTextA(DRV_NAME), "sbp") != 0) { // not for sbp!
// pbobblen needs this (V ROMs are v3 & v4), note aof/wh1/wh1h/kotm2 (V ROMs are v2 & v4)
if (pInfo->nADPCMANum == 2 && pName[FindType(pName) + 1] == '3') {
pADPCMData += ri.nLen * 2;
}
}
if (!strcmp(BurnDrvGetTextA(DRV_NAME), "pbobblenb")) {
pADPCMData = YM2610ADPCMAROM[nNeoActiveSlot] + 0x200000;
}
NeoLoadADPCM(pInfo->nADPCMOffset, pInfo->nADPCMANum, pADPCMData);
if (BurnDrvGetHardwareCode() & HARDWARE_SNK_SWAPV) {
for (INT32 i = 0; i < 0x00200000; i++) {
UINT8 n = YM2610ADPCMAROM[nNeoActiveSlot][i];
YM2610ADPCMAROM[nNeoActiveSlot][i] = YM2610ADPCMAROM[nNeoActiveSlot][0x00200000 + i];
YM2610ADPCMAROM[nNeoActiveSlot][0x00200000 + i] = n;
}
}
}
if (pInfo->nADPCMBNum) {
YM2610ADPCMBROM[nNeoActiveSlot] = (UINT8*)BurnMalloc(nYM2610ADPCMBSize[nNeoActiveSlot]);
if (YM2610ADPCMBROM[nNeoActiveSlot] == NULL) {
return 1;
}
NeoLoadADPCM(pInfo->nADPCMOffset + pInfo->nADPCMANum, pInfo->nADPCMBNum, YM2610ADPCMBROM[nNeoActiveSlot]);
} else {
YM2610ADPCMBROM[nNeoActiveSlot] = YM2610ADPCMAROM[nNeoActiveSlot];
nYM2610ADPCMBSize[nNeoActiveSlot] = nYM2610ADPCMASize[nNeoActiveSlot];
}
return 0;
}
// ----------------------------------------------------------------------------
// Bankswitch / memory map functions
static void NeoZ80SetBank0(INT32 nBank)
{
nBank &= 0x0F;
if (nBank != nZ80Bank0) {
UINT8* nStartAddress = NeoZ80ROMActive + (nBank << 14);
ZetMapArea(0x8000, 0xBFFF, 0, nStartAddress);
ZetMapArea(0x8000, 0xBFFF, 2, nStartAddress);
nZ80Bank0 = nBank;
}
return;
}
static void NeoZ80SetBank1(INT32 nBank)
{
nBank &= 0x1F;
if (nBank != nZ80Bank1) {
UINT8* nStartAddress = NeoZ80ROMActive + (nBank << 13);
ZetMapArea(0xC000, 0xDFFF, 0, nStartAddress);
ZetMapArea(0xC000, 0xDFFF, 2, nStartAddress);
nZ80Bank1 = nBank;
}
return;
}
static void NeoZ80SetBank2(INT32 nBank)
{
nBank &= 0x3F;
if (nBank != nZ80Bank2) {
UINT8* nStartAddress = NeoZ80ROMActive + (nBank << 12);
ZetMapArea(0xE000, 0xEFFF, 0, nStartAddress);
ZetMapArea(0xE000, 0xEFFF, 2, nStartAddress);
nZ80Bank2 = nBank;
}
return;
}
static void NeoZ80SetBank3(INT32 nBank)
{
nBank &= 0x7F;
if (nBank != nZ80Bank3) {
UINT8* nStartAddress = NeoZ80ROMActive + (nBank << 11);
ZetMapArea(0xF000, 0xF7FF, 0, nStartAddress);
ZetMapArea(0xF000, 0xF7FF, 2, nStartAddress);
nZ80Bank3 = nBank;
}
return;
}
static void NeoZ80MapROM(bool bMapBoardROM)
{
if (nNeoSystemType & NEO_SYS_CART) {
if (bMapBoardROM && bZ80BIOS) {
// Bank in the Z80 boardROM
ZetMapArea(0x0000, 0x7FFF, 0, NeoZ80BIOS);
ZetMapArea(0x0000, 0x7FFF, 2, NeoZ80BIOS);
} else {
// Bank in the Z80 cartridge ROM
ZetMapArea(0x0000, 0x7FFF, 0, NeoZ80ROMActive);
ZetMapArea(0x0000, 0x7FFF, 2, NeoZ80ROMActive);
}
}
}
static void MapVectorTable(bool bMapBoardROM)
{
if (nNeoSystemType & NEO_SYS_CD) {
NeoCDVectorSwitch = (!bMapBoardROM && Neo68KROMActive);
return;
}
if (!bMapBoardROM && Neo68KROMActive) {
SekMapMemory(Neo68KFix[nNeoActiveSlot], 0x000000, 0x0003FF, MAP_ROM);
} else {
SekMapMemory(NeoVectorActive, 0x000000, 0x0003FF, MAP_ROM);
}
}
inline static void MapPalette(INT32 nBank)
{
if (nNeoPaletteBank != nBank) {
nNeoPaletteBank = nBank;
SekMapMemory(NeoPalSrc[nBank], 0x400000, 0x401FFF, MAP_ROM);
NeoSetPalette();
}
}
static void Bankswitch(UINT32 nBank)
{
nBank = 0x100000 + ((nBank & 7) << 20);
if (nBank >= nCodeSize[nNeoActiveSlot]) {
nBank = 0x100000;
}
if (nBank != nNeo68KROMBank) {
// bprintf(PRINT_NORMAL, "Bankswitched main ROM, new address is 0x%08X.\n", nBank);
nNeo68KROMBank = nBank;
SekMapMemory(Neo68KROMActive + nNeo68KROMBank, 0x200000, 0x2FFFFF, MAP_ROM);
}
}
void NeoMapBank()
{
SekMapMemory(Neo68KROMActive + nNeo68KROMBank, 0x200000, 0x2FFFFF, MAP_ROM);
}
void NeoMap68KFix()
{
if ((nNeoSystemType & NEO_SYS_CART) && (nCodeSize[nNeoActiveSlot] > 0x100000)) {
SekMapMemory(Neo68KFix[nNeoActiveSlot] + 0x0400, 0x000400, 0x0FFFFF, MAP_ROM);
if (Neo68KROM[nNeoActiveSlot]) {
memcpy(NeoVector[nNeoActiveSlot] + 0x80, Neo68KFix[nNeoActiveSlot] + 0x80, 0x0380);
}
}
MapVectorTable(b68KBoardROMBankedIn);
}
void NeoUpdateVector()
{
// Create copy of 68K with BIOS vector table
for (INT32 i = 0; i < MAX_SLOT; i++) {
if (NeoVector[i]) {
memcpy(NeoVector[i] + 0x00, Neo68KBIOS, 0x0080);
if (Neo68KROM[i]) {
memcpy(NeoVector[i] + 0x80, Neo68KFix[i] + 0x80, 0x0380);
}
}
}
}
// ----------------------------------------------------------------------------
// 68K bankswitch for most games without SMA/PVC protection
static void __fastcall neogeoWriteByteBankswitch(UINT32 sekAddress, UINT8 byteValue)
{
if (sekAddress >= 0x2FFFF0) {
// bprintf(PRINT_NORMAL, _T(" - Bankswitch: 0x%06X -> 0x%02X\n"), sekAddress, byteValue);
Bankswitch(byteValue);
return;
}
}
static void __fastcall neogeoWriteWordBankswitch(UINT32 sekAddress, UINT16 wordValue)
{
if (sekAddress >= 0x2FFFF0) {
// bprintf(PRINT_NORMAL, _T(" - Bankswitch: 0x%06X -> 0x%04X\n"), sekAddress, wordValue);
Bankswitch(wordValue);
return;
}
}
// ----------------------------------------------------------------------------
// CPU synchronisation
static inline void neogeoSynchroniseZ80(INT32 nExtraCycles)
{
#if defined Z80_SPEED_ADJUST
INT32 nCycles = SekTotalCycles() / 3 + nExtraCycles;
#else
INT32 nCycles = ((INT64)SekTotalCycles() * nCyclesTotal[1] / nCyclesTotal[0]) + nExtraCycles;
#endif
if (nCycles <= ZetTotalCycles()) {
return;
}