-
-
Notifications
You must be signed in to change notification settings - Fork 312
/
libretro.cpp
1599 lines (1375 loc) · 44 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#include "SoundRetro.h"
#include "libretro.h"
#include "../System.h"
#include "../Util.h"
#include "../apu/Blip_Buffer.h"
#include "../apu/Gb_Apu.h"
#include "../apu/Gb_Oscs.h"
#include "../common/Port.h"
#include "../common/ConfigManager.h"
#include "../gba/Cheats.h"
#include "../gba/EEprom.h"
#include "../gba/Flash.h"
#include "../gba/GBAGfx.h"
#include "../gba/Globals.h"
#include "../gba/RTC.h"
#include "../gba/Sound.h"
#include "../gba/bios.h"
#include "../gb/gb.h"
#include "../gb/gbCheats.h"
#include "../gb/gbGlobals.h"
#include "../gb/gbMemory.h"
#include "../gb/gbSGB.h"
#include "../gb/gbSound.h"
#define RETRO_DEVICE_GBA RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_JOYPAD, 0)
#define RETRO_DEVICE_GBA_ALT1 RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_JOYPAD, 1)
#define RETRO_DEVICE_GBA_ALT2 RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_JOYPAD, 2)
static retro_log_printf_t log_cb;
static retro_video_refresh_t video_cb;
static retro_input_poll_t poll_cb;
static retro_input_state_t input_cb;
static retro_environment_t environ_cb;
retro_audio_sample_batch_t audio_batch_cb;
static char retro_system_directory[4096];
static char biosfile[4096];
static float sndFiltering = 0.5f;
static bool sndInterpolation = true;
static bool can_dupe = false;
static bool usebios = false;
static unsigned retropad_layout = 1;
static const double FramesPerSecond = (16777216.0 / 280896.0); // 59.73
static const long SampleRate = 32768;
static const int GBWidth = 160;
static const int GBHeight = 144;
static const int SGBWidth = 256;
static const int SGBHeight = 224;
static const int GBAWidth = 240;
static const int GBAHeight = 160;
static unsigned width = 240;
static unsigned height = 160;
static EmulatedSystem* core = NULL;
static IMAGE_TYPE type = IMAGE_UNKNOWN;
uint16_t systemColorMap16[0x10000];
uint32_t systemColorMap32[0x10000];
int RGB_LOW_BITS_MASK = 0;
int systemRedShift = 0;
int systemBlueShift = 0;
int systemGreenShift = 0;
int systemColorDepth = 32;
int systemDebug = 0;
int systemVerbose = 0;
int systemFrameSkip = 0;
int systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
int systemSpeed = 0;
int emulating = 0;
void (*dbgOutput)(const char* s, uint32_t addr);
void (*dbgSignal)(int sig, int number);
// Dummy vars/funcs for serial io emulation without LINK communication related stuff
#ifndef NO_LINK
#include "../gba/GBALink.h"
uint8_t gbSIO_SC;
bool LinkIsWaiting;
bool LinkFirstTime;
bool EmuReseted;
int winGbPrinterEnabled;
bool gba_joybus_active = false;
LinkMode GetLinkMode()
{
return LINK_DISCONNECTED;
}
void StartGPLink(uint16_t value)
{
}
void LinkUpdate(int ticks)
{
}
void StartLink(uint16_t siocnt)
{
}
void CheckLinkConnection()
{
}
void gbInitLink()
{
LinkIsWaiting = false;
LinkFirstTime = true;
}
uint16_t gbLinkUpdate(uint8_t b, int gbSerialOn) //used on external clock
{
return (b << 8);
}
#endif
#define GS555(x) (x | (x << 5) | (x << 10))
uint16_t systemGbPalette[24] = {
GS555(0x1f), GS555(0x15), GS555(0x0c), 0,
GS555(0x1f), GS555(0x15), GS555(0x0c), 0,
GS555(0x1f), GS555(0x15), GS555(0x0c), 0,
GS555(0x1f), GS555(0x15), GS555(0x0c), 0,
GS555(0x1f), GS555(0x15), GS555(0x0c), 0,
GS555(0x1f), GS555(0x15), GS555(0x0c), 0
};
extern int gbRomType; // gets type from header 0x147
extern int gbBattery; // enabled when gbRamSize != 0
static bool gb_hasbattery(void)
{
switch (gbRomType) {
case 0x03: // MBC1
case 0xff: // MBC1
case 0x0d: // MM01
case 0x0f:
case 0x10: // MBC3 + extended
case 0x13:
case 0xfc: // MBC3
case 0x1b:
case 0x1e: // MBC5
//incomplete, does not save gbTAMA5ram
case 0xfd: // TAMA5 + extended
case 0x06: // MBC2
case 0x22: // MBC7
return true;
}
return false;
}
static bool gb_hasrtc(void)
{
switch (gbRomType) {
case 0x0f:
case 0x10: // MBC3 + extended
case 0x13:
case 0xfd: // TAMA5 + extended
return true;
}
return false;
}
static void* gb_rtcdata_prt(void)
{
if (gb_hasrtc()) {
switch (gbRomType) {
case 0x0f:
case 0x10: // MBC3 + extended
return &gbDataMBC3.mapperSeconds;
case 0x13:
case 0xfd: // TAMA5 + extended
return &gbDataTAMA5.mapperSeconds;
}
}
return NULL;
}
static size_t gb_rtcdata_size(void)
{
if (gb_hasrtc()) {
switch (gbRomType) {
case 0x0f:
case 0x10: // MBC3 + extended
return (10 * sizeof(int) + sizeof(time_t));
break;
case 0x13:
case 0xfd: // TAMA5 + extended
return (14 * sizeof(int) + sizeof(time_t));
break;
}
}
return 0;
}
static void gbUpdateRTC(void)
{
if (gb_hasrtc()) {
struct tm* lt;
time_t rawtime;
time(&rawtime);
lt = localtime(&rawtime);
switch (gbRomType) {
case 0x0f:
case 0x10: {
gbDataMBC3.mapperSeconds = lt->tm_sec;
gbDataMBC3.mapperMinutes = lt->tm_min;
gbDataMBC3.mapperHours = lt->tm_hour;
gbDataMBC3.mapperDays = lt->tm_yday & 255;
gbDataMBC3.mapperControl = (gbDataMBC3.mapperControl & 0xfe) | (lt->tm_yday > 255 ? 1 : 0);
gbDataMBC3.mapperLastTime = rawtime;
}
break;
case 0xfd: {
uint8_t gbDaysinMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
gbDataTAMA5.mapperSeconds = lt->tm_sec;
gbDataTAMA5.mapperMinutes = lt->tm_min;
gbDataTAMA5.mapperHours = lt->tm_hour;
gbDataTAMA5.mapperDays = 1;
gbDataTAMA5.mapperMonths = 1;
gbDataTAMA5.mapperYears = 1970;
gbDataTAMA5.mapperLastTime = rawtime;
int days = lt->tm_yday + 365 * 3;
while (days) {
gbDataTAMA5.mapperDays++;
days--;
if (gbDataTAMA5.mapperDays > gbDaysinMonth[gbDataTAMA5.mapperMonths - 1]) {
gbDataTAMA5.mapperDays = 1;
gbDataTAMA5.mapperMonths++;
if (gbDataTAMA5.mapperMonths > 12) {
gbDataTAMA5.mapperMonths = 1;
gbDataTAMA5.mapperYears++;
if ((gbDataTAMA5.mapperYears & 3) == 0)
gbDaysinMonth[1] = 29;
else
gbDaysinMonth[1] = 28;
}
}
}
gbDataTAMA5.mapperControl = (gbDataTAMA5.mapperControl & 0xfe) | (lt->tm_yday > 255 ? 1 : 0);
}
break;
}
}
}
void* retro_get_memory_data(unsigned id)
{
if (type == IMAGE_GBA) {
switch (id) {
case RETRO_MEMORY_SAVE_RAM:
if ((saveType == 1) | (saveType == 4))
return eepromData;
if ((saveType == 2) | (saveType == 3))
return flashSaveMemory;
return NULL;
case RETRO_MEMORY_SYSTEM_RAM:
return workRAM;
case RETRO_MEMORY_VIDEO_RAM:
return vram;
}
}
else if (type == IMAGE_GB) {
switch (id) {
case RETRO_MEMORY_SAVE_RAM:
if (gb_hasbattery())
return gbRam;
return NULL;
case RETRO_MEMORY_SYSTEM_RAM:
return gbMemoryMap[0x0c];
case RETRO_MEMORY_VIDEO_RAM:
return gbMemoryMap[0x08] ;
}
}
return NULL;
}
size_t retro_get_memory_size(unsigned id)
{
if (type == IMAGE_GBA) {
switch (id) {
case RETRO_MEMORY_SAVE_RAM:
if ((saveType == 1) | (saveType == 4))
return eepromSize;
if ((saveType == 2) | (saveType == 3))
return flashSize;
return 0;
case RETRO_MEMORY_SYSTEM_RAM:
return 0x40000;
case RETRO_MEMORY_VIDEO_RAM:
return 0x20000;
}
}
else if (type == IMAGE_GB) {
switch (id) {
case RETRO_MEMORY_SAVE_RAM:
if (gb_hasbattery())
return gbRamSize;
return 0;
case RETRO_MEMORY_SYSTEM_RAM:
return 0x2000;
case RETRO_MEMORY_VIDEO_RAM:
return 0x2000;
}
}
return 0;
}
unsigned retro_api_version(void)
{
return RETRO_API_VERSION;
}
void retro_set_video_refresh(retro_video_refresh_t cb)
{
video_cb = cb;
}
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)
{
poll_cb = cb;
}
void retro_set_input_state(retro_input_state_t cb)
{
input_cb = cb;
}
static void update_input_descriptors(void);
void retro_set_controller_port_device(unsigned port, unsigned device)
{
if (port > 0) return; // GBA Only supports 1 controller
log_cb(RETRO_LOG_INFO, "Controller %d'\n", device);
switch (device) {
case RETRO_DEVICE_JOYPAD:
case RETRO_DEVICE_GBA:
default:
retropad_layout = 1;
break;
case RETRO_DEVICE_GBA_ALT1:
retropad_layout = 2;
break;
case RETRO_DEVICE_GBA_ALT2:
retropad_layout = 3;
break;
case RETRO_DEVICE_NONE:
retropad_layout = 0;
break;
}
update_input_descriptors();
}
void retro_set_environment(retro_environment_t cb)
{
environ_cb = cb;
struct retro_variable variables[] = {
{ "vbam_solarsensor", "Solar Sensor Level; 0|1|2|3|4|5|6|7|8|9|10" },
{ "vbam_usebios", "Use BIOS file (Restart); disabled|enabled" },
{ "vbam_soundinterpolation", "Sound Interpolation; disabled|enabled" },
{ "vbam_soundfiltering", "Sound Filtering; 5|6|7|8|9|10|0|1|2|3|4" },
{ "vbam_gbHardware", "(GB) Emulated Hardware; Automatic|Game Boy Color|Super Game Boy|Game Boy|Game Boy Advance|Super Game Boy 2" },
{ "vbam_showborders", "(GB) Show Borders; disabled|enabled|auto" },
{ "vbam_turboenable", "Enable Turbo Buttons; disabled|enabled" },
{ "vbam_turbodelay", "Turbo Delay (in frames); 3|4|5|6|7|8|9|10|11|12|13|14|15|1|2" },
{ "vbam_layer_1", "Show layer 1; enabled|disabled" },
{ "vbam_layer_2", "Show layer 2; enabled|disabled" },
{ "vbam_layer_3", "Show layer 3; enabled|disabled" },
{ "vbam_layer_4", "Show layer 4; enabled|disabled" },
{ "vbam_layer_5", "Show sprite layer; enabled|disabled" },
{ "vbam_layer_6", "Show window layer 1; enabled|disabled" },
{ "vbam_layer_7", "Show window layer 2; enabled|disabled" },
{ "vbam_layer_8", "Show sprite window layer; enabled|disabled" },
{ "vbam_sound_1", "Sound channel 1; enabled|disabled" },
{ "vbam_sound_2", "Sound channel 2; enabled|disabled" },
{ "vbam_sound_3", "Sound channel 3; enabled|disabled" },
{ "vbam_sound_4", "Sound channel 4; enabled|disabled" },
{ "vbam_sound_5", "Direct Sound A; enabled|disabled" },
{ "vbam_sound_6", "Direct Sound B; enabled|disabled" },
{ NULL, NULL },
};
static const struct retro_controller_description port_1[] = {
{ "GBA Joypad", RETRO_DEVICE_JOYPAD },
{ "Alt Joypad YB", RETRO_DEVICE_GBA_ALT1 },
{ "Alt Joypad AB", RETRO_DEVICE_GBA_ALT2 },
{ NULL, 0 },
};
static const struct retro_controller_info ports[] = {
{port_1, 3},
{ NULL, 0 }
};
cb(RETRO_ENVIRONMENT_SET_VARIABLES, variables);
cb(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports);
}
void retro_get_system_info(struct retro_system_info *info)
{
info->need_fullpath = false;
info->valid_extensions = "dmg|gb|gbc|cgb|sgb|gba";
#ifdef GIT_VERSION
info->library_version = "2.1.0" GIT_VERSION;
#else
info->library_version = "2.1.0-GIT";
#endif
info->library_name = "VBA-M";
info->block_extract = false;
}
void retro_get_system_av_info(struct retro_system_av_info *info)
{
float aspect = (3.0f / 2.0f);
if (type == IMAGE_GB)
aspect = !gbBorderOn ? (10.0 / 9.0) : (8.0 / 7.0);
info->geometry.base_width = width;
info->geometry.base_height = height;
info->geometry.max_width = width;
info->geometry.max_height = height;
info->geometry.aspect_ratio = aspect;
info->timing.fps = FramesPerSecond;
info->timing.sample_rate = (double)SampleRate;
}
void retro_init(void)
{
struct retro_log_callback log;
environ_cb(RETRO_ENVIRONMENT_GET_CAN_DUPE, &can_dupe);
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)
snprintf(retro_system_directory, sizeof(retro_system_directory), "%s", dir);
#ifdef 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");
#else
enum retro_pixel_format rgb8888 = RETRO_PIXEL_FORMAT_XRGB8888;
if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &rgb8888) && log_cb)
log_cb(RETRO_LOG_INFO, "Frontend supports XRGB8888 - will use that instead of XRGB1555.\n");
#endif
bool yes = true;
environ_cb(RETRO_ENVIRONMENT_SET_SUPPORT_ACHIEVEMENTS, &yes);
}
static const char *gbGetCartridgeType(void)
{
const char *type = "Unknown";
switch (gbRom[0x147]) {
case 0x00:
type = "ROM";
break;
case 0x01:
type = "ROM+MBC1";
break;
case 0x02:
type = "ROM+MBC1+RAM";
break;
case 0x03:
type = "ROM+MBC1+RAM+BATTERY";
break;
case 0x05:
type = "ROM+MBC2";
break;
case 0x06:
type = "ROM+MBC2+BATTERY";
break;
case 0x0b:
type = "ROM+MMM01";
break;
case 0x0c:
type = "ROM+MMM01+RAM";
break;
case 0x0d:
type = "ROM+MMM01+RAM+BATTERY";
break;
case 0x0f:
type = "ROM+MBC3+TIMER+BATTERY";
break;
case 0x10:
type = "ROM+MBC3+TIMER+RAM+BATTERY";
break;
case 0x11:
type = "ROM+MBC3";
break;
case 0x12:
type = "ROM+MBC3+RAM";
break;
case 0x13:
type = "ROM+MBC3+RAM+BATTERY";
break;
case 0x19:
type = "ROM+MBC5";
break;
case 0x1a:
type = "ROM+MBC5+RAM";
break;
case 0x1b:
type = "ROM+MBC5+RAM+BATTERY";
break;
case 0x1c:
type = "ROM+MBC5+RUMBLE";
break;
case 0x1d:
type = "ROM+MBC5+RUMBLE+RAM";
break;
case 0x1e:
type = "ROM+MBC5+RUMBLE+RAM+BATTERY";
break;
case 0x22:
type = "ROM+MBC7+BATTERY";
break;
case 0x55:
type = "GameGenie";
break;
case 0x56:
type = "GameShark V3.0";
break;
case 0xfc:
type = "ROM+POCKET CAMERA";
break;
case 0xfd:
type = "ROM+BANDAI TAMA5";
break;
case 0xfe:
type = "ROM+HuC-3";
break;
case 0xff:
type = "ROM+HuC-1";
break;
}
return (type);
}
static const char *gbGetSaveRamSize(void)
{
const char *type = "Unknown";
switch (gbRom[0x149]) {
case 0:
type = "None";
break;
case 1:
type = "2K";
break;
case 2:
type = "8K";
break;
case 3:
type = "32K";
break;
case 4:
type = "128K";
break;
case 5:
type = "64K";
break;
}
return (type);
}
typedef struct {
char romtitle[256];
char romid[5];
int saveSize; // also can override eeprom size
int saveType; // 0auto 1eeprom 2sram 3flash 4sensor+eeprom 5none
int rtcEnabled;
int mirroringEnabled;
int useBios;
} ini_t;
static const ini_t gbaover[512] = {
#include "gba-over.inc"
};
static int romSize = 0;
static void load_image_preferences(void)
{
const char *savetype[] = {
"AUTO",
"EEPROM",
"SRAM",
"FLASH",
"SENSOR + EEPROM",
"NONE"
};
char buffer[5];
buffer[0] = rom[0xac];
buffer[1] = rom[0xad];
buffer[2] = rom[0xae];
buffer[3] = rom[0xaf];
buffer[4] = 0;
cpuSaveType = 0;
flashSize = 0x10000;
eepromSize = 512;
rtcEnabled = false;
mirroringEnable = false;
log("GameID in ROM is: %s\n", buffer);
bool found = false;
int found_no = 0;
for (int i = 0; i < 512; i++) {
if (!strcmp(gbaover[i].romid, buffer)) {
found = true;
found_no = i;
break;
}
}
if (found) {
log("Found ROM in vba-over list.\n");
log("Name : %s\n", gbaover[found_no].romtitle);
cpuSaveType = gbaover[found_no].saveType;
if (gbaover[found_no].saveSize != 0) {
unsigned size = gbaover[found_no].saveSize;
if ((cpuSaveType == 3) && ((size == 65536) || (size == 131072)))
flashSize = size;
else if ((cpuSaveType == 1) && (size == 8192))
eepromSize = 0x2000;
}
rtcEnabled = gbaover[found_no].rtcEnabled;
mirroringEnable = gbaover[found_no].mirroringEnabled;
}
if (!cpuSaveType) {
utilGBAFindSave(romSize);
}
log("romSize : %dKB\n", (romSize + 1023) / 1024);
log("has RTC : %s.\n", rtcEnabled ? "Yes" : "No");
log("cpuSaveType : %s.\n", savetype[cpuSaveType]);
if (cpuSaveType == 3)
log("flashSize : %d.\n", flashSize);
if (cpuSaveType == 1)
log("eepromSize : %d.\n", eepromSize);
log("mirroringEnable : %d.\n", mirroringEnable);
}
static void update_colormaps(void)
{
#ifdef FRONTEND_SUPPORTS_RGB565
systemColorDepth = 16;
systemRedShift = 11;
systemGreenShift = 6;
systemBlueShift = 0;
#else
systemColorDepth = 32;
systemRedShift = 19;
systemGreenShift = 11;
systemBlueShift = 3;
#endif
utilUpdateSystemColorMaps(false);
log("Color Depth = %d\n", systemColorDepth);
}
#ifdef _WIN32
static const char SLASH = '\\';
#else
static const char SLASH = '/';
#endif
static void gba_init(void)
{
log("Loading VBA-M Core (GBA)...\n");
load_image_preferences();
saveType = cpuSaveType;
if (flashSize == 0x10000 || flashSize == 0x20000)
flashSetSize(flashSize);
rtcEnable(rtcEnabled);
rtcEnableRumble(!rtcEnabled);
doMirroring(mirroringEnable);
soundSetSampleRate(SampleRate);
if (usebios) {
snprintf(biosfile, sizeof(biosfile), "%s%c%s", retro_system_directory, SLASH, "gba_bios.bin");
log("Loading bios: %s\n", biosfile);
}
CPUInit(biosfile, usebios);
width = GBAWidth;
height = GBAHeight;
// CPUReset() will reset eepromSize to 512.
// Save current eepromSize override then restore after CPUReset()
int tmp = eepromSize;
CPUReset();
eepromSize = tmp;
}
static void gb_init(void)
{
const char *biosname[] = {"gb_bios.bin", "gbc_bios.bin"};
log("Loading VBA-M Core (GB/GBC)...\n");
gbGetHardwareType();
if (usebios) {
snprintf(biosfile, sizeof(biosfile), "%s%c%s",
retro_system_directory, SLASH, biosname[gbCgbMode]);
log("Loading bios: %s\n", biosfile);
}
gbCPUInit(biosfile, usebios);
if (gbBorderOn) {
width = gbBorderLineSkip = SGBWidth;
height = SGBHeight;
gbBorderColumnSkip = (SGBWidth - GBWidth) >> 1;
gbBorderRowSkip = (SGBHeight - GBHeight) >> 1;
} else {
width = gbBorderLineSkip = GBWidth;
height = GBHeight;
gbBorderColumnSkip = gbBorderRowSkip = 0;
}
gbSoundSetSampleRate(SampleRate);
gbSoundSetDeclicking(1);
gbReset(); // also resets sound;
// VBA-M always updates time based on current time and not in-game time.
// No need to add RTC data to RETRO_MEMORY_RTC, so its safe to place this here.
gbUpdateRTC();
log("Rom size : %02x (%dK)\n", gbRom[0x148], (romSize + 1023) / 1024);
log("Cartridge type : %02x (%s)\n", gbRom[0x147], gbGetCartridgeType());
log("Ram size : %02x (%s)\n", gbRom[0x149], gbGetSaveRamSize());
int i = 0;
uint8_t crc = 25;
for (i = 0x134; i < 0x14d; i++)
crc += gbRom[i];
crc = 256 - crc;
log("CRC : %02x (%02x)\n", crc, gbRom[0x14d]);
uint16_t crc16 = 0;
for (i = 0; i < gbRomSize; i++)
crc16 += gbRom[i];
crc16 -= gbRom[0x14e] + gbRom[0x14f];
log("Checksum : %04x (%04x)\n", crc16, gbRom[0x14e] * 256 + gbRom[0x14f]);
if (gb_hasbattery)
log(": Game supports battery save ram.\n");
if (gbRom[0x143] == 0xc0)
log(": Game works on CGB only\n");
else if (gbRom[0x143] == 0x80)
log(": Game supports GBC functions, GB compatible.\n");
if (gbRom[0x146] == 0x03)
log(": Game supports SGB functions\n");
}
static void gba_soundchanged(void)
{
soundInterpolation = sndInterpolation;
soundFiltering = sndFiltering;
}
void retro_deinit(void)
{
emulating = 0;
core->emuCleanUp();
soundShutdown();
}
void retro_reset(void)
{
// save current eepromSize
int tmp = eepromSize;
core->emuReset();
eepromSize = tmp;
}
#define MAX_BUTTONS 10
static const unsigned binds[4][MAX_BUTTONS] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // placeholder for no input
{
RETRO_DEVICE_ID_JOYPAD_A,
RETRO_DEVICE_ID_JOYPAD_B,
RETRO_DEVICE_ID_JOYPAD_SELECT,
RETRO_DEVICE_ID_JOYPAD_START,
RETRO_DEVICE_ID_JOYPAD_RIGHT,
RETRO_DEVICE_ID_JOYPAD_LEFT,
RETRO_DEVICE_ID_JOYPAD_UP,
RETRO_DEVICE_ID_JOYPAD_DOWN,
RETRO_DEVICE_ID_JOYPAD_R,
RETRO_DEVICE_ID_JOYPAD_L
},
{
RETRO_DEVICE_ID_JOYPAD_B,
RETRO_DEVICE_ID_JOYPAD_Y,
RETRO_DEVICE_ID_JOYPAD_SELECT,
RETRO_DEVICE_ID_JOYPAD_START,
RETRO_DEVICE_ID_JOYPAD_RIGHT,
RETRO_DEVICE_ID_JOYPAD_LEFT,
RETRO_DEVICE_ID_JOYPAD_UP,
RETRO_DEVICE_ID_JOYPAD_DOWN,
RETRO_DEVICE_ID_JOYPAD_R,
RETRO_DEVICE_ID_JOYPAD_L
},
{
RETRO_DEVICE_ID_JOYPAD_B,
RETRO_DEVICE_ID_JOYPAD_A,
RETRO_DEVICE_ID_JOYPAD_SELECT,
RETRO_DEVICE_ID_JOYPAD_START,
RETRO_DEVICE_ID_JOYPAD_RIGHT,
RETRO_DEVICE_ID_JOYPAD_LEFT,
RETRO_DEVICE_ID_JOYPAD_UP,
RETRO_DEVICE_ID_JOYPAD_DOWN,
RETRO_DEVICE_ID_JOYPAD_R,
RETRO_DEVICE_ID_JOYPAD_L
}
};
#define TURBO_BUTTONS 2
static bool turbo_enable = false;
static int turbo_delay = 3;
static int turbo_delay_counter[TURBO_BUTTONS] = {0};
static const unsigned turbo_binds[4][TURBO_BUTTONS] = {
{ 0, 0 }, // placeholder for no input
{
RETRO_DEVICE_ID_JOYPAD_X,
RETRO_DEVICE_ID_JOYPAD_Y
},
{
RETRO_DEVICE_ID_JOYPAD_A,
RETRO_DEVICE_ID_JOYPAD_X
},
{
RETRO_DEVICE_ID_JOYPAD_Y,
RETRO_DEVICE_ID_JOYPAD_X
}
};
static void systemGbBorderOff(void);
static void systemUpdateSolarSensor(int level);
static uint8_t sensorDarkness = 0xE8;
static uint8_t sensorDarknessLevel = 0; // so we can adjust sensor from gamepad
static void update_variables(void)
{
bool sound_changed = false;
char key[256];
struct retro_variable var;
var.key = key;
int disabled_layers=0;
strcpy(key, "vbam_layer_x");
for (int i = 0; i < 8; i++) {
key[strlen("vbam_layer_")] = '1' + i;
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value && var.value[0] == 'd')
disabled_layers |= 0x100 << i;
}
layerSettings = 0xFF00 ^ disabled_layers;
layerEnable = DISPCNT & layerSettings;
CPUUpdateRenderBuffers(false);
int sound_enabled = 0x30F;
strcpy(key, "vbam_sound_x");
for (unsigned i = 0; i < 6; i++) {
key[strlen("vbam_sound_")] = '1' + i;
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value && var.value[0] == 'd') {
unsigned which = (i < 4) ? (1 << i) : (0x100 << (i - 4));
sound_enabled &= ~(which);
}
}
if (soundGetEnable() != sound_enabled)
soundSetEnable(sound_enabled & 0x30F);
var.key = "vbam_soundinterpolation";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
bool newval = (strcmp(var.value, "enabled") == 0);
if (sndInterpolation != newval) {
sndInterpolation = newval;
sound_changed = true;
}
}
var.key = "vbam_soundfiltering";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
float newval = atof(var.value) * 0.1f;
if (sndFiltering != newval) {
sndFiltering = newval;
sound_changed = true;
}
}
if (sound_changed) {
//Update interpolation and filtering values
gba_soundchanged();
}
var.key = "vbam_usebios";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
bool newval = (strcmp(var.value, "enabled") == 0);
usebios = newval;
}
var.key = "vbam_solarsensor";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
sensorDarknessLevel = atoi(var.value);
systemUpdateSolarSensor(sensorDarknessLevel);
}
var.key = "vbam_showborders";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
int oldval = (gbBorderOn << 1) | gbBorderAutomatic;
if (strcmp(var.value, "auto") == 0) {
gbBorderOn = 0;
gbBorderAutomatic = 1;
}
else if (strcmp(var.value, "enabled") == 0) {
gbBorderAutomatic = 0;
gbBorderOn = 1;
}
else { // disabled
gbBorderOn = 0;
gbBorderAutomatic = 0;
}
if ((type == IMAGE_GB) && (oldval != ((gbBorderOn << 1) | gbBorderAutomatic))) {
if (gbBorderOn) {
systemGbBorderOn();
gbSgbRenderBorder();
}
else
systemGbBorderOff();
}