-
Notifications
You must be signed in to change notification settings - Fork 3
/
libretro.cpp
497 lines (414 loc) · 13.4 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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <memalign.h>
#include "libretro.h"
#include "alport.h"
#include "zc/zc_exports.h"
#define SCR_WIDTH 256
#define SCR_HEIGHT 224
//#define WANT_BPP32
#ifdef WANT_BPP32
typedef uint32_t bpp_t;
static unsigned STRIDE_SHIFT = 2;
static enum retro_pixel_format RETRO_PIX_FORMAT = RETRO_PIXEL_FORMAT_XRGB8888;
#define RETRO_PIX_NAME "XRGB8888"
#else
typedef uint16_t bpp_t;
static unsigned STRIDE_SHIFT = 1;
static enum retro_pixel_format RETRO_PIX_FORMAT = RETRO_PIXEL_FORMAT_RGB565;
#define RETRO_PIX_NAME "RGB565"
#endif
static retro_video_refresh_t video_cb;
static retro_audio_sample_t audio_cb;
static retro_audio_sample_batch_t audio_batch_cb;
static retro_environment_t environ_cb;
static retro_input_poll_t input_poll_cb;
static retro_input_state_t input_state_cb;
static retro_log_printf_t log_cb;
float sampling_rate;
char default_path[] = ".";
char *save_path;
char *system_path;
char sfx_file[16];
char sf2_file[16];
/* threads stuff */
static sthread_t *zc_thread;
slock_t *mutex;
scond_t *cond;
static bpp_t lr_palette[PAL_SIZE];
static bpp_t *framebuf;
static short *soundbuf;
void zc_log(bool err, const char *format, ...)
{
char buf[MAX_STRLEN];
va_list ap;
va_start(ap, format);
vsprintf(buf, format, ap);
va_end(ap);
log_cb(err ? RETRO_LOG_ERROR : RETRO_LOG_INFO, "%s\n", buf);
}
static void fallback_log(enum retro_log_level level, const char *fmt, ...)
{
(void)level;
va_list va;
va_start(va, fmt);
vfprintf(stderr, fmt, va);
va_end(va);
}
void retro_init(void)
{
mutex = slock_new();
cond = scond_new();
/* Create video buffer for libretro */
framebuf = (bpp_t *)calloc(SCR_WIDTH * SCR_HEIGHT, sizeof(bpp_t));
if (!environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &save_path)
|| !save_path)
{
log_cb(RETRO_LOG_INFO, "Defaulting save directory to %s\n", default_path);
save_path = default_path;
}
if (!environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &system_path)
|| !system_path)
{
log_cb(RETRO_LOG_INFO, "Defaulting system directory to %s\n", default_path);
system_path = default_path;
}
}
void retro_deinit(void)
{
free(framebuf);
scond_free(cond);
slock_free(mutex);
}
unsigned retro_api_version(void)
{
return RETRO_API_VERSION;
}
void retro_set_controller_port_device(unsigned port, unsigned device)
{
log_cb(RETRO_LOG_INFO, "Plugging device %u into port %u.\n", device, port);
}
void retro_get_system_info(struct retro_system_info *info)
{
memset(info, 0, sizeof(*info));
info->library_name = "Zelda Classic v2.10";
info->library_version = "Beta 5";
info->need_fullpath = true;
info->valid_extensions = "qst";
}
void retro_get_system_av_info(struct retro_system_av_info *info)
{
info->timing.fps = TIMING_FPS;
info->timing.sample_rate = sampling_rate;
info->geometry.base_width = SCR_WIDTH;
info->geometry.base_height = SCR_HEIGHT;
info->geometry.max_width = SCR_WIDTH;
info->geometry.max_height = SCR_HEIGHT;
info->geometry.aspect_ratio = 4.0f / 3.0f;
}
void retro_set_environment(retro_environment_t cb)
{
environ_cb = cb;
static const struct retro_variable vars[] =
{
{ "zc_samplerate", "Sample Rate (Requires Restart); 44100|48000|22050|32000" },
{ "zc_mix_quality", "Sound Quality (Requires Restart); Normal|High|Low" },
{ "zc_master_vol", "Master Volume; 16|0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15" },
{ "zc_sfx_vol", "SFX Volume; 16|0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15" },
{ "zc_music_vol", "Music Volume; 16|0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15" },
{ "zc_pan_style", "Sound Pan Style; 1/2|3/4|Full|Mono" },
{ "zc_heart_beep", "Enable Low Health Beep; true|false" },
{ "zc_trans_layers", "Show Transparent Layers; true|false" },
{ "zc_allow_cheats", "Allow cheats (press 'Cheat' and L, R, Map, Select, or Start); false|true" },
{ "zc_soundfont", "SF2 soundfont To Use (See GitHub readme - Requires Restart); default|custom0|custom1|custom2|custom3|custom4|custom5|custom6|custom7|custom8|custom9" },
{ "zc_custom_sfx", "Custom Sound FX To Use (See GitHub readme - Requires Restart); Off|LinkTothePast|LinksAwakening|BSZelda|Metroid|Tortuga|Castle|Elise|SwangSong|Custom" },
{ NULL, NULL },
};
environ_cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void *)vars);
static struct retro_log_callback logging;
if (environ_cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &logging))
log_cb = logging.log;
else
log_cb = fallback_log;
enum retro_pixel_format fmt = RETRO_PIX_FORMAT;
if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt))
{
log_cb(RETRO_LOG_INFO, "Using " RETRO_PIX_NAME " pixel format.\n");
}
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, "B" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Map" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "Cheat" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "L" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "R" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
{ 0 },
};
environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);
}
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_set_video_refresh(retro_video_refresh_t cb)
{
video_cb = cb;
}
void retro_reset(void)
{
zc_action(ZC_RESET);
}
static void update_input(void)
{
input_poll_cb();
DUkey = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP);
DDkey = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN);
DLkey = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT);
DRkey = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT);
Akey = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A);
Bkey = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B);
Mkey = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X);
Ckey = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y);
Lkey = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L);
Rkey = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R);
Ekey = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT);
Skey = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START);
}
void update_palette(RGB *p)
{
int i;
#ifdef WANT_BPP32
bpp_t *pa = (bpp_t *)p;
/* Just need to shift it in order to make it 0-255 range
* (allegro palette is 0-63 range). */
for (i = 0; i < PAL_SIZE; i++)
lr_palette[i] = pa[i] << 2;
#else
for (i = 0; i < PAL_SIZE; i++)
lr_palette[i] = ((p[i].r & 0x3E) << 10) | (p[i].g << 5) | (p[i].b >> 1);
#endif
}
static void render_video(void)
{
/* Try rendering straight into VRAM if we can. */
bpp_t *buf = NULL;
unsigned int stride = 0;
unsigned int i = 0;
struct retro_framebuffer fb = {0};
unsigned char *canvp = NULL;
fb.width = SCR_WIDTH;
fb.height = SCR_HEIGHT;
fb.access_flags = RETRO_MEMORY_ACCESS_WRITE;
if (environ_cb(RETRO_ENVIRONMENT_GET_CURRENT_SOFTWARE_FRAMEBUFFER, &fb)
&& fb.format == RETRO_PIX_FORMAT)
{
buf = (bpp_t *)fb.data;
stride = fb.pitch >> STRIDE_SHIFT;
}
else
{
buf = framebuf;
stride = SCR_WIDTH;
}
/* update the Libretro palette if required */
if (zc_sync_pal)
{
zc_sync_pal = false;
update_palette(zc_palette);
}
/* Blit the ZC canvas into the libretro framebuffer */
canvp = (unsigned char *) zc_canvas->dat;
for (i = 0; i < SCR_WIDTH * SCR_HEIGHT; i++)
buf[i] = lr_palette[canvp[i]];
video_cb(buf, SCR_WIDTH, SCR_HEIGHT, stride << STRIDE_SHIFT);
}
static void check_variables(bool startup = false)
{
struct retro_variable var = {0};
int old_musicv = music_vol;
int old_masterv = master_vol;
int old_sfxv = sfx_vol;
var.key = "zc_master_vol";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
master_vol = atoi(var.value);
if (master_vol > 0)
master_vol = (master_vol << 4) - 1;
}
var.key = "zc_sfx_vol";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
sfx_vol = atoi(var.value);
if (sfx_vol > 0)
sfx_vol = (sfx_vol << 4) - 1;
}
var.key = "zc_music_vol";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
music_vol = atoi(var.value);
if (music_vol > 0)
music_vol = (music_vol << 4) - 1;
}
var.key = "zc_pan_style";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
if (!strcmp(var.value, "Mono"))
pan_style = 0;
else if (!strcmp(var.value, "3/4"))
pan_style = 2;
else if (!strcmp(var.value, "Full"))
pan_style = 3;
else
pan_style = 1; /* 1/2 */
}
var.key = "zc_heart_beep";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
heart_beep = !strcmp(var.value, "true") ? true : false;
var.key = "zc_trans_layers";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
trans_layers = !strcmp(var.value, "true") ? true : false;
var.key = "zc_allow_cheats";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
allow_cheats = !strcmp(var.value, "true") ? true : false;
if (startup)
{
/* For these we require restart */
var.key = "zc_samplerate";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
sampling_rate = strtof(var.value, NULL);
var.key = "zc_mix_quality";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
if (!strcmp(var.value, "Low"))
mix_quality = 0;
else if (!strcmp(var.value, "High"))
mix_quality = 2;
else
mix_quality = 1; /* Normal */
}
var.key = "zc_soundfont";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
strcpy(sf2_file, var.value);
var.key = "zc_custom_sfx";
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
strcpy(sfx_file, var.value);
}
else
{
/* Want to exercise these on run-time after setup only */
/* Apply master volume */
if (master_vol != old_masterv)
mixer_set_volume(master_vol);
/* Apply music volume if updated. */
if (music_vol != old_musicv)
update_music_volume();
/* Apply sound volume if updated. */
if (sfx_vol != old_sfxv)
update_sfx_volume();
}
}
static void render_audio(void)
{
mixer_mix(soundbuf);
audio_batch_cb(soundbuf, sampling_rate / TIMING_FPS);
}
void retro_run(void)
{
bool updated = false;
slock_lock(mutex);
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
check_variables();
update_input();
/* wake up gameloop thread and
* wait for it to run one
* frame of audio and video */
scond_signal(cond);
scond_wait(cond, mutex);
render_audio();
render_video();
slock_unlock(mutex);
}
bool retro_load_game(const struct retro_game_info *info)
{
check_variables(true);
/* Allocate sound buffer, done after check_variables to respect settings */
soundbuf = (short *)memalign_alloc_aligned(sampling_rate / TIMING_FPS * 2 *
sizeof(short));
/* init zelda classic engine and load the qst file */
if (!zc_init(info->path))
return false;
/* Create a thread to generate 1-frame of game video & audio */
zc_state = ZC_RUN;
zc_thread = sthread_create(zc_gameloop, NULL);
return true;
}
void retro_unload_game(void)
{
/* stop zc game loop thread from running */
zc_state = ZC_EXIT;
scond_signal(cond);
sthread_join(zc_thread);
zc_deinit();
memalign_free(soundbuf);
}
unsigned retro_get_region(void)
{
return RETRO_REGION_NTSC;
}
bool retro_load_game_special(unsigned type, const struct retro_game_info *info,
size_t num)
{
return false;
}
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_get_memory_data(unsigned id)
{
(void)id;
return NULL;
}
size_t retro_get_memory_size(unsigned id)
{
(void)id;
return 0;
}
void retro_cheat_reset(void)
{}
void retro_cheat_set(unsigned index, bool enabled, const char *code)
{
(void)index;
(void)enabled;
(void)code;
}