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
20 #include <libkern/OSCacheControl.h>
23 #include <pico/pico_int.h>
24 #include <pico/state.h>
25 #include "common/input_pico.h"
26 #include "common/version.h"
30 #define MAP_ANONYMOUS MAP_ANON
33 static retro_video_refresh_t video_cb;
34 static retro_input_poll_t input_poll_cb;
35 static retro_input_state_t input_state_cb;
36 static retro_environment_t environ_cb;
37 static retro_audio_sample_batch_t audio_batch_cb;
41 #define VOUT_MAX_WIDTH 320
42 #define VOUT_MAX_HEIGHT 240
43 static void *vout_buf;
44 static int vout_width, vout_height;
46 static short __attribute__((aligned(4))) sndBuffer[2*44100/50];
48 static void snd_write(int len);
56 /* functions called by the core */
58 void cache_flush_d_inval_i(void *start, void *end)
61 #if defined(__BLACKBERRY_QNX__)
62 msync(start, end - start, MS_SYNC | MS_CACHE_ONLY | MS_INVALIDATE_ICACHE);
63 #elif defined(__MACH__)
64 size_t len = (char *)end - (char *)start;
65 sys_dcache_flush(start, len);
66 sys_icache_invalidate(start, len);
68 __clear_cache(start, end);
74 void* mmap(void *desired_addr,
83 SECURITY_ATTRIBUTES sa;
84 sa.nLength = sizeof(sa);
85 sa.lpSecurityDescriptor = NULL;
86 sa.bInheritHandle = TRUE;
87 fmh = CreateFileMapping(fd, &sa, PAGE_WRITECOPY , 0, len, NULL);
88 if (fmh == NULL){ return NULL; }
89 base_addr = MapViewOfFileEx(fmh, FILE_MAP_WRITE|FILE_MAP_READ, 0, off, len, desired_addr);
99 UnmapViewOfFile(base_addr);
104 #define MAP_PRIVATE 0
105 #define MAP_ANONYMOUS 0
107 void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed)
111 void *ret = mmap((void*)addr,size,PROT_READ | PROT_WRITE, flags, -1, 0);
112 if (addr != 0 && ret != (void *)addr) {
113 lprintf("warning: wanted to map @%08lx, got %p\n",
122 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
126 ret = mmap(req, size, PROT_READ | PROT_WRITE, flags, -1, 0);
127 if (ret == MAP_FAILED) {
128 lprintf("mmap(%08lx, %zd) failed: %d\n", addr, size, errno);
132 if (addr != 0 && ret != (void *)addr) {
133 lprintf("warning: wanted to map @%08lx, got %p\n",
146 void *plat_mremap(void *ptr, size_t oldsize, size_t newsize)
149 void *ret = mremap(ptr, oldsize, newsize, 0);
150 if (ret == MAP_FAILED)
156 size_t preserve_size;
158 preserve_size = oldsize;
159 if (preserve_size > newsize)
160 preserve_size = newsize;
161 tmp = malloc(preserve_size);
164 memcpy(tmp, ptr, preserve_size);
166 munmap(ptr, oldsize);
167 ret = mmap(ptr, newsize, PROT_READ | PROT_WRITE,
168 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
169 if (ret == MAP_FAILED) {
173 memcpy(ret, tmp, preserve_size);
179 void plat_munmap(void *ptr, size_t size)
185 int plat_mem_set_exec(void *ptr, size_t size)
188 int ret = VirtualProtect(ptr,size,PAGE_EXECUTE_READWRITE,0);
190 lprintf("mprotect(%p, %zd) failed: %d\n", ptr, size, 0);
192 int ret = mprotect(ptr, size, PROT_READ | PROT_WRITE | PROT_EXEC);
194 lprintf("mprotect(%p, %zd) failed: %d\n", ptr, size, errno);
199 void emu_video_mode_change(int start_line, int line_count, int is_32cols)
201 memset(vout_buf, 0, 320 * 240 * 2);
202 vout_width = is_32cols ? 256 : 320;
203 PicoDrawSetOutBuf(vout_buf, vout_width * 2);
206 void emu_32x_startup(void)
212 void lprintf(const char *fmt, ...)
217 fprintf(emu_log, "PicoDrive: ");
218 vfprintf(emu_log, fmt, list);
225 #include <android/log.h>
227 void lprintf(const char *fmt, ...)
232 __android_log_vprint(ANDROID_LOG_INFO, "PicoDrive", fmt, list);
239 void retro_set_environment(retro_environment_t cb)
241 static const struct retro_variable vars[] = {
242 //{ "region", "Region; Auto|NTSC|PAL" },
248 cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void *)vars);
251 void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
252 void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
253 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
254 void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
255 void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
257 unsigned retro_api_version(void)
259 return RETRO_API_VERSION;
262 void retro_set_controller_port_device(unsigned port, unsigned device)
266 void retro_get_system_info(struct retro_system_info *info)
268 memset(info, 0, sizeof(*info));
269 info->library_name = "PicoDrive";
270 info->library_version = VERSION;
271 info->valid_extensions = "bin|gen|smd|md|32x|cue|iso|sms";
272 info->need_fullpath = true;
275 void retro_get_system_av_info(struct retro_system_av_info *info)
277 memset(info, 0, sizeof(*info));
278 info->timing.fps = Pico.m.pal ? 50 : 60;
279 info->timing.sample_rate = 44100;
280 info->geometry.base_width = 320;
281 info->geometry.base_height = 240;
282 info->geometry.max_width = VOUT_MAX_WIDTH;
283 info->geometry.max_height = VOUT_MAX_HEIGHT;
284 info->geometry.aspect_ratio = 4.0 / 3.0;
288 struct savestate_state {
289 const char *load_buf;
295 size_t state_read(void *p, size_t size, size_t nmemb, void *file)
297 struct savestate_state *state = file;
298 size_t bsize = size * nmemb;
300 if (state->pos + bsize > state->size) {
301 lprintf("savestate error: %u/%u\n",
302 state->pos + bsize, state->size);
303 bsize = state->size - state->pos;
308 memcpy(p, state->load_buf + state->pos, bsize);
313 size_t state_write(void *p, size_t size, size_t nmemb, void *file)
315 struct savestate_state *state = file;
316 size_t bsize = size * nmemb;
318 if (state->pos + bsize > state->size) {
319 lprintf("savestate error: %u/%u\n",
320 state->pos + bsize, state->size);
321 bsize = state->size - state->pos;
326 memcpy(state->save_buf + state->pos, p, bsize);
331 size_t state_skip(void *p, size_t size, size_t nmemb, void *file)
333 struct savestate_state *state = file;
334 size_t bsize = size * nmemb;
340 size_t state_eof(void *file)
342 struct savestate_state *state = file;
344 return state->pos >= state->size;
347 int state_fseek(void *file, long offset, int whence)
349 struct savestate_state *state = file;
356 state->pos += offset;
359 state->pos = state->size + offset;
362 return (int)state->pos;
365 /* savestate sizes vary wildly depending if cd/32x or
366 * carthw is active, so run the whole thing to get size */
367 size_t retro_serialize_size(void)
369 struct savestate_state state = { 0, };
372 ret = PicoStateFP(&state, 1, NULL, state_skip, NULL, state_fseek);
379 bool retro_serialize(void *data, size_t size)
381 struct savestate_state state = { 0, };
384 state.save_buf = data;
388 ret = PicoStateFP(&state, 1, NULL, state_write,
393 bool retro_unserialize(const void *data, size_t size)
395 struct savestate_state state = { 0, };
398 state.load_buf = data;
402 ret = PicoStateFP(&state, 0, state_read, NULL,
403 state_eof, state_fseek);
408 void retro_cheat_reset(void)
412 void retro_cheat_set(unsigned index, bool enabled, const char *code)
416 /* multidisk support */
417 static bool disk_ejected;
418 static unsigned int disk_current_index;
419 static unsigned int disk_count;
420 static struct disks_state {
424 static bool disk_set_eject_state(bool ejected)
427 disk_ejected = ejected;
431 static bool disk_get_eject_state(void)
436 static unsigned int disk_get_image_index(void)
438 return disk_current_index;
441 static bool disk_set_image_index(unsigned int index)
446 if (index >= sizeof(disks) / sizeof(disks[0]))
449 if (disks[index].fname == NULL) {
450 lprintf("missing disk #%u\n", index);
452 // RetroArch specifies "no disk" with index == count,
453 // so don't fail here..
454 disk_current_index = index;
458 lprintf("switching to disk %u: \"%s\"\n", index,
462 cd_type = PicoCdCheck(disks[index].fname, NULL);
463 if (cd_type != CIT_NOT_CD)
464 ret = Insert_CD(disks[index].fname, cd_type);
466 lprintf("Load failed, invalid CD image?\n");
470 disk_current_index = index;
474 static unsigned int disk_get_num_images(void)
479 static bool disk_replace_image_index(unsigned index,
480 const struct retro_game_info *info)
484 if (index >= sizeof(disks) / sizeof(disks[0]))
487 if (disks[index].fname != NULL)
488 free(disks[index].fname);
489 disks[index].fname = NULL;
492 disks[index].fname = strdup(info->path);
493 if (index == disk_current_index)
494 ret = disk_set_image_index(index);
500 static bool disk_add_image_index(void)
502 if (disk_count >= sizeof(disks) / sizeof(disks[0]))
509 static struct retro_disk_control_callback disk_control = {
510 .set_eject_state = disk_set_eject_state,
511 .get_eject_state = disk_get_eject_state,
512 .get_image_index = disk_get_image_index,
513 .set_image_index = disk_set_image_index,
514 .get_num_images = disk_get_num_images,
515 .replace_image_index = disk_replace_image_index,
516 .add_image_index = disk_add_image_index,
519 static void disk_tray_open(void)
521 lprintf("cd tray open\n");
525 static void disk_tray_close(void)
527 lprintf("cd tray close\n");
532 static const char * const biosfiles_us[] = {
533 "us_scd1_9210", "us_scd2_9306", "SegaCDBIOS9303", "bios_CD_U"
535 static const char * const biosfiles_eu[] = {
536 "eu_mcd1_9210", "eu_mcd2_9306", "eu_mcd2_9303", "bios_CD_E"
538 static const char * const biosfiles_jp[] = {
539 "jp_mcd1_9112", "jp_mcd1_9111", "bios_CD_J"
542 static void make_system_path(char *buf, size_t buf_size,
543 const char *name, const char *ext)
545 const char *dir = NULL;
547 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir) {
548 snprintf(buf, buf_size, "%s%c%s%s", dir, SLASH, name, ext);
551 snprintf(buf, buf_size, "%s%s", name, ext);
555 static const char *find_bios(int *region, const char *cd_fname)
557 const char * const *files;
558 static char path[256];
562 if (*region == 4) { // US
563 files = biosfiles_us;
564 count = sizeof(biosfiles_us) / sizeof(char *);
565 } else if (*region == 8) { // EU
566 files = biosfiles_eu;
567 count = sizeof(biosfiles_eu) / sizeof(char *);
568 } else if (*region == 1 || *region == 2) {
569 files = biosfiles_jp;
570 count = sizeof(biosfiles_jp) / sizeof(char *);
575 for (i = 0; i < count; i++)
577 make_system_path(path, sizeof(path), files[i], ".bin");
578 f = fopen(path, "rb");
582 make_system_path(path, sizeof(path), files[i], ".zip");
583 f = fopen(path, "rb");
589 lprintf("using bios: %s\n", path);
597 bool retro_load_game(const struct retro_game_info *info)
599 enum media_type_e media_type;
600 static char carthw_path[256];
603 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
604 if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
605 lprintf("RGB565 suppot required, sorry\n");
609 if (info == NULL || info->path == NULL) {
610 lprintf("info->path required\n");
614 for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++) {
615 if (disks[i].fname != NULL) {
616 free(disks[i].fname);
617 disks[i].fname = NULL;
621 disk_current_index = 0;
623 disks[0].fname = strdup(info->path);
625 make_system_path(carthw_path, sizeof(carthw_path), "carthw", ".cfg");
627 media_type = PicoLoadMedia(info->path, carthw_path,
630 switch (media_type) {
632 lprintf("Failed to detect ROM/CD image type.\n");
635 lprintf("Invalid CD image\n");
637 case PM_BAD_CD_NO_BIOS:
638 lprintf("Missing BIOS\n");
641 lprintf("Load error\n");
649 PicoWriteSound = snd_write;
650 memset(sndBuffer, 0, sizeof(sndBuffer));
657 bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
662 void retro_unload_game(void)
666 unsigned retro_get_region(void)
668 return Pico.m.pal ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
671 void *retro_get_memory_data(unsigned id)
673 if (id != RETRO_MEMORY_SAVE_RAM)
676 if (PicoAHW & PAHW_MCD)
677 return Pico_mcd->bram;
682 size_t retro_get_memory_size(unsigned id)
684 if (id != RETRO_MEMORY_SAVE_RAM)
687 if (PicoAHW & PAHW_MCD)
694 void retro_reset(void)
699 static const unsigned short retro_pico_map[] = {
700 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << GBTN_B,
701 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << GBTN_A,
702 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << GBTN_MODE,
703 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << GBTN_START,
704 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << GBTN_UP,
705 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << GBTN_DOWN,
706 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << GBTN_LEFT,
707 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << GBTN_RIGHT,
708 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << GBTN_C,
709 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << GBTN_Y,
710 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << GBTN_X,
711 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << GBTN_Z,
713 #define RETRO_PICO_MAP_LEN (sizeof(retro_pico_map) / sizeof(retro_pico_map[0]))
715 static void snd_write(int len)
717 audio_batch_cb(PsndOut, len / 4);
722 bool updated = false;
725 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
726 ; //update_variables(true);
730 PicoPad[0] = PicoPad[1] = 0;
731 for (pad = 0; pad < 2; pad++)
732 for (i = 0; i < RETRO_PICO_MAP_LEN; i++)
733 if (input_state_cb(pad, RETRO_DEVICE_JOYPAD, 0, i))
734 PicoPad[pad] |= retro_pico_map[i];
738 video_cb(vout_buf, vout_width, vout_height, vout_width * 2);
741 void retro_init(void)
746 emu_log = fopen("/User/Documents/PicoDrive.log", "w");
748 emu_log = fopen("PicoDrive.log", "w");
754 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
756 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control);
758 PicoOpt = POPT_EN_STEREO|POPT_EN_FM|POPT_EN_PSG|POPT_EN_Z80
759 | POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_MCD_GFX
760 | POPT_EN_32X|POPT_EN_PWM
761 | POPT_ACC_SPRITES|POPT_DIS_32C_BORDER;
763 PicoOpt |= POPT_EN_SVP_DRC;
766 PicoAutoRgnOrder = 0x184; // US, EU, JP
771 vout_buf = malloc(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
774 PicoDrawSetOutFormat(PDF_RGB555, 0);
775 PicoDrawSetOutBuf(vout_buf, vout_width * 2);
777 //PicoMessage = plat_status_msg_busy_next;
778 PicoMCDopenTray = disk_tray_open;
779 PicoMCDcloseTray = disk_tray_close;
782 void retro_deinit(void)