forked from hissorii/px68k
-
Notifications
You must be signed in to change notification settings - Fork 2
/
libretro.c
executable file
·514 lines (403 loc) · 11.7 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
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "libretro.h"
#ifdef _WIN32
char slash = '\\';
#else
char slash = '/';
#endif
#define SOUNDRATE 44100.0
#define SNDSZ 795
char RPATH[512];
char RETRO_DIR[512];
const char *retro_save_directory;
const char *retro_system_directory;
const char *retro_content_directory;
char retro_system_conf[512];
char Core_Key_Sate[512];
char Core_old_Key_Sate[512];
bool opt_analog;
int retrow=800;
int retroh=600;
int CHANGEAV=0;
int pauseg=0;
signed short soundbuf[1024*2];
uint16_t *videoBuffer;
static retro_video_refresh_t video_cb;
static retro_environment_t environ_cb;
static retro_input_poll_t input_poll_cb;
retro_input_state_t input_state_cb;
retro_audio_sample_t audio_cb;
retro_audio_sample_batch_t audio_batch_cb;
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; }
static char CMDFILE[512];
int loadcmdfile(char *argv)
{
int res=0;
FILE *fp = fopen(argv,"r");
if( fp != NULL )
{
if ( fgets (CMDFILE , 512 , fp) != NULL )
res=1;
fclose (fp);
}
return res;
}
int HandleExtension(char *path,char *ext)
{
int len = strlen(path);
if (len >= 4 &&
path[len-4] == '.' &&
path[len-3] == ext[0] &&
path[len-2] == ext[1] &&
path[len-1] == ext[2])
{
return 1;
}
return 0;
}
//Args for experimental_cmdline
static char ARGUV[64][1024];
static unsigned char ARGUC=0;
// Args for Core
static char XARGV[64][1024];
static const char* xargv_cmd[64];
int PARAMCOUNT=0;
extern int cmain(int argc, char *argv[]);
void parse_cmdline( const char *argv );
void Add_Option(const char* option)
{
static int first=0;
if(first==0)
{
PARAMCOUNT=0;
first++;
}
sprintf(XARGV[PARAMCOUNT++],"%s\0",option);
}
int pre_main(const char *argv)
{
int i=0;
int Only1Arg;
if (strlen(argv) > strlen("cmd"))
{
if( HandleExtension((char*)argv,"cmd") || HandleExtension((char*)argv,"CMD"))
i=loadcmdfile((char*)argv);
}
if(i==1)
parse_cmdline(CMDFILE);
else
parse_cmdline(argv);
Only1Arg = (strcmp(ARGUV[0],"px68k") == 0) ? 0 : 1;
for (i = 0; i<64; i++)
xargv_cmd[i] = NULL;
if(Only1Arg)
{
int cfgload=0;
Add_Option("px68k");
if (strlen(RPATH) >= strlen("hdf")){
if(!strcasecmp(&RPATH[strlen(RPATH)-strlen("hdf")], "hdf")){
Add_Option("-h");
cfgload=1;
}
}
if(cfgload==0)
{
//Add_Option("-verbose");
//Add_Option(retro_system_tos);
//Add_Option("-8");
}
Add_Option(RPATH);
}
else
{ // Pass all cmdline args
for(i = 0; i < ARGUC; i++)
Add_Option(ARGUV[i]);
}
for (i = 0; i < PARAMCOUNT; i++)
{
xargv_cmd[i] = (char*)(XARGV[i]);
}
pmain(PARAMCOUNT,( char **)xargv_cmd);
xargv_cmd[PARAMCOUNT - 2] = NULL;
return 0;
}
void parse_cmdline(const char *argv)
{
char *p,*p2,*start_of_word;
int c,c2;
static char buffer[512*4];
enum states { DULL, IN_WORD, IN_STRING } state = DULL;
strcpy(buffer,argv);
strcat(buffer," \0");
for (p = buffer; *p != '\0'; p++)
{
c = (unsigned char) *p; /* convert to unsigned char for is* functions */
switch (state)
{
case DULL: /* not in a word, not in a double quoted string */
if (isspace(c)) /* still not in a word, so ignore this char */
continue;
/* not a space -- if it's a double quote we go to IN_STRING, else to IN_WORD */
if (c == '"')
{
state = IN_STRING;
start_of_word = p + 1; /* word starts at *next* char, not this one */
continue;
}
state = IN_WORD;
start_of_word = p; /* word starts here */
continue;
case IN_STRING:
/* we're in a double quoted string, so keep going until we hit a close " */
if (c == '"')
{
/* word goes from start_of_word to p-1 */
//... do something with the word ...
for (c2 = 0,p2 = start_of_word; p2 < p; p2++, c2++)
ARGUV[ARGUC][c2] = (unsigned char) *p2;
ARGUC++;
state = DULL; /* back to "not in word, not in string" state */
}
continue; /* either still IN_STRING or we handled the end above */
case IN_WORD:
/* we're in a word, so keep going until we get to a space */
if (isspace(c))
{
/* word goes from start_of_word to p-1 */
//... do something with the word ...
for (c2 = 0,p2 = start_of_word; p2 <p; p2++,c2++)
ARGUV[ARGUC][c2] = (unsigned char) *p2;
ARGUC++;
state = DULL; /* back to "not in word, not in string" state */
}
continue; /* either still IN_WORD or we handled the end above */
}
}
}
void texture_init(void)
{
memset(videoBuffer, 0, sizeof(*videoBuffer));
}
void retro_set_environment(retro_environment_t cb)
{
environ_cb = cb;
struct retro_variable variables[] = {
{
"px68k_analog","Use Analog; OFF|ON",
},
{ NULL, NULL },
};
cb(RETRO_ENVIRONMENT_SET_VARIABLES, variables);
}
static void update_variables(void)
{
struct retro_variable var = {0};
var.key = "px68k_analog";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
fprintf(stderr, "value: %s\n", var.value);
if (!strcmp(var.value, "OFF"))
opt_analog = false;
if (!strcmp(var.value, "ON"))
opt_analog = true;
fprintf(stderr, "[libretro-test]: Analog: %s.\n",opt_analog?"ON":"OFF");
}
}
void update_input(void)
{
input_poll_cb();
}
#if 0
static void keyboard_cb(bool down, unsigned keycode, uint32_t character, uint16_t mod)
{
}
#endif
/************************************
* libretro implementation
************************************/
//static struct retro_system_av_info g_av_info;
void retro_get_system_info(struct retro_system_info *info)
{
memset(info, 0, sizeof(*info));
info->library_name = "px68k";
info->library_version = "0.15+";
info->need_fullpath = true;
info->valid_extensions = "dim|zip|img|d88|88d|hdm|dup|2hd|xdf|hdf|cmd";
}
void retro_get_system_av_info(struct retro_system_av_info *info)
{
/* FIXME handle PAL/NTSC */
struct retro_game_geometry geom = { retrow, retroh,800, 600 ,4.0 / 3.0 };
struct retro_system_timing timing = { 55.45, SOUNDRATE };
info->geometry = geom;
info->timing = timing;
}
void update_geometry(void)
{
struct retro_system_av_info system_av_info;
system_av_info.geometry.base_width = retrow;
system_av_info.geometry.base_height = retroh;
system_av_info.geometry.aspect_ratio = (float)4/3;// retro_aspect;
environ_cb(RETRO_ENVIRONMENT_SET_GEOMETRY, &system_av_info);
}
void retro_set_controller_port_device(unsigned port, unsigned device)
{
(void)port;
(void)device;
}
size_t retro_serialize_size(void)
{
return 0;
}
bool retro_serialize(void *data, size_t size)
{
return false;
}
bool retro_unserialize(const void *data, size_t size)
{
return false;
}
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)
{
const char *full_path;
full_path = info->path;
strcpy(RPATH,full_path);
printf("LOAD EMU\n");
return true;
}
bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
{
(void)game_type;
(void)info;
(void)num_info;
return false;
}
void retro_unload_game(void)
{
pauseg=0;
}
unsigned retro_get_region(void)
{
return RETRO_REGION_NTSC;
}
unsigned retro_api_version(void)
{
return RETRO_API_VERSION;
}
void *retro_get_memory_data(unsigned id)
{
return NULL;
}
size_t retro_get_memory_size(unsigned id)
{
return 0;
}
void retro_init(void)
{
const char *system_dir = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &system_dir) && system_dir)
{
// if defined, use the system directory
retro_system_directory=system_dir;
}
const char *content_dir = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_CONTENT_DIRECTORY, &content_dir) && content_dir)
{
// if defined, use the system directory
retro_content_directory=content_dir;
}
const char *save_dir = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &save_dir) && save_dir)
{
// If save directory is defined use it, otherwise use system directory
retro_save_directory = *save_dir ? save_dir : retro_system_directory;
}
else
{
// make retro_save_directory the same in case RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY is not implemented by the frontend
retro_save_directory=retro_system_directory;
}
if(retro_system_directory==NULL)sprintf(RETRO_DIR, "%s\0",".");
else sprintf(RETRO_DIR, "%s\0", retro_system_directory);
sprintf(retro_system_conf, "%s%ckeropi\0",RETRO_DIR,slash);
enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt))
{
fprintf(stderr, "RGB565 is not supported.\n");
exit(0);
}
struct retro_input_descriptor inputDescriptors[] = {
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "X" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Y" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Right" },
{ 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_R, "R" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "R2" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "L2" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R3, "R3" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L3, "L3" }
};
environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, &inputDescriptors);
/*
struct retro_keyboard_callback cbk = { keyboard_cb };
environ_cb(RETRO_ENVIRONMENT_SET_KEYBOARD_CALLBACK, &cbk);
*/
update_variables();
memset(Core_Key_Sate,0,512);
memset(Core_old_Key_Sate ,0, sizeof(Core_old_Key_Sate));
}
void retro_deinit(void)
{
end_loop_retro();
printf("Retro DeInit\n");
}
void retro_reset(void)
{
}
static int firstcall=1;
void retro_run(void)
{
if(firstcall)
{
pre_main(RPATH);
firstcall=0;
printf("INIT done\n");
return;
}
if (CHANGEAV == 1)
{
update_geometry();
printf("w:%d h:%d a:%f\n",retrow,retroh,(float)(4/3));
CHANGEAV=0;
}
update_input();
if(pauseg!=-1)
{
}
exec_app_retro();
raudio_callback(NULL, NULL,SNDSZ*4);
video_cb(videoBuffer, retrow, retroh, /*retrow*/ 800 << 1/*2*/);
}