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
15 #include <libkern/OSCacheControl.h>
18 #include <pico/pico_int.h>
19 #include "common/input_pico.h"
20 #include "common/version.h"
23 static retro_video_refresh_t video_cb;
24 static retro_input_poll_t input_poll_cb;
25 static retro_input_state_t input_state_cb;
26 static retro_environment_t environ_cb;
27 static retro_audio_sample_batch_t audio_batch_cb;
31 #define VOUT_MAX_WIDTH 320
32 #define VOUT_MAX_HEIGHT 240
33 static void *vout_buf;
34 static int vout_width, vout_height;
36 static short __attribute__((aligned(4))) sndBuffer[2*44100/50];
38 // FIXME: these 2 shouldn't be here
39 static unsigned char PicoDraw2FB_[(8+320) * (8+240+8)];
40 unsigned char *PicoDraw2FB = PicoDraw2FB_;
42 static void snd_write(int len);
50 /* functions called by the core */
52 void cache_flush_d_inval_i(void *start, void *end)
55 #if defined(__BLACKBERRY_QNX__)
56 msync(start, end - start, MS_SYNC | MS_CACHE_ONLY | MS_INVALIDATE_ICACHE);
57 #elif defined(__MACH__)
58 size_t len = (char *)end - (char *)start;
59 sys_dcache_flush(start, len);
60 sys_icache_invalidate(start, len);
62 __clear_cache(start, end);
67 void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed)
69 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
73 ret = mmap(req, size, PROT_READ | PROT_WRITE, flags, -1, 0);
74 if (ret == MAP_FAILED)
77 if (addr != 0 && ret != (void *)addr) {
78 lprintf("warning: wanted to map @%08lx, got %p\n",
90 void *plat_mremap(void *ptr, size_t oldsize, size_t newsize)
92 void *ret = mremap(ptr, oldsize, newsize, 0);
93 if (ret == MAP_FAILED)
99 void plat_munmap(void *ptr, size_t size)
105 void emu_video_mode_change(int start_line, int line_count, int is_32cols)
107 memset(vout_buf, 0, 320 * 240 * 2);
108 vout_width = is_32cols ? 256 : 320;
109 PicoDrawSetOutBuf(vout_buf, vout_width * 2);
112 void emu_32x_startup(void)
114 PicoDrawSetOutFormat(PDF_RGB555, 1);
119 void lprintf(const char *fmt, ...)
124 fprintf(emu_log, "PicoDrive: ");
125 vfprintf(emu_log, fmt, list);
132 #include <android/log.h>
134 void lprintf(const char *fmt, ...)
139 __android_log_vprint(ANDROID_LOG_INFO, "PicoDrive", fmt, list);
146 void retro_set_environment(retro_environment_t cb)
148 static const struct retro_variable vars[] = {
149 //{ "region", "Region; Auto|NTSC|PAL" },
155 cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void *)vars);
158 void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
159 void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
160 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
161 void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
162 void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
164 unsigned retro_api_version(void)
166 return RETRO_API_VERSION;
169 void retro_set_controller_port_device(unsigned port, unsigned device)
173 void retro_get_system_info(struct retro_system_info *info)
175 memset(info, 0, sizeof(*info));
176 info->library_name = "PicoDrive";
177 info->library_version = VERSION;
178 info->valid_extensions = "bin|gen|smd|32x|cue|iso|sms";
179 info->need_fullpath = true;
182 void retro_get_system_av_info(struct retro_system_av_info *info)
184 memset(info, 0, sizeof(*info));
185 info->timing.fps = Pico.m.pal ? 50 : 60;
186 info->timing.sample_rate = 44100;
187 info->geometry.base_width = 320;
188 info->geometry.base_height = 240;
189 info->geometry.max_width = VOUT_MAX_WIDTH;
190 info->geometry.max_height = VOUT_MAX_HEIGHT;
191 info->geometry.aspect_ratio = 4.0 / 3.0;
194 /* savestates - TODO */
195 size_t retro_serialize_size(void)
200 bool retro_serialize(void *data, size_t size)
205 bool retro_unserialize(const void *data, size_t size)
211 void retro_cheat_reset(void)
215 void retro_cheat_set(unsigned index, bool enabled, const char *code)
219 /* multidisk support */
220 static bool disk_ejected;
221 static unsigned int disk_current_index;
222 static unsigned int disk_count;
223 static struct disks_state {
227 static bool disk_set_eject_state(bool ejected)
230 disk_ejected = ejected;
234 static bool disk_get_eject_state(void)
239 static unsigned int disk_get_image_index(void)
241 return disk_current_index;
244 static bool disk_set_image_index(unsigned int index)
249 if (index >= sizeof(disks) / sizeof(disks[0]))
252 if (disks[index].fname == NULL) {
253 lprintf("missing disk #%u\n", index);
255 // RetroArch specifies "no disk" with index == count,
256 // so don't fail here..
257 disk_current_index = index;
261 lprintf("switching to disk %u: \"%s\"\n", index,
265 cd_type = PicoCdCheck(disks[index].fname, NULL);
266 if (cd_type != CIT_NOT_CD)
267 ret = Insert_CD(disks[index].fname, cd_type);
269 lprintf("Load failed, invalid CD image?\n");
273 disk_current_index = index;
277 static unsigned int disk_get_num_images(void)
282 static bool disk_replace_image_index(unsigned index,
283 const struct retro_game_info *info)
287 if (index >= sizeof(disks) / sizeof(disks[0]))
290 if (disks[index].fname != NULL)
291 free(disks[index].fname);
292 disks[index].fname = NULL;
295 disks[index].fname = strdup(info->path);
296 if (index == disk_current_index)
297 ret = disk_set_image_index(index);
303 static bool disk_add_image_index(void)
305 if (disk_count >= sizeof(disks) / sizeof(disks[0]))
312 static struct retro_disk_control_callback disk_control = {
313 .set_eject_state = disk_set_eject_state,
314 .get_eject_state = disk_get_eject_state,
315 .get_image_index = disk_get_image_index,
316 .set_image_index = disk_set_image_index,
317 .get_num_images = disk_get_num_images,
318 .replace_image_index = disk_replace_image_index,
319 .add_image_index = disk_add_image_index,
322 static void disk_tray_open(void)
324 lprintf("cd tray open\n");
328 static void disk_tray_close(void)
330 lprintf("cd tray close\n");
335 static const char * const biosfiles_us[] = {
336 "us_scd1_9210", "us_scd2_9306", "SegaCDBIOS9303", "bios_CD_U"
338 static const char * const biosfiles_eu[] = {
339 "eu_mcd1_9210", "eu_mcd2_9306", "eu_mcd2_9303", "bios_CD_E"
341 static const char * const biosfiles_jp[] = {
342 "jp_mcd1_9112", "jp_mcd1_9111", "bios_CD_J"
345 static void make_system_path(char *buf, size_t buf_size,
346 const char *name, const char *ext)
348 const char *dir = NULL;
350 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir) {
351 snprintf(buf, buf_size, "%s%c%s%s", dir, SLASH, name, ext);
354 snprintf(buf, buf_size, "%s%s", name, ext);
358 static const char *find_bios(int *region, const char *cd_fname)
360 const char * const *files;
361 static char path[256];
365 if (*region == 4) { // US
366 files = biosfiles_us;
367 count = sizeof(biosfiles_us) / sizeof(char *);
368 } else if (*region == 8) { // EU
369 files = biosfiles_eu;
370 count = sizeof(biosfiles_eu) / sizeof(char *);
371 } else if (*region == 1 || *region == 2) {
372 files = biosfiles_jp;
373 count = sizeof(biosfiles_jp) / sizeof(char *);
378 for (i = 0; i < count; i++)
380 make_system_path(path, sizeof(path), files[i], ".bin");
381 f = fopen(path, "rb");
385 make_system_path(path, sizeof(path), files[i], ".zip");
386 f = fopen(path, "rb");
392 lprintf("using bios: %s\n", path);
400 bool retro_load_game(const struct retro_game_info *info)
402 enum media_type_e media_type;
403 static char carthw_path[256];
406 enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
407 if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
408 lprintf("RGB565 suppot required, sorry\n");
412 if (info == NULL || info->path == NULL) {
413 lprintf("info->path required\n");
417 for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++) {
418 if (disks[i].fname != NULL) {
419 free(disks[i].fname);
420 disks[i].fname = NULL;
424 disk_current_index = 0;
426 disks[0].fname = strdup(info->path);
428 make_system_path(carthw_path, sizeof(carthw_path), "carthw", ".cfg");
430 media_type = PicoLoadMedia(info->path, carthw_path,
433 switch (media_type) {
435 lprintf("Failed to detect ROM/CD image type.\n");
438 lprintf("Invalid CD image\n");
440 case PM_BAD_CD_NO_BIOS:
441 lprintf("Missing BIOS\n");
444 lprintf("Load error\n");
452 PicoWriteSound = snd_write;
453 memset(sndBuffer, 0, sizeof(sndBuffer));
460 bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
465 void retro_unload_game(void)
469 unsigned retro_get_region(void)
471 return Pico.m.pal ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
474 void *retro_get_memory_data(unsigned id)
476 if (id != RETRO_MEMORY_SAVE_RAM)
479 if (PicoAHW & PAHW_MCD)
480 return Pico_mcd->bram;
485 size_t retro_get_memory_size(unsigned id)
487 if (id != RETRO_MEMORY_SAVE_RAM)
490 if (PicoAHW & PAHW_MCD)
497 void retro_reset(void)
502 static const unsigned short retro_pico_map[] = {
503 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << GBTN_B,
504 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << GBTN_A,
505 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << GBTN_MODE,
506 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << GBTN_START,
507 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << GBTN_UP,
508 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << GBTN_DOWN,
509 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << GBTN_LEFT,
510 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << GBTN_RIGHT,
511 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << GBTN_C,
512 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << GBTN_Y,
513 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << GBTN_X,
514 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << GBTN_Z,
516 #define RETRO_PICO_MAP_LEN (sizeof(retro_pico_map) / sizeof(retro_pico_map[0]))
518 static void snd_write(int len)
520 audio_batch_cb(PsndOut, len / 4);
525 bool updated = false;
528 if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
529 ; //update_variables(true);
533 PicoPad[0] = PicoPad[1] = 0;
534 for (pad = 0; pad < 2; pad++)
535 for (i = 0; i < RETRO_PICO_MAP_LEN; i++)
536 if (input_state_cb(pad, RETRO_DEVICE_JOYPAD, 0, i))
537 PicoPad[pad] |= retro_pico_map[i];
541 video_cb(vout_buf, vout_width, vout_height, vout_width * 2);
544 void retro_init(void)
549 emu_log = fopen("/User/Documents/PicoDrive.log", "w");
551 emu_log = fopen("PicoDrive.log", "w");
557 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
559 environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control);
561 PicoOpt = POPT_EN_STEREO|POPT_EN_FM|POPT_EN_PSG|POPT_EN_Z80
562 | POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_SVP_DRC
563 | POPT_ACC_SPRITES|POPT_EN_32X|POPT_EN_PWM
564 | POPT_DIS_32C_BORDER;
566 PicoAutoRgnOrder = 0x184; // US, EU, JP
569 p32x_msh2_multiplier = MSH2_MULTI_DEFAULT;
570 p32x_ssh2_multiplier = SSH2_MULTI_DEFAULT;
574 vout_buf = malloc(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
577 PicoDrawSetOutFormat(PDF_RGB555, 1);
578 PicoDrawSetOutBuf(vout_buf, vout_width * 2);
580 //PicoMessage = plat_status_msg_busy_next;
581 PicoMCDopenTray = disk_tray_open;
582 PicoMCDcloseTray = disk_tray_close;
585 void retro_deinit(void)