forked from libretro/beetle-ngp-libretro
-
Notifications
You must be signed in to change notification settings - Fork 1
/
libretro.cpp
1061 lines (827 loc) · 24.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 "mednafen/mednafen.h"
#include "mednafen/mempatcher.h"
#include "mednafen/git.h"
#include "mednafen/general.h"
#include <libretro.h>
#include <streams/file_stream.h>
#include "libretro_core_options.h"
#include "mednafen/state.h"
#include "mednafen/state_helpers.h"
/* core options */
static int RETRO_SAMPLE_RATE = 44100;
static int RETRO_PIX_BYTES = 2;
static int RETRO_PIX_DEPTH = 15;
// ====================================================
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_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 bool overscan;
static MDFN_Surface *surf;
static bool failed_init;
static bool libretro_supports_bitmasks = false;
static void hookup_ports(bool force);
static bool initial_ports_hookup = false;
extern "C" char retro_base_directory[1024];
std::string retro_base_name;
char retro_save_directory[1024];
//---------------------------------------------------------------------------
// NEOPOP : Emulator as in Dreamland
//
// Copyright (c) 2001-2002 by neopop_uk
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// 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. See also the license.txt file for
// additional informations.
//---------------------------------------------------------------------------
#include "mednafen/ngp/neopop.h"
#include "mednafen/general.h"
#include "mednafen/ngp/TLCS-900h/TLCS900h_interpret.h"
#include "mednafen/ngp/TLCS-900h/TLCS900h_registers.h"
#include "mednafen/ngp/Z80_interface.h"
#include "mednafen/ngp/interrupt.h"
#include "mednafen/ngp/mem.h"
#include "mednafen/ngp/gfx.h"
#include "mednafen/ngp/sound.h"
#include "mednafen/ngp/dma.h"
#include "mednafen/ngp/bios.h"
#include "mednafen/ngp/flash.h"
#include "mednafen/ngp/system.h"
extern uint8 CPUExRAM[16384];
ngpgfx_t *NGPGfx;
COLOURMODE system_colour = COLOURMODE_AUTO;
uint8 NGPJoyLatch;
static uint8 *chee;
static int32 z80_runtime;
extern "C" bool NGPFrameSkip;
extern "C" int32_t ngpc_soundTS;
static void Emulate(EmulateSpecStruct *espec)
{
bool MeowMeow = false;
espec->DisplayRect.x = 0;
espec->DisplayRect.y = 0;
espec->DisplayRect.w = 160;
espec->DisplayRect.h = 152;
if(espec->VideoFormatChanged)
ngpgfx_set_pixel_format(NGPGfx, espec->surface->depth);
if(espec->SoundFormatChanged)
MDFNNGPC_SetSoundRate(espec->SoundRate);
NGPJoyLatch = *chee;
storeB(0x6F82, *chee);
MDFNMP_ApplyPeriodicCheats();
ngpc_soundTS = 0;
NGPFrameSkip = espec->skip;
do
{
int32 timetime = (uint8)TLCS900h_interpret();
MeowMeow |= updateTimers(espec->surface, timetime);
z80_runtime += timetime;
while(z80_runtime > 0)
{
int z80rantime = Z80_RunOP();
if (z80rantime < 0) // Z80 inactive, so take up all run time!
{
z80_runtime = 0;
break;
}
z80_runtime -= z80rantime << 1;
}
}while(!MeowMeow);
espec->MasterCycles = ngpc_soundTS;
espec->SoundBufSize = MDFNNGPCSOUND_Flush(espec->SoundBuf,
espec->SoundBufMaxSize);
}
void reset(void)
{
ngpgfx_power(NGPGfx);
Z80_reset();
reset_int();
reset_timers();
reset_memory();
BIOSHLE_Reset();
reset_registers(); /* TLCS900H registers */
reset_dma();
}
static int Load(const char *name, MDFNFILE *fp, const uint8_t *data, size_t size)
{
if ((data != NULL) && (size != 0)) {
if (!(ngpc_rom.data = (uint8 *)malloc(size)))
return(0);
ngpc_rom.length = size;
memcpy(ngpc_rom.data, data, size);
}
else
{
if(!(ngpc_rom.data = (uint8 *)malloc(fp->size)))
return(0);
ngpc_rom.length = fp->size;
memcpy(ngpc_rom.data, fp->data, fp->size);
}
rom_loaded();
MDFNMP_Init(1024, 1024 * 1024 * 16 / 1024);
NGPGfx = (ngpgfx_t*)calloc(1, sizeof(*NGPGfx));
NGPGfx->layer_enable = 1 | 2 | 4;
MDFNGameInfo->fps = (uint32)((uint64)6144000 * 65536 * 256 / 515 / 198); // 3072000 * 2 * 10000 / 515 / 198
MDFNNGPCSOUND_Init();
MDFNMP_AddRAM(16384, 0x4000, CPUExRAM);
SetFRM(); // Set up fast read memory mapping
bios_install();
z80_runtime = 0;
reset();
return(1);
}
static void CloseGame(void)
{
rom_unload();
if (NGPGfx)
free(NGPGfx);
NGPGfx = NULL;
}
static void SetInput(int port, const char *type, void *ptr)
{
if(!port)
chee = (uint8 *)ptr;
}
extern "C" int StateAction(StateMem *sm, int load, int data_only)
{
SFORMAT StateRegs[] =
{
SFVAR(z80_runtime),
SFARRAY(CPUExRAM, 16384),
SFVAR(FlashStatusEnable),
SFEND
};
SFORMAT TLCS_StateRegs[] =
{
SFVARN(pc, "PC"),
SFVARN(sr, "SR"),
SFVARN(f_dash, "F_DASH"),
SFARRAY32N(gpr, 4, "GPR"),
SFARRAY32N(gprBank[0], 4, "GPRB0"),
SFARRAY32N(gprBank[1], 4, "GPRB1"),
SFARRAY32N(gprBank[2], 4, "GPRB2"),
SFARRAY32N(gprBank[3], 4, "GPRB3"),
SFEND
};
if(!MDFNSS_StateAction(sm, load, data_only, StateRegs, "MAIN", false))
return(0);
if(!MDFNSS_StateAction(sm, load, data_only, TLCS_StateRegs, "TLCS", false))
return(0);
if(!MDFNNGPCDMA_StateAction(sm, load, data_only))
return(0);
if(!MDFNNGPCSOUND_StateAction(sm, load, data_only))
return(0);
if(!ngpgfx_StateAction(NGPGfx, sm, load, data_only))
return(0);
if(!MDFNNGPCZ80_StateAction(sm, load, data_only))
return(0);
if(!int_timer_StateAction(sm, load, data_only))
return(0);
if(!BIOSHLE_StateAction(sm, load, data_only))
return(0);
if(!FLASH_StateAction(sm, load, data_only))
return(0);
if(load)
{
RecacheFRM();
changedSP();
}
return(1);
}
static void DoSimpleCommand(int cmd)
{
switch(cmd)
{
case MDFN_MSC_POWER:
case MDFN_MSC_RESET:
reset();
break;
}
}
static const MDFNSetting_EnumList LanguageList[] =
{
{ "japanese", 0, "Japanese" },
{ "0", 0 },
{ "english", 1, "English" },
{ "1", 1 },
{ NULL, 0 },
};
static MDFNSetting NGPSettings[] =
{
{ "ngp.language", MDFNSF_EMU_STATE | MDFNSF_UNTRUSTED_SAFE, "Language games should display text in.", NULL, MDFNST_ENUM, "english", NULL, NULL, NULL, NULL, LanguageList },
{ NULL }
};
static void SetLayerEnableMask(uint64 mask)
{
ngpgfx_SetLayerEnableMask(NGPGfx, mask);
}
static const InputDeviceInputInfoStruct IDII[] =
{
{ "up", "UP ↑", 0, IDIT_BUTTON, "down" },
{ "down", "DOWN ↓", 1, IDIT_BUTTON, "up" },
{ "left", "LEFT ←", 2, IDIT_BUTTON, "right" },
{ "right", "RIGHT →", 3, IDIT_BUTTON, "left" },
{ "a", "A", 5, IDIT_BUTTON_CAN_RAPID, NULL },
{ "b", "B", 6, IDIT_BUTTON_CAN_RAPID, NULL },
{ "option", "OPTION", 4, IDIT_BUTTON, NULL },
};
static InputDeviceInfoStruct InputDeviceInfo[] =
{
{
"gamepad",
"Gamepad",
NULL,
NULL,
sizeof(IDII) / sizeof(InputDeviceInputInfoStruct),
IDII,
}
};
static const InputPortInfoStruct PortInfo[] =
{
{ "builtin", "Built-In", sizeof(InputDeviceInfo) / sizeof(InputDeviceInfoStruct), InputDeviceInfo, "gamepad" }
};
static InputInfoStruct InputInfo =
{
sizeof(PortInfo) / sizeof(InputPortInfoStruct),
PortInfo
};
static const FileExtensionSpecStruct KnownExtensions[] =
{
{ ".ngp", "Neo Geo Pocket ROM Image" },
{ ".ngc", "Neo Geo Pocket Color ROM Image" },
{ NULL, NULL }
};
MDFNGI EmulatedNGP = {};
MDFNGI *MDFNGameInfo = &EmulatedNGP;
static void MDFNGI_reset(MDFNGI *gameinfo)
{
gameinfo->Settings = NGPSettings;
gameinfo->MasterClock = MDFN_MASTERCLOCK_FIXED(6144000);
gameinfo->fps = 0;
gameinfo->multires = false; // Multires possible?
gameinfo->lcm_width = 160;
gameinfo->lcm_height = 152;
gameinfo->dummy_separator = NULL;
gameinfo->nominal_width = 160;
gameinfo->nominal_height = 152;
gameinfo->fb_width = 160;
gameinfo->fb_height = 152;
gameinfo->soundchan = 2;
}
static MDFNGI *MDFNI_LoadGame(const char *name)
{
MDFNFILE *GameFile = file_open(name);
if(!GameFile)
goto error;
if(Load(name, GameFile, NULL, 0) <= 0)
goto error;
file_close(GameFile);
GameFile = NULL;
return MDFNGameInfo;
error:
if (GameFile)
file_close(GameFile);
GameFile = NULL;
MDFNGI_reset(MDFNGameInfo);
return(0);
}
static MDFNGI *MDFNI_LoadGameData(const uint8_t *data, size_t size)
{
if(Load("", NULL, data, size) <= 0)
goto error;
return MDFNGameInfo;
error:
MDFNGI_reset(MDFNGameInfo);
return(0);
}
static void MDFNI_CloseGame(void)
{
if(!MDFNGameInfo)
return;
CloseGame();
MDFNGI_reset(MDFNGameInfo);
}
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('.'));
}
static bool update_video = false;
static bool update_audio = false;
#define MEDNAFEN_CORE_NAME_MODULE "ngp"
#define MEDNAFEN_CORE_NAME "Beetle NeoPop"
#define MEDNAFEN_CORE_VERSION "v0.9.36.1"
#define MEDNAFEN_CORE_EXTENSIONS "ngp|ngc|ngpc|npc"
#define MEDNAFEN_CORE_TIMING_FPS 60.25
#define MEDNAFEN_CORE_GEOMETRY_BASE_W 160
#define MEDNAFEN_CORE_GEOMETRY_BASE_H 152
#define MEDNAFEN_CORE_GEOMETRY_MAX_W 160
#define MEDNAFEN_CORE_GEOMETRY_MAX_H 152
#define MEDNAFEN_CORE_GEOMETRY_ASPECT_RATIO (20.0 / 19.0)
#define FB_WIDTH 160
#define FB_HEIGHT 152
#define FB_MAX_HEIGHT FB_HEIGHT
const char *mednafen_core_str = MEDNAFEN_CORE_NAME;
static void check_system_specs(void)
{
unsigned level = 0;
environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
}
static void check_color_depth(void)
{
if (RETRO_PIX_DEPTH == 24)
{
enum retro_pixel_format rgb888 = RETRO_PIXEL_FORMAT_XRGB8888;
if(!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &rgb888))
{
if(log_cb) log_cb(RETRO_LOG_ERROR, "Pixel format XRGB8888 not supported by platform.\n");
RETRO_PIX_BYTES = 2;
RETRO_PIX_DEPTH = 15;
}
}
if (RETRO_PIX_BYTES == 2)
{
#if defined(FRONTEND_SUPPORTS_RGB565)
enum retro_pixel_format rgb565 = RETRO_PIXEL_FORMAT_RGB565;
if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &rgb565))
{
if(log_cb) log_cb(RETRO_LOG_INFO, "Frontend supports RGB565 - will use that instead of XRGB1555.\n");
RETRO_PIX_DEPTH = 16;
}
#else
enum retro_pixel_format rgb555 = RETRO_PIXEL_FORMAT_0RGB1555;
if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &rgb555))
{
if(log_cb) log_cb(RETRO_LOG_INFO, "Using default 0RGB1555 pixel format.\n");
RETRO_PIX_DEPTH = 15;
}
#endif
}
}
static void check_variables(void)
{
struct retro_variable var = {0};
var.key = "ngp_language";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
if (!strcmp(var.value, "japanese"))
setting_ngp_language = 0;
else if (!strcmp(var.value, "english"))
setting_ngp_language = 1;
else
setting_ngp_language = 1;
}
else
setting_ngp_language = 1;
var.key = "ngp_sound_sample_rate";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
int old_value = RETRO_SAMPLE_RATE;
RETRO_SAMPLE_RATE = atoi(var.value);
if (old_value != RETRO_SAMPLE_RATE)
update_audio = true;
}
var.key = "ngp_gfx_colors";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
int old_value = RETRO_PIX_BYTES;
if (strcmp(var.value, "16bit") == 0)
{
RETRO_PIX_BYTES = 2;
RETRO_PIX_DEPTH = 16;
}
else if (strcmp(var.value, "24bit") == 0)
{
RETRO_PIX_BYTES = 4;
RETRO_PIX_DEPTH = 24;
}
if (old_value != RETRO_PIX_BYTES)
update_video = true;
}
}
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;
const char *dir = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
{
std::string retro_base_dir_tmp;
retro_base_dir_tmp = dir;
// Make sure that we don't have any lingering slashes, etc, as they break Windows.
size_t last = retro_base_dir_tmp.find_last_not_of("/\\");
if (last != std::string::npos)
last++;
retro_base_dir_tmp= retro_base_dir_tmp.substr(0, last);
strcpy(retro_base_directory, retro_base_dir_tmp.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)
{
std::string retro_save_dir_tmp;
// If save directory is defined use it, otherwise use system directory
retro_save_dir_tmp = *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_dir_tmp.find_last_not_of("/\\");
if (last != std::string::npos)
last++;
retro_save_dir_tmp = retro_save_dir_tmp.substr(0, last);
strcpy(retro_save_directory, retro_save_dir_tmp.c_str());
}
else
{
/* TODO: Add proper fallback */
if (log_cb)
log_cb(RETRO_LOG_WARN, "Save directory is not defined. Fallback on using SYSTEM directory ...\n");
strcpy(retro_save_directory, retro_base_directory);
}
perf_get_cpu_features_cb = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_PERF_INTERFACE, &perf_cb))
perf_get_cpu_features_cb = perf_cb.get_cpu_features;
if (environ_cb(RETRO_ENVIRONMENT_GET_INPUT_BITMASKS, NULL))
libretro_supports_bitmasks = true;
check_system_specs();
}
void retro_reset(void)
{
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)
{
case 0:
default:
*ptr = number;
break;
}
}
#define MAX_PLAYERS 1
#define MAX_BUTTONS 7
static uint8_t input_buf;
static void hookup_ports(bool force)
{
if (initial_ports_hookup && !force)
return;
SetInput(0, "gamepad", &input_buf);
initial_ports_hookup = true;
}
bool retro_load_game(const struct retro_game_info *info)
{
if (!info || failed_init)
return false;
struct retro_input_descriptor desc[] = {
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "A" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "B" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Option" },
{ 0 },
};
environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);
overscan = false;
environ_cb(RETRO_ENVIRONMENT_GET_OVERSCAN, &overscan);
set_basename(info->path);
check_variables();
check_color_depth();
#ifdef LOAD_FROM_MEMORY
game = MDFNI_LoadGameData((const uint8_t *)info->data, info->size);
#else
game = MDFNI_LoadGame(info->path);
#endif
if (!game)
return false;
MDFN_LoadGameCheats(NULL);
MDFNMP_InstallReadPatches();
surf = (MDFN_Surface*)calloc(1, sizeof(*surf));
if (!surf)
return false;
surf->width = FB_WIDTH;
surf->height = FB_HEIGHT;
surf->pitch = FB_WIDTH;
surf->depth = RETRO_PIX_DEPTH;
surf->pixels = (uint16_t*)calloc(1, FB_WIDTH * FB_HEIGHT * sizeof(uint32_t));
if (!surf->pixels)
{
free(surf);
return false;
}
hookup_ports(true);
ngpgfx_set_pixel_format(NGPGfx, RETRO_PIX_DEPTH);
MDFNNGPC_SetSoundRate(RETRO_SAMPLE_RATE);
update_video = false;
update_audio = false;
return game;
}
void retro_unload_game(void)
{
if (!game)
return;
MDFN_FlushGameCheats(0);
MDFNI_CloseGame();
MDFNMP_Kill();
if (surf)
{
if (surf->pixels)
free(surf->pixels);
free(surf);
}
surf = NULL;
}
static void update_input(void)
{
// RetroPad -> ALU button mapping
// JOYPAD_Y -> A
// JOYPAD_B -> B
// JOYPAD_A -> C
// JOYPAD_L -> X
// JOYPAD_X -> Y
// JOYPAD_R -> Z
static unsigned map[] = {
1 << RETRO_DEVICE_ID_JOYPAD_UP, //X Cursor horizontal-layout games
1 << RETRO_DEVICE_ID_JOYPAD_DOWN, //X Cursor horizontal-layout games
1 << RETRO_DEVICE_ID_JOYPAD_LEFT, //X Cursor horizontal-layout games
1 << RETRO_DEVICE_ID_JOYPAD_RIGHT, //X Cursor horizontal-layout games
1 << RETRO_DEVICE_ID_JOYPAD_Y,
1 << RETRO_DEVICE_ID_JOYPAD_B,
(1 << RETRO_DEVICE_ID_JOYPAD_L) | (1 << RETRO_DEVICE_ID_JOYPAD_START),
};
unsigned i, j;
int16_t ret = 0;
input_buf = 0;
if (libretro_supports_bitmasks)
ret = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_MASK);
else
{
for (j = 0; j < (RETRO_DEVICE_ID_JOYPAD_R3+1); j++)
if (input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, j))
ret |= (1 << j);
}
for (i = 0; i < MAX_BUTTONS; i++)
if ((map[i] != -1u) && (ret & map[i]))
input_buf |= (1 << i);
}
static uint64_t video_frames, audio_frames;
void retro_run(void)
{
int total = 0;
int32 SoundBufSize;
unsigned width, height;
static int16_t sound_buf[0x10000];
static MDFN_Rect rects[FB_MAX_HEIGHT];
EmulateSpecStruct spec = {0};
bool updated = false;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
check_variables();
input_poll_cb();
update_input();
rects[0].w = ~0;
spec.surface = surf;
spec.SoundRate = RETRO_SAMPLE_RATE;
spec.SoundBuf = sound_buf;
spec.LineWidths = rects;
spec.SoundBufMaxSize = sizeof(sound_buf) / 2;
spec.SoundVolume = 1.0;
spec.soundmultiplier = 1.0;
spec.SoundBufSize = 0;
spec.VideoFormatChanged = update_video;
spec.SoundFormatChanged = update_audio;
if (update_video || update_audio)
{
struct retro_system_av_info system_av_info;
if (update_video)
{
memset(&system_av_info, 0, sizeof(system_av_info));
environ_cb(RETRO_ENVIRONMENT_SET_SYSTEM_AV_INFO, &system_av_info);
}
retro_get_system_av_info(&system_av_info);
environ_cb(RETRO_ENVIRONMENT_SET_SYSTEM_AV_INFO, &system_av_info);
surf->depth = RETRO_PIX_DEPTH;
update_video = false;
update_audio = false;
}
Emulate(&spec);
SoundBufSize = spec.SoundBufSize - spec.SoundBufSizeALMS;
spec.SoundBufSize = spec.SoundBufSizeALMS + SoundBufSize;
width = spec.DisplayRect.w;
height = spec.DisplayRect.h;
video_cb(surf->pixels, width, height, FB_WIDTH * RETRO_PIX_BYTES);
video_frames++;
audio_frames += spec.SoundBufSize;
for (total = 0; total < spec.SoundBufSize; )
total += audio_batch_cb(spec.SoundBuf + total*2, spec.SoundBufSize - total);
}
void retro_get_system_info(struct retro_system_info *info)
{
memset(info, 0, sizeof(*info));
info->library_name = MEDNAFEN_CORE_NAME;
#ifndef GIT_VERSION
#define GIT_VERSION ""
#endif
#ifdef LOAD_FROM_MEMORY
info->need_fullpath = false;
#else
info->need_fullpath = true;
#endif
info->library_version = MEDNAFEN_CORE_VERSION GIT_VERSION;
info->valid_extensions = MEDNAFEN_CORE_EXTENSIONS;
info->block_extract = false;
}
void retro_get_system_av_info(struct retro_system_av_info *info)
{
memset(info, 0, sizeof(*info));
info->timing.fps = MEDNAFEN_CORE_TIMING_FPS;
info->timing.sample_rate = RETRO_SAMPLE_RATE;
info->geometry.base_width = MEDNAFEN_CORE_GEOMETRY_BASE_W;
info->geometry.base_height = MEDNAFEN_CORE_GEOMETRY_BASE_H;
info->geometry.max_width = MEDNAFEN_CORE_GEOMETRY_MAX_W;
info->geometry.max_height = MEDNAFEN_CORE_GEOMETRY_MAX_H;
info->geometry.aspect_ratio = MEDNAFEN_CORE_GEOMETRY_ASPECT_RATIO;
check_color_depth();
}
void retro_deinit(void)
{
if (surf)
{
if (surf->pixels)
free(surf->pixels);
free(surf);
}
surf = NULL;
if (log_cb)
{
log_cb(RETRO_LOG_INFO, "[%s]: Samples / Frame: %.5f\n",
mednafen_core_str, (double)audio_frames / video_frames);
log_cb(RETRO_LOG_INFO, "[%s]: Estimated FPS: %.5f\n",
mednafen_core_str, (double)video_frames * 44100 / audio_frames);
}
libretro_supports_bitmasks = false;
}
unsigned retro_get_region(void)
{
return RETRO_REGION_NTSC; // FIXME: Regions for other cores.
}
unsigned retro_api_version(void)
{
return RETRO_API_VERSION;
}
void retro_set_controller_port_device(unsigned in_port, unsigned device)
{
}
void retro_set_environment(retro_environment_t cb)
{
struct retro_vfs_interface_info vfs_iface_info;
environ_cb = cb;
libretro_set_core_options(environ_cb);
vfs_iface_info.required_interface_version = 1;
vfs_iface_info.iface = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VFS_INTERFACE, &vfs_iface_info))
filestream_vfs_init(&vfs_iface_info);
}
void retro_set_audio_sample(retro_audio_sample_t cb)
{
}
void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb)
{
audio_batch_cb = cb;
}
void retro_set_input_poll(retro_input_poll_t cb)
{
input_poll_cb = cb;
}
void retro_set_input_state(retro_input_state_t cb)
{
input_state_cb = cb;
}
void retro_set_video_refresh(retro_video_refresh_t cb)
{
video_cb = cb;
}
static size_t serialize_size;
size_t retro_serialize_size(void)
{
StateMem st;
st.data = NULL;
st.loc = 0;
st.len = 0;
st.malloced = 0;
st.initial_malloc = 0;
if (!MDFNSS_SaveSM(&st, 0, 0, NULL, NULL, NULL))
return 0;
free(st.data);
return serialize_size = st.len;
}
bool retro_serialize(void *data, size_t size)
{
StateMem st;
bool ret = false;
uint8_t *_dat = (uint8_t*)malloc(size);
if (!_dat)
return false;
/* Mednafen can realloc the buffer so we need to ensure this is safe. */
st.data = _dat;
st.loc = 0;
st.len = 0;
st.malloced = size;
st.initial_malloc = 0;
ret = MDFNSS_SaveSM(&st, 0, 0, NULL, NULL, NULL);
memcpy(data, st.data, size);
free(st.data);
return ret;
}
bool retro_unserialize(const void *data, size_t size)
{
StateMem st;
st.data = (uint8_t*)data;
st.loc = 0;
st.len = size;
st.malloced = 0;
st.initial_malloc = 0;
return MDFNSS_LoadSM(&st, 0, 0);
}