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
18 #include <sys/types.h>
22 #include <libkern/OSCacheControl.h>
25 #include <pico/pico_int.h>
26 #include <pico/state.h>
27 #include "common/input_pico.h"
28 #include "common/version.h"
32 #define MAP_ANONYMOUS MAP_ANON
35 static retro_video_refresh_t video_cb;
36 static retro_input_poll_t input_poll_cb;
37 static retro_input_state_t input_state_cb;
38 static retro_environment_t environ_cb;
39 static retro_audio_sample_batch_t audio_batch_cb;
43 #define VOUT_MAX_WIDTH 320
44 #define VOUT_MAX_HEIGHT 240
45 static void *vout_buf;
46 static int vout_width, vout_height;
48 static short __attribute__((aligned(4))) sndBuffer[2*44100/50];
50 static void snd_write(int len);
58 /* functions called by the core */
60 void cache_flush_d_inval_i(void *start, void *end)
63 #if defined(__BLACKBERRY_QNX__)
64 msync(start, end - start, MS_SYNC | MS_CACHE_ONLY | MS_INVALIDATE_ICACHE);
65 #elif defined(__MACH__)
66 size_t len = (char *)end - (char *)start;
67 sys_dcache_flush(start, len);
68 sys_icache_invalidate(start, len);
70 __clear_cache(start, end);
76 /* mmap() replacement for Windows
78 * Author: Mike Frysinger <vapier@gentoo.org>
79 * Placed into the public domain
83 * CreateFileMapping: http://msdn.microsoft.com/en-us/library/aa366537(VS.85).aspx
84 * CloseHandle: http://msdn.microsoft.com/en-us/library/ms724211(VS.85).aspx
85 * MapViewOfFile: http://msdn.microsoft.com/en-us/library/aa366761(VS.85).aspx
86 * UnmapViewOfFile: http://msdn.microsoft.com/en-us/library/aa366882(VS.85).aspx
90 #define PROT_WRITE 0x2
91 /* This flag is only available in WinXP+ */
92 #ifdef FILE_MAP_EXECUTE
96 #define FILE_MAP_EXECUTE 0
99 #define MAP_SHARED 0x01
100 #define MAP_PRIVATE 0x02
101 #define MAP_ANONYMOUS 0x20
102 #define MAP_ANON MAP_ANONYMOUS
103 #define MAP_FAILED ((void *) -1)
105 #ifdef __USE_FILE_OFFSET64
106 # define DWORD_HI(x) (x >> 32)
107 # define DWORD_LO(x) ((x) & 0xffffffff)
109 # define DWORD_HI(x) (0)
110 # define DWORD_LO(x) (x)
113 static void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
115 if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
118 if (!(flags & MAP_ANON) || offset)
120 } else if (flags & MAP_ANON)
124 if (prot & PROT_WRITE) {
125 if (prot & PROT_EXEC)
126 flProtect = PAGE_EXECUTE_READWRITE;
128 flProtect = PAGE_READWRITE;
129 } else if (prot & PROT_EXEC) {
130 if (prot & PROT_READ)
131 flProtect = PAGE_EXECUTE_READ;
132 else if (prot & PROT_EXEC)
133 flProtect = PAGE_EXECUTE;
135 flProtect = PAGE_READONLY;
137 off_t end = length + offset;
140 mmap_fd = INVALID_HANDLE_VALUE;
142 mmap_fd = (HANDLE)_get_osfhandle(fd);
143 h = CreateFileMapping(mmap_fd, NULL, flProtect, DWORD_HI(end), DWORD_LO(end), NULL);
147 DWORD dwDesiredAccess;
148 if (prot & PROT_WRITE)
149 dwDesiredAccess = FILE_MAP_WRITE;
151 dwDesiredAccess = FILE_MAP_READ;
152 if (prot & PROT_EXEC)
153 dwDesiredAccess |= FILE_MAP_EXECUTE;
154 if (flags & MAP_PRIVATE)
155 dwDesiredAccess |= FILE_MAP_COPY;
156 void *ret = MapViewOfFile(h, dwDesiredAccess, DWORD_HI(offset), DWORD_LO(offset), length);
164 static void munmap(void *addr, size_t length)
166 UnmapViewOfFile(addr);
167 /* ruh-ro, we leaked handle from CreateFileMapping() ... */
170 void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed)
172 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
176 ret = mmap(req, size, PROT_READ | PROT_WRITE, flags, -1, 0);
177 if (ret == MAP_FAILED) {
178 lprintf("mmap(%08lx, %zd) failed: %d\n", addr, size, errno);
182 if (addr != 0 && ret != (void *)addr) {
183 lprintf("warning: wanted to map @%08lx, got %p\n",
195 void *plat_mremap(void *ptr, size_t oldsize, size_t newsize)
198 void *ret = mremap(ptr, oldsize, newsize, 0);
199 if (ret == MAP_FAILED)
205 size_t preserve_size;
207 preserve_size = oldsize;
208 if (preserve_size > newsize)
209 preserve_size = newsize;
210 tmp = malloc(preserve_size);
213 memcpy(tmp, ptr, preserve_size);
215 munmap(ptr, oldsize);
216 ret = mmap(ptr, newsize, PROT_READ | PROT_WRITE,
217 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
218 if (ret == MAP_FAILED) {
222 memcpy(ret, tmp, preserve_size);
228 void plat_munmap(void *ptr, size_t size)
234 int plat_mem_set_exec(void *ptr, size_t size)
237 int ret = VirtualProtect(ptr,size,PAGE_EXECUTE_READWRITE,0);
239 lprintf("mprotect(%p, %zd) failed: %d\n", ptr, size, 0);
241 int ret = mprotect(ptr, size, PROT_READ | PROT_WRITE | PROT_EXEC);
243 lprintf("mprotect(%p, %zd) failed: %d\n", ptr, size, errno);
248 void emu_video_mode_change(int start_line, int line_count, int is_32cols)
250 memset(vout_buf, 0, 320 * 240 * 2);
251 vout_width = is_32cols ? 256 : 320;
252 PicoDrawSetOutBuf(vout_buf, vout_width * 2);
255 void emu_32x_startup(void)
261 void lprintf(const char *fmt, ...)
266 fprintf(emu_log, "PicoDrive: ");
267 vfprintf(emu_log, fmt, list);
274 #include <android/log.h>
276 void lprintf(const char *fmt, ...)
281 __android_log_vprint(ANDROID_LOG_INFO, "PicoDrive", fmt, list);
288 void retro_set_environment(retro_environment_t cb)
290 static const struct retro_variable vars[] = {
291 //{ "region", "Region; Auto|NTSC|PAL" },
297 cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void *)vars);
300 void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
301 void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
302 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
303 void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
304 void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
306 unsigned retro_api_version(void)
308 return RETRO_API_VERSION;
311 void retro_set_controller_port_device(unsigned port, unsigned device)
315 void retro_get_system_info(struct retro_system_info *info)
317 memset(info, 0, sizeof(*info));
318 info->library_name = "PicoDrive";
319 info->library_version = VERSION;
320 info->valid_extensions = "bin|gen|smd|md|32x|cue|iso|sms";
321 info->need_fullpath = true;
324 void retro_get_system_av_info(struct retro_system_av_info *info)
326 memset(info, 0, sizeof(*info));
327 info->timing.fps = Pico.m.pal ? 50 : 60;
328 info->timing.sample_rate = 44100;
329 info->geometry.base_width = 320;
330 info->geometry.base_height = 240;
331 info->geometry.max_width = VOUT_MAX_WIDTH;
332 info->geometry.max_height = VOUT_MAX_HEIGHT;
333 info->geometry.aspect_ratio = 4.0 / 3.0;
337 struct savestate_state {
338 const char *load_buf;
344 size_t state_read(void *p, size_t size, size_t nmemb, void *file)
346 struct savestate_state *state = file;
347 size_t bsize = size * nmemb;
349 if (state->pos + bsize > state->size) {
350 lprintf("savestate error: %u/%u\n",
351 state->pos + bsize, state->size);
352 bsize = state->size - state->pos;
357 memcpy(p, state->load_buf + state->pos, bsize);
362 size_t state_write(void *p, size_t size, size_t nmemb, void *file)
364 struct savestate_state *state = file;
365 size_t bsize = size * nmemb;
367 if (state->pos + bsize > state->size) {
368 lprintf("savestate error: %u/%u\n",
369 state->pos + bsize, state->size);
370 bsize = state->size - state->pos;
375 memcpy(state->save_buf + state->pos, p, bsize);
380 size_t state_skip(void *p, size_t size, size_t nmemb, void *file)
382 struct savestate_state *state = file;
383 size_t bsize = size * nmemb;
389 size_t state_eof(void *file)
391 struct savestate_state *state = file;
393 return state->pos >= state->size;
396 int state_fseek(void *file, long offset, int whence)
398 struct savestate_state *state = file;
405 state->pos += offset;
408 state->pos = state->size + offset;
411 return (int)state->pos;
414 /* savestate sizes vary wildly depending if cd/32x or
415 * carthw is active, so run the whole thing to get size */
416 size_t retro_serialize_size(void)
418 struct savestate_state state = { 0, };
421 ret = PicoStateFP(&state, 1, NULL, state_skip, NULL, state_fseek);
428 bool retro_serialize(void *data, size_t size)
430 struct savestate_state state = { 0, };
433 state.save_buf = data;
437 ret = PicoStateFP(&state, 1, NULL, state_write,
442 bool retro_unserialize(const void *data, size_t size)
444 struct savestate_state state = { 0, };
447 state.load_buf = data;
451 ret = PicoStateFP(&state, 0, state_read, NULL,
452 state_eof, state_fseek);
457 void retro_cheat_reset(void)
461 void retro_cheat_set(unsigned index, bool enabled, const char *code)
465 /* multidisk support */
466 static bool disk_ejected;
467 static unsigned int disk_current_index;
468 static unsigned int disk_count;
469 static struct disks_state {
473 static bool disk_set_eject_state(bool ejected)
476 disk_ejected = ejected;
480 static bool disk_get_eject_state(void)
485 static unsigned int disk_get_image_index(void)
487 return disk_current_index;
490 static bool disk_set_image_index(unsigned int index)
495 if (index >= sizeof(disks) / sizeof(disks[0]))
498 if (disks[index].fname == NULL) {
499 lprintf("missing disk #%u\n", index);
501 // RetroArch specifies "no disk" with index == count,
502 // so don't fail here..
503 disk_current_index = index;
507 lprintf("switching to disk %u: \"%s\"\n", index,
511 cd_type = PicoCdCheck(disks[index].fname, NULL);
512 if (cd_type != CIT_NOT_CD)
513 ret = Insert_CD(disks[index].fname, cd_type);
515 lprintf("Load failed, invalid CD image?\n");
519 disk_current_index = index;
523 static unsigned int disk_get_num_images(void)
528 static bool disk_replace_image_index(unsigned index,
529 const struct retro_game_info *info)
533 if (index >= sizeof(disks) / sizeof(disks[0]))
536 if (disks[index].fname != NULL)
537 free(disks[index].fname);
538 disks[index].fname = NULL;
541 disks[index].fname = strdup(info->path);
542 if (index == disk_current_index)
543 ret = disk_set_image_index(index);
549 static bool disk_add_image_index(void)
551 if (disk_count >= sizeof(disks) / sizeof(disks[0]))
558 static struct retro_disk_control_callback disk_control = {
559 .set_eject_state = disk_set_eject_state,
560 .get_eject_state = disk_get_eject_state,
561 .get_image_index = disk_get_image_index,
562 .set_image_index = disk_set_image_index,
563 .get_num_images = disk_get_num_images,
564 .replace_image_index = disk_replace_image_index,
565 .add_image_index = disk_add_image_index,
568 static void disk_tray_open(void)
570 lprintf("cd tray open\n");
574 static void disk_tray_close(void)
576 lprintf("cd tray close\n");
581 static const char * const biosfiles_us[] = {
582 "us_scd1_9210", "us_scd2_9306", "SegaCDBIOS9303", "bios_CD_U"
584 static const char * const biosfiles_eu[] = {
585 "eu_mcd1_9210", "eu_mcd2_9306", "eu_mcd2_9303", "bios_CD_E"
587 static const char * const biosfiles_jp[] = {
588 "jp_mcd1_9112", "jp_mcd1_9111", "bios_CD_J"
591 static void make_system_path(char *buf, size_t buf_size,
592 const char *name, const char *ext)
594 const char *dir = NULL;
596 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir) {
597 snprintf(buf, buf_size, "%s%c%s%s", dir, SLASH, name, ext);
600 snprintf(buf, buf_size, "%s%s", name, ext);
604 static const char *find_bios(int *region, const char *cd_fname)
606 const char * const *files;
607 static char path[256];
611 if (*region == 4) { // US
612 files = biosfiles_us;
613 count = sizeof(biosfiles_us) / sizeof(char *);
614 } else if (*region == 8) { // EU
615 files = biosfiles_eu;
616 count = sizeof(biosfiles_eu) / sizeof(char *);
617 } else if (*region == 1 || *region == 2) {
618 files = biosfiles_jp;
619 count = sizeof(biosfiles_jp) / sizeof(char *);
624 for (i = 0; i < count; i++)
626 make_system_path(path, sizeof(path), files[i], ".bin");
627 f = fopen(path, "rb");
631 make_system_path(path, sizeof(path), files[i], ".zip");
632 f = fopen(path, "rb");
638 lprintf("using bios: %s\n", path);
646 bool retro_load_game(const struct retro_game_info *info)
648 enum media_type_e media_type;
649 static char carthw_path[256];
652 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
653 if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
654 lprintf("RGB565 suppot required, sorry\n");
658 if (info == NULL || info->path == NULL) {
659 lprintf("info->path required\n");
663 for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++) {
664 if (disks[i].fname != NULL) {
665 free(disks[i].fname);
666 disks[i].fname = NULL;
670 disk_current_index = 0;
672 disks[0].fname = strdup(info->path);
674 make_system_path(carthw_path, sizeof(carthw_path), "carthw", ".cfg");
676 media_type = PicoLoadMedia(info->path, carthw_path,
679 switch (media_type) {
681 lprintf("Failed to detect ROM/CD image type.\n");
684 lprintf("Invalid CD image\n");
686 case PM_BAD_CD_NO_BIOS:
687 lprintf("Missing BIOS\n");
690 lprintf("Load error\n");
698 PicoWriteSound = snd_write;
699 memset(sndBuffer, 0, sizeof(sndBuffer));
706 bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
711 void retro_unload_game(void)
715 unsigned retro_get_region(void)
717 return Pico.m.pal ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
720 void *retro_get_memory_data(unsigned id)
722 if (id != RETRO_MEMORY_SAVE_RAM)
725 if (PicoAHW & PAHW_MCD)
726 return Pico_mcd->bram;
731 size_t retro_get_memory_size(unsigned id)
733 if (id != RETRO_MEMORY_SAVE_RAM)
736 if (PicoAHW & PAHW_MCD)
743 void retro_reset(void)
748 static const unsigned short retro_pico_map[] = {
749 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << GBTN_B,
750 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << GBTN_A,
751 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << GBTN_MODE,
752 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << GBTN_START,
753 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << GBTN_UP,
754 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << GBTN_DOWN,
755 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << GBTN_LEFT,
756 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << GBTN_RIGHT,
757 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << GBTN_C,
758 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << GBTN_Y,
759 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << GBTN_X,
760 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << GBTN_Z,
762 #define RETRO_PICO_MAP_LEN (sizeof(retro_pico_map) / sizeof(retro_pico_map[0]))
764 static void snd_write(int len)
766 audio_batch_cb(PsndOut, len / 4);
771 bool updated = false;
774 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
775 ; //update_variables(true);
779 PicoPad[0] = PicoPad[1] = 0;
780 for (pad = 0; pad < 2; pad++)
781 for (i = 0; i < RETRO_PICO_MAP_LEN; i++)
782 if (input_state_cb(pad, RETRO_DEVICE_JOYPAD, 0, i))
783 PicoPad[pad] |= retro_pico_map[i];
787 video_cb(vout_buf, vout_width, vout_height, vout_width * 2);
790 void retro_init(void)
795 emu_log = fopen("/User/Documents/PicoDrive.log", "w");
797 emu_log = fopen("PicoDrive.log", "w");
803 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
805 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control);
807 PicoOpt = POPT_EN_STEREO|POPT_EN_FM|POPT_EN_PSG|POPT_EN_Z80
808 | POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_MCD_GFX
809 | POPT_EN_32X|POPT_EN_PWM
810 | POPT_ACC_SPRITES|POPT_DIS_32C_BORDER;
812 PicoOpt |= POPT_EN_SVP_DRC;
815 PicoAutoRgnOrder = 0x184; // US, EU, JP
820 vout_buf = malloc(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
823 PicoDrawSetOutFormat(PDF_RGB555, 0);
824 PicoDrawSetOutBuf(vout_buf, vout_width * 2);
826 //PicoMessage = plat_status_msg_busy_next;
827 PicoMCDopenTray = disk_tray_open;
828 PicoMCDcloseTray = disk_tray_close;
831 void retro_deinit(void)