-
Notifications
You must be signed in to change notification settings - Fork 43
/
libretro.cpp
2488 lines (1984 loc) · 61.3 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 "mednafen/git.h"
#include "mednafen/mempatcher.h"
#include "mednafen/git.h"
#include "mednafen/general.h"
#include <compat/msvc.h>
#ifdef NEED_DEINTERLACER
#include "mednafen/video/Deinterlacer.h"
#endif
#include "libretro.h"
#include <rthreads/rthreads.h>
#include <string/stdstring.h>
#include "libretro_cbs.h"
#include "libretro_core_options.h"
#include "libretro_settings.h"
#include "input.h"
#include "disc.h"
#include "mednafen/settings.h"
#include "mednafen/cdrom/cdromif.h"
#include "mednafen/FileStream.h"
#include "mednafen/hash/sha256.h"
#include "mednafen/hash/md5.h"
#include "mednafen/ss/ss.h"
/* Forward declarations */
void MDFN_LoadGameCheats(void *override_ptr);
void MDFN_FlushGameCheats(int nosave);
static INLINE bool DBG_NeedCPUHooks(void) { return false; } // <-- replaces debug.inc
#include <ctype.h>
#include <time.h>
#include <bitset>
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_audio_sample_t audio_cb;
static retro_audio_sample_batch_t audio_batch_cb;
static retro_input_poll_t input_poll_cb;
static retro_input_state_t input_state_cb;
static retro_environment_t environ_cb;
void MDFN_DispMessage(const char *format, ...)
{
va_list ap;
struct retro_message msg;
const char *strc = NULL;
char *str = (char*)malloc(4096 * sizeof(char));
va_start(ap,format);
vsnprintf(str, 4096, format, ap);
va_end(ap);
strc = str;
msg.frames = 180;
msg.msg = strc;
environ_cb(RETRO_ENVIRONMENT_SET_MESSAGE, &msg);
free(str);
}
#define MEDNAFEN_CORE_NAME "Beetle Saturn"
#define MEDNAFEN_CORE_VERSION "v1.22.2"
#define MEDNAFEN_CORE_VERSION_NUMERIC 0x00102202
#define MEDNAFEN_CORE_EXTENSIONS "cue|ccd|chd|toc|m3u"
#define MEDNAFEN_CORE_TIMING_FPS 59.82
#define MEDNAFEN_CORE_GEOMETRY_BASE_W 320
#define MEDNAFEN_CORE_GEOMETRY_BASE_H 240
#define MEDNAFEN_CORE_GEOMETRY_MAX_W 704
#define MEDNAFEN_CORE_GEOMETRY_MAX_H 576
#define MEDNAFEN_CORE_GEOMETRY_ASPECT_RATIO (4.0 / 3.0)
#define FB_WIDTH MEDNAFEN_CORE_GEOMETRY_MAX_W
static unsigned frame_count = 0;
static unsigned internal_frame_count = 0;
static bool failed_init = false;
static unsigned image_offset = 0;
static unsigned image_crop = 0;
static unsigned h_mask = 0;
static unsigned first_sl = 0;
static unsigned last_sl = 239;
static unsigned first_sl_pal = 0;
static unsigned last_sl_pal = 287;
static bool is_pal = false;
// Sets how often (in number of output frames/retro_run invocations)
// the internal framerace counter should be updated if
// display_internal_framerate is true.
#define INTERNAL_FPS_SAMPLE_PERIOD 32
static char retro_cd_base_directory[4096];
static char retro_cd_path[4096];
extern "C" char retro_cd_base_name[4096];
extern "C" char retro_save_directory[4096];
extern "C" char retro_base_directory[4096];
#ifdef _WIN32
static char retro_slash = '\\';
#else
static char retro_slash = '/';
#endif
static bool libretro_supports_option_categories = false;
static bool libretro_supports_bitmasks = false;
extern MDFNGI EmulatedSS;
MDFNGI *MDFNGameInfo = NULL;
#include "../MemoryStream.h"
#include "mednafen/ss/ss.h"
#include "mednafen/ss/sound.h"
#include "mednafen/ss/scsp.h"
#include "mednafen/ss/smpc.h"
#include "mednafen/ss/cdb.h"
#include "mednafen/ss/vdp1.h"
#include "mednafen/ss/vdp2.h"
#include "mednafen/ss/scu.h"
#include "mednafen/ss/cart.h"
#include "mednafen/ss/db.h"
static sscpu_timestamp_t MidSync(const sscpu_timestamp_t timestamp);
static bool NeedEmuICache;
static const uint8 BRAM_Init_Data[0x10] = { 0x42, 0x61, 0x63, 0x6b, 0x55, 0x70, 0x52, 0x61, 0x6d, 0x20, 0x46, 0x6f, 0x72, 0x6d, 0x61, 0x74 };
static void SaveBackupRAM(void);
static void LoadBackupRAM(void);
static void SaveCartNV(void);
static void LoadCartNV(void);
static void SaveRTC(void);
static void LoadRTC(void);
static MDFN_COLD void BackupBackupRAM(void);
static MDFN_COLD void BackupCartNV(void);
#include "mednafen/ss/sh7095.h"
static uint8 SCU_MSH2VectorFetch(void);
static uint8 SCU_SSH2VectorFetch(void);
static void CheckEventsByMemTS(void);
SH7095 CPU[2]{ {"SH2-M", SS_EVENT_SH2_M_DMA, SCU_MSH2VectorFetch}, {"SH2-S", SS_EVENT_SH2_S_DMA, SCU_SSH2VectorFetch}};
static uint16 BIOSROM[524288 / sizeof(uint16)];
#define WORKRAM_BANK_SIZE_BYTES (1024*1024)
static uint8 WorkRAM[2*WORKRAM_BANK_SIZE_BYTES]; // unified 2MB work ram for linear access.
// Effectively 32-bit in reality, but 16-bit here because of CPU interpreter design(regarding fastmap).
static uint16* WorkRAML = (uint16*)(WorkRAM + (WORKRAM_BANK_SIZE_BYTES*0));
static uint16* WorkRAMH = (uint16*)(WorkRAM + (WORKRAM_BANK_SIZE_BYTES*1));
static uint8 BackupRAM[32768];
static bool BackupRAM_Dirty;
static int64 BackupRAM_SaveDelay;
static int64 CartNV_SaveDelay;
#define SH7095_EXT_MAP_GRAN_BITS 16
static uintptr_t SH7095_FastMap[1U << (32 - SH7095_EXT_MAP_GRAN_BITS)];
int32 SH7095_mem_timestamp;
uint32 SH7095_BusLock;
static uint32 SH7095_DB;
#include "mednafen/ss/scu.inc"
static sha256_digest BIOS_SHA256; // SHA-256 hash of the currently-loaded BIOS; used for save state sanity checks.
static std::bitset<1U << (27 - SH7095_EXT_MAP_GRAN_BITS)> FMIsWriteable;
template<typename T>
static void INLINE SH7095_BusWrite(uint32 A, T V, const bool BurstHax, int32* SH2DMAHax);
template<typename T>
static T INLINE SH7095_BusRead(uint32 A, const bool BurstHax, int32* SH2DMAHax);
/*
SH-2 external bus address map:
CS0: 0x00000000...0x01FFFFFF (16-bit)
0x00000000...0x000FFFFF: BIOS ROM (R)
0x00100000...0x0017FFFF: SMPC (R/W; 8-bit mapped as 16-bit)
0x00180000...0x001FFFFF: Backup RAM(32KiB) (R/W; 8-bit mapped as 16-bit)
0x00200000...0x003FFFFF: Low RAM(1MiB) (R/W)
0x01000000...0x017FFFFF: Slave FRT Input Capture Trigger (W)
0x01800000...0x01FFFFFF: Master FRT Input Capture Trigger (W)
CS1: 0x02000000...0x03FFFFFF (SCU managed)
0x02000000...0x03FFFFFF: A-bus CS0 (R/W)
CS2: 0x04000000...0x05FFFFFF (SCU managed)
0x04000000...0x04FFFFFF: A-bus CS1 (R/W)
0x05000000...0x057FFFFF: A-bus Dummy
0x05800000...0x058FFFFF: A-bus CS2 (R/W)
0x05A00000...0x05AFFFFF: SCSP RAM (R/W)
0x05B00000...0x05BFFFFF: SCSP Registers (R/W)
0x05C00000...0x05C7FFFF: VDP1 VRAM (R/W)
0x05C80000...0x05CFFFFF: VDP1 FB RAM (R/W; swappable between two framebuffers, but may be temporarily unreadable at swap time)
0x05D00000...0x05D7FFFF: VDP1 Registers (R/W)
0x05E00000...0x05EFFFFF: VDP2 VRAM (R/W)
0x05F00000...0x05F7FFFF: VDP2 CRAM (R/W; 8-bit writes are illegal)
0x05F80000...0x05FBFFFF: VDP2 Registers (R/W; 8-bit writes are illegal)
0x05FE0000...0x05FEFFFF: SCU Registers (R/W)
0x05FF0000...0x05FFFFFF: SCU Debug/Test Registers (R/W)
CS3: 0x06000000...0x07FFFFFF
0x06000000...0x07FFFFFF: High RAM/SDRAM(1MiB) (R/W)
*/
//
// Never add anything to SH7095_mem_timestamp when DMAHax is true.
//
// When BurstHax is true and we're accessing high work RAM, don't add anything.
//
template<typename T, bool IsWrite>
static INLINE void BusRW_DB_CS0(const uint32 A, uint32& DB, const bool BurstHax, int32* SH2DMAHax)
{
//
// Low(and kinda slow) work RAM
//
if(A >= 0x00200000 && A <= 0x003FFFFF)
{
if(IsWrite)
ne16_wbo_be<T>(WorkRAML, A & 0xFFFFF, DB >> (((A & 1) ^ (2 - sizeof(T))) << 3));
else
DB = (DB & 0xFFFF0000) | ne16_rbo_be<uint16>(WorkRAML, A & 0xFFFFE);
if(!SH2DMAHax)
SH7095_mem_timestamp += 7;
else
*SH2DMAHax -= 7;
return;
}
//
// BIOS ROM
//
if(A >= 0x00000000 && A <= 0x000FFFFF)
{
if(!SH2DMAHax)
SH7095_mem_timestamp += 8;
else
*SH2DMAHax -= 8;
if(!IsWrite)
DB = (DB & 0xFFFF0000) | ne16_rbo_be<uint16>(BIOSROM, A & 0x7FFFE);
return;
}
//
// SMPC
//
if(A >= 0x00100000 && A <= 0x0017FFFF)
{
const uint32 SMPC_A = (A & 0x7F) >> 1;
if(!SH2DMAHax)
{
// SH7095_mem_timestamp += 2;
CheckEventsByMemTS();
}
if(IsWrite)
{
if(sizeof(T) == 2 || (A & 1))
SMPC_Write(SH7095_mem_timestamp, SMPC_A, DB);
}
else
DB = (DB & 0xFFFF0000) | 0xFF00 | SMPC_Read(SH7095_mem_timestamp, SMPC_A);
return;
}
//
// Backup RAM
//
if(A >= 0x00180000 && A <= 0x001FFFFF)
{
if(!SH2DMAHax)
SH7095_mem_timestamp += 8;
else
*SH2DMAHax -= 8;
if(IsWrite)
{
if(sizeof(T) != 1 || (A & 1))
{
BackupRAM[(A >> 1) & 0x7FFF] = DB;
BackupRAM_Dirty = true;
}
}
else
DB = (DB & 0xFFFF0000) | 0xFF00 | BackupRAM[(A >> 1) & 0x7FFF];
return;
}
//
// FRT trigger region
//
if(A >= 0x01000000 && A <= 0x01FFFFFF)
{
if(!SH2DMAHax)
SH7095_mem_timestamp += 8;
else
*SH2DMAHax -= 8;
if(IsWrite)
{
if(sizeof(T) != 1)
{
const unsigned c = ((A >> 23) & 1) ^ 1;
if(!c || SMPC_IsSlaveOn())
{
CPU[c].SetFTI(true);
CPU[c].SetFTI(false);
}
}
}
return;
}
//
//
//
if(!SH2DMAHax)
SH7095_mem_timestamp += 4;
else
*SH2DMAHax -= 4;
if(IsWrite)
SS_DBG(SS_DBG_WARNING, "[SH2 BUS] Unknown %zu-byte write of 0x%08x to 0x%08x\n", sizeof(T), DB >> (((A & 1) ^ (2 - sizeof(T))) << 3), A);
else
SS_DBG(SS_DBG_WARNING, "[SH2 BUS] Unknown %zu-byte read from 0x%08x\n", sizeof(T), A);
}
template<typename T, bool IsWrite>
static INLINE void BusRW_DB_CS123(const uint32 A, uint32& DB, const bool BurstHax, int32* SH2DMAHax)
{
//
// CS3: High work RAM/SDRAM, 0x06000000 ... 0x07FFFFFF
//
if(A >= 0x06000000)
{
if(!IsWrite || sizeof(T) == 4)
ne16_rwbo_be<uint32, IsWrite>(WorkRAMH, A & 0xFFFFC, &DB);
else
ne16_wbo_be<T>(WorkRAMH, A & 0xFFFFF, DB >> (((A & 3) ^ (4 - sizeof(T))) << 3));
if(!BurstHax)
{
if(!SH2DMAHax)
{
if(IsWrite)
{
SH7095_mem_timestamp = (SH7095_mem_timestamp + 4) &~ 3;
}
else
{
SH7095_mem_timestamp += 7;
}
}
else
*SH2DMAHax -= IsWrite ? 3 : 6;
}
return;
}
//
// CS1 and CS2: SCU
//
if(!IsWrite)
DB = 0;
SCU_FromSH2_BusRW_DB<T, IsWrite>(A, &DB, SH2DMAHax);
}
template<typename T>
static void INLINE SH7095_BusWrite(uint32 A, T V, const bool BurstHax, int32* SH2DMAHax)
{
uint32 DB = SH7095_DB;
if(A < 0x02000000) // CS0, configured as 16-bit
{
if(sizeof(T) == 4)
{
// TODO/FIXME: Don't allow DMA transfers to occur between the two 16-bit accesses.
//if(!SH2DMAHax)
// SH7095_BusLock++;
DB = (DB & 0xFFFF0000) | (V >> 16);
BusRW_DB_CS0<uint16, true>(A, DB, BurstHax, SH2DMAHax);
DB = (DB & 0xFFFF0000) | (uint16)V;
BusRW_DB_CS0<uint16, true>(A | 2, DB, BurstHax, SH2DMAHax);
//if(!SH2DMAHax)
// SH7095_BusLock--;
}
else
{
const uint32 shift = ((A & 1) ^ (2 - sizeof(T))) << 3;
const uint32 mask = (0xFFFF >> ((2 - sizeof(T)) * 8)) << shift;
DB = (DB & ~mask) | (V << shift);
BusRW_DB_CS0<T, true>(A, DB, BurstHax, SH2DMAHax);
}
}
else // CS1, CS2, CS3; 32-bit
{
const uint32 shift = ((A & 3) ^ (4 - sizeof(T))) << 3;
const uint32 mask = (0xFFFFFFFF >> ((4 - sizeof(T)) * 8)) << shift;
DB = (DB & ~mask) | (V << shift); // //ne32_wbo_be<T>(&DB, A & 0x3, V);
BusRW_DB_CS123<T, true>(A, DB, BurstHax, SH2DMAHax);
}
SH7095_DB = DB;
}
template<typename T>
static T INLINE SH7095_BusRead(uint32 A, const bool BurstHax, int32* SH2DMAHax)
{
uint32 DB = SH7095_DB;
T ret;
if(A < 0x02000000) // CS0, configured as 16-bit
{
if(sizeof(T) == 4)
{
// TODO/FIXME: Don't allow DMA transfers to occur between the two 16-bit accesses.
//if(!SH2DMAHax)
// SH7095_BusLock++;
BusRW_DB_CS0<uint16, false>(A, DB, BurstHax, SH2DMAHax);
ret = DB << 16;
BusRW_DB_CS0<uint16, false>(A | 2, DB, BurstHax, SH2DMAHax);
ret |= (uint16)DB;
//if(!SH2DMAHax)
// SH7095_BusLock--;
}
else
{
BusRW_DB_CS0<T, false>(A, DB, BurstHax, SH2DMAHax);
ret = DB >> (((A & 1) ^ (2 - sizeof(T))) << 3);
}
}
else // CS1, CS2, CS3; 32-bit
{
BusRW_DB_CS123<T, false>(A, DB, BurstHax, SH2DMAHax);
ret = DB >> (((A & 3) ^ (4 - sizeof(T))) << 3);
// SDRAM leaves data bus in a weird state after read...
//if(A >= 0x06000000)
// DB = 0;
}
SH7095_DB = DB;
return ret;
}
//
//
//
static MDFN_COLD uint8 CheatMemRead(uint32 A)
{
A &= (1U << 27) - 1;
return ne16_rbo_be<uint8>(SH7095_FastMap[A >> SH7095_EXT_MAP_GRAN_BITS], A);
}
static MDFN_COLD void CheatMemWrite(uint32 A, uint8 V)
{
A &= (1U << 27) - 1;
if(FMIsWriteable[A >> SH7095_EXT_MAP_GRAN_BITS])
{
ne16_wbo_be<uint8>(SH7095_FastMap[A >> SH7095_EXT_MAP_GRAN_BITS], A, V);
for(unsigned c = 0; c < 2; c++)
{
if(CPU[c].CCR & SH7095::CCR_CE)
{
for(uint32 Abase = 0x00000000; Abase < 0x20000000; Abase += 0x08000000)
{
CPU[c].Write_UpdateCache<uint8>(Abase + A, V);
}
}
}
}
}
//
//
//
static void SetFastMemMap(uint32 Astart, uint32 Aend, uint16* ptr, uint32 length, bool is_writeable)
{
const uint64 Abound = (uint64)Aend + 1;
assert((Astart & ((1U << SH7095_EXT_MAP_GRAN_BITS) - 1)) == 0);
assert((Abound & ((1U << SH7095_EXT_MAP_GRAN_BITS) - 1)) == 0);
assert((length & ((1U << SH7095_EXT_MAP_GRAN_BITS) - 1)) == 0);
assert(length > 0);
assert(length <= (Abound - Astart));
for(uint64 A = Astart; A < Abound; A += (1U << SH7095_EXT_MAP_GRAN_BITS))
{
uintptr_t tmp = (uintptr_t)ptr + ((A - Astart) % length);
if(A < (1U << 27))
FMIsWriteable[A >> SH7095_EXT_MAP_GRAN_BITS] = is_writeable;
SH7095_FastMap[A >> SH7095_EXT_MAP_GRAN_BITS] = tmp - A;
}
}
static uint16 fmap_dummy[(1U << SH7095_EXT_MAP_GRAN_BITS) / sizeof(uint16)];
static MDFN_COLD void InitFastMemMap(void)
{
for(unsigned i = 0; i < sizeof(fmap_dummy) / sizeof(fmap_dummy[0]); i++)
{
fmap_dummy[i] = 0;
}
FMIsWriteable.reset();
MDFNMP_Init(1ULL << SH7095_EXT_MAP_GRAN_BITS, (1ULL << 27) / (1ULL << SH7095_EXT_MAP_GRAN_BITS));
for(uint64 A = 0; A < 1ULL << 32; A += (1U << SH7095_EXT_MAP_GRAN_BITS))
{
SH7095_FastMap[A >> SH7095_EXT_MAP_GRAN_BITS] = (uintptr_t)fmap_dummy - A;
}
}
void SS_SetPhysMemMap(uint32 Astart, uint32 Aend, uint16* ptr, uint32 length, bool is_writeable)
{
assert(Astart < 0x20000000);
assert(Aend < 0x20000000);
if(!ptr)
{
ptr = fmap_dummy;
length = sizeof(fmap_dummy);
}
for(uint32 Abase = 0; Abase < 0x40000000; Abase += 0x20000000)
SetFastMemMap(Astart + Abase, Aend + Abase, ptr, length, is_writeable);
}
#include "mednafen/ss/sh7095.inc"
static bool Running;
event_list_entry events[SS_EVENT__COUNT];
static sscpu_timestamp_t next_event_ts;
template<unsigned c>
static sscpu_timestamp_t SH_DMA_EventHandler(sscpu_timestamp_t et)
{
if(et < SH7095_mem_timestamp)
return SH7095_mem_timestamp;
// Must come after the (et < SH7095_mem_timestamp) check.
if(MDFN_UNLIKELY(SH7095_BusLock))
return et + 1;
return CPU[c].DMA_Update(et);
}
//
//
//
static MDFN_COLD void InitEvents(void)
{
for(unsigned i = 0; i < SS_EVENT__COUNT; i++)
{
if(i == SS_EVENT__SYNFIRST)
events[i].event_time = 0;
else if(i == SS_EVENT__SYNLAST)
events[i].event_time = 0x7FFFFFFF;
else
events[i].event_time = 0; //SS_EVENT_DISABLED_TS;
events[i].prev = (i > 0) ? &events[i - 1] : NULL;
events[i].next = (i < (SS_EVENT__COUNT - 1)) ? &events[i + 1] : NULL;
}
events[SS_EVENT_SH2_M_DMA].event_handler = &SH_DMA_EventHandler<0>;
events[SS_EVENT_SH2_S_DMA].event_handler = &SH_DMA_EventHandler<1>;
events[SS_EVENT_SCU_DMA].event_handler = SCU_UpdateDMA;
events[SS_EVENT_SCU_DSP].event_handler = SCU_UpdateDSP;
events[SS_EVENT_SMPC].event_handler = SMPC_Update;
events[SS_EVENT_VDP1].event_handler = VDP1::Update;
events[SS_EVENT_VDP2].event_handler = VDP2::Update;
events[SS_EVENT_CDB].event_handler = CDB_Update;
events[SS_EVENT_SOUND].event_handler = SOUND_Update;
events[SS_EVENT_CART].event_handler = CART_GetEventHandler();
events[SS_EVENT_MIDSYNC].event_handler = MidSync;
events[SS_EVENT_MIDSYNC].event_time = SS_EVENT_DISABLED_TS;
}
static void RebaseTS(const sscpu_timestamp_t timestamp)
{
for(unsigned i = 0; i < SS_EVENT__COUNT; i++)
{
if(i == SS_EVENT__SYNFIRST || i == SS_EVENT__SYNLAST)
continue;
assert(events[i].event_time > timestamp);
if(events[i].event_time != SS_EVENT_DISABLED_TS)
events[i].event_time -= timestamp;
}
next_event_ts = events[SS_EVENT__SYNFIRST].next->event_time;
}
void SS_SetEventNT(event_list_entry* e, const sscpu_timestamp_t next_timestamp)
{
if(next_timestamp < e->event_time)
{
event_list_entry *fe = e;
do
{
fe = fe->prev;
} while(next_timestamp < fe->event_time);
// Remove this event from the list, temporarily of course.
e->prev->next = e->next;
e->next->prev = e->prev;
// Insert into the list, just after "fe".
e->prev = fe;
e->next = fe->next;
fe->next->prev = e;
fe->next = e;
e->event_time = next_timestamp;
}
else if(next_timestamp > e->event_time)
{
event_list_entry *fe = e;
do
{
fe = fe->next;
} while(next_timestamp > fe->event_time);
// Remove this event from the list, temporarily of course
e->prev->next = e->next;
e->next->prev = e->prev;
// Insert into the list, just BEFORE "fe".
e->prev = fe->prev;
e->next = fe;
fe->prev->next = e;
fe->prev = e;
e->event_time = next_timestamp;
}
next_event_ts = (Running ? events[SS_EVENT__SYNFIRST].next->event_time : 0);
}
// Called from debug.cpp too.
void ForceEventUpdates(const sscpu_timestamp_t timestamp)
{
CPU[0].ForceInternalEventUpdates();
if(SMPC_IsSlaveOn())
CPU[1].ForceInternalEventUpdates();
for(unsigned evnum = SS_EVENT__SYNFIRST + 1; evnum < SS_EVENT__SYNLAST; evnum++)
{
if(events[evnum].event_time != SS_EVENT_DISABLED_TS)
SS_SetEventNT(&events[evnum], events[evnum].event_handler(timestamp));
}
next_event_ts = (Running ? events[SS_EVENT__SYNFIRST].next->event_time : 0);
}
static INLINE bool EventHandler(const sscpu_timestamp_t timestamp)
{
event_list_entry *e = NULL;
while(timestamp >= (e = events[SS_EVENT__SYNFIRST].next)->event_time) // If Running = 0, EventHandler() may be called even if there isn't an event per-se, so while() instead of do { ... } while
{
sscpu_timestamp_t nt;
nt = e->event_handler(e->event_time);
SS_SetEventNT(e, nt);
}
return(Running);
}
static void CheckEventsByMemTS_Sub(void)
{
EventHandler(SH7095_mem_timestamp);
}
static void CheckEventsByMemTS(void)
{
if(MDFN_UNLIKELY(SH7095_mem_timestamp >= next_event_ts))
CheckEventsByMemTS_Sub();
}
void SS_RequestMLExit(void)
{
Running = 0;
next_event_ts = 0;
}
#pragma GCC push_options
#if !defined(__clang__) && defined(__GNUC__) && __GNUC__ < 5
// gcc 5.3.0 and 6.1.0 produce some braindead code for the big switch() statement at -Os.
#pragma GCC optimize("Os,no-unroll-loops,no-peel-loops,no-crossjumping")
#else
#pragma GCC optimize("O2,no-unroll-loops,no-peel-loops,no-crossjumping")
#endif
template<bool EmulateICache, bool DebugMode>
static int32 NO_INLINE RunLoop(EmulateSpecStruct* espec)
{
sscpu_timestamp_t eff_ts = 0;
do
{
do
{
CPU[0].Step<0, EmulateICache, DebugMode>();
CPU[0].DMA_BusTimingKludge();
while(MDFN_LIKELY(CPU[0].timestamp > CPU[1].timestamp))
{
CPU[1].Step<1, EmulateICache, DebugMode>();
}
eff_ts = CPU[0].timestamp;
if(SH7095_mem_timestamp > eff_ts)
eff_ts = SH7095_mem_timestamp;
else
SH7095_mem_timestamp = eff_ts;
} while(MDFN_LIKELY(eff_ts < next_event_ts));
} while(MDFN_LIKELY(EventHandler(eff_ts)));
return eff_ts;
}
#pragma GCC pop_options
// Must not be called within an event or read/write handler.
void SS_Reset(bool powering_up)
{
SH7095_BusLock = 0;
if(powering_up)
{
memset(WorkRAM, 0x00, sizeof(WorkRAM)); // TODO: Check real hardware
}
if(powering_up)
{
CPU[0].TruePowerOn();
CPU[1].TruePowerOn();
}
SCU_Reset(powering_up);
CPU[0].Reset(powering_up);
SMPC_Reset(powering_up);
VDP1::Reset(powering_up);
VDP2::Reset(powering_up);
CDB_Reset(powering_up);
SOUND_Reset(powering_up);
CART_Reset(powering_up);
}
static EmulateSpecStruct* espec;
static bool AllowMidSync;
static int32 cur_clock_div;
static int64 UpdateInputLastBigTS;
static INLINE void UpdateSMPCInput(const sscpu_timestamp_t timestamp)
{
SMPC_TransformInput();
int32 elapsed_time = (((int64)timestamp * cur_clock_div * 1000 * 1000) - UpdateInputLastBigTS) / (EmulatedSS.MasterClock / MDFN_MASTERCLOCK_FIXED(1));
UpdateInputLastBigTS += (int64)elapsed_time * (EmulatedSS.MasterClock / MDFN_MASTERCLOCK_FIXED(1));
SMPC_UpdateInput(elapsed_time);
}
static sscpu_timestamp_t MidSync(const sscpu_timestamp_t timestamp)
{
if(AllowMidSync)
{
//
// Don't call SOUND_Update() here, it's not necessary and will subtly alter emulation behavior from the perspective of the emulated program
// (which is not a problem in and of itself, but it's preferable to keep settings from altering emulation behavior when they don't need to).
//
//printf("MidSync: %d\n", VDP2::PeekLine());
//{
// espec->SoundBufSize += SOUND_FlushOutput();
// espec->MasterCycles = timestamp * cur_clock_div;
//}
//printf("%d\n", espec->SoundBufSize);
SMPC_UpdateOutput();
input_poll_cb();
input_update(libretro_supports_bitmasks, input_state_cb );
UpdateSMPCInput(timestamp);
AllowMidSync = false;
}
return SS_EVENT_DISABLED_TS;
}
static void Emulate(EmulateSpecStruct* espec_arg)
{
int32 end_ts;
espec = espec_arg;
AllowMidSync = setting_midsync;
cur_clock_div = SMPC_StartFrame(espec);
UpdateSMPCInput(0);
VDP2::StartFrame(espec, cur_clock_div == 61);
CART_SetCPUClock(EmulatedSS.MasterClock / MDFN_MASTERCLOCK_FIXED(1), cur_clock_div);
espec->SoundBufSize = 0;
espec->MasterCycles = 0;
espec->soundmultiplier = 1;
//
//
//
Running = true; // Set before ForceEventUpdates()
ForceEventUpdates(0);
#define RLTDAT false
static int32 (*const rltab[2][2])(EmulateSpecStruct*) =
{
// DebugMode=false DebugMode=true
{ RunLoop<false, false>, RunLoop<false, RLTDAT> }, // EmulateICache=false
{ RunLoop<true, false>, RunLoop<true, RLTDAT> }, // EmulateICache=true
};
#undef RLTDAT
end_ts = rltab[NeedEmuICache][DBG_NeedCPUHooks()](espec);
ForceEventUpdates(end_ts);
//
SMPC_EndFrame(espec, end_ts);
//
//
//
RebaseTS(end_ts);
CDB_ResetTS();
SOUND_ResetTS();
VDP1::AdjustTS(-end_ts);
VDP2::AdjustTS(-end_ts);
SMPC_ResetTS();
SCU_AdjustTS(-end_ts);
CART_AdjustTS(-end_ts);
UpdateInputLastBigTS -= (int64)end_ts * cur_clock_div * 1000 * 1000;
if(!(SH7095_mem_timestamp & 0x40000000)) // or maybe >= 0 instead?
SH7095_mem_timestamp -= end_ts;
CPU[0].AdjustTS(-end_ts);
if(SMPC_IsSlaveOn())
CPU[1].AdjustTS(-end_ts);
//
//
//
espec->MasterCycles = end_ts * cur_clock_div;
espec->SoundBufSize += SOUND_FlushOutput();
espec->NeedSoundReverse = false;
SMPC_UpdateOutput();
//
//
//
if(BackupRAM_Dirty)
{
BackupRAM_SaveDelay = (int64)3 * (EmulatedSS.MasterClock / MDFN_MASTERCLOCK_FIXED(1)); // 3 second delay
BackupRAM_Dirty = false;
}
else if(BackupRAM_SaveDelay > 0)
{
BackupRAM_SaveDelay -= espec->MasterCycles;
if(BackupRAM_SaveDelay <= 0)
{
try
{
SaveBackupRAM();
}
catch(std::exception& e)
{
BackupRAM_SaveDelay = (int64)60 * (EmulatedSS.MasterClock / MDFN_MASTERCLOCK_FIXED(1)); // 60 second retry delay.
}
}
}
if(CART_GetClearNVDirty())
CartNV_SaveDelay = (int64)3 * (EmulatedSS.MasterClock / MDFN_MASTERCLOCK_FIXED(1)); // 3 second delay
else if(CartNV_SaveDelay > 0)
{
CartNV_SaveDelay -= espec->MasterCycles;
if(CartNV_SaveDelay <= 0)
{
try
{
SaveCartNV();
}
catch(std::exception& e)
{
CartNV_SaveDelay = (int64)60 * (EmulatedSS.MasterClock / MDFN_MASTERCLOCK_FIXED(1)); // 60 second retry delay.
}
}
}
}
//
//
//
static MDFN_COLD void Cleanup(void)
{
CART_Kill();
VDP1::Kill();
VDP2::Kill();
SOUND_Kill();
CDB_Kill();
disc_cleanup();
}
typedef struct
{
const unsigned type;
const char *name;
} CartName;
uint32 ss_horrible_hacks;
static bool InitCommon(const unsigned cpucache_emumode, const unsigned cart_type, const unsigned smpc_area, const uint32 horrible_hacks )
{
//
unsigned i;
//
{
const struct
{
unsigned mode;
const char* name;
} CPUCacheEmuModes[] =
{
{ CPUCACHE_EMUMODE_DATA_CB, "Data only, with high-level bypass" },
{ CPUCACHE_EMUMODE_DATA, "Data only" },
{ CPUCACHE_EMUMODE_FULL, "Full" },
};
const char* cem = "Unknown";
for(auto const& ceme : CPUCacheEmuModes)
{
if(ceme.mode == cpucache_emumode)
{
cem = ceme.name;
break;
}