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 <sys/types.h>
24 #include <libkern/OSCacheControl.h>
28 #include "3ds/3ds_utils.h"
30 int svcDuplicateHandle(unsigned int* out, unsigned int original);
31 int svcCloseHandle(unsigned int handle);
32 int svcControlProcessMemory(unsigned int process, void* addr0, void* addr1,
33 unsigned int size, unsigned int type, unsigned int perm);
37 static int ctr_svchack_successful = 0;
40 #include <pico/pico_int.h>
41 #include <pico/state.h>
42 #include "../common/input_pico.h"
43 #include "../common/version.h"
46 static retro_log_printf_t log_cb;
47 static retro_video_refresh_t video_cb;
48 static retro_input_poll_t input_poll_cb;
49 static retro_input_state_t input_state_cb;
50 static retro_environment_t environ_cb;
51 static retro_audio_sample_batch_t audio_batch_cb;
53 #define VOUT_MAX_WIDTH 320
54 #define VOUT_MAX_HEIGHT 240
55 static void *vout_buf;
56 static int vout_width, vout_height, vout_offset;
59 static short sndBuffer[2*44100/50];
61 static short __attribute__((aligned(4))) sndBuffer[2*44100/50];
64 static void snd_write(int len);
72 /* functions called by the core */
74 void cache_flush_d_inval_i(void *start, void *end)
77 #if defined(__BLACKBERRY_QNX__)
78 msync(start, end - start, MS_SYNC | MS_CACHE_ONLY | MS_INVALIDATE_ICACHE);
79 #elif defined(__MACH__)
80 size_t len = (char *)end - (char *)start;
81 sys_dcache_flush(start, len);
82 sys_icache_invalidate(start, len);
84 ctr_flush_invalidate_cache();
86 __clear_cache(start, end);
92 /* mmap() replacement for Windows
94 * Author: Mike Frysinger <vapier@gentoo.org>
95 * Placed into the public domain
99 * CreateFileMapping: http://msdn.microsoft.com/en-us/library/aa366537(VS.85).aspx
100 * CloseHandle: http://msdn.microsoft.com/en-us/library/ms724211(VS.85).aspx
101 * MapViewOfFile: http://msdn.microsoft.com/en-us/library/aa366761(VS.85).aspx
102 * UnmapViewOfFile: http://msdn.microsoft.com/en-us/library/aa366882(VS.85).aspx
105 #define PROT_READ 0x1
106 #define PROT_WRITE 0x2
107 /* This flag is only available in WinXP+ */
108 #ifdef FILE_MAP_EXECUTE
109 #define PROT_EXEC 0x4
111 #define PROT_EXEC 0x0
112 #define FILE_MAP_EXECUTE 0
115 #define MAP_SHARED 0x01
116 #define MAP_PRIVATE 0x02
117 #define MAP_ANONYMOUS 0x20
118 #define MAP_ANON MAP_ANONYMOUS
119 #define MAP_FAILED ((void *) -1)
121 #ifdef __USE_FILE_OFFSET64
122 # define DWORD_HI(x) (x >> 32)
123 # define DWORD_LO(x) ((x) & 0xffffffff)
125 # define DWORD_HI(x) (0)
126 # define DWORD_LO(x) (x)
129 static void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
131 uint32_t flProtect, dwDesiredAccess;
136 if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
139 if (!(flags & MAP_ANON) || offset)
141 } else if (flags & MAP_ANON)
144 if (prot & PROT_WRITE) {
145 if (prot & PROT_EXEC)
146 flProtect = PAGE_EXECUTE_READWRITE;
148 flProtect = PAGE_READWRITE;
149 } else if (prot & PROT_EXEC) {
150 if (prot & PROT_READ)
151 flProtect = PAGE_EXECUTE_READ;
152 else if (prot & PROT_EXEC)
153 flProtect = PAGE_EXECUTE;
155 flProtect = PAGE_READONLY;
157 end = length + offset;
160 mmap_fd = INVALID_HANDLE_VALUE;
162 mmap_fd = (HANDLE)_get_osfhandle(fd);
163 h = CreateFileMapping(mmap_fd, NULL, flProtect, DWORD_HI(end), DWORD_LO(end), NULL);
167 if (prot & PROT_WRITE)
168 dwDesiredAccess = FILE_MAP_WRITE;
170 dwDesiredAccess = FILE_MAP_READ;
171 if (prot & PROT_EXEC)
172 dwDesiredAccess |= FILE_MAP_EXECUTE;
173 if (flags & MAP_PRIVATE)
174 dwDesiredAccess |= FILE_MAP_COPY;
175 ret = MapViewOfFile(h, dwDesiredAccess, DWORD_HI(offset), DWORD_LO(offset), length);
183 static void munmap(void *addr, size_t length)
185 UnmapViewOfFile(addr);
186 /* ruh-ro, we leaked handle from CreateFileMapping() ... */
188 #elif defined(NO_MMAP)
189 #define PROT_EXEC 0x04
193 #define MAP_PRIVATE 0
194 #define MAP_ANONYMOUS 0
196 void* mmap(void *desired_addr, size_t len, int mmap_prot, int mmap_flags, int fildes, size_t off)
201 void munmap(void *base_addr, size_t len)
206 int mprotect(void *addr, size_t len, int prot)
208 /* stub - not really needed at this point since this codepath has no dynarecs */
214 #ifndef MAP_ANONYMOUS
215 #define MAP_ANONYMOUS MAP_ANON
221 unsigned int requested_map;
225 pico_mmap_t pico_mmaps[] = {
231 void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed)
235 if (ctr_svchack_successful)
237 pico_mmap_t* pico_mmap;
239 for (pico_mmap = pico_mmaps; pico_mmap->requested_map; pico_mmap++)
241 if ((pico_mmap->requested_map == addr))
243 unsigned int ptr_aligned, tmp;
244 unsigned int currentHandle;
245 unsigned int perm = 0b011;
250 size = (size + 0xFFF) & ~0xFFF;
251 pico_mmap->buffer = malloc(size + 0x1000);
252 ptr_aligned = (((unsigned int)pico_mmap->buffer) + 0xFFF) & ~0xFFF;
254 svcDuplicateHandle(¤tHandle, 0xFFFF8001);
256 if(svcControlProcessMemory(currentHandle, pico_mmap->requested_map, ptr_aligned, size, MEMOP_MAP, perm) < 0)
259 log_cb(RETRO_LOG_ERROR, "could not map memory @0x%08X\n", pico_mmap->requested_map);
263 svcCloseHandle(currentHandle);
264 return (void*)pico_mmap->requested_map;
272 void *plat_mremap(void *ptr, size_t oldsize, size_t newsize)
274 if (ctr_svchack_successful)
276 pico_mmap_t* pico_mmap;
278 for (pico_mmap = pico_mmaps; pico_mmap->requested_map; pico_mmap++)
280 if ((pico_mmap->requested_map == (unsigned int)ptr))
282 unsigned int ptr_aligned;
283 unsigned int currentHandle;
286 oldsize = (oldsize + 0xFFF) & ~0xFFF;
287 newsize = (newsize + 0xFFF) & ~0xFFF;
288 ptr_aligned = (((unsigned int)pico_mmap->buffer) + 0xFFF) & ~0xFFF;
290 svcDuplicateHandle(¤tHandle, 0xFFFF8001);
292 svcControlProcessMemory(currentHandle, pico_mmap->requested_map, ptr_aligned, oldsize, MEMOP_UNMAP, 0b011);
294 tmp = realloc(pico_mmap->buffer, newsize + 0x1000);
298 pico_mmap->buffer = tmp;
299 ptr_aligned = (((unsigned int)pico_mmap->buffer) + 0xFFF) & ~0xFFF;
301 svcControlProcessMemory(currentHandle, pico_mmap->requested_map, ptr_aligned, newsize, MEMOP_MAP, 0x3);
303 svcCloseHandle(currentHandle);
310 return realloc(ptr, newsize);
313 void plat_munmap(void *ptr, size_t size)
315 if (ctr_svchack_successful)
317 pico_mmap_t* pico_mmap;
319 for (pico_mmap = pico_mmaps; pico_mmap->requested_map; pico_mmap++)
321 if ((pico_mmap->requested_map == (unsigned int)ptr))
323 unsigned int ptr_aligned;
324 unsigned int currentHandle;
326 size = (size + 0xFFF) & ~0xFFF;
327 ptr_aligned = (((unsigned int)pico_mmap->buffer) + 0xFFF) & ~0xFFF;
329 svcDuplicateHandle(¤tHandle, 0xFFFF8001);
331 svcControlProcessMemory(currentHandle, (void*)pico_mmap->requested_map, (void*)ptr_aligned, size, MEMOP_UNMAP, 0b011);
333 svcCloseHandle(currentHandle);
335 free(pico_mmap->buffer);
336 pico_mmap->buffer = NULL;
346 void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed)
348 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
352 ret = mmap(req, size, PROT_READ | PROT_WRITE, flags, -1, 0);
353 if (ret == MAP_FAILED) {
355 log_cb(RETRO_LOG_ERROR, "mmap(%08lx, %zd) failed: %d\n", addr, size, errno);
359 if (addr != 0 && ret != (void *)addr) {
361 log_cb(RETRO_LOG_WARN, "warning: wanted to map @%08lx, got %p\n",
373 void *plat_mremap(void *ptr, size_t oldsize, size_t newsize)
376 void *ret = mremap(ptr, oldsize, newsize, 0);
377 if (ret == MAP_FAILED)
383 size_t preserve_size;
385 preserve_size = oldsize;
386 if (preserve_size > newsize)
387 preserve_size = newsize;
388 tmp = malloc(preserve_size);
391 memcpy(tmp, ptr, preserve_size);
393 munmap(ptr, oldsize);
394 ret = mmap(ptr, newsize, PROT_READ | PROT_WRITE,
395 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
396 if (ret == MAP_FAILED) {
400 memcpy(ret, tmp, preserve_size);
406 void plat_munmap(void *ptr, size_t size)
413 int plat_mem_set_exec(void *ptr, size_t size)
416 int ret = VirtualProtect(ptr,size,PAGE_EXECUTE_READWRITE,0);
417 if (ret == 0 && log_cb)
418 log_cb(RETRO_LOG_ERROR, "mprotect(%p, %zd) failed: %d\n", ptr, size, 0);
421 if (ctr_svchack_successful)
423 unsigned int currentHandle;
424 svcDuplicateHandle(¤tHandle, 0xFFFF8001);
425 ret = svcControlProcessMemory(currentHandle, ptr, 0x0,
426 size, MEMOP_PROT, 0b111);
427 svcCloseHandle(currentHandle);
428 ctr_flush_invalidate_cache();
434 log_cb(RETRO_LOG_ERROR, "plat_mem_set_exec called with no svcControlProcessMemory access\n");
440 int ret = mprotect(ptr, size, PROT_READ | PROT_WRITE | PROT_EXEC);
441 if (ret != 0 && log_cb)
442 log_cb(RETRO_LOG_ERROR, "mprotect(%p, %zd) failed: %d\n", ptr, size, errno);
447 void emu_video_mode_change(int start_line, int line_count, int is_32cols)
449 memset(vout_buf, 0, 320 * 240 * 2);
450 vout_width = is_32cols ? 256 : 320;
451 PicoDrawSetOutBuf(vout_buf, vout_width * 2);
453 vout_height = line_count;
454 vout_offset = vout_width * start_line;
457 void emu_32x_startup(void)
461 void lprintf(const char *fmt, ...)
466 vsprintf(buffer, fmt, ap);
467 /* TODO - add 'level' param for warning/error messages? */
469 log_cb(RETRO_LOG_INFO, "%s\n", fmt, ap);
474 void retro_set_environment(retro_environment_t cb)
476 static const struct retro_variable vars[] = {
477 //{ "region", "Region; Auto|NTSC|PAL" },
478 { "picodrive_input1", "Input device 1; 3 button pad|6 button pad|None" },
479 { "picodrive_input2", "Input device 2; 3 button pad|6 button pad|None" },
480 { "picodrive_sprlim", "No sprite limit; disabled|enabled" },
481 { "picodrive_ramcart", "MegaCD RAM cart; disabled|enabled" },
482 { "picodrive_region", "Region; Auto|Japan NTSC|Japan PAL|US|Europe" },
484 { "picodrive_drc", "Dynamic recompilers; enabled|disabled" },
491 cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void *)vars);
494 void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
495 void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
496 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
497 void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
498 void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
500 unsigned retro_api_version(void)
502 return RETRO_API_VERSION;
505 void retro_set_controller_port_device(unsigned port, unsigned device)
509 void retro_get_system_info(struct retro_system_info *info)
511 memset(info, 0, sizeof(*info));
512 info->library_name = "PicoDrive";
513 info->library_version = VERSION;
514 info->valid_extensions = "bin|gen|smd|md|32x|cue|iso|sms";
515 info->need_fullpath = true;
518 void retro_get_system_av_info(struct retro_system_av_info *info)
520 memset(info, 0, sizeof(*info));
521 info->timing.fps = Pico.m.pal ? 50 : 60;
522 info->timing.sample_rate = 44100;
523 info->geometry.base_width = 320;
524 info->geometry.base_height = vout_height;
525 info->geometry.max_width = VOUT_MAX_WIDTH;
526 info->geometry.max_height = VOUT_MAX_HEIGHT;
527 info->geometry.aspect_ratio = 4.0f / 3.0f;
531 struct savestate_state {
532 const char *load_buf;
538 size_t state_read(void *p, size_t size, size_t nmemb, void *file)
540 struct savestate_state *state = file;
541 size_t bsize = size * nmemb;
543 if (state->pos + bsize > state->size) {
545 log_cb(RETRO_LOG_ERROR, "savestate error: %u/%u\n",
546 state->pos + bsize, state->size);
547 bsize = state->size - state->pos;
552 memcpy(p, state->load_buf + state->pos, bsize);
557 size_t state_write(void *p, size_t size, size_t nmemb, void *file)
559 struct savestate_state *state = file;
560 size_t bsize = size * nmemb;
562 if (state->pos + bsize > state->size) {
564 log_cb(RETRO_LOG_ERROR, "savestate error: %u/%u\n",
565 state->pos + bsize, state->size);
566 bsize = state->size - state->pos;
571 memcpy(state->save_buf + state->pos, p, bsize);
576 size_t state_skip(void *p, size_t size, size_t nmemb, void *file)
578 struct savestate_state *state = file;
579 size_t bsize = size * nmemb;
585 size_t state_eof(void *file)
587 struct savestate_state *state = file;
589 return state->pos >= state->size;
592 int state_fseek(void *file, long offset, int whence)
594 struct savestate_state *state = file;
601 state->pos += offset;
604 state->pos = state->size + offset;
607 return (int)state->pos;
610 /* savestate sizes vary wildly depending if cd/32x or
611 * carthw is active, so run the whole thing to get size */
612 size_t retro_serialize_size(void)
614 struct savestate_state state = { 0, };
617 ret = PicoStateFP(&state, 1, NULL, state_skip, NULL, state_fseek);
624 bool retro_serialize(void *data, size_t size)
626 struct savestate_state state = { 0, };
629 state.save_buf = data;
633 ret = PicoStateFP(&state, 1, NULL, state_write,
638 bool retro_unserialize(const void *data, size_t size)
640 struct savestate_state state = { 0, };
643 state.load_buf = data;
647 ret = PicoStateFP(&state, 0, state_read, NULL,
648 state_eof, state_fseek);
653 void retro_cheat_reset(void)
657 void retro_cheat_set(unsigned index, bool enabled, const char *code)
661 /* multidisk support */
662 static bool disk_ejected;
663 static unsigned int disk_current_index;
664 static unsigned int disk_count;
665 static struct disks_state {
669 static bool disk_set_eject_state(bool ejected)
672 disk_ejected = ejected;
676 static bool disk_get_eject_state(void)
681 static unsigned int disk_get_image_index(void)
683 return disk_current_index;
686 static bool disk_set_image_index(unsigned int index)
688 enum cd_img_type cd_type;
691 if (index >= sizeof(disks) / sizeof(disks[0]))
694 if (disks[index].fname == NULL) {
696 log_cb(RETRO_LOG_ERROR, "missing disk #%u\n", index);
698 // RetroArch specifies "no disk" with index == count,
699 // so don't fail here..
700 disk_current_index = index;
705 log_cb(RETRO_LOG_INFO, "switching to disk %u: \"%s\"\n", index,
709 cd_type = PicoCdCheck(disks[index].fname, NULL);
710 if (cd_type != CIT_NOT_CD)
711 ret = cdd_load(disks[index].fname, cd_type);
714 log_cb(RETRO_LOG_ERROR, "Load failed, invalid CD image?\n");
718 disk_current_index = index;
722 static unsigned int disk_get_num_images(void)
727 static bool disk_replace_image_index(unsigned index,
728 const struct retro_game_info *info)
732 if (index >= sizeof(disks) / sizeof(disks[0]))
735 if (disks[index].fname != NULL)
736 free(disks[index].fname);
737 disks[index].fname = NULL;
740 disks[index].fname = strdup(info->path);
741 if (index == disk_current_index)
742 ret = disk_set_image_index(index);
748 static bool disk_add_image_index(void)
750 if (disk_count >= sizeof(disks) / sizeof(disks[0]))
757 static struct retro_disk_control_callback disk_control = {
758 disk_set_eject_state,
759 disk_get_eject_state,
760 disk_get_image_index,
761 disk_set_image_index,
763 disk_replace_image_index,
764 disk_add_image_index,
767 static void disk_tray_open(void)
770 log_cb(RETRO_LOG_INFO, "cd tray open\n");
774 static void disk_tray_close(void)
777 log_cb(RETRO_LOG_INFO, "cd tray close\n");
782 static const char * const biosfiles_us[] = {
783 "us_scd2_9306", "SegaCDBIOS9303", "us_scd1_9210", "bios_CD_U"
785 static const char * const biosfiles_eu[] = {
786 "eu_mcd2_9306", "eu_mcd2_9303", "eu_mcd1_9210", "bios_CD_E"
788 static const char * const biosfiles_jp[] = {
789 "jp_mcd2_921222", "jp_mcd1_9112", "jp_mcd1_9111", "bios_CD_J"
792 static void make_system_path(char *buf, size_t buf_size,
793 const char *name, const char *ext)
795 const char *dir = NULL;
797 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir) {
798 snprintf(buf, buf_size, "%s%c%s%s", dir, SLASH, name, ext);
801 snprintf(buf, buf_size, "%s%s", name, ext);
805 static const char *find_bios(int *region, const char *cd_fname)
807 const char * const *files;
808 static char path[256];
812 if (*region == 4) { // US
813 files = biosfiles_us;
814 count = sizeof(biosfiles_us) / sizeof(char *);
815 } else if (*region == 8) { // EU
816 files = biosfiles_eu;
817 count = sizeof(biosfiles_eu) / sizeof(char *);
818 } else if (*region == 1 || *region == 2) {
819 files = biosfiles_jp;
820 count = sizeof(biosfiles_jp) / sizeof(char *);
825 for (i = 0; i < count; i++)
827 make_system_path(path, sizeof(path), files[i], ".bin");
828 f = fopen(path, "rb");
832 make_system_path(path, sizeof(path), files[i], ".zip");
833 f = fopen(path, "rb");
840 log_cb(RETRO_LOG_INFO, "using bios: %s\n", path);
848 bool retro_load_game(const struct retro_game_info *info)
850 enum media_type_e media_type;
851 static char carthw_path[256];
854 struct retro_input_descriptor desc[] = {
855 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
856 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
857 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
858 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
859 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
860 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "C" },
861 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Y" },
862 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "A" },
863 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "X" },
864 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "Z" },
865 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Mode" },
866 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
868 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
869 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
870 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
871 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
872 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
873 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "C" },
874 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Y" },
875 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "A" },
876 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "X" },
877 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "Z" },
878 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Mode" },
879 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
881 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
882 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
883 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
884 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
885 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
886 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "C" },
887 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Y" },
888 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "A" },
889 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "X" },
890 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "Z" },
891 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Mode" },
892 { 2, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
894 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
895 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
896 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
897 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
898 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
899 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "C" },
900 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Y" },
901 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "A" },
902 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "X" },
903 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "Z" },
904 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Mode" },
905 { 3, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
907 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
908 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
909 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
910 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
911 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
912 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "C" },
913 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Y" },
914 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "A" },
915 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "X" },
916 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "Z" },
917 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Mode" },
918 { 4, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
920 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
921 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
922 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
923 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
924 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
925 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "C" },
926 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Y" },
927 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "A" },
928 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "X" },
929 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "Z" },
930 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Mode" },
931 { 5, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
933 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
934 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
935 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
936 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
937 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
938 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "C" },
939 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Y" },
940 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "A" },
941 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "X" },
942 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "Z" },
943 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Mode" },
944 { 6, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
946 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
947 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
948 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
949 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
950 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
951 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "C" },
952 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Y" },
953 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "A" },
954 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "X" },
955 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "Z" },
956 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Mode" },
957 { 7, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
962 environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);
964 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
965 if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
967 log_cb(RETRO_LOG_ERROR, "RGB565 support required, sorry\n");
971 if (info == NULL || info->path == NULL) {
973 log_cb(RETRO_LOG_ERROR, "info->path required\n");
977 for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++) {
978 if (disks[i].fname != NULL) {
979 free(disks[i].fname);
980 disks[i].fname = NULL;
984 disk_current_index = 0;
986 disks[0].fname = strdup(info->path);
988 make_system_path(carthw_path, sizeof(carthw_path), "carthw", ".cfg");
990 media_type = PicoLoadMedia(info->path, carthw_path,
993 switch (media_type) {
996 log_cb(RETRO_LOG_ERROR, "Failed to detect ROM/CD image type.\n");
1000 log_cb(RETRO_LOG_ERROR, "Invalid CD image\n");
1002 case PM_BAD_CD_NO_BIOS:
1004 log_cb(RETRO_LOG_ERROR, "Missing BIOS\n");
1008 log_cb(RETRO_LOG_ERROR, "Load error\n");
1016 PicoWriteSound = snd_write;
1017 memset(sndBuffer, 0, sizeof(sndBuffer));
1018 PsndOut = sndBuffer;
1024 bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
1029 void retro_unload_game(void)
1033 unsigned retro_get_region(void)
1035 return Pico.m.pal ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
1038 void *retro_get_memory_data(unsigned id)
1040 if (id != RETRO_MEMORY_SAVE_RAM)
1043 if (PicoAHW & PAHW_MCD)
1044 return Pico_mcd->bram;
1049 size_t retro_get_memory_size(unsigned id)
1054 if (id != RETRO_MEMORY_SAVE_RAM)
1057 if (PicoAHW & PAHW_MCD)
1061 if (Pico.m.frame_count == 0)
1064 // if game doesn't write to sram, don't report it to
1065 // libretro so that RA doesn't write out zeroed .srm
1066 for (i = 0, sum = 0; i < SRam.size; i++)
1067 sum |= SRam.data[i];
1069 return (sum != 0) ? SRam.size : 0;
1072 void retro_reset(void)
1077 static const unsigned short retro_pico_map[] = {
1091 #define RETRO_PICO_MAP_LEN (sizeof(retro_pico_map) / sizeof(retro_pico_map[0]))
1093 static void snd_write(int len)
1095 audio_batch_cb(PsndOut, len / 4);
1098 static enum input_device input_name_to_val(const char *name)
1100 if (strcmp(name, "3 button pad") == 0)
1101 return PICO_INPUT_PAD_3BTN;
1102 if (strcmp(name, "6 button pad") == 0)
1103 return PICO_INPUT_PAD_6BTN;
1104 if (strcmp(name, "None") == 0)
1105 return PICO_INPUT_NOTHING;
1108 log_cb(RETRO_LOG_WARN, "invalid picodrive_input: '%s'\n", name);
1109 return PICO_INPUT_PAD_3BTN;
1112 static void update_variables(void)
1114 struct retro_variable var;
1117 var.key = "picodrive_input1";
1118 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1119 PicoSetInputDevice(0, input_name_to_val(var.value));
1122 var.key = "picodrive_input2";
1123 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1124 PicoSetInputDevice(1, input_name_to_val(var.value));
1127 var.key = "picodrive_sprlim";
1128 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
1129 if (strcmp(var.value, "enabled") == 0)
1130 PicoOpt |= POPT_DIS_SPRITE_LIM;
1132 PicoOpt &= ~POPT_DIS_SPRITE_LIM;
1136 var.key = "picodrive_ramcart";
1137 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
1138 if (strcmp(var.value, "enabled") == 0)
1139 PicoOpt |= POPT_EN_MCD_RAMCART;
1141 PicoOpt &= ~POPT_EN_MCD_RAMCART;
1145 var.key = "picodrive_region";
1146 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
1147 if (strcmp(var.value, "Auto") == 0)
1148 PicoRegionOverride = 0;
1149 else if (strcmp(var.value, "Japan NTSC") == 0)
1150 PicoRegionOverride = 1;
1151 else if (strcmp(var.value, "Japan PAL") == 0)
1152 PicoRegionOverride = 2;
1153 else if (strcmp(var.value, "US") == 0)
1154 PicoRegionOverride = 4;
1155 else if (strcmp(var.value, "Europe") == 0)
1156 PicoRegionOverride = 8;
1161 var.key = "picodrive_drc";
1162 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
1163 if (strcmp(var.value, "enabled") == 0)
1164 PicoOpt |= POPT_EN_DRC;
1166 PicoOpt &= ~POPT_EN_DRC;
1170 if(!ctr_svchack_successful)
1171 PicoOpt &= ~POPT_EN_DRC;
1175 void retro_run(void)
1177 bool updated = false;
1180 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
1185 PicoPad[0] = PicoPad[1] = 0;
1186 for (pad = 0; pad < 2; pad++)
1187 for (i = 0; i < RETRO_PICO_MAP_LEN; i++)
1188 if (input_state_cb(pad, RETRO_DEVICE_JOYPAD, 0, i))
1189 PicoPad[pad] |= retro_pico_map[i];
1193 video_cb((short *)vout_buf + vout_offset,
1194 vout_width, vout_height, vout_width * 2);
1197 static void check_system_specs(void)
1199 /* TODO - set different performance level for 32X - 6 for ARM dynarec, higher for interpreter core */
1201 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
1204 void retro_init(void)
1206 struct retro_log_callback log;
1210 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
1212 if (environ_cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &log))
1217 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control);
1220 ctr_svchack_successful = ctr_svchack_init();
1223 PicoOpt = POPT_EN_STEREO|POPT_EN_FM|POPT_EN_PSG|POPT_EN_Z80
1224 | POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_MCD_GFX
1225 | POPT_EN_32X|POPT_EN_PWM
1226 | POPT_ACC_SPRITES|POPT_DIS_32C_BORDER;
1229 if (ctr_svchack_successful)
1231 PicoOpt |= POPT_EN_DRC;
1234 PicoAutoRgnOrder = 0x184; // US, EU, JP
1238 vout_buf = malloc(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
1241 PicoDrawSetOutFormat(PDF_RGB555, 0);
1242 PicoDrawSetOutBuf(vout_buf, vout_width * 2);
1244 //PicoMessage = plat_status_msg_busy_next;
1245 PicoMCDopenTray = disk_tray_open;
1246 PicoMCDcloseTray = disk_tray_close;
1251 void retro_deinit(void)