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"
33 int svcDuplicateHandle(unsigned int* out, unsigned int original);
34 int svcCloseHandle(unsigned int handle);
35 int svcControlProcessMemory(unsigned int process, void* addr0, void* addr1,
36 unsigned int size, unsigned int type, unsigned int perm);
37 void* linearMemAlign(size_t size, size_t alignment);
38 void linearFree(void* mem);
40 static int ctr_svchack_successful = 0;
43 #include <pico/pico_int.h>
44 #include <pico/state.h>
45 #include "../common/input_pico.h"
46 #include "../common/version.h"
49 static retro_log_printf_t log_cb;
50 static retro_video_refresh_t video_cb;
51 static retro_input_poll_t input_poll_cb;
52 static retro_input_state_t input_state_cb;
53 static retro_environment_t environ_cb;
54 static retro_audio_sample_batch_t audio_batch_cb;
56 #define VOUT_MAX_WIDTH 320
57 #define VOUT_MAX_HEIGHT 240
58 static void *vout_buf;
59 static int vout_width, vout_height, vout_offset;
62 static short sndBuffer[2*44100/50];
64 static short __attribute__((aligned(4))) sndBuffer[2*44100/50];
67 static void snd_write(int len);
75 /* functions called by the core */
77 void cache_flush_d_inval_i(void *start, void *end)
80 #if defined(__BLACKBERRY_QNX__)
81 msync(start, end - start, MS_SYNC | MS_CACHE_ONLY | MS_INVALIDATE_ICACHE);
82 #elif defined(__MACH__)
83 size_t len = (char *)end - (char *)start;
84 sys_dcache_flush(start, len);
85 sys_icache_invalidate(start, len);
87 ctr_flush_invalidate_cache();
89 __clear_cache(start, end);
95 /* mmap() replacement for Windows
97 * Author: Mike Frysinger <vapier@gentoo.org>
98 * Placed into the public domain
102 * CreateFileMapping: http://msdn.microsoft.com/en-us/library/aa366537(VS.85).aspx
103 * CloseHandle: http://msdn.microsoft.com/en-us/library/ms724211(VS.85).aspx
104 * MapViewOfFile: http://msdn.microsoft.com/en-us/library/aa366761(VS.85).aspx
105 * UnmapViewOfFile: http://msdn.microsoft.com/en-us/library/aa366882(VS.85).aspx
108 #define PROT_READ 0x1
109 #define PROT_WRITE 0x2
110 /* This flag is only available in WinXP+ */
111 #ifdef FILE_MAP_EXECUTE
112 #define PROT_EXEC 0x4
114 #define PROT_EXEC 0x0
115 #define FILE_MAP_EXECUTE 0
118 #define MAP_SHARED 0x01
119 #define MAP_PRIVATE 0x02
120 #define MAP_ANONYMOUS 0x20
121 #define MAP_ANON MAP_ANONYMOUS
122 #define MAP_FAILED ((void *) -1)
124 #ifdef __USE_FILE_OFFSET64
125 # define DWORD_HI(x) (x >> 32)
126 # define DWORD_LO(x) ((x) & 0xffffffff)
128 # define DWORD_HI(x) (0)
129 # define DWORD_LO(x) (x)
132 static void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
134 uint32_t flProtect, dwDesiredAccess;
139 if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
142 if (!(flags & MAP_ANON) || offset)
144 } else if (flags & MAP_ANON)
147 if (prot & PROT_WRITE) {
148 if (prot & PROT_EXEC)
149 flProtect = PAGE_EXECUTE_READWRITE;
151 flProtect = PAGE_READWRITE;
152 } else if (prot & PROT_EXEC) {
153 if (prot & PROT_READ)
154 flProtect = PAGE_EXECUTE_READ;
155 else if (prot & PROT_EXEC)
156 flProtect = PAGE_EXECUTE;
158 flProtect = PAGE_READONLY;
160 end = length + offset;
163 mmap_fd = INVALID_HANDLE_VALUE;
165 mmap_fd = (HANDLE)_get_osfhandle(fd);
166 h = CreateFileMapping(mmap_fd, NULL, flProtect, DWORD_HI(end), DWORD_LO(end), NULL);
170 if (prot & PROT_WRITE)
171 dwDesiredAccess = FILE_MAP_WRITE;
173 dwDesiredAccess = FILE_MAP_READ;
174 if (prot & PROT_EXEC)
175 dwDesiredAccess |= FILE_MAP_EXECUTE;
176 if (flags & MAP_PRIVATE)
177 dwDesiredAccess |= FILE_MAP_COPY;
178 ret = MapViewOfFile(h, dwDesiredAccess, DWORD_HI(offset), DWORD_LO(offset), length);
186 static void munmap(void *addr, size_t length)
188 UnmapViewOfFile(addr);
189 /* ruh-ro, we leaked handle from CreateFileMapping() ... */
191 #elif defined(NO_MMAP)
192 #define PROT_EXEC 0x04
196 #define MAP_PRIVATE 0
197 #define MAP_ANONYMOUS 0
199 void* mmap(void *desired_addr, size_t len, int mmap_prot, int mmap_flags, int fildes, size_t off)
204 void munmap(void *base_addr, size_t len)
209 int mprotect(void *addr, size_t len, int prot)
211 /* stub - not really needed at this point since this codepath has no dynarecs */
217 #ifndef MAP_ANONYMOUS
218 #define MAP_ANONYMOUS MAP_ANON
224 unsigned int requested_map;
228 pico_mmap_t pico_mmaps[] = {
234 void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed)
238 if (ctr_svchack_successful)
240 pico_mmap_t* pico_mmap;
242 for (pico_mmap = pico_mmaps; pico_mmap->requested_map; pico_mmap++)
244 if ((pico_mmap->requested_map == addr))
246 unsigned int ptr_aligned, tmp;
247 unsigned int currentHandle;
248 unsigned int perm = 0b011;
253 size = (size + 0xFFF) & ~0xFFF;
254 pico_mmap->buffer = malloc(size + 0x1000);
255 ptr_aligned = (((unsigned int)pico_mmap->buffer) + 0xFFF) & ~0xFFF;
257 svcDuplicateHandle(¤tHandle, 0xFFFF8001);
259 if(svcControlProcessMemory(currentHandle, pico_mmap->requested_map, ptr_aligned, size, MEMOP_MAP, perm) < 0)
262 log_cb(RETRO_LOG_ERROR, "could not map memory @0x%08X\n", pico_mmap->requested_map);
266 svcCloseHandle(currentHandle);
267 return (void*)pico_mmap->requested_map;
275 void *plat_mremap(void *ptr, size_t oldsize, size_t newsize)
277 if (ctr_svchack_successful)
279 pico_mmap_t* pico_mmap;
281 for (pico_mmap = pico_mmaps; pico_mmap->requested_map; pico_mmap++)
283 if ((pico_mmap->requested_map == (unsigned int)ptr))
285 unsigned int ptr_aligned;
286 unsigned int currentHandle;
289 oldsize = (oldsize + 0xFFF) & ~0xFFF;
290 newsize = (newsize + 0xFFF) & ~0xFFF;
291 ptr_aligned = (((unsigned int)pico_mmap->buffer) + 0xFFF) & ~0xFFF;
293 svcDuplicateHandle(¤tHandle, 0xFFFF8001);
295 svcControlProcessMemory(currentHandle, pico_mmap->requested_map, ptr_aligned, oldsize, MEMOP_UNMAP, 0b011);
297 tmp = realloc(pico_mmap->buffer, newsize + 0x1000);
301 pico_mmap->buffer = tmp;
302 ptr_aligned = (((unsigned int)pico_mmap->buffer) + 0xFFF) & ~0xFFF;
304 svcControlProcessMemory(currentHandle, pico_mmap->requested_map, ptr_aligned, newsize, MEMOP_MAP, 0x3);
306 svcCloseHandle(currentHandle);
313 return realloc(ptr, newsize);
316 void plat_munmap(void *ptr, size_t size)
318 if (ctr_svchack_successful)
320 pico_mmap_t* pico_mmap;
322 for (pico_mmap = pico_mmaps; pico_mmap->requested_map; pico_mmap++)
324 if ((pico_mmap->requested_map == (unsigned int)ptr))
326 unsigned int ptr_aligned;
327 unsigned int currentHandle;
329 size = (size + 0xFFF) & ~0xFFF;
330 ptr_aligned = (((unsigned int)pico_mmap->buffer) + 0xFFF) & ~0xFFF;
332 svcDuplicateHandle(¤tHandle, 0xFFFF8001);
334 svcControlProcessMemory(currentHandle, (void*)pico_mmap->requested_map, (void*)ptr_aligned, size, MEMOP_UNMAP, 0b011);
336 svcCloseHandle(currentHandle);
338 free(pico_mmap->buffer);
339 pico_mmap->buffer = NULL;
349 void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed)
351 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
355 ret = mmap(req, size, PROT_READ | PROT_WRITE, flags, -1, 0);
356 if (ret == MAP_FAILED) {
358 log_cb(RETRO_LOG_ERROR, "mmap(%08lx, %zd) failed: %d\n", addr, size, errno);
362 if (addr != 0 && ret != (void *)addr) {
364 log_cb(RETRO_LOG_WARN, "warning: wanted to map @%08lx, got %p\n",
376 void *plat_mremap(void *ptr, size_t oldsize, size_t newsize)
379 void *ret = mremap(ptr, oldsize, newsize, 0);
380 if (ret == MAP_FAILED)
386 size_t preserve_size;
388 preserve_size = oldsize;
389 if (preserve_size > newsize)
390 preserve_size = newsize;
391 tmp = malloc(preserve_size);
394 memcpy(tmp, ptr, preserve_size);
396 munmap(ptr, oldsize);
397 ret = mmap(ptr, newsize, PROT_READ | PROT_WRITE,
398 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
399 if (ret == MAP_FAILED) {
403 memcpy(ret, tmp, preserve_size);
409 void plat_munmap(void *ptr, size_t size)
416 int plat_mem_set_exec(void *ptr, size_t size)
419 int ret = VirtualProtect(ptr,size,PAGE_EXECUTE_READWRITE,0);
420 if (ret == 0 && log_cb)
421 log_cb(RETRO_LOG_ERROR, "mprotect(%p, %zd) failed: %d\n", ptr, size, 0);
424 if (ctr_svchack_successful)
426 unsigned int currentHandle;
427 svcDuplicateHandle(¤tHandle, 0xFFFF8001);
428 ret = svcControlProcessMemory(currentHandle, ptr, 0x0,
429 size, MEMOP_PROT, 0b111);
430 svcCloseHandle(currentHandle);
431 ctr_flush_invalidate_cache();
437 log_cb(RETRO_LOG_ERROR, "plat_mem_set_exec called with no svcControlProcessMemory access\n");
443 int ret = mprotect(ptr, size, PROT_READ | PROT_WRITE | PROT_EXEC);
444 if (ret != 0 && log_cb)
445 log_cb(RETRO_LOG_ERROR, "mprotect(%p, %zd) failed: %d\n", ptr, size, errno);
450 void emu_video_mode_change(int start_line, int line_count, int is_32cols)
452 memset(vout_buf, 0, 320 * 240 * 2);
453 vout_width = is_32cols ? 256 : 320;
454 PicoDrawSetOutBuf(vout_buf, vout_width * 2);
456 vout_height = line_count;
457 vout_offset = vout_width * start_line;
460 void emu_32x_startup(void)
464 void lprintf(const char *fmt, ...)
469 vsprintf(buffer, fmt, ap);
470 /* TODO - add 'level' param for warning/error messages? */
472 log_cb(RETRO_LOG_INFO, "%s", buffer);
477 void retro_set_environment(retro_environment_t cb)
479 static const struct retro_variable vars[] = {
480 { "picodrive_input1", "Input device 1; 3 button pad|6 button pad|None" },
481 { "picodrive_input2", "Input device 2; 3 button pad|6 button pad|None" },
482 { "picodrive_sprlim", "No sprite limit; disabled|enabled" },
483 { "picodrive_ramcart", "MegaCD RAM cart; disabled|enabled" },
484 { "picodrive_region", "Region; Auto|Japan NTSC|Japan PAL|US|Europe" },
485 { "picodrive_region_fps", "Region FPS; Auto|NTSC|PAL" },
487 { "picodrive_drc", "Dynamic recompilers; enabled|disabled" },
494 cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void *)vars);
497 void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
498 void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
499 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
500 void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
501 void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
503 unsigned retro_api_version(void)
505 return RETRO_API_VERSION;
508 void retro_set_controller_port_device(unsigned port, unsigned device)
512 void retro_get_system_info(struct retro_system_info *info)
514 memset(info, 0, sizeof(*info));
515 info->library_name = "PicoDrive";
516 info->library_version = VERSION;
517 info->valid_extensions = "bin|gen|smd|md|32x|cue|iso|sms";
518 info->need_fullpath = true;
521 void retro_get_system_av_info(struct retro_system_av_info *info)
523 memset(info, 0, sizeof(*info));
524 info->timing.fps = Pico.m.pal ? 50 : 60;
525 info->timing.sample_rate = 44100;
526 info->geometry.base_width = 320;
527 info->geometry.base_height = vout_height;
528 info->geometry.max_width = VOUT_MAX_WIDTH;
529 info->geometry.max_height = VOUT_MAX_HEIGHT;
530 info->geometry.aspect_ratio = 4.0f / 3.0f;
534 struct savestate_state {
535 const char *load_buf;
541 size_t state_read(void *p, size_t size, size_t nmemb, void *file)
543 struct savestate_state *state = file;
544 size_t bsize = size * nmemb;
546 if (state->pos + bsize > state->size) {
548 log_cb(RETRO_LOG_ERROR, "savestate error: %u/%u\n",
549 state->pos + bsize, state->size);
550 bsize = state->size - state->pos;
555 memcpy(p, state->load_buf + state->pos, bsize);
560 size_t state_write(void *p, size_t size, size_t nmemb, void *file)
562 struct savestate_state *state = file;
563 size_t bsize = size * nmemb;
565 if (state->pos + bsize > state->size) {
567 log_cb(RETRO_LOG_ERROR, "savestate error: %u/%u\n",
568 state->pos + bsize, state->size);
569 bsize = state->size - state->pos;
574 memcpy(state->save_buf + state->pos, p, bsize);
579 size_t state_skip(void *p, size_t size, size_t nmemb, void *file)
581 struct savestate_state *state = file;
582 size_t bsize = size * nmemb;
588 size_t state_eof(void *file)
590 struct savestate_state *state = file;
592 return state->pos >= state->size;
595 int state_fseek(void *file, long offset, int whence)
597 struct savestate_state *state = file;
604 state->pos += offset;
607 state->pos = state->size + offset;
610 return (int)state->pos;
613 /* savestate sizes vary wildly depending if cd/32x or
614 * carthw is active, so run the whole thing to get size */
615 size_t retro_serialize_size(void)
617 struct savestate_state state = { 0, };
620 ret = PicoStateFP(&state, 1, NULL, state_skip, NULL, state_fseek);
627 bool retro_serialize(void *data, size_t size)
629 struct savestate_state state = { 0, };
632 state.save_buf = data;
636 ret = PicoStateFP(&state, 1, NULL, state_write,
641 bool retro_unserialize(const void *data, size_t size)
643 struct savestate_state state = { 0, };
646 state.load_buf = data;
650 ret = PicoStateFP(&state, 0, state_read, NULL,
651 state_eof, state_fseek);
656 void retro_cheat_reset(void)
660 void retro_cheat_set(unsigned index, bool enabled, const char *code)
664 /* multidisk support */
665 static bool disk_ejected;
666 static unsigned int disk_current_index;
667 static unsigned int disk_count;
668 static struct disks_state {
672 static bool disk_set_eject_state(bool ejected)
675 disk_ejected = ejected;
679 static bool disk_get_eject_state(void)
684 static unsigned int disk_get_image_index(void)
686 return disk_current_index;
689 static bool disk_set_image_index(unsigned int index)
691 enum cd_img_type cd_type;
694 if (index >= sizeof(disks) / sizeof(disks[0]))
697 if (disks[index].fname == NULL) {
699 log_cb(RETRO_LOG_ERROR, "missing disk #%u\n", index);
701 // RetroArch specifies "no disk" with index == count,
702 // so don't fail here..
703 disk_current_index = index;
708 log_cb(RETRO_LOG_INFO, "switching to disk %u: \"%s\"\n", index,
712 cd_type = PicoCdCheck(disks[index].fname, NULL);
713 if (cd_type != CIT_NOT_CD)
714 ret = cdd_load(disks[index].fname, cd_type);
717 log_cb(RETRO_LOG_ERROR, "Load failed, invalid CD image?\n");
721 disk_current_index = index;
725 static unsigned int disk_get_num_images(void)
730 static bool disk_replace_image_index(unsigned index,
731 const struct retro_game_info *info)
735 if (index >= sizeof(disks) / sizeof(disks[0]))
738 if (disks[index].fname != NULL)
739 free(disks[index].fname);
740 disks[index].fname = NULL;
743 disks[index].fname = strdup(info->path);
744 if (index == disk_current_index)
745 ret = disk_set_image_index(index);
751 static bool disk_add_image_index(void)
753 if (disk_count >= sizeof(disks) / sizeof(disks[0]))
760 static struct retro_disk_control_callback disk_control = {
761 disk_set_eject_state,
762 disk_get_eject_state,
763 disk_get_image_index,
764 disk_set_image_index,
766 disk_replace_image_index,
767 disk_add_image_index,
770 static void disk_tray_open(void)
773 log_cb(RETRO_LOG_INFO, "cd tray open\n");
777 static void disk_tray_close(void)
780 log_cb(RETRO_LOG_INFO, "cd tray close\n");
785 static const char * const biosfiles_us[] = {
786 "us_scd2_9306", "SegaCDBIOS9303", "us_scd1_9210", "bios_CD_U"
788 static const char * const biosfiles_eu[] = {
789 "eu_mcd2_9306", "eu_mcd2_9303", "eu_mcd1_9210", "bios_CD_E"
791 static const char * const biosfiles_jp[] = {
792 "jp_mcd2_921222", "jp_mcd1_9112", "jp_mcd1_9111", "bios_CD_J"
795 static void make_system_path(char *buf, size_t buf_size,
796 const char *name, const char *ext)
798 const char *dir = NULL;
800 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir) {
801 snprintf(buf, buf_size, "%s%c%s%s", dir, SLASH, name, ext);
804 snprintf(buf, buf_size, "%s%s", name, ext);
808 static const char *find_bios(int *region, const char *cd_fname)
810 const char * const *files;
811 static char path[256];
815 if (*region == 4) { // US
816 files = biosfiles_us;
817 count = sizeof(biosfiles_us) / sizeof(char *);
818 } else if (*region == 8) { // EU
819 files = biosfiles_eu;
820 count = sizeof(biosfiles_eu) / sizeof(char *);
821 } else if (*region == 1 || *region == 2) {
822 files = biosfiles_jp;
823 count = sizeof(biosfiles_jp) / sizeof(char *);
828 for (i = 0; i < count; i++)
830 make_system_path(path, sizeof(path), files[i], ".bin");
831 f = fopen(path, "rb");
835 make_system_path(path, sizeof(path), files[i], ".zip");
836 f = fopen(path, "rb");
843 log_cb(RETRO_LOG_INFO, "using bios: %s\n", path);
851 bool retro_load_game(const struct retro_game_info *info)
853 enum media_type_e media_type;
854 static char carthw_path[256];
857 struct retro_input_descriptor desc[] = {
858 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
859 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
860 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
861 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
862 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
863 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "C" },
864 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Y" },
865 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "A" },
866 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "X" },
867 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "Z" },
868 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT,"Mode" },
869 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
871 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
872 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
873 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
874 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
875 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
876 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "C" },
877 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Y" },
878 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "A" },
879 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "X" },
880 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "Z" },
881 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT,"Mode" },
882 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
887 struct retro_input_descriptor desc_sms[] = {
888 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
889 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
890 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
891 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
892 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Button 1 Start" },
893 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Button 2" },
894 { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Button Pause" },
896 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
897 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
898 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
899 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
900 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Button 1 Start" },
901 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Button 2" },
902 { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Button Pause" },
907 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
908 if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
910 log_cb(RETRO_LOG_ERROR, "RGB565 support required, sorry\n");
914 if (info == NULL || info->path == NULL) {
916 log_cb(RETRO_LOG_ERROR, "info->path required\n");
920 for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++) {
921 if (disks[i].fname != NULL) {
922 free(disks[i].fname);
923 disks[i].fname = NULL;
927 disk_current_index = 0;
929 disks[0].fname = strdup(info->path);
931 make_system_path(carthw_path, sizeof(carthw_path), "carthw", ".cfg");
933 media_type = PicoLoadMedia(info->path, carthw_path,
936 switch (media_type) {
939 log_cb(RETRO_LOG_ERROR, "Failed to detect ROM/CD image type.\n");
943 log_cb(RETRO_LOG_ERROR, "Invalid CD image\n");
945 case PM_BAD_CD_NO_BIOS:
947 log_cb(RETRO_LOG_ERROR, "Missing BIOS\n");
951 log_cb(RETRO_LOG_ERROR, "Load error\n");
957 if (media_type == PM_MARK3)
958 environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc_sms);
960 environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);
964 PicoWriteSound = snd_write;
965 memset(sndBuffer, 0, sizeof(sndBuffer));
972 bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
977 void retro_unload_game(void)
981 unsigned retro_get_region(void)
983 return Pico.m.pal ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
986 void *retro_get_memory_data(unsigned type)
992 case RETRO_MEMORY_SAVE_RAM:
993 if (PicoAHW & PAHW_MCD)
994 data = Pico_mcd->bram;
998 case RETRO_MEMORY_SYSTEM_RAM:
999 if (PicoAHW & PAHW_SMS)
1012 size_t retro_get_memory_size(unsigned type)
1019 case RETRO_MEMORY_SAVE_RAM:
1020 if (PicoAHW & PAHW_MCD)
1024 if (Pico.m.frame_count == 0)
1027 // if game doesn't write to sram, don't report it to
1028 // libretro so that RA doesn't write out zeroed .srm
1029 for (i = 0, sum = 0; i < SRam.size; i++)
1030 sum |= SRam.data[i];
1032 return (sum != 0) ? SRam.size : 0;
1034 case RETRO_MEMORY_SYSTEM_RAM:
1035 if (PicoAHW & PAHW_SMS)
1036 return sizeof(Pico.vramb);
1038 return sizeof(Pico.ram);
1046 void retro_reset(void)
1051 static const unsigned short retro_pico_map[] = {
1065 #define RETRO_PICO_MAP_LEN (sizeof(retro_pico_map) / sizeof(retro_pico_map[0]))
1067 static void snd_write(int len)
1069 audio_batch_cb(PsndOut, len / 4);
1072 static enum input_device input_name_to_val(const char *name)
1074 if (strcmp(name, "3 button pad") == 0)
1075 return PICO_INPUT_PAD_3BTN;
1076 if (strcmp(name, "6 button pad") == 0)
1077 return PICO_INPUT_PAD_6BTN;
1078 if (strcmp(name, "None") == 0)
1079 return PICO_INPUT_NOTHING;
1082 log_cb(RETRO_LOG_WARN, "invalid picodrive_input: '%s'\n", name);
1083 return PICO_INPUT_PAD_3BTN;
1086 static void update_variables(void)
1088 struct retro_variable var;
1091 var.key = "picodrive_input1";
1092 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1093 PicoSetInputDevice(0, input_name_to_val(var.value));
1096 var.key = "picodrive_input2";
1097 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
1098 PicoSetInputDevice(1, input_name_to_val(var.value));
1101 var.key = "picodrive_sprlim";
1102 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
1103 if (strcmp(var.value, "enabled") == 0)
1104 PicoOpt |= POPT_DIS_SPRITE_LIM;
1106 PicoOpt &= ~POPT_DIS_SPRITE_LIM;
1110 var.key = "picodrive_ramcart";
1111 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
1112 if (strcmp(var.value, "enabled") == 0)
1113 PicoOpt |= POPT_EN_MCD_RAMCART;
1115 PicoOpt &= ~POPT_EN_MCD_RAMCART;
1118 int OldPicoRegionOverride = PicoRegionOverride;
1120 var.key = "picodrive_region";
1121 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
1122 if (strcmp(var.value, "Auto") == 0)
1123 PicoRegionOverride = 0;
1124 else if (strcmp(var.value, "Japan NTSC") == 0)
1125 PicoRegionOverride = 1;
1126 else if (strcmp(var.value, "Japan PAL") == 0)
1127 PicoRegionOverride = 2;
1128 else if (strcmp(var.value, "US") == 0)
1129 PicoRegionOverride = 4;
1130 else if (strcmp(var.value, "Europe") == 0)
1131 PicoRegionOverride = 8;
1134 int OldPicoRegionFPSOverride = PicoRegionFPSOverride;
1136 var.key = "picodrive_region_fps";
1137 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
1138 if (strcmp(var.value, "Auto") == 0)
1139 PicoRegionFPSOverride = 0;
1140 else if (strcmp(var.value, "NTSC") == 0)
1141 PicoRegionFPSOverride = 1;
1142 else if (strcmp(var.value, "PAL") == 0)
1143 PicoRegionFPSOverride = 2;
1146 // Update region, fps and sound flags if needed
1147 if (PicoRegionOverride != OldPicoRegionOverride ||
1148 PicoRegionFPSOverride != OldPicoRegionFPSOverride)
1157 var.key = "picodrive_drc";
1158 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
1159 if (strcmp(var.value, "enabled") == 0)
1160 PicoOpt |= POPT_EN_DRC;
1162 PicoOpt &= ~POPT_EN_DRC;
1166 if(!ctr_svchack_successful)
1167 PicoOpt &= ~POPT_EN_DRC;
1171 void retro_run(void)
1173 bool updated = false;
1176 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
1181 PicoPad[0] = PicoPad[1] = 0;
1182 for (pad = 0; pad < 2; pad++)
1183 for (i = 0; i < RETRO_PICO_MAP_LEN; i++)
1184 if (input_state_cb(pad, RETRO_DEVICE_JOYPAD, 0, i))
1185 PicoPad[pad] |= retro_pico_map[i];
1189 video_cb((short *)vout_buf + vout_offset,
1190 vout_width, vout_height, vout_width * 2);
1193 static void check_system_specs(void)
1195 /* TODO - set different performance level for 32X - 6 for ARM dynarec, higher for interpreter core */
1197 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
1200 void retro_init(void)
1202 struct retro_log_callback log;
1206 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
1208 if (environ_cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &log))
1213 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control);
1216 ctr_svchack_successful = ctr_svchack_init();
1219 PicoOpt = POPT_EN_STEREO|POPT_EN_FM|POPT_EN_PSG|POPT_EN_Z80
1220 | POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_MCD_GFX
1221 | POPT_EN_32X|POPT_EN_PWM
1222 | POPT_ACC_SPRITES|POPT_DIS_32C_BORDER;
1225 if (ctr_svchack_successful)
1227 PicoOpt |= POPT_EN_DRC;
1230 PicoAutoRgnOrder = 0x184; // US, EU, JP
1235 vout_buf = linearMemAlign(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2, 0x80);
1237 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)
1254 linearFree(vout_buf);