forked from libretro/beetle-psx-libretro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libretro.cpp
5103 lines (4186 loc) · 139 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 "mednafen/mednafen.h"
#include "mednafen/mempatcher.h"
#include "mednafen/git.h"
#include "mednafen/general.h"
#include "mednafen/md5.h"
#include <compat/msvc.h>
#include "mednafen/psx/gpu.h"
#ifdef NEED_DEINTERLACER
#include "mednafen/video/Deinterlacer.h"
#endif
#include <libretro.h>
#include <rthreads/rthreads.h>
#include <streams/file_stream.h>
#include <string/stdstring.h>
#include <rhash.h>
#include "ugui_tools.h"
#include "rsx/rsx_intf.h"
#include "libretro_cbs.h"
#include "beetle_psx_globals.h"
#include "libretro_options.h"
#include "input.h"
#include "parallel-psx/custom-textures/dbg_input_callback.h"
retro_input_state_t dbg_input_state_cb = 0;
#include "mednafen/mednafen-endian.h"
#include "mednafen/mednafen-types.h"
#include "mednafen/psx/psx.h"
#include "mednafen/error.h"
#include "pgxp/pgxp_main.h"
#include <vector>
#define ISHEXDEC ((codeLine[cursor]>='0') && (codeLine[cursor]<='9')) || ((codeLine[cursor]>='a') && (codeLine[cursor]<='f')) || ((codeLine[cursor]>='A') && (codeLine[cursor]<='F'))
#ifdef HAVE_LIGHTREC
#include <sys/mman.h>
#ifdef HAVE_ASHMEM
#include <sys/ioctl.h>
#include <linux/ashmem.h>
#endif
#if defined(HAVE_SHM) || defined(HAVE_ASHMEM)
#include <sys/stat.h>
#include <fcntl.h>
#endif
#ifdef HAVE_WIN_SHM
#include <windows.h>
#endif
#endif /* HAVE_LIGHTREC */
//Fast Save States exclude string labels from variables in the savestate, and are at least 20% faster.
extern bool FastSaveStates;
const int DEFAULT_STATE_SIZE = 16 * 1024 * 1024;
static bool libretro_supports_bitmasks = false;
static unsigned libretro_msg_interface_version = 0;
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 unsigned frame_count = 0;
static unsigned internal_frame_count = 0;
static bool display_internal_framerate = false;
static bool allow_frame_duping = false;
static bool failed_init = false;
static unsigned image_offset = 0;
static unsigned image_crop = 0;
static bool enable_memcard1 = false;
static bool enable_variable_serialization_size = false;
static int frame_width = 0;
static int frame_height = 0;
static bool gui_inited = false;
static bool gui_show = false;
static char bios_path[4096];
static bool firmware_found = false;
// Switchable memory cards
static int memcard_left_index = 0;
static int memcard_left_index_old;
static int memcard_right_index = 1;
static int memcard_right_index_old;
unsigned cd_2x_speedup = 1;
bool cd_async = false;
bool cd_warned_slow = false;
int64 cd_slow_timeout = 8000; // microseconds
// If true, PAL games will run at 60fps
bool fast_pal = false;
#ifdef HAVE_LIGHTREC
enum DYNAREC psx_dynarec;
bool psx_dynarec_invalidate;
uint8 psx_mmap = 0;
uint8 *psx_mem = NULL;
uint8 *psx_bios = NULL;
uint8 *psx_scratch = NULL;
#if defined(HAVE_ASHMEM)
int memfd;
#endif
#endif
uint32 EventCycles = 128;
// CPU overclock factor (or 0 if disabled)
int32_t psx_overclock_factor = 0;
// GPU rasterizer overclock shift
unsigned psx_gpu_overclock_shift = 0;
// 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 64
static int psx_skipbios;
bool psx_gte_overclock;
enum dither_mode psx_gpu_dither_mode;
//iCB: PGXP options
unsigned int psx_pgxp_mode;
unsigned int psx_pgxp_vertex_caching;
unsigned int psx_pgxp_texture_correction;
// \iCB
#define NEGCON_RANGE 0x7FFF
char retro_save_directory[4096];
char retro_base_directory[4096];
char retro_cd_base_directory[4096];
static char retro_cd_path[4096];
char retro_cd_base_name[4096];
#ifdef _WIN32
static char retro_slash = '\\';
#else
static char retro_slash = '/';
#endif
enum
{
REGION_JP = 0,
REGION_NA = 1,
REGION_EU = 2,
};
static bool firmware_is_present(unsigned region)
{
static const size_t list_size = 10;
const char *bios_name_list[list_size];
const char *bios_sha1 = NULL;
log_cb(RETRO_LOG_INFO, "Checking if required firmware is present...\n");
/* SHA1 and alternate BIOS names sourced from
https://github.com/mamedev/mame/blob/master/src/mame/drivers/psx.cpp */
if (region == REGION_JP)
{
bios_name_list[0] = "scph5500.bin";
bios_name_list[1] = "SCPH5500.bin";
bios_name_list[2] = "SCPH-5500.bin";
bios_name_list[3] = NULL;
bios_name_list[4] = NULL;
bios_name_list[5] = NULL;
bios_name_list[6] = NULL;
bios_name_list[7] = NULL;
bios_name_list[8] = NULL;
bios_name_list[9] = NULL;
bios_sha1 = "B05DEF971D8EC59F346F2D9AC21FB742E3EB6917";
}
else if (region == REGION_NA)
{
bios_name_list[0] = "scph5501.bin";
bios_name_list[1] = "SCPH5501.bin";
bios_name_list[2] = "SCPH-5501.bin";
bios_name_list[3] = "scph5503.bin";
bios_name_list[4] = "SCPH5503.bin";
bios_name_list[5] = "SCPH-5503.bin";
bios_name_list[6] = "scph7003.bin";
bios_name_list[7] = "SCPH7003.bin";
bios_name_list[8] = "SCPH-7003.bin";
bios_name_list[9] = NULL;
bios_sha1 = "0555C6FAE8906F3F09BAF5988F00E55F88E9F30B";
}
else if (region == REGION_EU)
{
bios_name_list[0] = "scph5502.bin";
bios_name_list[1] = "SCPH5502.bin";
bios_name_list[2] = "SCPH-5502.bin";
bios_name_list[3] = "scph5552.bin";
bios_name_list[4] = "SCPH5552.bin";
bios_name_list[5] = "SCPH-5552.bin";
bios_name_list[6] = NULL;
bios_name_list[7] = NULL;
bios_name_list[8] = NULL;
bios_name_list[9] = NULL;
bios_sha1 = "F6BC2D1F5EB6593DE7D089C425AC681D6FFFD3F0";
}
size_t i;
for (i = 0; i < list_size; ++i)
{
if (!bios_name_list[i])
break;
int r = snprintf(bios_path, sizeof(bios_path), "%s%c%s", retro_base_directory, retro_slash, bios_name_list[i]);
if (r >= 4096)
{
log_cb(RETRO_LOG_ERROR, "Firmware path longer than 4095: %s\n", bios_path);
break;
}
if (filestream_exists(bios_path))
{
firmware_found = true;
break;
}
}
if (!firmware_found)
{
char s[4096];
log_cb(RETRO_LOG_ERROR, "Firmware is missing: %s\n", bios_name_list[0]);
s[4095] = '\0';
snprintf(s, sizeof(s), "Firmware is missing:\n\n%s", bios_name_list[0]);
gui_set_message(s);
gui_show = true;
return false;
}
char obtained_sha1[41];
sha1_calculate(bios_path, obtained_sha1);
if (strcmp(obtained_sha1, bios_sha1))
{
log_cb(RETRO_LOG_WARN, "Firmware found but has invalid SHA1: %s\n", bios_path);
log_cb(RETRO_LOG_WARN, "Expected SHA1: %s\n", bios_sha1);
log_cb(RETRO_LOG_WARN, "Obtained SHA1: %s\n", obtained_sha1);
log_cb(RETRO_LOG_WARN, "Unsupported firmware may cause emulation glitches.\n");
return true;
}
log_cb(RETRO_LOG_INFO, "Firmware found: %s\n", bios_path);
log_cb(RETRO_LOG_INFO, "Firmware SHA1: %s\n", obtained_sha1);
return true;
}
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';
}
/* start of Mednafen psx.cpp */
/* 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
*/
#include "mednafen/psx/psx.h"
#include "mednafen/psx/mdec.h"
#include "mednafen/psx/frontio.h"
#include "mednafen/psx/timer.h"
#include "mednafen/psx/sio.h"
#include "mednafen/psx/cdc.h"
#include "mednafen/psx/spu.h"
#include "mednafen/mempatcher.h"
#include <stdarg.h>
#include <ctype.h>
bool setting_apply_analog_toggle = false;
bool use_mednafen_memcard0_method = false;
extern MDFNGI EmulatedPSX;
MDFNGI *MDFNGameInfo = NULL;
#if PSX_DBGPRINT_ENABLE
static unsigned psx_dbg_level = 0;
void PSX_DBG(unsigned level, const char *format, ...)
{
if(psx_dbg_level >= level)
{
va_list ap;
va_start(ap, format);
vprintf(format, ap);
va_end(ap);
}
}
#else
static unsigned const psx_dbg_level = 0;
#endif
/* Based off(but not the same as) public-domain "JKISS" PRNG. */
struct MDFN_PseudoRNG
{
uint32_t x,y,z,c;
uint64_t lcgo;
};
static MDFN_PseudoRNG PSX_PRNG;
uint32_t PSX_GetRandU32(uint32_t mina, uint32_t maxa)
{
uint32_t tmp;
const uint32_t range_m1 = maxa - mina;
uint32_t range_mask = range_m1;
range_mask |= range_mask >> 1;
range_mask |= range_mask >> 2;
range_mask |= range_mask >> 4;
range_mask |= range_mask >> 8;
range_mask |= range_mask >> 16;
do
{
uint64_t t = 4294584393ULL * PSX_PRNG.z + PSX_PRNG.c;
PSX_PRNG.x = 314527869 * PSX_PRNG.x + 1234567;
PSX_PRNG.y ^= PSX_PRNG.y << 5;
PSX_PRNG.y ^= PSX_PRNG.y >> 7;
PSX_PRNG.y ^= PSX_PRNG.y << 22;
PSX_PRNG.c = t >> 32;
PSX_PRNG.z = t;
PSX_PRNG.lcgo = (19073486328125ULL * PSX_PRNG.lcgo) + 1;
tmp = ((PSX_PRNG.x + PSX_PRNG.y + PSX_PRNG.z) ^ (PSX_PRNG.lcgo >> 16)) & range_mask;
} while(tmp > range_m1);
return(mina + tmp);
}
static std::vector<CDIF *> CDInterfaces; // FIXME: Cleanup on error out.
static std::vector<CDIF*> *cdifs = NULL;
static std::vector<const char *> cdifs_scex_ids;
static bool eject_state;
static bool CD_TrayOpen;
int CD_SelectedDisc; // -1 for no disc
static bool CD_IsPBP = false;
extern int PBP_DiscCount;
/* The global value PBP_DiscCount is set to
* zero when loading single-disk PBP files.
* We therefore have to maintain a separate
* 'physical' disk count, otherwise the
* frontend disk control interface will fail */
static int PBP_PhysicalDiscCount;
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;
static uint64_t Memcard_PrevDC[8];
static int64_t Memcard_SaveDelay[8];
PS_CPU *PSX_CPU = NULL;
PS_SPU *PSX_SPU = NULL;
PS_CDC *PSX_CDC = NULL;
FrontIO *PSX_FIO = NULL;
MultiAccessSizeMem<512 * 1024, uint32, false> *BIOSROM = NULL;
MultiAccessSizeMem<65536, uint32, false> *PIOMem = NULL;
MultiAccessSizeMem<2048 * 1024, uint32, false> *MainRAM = NULL;
MultiAccessSizeMem<1024, uint32, false> *ScratchRAM = NULL;
#ifdef HAVE_LIGHTREC
/* Size of Expansion 1 (8MB) */
#define PSX_EXPANSION1_SIZE 0x800000U
/* Base address of Expansion 1 */
#define PSX_EXPANSION1_BASE 0x1F000000U
/* Mednafen splits the expansion in two buffers (PIOMem and TextMem). That's not
* super convenient for us so I'm going to copy both of them in one contiguous
* buffer */
const uint8_t *PSX_LoadExpansion1(void) {
static uint8_t *expansion1 = NULL;
if (expansion1 == NULL) {
expansion1 = new uint8_t[PSX_EXPANSION1_SIZE];
}
/* Let's read 32bits at a time to speed things up a bit */
uint32_t *p = reinterpret_cast<uint32_t *>(expansion1);
for (unsigned i = 0; i < PSX_EXPANSION1_SIZE / 4; i++) {
p[i] = PSX_MemPeek32(PSX_EXPANSION1_BASE + i * 4);
}
return expansion1;
}
#endif
static uint32_t TextMem_Start;
static std::vector<uint8> TextMem;
static const uint32_t SysControl_Mask[9] = { 0x00ffffff, 0x00ffffff, 0xffffffff, 0x2f1fffff,
0xffffffff, 0x2f1fffff, 0x2f1fffff, 0xffffffff,
0x0003ffff };
static const uint32_t SysControl_OR[9] = { 0x1f000000, 0x1f000000, 0x00000000, 0x00000000,
0x00000000, 0x00000000, 0x00000000, 0x00000000,
0x00000000 };
static struct
{
union
{
struct
{
uint32_t PIO_Base; // 0x1f801000 // BIOS Init: 0x1f000000, Writeable bits: 0x00ffffff(assumed, verify), FixedOR = 0x1f000000
uint32_t Unknown0; // 0x1f801004 // BIOS Init: 0x1f802000, Writeable bits: 0x00ffffff, FixedOR = 0x1f000000
uint32_t Unknown1; // 0x1f801008 // BIOS Init: 0x0013243f, ????
uint32_t Unknown2; // 0x1f80100c // BIOS Init: 0x00003022, Writeable bits: 0x2f1fffff, FixedOR = 0x00000000
uint32_t BIOS_Mapping; // 0x1f801010 // BIOS Init: 0x0013243f, ????
uint32_t SPU_Delay; // 0x1f801014 // BIOS Init: 0x200931e1, Writeable bits: 0x2f1fffff, FixedOR = 0x00000000 - Affects bus timing on access to SPU
uint32_t CDC_Delay; // 0x1f801018 // BIOS Init: 0x00020843, Writeable bits: 0x2f1fffff, FixedOR = 0x00000000
uint32_t Unknown4; // 0x1f80101c // BIOS Init: 0x00070777, ????
uint32_t Unknown5; // 0x1f801020 // BIOS Init: 0x00031125(but rewritten with other values often), Writeable bits: 0x0003ffff, FixedOR = 0x00000000 -- Possibly CDC related
};
uint32_t Regs[9];
};
} SysControl;
static unsigned DMACycleSteal = 0; // Doesn't need to be saved in save states, since it's calculated in the ForceEventUpdates() call chain.
void PSX_SetDMACycleSteal(unsigned stealage)
{
if (stealage > 200) // Due to 8-bit limitations in the CPU core.
stealage = 200;
DMACycleSteal = stealage;
}
//
// Event stuff
//
static int32_t Running; // Set to -1 when not desiring exit, and 0 when we are.
struct event_list_entry
{
uint32_t which;
int32_t event_time;
event_list_entry *prev;
event_list_entry *next;
};
static event_list_entry events[PSX_EVENT__COUNT];
static void EventReset(void)
{
unsigned i;
for(i = 0; i < PSX_EVENT__COUNT; i++)
{
events[i].which = i;
if(i == PSX_EVENT__SYNFIRST)
events[i].event_time = (int32_t)0x80000000;
else if(i == PSX_EVENT__SYNLAST)
events[i].event_time = 0x7FFFFFFF;
else
events[i].event_time = PSX_EVENT_MAXTS;
events[i].prev = (i > 0) ? &events[i - 1] : NULL;
events[i].next = (i < (PSX_EVENT__COUNT - 1)) ? &events[i + 1] : NULL;
}
}
//static void RemoveEvent(event_list_entry *e)
//{
// e->prev->next = e->next;
// e->next->prev = e->prev;
//}
static void RebaseTS(const int32_t timestamp)
{
unsigned i;
for(i = 0; i < PSX_EVENT__COUNT; i++)
{
if(i == PSX_EVENT__SYNFIRST || i == PSX_EVENT__SYNLAST)
continue;
assert(events[i].event_time > timestamp);
events[i].event_time -= timestamp;
}
PSX_CPU->SetEventNT(events[PSX_EVENT__SYNFIRST].next->event_time);
}
void PSX_SetEventNT(const int type, const int32_t next_timestamp)
{
event_list_entry *e = &events[type];
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;
}
PSX_CPU->SetEventNT(events[PSX_EVENT__SYNFIRST].next->event_time & Running);
}
// Called from debug.cpp too.
void ForceEventUpdates(const int32_t timestamp)
{
PSX_SetEventNT(PSX_EVENT_GPU, GPU_Update(timestamp));
PSX_SetEventNT(PSX_EVENT_CDC, PSX_CDC->Update(timestamp));
PSX_SetEventNT(PSX_EVENT_TIMER, TIMER_Update(timestamp));
PSX_SetEventNT(PSX_EVENT_DMA, DMA_Update(timestamp));
PSX_SetEventNT(PSX_EVENT_FIO, PSX_FIO->Update(timestamp));
PSX_CPU->SetEventNT(events[PSX_EVENT__SYNFIRST].next->event_time);
}
bool MDFN_FASTCALL PSX_EventHandler(const int32_t timestamp)
{
event_list_entry *e = events[PSX_EVENT__SYNFIRST].next;
while(timestamp >= e->event_time) // If Running = 0, PSX_EventHandler() may be called even if there isn't an event per-se, so while() instead of do { ... } while
{
int32_t nt;
event_list_entry *prev = e->prev;
switch(e->which)
{
default:
abort();
case PSX_EVENT_GPU:
nt = GPU_Update(e->event_time);
break;
case PSX_EVENT_CDC:
nt = PSX_CDC->Update(e->event_time);
break;
case PSX_EVENT_TIMER:
nt = TIMER_Update(e->event_time);
break;
case PSX_EVENT_DMA:
nt = DMA_Update(e->event_time);
break;
case PSX_EVENT_FIO:
nt = PSX_FIO->Update(e->event_time);
break;
}
PSX_SetEventNT(e->which, nt);
// Order of events can change due to calling PSX_SetEventNT(), this prev business ensures we don't miss an event due to reordering.
e = prev->next;
}
return(Running);
}
void PSX_RequestMLExit(void)
{
Running = 0;
PSX_CPU->SetEventNT(0);
}
//
// End event stuff
//
/* Remember to update MemPeek<>() and MemPoke<>() when we change address decoding in MemRW() */
template<typename T, bool IsWrite, bool Access24> static INLINE void MemRW(int32_t ×tamp, uint32_t A, uint32_t &V)
{
#if 0
if(IsWrite)
printf("Write%d: %08x(orig=%08x), %08x\n", (int)(sizeof(T) * 8), A & mask[A >> 29], A, V);
else
printf("Read%d: %08x(orig=%08x)\n", (int)(sizeof(T) * 8), A & mask[A >> 29], A);
#endif
if(!IsWrite)
timestamp += DMACycleSteal;
//if(A == 0xa0 && IsWrite)
// DBG_Break();
if(A < 0x00800000)
{
if(IsWrite)
{
//timestamp++; // Best-case timing.
}
else
{
// Overclock: get rid of memory access latency
if (!psx_gte_overclock)
timestamp += 3;
}
if(Access24)
{
if(IsWrite)
MainRAM->WriteU24(A & 0x1FFFFF, V);
else
V = MainRAM->ReadU24(A & 0x1FFFFF);
}
else
{
if(IsWrite)
MainRAM->Write<T>(A & 0x1FFFFF, V);
else
V = MainRAM->Read<T>(A & 0x1FFFFF);
}
return;
}
if(A >= 0x1FC00000 && A <= 0x1FC7FFFF)
{
if(!IsWrite)
{
if(Access24)
V = BIOSROM->ReadU24(A & 0x7FFFF);
else
V = BIOSROM->Read<T>(A & 0x7FFFF);
}
return;
}
if(timestamp >= events[PSX_EVENT__SYNFIRST].next->event_time)
PSX_EventHandler(timestamp);
if(A >= 0x1F801000 && A <= 0x1F802FFF)
{
//if(IsWrite)
// printf("HW Write%d: %08x %08x\n", (unsigned int)(sizeof(T)*8), (unsigned int)A, (unsigned int)V);
//else
// printf("HW Read%d: %08x\n", (unsigned int)(sizeof(T)*8), (unsigned int)A);
if(A >= 0x1F801C00 && A <= 0x1F801FFF) // SPU
{
if(sizeof(T) == 4 && !Access24)
{
if(IsWrite)
{
//timestamp += 15;
//if(timestamp >= events[PSX_EVENT__SYNFIRST].next->event_time)
// PSX_EventHandler(timestamp);
PSX_SPU->Write(timestamp, A | 0, V);
PSX_SPU->Write(timestamp, A | 2, V >> 16);
}
else
{
timestamp += 36;
if(timestamp >= events[PSX_EVENT__SYNFIRST].next->event_time)
PSX_EventHandler(timestamp);
V = PSX_SPU->Read(timestamp, A) | (PSX_SPU->Read(timestamp, A | 2) << 16);
}
}
else
{
if(IsWrite)
{
//timestamp += 8;
//if(timestamp >= events[PSX_EVENT__SYNFIRST].next->event_time)
// PSX_EventHandler(timestamp);
PSX_SPU->Write(timestamp, A & ~1, V);
}
else
{
timestamp += 16; // Just a guess, need to test.
if(timestamp >= events[PSX_EVENT__SYNFIRST].next->event_time)
PSX_EventHandler(timestamp);
V = PSX_SPU->Read(timestamp, A & ~1);
}
}
return;
} // End SPU
// CDC: TODO - 8-bit access.
if(A >= 0x1f801800 && A <= 0x1f80180F)
{
if(!IsWrite)
{
timestamp += 6 * sizeof(T); //24;
}
if(IsWrite)
PSX_CDC->Write(timestamp, A & 0x3, V);
else
V = PSX_CDC->Read(timestamp, A & 0x3);
return;
}
if(A >= 0x1F801810 && A <= 0x1F801817)
{
if(!IsWrite)
timestamp++;
if(IsWrite)
GPU_Write(timestamp, A, V);
else
V = GPU_Read(timestamp, A);
return;
}
if(A >= 0x1F801820 && A <= 0x1F801827)
{
if(!IsWrite)
timestamp++;
if(IsWrite)
MDEC_Write(timestamp, A, V);
else
V = MDEC_Read(timestamp, A);
return;
}
if(A >= 0x1F801000 && A <= 0x1F801023)
{
unsigned index = (A & 0x1F) >> 2;
if(!IsWrite)
timestamp++;
//if(A == 0x1F801014 && IsWrite)
// fprintf(stderr, "%08x %08x\n",A,V);
if(IsWrite)
{
V <<= (A & 3) * 8;
SysControl.Regs[index] = V & SysControl_Mask[index];
}
else
{
V = SysControl.Regs[index] | SysControl_OR[index];
V >>= (A & 3) * 8;
}
return;
}
if(A >= 0x1F801040 && A <= 0x1F80104F)
{
if(!IsWrite)
timestamp++;
if(IsWrite)
PSX_FIO->Write(timestamp, A, V);
else
V = PSX_FIO->Read(timestamp, A);
return;
}
if(A >= 0x1F801050 && A <= 0x1F80105F)
{
if(!IsWrite)
timestamp++;
#if 0
if(IsWrite)
{
PSX_WARNING("[SIO] Write: 0x%08x 0x%08x %u", A, V, (unsigned)sizeof(T));
}
else
{
PSX_WARNING("[SIO] Read: 0x%08x", A);
}
#endif
if(IsWrite)
SIO_Write(timestamp, A, V);
else
V = SIO_Read(timestamp, A);
return;
}
#if 0
if(A >= 0x1F801060 && A <= 0x1F801063)
{
if(IsWrite)
{
}
else
{
}
return;
}
#endif
if(A >= 0x1F801070 && A <= 0x1F801077) // IRQ
{
if(!IsWrite)
timestamp++;
if(IsWrite)
::IRQ_Write(A, V);
else
V = ::IRQ_Read(A);
return;
}
if(A >= 0x1F801080 && A <= 0x1F8010FF) // DMA
{
if(!IsWrite)
timestamp++;
if(IsWrite)
DMA_Write(timestamp, A, V);
else
V = DMA_Read(timestamp, A);
return;
}
if(A >= 0x1F801100 && A <= 0x1F80113F) // Root counters
{
if(!IsWrite)
timestamp++;
if(IsWrite)
TIMER_Write(timestamp, A, V);
else
V = TIMER_Read(timestamp, A);
return;
}
}
if(A >= 0x1F000000 && A <= 0x1F7FFFFF)
{
if(!IsWrite)
{
//if((A & 0x7FFFFF) <= 0x84)
//PSX_WARNING("[PIO] Read%d from 0x%08x at time %d", (int)(sizeof(T) * 8), A, timestamp);
V = ~0U; // A game this affects: Tetris with Cardcaptor Sakura
if(PIOMem)
{
if((A & 0x7FFFFF) < 65536)
{
if(Access24)
V = PIOMem->ReadU24(A & 0x7FFFFF);
else
V = PIOMem->Read<T>(A & 0x7FFFFF);
}
else if((A & 0x7FFFFF) < (65536 + TextMem.size()))
{
if(Access24)
V = MDFN_de24lsb(&TextMem[(A & 0x7FFFFF) - 65536]);
else switch(sizeof(T))
{
case 1: V = TextMem[(A & 0x7FFFFF) - 65536]; break;
case 2: V = MDFN_de16lsb<false>(&TextMem[(A & 0x7FFFFF) - 65536]); break;
case 4: V = MDFN_de32lsb<false>(&TextMem[(A & 0x7FFFFF) - 65536]); break;
}
}
}
}
return;
}
if(A == 0xFFFE0130) // Per tests on PS1, ignores the access(sort of, on reads the value is forced to 0 if not aligned) if not aligned to 4-bytes.
{
if(!IsWrite)
V = PSX_CPU->GetBIU();
else
PSX_CPU->SetBIU(V);
return;
}
if(IsWrite)
{
PSX_WARNING("[MEM] Unknown write%d to %08x at time %d, =%08x(%d)", (int)(sizeof(T) * 8), A, timestamp, V, V);
}
else
{
V = 0;
PSX_WARNING("[MEM] Unknown read%d from %08x at time %d", (int)(sizeof(T) * 8), A, timestamp);
}
}
void MDFN_FASTCALL PSX_MemWrite8(int32_t timestamp, uint32_t A, uint32_t V)
{
MemRW<uint8, true, false>(timestamp, A, V);