-
Notifications
You must be signed in to change notification settings - Fork 30
/
libretro.cpp
1771 lines (1413 loc) · 47.5 KB
/
libretro.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
#include <stdarg.h>
#include <string.h>
#include <math.h>
#ifdef _MSC_VER
#include <compat/msvc.h>
#endif
#include <string/stdstring.h>
#include <retro_timers.h>
#include <streams/file_stream.h>
#include "mednafen/mednafen.h"
#include "mednafen/mempatcher.h"
#include "mednafen/git.h"
#include "mednafen/general.h"
#include "mednafen/md5.h"
#ifdef NEED_DEINTERLACER
#include "mednafen/video/Deinterlacer.h"
#endif
#include "libretro.h"
#include <rthreads/rthreads.h>
#include "mednafen/pcfx/pcfx.h"
#include "mednafen/pcfx/soundbox.h"
#include "mednafen/pcfx/input.h"
#include "mednafen/pcfx/king.h"
#include "mednafen/pcfx/timer.h"
#include "mednafen/pcfx/interrupt.h"
#include "mednafen/pcfx/rainbow.h"
#include "mednafen/pcfx/huc6273.h"
#include "mednafen/cdrom/scsicd.h"
#include "mednafen/mempatcher.h"
#include "mednafen/cdrom/cdromif.h"
#include "mednafen/md5.h"
#include "mednafen/clamp.h"
#include "mednafen/state_helpers.h"
#include "libretro_core_options.h"
struct retro_perf_callback perf_cb;
retro_get_cpu_features_t perf_get_cpu_features_cb = NULL;
retro_log_printf_t log_cb;
static retro_video_refresh_t video_cb;
static retro_audio_sample_t audio_cb;
static retro_audio_sample_batch_t audio_batch_cb;
static retro_environment_t environ_cb;
static retro_input_poll_t input_poll_cb;
static retro_input_state_t input_state_cb;
static MDFN_PixelFormat last_pixel_format;
static MDFN_Surface surf;
static bool failed_init;
static std::string retro_base_directory;
static std::string retro_save_directory;
static bool cd_eject_state;
static bool libretro_supports_bitmasks = false;
static bool libretro_supports_option_categories = false;
typedef struct
{
unsigned initial_index;
std::string initial_path;
std::vector<std::string> image_paths;
std::vector<std::string> image_labels;
} disk_control_ext_info_t;
static disk_control_ext_info_t disk_control_ext_info;
/* Mednafen - Multi-system Emulator
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* FIXME: soundbox, vce, vdc, rainbow, and king store wait states should be 4, not 2, but V810 has write buffers which can mask wait state penalties.
This is a hack to somewhat address the issue, but to really fix it, we need to handle write buffer emulation in the V810 emulation core itself.
*/
static std::vector<CDIF*> *cdifs = NULL;
static bool CD_TrayOpen;
static int CD_SelectedDisc; // -1 for no disc
V810 PCFX_V810;
static uint8 *BIOSROM = NULL; // 1MB
static uint8 *RAM = NULL; // 2MB
static uint32 RAM_LPA; // Last page access
static const int RAM_PageSize = 2048;
static const int RAM_PageNOTMask = ~(RAM_PageSize - 1);
static uint16 Last_VDC_AR[2];
static bool WantHuC6273 = FALSE;
//static
VDC *fx_vdc_chips[2];
static uint16 BackupControl;
static uint8 SaveRAM[2 * 0x8000]; // BackupRAM + ExBackupRAM
static uint8* BackupRAM = (uint8*)(SaveRAM + (0x8000 * 0));
static uint8* ExBackupRAM = (uint8*)(SaveRAM + (0x8000 * 1));
static uint8 ExBusReset; // I/O Register at 0x0700
static bool BRAMDisabled;// Cached at game load, don't remove this caching behavior or save game loss may result(if we ever get a GUI).
// Checks to see if this main-RAM-area access
// is in the same DRAM page as the last access.
#define RAMLPCHECK \
{ \
if ((A & RAM_PageNOTMask) != RAM_LPA) \
{ \
timestamp += 3; \
RAM_LPA = A & RAM_PageNOTMask; \
} \
}
static v810_timestamp_t next_pad_ts, next_timer_ts, next_adpcm_ts, next_king_ts;
static void PCFX_FixNonEvents(void)
{
if (next_pad_ts & 0x40000000)
next_pad_ts = PCFX_EVENT_NONONO;
if (next_timer_ts & 0x40000000)
next_timer_ts = PCFX_EVENT_NONONO;
if (next_adpcm_ts & 0x40000000)
next_adpcm_ts = PCFX_EVENT_NONONO;
if (next_king_ts & 0x40000000)
next_king_ts = PCFX_EVENT_NONONO;
}
static void PCFX_Event_Reset(void)
{
next_pad_ts = PCFX_EVENT_NONONO;
next_timer_ts = PCFX_EVENT_NONONO;
next_adpcm_ts = PCFX_EVENT_NONONO;
next_king_ts = PCFX_EVENT_NONONO;
}
static INLINE uint32 CalcNextTS(void)
{
v810_timestamp_t next_timestamp = next_king_ts;
if (next_timestamp > next_pad_ts)
next_timestamp = next_pad_ts;
if (next_timestamp > next_timer_ts)
next_timestamp = next_timer_ts;
if (next_timestamp > next_adpcm_ts)
next_timestamp = next_adpcm_ts;
return next_timestamp;
}
static void RebaseTS(const v810_timestamp_t timestamp, const v810_timestamp_t new_base_timestamp)
{
assert(next_pad_ts > timestamp);
assert(next_timer_ts > timestamp);
assert(next_adpcm_ts > timestamp);
assert(next_king_ts > timestamp);
next_pad_ts -= (timestamp - new_base_timestamp);
next_timer_ts -= (timestamp - new_base_timestamp);
next_adpcm_ts -= (timestamp - new_base_timestamp);
next_king_ts -= (timestamp - new_base_timestamp);
}
void PCFX_SetEvent(const int type, const v810_timestamp_t next_timestamp)
{
if (type == PCFX_EVENT_PAD)
next_pad_ts = next_timestamp;
else if (type == PCFX_EVENT_TIMER)
next_timer_ts = next_timestamp;
else if (type == PCFX_EVENT_ADPCM)
next_adpcm_ts = next_timestamp;
else if (type == PCFX_EVENT_KING)
next_king_ts = next_timestamp;
if (next_timestamp < PCFX_V810.GetEventNT())
PCFX_V810.SetEventNT(next_timestamp);
}
static int32 MDFN_FASTCALL pcfx_event_handler(const v810_timestamp_t timestamp)
{
if (timestamp >= next_king_ts)
next_king_ts = KING_Update(timestamp);
if (timestamp >= next_pad_ts)
next_pad_ts = FXINPUT_Update(timestamp);
if (timestamp >= next_timer_ts)
next_timer_ts = FXTIMER_Update(timestamp);
if (timestamp >= next_adpcm_ts)
next_adpcm_ts = SoundBox_ADPCMUpdate(timestamp);
return CalcNextTS();
}
// Called externally from debug.cpp
static void ForceEventUpdates(const uint32 timestamp)
{
next_king_ts = KING_Update(timestamp);
next_pad_ts = FXINPUT_Update(timestamp);
next_timer_ts = FXTIMER_Update(timestamp);
next_adpcm_ts = SoundBox_ADPCMUpdate(timestamp);
PCFX_V810.SetEventNT(CalcNextTS());
}
#include "mednafen/pcfx/io-handler.inc"
#include "mednafen/pcfx/mem-handler.inc"
typedef struct
{
int8 tracknum;
int8 format;
uint32 lba;
} CDGameEntryTrack;
typedef struct
{
const char *name;
const char *name_original; // Original non-Romanized text.
const uint32 flags; // Emulation flags.
const unsigned int discs; // Number of discs for this game.
CDGameEntryTrack tracks[2][100]; // 99 tracks and 1 leadout track
} CDGameEntry;
#define CDGE_FORMAT_AUDIO 0
#define CDGE_FORMAT_DATA 1
#define CDGE_FLAG_ACCURATE_V810 0x01
#define CDGE_FLAG_FXGA 0x02
static uint32 EmuFlags;
static void Emulate(EmulateSpecStruct *espec)
{
FXINPUT_Frame();
MDFNMP_ApplyPeriodicCheats();
if (espec->VideoFormatChanged)
KING_SetPixelFormat(espec->surface->format);
KING_StartFrame(fx_vdc_chips, espec);
v810_timestamp_t v810_timestamp;
v810_timestamp = PCFX_V810.Run(pcfx_event_handler);
PCFX_FixNonEvents();
// Call before resetting v810_timestamp
ForceEventUpdates(v810_timestamp);
//
// Call KING_EndFrame() before SoundBox_Flush(), otherwise CD-DA audio distortion will occur due to sound data being updated
// after it was needed instead of before.
//
KING_EndFrame(v810_timestamp);
// new_base_ts is guaranteed to be <= v810_timestamp
v810_timestamp_t new_base_ts;
espec->SoundBufSize = SoundBox_Flush(v810_timestamp, &new_base_ts, espec->SoundBuf, espec->SoundBufMaxSize);
KING_ResetTS(new_base_ts);
FXTIMER_ResetTS(new_base_ts);
FXINPUT_ResetTS(new_base_ts);
SoundBox_ResetTS(new_base_ts);
// Call this AFTER all the EndFrame/Flush/ResetTS stuff
RebaseTS(v810_timestamp, new_base_ts);
PCFX_V810.ResetTS(new_base_ts);
}
static void PCFX_Reset(void)
{
const uint32 timestamp = PCFX_V810.v810_timestamp;
// Make sure all devices are synched to current timestamp before calling their Reset()/Power()(though devices should already do this sort of thing on their
// own, but it's not implemented for all of them yet, and even if it was all implemented this is also INSURANCE).
ForceEventUpdates(timestamp);
PCFX_Event_Reset();
RAM_LPA = 0;
ExBusReset = 0;
BackupControl = 0;
Last_VDC_AR[0] = 0;
Last_VDC_AR[1] = 0;
memset(RAM, 0x00, 2048 * 1024);
for (int i = 0; i < 2; i++)
{
int32 dummy_ne MDFN_NOWARN_UNUSED;
dummy_ne = fx_vdc_chips[i]->Reset();
}
KING_Reset(timestamp); // SCSICD_Power() is called from KING_Reset()
SoundBox_Reset(timestamp);
RAINBOW_Reset();
if (WantHuC6273)
HuC6273_Reset();
PCFXIRQ_Reset();
FXTIMER_Reset();
PCFX_V810.Reset();
// Force device updates so we can get new next event timestamp values.
ForceEventUpdates(timestamp);
}
static void PCFX_Power(void)
{
PCFX_Reset();
}
static void VDCA_IRQHook(bool asserted)
{
PCFXIRQ_Assert(PCFXIRQ_SOURCE_VDCA, asserted);
}
static void VDCB_IRQHook(bool asserted)
{
PCFXIRQ_Assert(PCFXIRQ_SOURCE_VDCB, asserted);
}
#ifdef _WIN32
char slash = '\\';
#else
char slash = '/';
#endif
static bool LoadCommon(std::vector<CDIF *> *CDInterfaces)
{
V810_Emu_Mode cpu_mode = _V810_EMU_MODE_COUNT;
std::string biospath = retro_base_directory + slash + MDFN_GetSettingS("pcfx.bios");
MDFNFILE *BIOSFile = file_open(biospath.c_str());
if (!BIOSFile)
return false;
cpu_mode = (V810_Emu_Mode)MDFN_GetSettingI("pcfx.cpu_emulation");
if (cpu_mode == _V810_EMU_MODE_COUNT)
cpu_mode = (EmuFlags & CDGE_FLAG_ACCURATE_V810) ? V810_EMU_MODE_ACCURATE : V810_EMU_MODE_FAST;
PCFX_V810.Init(cpu_mode, false);
uint32 RAM_Map_Addresses[1] = { 0x00000000 };
uint32 BIOSROM_Map_Addresses[1] = { 0xFFF00000 };
RAM = PCFX_V810.SetFastMap(RAM_Map_Addresses, 0x00200000, 1, "RAM");
if (!RAM)
return false;
BIOSROM = PCFX_V810.SetFastMap(BIOSROM_Map_Addresses, 0x00100000, 1, "BIOS ROM");
if (!BIOSROM)
return false;
if (BIOSFile->size != 1024 * 1024)
return false;
memcpy(BIOSROM, BIOSFile->data, 1024 * 1024);
file_close(BIOSFile);
BIOSFile = NULL;
for (int i = 0; i < 2; i++)
{
fx_vdc_chips[i] = new VDC(MDFN_GetSettingB("pcfx.nospritelimit"), 65536);
fx_vdc_chips[i]->SetWSHook(NULL);
fx_vdc_chips[i]->SetIRQHook(i ? VDCB_IRQHook : VDCA_IRQHook);
}
SoundBox_Init(MDFN_GetSettingB("pcfx.adpcm.emulate_buggy_codec"), MDFN_GetSettingB("pcfx.adpcm.suppress_channel_reset_clicks"));
RAINBOW_Init(MDFN_GetSettingB("pcfx.rainbow.chromaip"));
FXINPUT_Init();
FXTIMER_Init();
if (WantHuC6273)
HuC6273_Init();
if (!KING_Init())
{
free(BIOSROM);
free(RAM);
BIOSROM = NULL;
RAM = NULL;
return false;
}
CD_TrayOpen = false;
CD_SelectedDisc = 0;
/* Attempt to set initial disk index */
if ((disk_control_ext_info.initial_index > 0) &&
(disk_control_ext_info.initial_index < (*CDInterfaces).size()))
if (disk_control_ext_info.initial_index <
disk_control_ext_info.image_paths.size())
if (string_is_equal(
disk_control_ext_info.image_paths[disk_control_ext_info.initial_index].c_str(),
disk_control_ext_info.initial_path.c_str()))
CD_SelectedDisc = (int)disk_control_ext_info.initial_index;
SCSICD_SetDisc(true, NULL, true);
SCSICD_SetDisc(false, (*CDInterfaces)[CD_SelectedDisc], true);
EmulatedPCFX.nominal_height = MDFN_GetSettingUI("pcfx.slend") - MDFN_GetSettingUI("pcfx.slstart") + 1;
// Emulation raw framebuffer image should always be of 256 width when the pcfx.high_dotclock_width setting is set to "256",
// but it could be either 256 or 341 when the setting is set to "341", so stay with 1024 in that case so we won't have
// a messed up aspect ratio in our recorded QuickTime movies.
EmulatedPCFX.lcm_width = (MDFN_GetSettingUI("pcfx.high_dotclock_width") == 256) ? 256 : 1024;
EmulatedPCFX.lcm_height = EmulatedPCFX.nominal_height;
MDFNMP_Init(1024 * 1024, ((uint64)1 << 32) / (1024 * 1024));
MDFNMP_AddRAM(2048 * 1024, 0x00000000, RAM);
if (!(BRAMDisabled = MDFN_GetSettingB("pcfx.disable_bram")))
{
// Initialize Save RAM
memset(SaveRAM, 0, sizeof(SaveRAM));
static const uint8 BRInit00[] = { 0x24, 0x8A, 0xDF, 0x50, 0x43, 0x46, 0x58, 0x53, 0x72, 0x61, 0x6D, 0x80,
0x00, 0x01, 0x01, 0x00, 0x01, 0x40, 0x00, 0x00, 0x01, 0xF9, 0x03, 0x00,
0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
};
static const uint8 BRInit80[] = { 0xF9, 0xFF, 0xFF };
memcpy(BackupRAM + 0x00, BRInit00, sizeof(BRInit00));
memcpy(BackupRAM + 0x80, BRInit80, sizeof(BRInit80));
static const uint8 ExBRInit00[] = { 0x24, 0x8A, 0xDF, 0x50, 0x43, 0x46, 0x58, 0x43, 0x61, 0x72, 0x64, 0x80,
0x00, 0x01, 0x01, 0x00, 0x01, 0x40, 0x00, 0x00, 0x01, 0xF9, 0x03, 0x00,
0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
};
static const uint8 ExBRInit80[] = { 0xF9, 0xFF, 0xFF };
memcpy(ExBackupRAM + 0x00, ExBRInit00, sizeof(ExBRInit00));
memcpy(ExBackupRAM + 0x80, ExBRInit80, sizeof(ExBRInit80));
}
// Default to 16-bit bus.
for (int i = 0; i < 256; i++)
{
PCFX_V810.SetMemReadBus32(i, FALSE);
PCFX_V810.SetMemWriteBus32(i, FALSE);
}
// 16MiB RAM area.
PCFX_V810.SetMemReadBus32(0, TRUE);
PCFX_V810.SetMemWriteBus32(0, TRUE);
// Bitstring read range
for (int i = 0xA0; i <= 0xAF; i++)
{
PCFX_V810.SetMemReadBus32(i, FALSE); // Reads to the read range are 16-bit, and
PCFX_V810.SetMemWriteBus32(i, TRUE); // writes are 32-bit.
}
// Bitstring write range
for (int i = 0xB0; i <= 0xBF; i++)
{
PCFX_V810.SetMemReadBus32(i, TRUE); // Reads to the write range are 32-bit,
PCFX_V810.SetMemWriteBus32(i, FALSE); // but writes are 16-bit!
}
// BIOS area
for (int i = 0xF0; i <= 0xFF; i++)
{
PCFX_V810.SetMemReadBus32(i, FALSE);
PCFX_V810.SetMemWriteBus32(i, FALSE);
}
PCFX_V810.SetMemReadHandlers(mem_rbyte, mem_rhword, mem_rword);
PCFX_V810.SetMemWriteHandlers(mem_wbyte, mem_whword, mem_wword);
PCFX_V810.SetIOReadHandlers(port_rbyte, port_rhword, NULL);
PCFX_V810.SetIOWriteHandlers(port_wbyte, port_whword, NULL);
return true;
}
static void DoMD5CDVoodoo(std::vector<CDIF *> *CDInterfaces)
{
static CDGameEntry GameList[] =
{
#include "mednafen/pcfx/gamedb.inc"
};
const CDGameEntry *found_entry = NULL;
TOC toc;
for (unsigned if_disc = 0; if_disc < CDInterfaces->size(); if_disc++)
{
(*CDInterfaces)[if_disc]->ReadTOC(&toc);
if (toc.first_track == 1)
{
for (unsigned int g = 0; g < sizeof(GameList) / sizeof(CDGameEntry); g++)
{
const CDGameEntry *entry = &GameList[g];
assert(entry->discs == 1 || entry->discs == 2);
for (unsigned int disc = 0; disc < entry->discs; disc++)
{
const CDGameEntryTrack *et = entry->tracks[disc];
bool GameFound = TRUE;
while(et->tracknum != -1 && GameFound)
{
assert(et->tracknum > 0 && et->tracknum < 100);
if (toc.tracks[et->tracknum].lba != et->lba)
GameFound = FALSE;
if ( ((et->format == CDGE_FORMAT_DATA) ? 0x4 : 0x0) != (toc.tracks[et->tracknum].control & 0x4))
GameFound = FALSE;
et++;
}
if (et->tracknum == -1)
{
if ((et - 1)->tracknum != toc.last_track)
GameFound = FALSE;
if (et->lba != toc.tracks[100].lba)
GameFound = FALSE;
}
if (GameFound)
{
found_entry = entry;
goto FoundIt;
}
} // End disc count loop
}
}
FoundIt: ;
if (found_entry)
{
EmuFlags = found_entry->flags;
if (found_entry->discs > 1)
{
const char *hash_prefix = "Mednafen PC-FX Multi-Game Set";
md5_context md5_gameset;
mednafen_md5_starts(&md5_gameset);
mednafen_md5_update(&md5_gameset, (uint8_t*)hash_prefix, strlen(hash_prefix));
for (unsigned int disc = 0; disc < found_entry->discs; disc++)
{
const CDGameEntryTrack *et = found_entry->tracks[disc];
while(et->tracknum)
{
mednafen_md5_update_u32_as_lsb(&md5_gameset, et->tracknum);
mednafen_md5_update_u32_as_lsb(&md5_gameset, (uint32)et->format);
mednafen_md5_update_u32_as_lsb(&md5_gameset, et->lba);
if (et->tracknum == -1)
break;
et++;
}
}
}
break;
}
} // end: for (unsigned if_disc = 0; if_disc < CDInterfaces->size(); if_disc++)
}
static int LoadCD(std::vector<CDIF *> *CDInterfaces)
{
EmuFlags = 0;
cdifs = CDInterfaces;
DoMD5CDVoodoo(CDInterfaces);
if (!LoadCommon(CDInterfaces))
return 0;
PCFX_Power();
return 1;
}
static void PCFX_CDInsertEject(void)
{
CD_TrayOpen = !CD_TrayOpen;
#if 0
for (unsigned disc = 0; disc < cdifs->size(); disc++)
{
if (!(*cdifs)[disc]->Eject(CD_TrayOpen))
{
MDFN_DispMessage("Eject error.");
CD_TrayOpen = !CD_TrayOpen;
}
}
#endif
if (CD_TrayOpen)
MDFN_DispMessage("Virtual CD Drive Tray Open");
else
MDFN_DispMessage("Virtual CD Drive Tray Closed");
SCSICD_SetDisc(CD_TrayOpen, (CD_SelectedDisc >= 0 && !CD_TrayOpen) ? (*cdifs)[CD_SelectedDisc] : NULL);
}
static void PCFX_CDEject(void)
{
if (!CD_TrayOpen)
PCFX_CDInsertEject();
}
static void PCFX_CDSelect(void)
{
if (cdifs && CD_TrayOpen)
{
CD_SelectedDisc = (CD_SelectedDisc + 1) % (cdifs->size() + 1);
if ((unsigned)CD_SelectedDisc == cdifs->size())
CD_SelectedDisc = -1;
if (CD_SelectedDisc == -1)
MDFN_DispMessage("Disc absence selected.");
else
MDFN_DispMessage("Disc %d of %d selected.", CD_SelectedDisc + 1, (int)cdifs->size());
}
}
static void CloseGame(void)
{
unsigned i;
for (i = 0; i < 2; i++)
{
if (fx_vdc_chips[i])
{
delete fx_vdc_chips[i];
fx_vdc_chips[i] = NULL;
}
}
RAINBOW_Close();
KING_Close();
SoundBox_Kill();
PCFX_V810.Kill();
// The allocated memory RAM and BIOSROM is free'd in V810_Kill()
RAM = NULL;
BIOSROM = NULL;
}
static void DoSimpleCommand(int cmd)
{
switch(cmd)
{
case MDFN_MSC_INSERT_DISK: PCFX_CDInsertEject(); break;
case MDFN_MSC_SELECT_DISK: PCFX_CDSelect(); break;
case MDFN_MSC_EJECT_DISK: PCFX_CDEject(); break;
case MDFN_MSC_RESET: PCFX_Reset(); break;
case MDFN_MSC_POWER: PCFX_Power(); break;
}
}
extern "C" int StateAction(StateMem *sm, int load, int data_only)
{
const v810_timestamp_t timestamp = PCFX_V810.v810_timestamp;
SFORMAT StateRegs[] =
{
SFARRAY(RAM, 0x200000),
SFARRAY16(Last_VDC_AR, 2),
SFVAR(RAM_LPA),
SFVAR(BackupControl),
SFVAR(ExBusReset),
SFARRAY(BackupRAM, BRAMDisabled ? 0 : 0x8000), //SFPTR8
SFARRAY(ExBackupRAM, BRAMDisabled ? 0 : 0x8000), //SFPTR8
// SFVAR(CD_TrayOpen),
// SFVAR(CD_SelectedDisc),
SFEND
};
int ret = MDFNSS_StateAction(sm, load, data_only, StateRegs, "MAIN", false);
for (int i = 0; i < 2; i++)
ret &= fx_vdc_chips[i]->StateAction(sm, load, data_only, i ? "VDC1" : "VDC0");
ret &= FXINPUT_StateAction(sm, load, data_only);
ret &= PCFXIRQ_StateAction(sm, load, data_only);
ret &= KING_StateAction(sm, load, data_only);
ret &= PCFX_V810.StateAction(sm, load, data_only);
ret &= FXTIMER_StateAction(sm, load, data_only);
ret &= SoundBox_StateAction(sm, load, data_only);
ret &= SCSICD_StateAction(sm, load, data_only, "CDRM");
ret &= RAINBOW_StateAction(sm, load, data_only);
if (load)
{
//
// Rather than bothering to store next event timestamp deltas in save states, we'll just recalculate next event times on save state load as a side effect
// of this call.
//
ForceEventUpdates(timestamp);
if (cdifs)
{
// Sanity check.
if (CD_SelectedDisc >= (int)cdifs->size())
CD_SelectedDisc = (int)cdifs->size() - 1;
SCSICD_SetDisc(CD_TrayOpen, (CD_SelectedDisc >= 0 && !CD_TrayOpen) ? (*cdifs)[CD_SelectedDisc] : NULL, true);
}
}
return(ret);
}
MDFNGI EmulatedPCFX =
{
0, // lcm_width
0, // lcm_height
NULL, // Dummy
288, // Nominal width
240, // Nominal height
1024, // Framebuffer width
512, // Framebuffer height
};
#ifdef NEED_DEINTERLACER
static bool PrevInterlaced;
static Deinterlacer deint;
#endif
#define MEDNAFEN_CORE_NAME_MODULE "pcfx"
#define MEDNAFEN_CORE_NAME "Beetle PC-FX"
#define MEDNAFEN_CORE_VERSION "v0.9.36.5"
#define MEDNAFEN_CORE_EXTENSIONS "cue|ccd|toc|chd"
#define MEDNAFEN_CORE_TIMING_FPS 59.94
#define MEDNAFEN_CORE_GEOMETRY_BASE_W (EmulatedPCFX.nominal_width)
#define MEDNAFEN_CORE_GEOMETRY_BASE_H (EmulatedPCFX.nominal_height)
#define MEDNAFEN_CORE_GEOMETRY_MAX_W 1024
#define MEDNAFEN_CORE_GEOMETRY_MAX_H 480
#define MEDNAFEN_CORE_GEOMETRY_ASPECT_RATIO (4.0 / 3.0)
#define FB_WIDTH 1024
#define FB_HEIGHT 480
#define FB_MAX_HEIGHT FB_HEIGHT
static bool cdimagecache = false;
static std::vector<CDIF *> CDInterfaces; // FIXME: Cleanup on error out.
// TODO: LoadCommon()
static void extract_basename(char *buf, const char *path, size_t size)
{
const char *base = strrchr(path, '/');
if (!base)
base = strrchr(path, '\\');
if (!base)
base = path;
if (*base == '\\' || *base == '/')
base++;
strncpy(buf, base, size - strlen(buf) - 1);
buf[size - 1] = '\0';
char *ext = strrchr(buf, '.');
if (ext)
*ext = '\0';
}
static void extract_directory(char *buf, const char *path, size_t size)
{
strncpy(buf, path, size - 1);
buf[size - 1] = '\0';
char *base = strrchr(buf, '/');
if (!base)
base = strrchr(buf, '\\');
if (base)
*base = '\0';
else
buf[0] = '\0';
}
//
// Disk Interface
//
static bool disk_set_eject_state( bool ejected )
{
if ( ejected == cd_eject_state )
return false;
cd_eject_state = ejected;
DoSimpleCommand(ejected ? MDFN_MSC_EJECT_DISK : MDFN_MSC_INSERT_DISK);
return true;
}
static bool disk_get_eject_state(void)
{
return cd_eject_state;
}
static bool disk_set_image_index(unsigned index)
{
// only listen if the tray is open
if (cd_eject_state)
{
CD_SelectedDisc = index;
if (CD_SelectedDisc > CDInterfaces.size())
CD_SelectedDisc = CDInterfaces.size();
// Very hacky. CDSelect command will want to increment first.
CD_SelectedDisc--;
DoSimpleCommand(MDFN_MSC_SELECT_DISK);
return true;
}
return false;
}
static unsigned disk_get_num_images(void)
{
return CDInterfaces.size();
}
static unsigned disk_get_image_index(void)
{
return CD_SelectedDisc;
}
static bool disk_replace_image_index(unsigned index, const struct retro_game_info *info)
{
return false;
}
static bool disk_add_image_index(void)
{
return true;
}
static bool disk_set_initial_image(unsigned index, const char *path)
{
if (string_is_empty(path))
return false;
disk_control_ext_info.initial_index = index;
disk_control_ext_info.initial_path = path;
return true;
}
static bool disk_get_image_path(unsigned index, char *path, size_t len)
{
if (len < 1)
return false;
if ((index < disk_get_num_images()) &&
(index < disk_control_ext_info.image_paths.size()))
{
if (!string_is_empty(disk_control_ext_info.image_paths[index].c_str()))
{
strlcpy(path, disk_control_ext_info.image_paths[index].c_str(), len);
return true;
}
}
return false;
}
static bool disk_get_image_label(unsigned index, char *label, size_t len)
{
if (len < 1)
return false;
if ((index < disk_get_num_images()) &&
(index < disk_control_ext_info.image_labels.size()))
{
if (!string_is_empty(disk_control_ext_info.image_labels[index].c_str()))
{
strlcpy(label, disk_control_ext_info.image_labels[index].c_str(), len);
return true;
}
}
return false;
}
static struct retro_disk_control_callback disk_interface =
{
disk_set_eject_state,
disk_get_eject_state,
disk_get_image_index,
disk_set_image_index,
disk_get_num_images,
disk_replace_image_index,
disk_add_image_index,
};
static struct retro_disk_control_ext_callback disk_interface_ext =
{
disk_set_eject_state,
disk_get_eject_state,
disk_get_image_index,
disk_set_image_index,
disk_get_num_images,
disk_replace_image_index,
disk_add_image_index,
disk_set_initial_image,
disk_get_image_path,
disk_get_image_label,
};
static void disc_clear(void)
{
disk_control_ext_info.initial_index = 0;
disk_control_ext_info.initial_path.clear();
disk_control_ext_info.image_paths.clear();
disk_control_ext_info.image_labels.clear();
}
static void disc_init(void)
{
unsigned dci_version = 0;
cd_eject_state = false;
if (environ_cb(RETRO_ENVIRONMENT_GET_DISK_CONTROL_INTERFACE_VERSION, &dci_version) && (dci_version >= 1))
environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_EXT_INTERFACE, &disk_interface_ext);
else
environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_interface);
disc_clear();
}
static void check_system_specs(void)
{
unsigned level = 15;
environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
}
void retro_init(void)
{
struct retro_log_callback log;
const char *dir = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &log))
log_cb = log.log;
else
log_cb = NULL;
disc_init(); // Initialize disc control interface
CDUtility_Init();