| 1 | /* |
| 2 | * libretro core glue for PicoDrive |
| 3 | * (C) notaz, 2013 |
| 4 | * |
| 5 | * This work is licensed under the terms of MAME license. |
| 6 | * See COPYING file in the top-level directory. |
| 7 | */ |
| 8 | |
| 9 | #define _GNU_SOURCE 1 // mremap |
| 10 | #include <stdio.h> |
| 11 | #include <stdarg.h> |
| 12 | #include <string.h> |
| 13 | #ifndef _WIN32 |
| 14 | #include <sys/mman.h> |
| 15 | #else |
| 16 | #include <io.h> |
| 17 | #include <windows.h> |
| 18 | #include <sys/types.h> |
| 19 | #endif |
| 20 | #include <errno.h> |
| 21 | #ifdef __MACH__ |
| 22 | #include <libkern/OSCacheControl.h> |
| 23 | #endif |
| 24 | |
| 25 | #include <pico/pico_int.h> |
| 26 | #include <pico/state.h> |
| 27 | #include "common/input_pico.h" |
| 28 | #include "common/version.h" |
| 29 | #include "libretro.h" |
| 30 | |
| 31 | static retro_video_refresh_t video_cb; |
| 32 | static retro_input_poll_t input_poll_cb; |
| 33 | static retro_input_state_t input_state_cb; |
| 34 | static retro_environment_t environ_cb; |
| 35 | static retro_audio_sample_batch_t audio_batch_cb; |
| 36 | |
| 37 | static FILE *emu_log; |
| 38 | |
| 39 | #define VOUT_MAX_WIDTH 320 |
| 40 | #define VOUT_MAX_HEIGHT 240 |
| 41 | static void *vout_buf; |
| 42 | static int vout_width, vout_height; |
| 43 | |
| 44 | static short __attribute__((aligned(4))) sndBuffer[2*44100/50]; |
| 45 | |
| 46 | static void snd_write(int len); |
| 47 | |
| 48 | #ifdef _WIN32 |
| 49 | #define SLASH '\\' |
| 50 | #else |
| 51 | #define SLASH '/' |
| 52 | #endif |
| 53 | |
| 54 | /* functions called by the core */ |
| 55 | |
| 56 | void cache_flush_d_inval_i(void *start, void *end) |
| 57 | { |
| 58 | #ifdef __arm__ |
| 59 | #if defined(__BLACKBERRY_QNX__) |
| 60 | msync(start, end - start, MS_SYNC | MS_CACHE_ONLY | MS_INVALIDATE_ICACHE); |
| 61 | #elif defined(__MACH__) |
| 62 | size_t len = (char *)end - (char *)start; |
| 63 | sys_dcache_flush(start, len); |
| 64 | sys_icache_invalidate(start, len); |
| 65 | #else |
| 66 | __clear_cache(start, end); |
| 67 | #endif |
| 68 | #endif |
| 69 | } |
| 70 | |
| 71 | #ifdef _WIN32 |
| 72 | /* mmap() replacement for Windows |
| 73 | * |
| 74 | * Author: Mike Frysinger <vapier@gentoo.org> |
| 75 | * Placed into the public domain |
| 76 | */ |
| 77 | |
| 78 | /* References: |
| 79 | * CreateFileMapping: http://msdn.microsoft.com/en-us/library/aa366537(VS.85).aspx |
| 80 | * CloseHandle: http://msdn.microsoft.com/en-us/library/ms724211(VS.85).aspx |
| 81 | * MapViewOfFile: http://msdn.microsoft.com/en-us/library/aa366761(VS.85).aspx |
| 82 | * UnmapViewOfFile: http://msdn.microsoft.com/en-us/library/aa366882(VS.85).aspx |
| 83 | */ |
| 84 | |
| 85 | #define PROT_READ 0x1 |
| 86 | #define PROT_WRITE 0x2 |
| 87 | /* This flag is only available in WinXP+ */ |
| 88 | #ifdef FILE_MAP_EXECUTE |
| 89 | #define PROT_EXEC 0x4 |
| 90 | #else |
| 91 | #define PROT_EXEC 0x0 |
| 92 | #define FILE_MAP_EXECUTE 0 |
| 93 | #endif |
| 94 | |
| 95 | #define MAP_SHARED 0x01 |
| 96 | #define MAP_PRIVATE 0x02 |
| 97 | #define MAP_ANONYMOUS 0x20 |
| 98 | #define MAP_ANON MAP_ANONYMOUS |
| 99 | #define MAP_FAILED ((void *) -1) |
| 100 | |
| 101 | #ifdef __USE_FILE_OFFSET64 |
| 102 | # define DWORD_HI(x) (x >> 32) |
| 103 | # define DWORD_LO(x) ((x) & 0xffffffff) |
| 104 | #else |
| 105 | # define DWORD_HI(x) (0) |
| 106 | # define DWORD_LO(x) (x) |
| 107 | #endif |
| 108 | |
| 109 | static void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset) |
| 110 | { |
| 111 | if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) |
| 112 | return MAP_FAILED; |
| 113 | if (fd == -1) { |
| 114 | if (!(flags & MAP_ANON) || offset) |
| 115 | return MAP_FAILED; |
| 116 | } else if (flags & MAP_ANON) |
| 117 | return MAP_FAILED; |
| 118 | |
| 119 | DWORD flProtect; |
| 120 | if (prot & PROT_WRITE) { |
| 121 | if (prot & PROT_EXEC) |
| 122 | flProtect = PAGE_EXECUTE_READWRITE; |
| 123 | else |
| 124 | flProtect = PAGE_READWRITE; |
| 125 | } else if (prot & PROT_EXEC) { |
| 126 | if (prot & PROT_READ) |
| 127 | flProtect = PAGE_EXECUTE_READ; |
| 128 | else if (prot & PROT_EXEC) |
| 129 | flProtect = PAGE_EXECUTE; |
| 130 | } else |
| 131 | flProtect = PAGE_READONLY; |
| 132 | |
| 133 | off_t end = length + offset; |
| 134 | HANDLE mmap_fd, h; |
| 135 | if (fd == -1) |
| 136 | mmap_fd = INVALID_HANDLE_VALUE; |
| 137 | else |
| 138 | mmap_fd = (HANDLE)_get_osfhandle(fd); |
| 139 | h = CreateFileMapping(mmap_fd, NULL, flProtect, DWORD_HI(end), DWORD_LO(end), NULL); |
| 140 | if (h == NULL) |
| 141 | return MAP_FAILED; |
| 142 | |
| 143 | DWORD dwDesiredAccess; |
| 144 | if (prot & PROT_WRITE) |
| 145 | dwDesiredAccess = FILE_MAP_WRITE; |
| 146 | else |
| 147 | dwDesiredAccess = FILE_MAP_READ; |
| 148 | if (prot & PROT_EXEC) |
| 149 | dwDesiredAccess |= FILE_MAP_EXECUTE; |
| 150 | if (flags & MAP_PRIVATE) |
| 151 | dwDesiredAccess |= FILE_MAP_COPY; |
| 152 | void *ret = MapViewOfFile(h, dwDesiredAccess, DWORD_HI(offset), DWORD_LO(offset), length); |
| 153 | if (ret == NULL) { |
| 154 | CloseHandle(h); |
| 155 | ret = MAP_FAILED; |
| 156 | } |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | static void munmap(void *addr, size_t length) |
| 161 | { |
| 162 | UnmapViewOfFile(addr); |
| 163 | /* ruh-ro, we leaked handle from CreateFileMapping() ... */ |
| 164 | } |
| 165 | #endif |
| 166 | |
| 167 | #ifndef MAP_ANONYMOUS |
| 168 | #define MAP_ANONYMOUS MAP_ANON |
| 169 | #endif |
| 170 | |
| 171 | void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed) |
| 172 | { |
| 173 | int flags = MAP_PRIVATE | MAP_ANONYMOUS; |
| 174 | void *req, *ret; |
| 175 | |
| 176 | req = (void *)addr; |
| 177 | ret = mmap(req, size, PROT_READ | PROT_WRITE, flags, -1, 0); |
| 178 | if (ret == MAP_FAILED) { |
| 179 | lprintf("mmap(%08lx, %zd) failed: %d\n", addr, size, errno); |
| 180 | return NULL; |
| 181 | } |
| 182 | |
| 183 | if (addr != 0 && ret != (void *)addr) { |
| 184 | lprintf("warning: wanted to map @%08lx, got %p\n", |
| 185 | addr, ret); |
| 186 | |
| 187 | if (is_fixed) { |
| 188 | munmap(ret, size); |
| 189 | return NULL; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | void *plat_mremap(void *ptr, size_t oldsize, size_t newsize) |
| 197 | { |
| 198 | #ifdef __linux__ |
| 199 | void *ret = mremap(ptr, oldsize, newsize, 0); |
| 200 | if (ret == MAP_FAILED) |
| 201 | return NULL; |
| 202 | |
| 203 | return ret; |
| 204 | #else |
| 205 | void *tmp, *ret; |
| 206 | size_t preserve_size; |
| 207 | |
| 208 | preserve_size = oldsize; |
| 209 | if (preserve_size > newsize) |
| 210 | preserve_size = newsize; |
| 211 | tmp = malloc(preserve_size); |
| 212 | if (tmp == NULL) |
| 213 | return NULL; |
| 214 | memcpy(tmp, ptr, preserve_size); |
| 215 | |
| 216 | munmap(ptr, oldsize); |
| 217 | ret = mmap(ptr, newsize, PROT_READ | PROT_WRITE, |
| 218 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 219 | if (ret == MAP_FAILED) { |
| 220 | free(tmp); |
| 221 | return NULL; |
| 222 | } |
| 223 | memcpy(ret, tmp, preserve_size); |
| 224 | free(tmp); |
| 225 | return ret; |
| 226 | #endif |
| 227 | } |
| 228 | |
| 229 | void plat_munmap(void *ptr, size_t size) |
| 230 | { |
| 231 | if (ptr != NULL) |
| 232 | munmap(ptr, size); |
| 233 | } |
| 234 | |
| 235 | int plat_mem_set_exec(void *ptr, size_t size) |
| 236 | { |
| 237 | #ifdef _WIN32 |
| 238 | int ret = VirtualProtect(ptr,size,PAGE_EXECUTE_READWRITE,0); |
| 239 | if (ret == 0) |
| 240 | lprintf("mprotect(%p, %zd) failed: %d\n", ptr, size, 0); |
| 241 | #else |
| 242 | int ret = mprotect(ptr, size, PROT_READ | PROT_WRITE | PROT_EXEC); |
| 243 | if (ret != 0) |
| 244 | lprintf("mprotect(%p, %zd) failed: %d\n", ptr, size, errno); |
| 245 | #endif |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | void emu_video_mode_change(int start_line, int line_count, int is_32cols) |
| 250 | { |
| 251 | memset(vout_buf, 0, 320 * 240 * 2); |
| 252 | vout_width = is_32cols ? 256 : 320; |
| 253 | PicoDrawSetOutBuf(vout_buf, vout_width * 2); |
| 254 | } |
| 255 | |
| 256 | void emu_32x_startup(void) |
| 257 | { |
| 258 | } |
| 259 | |
| 260 | #ifndef ANDROID |
| 261 | |
| 262 | void lprintf(const char *fmt, ...) |
| 263 | { |
| 264 | va_list list; |
| 265 | |
| 266 | va_start(list, fmt); |
| 267 | fprintf(emu_log, "PicoDrive: "); |
| 268 | vfprintf(emu_log, fmt, list); |
| 269 | va_end(list); |
| 270 | fflush(emu_log); |
| 271 | } |
| 272 | |
| 273 | #else |
| 274 | |
| 275 | #include <android/log.h> |
| 276 | |
| 277 | void lprintf(const char *fmt, ...) |
| 278 | { |
| 279 | va_list list; |
| 280 | |
| 281 | va_start(list, fmt); |
| 282 | __android_log_vprint(ANDROID_LOG_INFO, "PicoDrive", fmt, list); |
| 283 | va_end(list); |
| 284 | } |
| 285 | |
| 286 | #endif |
| 287 | |
| 288 | /* libretro */ |
| 289 | void retro_set_environment(retro_environment_t cb) |
| 290 | { |
| 291 | static const struct retro_variable vars[] = { |
| 292 | //{ "region", "Region; Auto|NTSC|PAL" }, |
| 293 | { "picodrive_input1", "Input device 1; 3 button pad|6 button pad|None" }, |
| 294 | { "picodrive_input2", "Input device 2; 3 button pad|6 button pad|None" }, |
| 295 | #ifdef DRC_SH2 |
| 296 | { "picodrive_drc", "Dynamic recompilers; enabled|disabled" }, |
| 297 | #endif |
| 298 | { NULL, NULL }, |
| 299 | }; |
| 300 | |
| 301 | environ_cb = cb; |
| 302 | |
| 303 | cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void *)vars); |
| 304 | } |
| 305 | |
| 306 | void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; } |
| 307 | void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; } |
| 308 | void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; } |
| 309 | void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; } |
| 310 | void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; } |
| 311 | |
| 312 | unsigned retro_api_version(void) |
| 313 | { |
| 314 | return RETRO_API_VERSION; |
| 315 | } |
| 316 | |
| 317 | void retro_set_controller_port_device(unsigned port, unsigned device) |
| 318 | { |
| 319 | } |
| 320 | |
| 321 | void retro_get_system_info(struct retro_system_info *info) |
| 322 | { |
| 323 | memset(info, 0, sizeof(*info)); |
| 324 | info->library_name = "PicoDrive"; |
| 325 | info->library_version = VERSION; |
| 326 | info->valid_extensions = "bin|gen|smd|md|32x|cue|iso|sms"; |
| 327 | info->need_fullpath = true; |
| 328 | } |
| 329 | |
| 330 | void retro_get_system_av_info(struct retro_system_av_info *info) |
| 331 | { |
| 332 | memset(info, 0, sizeof(*info)); |
| 333 | info->timing.fps = Pico.m.pal ? 50 : 60; |
| 334 | info->timing.sample_rate = 44100; |
| 335 | info->geometry.base_width = 320; |
| 336 | info->geometry.base_height = 240; |
| 337 | info->geometry.max_width = VOUT_MAX_WIDTH; |
| 338 | info->geometry.max_height = VOUT_MAX_HEIGHT; |
| 339 | info->geometry.aspect_ratio = 4.0 / 3.0; |
| 340 | } |
| 341 | |
| 342 | /* savestates */ |
| 343 | struct savestate_state { |
| 344 | const char *load_buf; |
| 345 | char *save_buf; |
| 346 | size_t size; |
| 347 | size_t pos; |
| 348 | }; |
| 349 | |
| 350 | size_t state_read(void *p, size_t size, size_t nmemb, void *file) |
| 351 | { |
| 352 | struct savestate_state *state = file; |
| 353 | size_t bsize = size * nmemb; |
| 354 | |
| 355 | if (state->pos + bsize > state->size) { |
| 356 | lprintf("savestate error: %u/%u\n", |
| 357 | state->pos + bsize, state->size); |
| 358 | bsize = state->size - state->pos; |
| 359 | if ((int)bsize <= 0) |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | memcpy(p, state->load_buf + state->pos, bsize); |
| 364 | state->pos += bsize; |
| 365 | return bsize; |
| 366 | } |
| 367 | |
| 368 | size_t state_write(void *p, size_t size, size_t nmemb, void *file) |
| 369 | { |
| 370 | struct savestate_state *state = file; |
| 371 | size_t bsize = size * nmemb; |
| 372 | |
| 373 | if (state->pos + bsize > state->size) { |
| 374 | lprintf("savestate error: %u/%u\n", |
| 375 | state->pos + bsize, state->size); |
| 376 | bsize = state->size - state->pos; |
| 377 | if ((int)bsize <= 0) |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | memcpy(state->save_buf + state->pos, p, bsize); |
| 382 | state->pos += bsize; |
| 383 | return bsize; |
| 384 | } |
| 385 | |
| 386 | size_t state_skip(void *p, size_t size, size_t nmemb, void *file) |
| 387 | { |
| 388 | struct savestate_state *state = file; |
| 389 | size_t bsize = size * nmemb; |
| 390 | |
| 391 | state->pos += bsize; |
| 392 | return bsize; |
| 393 | } |
| 394 | |
| 395 | size_t state_eof(void *file) |
| 396 | { |
| 397 | struct savestate_state *state = file; |
| 398 | |
| 399 | return state->pos >= state->size; |
| 400 | } |
| 401 | |
| 402 | int state_fseek(void *file, long offset, int whence) |
| 403 | { |
| 404 | struct savestate_state *state = file; |
| 405 | |
| 406 | switch (whence) { |
| 407 | case SEEK_SET: |
| 408 | state->pos = offset; |
| 409 | break; |
| 410 | case SEEK_CUR: |
| 411 | state->pos += offset; |
| 412 | break; |
| 413 | case SEEK_END: |
| 414 | state->pos = state->size + offset; |
| 415 | break; |
| 416 | } |
| 417 | return (int)state->pos; |
| 418 | } |
| 419 | |
| 420 | /* savestate sizes vary wildly depending if cd/32x or |
| 421 | * carthw is active, so run the whole thing to get size */ |
| 422 | size_t retro_serialize_size(void) |
| 423 | { |
| 424 | struct savestate_state state = { 0, }; |
| 425 | int ret; |
| 426 | |
| 427 | ret = PicoStateFP(&state, 1, NULL, state_skip, NULL, state_fseek); |
| 428 | if (ret != 0) |
| 429 | return 0; |
| 430 | |
| 431 | return state.pos; |
| 432 | } |
| 433 | |
| 434 | bool retro_serialize(void *data, size_t size) |
| 435 | { |
| 436 | struct savestate_state state = { 0, }; |
| 437 | int ret; |
| 438 | |
| 439 | state.save_buf = data; |
| 440 | state.size = size; |
| 441 | state.pos = 0; |
| 442 | |
| 443 | ret = PicoStateFP(&state, 1, NULL, state_write, |
| 444 | NULL, state_fseek); |
| 445 | return ret == 0; |
| 446 | } |
| 447 | |
| 448 | bool retro_unserialize(const void *data, size_t size) |
| 449 | { |
| 450 | struct savestate_state state = { 0, }; |
| 451 | int ret; |
| 452 | |
| 453 | state.load_buf = data; |
| 454 | state.size = size; |
| 455 | state.pos = 0; |
| 456 | |
| 457 | ret = PicoStateFP(&state, 0, state_read, NULL, |
| 458 | state_eof, state_fseek); |
| 459 | return ret == 0; |
| 460 | } |
| 461 | |
| 462 | /* cheats - TODO */ |
| 463 | void retro_cheat_reset(void) |
| 464 | { |
| 465 | } |
| 466 | |
| 467 | void retro_cheat_set(unsigned index, bool enabled, const char *code) |
| 468 | { |
| 469 | } |
| 470 | |
| 471 | /* multidisk support */ |
| 472 | static bool disk_ejected; |
| 473 | static unsigned int disk_current_index; |
| 474 | static unsigned int disk_count; |
| 475 | static struct disks_state { |
| 476 | char *fname; |
| 477 | } disks[8]; |
| 478 | |
| 479 | static bool disk_set_eject_state(bool ejected) |
| 480 | { |
| 481 | // TODO? |
| 482 | disk_ejected = ejected; |
| 483 | return true; |
| 484 | } |
| 485 | |
| 486 | static bool disk_get_eject_state(void) |
| 487 | { |
| 488 | return disk_ejected; |
| 489 | } |
| 490 | |
| 491 | static unsigned int disk_get_image_index(void) |
| 492 | { |
| 493 | return disk_current_index; |
| 494 | } |
| 495 | |
| 496 | static bool disk_set_image_index(unsigned int index) |
| 497 | { |
| 498 | cd_img_type cd_type; |
| 499 | int ret; |
| 500 | |
| 501 | if (index >= sizeof(disks) / sizeof(disks[0])) |
| 502 | return false; |
| 503 | |
| 504 | if (disks[index].fname == NULL) { |
| 505 | lprintf("missing disk #%u\n", index); |
| 506 | |
| 507 | // RetroArch specifies "no disk" with index == count, |
| 508 | // so don't fail here.. |
| 509 | disk_current_index = index; |
| 510 | return true; |
| 511 | } |
| 512 | |
| 513 | lprintf("switching to disk %u: \"%s\"\n", index, |
| 514 | disks[index].fname); |
| 515 | |
| 516 | ret = -1; |
| 517 | cd_type = PicoCdCheck(disks[index].fname, NULL); |
| 518 | if (cd_type != CIT_NOT_CD) |
| 519 | ret = Insert_CD(disks[index].fname, cd_type); |
| 520 | if (ret != 0) { |
| 521 | lprintf("Load failed, invalid CD image?\n"); |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | disk_current_index = index; |
| 526 | return true; |
| 527 | } |
| 528 | |
| 529 | static unsigned int disk_get_num_images(void) |
| 530 | { |
| 531 | return disk_count; |
| 532 | } |
| 533 | |
| 534 | static bool disk_replace_image_index(unsigned index, |
| 535 | const struct retro_game_info *info) |
| 536 | { |
| 537 | bool ret = true; |
| 538 | |
| 539 | if (index >= sizeof(disks) / sizeof(disks[0])) |
| 540 | return false; |
| 541 | |
| 542 | if (disks[index].fname != NULL) |
| 543 | free(disks[index].fname); |
| 544 | disks[index].fname = NULL; |
| 545 | |
| 546 | if (info != NULL) { |
| 547 | disks[index].fname = strdup(info->path); |
| 548 | if (index == disk_current_index) |
| 549 | ret = disk_set_image_index(index); |
| 550 | } |
| 551 | |
| 552 | return ret; |
| 553 | } |
| 554 | |
| 555 | static bool disk_add_image_index(void) |
| 556 | { |
| 557 | if (disk_count >= sizeof(disks) / sizeof(disks[0])) |
| 558 | return false; |
| 559 | |
| 560 | disk_count++; |
| 561 | return true; |
| 562 | } |
| 563 | |
| 564 | static struct retro_disk_control_callback disk_control = { |
| 565 | .set_eject_state = disk_set_eject_state, |
| 566 | .get_eject_state = disk_get_eject_state, |
| 567 | .get_image_index = disk_get_image_index, |
| 568 | .set_image_index = disk_set_image_index, |
| 569 | .get_num_images = disk_get_num_images, |
| 570 | .replace_image_index = disk_replace_image_index, |
| 571 | .add_image_index = disk_add_image_index, |
| 572 | }; |
| 573 | |
| 574 | static void disk_tray_open(void) |
| 575 | { |
| 576 | lprintf("cd tray open\n"); |
| 577 | disk_ejected = 1; |
| 578 | } |
| 579 | |
| 580 | static void disk_tray_close(void) |
| 581 | { |
| 582 | lprintf("cd tray close\n"); |
| 583 | disk_ejected = 0; |
| 584 | } |
| 585 | |
| 586 | |
| 587 | static const char * const biosfiles_us[] = { |
| 588 | "us_scd1_9210", "us_scd2_9306", "SegaCDBIOS9303", "bios_CD_U" |
| 589 | }; |
| 590 | static const char * const biosfiles_eu[] = { |
| 591 | "eu_mcd1_9210", "eu_mcd2_9306", "eu_mcd2_9303", "bios_CD_E" |
| 592 | }; |
| 593 | static const char * const biosfiles_jp[] = { |
| 594 | "jp_mcd1_9112", "jp_mcd1_9111", "bios_CD_J" |
| 595 | }; |
| 596 | |
| 597 | static void make_system_path(char *buf, size_t buf_size, |
| 598 | const char *name, const char *ext) |
| 599 | { |
| 600 | const char *dir = NULL; |
| 601 | |
| 602 | if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir) { |
| 603 | snprintf(buf, buf_size, "%s%c%s%s", dir, SLASH, name, ext); |
| 604 | } |
| 605 | else { |
| 606 | snprintf(buf, buf_size, "%s%s", name, ext); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | static const char *find_bios(int *region, const char *cd_fname) |
| 611 | { |
| 612 | const char * const *files; |
| 613 | static char path[256]; |
| 614 | int i, count; |
| 615 | FILE *f = NULL; |
| 616 | |
| 617 | if (*region == 4) { // US |
| 618 | files = biosfiles_us; |
| 619 | count = sizeof(biosfiles_us) / sizeof(char *); |
| 620 | } else if (*region == 8) { // EU |
| 621 | files = biosfiles_eu; |
| 622 | count = sizeof(biosfiles_eu) / sizeof(char *); |
| 623 | } else if (*region == 1 || *region == 2) { |
| 624 | files = biosfiles_jp; |
| 625 | count = sizeof(biosfiles_jp) / sizeof(char *); |
| 626 | } else { |
| 627 | return NULL; |
| 628 | } |
| 629 | |
| 630 | for (i = 0; i < count; i++) |
| 631 | { |
| 632 | make_system_path(path, sizeof(path), files[i], ".bin"); |
| 633 | f = fopen(path, "rb"); |
| 634 | if (f != NULL) |
| 635 | break; |
| 636 | |
| 637 | make_system_path(path, sizeof(path), files[i], ".zip"); |
| 638 | f = fopen(path, "rb"); |
| 639 | if (f != NULL) |
| 640 | break; |
| 641 | } |
| 642 | |
| 643 | if (f != NULL) { |
| 644 | lprintf("using bios: %s\n", path); |
| 645 | fclose(f); |
| 646 | return path; |
| 647 | } |
| 648 | |
| 649 | return NULL; |
| 650 | } |
| 651 | |
| 652 | bool retro_load_game(const struct retro_game_info *info) |
| 653 | { |
| 654 | enum media_type_e media_type; |
| 655 | static char carthw_path[256]; |
| 656 | size_t i; |
| 657 | |
| 658 | enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565; |
| 659 | if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) { |
| 660 | lprintf("RGB565 support required, sorry\n"); |
| 661 | return false; |
| 662 | } |
| 663 | |
| 664 | if (info == NULL || info->path == NULL) { |
| 665 | lprintf("info->path required\n"); |
| 666 | return false; |
| 667 | } |
| 668 | |
| 669 | for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++) { |
| 670 | if (disks[i].fname != NULL) { |
| 671 | free(disks[i].fname); |
| 672 | disks[i].fname = NULL; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | disk_current_index = 0; |
| 677 | disk_count = 1; |
| 678 | disks[0].fname = strdup(info->path); |
| 679 | |
| 680 | make_system_path(carthw_path, sizeof(carthw_path), "carthw", ".cfg"); |
| 681 | |
| 682 | media_type = PicoLoadMedia(info->path, carthw_path, |
| 683 | find_bios, NULL); |
| 684 | |
| 685 | switch (media_type) { |
| 686 | case PM_BAD_DETECT: |
| 687 | lprintf("Failed to detect ROM/CD image type.\n"); |
| 688 | return false; |
| 689 | case PM_BAD_CD: |
| 690 | lprintf("Invalid CD image\n"); |
| 691 | return false; |
| 692 | case PM_BAD_CD_NO_BIOS: |
| 693 | lprintf("Missing BIOS\n"); |
| 694 | return false; |
| 695 | case PM_ERROR: |
| 696 | lprintf("Load error\n"); |
| 697 | return false; |
| 698 | default: |
| 699 | break; |
| 700 | } |
| 701 | |
| 702 | PicoLoopPrepare(); |
| 703 | |
| 704 | PicoWriteSound = snd_write; |
| 705 | memset(sndBuffer, 0, sizeof(sndBuffer)); |
| 706 | PsndOut = sndBuffer; |
| 707 | PsndRerate(1); |
| 708 | |
| 709 | return true; |
| 710 | } |
| 711 | |
| 712 | bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info) |
| 713 | { |
| 714 | return false; |
| 715 | } |
| 716 | |
| 717 | void retro_unload_game(void) |
| 718 | { |
| 719 | } |
| 720 | |
| 721 | unsigned retro_get_region(void) |
| 722 | { |
| 723 | return Pico.m.pal ? RETRO_REGION_PAL : RETRO_REGION_NTSC; |
| 724 | } |
| 725 | |
| 726 | void *retro_get_memory_data(unsigned id) |
| 727 | { |
| 728 | if (id != RETRO_MEMORY_SAVE_RAM) |
| 729 | return NULL; |
| 730 | |
| 731 | if (PicoAHW & PAHW_MCD) |
| 732 | return Pico_mcd->bram; |
| 733 | else |
| 734 | return SRam.data; |
| 735 | } |
| 736 | |
| 737 | size_t retro_get_memory_size(unsigned id) |
| 738 | { |
| 739 | if (id != RETRO_MEMORY_SAVE_RAM) |
| 740 | return 0; |
| 741 | |
| 742 | if (PicoAHW & PAHW_MCD) |
| 743 | // bram |
| 744 | return 0x2000; |
| 745 | else |
| 746 | return SRam.size; |
| 747 | } |
| 748 | |
| 749 | void retro_reset(void) |
| 750 | { |
| 751 | PicoReset(); |
| 752 | } |
| 753 | |
| 754 | static const unsigned short retro_pico_map[] = { |
| 755 | [RETRO_DEVICE_ID_JOYPAD_B] = 1 << GBTN_B, |
| 756 | [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << GBTN_A, |
| 757 | [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << GBTN_MODE, |
| 758 | [RETRO_DEVICE_ID_JOYPAD_START] = 1 << GBTN_START, |
| 759 | [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << GBTN_UP, |
| 760 | [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << GBTN_DOWN, |
| 761 | [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << GBTN_LEFT, |
| 762 | [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << GBTN_RIGHT, |
| 763 | [RETRO_DEVICE_ID_JOYPAD_A] = 1 << GBTN_C, |
| 764 | [RETRO_DEVICE_ID_JOYPAD_X] = 1 << GBTN_Y, |
| 765 | [RETRO_DEVICE_ID_JOYPAD_L] = 1 << GBTN_X, |
| 766 | [RETRO_DEVICE_ID_JOYPAD_R] = 1 << GBTN_Z, |
| 767 | }; |
| 768 | #define RETRO_PICO_MAP_LEN (sizeof(retro_pico_map) / sizeof(retro_pico_map[0])) |
| 769 | |
| 770 | static void snd_write(int len) |
| 771 | { |
| 772 | audio_batch_cb(PsndOut, len / 4); |
| 773 | } |
| 774 | |
| 775 | static enum input_device input_name_to_val(const char *name) |
| 776 | { |
| 777 | if (strcmp(name, "3 button pad") == 0) |
| 778 | return PICO_INPUT_PAD_3BTN; |
| 779 | if (strcmp(name, "6 button pad") == 0) |
| 780 | return PICO_INPUT_PAD_6BTN; |
| 781 | if (strcmp(name, "None") == 0) |
| 782 | return PICO_INPUT_NOTHING; |
| 783 | |
| 784 | lprintf("invalid picodrive_input: '%s'\n", name); |
| 785 | return PICO_INPUT_PAD_3BTN; |
| 786 | } |
| 787 | |
| 788 | static void update_variables(void) |
| 789 | { |
| 790 | struct retro_variable var; |
| 791 | |
| 792 | var.value = NULL; |
| 793 | var.key = "picodrive_input1"; |
| 794 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) |
| 795 | PicoSetInputDevice(0, input_name_to_val(var.value)); |
| 796 | |
| 797 | var.value = NULL; |
| 798 | var.key = "picodrive_input2"; |
| 799 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) |
| 800 | PicoSetInputDevice(1, input_name_to_val(var.value)); |
| 801 | |
| 802 | #ifdef DRC_SH2 |
| 803 | var.value = NULL; |
| 804 | var.key = "picodrive_drc"; |
| 805 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { |
| 806 | if (strcmp(var.value, "enabled") == 0) |
| 807 | PicoOpt |= POPT_EN_DRC; |
| 808 | else |
| 809 | PicoOpt &= ~POPT_EN_DRC; |
| 810 | } |
| 811 | #endif |
| 812 | } |
| 813 | |
| 814 | void retro_run(void) |
| 815 | { |
| 816 | bool updated = false; |
| 817 | int pad, i; |
| 818 | |
| 819 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated) |
| 820 | update_variables(); |
| 821 | |
| 822 | input_poll_cb(); |
| 823 | |
| 824 | PicoPad[0] = PicoPad[1] = 0; |
| 825 | for (pad = 0; pad < 2; pad++) |
| 826 | for (i = 0; i < RETRO_PICO_MAP_LEN; i++) |
| 827 | if (input_state_cb(pad, RETRO_DEVICE_JOYPAD, 0, i)) |
| 828 | PicoPad[pad] |= retro_pico_map[i]; |
| 829 | |
| 830 | PicoFrame(); |
| 831 | |
| 832 | video_cb(vout_buf, vout_width, vout_height, vout_width * 2); |
| 833 | } |
| 834 | |
| 835 | void retro_init(void) |
| 836 | { |
| 837 | int level; |
| 838 | |
| 839 | #ifdef IOS |
| 840 | emu_log = fopen("/User/Documents/PicoDrive.log", "w"); |
| 841 | if (emu_log == NULL) |
| 842 | emu_log = fopen("PicoDrive.log", "w"); |
| 843 | if (emu_log == NULL) |
| 844 | #endif |
| 845 | emu_log = stdout; |
| 846 | |
| 847 | level = 0; |
| 848 | environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level); |
| 849 | |
| 850 | environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control); |
| 851 | |
| 852 | PicoOpt = POPT_EN_STEREO|POPT_EN_FM|POPT_EN_PSG|POPT_EN_Z80 |
| 853 | | POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_MCD_GFX |
| 854 | | POPT_EN_32X|POPT_EN_PWM |
| 855 | | POPT_ACC_SPRITES|POPT_DIS_32C_BORDER; |
| 856 | #ifdef __arm__ |
| 857 | PicoOpt |= POPT_EN_DRC; |
| 858 | #endif |
| 859 | PsndRate = 44100; |
| 860 | PicoAutoRgnOrder = 0x184; // US, EU, JP |
| 861 | PicoCDBuffers = 0; |
| 862 | |
| 863 | vout_width = 320; |
| 864 | vout_height = 240; |
| 865 | vout_buf = malloc(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2); |
| 866 | |
| 867 | PicoInit(); |
| 868 | PicoDrawSetOutFormat(PDF_RGB555, 0); |
| 869 | PicoDrawSetOutBuf(vout_buf, vout_width * 2); |
| 870 | |
| 871 | //PicoMessage = plat_status_msg_busy_next; |
| 872 | PicoMCDopenTray = disk_tray_open; |
| 873 | PicoMCDcloseTray = disk_tray_close; |
| 874 | |
| 875 | update_variables(); |
| 876 | } |
| 877 | |
| 878 | void retro_deinit(void) |
| 879 | { |
| 880 | PicoExit(); |
| 881 | } |