2 * libretro core glue for PicoDrive
5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
9 #define _GNU_SOURCE 1 // mremap
16 #include <libkern/OSCacheControl.h>
19 #include <pico/pico_int.h>
20 #include "common/input_pico.h"
21 #include "common/version.h"
25 #define MAP_ANONYMOUS MAP_ANON
28 static retro_video_refresh_t video_cb;
29 static retro_input_poll_t input_poll_cb;
30 static retro_input_state_t input_state_cb;
31 static retro_environment_t environ_cb;
32 static retro_audio_sample_batch_t audio_batch_cb;
36 #define VOUT_MAX_WIDTH 320
37 #define VOUT_MAX_HEIGHT 240
38 static void *vout_buf;
39 static int vout_width, vout_height;
41 static short __attribute__((aligned(4))) sndBuffer[2*44100/50];
43 // FIXME: these 2 shouldn't be here
44 static unsigned char PicoDraw2FB_[(8+320) * (8+240+8)];
45 unsigned char *PicoDraw2FB = PicoDraw2FB_;
47 static void snd_write(int len);
55 /* functions called by the core */
57 void cache_flush_d_inval_i(void *start, void *end)
60 #if defined(__BLACKBERRY_QNX__)
61 msync(start, end - start, MS_SYNC | MS_CACHE_ONLY | MS_INVALIDATE_ICACHE);
62 #elif defined(__MACH__)
63 size_t len = (char *)end - (char *)start;
64 sys_dcache_flush(start, len);
65 sys_icache_invalidate(start, len);
67 __clear_cache(start, end);
72 void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed)
74 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
78 ret = mmap(req, size, PROT_READ | PROT_WRITE, flags, -1, 0);
79 if (ret == MAP_FAILED) {
80 lprintf("mmap(%08lx, %zd) failed: %d\n", addr, size, errno);
84 if (addr != 0 && ret != (void *)addr) {
85 lprintf("warning: wanted to map @%08lx, got %p\n",
97 void *plat_mremap(void *ptr, size_t oldsize, size_t newsize)
100 void *ret = mremap(ptr, oldsize, newsize, 0);
101 if (ret == MAP_FAILED)
107 size_t preserve_size;
109 preserve_size = oldsize;
110 if (preserve_size > newsize)
111 preserve_size = newsize;
112 tmp = malloc(preserve_size);
115 memcpy(tmp, ptr, preserve_size);
117 munmap(ptr, oldsize);
118 ret = mmap(ptr, newsize, PROT_READ | PROT_WRITE,
119 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
120 if (ret == MAP_FAILED) {
124 memcpy(ret, tmp, preserve_size);
130 void plat_munmap(void *ptr, size_t size)
136 int plat_mem_set_exec(void *ptr, size_t size)
138 int ret = mprotect(ptr, size, PROT_READ | PROT_WRITE | PROT_EXEC);
140 lprintf("mprotect(%p, %zd) failed: %d\n", ptr, size, errno);
145 void emu_video_mode_change(int start_line, int line_count, int is_32cols)
147 memset(vout_buf, 0, 320 * 240 * 2);
148 vout_width = is_32cols ? 256 : 320;
149 PicoDrawSetOutBuf(vout_buf, vout_width * 2);
152 void emu_32x_startup(void)
154 PicoDrawSetOutFormat(PDF_RGB555, 1);
159 void lprintf(const char *fmt, ...)
164 fprintf(emu_log, "PicoDrive: ");
165 vfprintf(emu_log, fmt, list);
172 #include <android/log.h>
174 void lprintf(const char *fmt, ...)
179 __android_log_vprint(ANDROID_LOG_INFO, "PicoDrive", fmt, list);
186 void retro_set_environment(retro_environment_t cb)
188 static const struct retro_variable vars[] = {
189 //{ "region", "Region; Auto|NTSC|PAL" },
195 cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void *)vars);
198 void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
199 void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
200 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
201 void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
202 void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
204 unsigned retro_api_version(void)
206 return RETRO_API_VERSION;
209 void retro_set_controller_port_device(unsigned port, unsigned device)
213 void retro_get_system_info(struct retro_system_info *info)
215 memset(info, 0, sizeof(*info));
216 info->library_name = "PicoDrive";
217 info->library_version = VERSION;
218 info->valid_extensions = "bin|gen|smd|md|32x|cue|iso|sms";
219 info->need_fullpath = true;
222 void retro_get_system_av_info(struct retro_system_av_info *info)
224 memset(info, 0, sizeof(*info));
225 info->timing.fps = Pico.m.pal ? 50 : 60;
226 info->timing.sample_rate = 44100;
227 info->geometry.base_width = 320;
228 info->geometry.base_height = 240;
229 info->geometry.max_width = VOUT_MAX_WIDTH;
230 info->geometry.max_height = VOUT_MAX_HEIGHT;
231 info->geometry.aspect_ratio = 4.0 / 3.0;
234 /* savestates - TODO */
235 size_t retro_serialize_size(void)
240 bool retro_serialize(void *data, size_t size)
245 bool retro_unserialize(const void *data, size_t size)
251 void retro_cheat_reset(void)
255 void retro_cheat_set(unsigned index, bool enabled, const char *code)
259 /* multidisk support */
260 static bool disk_ejected;
261 static unsigned int disk_current_index;
262 static unsigned int disk_count;
263 static struct disks_state {
267 static bool disk_set_eject_state(bool ejected)
270 disk_ejected = ejected;
274 static bool disk_get_eject_state(void)
279 static unsigned int disk_get_image_index(void)
281 return disk_current_index;
284 static bool disk_set_image_index(unsigned int index)
289 if (index >= sizeof(disks) / sizeof(disks[0]))
292 if (disks[index].fname == NULL) {
293 lprintf("missing disk #%u\n", index);
295 // RetroArch specifies "no disk" with index == count,
296 // so don't fail here..
297 disk_current_index = index;
301 lprintf("switching to disk %u: \"%s\"\n", index,
305 cd_type = PicoCdCheck(disks[index].fname, NULL);
306 if (cd_type != CIT_NOT_CD)
307 ret = Insert_CD(disks[index].fname, cd_type);
309 lprintf("Load failed, invalid CD image?\n");
313 disk_current_index = index;
317 static unsigned int disk_get_num_images(void)
322 static bool disk_replace_image_index(unsigned index,
323 const struct retro_game_info *info)
327 if (index >= sizeof(disks) / sizeof(disks[0]))
330 if (disks[index].fname != NULL)
331 free(disks[index].fname);
332 disks[index].fname = NULL;
335 disks[index].fname = strdup(info->path);
336 if (index == disk_current_index)
337 ret = disk_set_image_index(index);
343 static bool disk_add_image_index(void)
345 if (disk_count >= sizeof(disks) / sizeof(disks[0]))
352 static struct retro_disk_control_callback disk_control = {
353 .set_eject_state = disk_set_eject_state,
354 .get_eject_state = disk_get_eject_state,
355 .get_image_index = disk_get_image_index,
356 .set_image_index = disk_set_image_index,
357 .get_num_images = disk_get_num_images,
358 .replace_image_index = disk_replace_image_index,
359 .add_image_index = disk_add_image_index,
362 static void disk_tray_open(void)
364 lprintf("cd tray open\n");
368 static void disk_tray_close(void)
370 lprintf("cd tray close\n");
375 static const char * const biosfiles_us[] = {
376 "us_scd1_9210", "us_scd2_9306", "SegaCDBIOS9303", "bios_CD_U"
378 static const char * const biosfiles_eu[] = {
379 "eu_mcd1_9210", "eu_mcd2_9306", "eu_mcd2_9303", "bios_CD_E"
381 static const char * const biosfiles_jp[] = {
382 "jp_mcd1_9112", "jp_mcd1_9111", "bios_CD_J"
385 static void make_system_path(char *buf, size_t buf_size,
386 const char *name, const char *ext)
388 const char *dir = NULL;
390 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir) {
391 snprintf(buf, buf_size, "%s%c%s%s", dir, SLASH, name, ext);
394 snprintf(buf, buf_size, "%s%s", name, ext);
398 static const char *find_bios(int *region, const char *cd_fname)
400 const char * const *files;
401 static char path[256];
405 if (*region == 4) { // US
406 files = biosfiles_us;
407 count = sizeof(biosfiles_us) / sizeof(char *);
408 } else if (*region == 8) { // EU
409 files = biosfiles_eu;
410 count = sizeof(biosfiles_eu) / sizeof(char *);
411 } else if (*region == 1 || *region == 2) {
412 files = biosfiles_jp;
413 count = sizeof(biosfiles_jp) / sizeof(char *);
418 for (i = 0; i < count; i++)
420 make_system_path(path, sizeof(path), files[i], ".bin");
421 f = fopen(path, "rb");
425 make_system_path(path, sizeof(path), files[i], ".zip");
426 f = fopen(path, "rb");
432 lprintf("using bios: %s\n", path);
440 bool retro_load_game(const struct retro_game_info *info)
442 enum media_type_e media_type;
443 static char carthw_path[256];
446 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
447 if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
448 lprintf("RGB565 suppot required, sorry\n");
452 if (info == NULL || info->path == NULL) {
453 lprintf("info->path required\n");
457 for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++) {
458 if (disks[i].fname != NULL) {
459 free(disks[i].fname);
460 disks[i].fname = NULL;
464 disk_current_index = 0;
466 disks[0].fname = strdup(info->path);
468 make_system_path(carthw_path, sizeof(carthw_path), "carthw", ".cfg");
470 media_type = PicoLoadMedia(info->path, carthw_path,
473 switch (media_type) {
475 lprintf("Failed to detect ROM/CD image type.\n");
478 lprintf("Invalid CD image\n");
480 case PM_BAD_CD_NO_BIOS:
481 lprintf("Missing BIOS\n");
484 lprintf("Load error\n");
492 PicoWriteSound = snd_write;
493 memset(sndBuffer, 0, sizeof(sndBuffer));
500 bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
505 void retro_unload_game(void)
509 unsigned retro_get_region(void)
511 return Pico.m.pal ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
514 void *retro_get_memory_data(unsigned id)
516 if (id != RETRO_MEMORY_SAVE_RAM)
519 if (PicoAHW & PAHW_MCD)
520 return Pico_mcd->bram;
525 size_t retro_get_memory_size(unsigned id)
527 if (id != RETRO_MEMORY_SAVE_RAM)
530 if (PicoAHW & PAHW_MCD)
537 void retro_reset(void)
542 static const unsigned short retro_pico_map[] = {
543 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << GBTN_B,
544 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << GBTN_A,
545 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << GBTN_MODE,
546 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << GBTN_START,
547 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << GBTN_UP,
548 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << GBTN_DOWN,
549 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << GBTN_LEFT,
550 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << GBTN_RIGHT,
551 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << GBTN_C,
552 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << GBTN_Y,
553 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << GBTN_X,
554 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << GBTN_Z,
556 #define RETRO_PICO_MAP_LEN (sizeof(retro_pico_map) / sizeof(retro_pico_map[0]))
558 static void snd_write(int len)
560 audio_batch_cb(PsndOut, len / 4);
565 bool updated = false;
568 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
569 ; //update_variables(true);
573 PicoPad[0] = PicoPad[1] = 0;
574 for (pad = 0; pad < 2; pad++)
575 for (i = 0; i < RETRO_PICO_MAP_LEN; i++)
576 if (input_state_cb(pad, RETRO_DEVICE_JOYPAD, 0, i))
577 PicoPad[pad] |= retro_pico_map[i];
581 video_cb(vout_buf, vout_width, vout_height, vout_width * 2);
584 void retro_init(void)
589 emu_log = fopen("/User/Documents/PicoDrive.log", "w");
591 emu_log = fopen("PicoDrive.log", "w");
597 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
599 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control);
601 PicoOpt = POPT_EN_STEREO|POPT_EN_FM|POPT_EN_PSG|POPT_EN_Z80
602 | POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_MCD_GFX
603 | POPT_EN_32X|POPT_EN_PWM
604 | POPT_ACC_SPRITES|POPT_DIS_32C_BORDER;
606 PicoOpt |= POPT_EN_SVP_DRC;
609 PicoAutoRgnOrder = 0x184; // US, EU, JP
612 p32x_msh2_multiplier = MSH2_MULTI_DEFAULT;
613 p32x_ssh2_multiplier = SSH2_MULTI_DEFAULT;
617 vout_buf = malloc(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
620 PicoDrawSetOutFormat(PDF_RGB555, 1);
621 PicoDrawSetOutBuf(vout_buf, vout_width * 2);
623 //PicoMessage = plat_status_msg_busy_next;
624 PicoMCDopenTray = disk_tray_open;
625 PicoMCDcloseTray = disk_tray_close;
628 void retro_deinit(void)