-
Notifications
You must be signed in to change notification settings - Fork 30
/
libretro.c
1343 lines (1152 loc) · 46.6 KB
/
libretro.c
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
#ifndef _MSC_VER
#include <stdbool.h>
#endif
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#ifdef _MSC_VER
#define snprintf _snprintf
#pragma pack(1)
#endif
#include <libretro.h>
#include "libretro_core_options.h"
#include "audio.h"
#include "config.h"
#include "cpu.h"
#include "keyboard.h"
#include "score.h"
#include "vdc.h"
#include "vmachine.h"
#include "voice.h"
#include "vpp.h"
#include "vkeyb/vkeyb.h"
#include "wrapalleg.h"
#ifdef HAVE_VOICE
#include <core_audio_mixer.h>
#include <audio/conversion/float_to_s16.h>
#include <audio/conversion/s16_to_float.h>
#endif
#include <file/file_path.h>
#include <streams/file_stream.h>
#include <encodings/crc32.h>
#include <retro_miscellaneous.h>
static retro_log_printf_t log_cb;
static retro_video_refresh_t video_cb;
static retro_input_poll_t input_poll_cb;
static retro_input_state_t input_state_cb;
static retro_environment_t environ_cb;
static retro_audio_sample_t audio_cb;
static retro_audio_sample_batch_t audio_batch_cb;
static bool libretro_supports_bitmasks = false;
void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
void retro_set_audio_sample(retro_audio_sample_t cb) { audio_cb = 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_destroybmp(void);
char bios_file_name[16] = {0};
uint16_t mbmp[TEX_WIDTH * TEX_HEIGHT];
uint16_t *mbmp_prev = NULL;
static bool crop_overscan = false;
/* Note: 320x240 is not really correct,
* since the actual overscan region varies
* per system and is not strictly defined.
* Cropping to 320x240 does however remove
* any glitchy border content in most games,
* and it optimises the display for devices
* with a native 320x240 resolution (this can
* have a significant beneficial performance
* impact) */
#define CROPPED_WIDTH 320
#define CROPPED_HEIGHT 240
/* 9 is not a typo. The emulated display
* is offset by 1 pixel, so we only crop
* 9 pixels from the left, not 10.
* (This means we lose one row of pixels
* from the active area of the virtual
* keyboard when cropping is enabled,
* but it does not affect usability
* in any meaningful way) */
#define CROPPED_OFFSET_X 9
#define CROPPED_OFFSET_Y 5
/* The internal emulation code produces exactly
* 1056 samples per frame. This leads to sample
* rates of 63360 Hz for NTSC content and 52800 Hz
* for PAL content. Unfortunately, these rates are
* so high and so peculiar that the sinc resampler
* (used for The Voice, and for general resampling
* in the RetroArch frontend) performs abominably,
* creating unacceptable levels of noise and
* distortion. We therefore only use two thirds
* of the internally generated samples (704 per
* frame), which reduces NTSC/PAL sample rates to
* 42240/35200 Hz. This greatly improves resampling
* performance and final output sound quality. */
#define AUDIO_SAMPLES_PER_FRAME ((SOUND_BUFFER_LEN * 2) / 3)
#define AUDIO_SAMPLERATE ((evblclk == EVBLCLK_NTSC) ? (AUDIO_SAMPLES_PER_FRAME * 60) : (AUDIO_SAMPLES_PER_FRAME * 50))
uint8_t soundBuffer[SOUND_BUFFER_LEN];
static int16_t audioOutBuffer[AUDIO_SAMPLES_PER_FRAME * 2];
static int16_t audio_volume = 50;
static float voice_volume = 0.7f;
static bool low_pass_enabled = false;
static int32_t low_pass_range = 0;
static int32_t low_pass_prev = 0;
int RLOOP=0;
int joystick_data[2][5]={{0,0,0,0,0},{0,0,0,0,0}};
static uint8_t p1_index = 0;
static uint8_t p2_index = 1;
int contax, o2flag, g74flag, c52flag, jopflag, helpflag;
extern uint8_t ram[];
/* True if the virtual keyboard must be showed */
static bool vkb_show = false;
struct ButtonsState
{
bool up, down, right, left;
bool select, start;
bool b, y;
};
/* Last state of the buttons for joypad 1 */
struct ButtonsState last_btn_state = { false, false, false, false,
false, false,
false, false };
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);
}
static bool load_bios(const char *biosname)
{
RFILE *bios_file = NULL;
int64_t bytes_read;
uint32_t crc;
size_t i;
if (!biosname)
return false;
bios_file = filestream_open(biosname,
RETRO_VFS_FILE_ACCESS_READ,
RETRO_VFS_FILE_ACCESS_HINT_NONE);
if (!bios_file)
{
log_cb(RETRO_LOG_ERROR, "[O2EM]: Error loading BIOS ROM (%s).\n", biosname);
return false;
}
bytes_read = filestream_read(bios_file, rom_table[0], 1024);
filestream_close(bios_file);
if (bytes_read != 1024)
{
log_cb(RETRO_LOG_ERROR, "[O2EM]: Error loading BIOS ROM (%s).\n", biosname);
return false;
}
for (i=1; i<8; i++)
memcpy(rom_table[i],rom_table[0],1024);
crc = encoding_crc32(0, rom_table[0], 1024);
switch (crc)
{
case 0x8016A315:
log_cb(RETRO_LOG_INFO, "[O2EM]: Magnavox Odyssey2 BIOS ROM loaded (G7000 model)\n");
app_data.vpp = 0;
app_data.bios = ROM_O2;
break;
case 0xE20A9F41:
log_cb(RETRO_LOG_INFO, "[O2EM]: Philips Videopac+ European BIOS ROM loaded (G7400 model)\n");
app_data.vpp = 1;
app_data.bios = ROM_G7400;
break;
case 0xA318E8D6:
if (!((!o2flag)&&(c52flag)))
log_cb(RETRO_LOG_INFO, "[O2EM]: Philips Videopac+ French BIOS ROM loaded (G7000 model)\n");
app_data.vpp = 0;
app_data.bios = ROM_C52;
break;
case 0x11647CA5:
if (g74flag)
log_cb(RETRO_LOG_INFO, "[O2EM]: Philips Videopac+ French BIOS ROM loaded (G7400 model)\n");
app_data.vpp = 1;
app_data.bios = ROM_JOPAC;
break;
default:
log_cb(RETRO_LOG_INFO, "[O2EM]: BIOS ROM loaded (unknown version)\n");
app_data.vpp = 0;
app_data.bios = ROM_UNKNOWN;
break;
}
return true;
}
static bool load_cart(const uint8_t *data, size_t size)
{
int i, nb;
/* Get ROM CRC */
app_data.crc = encoding_crc32(0, data, size);
if (app_data.crc == 0xAFB23F89)
app_data.exrom = 1; /* Musician */
if (app_data.crc == 0x3BFEF56B)
app_data.exrom = 1; /* Four in 1 Row! */
if (app_data.crc == 0x9B5E9356)
app_data.exrom = 1; /* Four in 1 Row! (french) */
if (((app_data.crc == 0x975AB8DA) || (app_data.crc == 0xE246A812)))
{
log_cb(RETRO_LOG_ERROR, "[O2EM]: Loaded content is an incomplete ROM dump.\n");
return false;
}
if ((size % 1024) != 0)
{
log_cb(RETRO_LOG_ERROR, "[O2EM]: Error: Loaded content is an invalid ROM dump.\n");
return false;
}
/* special MegaCART design by Soeren Gust */
if ((size == 32768) ||
(size == 65536) ||
(size == 131072) ||
(size == 262144) ||
(size == 524288) ||
(size == 1048576))
{
app_data.megaxrom = 1;
app_data.bank = 1;
megarom = malloc(1048576);
if (!megarom)
{
log_cb(RETRO_LOG_ERROR, "[O2EM]: Out of memory while processing loaded content.\n");
return false;
}
memcpy(megarom, data, size);
/* mirror shorter files into full megabyte */
if (size < 65536)
memcpy(megarom + 32768, megarom, 32768);
if (size < 131072)
memcpy(megarom + 65536, megarom, 65536);
if (size < 262144)
memcpy(megarom + 131072, megarom, 131072);
if (size < 524288)
memcpy(megarom + 262144, megarom, 262144);
if (size < 1048576)
memcpy(megarom + 524288, megarom, 524288);
/* start in bank 0xff */
memcpy(&rom_table[0][1024], megarom + 4096*255 + 1024, 3072);
log_cb(RETRO_LOG_INFO, "[O2EM]: MegaCart %luK\n", (unsigned long)(size / 1024));
nb = 1;
}
else if (((size % 3072) == 0))
{
app_data.three_k = 1;
nb = size / 3072;
for (i = (nb - 1); i >= 0; i--)
{
memcpy(&rom_table[i][1024], data, 3072);
data += 3072;
}
log_cb(RETRO_LOG_INFO, "[O2EM]: %uK\n", (unsigned)(nb * 3));
}
else
{
nb = size / 2048;
if ((nb == 2) && (app_data.exrom))
{
memcpy(&extROM[0], data, 1024);
data += 1024;
memcpy(&rom_table[0][1024], data, 3072);
data += 3072;
log_cb(RETRO_LOG_INFO, "[O2EM]: 3K EXROM\n");
}
else
{
for (i = (nb - 1); i >= 0; i--)
{
memcpy(&rom_table[i][1024], data, 2048);
data += 2048;
/* simulate missing A10 */
memcpy(&rom_table[i][3072], &rom_table[i][2048], 1024);
}
log_cb(RETRO_LOG_INFO, "[O2EM]: %uK\n", (unsigned)(nb * 2));
}
}
rom = rom_table[0];
if (nb == 1)
app_data.bank = 1;
else if (nb == 2)
app_data.bank = app_data.exrom ? 1 : 2;
else if (nb == 4)
app_data.bank = 3;
else
app_data.bank = 4;
if ((rom_table[nb-1][1024+12]=='O') &&
(rom_table[nb-1][1024+13]=='P') &&
(rom_table[nb-1][1024+14]=='N') &&
(rom_table[nb-1][1024+15]=='B'))
app_data.openb=1;
return true;
}
void update_joy(void)
{
}
static void pointerToScreenCoordinates(int *x, int *y)
{
if (crop_overscan)
{
*x = ((*x + 0x7FFF) * CROPPED_WIDTH / 0xFFFF) + CROPPED_OFFSET_X;
*y = ((*y + 0x7FFF) * CROPPED_HEIGHT / 0xFFFF) + CROPPED_OFFSET_Y;
}
else
{
*x = (*x + 0x7FFF) * EMUWIDTH / 0xFFFF;
*y = (*y + 0x7FFF) * EMUHEIGHT / 0xFFFF;
}
}
static void update_input_virtual_keyboard(unsigned joypad_bits)
{
bool select = (joypad_bits & (1 << RETRO_DEVICE_ID_JOYPAD_SELECT)) >> RETRO_DEVICE_ID_JOYPAD_SELECT;
bool start = (joypad_bits & (1 << RETRO_DEVICE_ID_JOYPAD_START)) >> RETRO_DEVICE_ID_JOYPAD_START;
bool y = (joypad_bits & (1 << RETRO_DEVICE_ID_JOYPAD_Y)) >> RETRO_DEVICE_ID_JOYPAD_Y;
bool up = (joypad_bits & (1 << RETRO_DEVICE_ID_JOYPAD_UP)) >> RETRO_DEVICE_ID_JOYPAD_UP;
bool down = (joypad_bits & (1 << RETRO_DEVICE_ID_JOYPAD_DOWN)) >> RETRO_DEVICE_ID_JOYPAD_DOWN;
bool left = (joypad_bits & (1 << RETRO_DEVICE_ID_JOYPAD_LEFT)) >> RETRO_DEVICE_ID_JOYPAD_LEFT;
bool right = (joypad_bits & (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT)) >> RETRO_DEVICE_ID_JOYPAD_RIGHT;
bool b = (joypad_bits & (1 << RETRO_DEVICE_ID_JOYPAD_B)) >> RETRO_DEVICE_ID_JOYPAD_B;
bool click = input_state_cb(2, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_PRESSED);
/* Show the virtual keyboard? */
if (select && !last_btn_state.select)
{
vkb_show = !vkb_show;
/* Release current key when virtual keyboard hidden */
if (!vkb_show)
key[vkb_get_current_key_scancode()] = false;
}
if (vkb_show)
{
/* Move keyboard */
if (y && !last_btn_state.y)
vkb_set_virtual_keyboard_position((vkb_get_virtual_keyboard_position() + 1) % 2);
/* Direct click on the keyboard (touch screen) */
if (click)
{
int xpointer, ypointer;
xpointer = input_state_cb(2, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_X);
ypointer = input_state_cb(2, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_Y);
pointerToScreenCoordinates(&xpointer, &ypointer);
/* A key was touched: act as if the "Action" button was pushed */
if (vkb_move_at(xpointer, ypointer))
b = true;
}
/* Press key */
if ((b && !last_btn_state.b) || (!b && last_btn_state.b))
key[vkb_get_current_key_scancode()] = b;
if (!b)
{
/* Move current key */
if (right && !last_btn_state.right)
vkb_move_key(VKB_MOVE_RIGHT);
else if (left && !last_btn_state.left)
vkb_move_key(VKB_MOVE_LEFT);
else if (down && !last_btn_state.down)
vkb_move_key(VKB_MOVE_DOWN);
else if (up && !last_btn_state.up)
vkb_move_key(VKB_MOVE_UP);
/* If start is pressed than press the Enter key */
if ((start && !last_btn_state.start) || (!start && last_btn_state.start))
key[RETROK_RETURN] = start;
}
}
last_btn_state.select = select;
last_btn_state.start = start;
last_btn_state.y = y;
last_btn_state.b = b;
last_btn_state.left = left;
last_btn_state.right = right;
last_btn_state.up = up;
last_btn_state.down = down;
}
static void update_input(void)
{
unsigned joypad_bits[2] = {0};
size_t i, j;
if (!input_poll_cb)
return;
input_poll_cb();
if (libretro_supports_bitmasks)
{
for (i = 0; i < 2; i++)
{
joypad_bits[i] = input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_MASK);
/* If virtual KBD is shown, only need input
* from port 1 */
if (vkb_show)
goto done;
}
}
else
{
for (i = 0; i < 2; i++)
{
for (j = 0; j < (RETRO_DEVICE_ID_JOYPAD_R3+1); j++)
joypad_bits[i] |= input_state_cb(i, RETRO_DEVICE_JOYPAD, 0, j) ? (1 << j) : 0;
/* If virtual KBD is shown, only need input
* from port 1 */
if (vkb_show)
goto done;
}
}
if (!vkb_show)
{
/* Joystick
* Player 1 */
joystick_data[p1_index][0]= (joypad_bits[0] & (1 << RETRO_DEVICE_ID_JOYPAD_UP)) >> RETRO_DEVICE_ID_JOYPAD_UP;
joystick_data[p1_index][1]= (joypad_bits[0] & (1 << RETRO_DEVICE_ID_JOYPAD_DOWN)) >> RETRO_DEVICE_ID_JOYPAD_DOWN;
joystick_data[p1_index][2]= (joypad_bits[0] & (1 << RETRO_DEVICE_ID_JOYPAD_LEFT)) >> RETRO_DEVICE_ID_JOYPAD_LEFT;
joystick_data[p1_index][3]= (joypad_bits[0] & (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT)) >> RETRO_DEVICE_ID_JOYPAD_RIGHT;
joystick_data[p1_index][4]= (joypad_bits[0] & (1 << RETRO_DEVICE_ID_JOYPAD_B)) >> RETRO_DEVICE_ID_JOYPAD_B; /* "Action" button on the joystick */
/* Player 2 */
joystick_data[p2_index][0]= (joypad_bits[1] & (1 << RETRO_DEVICE_ID_JOYPAD_UP)) >> RETRO_DEVICE_ID_JOYPAD_UP;
joystick_data[p2_index][1]= (joypad_bits[1] & (1 << RETRO_DEVICE_ID_JOYPAD_DOWN)) >> RETRO_DEVICE_ID_JOYPAD_DOWN;
joystick_data[p2_index][2]= (joypad_bits[1] & (1 << RETRO_DEVICE_ID_JOYPAD_LEFT)) >> RETRO_DEVICE_ID_JOYPAD_LEFT;
joystick_data[p2_index][3]= (joypad_bits[1] & (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT)) >> RETRO_DEVICE_ID_JOYPAD_RIGHT;
joystick_data[p2_index][4]= (joypad_bits[1] & (1 << RETRO_DEVICE_ID_JOYPAD_B)) >> RETRO_DEVICE_ID_JOYPAD_B; /* "Action" button on the joystick */
/* Numeric and Alpha */
key[RETROK_0] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_0) |
((joypad_bits[0] & (1 << RETRO_DEVICE_ID_JOYPAD_X)) >> RETRO_DEVICE_ID_JOYPAD_X);
key[RETROK_1] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_1) |
((joypad_bits[0] & (1 << RETRO_DEVICE_ID_JOYPAD_L)) >> RETRO_DEVICE_ID_JOYPAD_L);
key[RETROK_2] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_2) |
((joypad_bits[0] & (1 << RETRO_DEVICE_ID_JOYPAD_R)) >> RETRO_DEVICE_ID_JOYPAD_R);
key[RETROK_3] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_3) |
((joypad_bits[0] & (1 << RETRO_DEVICE_ID_JOYPAD_L2)) >> RETRO_DEVICE_ID_JOYPAD_L2);
key[RETROK_4] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_4) |
((joypad_bits[0] & (1 << RETRO_DEVICE_ID_JOYPAD_R2)) >> RETRO_DEVICE_ID_JOYPAD_R2);
key[RETROK_5] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_5) |
((joypad_bits[0] & (1 << RETRO_DEVICE_ID_JOYPAD_L3)) >> RETRO_DEVICE_ID_JOYPAD_L3);
key[RETROK_6] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_6) |
((joypad_bits[0] & (1 << RETRO_DEVICE_ID_JOYPAD_R3)) >> RETRO_DEVICE_ID_JOYPAD_R3);
key[RETROK_7] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_7);
key[RETROK_8] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_8);
key[RETROK_9] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_9);
key[RETROK_a] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_a);
key[RETROK_b] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_b);
key[RETROK_c] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_c);
key[RETROK_d] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_d);
key[RETROK_e] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_e);
key[RETROK_f] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_f);
key[RETROK_g] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_g);
key[RETROK_h] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_h);
key[RETROK_i] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_i);
key[RETROK_j] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_j);
key[RETROK_k] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_k);
key[RETROK_l] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_l);
key[RETROK_m] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_m);
key[RETROK_n] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_n);
key[RETROK_o] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_o);
key[RETROK_p] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_p);
key[RETROK_q] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_q);
key[RETROK_r] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_r);
key[RETROK_s] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_s);
key[RETROK_t] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_t);
key[RETROK_u] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_u);
key[RETROK_v] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_v);
key[RETROK_w] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_w);
key[RETROK_x] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_x);
key[RETROK_y] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_y);
key[RETROK_z] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_z);
key[RETROK_SPACE] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_SPACE); /* Space */
key[RETROK_QUESTION] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_QUESTION); /* ? */
key[RETROK_PERIOD] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_PERIOD); /* . */
key[RETROK_DELETE] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_END); /* "Clear" */
key[RETROK_RETURN] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_RETURN); /* "Enter" */
key[RETROK_MINUS] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_MINUS); /* - */
key[RETROK_ASTERISK] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_ASTERISK); /* Multiply sign */
key[RETROK_SLASH] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_SLASH); /* Divide sign */
key[RETROK_EQUALS] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_EQUALS); /* = */
key[RETROK_PLUS] = input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_PLUS); /* + */
}
done:
/* Virtual keyboard management */
update_input_virtual_keyboard(joypad_bits[0]);
/* Take into account RESET being pressed (F5 is O2EM original setting )*/
if (key[RETROK_F5] || input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, RETROK_F5))
{
retro_reset();
key[RETROK_F5] = 0;
}
}
static void upate_audio(void)
{
uint8_t *audio_samples_ptr = soundBuffer;
int16_t *audio_out_ptr = audioOutBuffer;
size_t i;
/* Convert 8u mono to 16s stereo */
if (low_pass_enabled)
{
/* Restore previous sample */
int32_t low_pass = low_pass_prev;
/* Single-pole low-pass filter (6 dB/octave) */
int32_t factor_a = low_pass_range;
int32_t factor_b = 0x10000 - factor_a;
for(i = 1; i <= SOUND_BUFFER_LEN; i++)
{
int16_t sample16;
/* For improved resampler performance, we
* reduce the internal sample rate to 2/3
* of its original value. Odyssey2/Videopac
* audio is so primitive that we can simply
* skip every third sample with no perceivable
* reduction in sound quality. */
if ((i % 3) == 0)
{
audio_samples_ptr++;
continue;
}
/* Get current sample */
sample16 = (((*(audio_samples_ptr++) * audio_volume) /
100) - 128) << 8;
/* Apply low-pass filter */
low_pass = (low_pass * factor_a) + (sample16 * factor_b);
/* 16.16 fixed point */
low_pass >>= 16;
/* Update output buffer */
*(audio_out_ptr++) = (int16_t)low_pass;
*(audio_out_ptr++) = (int16_t)low_pass;
}
/* Save last sample for next frame */
low_pass_prev = low_pass;
}
else
{
for(i = 1; i <= SOUND_BUFFER_LEN; i++)
{
int16_t sample16;
if ((i % 3) == 0)
{
audio_samples_ptr++;
continue;
}
/* Get current sample */
sample16 = (((*(audio_samples_ptr++) * audio_volume) /
100) - 128) << 8;
*(audio_out_ptr++) = (int16_t)sample16;
*(audio_out_ptr++) = (int16_t)sample16;
}
}
#ifdef HAVE_VOICE
if (get_voice_status())
{
float fbuf[AUDIO_SAMPLES_PER_FRAME * 2];
/* Convert output audio buffer to float */
convert_s16_to_float(fbuf, audioOutBuffer, AUDIO_SAMPLES_PER_FRAME * 2, 1.0f);
/* Mix in voice */
core_audio_mixer_mix(fbuf, AUDIO_SAMPLES_PER_FRAME, voice_volume, true);
/* Convert back to signed integer */
convert_float_to_s16(audioOutBuffer, fbuf, AUDIO_SAMPLES_PER_FRAME * 2);
}
#endif
audio_batch_cb(audioOutBuffer, AUDIO_SAMPLES_PER_FRAME);
}
/************************************
* Interframe blending
************************************/
enum frame_blend_method
{
FRAME_BLEND_NONE = 0,
FRAME_BLEND_MIX,
FRAME_BLEND_GHOST_65,
FRAME_BLEND_GHOST_75,
FRAME_BLEND_GHOST_85,
FRAME_BLEND_GHOST_95
};
/* It would be more flexible to have 'persistence'
* as a core option, but using a variable parameter
* reduces performance by ~15%. We therefore offer
* fixed values, and use a macro to avoid excessive
* duplication of code... (and yet we still have to
* duplicate code due to the annoying SUPPORT_ABGR1555
* ifdefs...)
* Note: persistence fraction is (persistence/128),
* using a power of 2 like this further increases
* performance by ~15% */
#if defined(SUPPORT_ABGR1555)
#define BLEND_FRAMES_GHOST(persistence) \
{ \
uint16_t *curr = mbmp; \
uint16_t *prev = mbmp_prev; \
size_t x, y; \
\
for (y = 0; y < EMUHEIGHT; y++) \
{ \
for (x = 0; x < EMUWIDTH; x++) \
{ \
/* Get colours from current + previous frames */ \
uint16_t color_curr = *(curr); \
uint16_t color_prev = *(prev); \
\
/* Unpack colours */ \
uint16_t r_curr = (color_curr ) & 0x1F; \
uint16_t g_curr = (color_curr >> 5) & 0x1F; \
uint16_t b_curr = (color_curr >> 10) & 0x1F; \
\
uint16_t r_prev = (color_curr ) & 0x1F; \
uint16_t g_prev = (color_curr >> 5) & 0x1F; \
uint16_t b_prev = (color_curr >> 10) & 0x1F; \
\
/* Mix colors */ \
uint16_t r_mix = ((r_curr * (128 - persistence)) >> 7) + ((r_prev * persistence) >> 7); \
uint16_t g_mix = ((g_curr * (128 - persistence)) >> 7) + ((g_prev * persistence) >> 7); \
uint16_t b_mix = ((b_curr * (128 - persistence)) >> 7) + ((b_prev * persistence) >> 7); \
\
/* Output colour is the maximum of the input \
* and decayed values */ \
uint16_t r_out = (r_mix > r_curr) ? r_mix : r_curr; \
uint16_t g_out = (g_mix > g_curr) ? g_mix : g_curr; \
uint16_t b_out = (b_mix > b_curr) ? b_mix : b_curr; \
uint16_t color_out = b_out << 10 | g_out << 5 | r_out; \
\
/* Assign colour and store for next frame */ \
*(prev++) = color_out; \
*(curr++) = color_out; \
} \
\
curr += (TEX_WIDTH - EMUWIDTH); \
prev += (TEX_WIDTH - EMUWIDTH); \
} \
}
#else
#define BLEND_FRAMES_GHOST(persistence) \
{ \
uint16_t *curr = mbmp; \
uint16_t *prev = mbmp_prev; \
size_t x, y; \
\
for (y = 0; y < EMUHEIGHT; y++) \
{ \
for (x = 0; x < EMUWIDTH; x++) \
{ \
/* Get colours from current + previous frames */ \
uint16_t color_curr = *(curr); \
uint16_t color_prev = *(prev); \
\
/* Unpack colours */ \
uint16_t r_curr = (color_curr >> 11) & 0x1F; \
uint16_t g_curr = (color_curr >> 6) & 0x1F; \
uint16_t b_curr = (color_curr ) & 0x1F; \
\
uint16_t r_prev = (color_prev >> 11) & 0x1F; \
uint16_t g_prev = (color_prev >> 6) & 0x1F; \
uint16_t b_prev = (color_prev ) & 0x1F; \
\
/* Mix colors */ \
uint16_t r_mix = ((r_curr * (128 - persistence)) >> 7) + ((r_prev * persistence) >> 7); \
uint16_t g_mix = ((g_curr * (128 - persistence)) >> 7) + ((g_prev * persistence) >> 7); \
uint16_t b_mix = ((b_curr * (128 - persistence)) >> 7) + ((b_prev * persistence) >> 7); \
\
/* Output colour is the maximum of the input \
* and decayed values */ \
uint16_t r_out = (r_mix > r_curr) ? r_mix : r_curr; \
uint16_t g_out = (g_mix > g_curr) ? g_mix : g_curr; \
uint16_t b_out = (b_mix > b_curr) ? b_mix : b_curr; \
uint16_t color_out = r_out << 11 | g_out << 6 | b_out; \
\
/* Assign colour and store for next frame */ \
*(prev++) = color_out; \
*(curr++) = color_out; \
} \
\
curr += (TEX_WIDTH - EMUWIDTH); \
prev += (TEX_WIDTH - EMUWIDTH); \
} \
}
#endif
static void blend_frames_mix(void)
{
uint16_t *curr = mbmp;
uint16_t *prev = mbmp_prev;
size_t x, y;
for (y = 0; y < EMUHEIGHT; y++)
{
for (x = 0; x < EMUWIDTH; x++)
{
/* Get colours from current + previous frames */
uint16_t color_curr = *(curr);
uint16_t color_prev = *(prev);
/* Store colours for next frame */
*(prev++) = color_curr;
/* Mix colours */
#if defined(SUPPORT_ABGR1555)
*(curr++) = (color_curr + color_prev + ((color_curr ^ color_prev) & 0x521)) >> 1;
#else
*(curr++) = (color_curr + color_prev + ((color_curr ^ color_prev) & 0x821)) >> 1;
#endif
}
curr += (TEX_WIDTH - EMUWIDTH);
prev += (TEX_WIDTH - EMUWIDTH);
}
}
static void blend_frames_ghost65(void)
{
/* 65% = 83 / 128 */
BLEND_FRAMES_GHOST(83);
}
static void blend_frames_ghost75(void)
{
/* 75% = 95 / 128 */
BLEND_FRAMES_GHOST(95);
}
static void blend_frames_ghost85(void)
{
/* 85% ~= 109 / 128 */
BLEND_FRAMES_GHOST(109);
}
static void blend_frames_ghost95(void)
{
/* 95% ~= 122 / 128 */
BLEND_FRAMES_GHOST(122);
}
static void (*blend_frames)(void) = NULL;
static void init_frame_blending(enum frame_blend_method blend_method)
{
/* Allocate/zero out buffer, if required */
if (blend_method != FRAME_BLEND_NONE)
{
if (!mbmp_prev)
mbmp_prev = (uint16_t*)malloc(TEX_WIDTH * TEX_HEIGHT * sizeof(uint16_t));
memset(mbmp_prev, 0, TEX_WIDTH * TEX_HEIGHT * sizeof(uint16_t));
}
/* Assign function pointer */
switch (blend_method)
{
case FRAME_BLEND_MIX:
blend_frames = blend_frames_mix;
break;
case FRAME_BLEND_GHOST_65:
blend_frames = blend_frames_ghost65;
break;
case FRAME_BLEND_GHOST_75:
blend_frames = blend_frames_ghost75;
break;
case FRAME_BLEND_GHOST_85:
blend_frames = blend_frames_ghost85;
break;
case FRAME_BLEND_GHOST_95:
blend_frames = blend_frames_ghost95;
break;
default:
blend_frames = NULL;
break;
}
}
/************************************
* libretro implementation
************************************/
void retro_get_system_info(struct retro_system_info *info)
{
memset(info, 0, sizeof(*info));
info->library_name = "O2EM";
#ifndef GIT_VERSION
#define GIT_VERSION ""
#endif
info->library_version = "1.18" GIT_VERSION;
info->need_fullpath = false;
info->valid_extensions = "bin";
}
void retro_get_system_av_info(struct retro_system_av_info *info)
{
memset(info, 0, sizeof(*info));
info->timing.fps = (evblclk == EVBLCLK_NTSC) ? 60 : 50;
info->timing.sample_rate = AUDIO_SAMPLERATE;
if (crop_overscan)
{
info->geometry.base_width = CROPPED_WIDTH;
info->geometry.base_height = CROPPED_HEIGHT;
}
else
{
info->geometry.base_width = EMUWIDTH;
info->geometry.base_height = EMUHEIGHT;
}
info->geometry.max_width = EMUWIDTH;
info->geometry.max_height = EMUHEIGHT;
info->geometry.aspect_ratio = 4.0f / 3.0f;
}
void retro_set_controller_port_device(unsigned port, unsigned device)
{
(void)port;
(void)device;
}
size_t retro_serialize_size(void)
{
return savestate_size();
}
bool retro_serialize(void *data, size_t size)
{
return savestate_to_mem((uint8_t *)data, size);
}
bool retro_unserialize(const void *data, size_t size)
{
return loadstate_from_mem((uint8_t *)data, size);
}
void retro_cheat_reset(void)
{}
void retro_cheat_set(unsigned index, bool enabled, const char *code)
{
(void)index;
(void)enabled;
(void)code;
}
bool retro_load_game(const struct retro_game_info *info)
{
char bios_file_path[PATH_MAX_LENGTH];
const uint8_t *rom_data = NULL;
size_t rom_size = 0;
const char *system_directory_c = NULL;
enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
struct retro_input_descriptor desc[] = {
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "Left" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "Up" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "Down" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Right" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Action" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Move Virtual Keyboard" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT,"Show/Hide Virtual Keyboard" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Numeric Key 0" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "Numeric Key 1" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "Numeric Key 2" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "Numeric Key 3" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "Numeric Key 4" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "Numeric Key 5" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "Numeric Key 6" },
{ 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "Left" },
{ 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "Up" },
{ 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "Down" },
{ 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Right" },
{ 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Action" },
{ 2, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_X, "Virtual Keyboard: Pointer X" },
{ 2, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_Y, "Virtual Keyboard: Pointer Y" },
{ 2, RETRO_DEVICE_POINTER, 0, RETRO_DEVICE_ID_POINTER_PRESSED, "Virtual Keyboard: Pointer Pressed" },
{ 0 }
};
if (!info || !info->data || (info->size < 1))
return false;
rom_data = (const uint8_t *)info->data;
rom_size = info->size;
if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt))
{
log_cb(RETRO_LOG_INFO, "[O2EM]: RGB565 is not supported.\n");
return false;
}
environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);
/* BIOS is required */
environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &system_directory_c);
if (!system_directory_c)
{
log_cb(RETRO_LOG_WARN, "[O2EM]: no system directory defined, unable to look for %s\n", bios_file_name);
return false;
}
fill_pathname_join(bios_file_path, system_directory_c, bios_file_name, sizeof(bios_file_path));
if (!path_is_valid(bios_file_path))
{
log_cb(RETRO_LOG_WARN, "[O2EM]: %s not found, cannot load BIOS\n", bios_file_name);
return false;
}
app_data.stick[0] = app_data.stick[1] = 1;
app_data.sticknumber[0] = app_data.sticknumber[1] = 0;
set_defjoykeys(0,0);
set_defjoykeys(1,1);
set_defsystemkeys();
app_data.bank = 0;
app_data.limit = 1;
app_data.sound_en = 1;
app_data.speed = 100;
app_data.wsize = 2;
app_data.scanlines = 0;
app_data.voice = 1;
/* These volume settings have no effect
* (they are allegro-specific) */
app_data.svolume = 100;
app_data.vvolume = 100;
/* Internal audio filter is worthless,
* disable it and use our own */
app_data.filter = 0;
app_data.exrom = 0;