forked from ficoos/mednafen-psx-libretro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libretro.cpp
executable file
·1650 lines (1389 loc) · 46 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"
#ifdef NEED_DEINTERLACER
#include "mednafen/video/Deinterlacer.h"
#endif
#include "libretro.h"
static MDFNGI *game;
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 retro_rumble_interface rumble;
static bool overscan;
static double last_sound_rate;
static MDFN_PixelFormat last_pixel_format;
static MDFN_Surface *surf;
static bool failed_init;
static void hookup_ports(bool force);
static bool initial_ports_hookup = false;
char *psx_analog_type;
#ifdef WANT_PSX_EMU
#define RETRO_DEVICE_PS1PAD RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_JOYPAD, 0)
#define RETRO_DEVICE_DUALANALOG RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_ANALOG, 0)
#define RETRO_DEVICE_DUALSHOCK RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_ANALOG, 1)
#define RETRO_DEVICE_FLIGHTSTICK RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_ANALOG, 2)
#endif
std::string retro_base_directory;
std::string retro_base_name;
std::string retro_save_directory;
static void set_basename(const char *path)
{
const char *base = strrchr(path, '/');
if (!base)
base = strrchr(path, '\\');
if (base)
retro_base_name = base + 1;
else
retro_base_name = path;
retro_base_name = retro_base_name.substr(0, retro_base_name.find_last_of('.'));
}
#ifdef NEED_DEINTERLACER
static bool PrevInterlaced;
static Deinterlacer deint;
#endif
#if defined(WANT_PSX_EMU)
#define MEDNAFEN_CORE_NAME_MODULE "psx"
#define MEDNAFEN_CORE_NAME "Mednafen PSX"
#define MEDNAFEN_CORE_VERSION "v0.9.33.3"
#define MEDNAFEN_CORE_EXTENSIONS "cue|toc|m3u|ccd"
static double mednafen_psx_fps = 59.82704; // Hardcoded for NTSC atm.
#define MEDNAFEN_CORE_GEOMETRY_BASE_W 320
#define MEDNAFEN_CORE_GEOMETRY_BASE_H 240
#define MEDNAFEN_CORE_GEOMETRY_MAX_W 700
#define MEDNAFEN_CORE_GEOMETRY_MAX_H 480
#define MEDNAFEN_CORE_GEOMETRY_ASPECT_RATIO (4.0 / 3.0)
#define FB_WIDTH 700
static int mednafen_psx_fb_height;
static bool is_pal = false;
#elif defined(WANT_LYNX_EMU)
#define MEDNAFEN_CORE_NAME_MODULE "lynx"
#define MEDNAFEN_CORE_NAME "Mednafen Lynx"
#define MEDNAFEN_CORE_VERSION "v0.9.32"
#define MEDNAFEN_CORE_EXTENSIONS "lnx"
#define MEDNAFEN_CORE_TIMING_FPS 75
#define MEDNAFEN_CORE_GEOMETRY_BASE_W 160
#define MEDNAFEN_CORE_GEOMETRY_BASE_H 102
#define MEDNAFEN_CORE_GEOMETRY_MAX_W 160
#define MEDNAFEN_CORE_GEOMETRY_MAX_H 102
#define MEDNAFEN_CORE_GEOMETRY_ASPECT_RATIO (4.0 / 3.0)
#define FB_WIDTH 160
#define FB_HEIGHT 102
static bool is_pal = false;
#elif defined(WANT_PCE_FAST_EMU)
#include "mednafen/pce_fast-09333/pcecd.h"
#define MEDNAFEN_CORE_NAME_MODULE "pce_fast"
#define MEDNAFEN_CORE_NAME "Mednafen PCE Fast"
#define MEDNAFEN_CORE_VERSION "v0.9.33.3"
#define MEDNAFEN_CORE_EXTENSIONS "pce|sgx|cue|ccd"
#define MEDNAFEN_CORE_TIMING_FPS 59.82
#define MEDNAFEN_CORE_GEOMETRY_BASE_W (game->nominal_width)
#define MEDNAFEN_CORE_GEOMETRY_BASE_H (game->nominal_height)
#define MEDNAFEN_CORE_GEOMETRY_MAX_W 512
#define MEDNAFEN_CORE_GEOMETRY_MAX_H 242
#define MEDNAFEN_CORE_GEOMETRY_ASPECT_RATIO (6.0 / 5.0)
#define FB_WIDTH 512
#define FB_HEIGHT 242
static int scanline_start;
static int scanline_end;
#elif defined(WANT_WSWAN_EMU)
#define MEDNAFEN_CORE_NAME_MODULE "wswan"
#define MEDNAFEN_CORE_NAME "Mednafen WonderSwan"
#define MEDNAFEN_CORE_VERSION "v0.9.33.3"
#define MEDNAFEN_CORE_EXTENSIONS "ws|wsc"
#define MEDNAFEN_CORE_TIMING_FPS 75.47
#define MEDNAFEN_CORE_GEOMETRY_BASE_W (game->nominal_width)
#define MEDNAFEN_CORE_GEOMETRY_BASE_H (game->nominal_height)
#define MEDNAFEN_CORE_GEOMETRY_MAX_W 224
#define MEDNAFEN_CORE_GEOMETRY_MAX_H 144
#define MEDNAFEN_CORE_GEOMETRY_ASPECT_RATIO (4.0 / 3.0)
#define FB_WIDTH 224
#define FB_HEIGHT 144
#elif defined(WANT_NGP_EMU)
#define MEDNAFEN_CORE_NAME_MODULE "ngp"
#define MEDNAFEN_CORE_NAME "Mednafen Neopop"
#define MEDNAFEN_CORE_VERSION "v0.9.33.3"
#define MEDNAFEN_CORE_EXTENSIONS "ngp|ngc"
#define MEDNAFEN_CORE_TIMING_FPS 60.25
#define MEDNAFEN_CORE_GEOMETRY_BASE_W (game->nominal_width)
#define MEDNAFEN_CORE_GEOMETRY_BASE_H (game->nominal_height)
#define MEDNAFEN_CORE_GEOMETRY_MAX_W 160
#define MEDNAFEN_CORE_GEOMETRY_MAX_H 152
#define MEDNAFEN_CORE_GEOMETRY_ASPECT_RATIO (4.0 / 3.0)
#define FB_WIDTH 160
#define FB_HEIGHT 152
#elif defined(WANT_GBA_EMU)
#define MEDNAFEN_CORE_NAME_MODULE "gba"
#define MEDNAFEN_CORE_NAME "Mednafen VBA-M"
#define MEDNAFEN_CORE_VERSION "v0.9.33.3"
#define MEDNAFEN_CORE_EXTENSIONS "gba|agb|bin"
#define MEDNAFEN_CORE_TIMING_FPS 59.73
#define MEDNAFEN_CORE_GEOMETRY_BASE_W (game->nominal_width)
#define MEDNAFEN_CORE_GEOMETRY_BASE_H (game->nominal_height)
#define MEDNAFEN_CORE_GEOMETRY_MAX_W 240
#define MEDNAFEN_CORE_GEOMETRY_MAX_H 160
#define MEDNAFEN_CORE_GEOMETRY_ASPECT_RATIO (4.0 / 3.0)
#define FB_WIDTH 240
#define FB_HEIGHT 160
#elif defined(WANT_SNES_EMU)
#define MEDNAFEN_CORE_NAME_MODULE "snes"
#define MEDNAFEN_CORE_NAME "Mednafen bSNES"
#define MEDNAFEN_CORE_VERSION "v0.9.26"
#define MEDNAFEN_CORE_EXTENSIONS "smc|fig|bs|st|sfc"
#define MEDNAFEN_CORE_TIMING_FPS 60.10
#define MEDNAFEN_CORE_GEOMETRY_BASE_W (game->nominal_width)
#define MEDNAFEN_CORE_GEOMETRY_BASE_H (game->nominal_height)
#define MEDNAFEN_CORE_GEOMETRY_MAX_W 512
#define MEDNAFEN_CORE_GEOMETRY_MAX_H 512
#define MEDNAFEN_CORE_GEOMETRY_ASPECT_RATIO (4.0 / 3.0)
#define FB_WIDTH 512
#define FB_HEIGHT 512
#elif defined(WANT_VB_EMU)
#define MEDNAFEN_CORE_NAME_MODULE "vb"
#define MEDNAFEN_CORE_NAME "Mednafen VB"
#define MEDNAFEN_CORE_VERSION "v0.9.33.3"
#define MEDNAFEN_CORE_EXTENSIONS "vb|vboy|bin"
#define MEDNAFEN_CORE_TIMING_FPS 50.27
#define MEDNAFEN_CORE_GEOMETRY_BASE_W (game->nominal_width)
#define MEDNAFEN_CORE_GEOMETRY_BASE_H (game->nominal_height)
#define MEDNAFEN_CORE_GEOMETRY_MAX_W 384
#define MEDNAFEN_CORE_GEOMETRY_MAX_H 224
#define MEDNAFEN_CORE_GEOMETRY_ASPECT_RATIO (4.0 / 3.0)
#define FB_WIDTH 384
#define FB_HEIGHT 224
#elif defined(WANT_PCFX_EMU)
#define MEDNAFEN_CORE_NAME_MODULE "pcfx"
#define MEDNAFEN_CORE_NAME "Mednafen PC-FX"
#define MEDNAFEN_CORE_VERSION "v0.9.33.3"
#define MEDNAFEN_CORE_EXTENSIONS "cue|ccd"
#define MEDNAFEN_CORE_TIMING_FPS 59.94
#define MEDNAFEN_CORE_GEOMETRY_BASE_W (game->nominal_width)
#define MEDNAFEN_CORE_GEOMETRY_BASE_H (game->nominal_height)
#define MEDNAFEN_CORE_GEOMETRY_MAX_W 341
#define MEDNAFEN_CORE_GEOMETRY_MAX_H 480
#define MEDNAFEN_CORE_GEOMETRY_ASPECT_RATIO (4.0 / 3.0)
#define FB_WIDTH 344
#define FB_HEIGHT 480
#endif
#if defined(WANT_PSX_EMU)
#define FB_MAX_HEIGHT 576
#else
#define FB_MAX_HEIGHT FB_HEIGHT
#endif
// Wastes a little space for NTSC PSX, but better than dynamically allocating.
#ifdef WANT_16BPP
static uint16_t mednafen_buf[FB_WIDTH * FB_MAX_HEIGHT];
#else
static uint32_t mednafen_buf[FB_WIDTH * FB_MAX_HEIGHT];
#endif
const char *mednafen_core_str = MEDNAFEN_CORE_NAME;
static void check_system_specs(void)
{
unsigned level = 0;
#if defined(WANT_PSX_EMU)
// Hints that we need a fairly powerful system to run this.
level = 3;
#endif
environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
}
#if defined(WANT_PSX_EMU)
// Ugly poking. There's no public interface for this.
#include "mednafen/psx/psx.h"
namespace MDFN_IEN_PSX
{
extern int CD_SelectedDisc;
extern std::vector<CDIF*> *cdifs;
}
// Poke into psx.cpp
unsigned CalcDiscSCEx();
using MDFN_IEN_PSX::CD_SelectedDisc;
using MDFN_IEN_PSX::cdifs;
enum
{
REGION_JP = 0,
REGION_NA = 1,
REGION_EU = 2,
};
static unsigned disk_get_num_images(void)
{
return cdifs ? cdifs->size() : 0;
}
static bool eject_state;
static bool disk_set_eject_state(bool ejected)
{
if (log_cb)
log_cb(RETRO_LOG_INFO, "[Mednafen]: Ejected: %u.\n", ejected);
if (ejected == eject_state)
return false;
game->DoSimpleCommand(ejected ? MDFN_MSC_EJECT_DISK : MDFN_MSC_INSERT_DISK);
eject_state = ejected;
return true;
}
static bool disk_get_eject_state(void)
{
return eject_state;
}
static unsigned disk_get_image_index(void)
{
// PSX global. Hacky.
return CD_SelectedDisc;
}
static bool disk_set_image_index(unsigned index)
{
CD_SelectedDisc = index;
if (CD_SelectedDisc > disk_get_num_images())
CD_SelectedDisc = disk_get_num_images();
// Very hacky. CDSelect command will increment first.
CD_SelectedDisc--;
game->DoSimpleCommand(MDFN_MSC_SELECT_DISK);
return true;
}
// Mednafen PSX really doesn't support adding disk images on the fly ...
// Hack around this.
static void update_md5_checksum(CDIF *iface)
{
uint8 LayoutMD5[16];
md5_context layout_md5;
layout_md5.starts();
CD_TOC toc;
iface->ReadTOC(&toc);
layout_md5.update_u32_as_lsb(toc.first_track);
layout_md5.update_u32_as_lsb(toc.last_track);
layout_md5.update_u32_as_lsb(toc.tracks[100].lba);
for (uint32 track = toc.first_track; track <= toc.last_track; track++)
{
layout_md5.update_u32_as_lsb(toc.tracks[track].lba);
layout_md5.update_u32_as_lsb(toc.tracks[track].control & 0x4);
}
layout_md5.finish(LayoutMD5);
memcpy(MDFNGameInfo->MD5, LayoutMD5, 16);
std::string md5 = md5_context::asciistr(MDFNGameInfo->MD5, 0);
if (log_cb)
log_cb(RETRO_LOG_INFO, "[Mednafen]: Updated md5 checksum: %s.\n", md5.c_str());
}
// Untested ...
static bool disk_replace_image_index(unsigned index, const struct retro_game_info *info)
{
if (index >= disk_get_num_images())
return false;
if (!eject_state)
return false;
if (!info)
{
delete cdifs->at(index);
cdifs->erase(cdifs->begin() + index);
if (index < CD_SelectedDisc)
CD_SelectedDisc--;
// Poke into psx.cpp
CalcDiscSCEx();
return true;
}
try
{
CDIF *iface = CDIF_Open(info->path, false, false);
delete cdifs->at(index);
cdifs->at(index) = iface;
CalcDiscSCEx();
set_basename(info->path); // If we replace, we want the "swap disk manually effect".
update_md5_checksum(iface); // Ugly, but needed to get proper disk swapping effect.
return true;
}
catch (const std::exception &e)
{
return false;
}
}
static bool disk_add_image_index(void)
{
cdifs->push_back(NULL);
return true;
}
///////
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,
};
#endif
void retro_init(void)
{
struct retro_log_callback log;
if (environ_cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &log))
log_cb = log.log;
else
log_cb = NULL;
MDFNI_InitializeModule();
#if defined(WANT_PSX_EMU)
eject_state = false;
#endif
const char *dir = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
{
retro_base_directory = dir;
// Make sure that we don't have any lingering slashes, etc, as they break Windows.
size_t last = retro_base_directory.find_last_not_of("/\\");
if (last != std::string::npos)
last++;
retro_base_directory = retro_base_directory.substr(0, last);
MDFNI_Initialize(retro_base_directory.c_str());
}
else
{
/* TODO: Add proper fallback */
if (log_cb)
log_cb(RETRO_LOG_WARN, "System directory is not defined. Fallback on using same dir as ROM for system directory later ...\n");
failed_init = true;
}
if (environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &dir) && dir)
{
// If save directory is defined use it, otherwise use system directory
retro_save_directory = *dir ? dir : retro_base_directory;
// Make sure that we don't have any lingering slashes, etc, as they break Windows.
size_t last = retro_save_directory.find_last_not_of("/\\");
if (last != std::string::npos)
last++;
retro_save_directory = retro_save_directory.substr(0, last);
}
else
{
/* TODO: Add proper fallback */
if (log_cb)
log_cb(RETRO_LOG_WARN, "Save directory is not defined. Fallback on using SYSTEM directory ...\n");
retro_save_directory = retro_base_directory;
}
#if defined(WANT_16BPP) && defined(FRONTEND_SUPPORTS_RGB565)
enum retro_pixel_format rgb565 = RETRO_PIXEL_FORMAT_RGB565;
if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &rgb565) && log_cb)
log_cb(RETRO_LOG_INFO, "Frontend supports RGB565 - will use that instead of XRGB1555.\n");
#endif
#if defined(WANT_PSX_EMU)
environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_interface);
#endif
if (environ_cb(RETRO_ENVIRONMENT_GET_PERF_INTERFACE, &perf_cb))
perf_get_cpu_features_cb = perf_cb.get_cpu_features;
else
perf_get_cpu_features_cb = NULL;
check_system_specs();
}
void retro_reset(void)
{
game->DoSimpleCommand(MDFN_MSC_RESET);
}
bool retro_load_game_special(unsigned, const struct retro_game_info *, size_t)
{
return false;
}
static void set_volume (uint32_t *ptr, unsigned number)
{
switch(number)
{
default:
*ptr = number;
break;
}
}
static void check_variables(void)
{
struct retro_variable var = {0};
#if defined(WANT_PCE_FAST_EMU)
var.key = "pce_nospritelimit";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
{
if (strcmp(var.value, "disabled") == 0)
setting_pce_fast_nospritelimit = 0;
else if (strcmp(var.value, "enabled") == 0)
setting_pce_fast_nospritelimit = 1;
}
var.key = "pce_scanlinestart";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
scanline_start = atoi(var.value);
var.key = "pce_scanlineend";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
scanline_end = atoi(var.value);
// removed the redundant "Keep Aspect" option; we control the ar)
// make Mednafen always output square pixels.
setting_pce_keepaspect = 0;
game->fb_width = FB_WIDTH; //682
game->nominal_width = FB_WIDTH / 2; //341
game->lcm_width = FB_WIDTH / 2;
bool do_cdsettings = false;
var.key = "pce_cddavolume";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
{
do_cdsettings = true;
setting_pce_fast_cddavolume = atoi(var.value);
}
var.key = "pce_adpcmvolume";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
{
do_cdsettings = true;
setting_pce_fast_adpcmvolume = atoi(var.value);
}
var.key = "pce_cdpsgvolume";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
{
do_cdsettings = true;
setting_pce_fast_cdpsgvolume = atoi(var.value);
}
var.key = "pce_cdspeed";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
{
do_cdsettings = true;
setting_pce_fast_cdspeed = atoi(var.value);
}
if (do_cdsettings)
{
PCE_Fast::PCECD_Settings settings = {0};
settings.CDDA_Volume = (double)setting_pce_fast_cddavolume / 100;
settings.CD_Speed = setting_pce_fast_cdspeed;
settings.ADPCM_Volume = (double)setting_pce_fast_adpcmvolume / 100;
if (PCECD_SetSettings(&settings) && log_cb)
log_cb(RETRO_LOG_INFO, "PCE CD Audio settings changed.\n");
}
#elif defined(WANT_PSX_EMU)
extern void PSXDitherApply(bool);
var.key = "psx_dithering";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
{
static bool old_apply_dither = false;
bool apply_dither = true;
if (strcmp(var.value, "enabled") == 0)
apply_dither = true;
else if (strcmp(var.value, "disabled") == 0)
apply_dither = false;
if (apply_dither != old_apply_dither)
{
PSXDitherApply(apply_dither);
old_apply_dither = apply_dither;
}
}
var.key = "psx_enable_analog_toggle";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
{
if (strcmp(var.value, "enabled") == 0)
setting_psx_analog_toggle = 1;
else if (strcmp(var.value, "disabled") == 0)
setting_psx_analog_toggle = 0;
}
var.key = "psx_enable_multitap_port1";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
{
if (strcmp(var.value, "enabled") == 0)
setting_psx_multitap_port_1 = true;
else if (strcmp(var.value, "disabled") == 0)
setting_psx_multitap_port_1 = false;
}
var.key = "psx_enable_multitap_port2";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
{
if (strcmp(var.value, "enabled") == 0)
setting_psx_multitap_port_2 = true;
else if (strcmp(var.value, "disabled") == 0)
setting_psx_multitap_port_2 = false;
}
#elif defined(WANT_NGP_EMU)
var.key = "ngp_language";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
{
if (strcmp(var.value, "japanese") == 0)
setting_ngp_language = 0;
else if (strcmp(var.value, "english") == 0)
setting_ngp_language = 1;
retro_reset();
}
#elif defined (WANT_GBA_EMU)
var.key = "gba_hle";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
{
if (strcmp(var.value, "enabled") == 0)
setting_gba_hle = 1;
else if (strcmp(var.value, "disabled") == 0)
setting_gba_hle = 0;
}
#elif defined (WANT_VB_EMU)
var.key = "vb_color_mode";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
{
if (strcmp(var.value, "black & red") == 0)
{
setting_vb_lcolor = 0xFF0000;
setting_vb_rcolor = 0x000000;
}
else if (strcmp(var.value, "black & white") == 0)
{
setting_vb_lcolor = 0xFFFFFF;
setting_vb_rcolor = 0x000000;
}
log_cb(RETRO_LOG_INFO, "[%s]: Palette changed: %s .\n", mednafen_core_str, var.value);
}
var.key = "vb_anaglyph_preset";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
{
if (strcmp(var.value, "disabled") == 0)
setting_vb_anaglyph_preset = 0;
else if (strcmp(var.value, "red & blue") == 0)
setting_vb_anaglyph_preset = 1;
else if (strcmp(var.value, "red & cyan") == 0)
setting_vb_anaglyph_preset = 2;
else if (strcmp(var.value, "red & electric cyan") == 0)
setting_vb_anaglyph_preset = 3;
else if (strcmp(var.value, "red & green") == 0)
setting_vb_anaglyph_preset = 4;
else if (strcmp(var.value, "green & magenta") == 0)
setting_vb_anaglyph_preset = 5;
else if (strcmp(var.value, "yellow & blue") == 0)
setting_vb_anaglyph_preset = 6;
log_cb(RETRO_LOG_INFO, "[%s]: Palette changed: %s .\n", mednafen_core_str, var.value);
}
#endif
}
#if defined(WANT_PSX_EMU)
#define MAX_PLAYERS 2
#define MAX_BUTTONS 16
union
{
uint32_t u32[MAX_PLAYERS][1 + 8 + 1]; // Buttons + Axes + Rumble
uint8_t u8[MAX_PLAYERS][(1 + 8 + 1) * sizeof(uint32_t)];
} static buf;
static uint16_t input_buf[MAX_PLAYERS] = {0};
#elif defined(WANT_PCE_FAST_EMU)
#define MAX_PLAYERS 5
#define MAX_BUTTONS 13
static uint8_t input_buf[MAX_PLAYERS][2] = {0};
#elif defined(WANT_LYNX_EMU)
#define MAX_PLAYERS 1
#define MAX_BUTTONS 9
static uint8_t input_buf[MAX_PLAYERS][2] = {0};
#elif defined(WANT_WSWAN_EMU)
#define MAX_PLAYERS 1
#define MAX_BUTTONS 11
static uint16_t input_buf;
#elif defined(WANT_NGP_EMU)
#define MAX_PLAYERS 1
#define MAX_BUTTONS 7
static uint8_t input_buf;
#elif defined(WANT_GBA_EMU)
#define MAX_PLAYERS 1
#define MAX_BUTTONS 11
static uint16_t input_buf;
#elif defined(WANT_SNES_EMU)
#define MAX_PLAYERS 5
#define MAX_BUTTONS 14
static uint8_t input_buf[MAX_PLAYERS][2];
#elif defined(WANT_VB_EMU)
#define MAX_PLAYERS 1
#define MAX_BUTTONS 14
static uint16_t input_buf[MAX_PLAYERS];
#elif defined(WANT_PCFX_EMU)
#define MAX_PLAYERS 2
#define MAX_BUTTONS 12
static uint16_t input_buf[MAX_PLAYERS];
#else
#define MAX_PLAYERS 1
#define MAX_BUTTONS 7
static uint16_t input_buf[1];
#endif
static void hookup_ports(bool force)
{
MDFNGI *currgame = game;
if (initial_ports_hookup && !force)
return;
#if defined(WANT_PCE_FAST_EMU)
// Possible endian bug ...
for (unsigned i = 0; i < MAX_PLAYERS; i++)
currgame->SetInput(i, "gamepad", &input_buf[i][0]);
#elif defined(WANT_LYNX_EMU)
currgame->SetInput(0, "gamepad", &input_buf);
#elif defined(WANT_WSWAN_EMU)
currgame->SetInput(0, "gamepad", &input_buf);
#elif defined(WANT_NGP_EMU)
currgame->SetInput(0, "gamepad", &input_buf);
#elif defined(WANT_GBA_EMU)
// Possible endian bug ...
currgame->SetInput(0, "gamepad", &input_buf);
#elif defined(WANT_SNES_EMU)
// Possible endian bug ...
for (unsigned i = 0; i < MAX_PLAYERS; i++)
currgame->SetInput(i, "gamepad", &input_buf[i][0]);
#elif defined(WANT_VB_EMU)
// Possible endian bug ...
currgame->SetInput(0, "gamepad", &input_buf[0]);
#else
// Possible endian bug ...
currgame->SetInput(0, "gamepad", &input_buf[0]);
#endif
initial_ports_hookup = true;
}
bool retro_load_game(const struct retro_game_info *info)
{
if (failed_init)
return false;
#ifdef WANT_32BPP
enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_XRGB8888;
if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt))
{
if (log_cb)
log_cb(RETRO_LOG_ERROR, "Pixel format XRGB8888 not supported by platform, cannot use %s.\n", MEDNAFEN_CORE_NAME);
return false;
}
#endif
overscan = false;
environ_cb(RETRO_ENVIRONMENT_GET_OVERSCAN, &overscan);
set_basename(info->path);
#if defined(WANT_GBA_EMU)
check_variables();
#endif
#if defined(WANT_PSX_EMU)
check_variables();
if (environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble) && log_cb)
log_cb(RETRO_LOG_INFO, "Rumble interface supported!\n");
#endif
#if defined(WANT_VB_EMU)
check_variables();
#endif
game = MDFNI_LoadGame(MEDNAFEN_CORE_NAME_MODULE, info->path);
if (!game)
return false;
MDFN_PixelFormat pix_fmt(MDFN_COLORSPACE_RGB, 16, 8, 0, 24);
memset(&last_pixel_format, 0, sizeof(MDFN_PixelFormat));
#if defined(WANT_PSX_EMU)
is_pal = (CalcDiscSCEx() == REGION_EU);
mednafen_psx_fb_height = is_pal ? 576 : 480;
mednafen_psx_fps = is_pal ? 50.0 : 59.82704;
surf = new MDFN_Surface(mednafen_buf, FB_WIDTH, mednafen_psx_fb_height, FB_WIDTH, pix_fmt);
#else
surf = new MDFN_Surface(mednafen_buf, FB_WIDTH, FB_HEIGHT, FB_WIDTH, pix_fmt);
#endif
#ifdef NEED_DEINTERLACER
PrevInterlaced = false;
deint.ClearState();
#endif
#if !defined(WANT_PSX_EMU) || !defined(WANT_PCE_FAST_EMU) || !defined(WANT_PCFX_EMU)
hookup_ports(true);
#endif
#if !defined(WANT_PSX_EMU)
check_variables();
#endif
return game;
}
void retro_unload_game()
{
if (!game)
return;
MDFNI_CloseGame();
}
// Hardcoded for PSX. No reason to parse lots of structures ...
// See mednafen/psx/input/gamepad.cpp
static void update_input(void)
{
MDFNGI *currgame = (MDFNGI*)game;
#if defined(WANT_PSX_EMU)
input_buf[0] = 0;
input_buf[1] = 0;
static unsigned map[] = {
RETRO_DEVICE_ID_JOYPAD_SELECT,
RETRO_DEVICE_ID_JOYPAD_L3,
RETRO_DEVICE_ID_JOYPAD_R3,
RETRO_DEVICE_ID_JOYPAD_START,
RETRO_DEVICE_ID_JOYPAD_UP,
RETRO_DEVICE_ID_JOYPAD_RIGHT,
RETRO_DEVICE_ID_JOYPAD_DOWN,
RETRO_DEVICE_ID_JOYPAD_LEFT,
RETRO_DEVICE_ID_JOYPAD_L2,
RETRO_DEVICE_ID_JOYPAD_R2,
RETRO_DEVICE_ID_JOYPAD_L,
RETRO_DEVICE_ID_JOYPAD_R,
RETRO_DEVICE_ID_JOYPAD_X,
RETRO_DEVICE_ID_JOYPAD_A,
RETRO_DEVICE_ID_JOYPAD_B,
RETRO_DEVICE_ID_JOYPAD_Y,
};
for (unsigned j = 0; j < MAX_PLAYERS; j++)
{
for (unsigned i = 0; i < MAX_BUTTONS; i++)
input_buf[j] |= input_state_cb(j, RETRO_DEVICE_JOYPAD, 0, map[i]) ? (1 << i) : 0;
}
// Buttons.
buf.u8[0][0] = (input_buf[0] >> 0) & 0xff;
buf.u8[0][1] = (input_buf[0] >> 8) & 0xff;
buf.u8[1][0] = (input_buf[1] >> 0) & 0xff;
buf.u8[1][1] = (input_buf[1] >> 8) & 0xff;
// Analogs
for (unsigned j = 0; j < 2; j++)
{
int analog_left_x = input_state_cb(j, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT,
RETRO_DEVICE_ID_ANALOG_X);
int analog_left_y = input_state_cb(j, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT,
RETRO_DEVICE_ID_ANALOG_Y);
int analog_right_x = input_state_cb(j, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT,
RETRO_DEVICE_ID_ANALOG_X);
int analog_right_y = input_state_cb(j, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_RIGHT,
RETRO_DEVICE_ID_ANALOG_Y);
uint32_t r_right = analog_right_x > 0 ? analog_right_x : 0;
uint32_t r_left = analog_right_x < 0 ? -analog_right_x : 0;
uint32_t r_down = analog_right_y > 0 ? analog_right_y : 0;
uint32_t r_up = analog_right_y < 0 ? -analog_right_y : 0;
uint32_t l_right = analog_left_x > 0 ? analog_left_x : 0;
uint32_t l_left = analog_left_x < 0 ? -analog_left_x : 0;
uint32_t l_down = analog_left_y > 0 ? analog_left_y : 0;
uint32_t l_up = analog_left_y < 0 ? -analog_left_y : 0;
buf.u32[j][1] = r_right;
buf.u32[j][2] = r_left;
buf.u32[j][3] = r_down;
buf.u32[j][4] = r_up;
buf.u32[j][5] = l_right;
buf.u32[j][6] = l_left;
buf.u32[j][7] = l_down;
buf.u32[j][8] = l_up;
}
//fprintf(stderr, "Rumble strong: %u, weak: %u.\n", buf.u8[0][9 * 4 + 1], buf.u8[0][9 * 4]);
if (rumble.set_rumble_state)
{
// Appears to be correct.
rumble.set_rumble_state(0, RETRO_RUMBLE_WEAK, buf.u8[0][9 * 4] * 0x101);
rumble.set_rumble_state(0, RETRO_RUMBLE_STRONG, buf.u8[0][9 * 4 + 1] * 0x101);
rumble.set_rumble_state(1, RETRO_RUMBLE_WEAK, buf.u8[1][9 * 4] * 0x101);
rumble.set_rumble_state(1, RETRO_RUMBLE_STRONG, buf.u8[1][9 * 4 + 1] * 0x101);
}
#elif defined(WANT_LYNX_EMU)
static unsigned map[] = {
RETRO_DEVICE_ID_JOYPAD_A,
RETRO_DEVICE_ID_JOYPAD_B,
RETRO_DEVICE_ID_JOYPAD_L,
RETRO_DEVICE_ID_JOYPAD_R,
RETRO_DEVICE_ID_JOYPAD_LEFT,
RETRO_DEVICE_ID_JOYPAD_RIGHT,
RETRO_DEVICE_ID_JOYPAD_UP,
RETRO_DEVICE_ID_JOYPAD_DOWN,
RETRO_DEVICE_ID_JOYPAD_START,
};
for (unsigned j = 0; j < MAX_PLAYERS; j++)
{
uint16_t input_state = 0;
for (unsigned i = 0; i < MAX_BUTTONS; i++)
input_state |= input_state_cb(j, RETRO_DEVICE_JOYPAD, 0, map[i]) ? (1 << i) : 0;
// Input data must be little endian.
input_buf[j][0] = (input_state >> 0) & 0xff;
input_buf[j][1] = (input_state >> 8) & 0xff;
}
#elif defined(WANT_PCE_FAST_EMU)
static unsigned map[] = {
RETRO_DEVICE_ID_JOYPAD_A,
RETRO_DEVICE_ID_JOYPAD_B,
RETRO_DEVICE_ID_JOYPAD_SELECT,
RETRO_DEVICE_ID_JOYPAD_START,
RETRO_DEVICE_ID_JOYPAD_UP,
RETRO_DEVICE_ID_JOYPAD_RIGHT,
RETRO_DEVICE_ID_JOYPAD_DOWN,
RETRO_DEVICE_ID_JOYPAD_LEFT,
RETRO_DEVICE_ID_JOYPAD_Y,
RETRO_DEVICE_ID_JOYPAD_X,
RETRO_DEVICE_ID_JOYPAD_L,
RETRO_DEVICE_ID_JOYPAD_R,
RETRO_DEVICE_ID_JOYPAD_L2
};
for (unsigned j = 0; j < MAX_PLAYERS; j++)
{
uint16_t input_state = 0;
for (unsigned i = 0; i < MAX_BUTTONS; i++)
input_state |= input_state_cb(j, RETRO_DEVICE_JOYPAD, 0, map[i]) ? (1 << i) : 0;
// Input data must be little endian.
input_buf[j][0] = (input_state >> 0) & 0xff;
input_buf[j][1] = (input_state >> 8) & 0xff;
}
#elif defined(WANT_WSWAN_EMU)
input_buf = 0;
static unsigned map[] = {
RETRO_DEVICE_ID_JOYPAD_UP, //X Cursor horizontal-layout games
RETRO_DEVICE_ID_JOYPAD_RIGHT, //X Cursor horizontal-layout games
RETRO_DEVICE_ID_JOYPAD_DOWN, //X Cursor horizontal-layout games
RETRO_DEVICE_ID_JOYPAD_LEFT, //X Cursor horizontal-layout games
RETRO_DEVICE_ID_JOYPAD_R2, //Y Cursor UP vertical-layout games
RETRO_DEVICE_ID_JOYPAD_R, //Y Cursor RIGHT vertical-layout games
RETRO_DEVICE_ID_JOYPAD_L2, //Y Cursor DOWN vertical-layout games
RETRO_DEVICE_ID_JOYPAD_L, //Y Cursor LEFT vertical-layout games
RETRO_DEVICE_ID_JOYPAD_START,
RETRO_DEVICE_ID_JOYPAD_A,
RETRO_DEVICE_ID_JOYPAD_B,
};
for (unsigned i = 0; i < MAX_BUTTONS; i++)
input_buf |= map[i] != -1u &&
input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, map[i]) ? (1 << i) : 0;
#ifdef MSB_FIRST
union {
uint8_t b[2];
uint16_t s;
} u;
u.s = input_buf;
input_buf = u.b[0] | u.b[1] << 8;
#endif