| 1 | /* |
| 2 | * libretro core glue for PicoDrive |
| 3 | * (C) notaz, 2013 |
| 4 | * (C) aliaspider, 2016 |
| 5 | * (C) Daniel De Matteis, 2013 |
| 6 | * |
| 7 | * This work is licensed under the terms of MAME license. |
| 8 | * See COPYING file in the top-level directory. |
| 9 | */ |
| 10 | |
| 11 | #define _GNU_SOURCE 1 // mremap |
| 12 | #include <stdio.h> |
| 13 | #include <stdarg.h> |
| 14 | #include <string.h> |
| 15 | #ifndef _WIN32 |
| 16 | #ifndef NO_MMAP |
| 17 | #include <sys/mman.h> |
| 18 | #endif |
| 19 | #else |
| 20 | #include <io.h> |
| 21 | #include <windows.h> |
| 22 | #include <sys/types.h> |
| 23 | #endif |
| 24 | #include <errno.h> |
| 25 | #ifdef __MACH__ |
| 26 | #include <libkern/OSCacheControl.h> |
| 27 | #endif |
| 28 | |
| 29 | #ifdef _3DS |
| 30 | #include "3ds/3ds_utils.h" |
| 31 | #define MEMOP_MAP 4 |
| 32 | #define MEMOP_UNMAP 5 |
| 33 | #define MEMOP_PROT 6 |
| 34 | |
| 35 | int svcDuplicateHandle(unsigned int* out, unsigned int original); |
| 36 | int svcCloseHandle(unsigned int handle); |
| 37 | int svcControlProcessMemory(unsigned int process, void* addr0, void* addr1, |
| 38 | unsigned int size, unsigned int type, unsigned int perm); |
| 39 | void* linearMemAlign(size_t size, size_t alignment); |
| 40 | void linearFree(void* mem); |
| 41 | |
| 42 | static int ctr_svchack_successful = 0; |
| 43 | |
| 44 | #elif defined(VITA) |
| 45 | #define TARGET_SIZE_2 24 // 2^24 = 16 megabytes |
| 46 | |
| 47 | #include <psp2/kernel/sysmem.h> |
| 48 | static int sceBlock; |
| 49 | int getVMBlock(); |
| 50 | int _newlib_vm_size_user = 1 << TARGET_SIZE_2; |
| 51 | |
| 52 | #endif |
| 53 | |
| 54 | #include <pico/pico_int.h> |
| 55 | #include <pico/state.h> |
| 56 | #include <pico/patch.h> |
| 57 | #include "../common/input_pico.h" |
| 58 | #include "../common/version.h" |
| 59 | #include "libretro.h" |
| 60 | |
| 61 | static retro_log_printf_t log_cb; |
| 62 | static retro_video_refresh_t video_cb; |
| 63 | static retro_input_poll_t input_poll_cb; |
| 64 | static retro_input_state_t input_state_cb; |
| 65 | static retro_environment_t environ_cb; |
| 66 | static retro_audio_sample_batch_t audio_batch_cb; |
| 67 | |
| 68 | #define VOUT_MAX_WIDTH 320 |
| 69 | #define VOUT_MAX_HEIGHT 240 |
| 70 | |
| 71 | static const float VOUT_PAR = 0.0; |
| 72 | static const float VOUT_4_3 = (224.0f * (4.0f / 3.0f)); |
| 73 | static const float VOUT_CRT = (224.0f * 1.29911f); |
| 74 | |
| 75 | bool show_overscan = false; |
| 76 | |
| 77 | static void *vout_buf; |
| 78 | static int vout_width, vout_height, vout_offset; |
| 79 | static float user_vout_width = 0.0; |
| 80 | |
| 81 | #ifdef _MSC_VER |
| 82 | static short sndBuffer[2*44100/50]; |
| 83 | #else |
| 84 | static short __attribute__((aligned(4))) sndBuffer[2*44100/50]; |
| 85 | #endif |
| 86 | |
| 87 | static void snd_write(int len); |
| 88 | |
| 89 | #ifdef _WIN32 |
| 90 | #define SLASH '\\' |
| 91 | #else |
| 92 | #define SLASH '/' |
| 93 | #endif |
| 94 | |
| 95 | /* functions called by the core */ |
| 96 | |
| 97 | void cache_flush_d_inval_i(void *start, void *end) |
| 98 | { |
| 99 | #ifdef __arm__ |
| 100 | size_t len = (char *)end - (char *)start; |
| 101 | #if defined(__BLACKBERRY_QNX__) |
| 102 | msync(start, end - start, MS_SYNC | MS_CACHE_ONLY | MS_INVALIDATE_ICACHE); |
| 103 | #elif defined(__MACH__) |
| 104 | sys_dcache_flush(start, len); |
| 105 | sys_icache_invalidate(start, len); |
| 106 | #elif defined(_3DS) |
| 107 | ctr_flush_invalidate_cache(); |
| 108 | #elif defined(VITA) |
| 109 | sceKernelSyncVMDomain(sceBlock, start, len); |
| 110 | #else |
| 111 | __clear_cache(start, end); |
| 112 | #endif |
| 113 | #endif |
| 114 | } |
| 115 | |
| 116 | #ifdef _WIN32 |
| 117 | /* mmap() replacement for Windows |
| 118 | * |
| 119 | * Author: Mike Frysinger <vapier@gentoo.org> |
| 120 | * Placed into the public domain |
| 121 | */ |
| 122 | |
| 123 | /* References: |
| 124 | * CreateFileMapping: http://msdn.microsoft.com/en-us/library/aa366537(VS.85).aspx |
| 125 | * CloseHandle: http://msdn.microsoft.com/en-us/library/ms724211(VS.85).aspx |
| 126 | * MapViewOfFile: http://msdn.microsoft.com/en-us/library/aa366761(VS.85).aspx |
| 127 | * UnmapViewOfFile: http://msdn.microsoft.com/en-us/library/aa366882(VS.85).aspx |
| 128 | */ |
| 129 | |
| 130 | #define PROT_READ 0x1 |
| 131 | #define PROT_WRITE 0x2 |
| 132 | /* This flag is only available in WinXP+ */ |
| 133 | #ifdef FILE_MAP_EXECUTE |
| 134 | #define PROT_EXEC 0x4 |
| 135 | #else |
| 136 | #define PROT_EXEC 0x0 |
| 137 | #define FILE_MAP_EXECUTE 0 |
| 138 | #endif |
| 139 | |
| 140 | #define MAP_SHARED 0x01 |
| 141 | #define MAP_PRIVATE 0x02 |
| 142 | #define MAP_ANONYMOUS 0x20 |
| 143 | #define MAP_ANON MAP_ANONYMOUS |
| 144 | #define MAP_FAILED ((void *) -1) |
| 145 | |
| 146 | #ifdef __USE_FILE_OFFSET64 |
| 147 | # define DWORD_HI(x) (x >> 32) |
| 148 | # define DWORD_LO(x) ((x) & 0xffffffff) |
| 149 | #else |
| 150 | # define DWORD_HI(x) (0) |
| 151 | # define DWORD_LO(x) (x) |
| 152 | #endif |
| 153 | |
| 154 | static void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset) |
| 155 | { |
| 156 | uint32_t flProtect, dwDesiredAccess; |
| 157 | off_t end; |
| 158 | HANDLE mmap_fd, h; |
| 159 | void *ret; |
| 160 | |
| 161 | if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) |
| 162 | return MAP_FAILED; |
| 163 | if (fd == -1) { |
| 164 | if (!(flags & MAP_ANON) || offset) |
| 165 | return MAP_FAILED; |
| 166 | } else if (flags & MAP_ANON) |
| 167 | return MAP_FAILED; |
| 168 | |
| 169 | if (prot & PROT_WRITE) { |
| 170 | if (prot & PROT_EXEC) |
| 171 | flProtect = PAGE_EXECUTE_READWRITE; |
| 172 | else |
| 173 | flProtect = PAGE_READWRITE; |
| 174 | } else if (prot & PROT_EXEC) { |
| 175 | if (prot & PROT_READ) |
| 176 | flProtect = PAGE_EXECUTE_READ; |
| 177 | else if (prot & PROT_EXEC) |
| 178 | flProtect = PAGE_EXECUTE; |
| 179 | } else |
| 180 | flProtect = PAGE_READONLY; |
| 181 | |
| 182 | end = length + offset; |
| 183 | |
| 184 | if (fd == -1) |
| 185 | mmap_fd = INVALID_HANDLE_VALUE; |
| 186 | else |
| 187 | mmap_fd = (HANDLE)_get_osfhandle(fd); |
| 188 | h = CreateFileMapping(mmap_fd, NULL, flProtect, DWORD_HI(end), DWORD_LO(end), NULL); |
| 189 | if (h == NULL) |
| 190 | return MAP_FAILED; |
| 191 | |
| 192 | if (prot & PROT_WRITE) |
| 193 | dwDesiredAccess = FILE_MAP_WRITE; |
| 194 | else |
| 195 | dwDesiredAccess = FILE_MAP_READ; |
| 196 | if (prot & PROT_EXEC) |
| 197 | dwDesiredAccess |= FILE_MAP_EXECUTE; |
| 198 | if (flags & MAP_PRIVATE) |
| 199 | dwDesiredAccess |= FILE_MAP_COPY; |
| 200 | ret = MapViewOfFile(h, dwDesiredAccess, DWORD_HI(offset), DWORD_LO(offset), length); |
| 201 | if (ret == NULL) { |
| 202 | CloseHandle(h); |
| 203 | ret = MAP_FAILED; |
| 204 | } |
| 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | static void munmap(void *addr, size_t length) |
| 209 | { |
| 210 | UnmapViewOfFile(addr); |
| 211 | /* ruh-ro, we leaked handle from CreateFileMapping() ... */ |
| 212 | } |
| 213 | #elif defined(NO_MMAP) |
| 214 | #define PROT_EXEC 0x04 |
| 215 | #define MAP_FAILED 0 |
| 216 | #define PROT_READ 0 |
| 217 | #define PROT_WRITE 0 |
| 218 | #define MAP_PRIVATE 0 |
| 219 | #define MAP_ANONYMOUS 0 |
| 220 | |
| 221 | void* mmap(void *desired_addr, size_t len, int mmap_prot, int mmap_flags, int fildes, size_t off) |
| 222 | { |
| 223 | return malloc(len); |
| 224 | } |
| 225 | |
| 226 | void munmap(void *base_addr, size_t len) |
| 227 | { |
| 228 | free(base_addr); |
| 229 | } |
| 230 | |
| 231 | int mprotect(void *addr, size_t len, int prot) |
| 232 | { |
| 233 | /* stub - not really needed at this point since this codepath has no dynarecs */ |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | #endif |
| 238 | |
| 239 | #ifndef MAP_ANONYMOUS |
| 240 | #define MAP_ANONYMOUS MAP_ANON |
| 241 | #endif |
| 242 | |
| 243 | #ifdef _3DS |
| 244 | typedef struct |
| 245 | { |
| 246 | unsigned int requested_map; |
| 247 | void* buffer; |
| 248 | }pico_mmap_t; |
| 249 | |
| 250 | pico_mmap_t pico_mmaps[] = { |
| 251 | {0x02000000, 0}, |
| 252 | {0x06000000, 0}, |
| 253 | {NULL, 0} |
| 254 | }; |
| 255 | |
| 256 | void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed) |
| 257 | { |
| 258 | (void)is_fixed; |
| 259 | |
| 260 | if (ctr_svchack_successful) |
| 261 | { |
| 262 | pico_mmap_t* pico_mmap; |
| 263 | |
| 264 | for (pico_mmap = pico_mmaps; pico_mmap->requested_map; pico_mmap++) |
| 265 | { |
| 266 | if ((pico_mmap->requested_map == addr)) |
| 267 | { |
| 268 | unsigned int ptr_aligned, tmp; |
| 269 | unsigned int currentHandle; |
| 270 | unsigned int perm = 0b011; |
| 271 | |
| 272 | if (need_exec) |
| 273 | perm = 0b111; |
| 274 | |
| 275 | size = (size + 0xFFF) & ~0xFFF; |
| 276 | pico_mmap->buffer = malloc(size + 0x1000); |
| 277 | ptr_aligned = (((unsigned int)pico_mmap->buffer) + 0xFFF) & ~0xFFF; |
| 278 | |
| 279 | svcDuplicateHandle(¤tHandle, 0xFFFF8001); |
| 280 | |
| 281 | if(svcControlProcessMemory(currentHandle, pico_mmap->requested_map, ptr_aligned, size, MEMOP_MAP, perm) < 0) |
| 282 | { |
| 283 | if (log_cb) |
| 284 | log_cb(RETRO_LOG_ERROR, "could not map memory @0x%08X\n", pico_mmap->requested_map); |
| 285 | exit(1); |
| 286 | } |
| 287 | |
| 288 | svcCloseHandle(currentHandle); |
| 289 | return (void*)pico_mmap->requested_map; |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | return malloc(size); |
| 295 | } |
| 296 | |
| 297 | void *plat_mremap(void *ptr, size_t oldsize, size_t newsize) |
| 298 | { |
| 299 | if (ctr_svchack_successful) |
| 300 | { |
| 301 | pico_mmap_t* pico_mmap; |
| 302 | |
| 303 | for (pico_mmap = pico_mmaps; pico_mmap->requested_map; pico_mmap++) |
| 304 | { |
| 305 | if ((pico_mmap->requested_map == (unsigned int)ptr)) |
| 306 | { |
| 307 | unsigned int ptr_aligned; |
| 308 | unsigned int currentHandle; |
| 309 | void* tmp; |
| 310 | |
| 311 | oldsize = (oldsize + 0xFFF) & ~0xFFF; |
| 312 | newsize = (newsize + 0xFFF) & ~0xFFF; |
| 313 | ptr_aligned = (((unsigned int)pico_mmap->buffer) + 0xFFF) & ~0xFFF; |
| 314 | |
| 315 | svcDuplicateHandle(¤tHandle, 0xFFFF8001); |
| 316 | |
| 317 | svcControlProcessMemory(currentHandle, pico_mmap->requested_map, ptr_aligned, oldsize, MEMOP_UNMAP, 0b011); |
| 318 | |
| 319 | tmp = realloc(pico_mmap->buffer, newsize + 0x1000); |
| 320 | if(!tmp) |
| 321 | return NULL; |
| 322 | |
| 323 | pico_mmap->buffer = tmp; |
| 324 | ptr_aligned = (((unsigned int)pico_mmap->buffer) + 0xFFF) & ~0xFFF; |
| 325 | |
| 326 | svcControlProcessMemory(currentHandle, pico_mmap->requested_map, ptr_aligned, newsize, MEMOP_MAP, 0x3); |
| 327 | |
| 328 | svcCloseHandle(currentHandle); |
| 329 | |
| 330 | return ptr; |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | return realloc(ptr, newsize); |
| 336 | |
| 337 | } |
| 338 | void plat_munmap(void *ptr, size_t size) |
| 339 | { |
| 340 | if (ctr_svchack_successful) |
| 341 | { |
| 342 | pico_mmap_t* pico_mmap; |
| 343 | |
| 344 | for (pico_mmap = pico_mmaps; pico_mmap->requested_map; pico_mmap++) |
| 345 | { |
| 346 | if ((pico_mmap->requested_map == (unsigned int)ptr)) |
| 347 | { |
| 348 | unsigned int ptr_aligned; |
| 349 | unsigned int currentHandle; |
| 350 | |
| 351 | size = (size + 0xFFF) & ~0xFFF; |
| 352 | ptr_aligned = (((unsigned int)pico_mmap->buffer) + 0xFFF) & ~0xFFF; |
| 353 | |
| 354 | svcDuplicateHandle(¤tHandle, 0xFFFF8001); |
| 355 | |
| 356 | svcControlProcessMemory(currentHandle, (void*)pico_mmap->requested_map, (void*)ptr_aligned, size, MEMOP_UNMAP, 0b011); |
| 357 | |
| 358 | svcCloseHandle(currentHandle); |
| 359 | |
| 360 | free(pico_mmap->buffer); |
| 361 | pico_mmap->buffer = NULL; |
| 362 | return; |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | free(ptr); |
| 368 | } |
| 369 | |
| 370 | #else |
| 371 | void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed) |
| 372 | { |
| 373 | int flags = MAP_PRIVATE | MAP_ANONYMOUS; |
| 374 | void *req, *ret; |
| 375 | |
| 376 | req = (void *)addr; |
| 377 | ret = mmap(req, size, PROT_READ | PROT_WRITE, flags, -1, 0); |
| 378 | if (ret == MAP_FAILED) { |
| 379 | if (log_cb) |
| 380 | log_cb(RETRO_LOG_ERROR, "mmap(%08lx, %zd) failed: %d\n", addr, size, errno); |
| 381 | return NULL; |
| 382 | } |
| 383 | |
| 384 | if (addr != 0 && ret != (void *)addr) { |
| 385 | if (log_cb) |
| 386 | log_cb(RETRO_LOG_WARN, "warning: wanted to map @%08lx, got %p\n", |
| 387 | addr, ret); |
| 388 | |
| 389 | if (is_fixed) { |
| 390 | munmap(ret, size); |
| 391 | return NULL; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | return ret; |
| 396 | } |
| 397 | |
| 398 | void *plat_mremap(void *ptr, size_t oldsize, size_t newsize) |
| 399 | { |
| 400 | #ifdef __linux__ |
| 401 | void *ret = mremap(ptr, oldsize, newsize, 0); |
| 402 | if (ret == MAP_FAILED) |
| 403 | return NULL; |
| 404 | |
| 405 | return ret; |
| 406 | #else |
| 407 | void *tmp, *ret; |
| 408 | size_t preserve_size; |
| 409 | |
| 410 | preserve_size = oldsize; |
| 411 | if (preserve_size > newsize) |
| 412 | preserve_size = newsize; |
| 413 | tmp = malloc(preserve_size); |
| 414 | if (tmp == NULL) |
| 415 | return NULL; |
| 416 | memcpy(tmp, ptr, preserve_size); |
| 417 | |
| 418 | munmap(ptr, oldsize); |
| 419 | ret = mmap(ptr, newsize, PROT_READ | PROT_WRITE, |
| 420 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 421 | if (ret == MAP_FAILED) { |
| 422 | free(tmp); |
| 423 | return NULL; |
| 424 | } |
| 425 | memcpy(ret, tmp, preserve_size); |
| 426 | free(tmp); |
| 427 | return ret; |
| 428 | #endif |
| 429 | } |
| 430 | |
| 431 | void plat_munmap(void *ptr, size_t size) |
| 432 | { |
| 433 | if (ptr != NULL) |
| 434 | munmap(ptr, size); |
| 435 | } |
| 436 | #endif |
| 437 | |
| 438 | int plat_mem_set_exec(void *ptr, size_t size) |
| 439 | { |
| 440 | #ifdef _WIN32 |
| 441 | int ret = VirtualProtect(ptr,size,PAGE_EXECUTE_READWRITE,0); |
| 442 | if (ret == 0 && log_cb) |
| 443 | log_cb(RETRO_LOG_ERROR, "mprotect(%p, %zd) failed: %d\n", ptr, size, 0); |
| 444 | #elif defined(_3DS) |
| 445 | int ret = -1; |
| 446 | if (ctr_svchack_successful) |
| 447 | { |
| 448 | unsigned int currentHandle; |
| 449 | svcDuplicateHandle(¤tHandle, 0xFFFF8001); |
| 450 | ret = svcControlProcessMemory(currentHandle, ptr, 0x0, |
| 451 | size, MEMOP_PROT, 0b111); |
| 452 | svcCloseHandle(currentHandle); |
| 453 | ctr_flush_invalidate_cache(); |
| 454 | |
| 455 | } |
| 456 | else |
| 457 | { |
| 458 | if (log_cb) |
| 459 | log_cb(RETRO_LOG_ERROR, "plat_mem_set_exec called with no svcControlProcessMemory access\n"); |
| 460 | exit(1); |
| 461 | } |
| 462 | |
| 463 | #elif defined(VITA) |
| 464 | int ret = sceKernelOpenVMDomain(); |
| 465 | #else |
| 466 | int ret = mprotect(ptr, size, PROT_READ | PROT_WRITE | PROT_EXEC); |
| 467 | if (ret != 0 && log_cb) |
| 468 | log_cb(RETRO_LOG_ERROR, "mprotect(%p, %zd) failed: %d\n", ptr, size, errno); |
| 469 | #endif |
| 470 | return ret; |
| 471 | } |
| 472 | |
| 473 | void emu_video_mode_change(int start_line, int line_count, int is_32cols) |
| 474 | { |
| 475 | memset(vout_buf, 0, 320 * 240 * 2); |
| 476 | vout_width = is_32cols ? 256 : 320; |
| 477 | PicoDrawSetOutBuf(vout_buf, vout_width * 2); |
| 478 | if (show_overscan == true) line_count += 16; |
| 479 | if (show_overscan == true) start_line -= 8; |
| 480 | |
| 481 | vout_height = line_count; |
| 482 | vout_offset = vout_width * start_line; |
| 483 | |
| 484 | // Update the geometry |
| 485 | struct retro_system_av_info av_info; |
| 486 | retro_get_system_av_info(&av_info); |
| 487 | environ_cb(RETRO_ENVIRONMENT_SET_GEOMETRY, &av_info); |
| 488 | } |
| 489 | |
| 490 | void emu_32x_startup(void) |
| 491 | { |
| 492 | } |
| 493 | |
| 494 | void lprintf(const char *fmt, ...) |
| 495 | { |
| 496 | char buffer[256]; |
| 497 | va_list ap; |
| 498 | va_start(ap, fmt); |
| 499 | vsprintf(buffer, fmt, ap); |
| 500 | /* TODO - add 'level' param for warning/error messages? */ |
| 501 | if (log_cb) |
| 502 | log_cb(RETRO_LOG_INFO, "%s", buffer); |
| 503 | va_end(ap); |
| 504 | } |
| 505 | |
| 506 | /* libretro */ |
| 507 | void retro_set_environment(retro_environment_t cb) |
| 508 | { |
| 509 | static const struct retro_variable vars[] = { |
| 510 | { "picodrive_input1", "Input device 1; 3 button pad|6 button pad|None" }, |
| 511 | { "picodrive_input2", "Input device 2; 3 button pad|6 button pad|None" }, |
| 512 | { "picodrive_sprlim", "No sprite limit; disabled|enabled" }, |
| 513 | { "picodrive_ramcart", "MegaCD RAM cart; disabled|enabled" }, |
| 514 | { "picodrive_region", "Region; Auto|Japan NTSC|Japan PAL|US|Europe" }, |
| 515 | { "picodrive_region_fps", "Region FPS; Auto|NTSC|PAL" }, |
| 516 | { "picodrive_aspect", "Core-provided aspect ratio; PAR|4/3|CRT" }, |
| 517 | { "picodrive_overscan", "Show Overscan; disabled|enabled" }, |
| 518 | #ifdef DRC_SH2 |
| 519 | { "picodrive_drc", "Dynamic recompilers; enabled|disabled" }, |
| 520 | #endif |
| 521 | { NULL, NULL }, |
| 522 | }; |
| 523 | |
| 524 | environ_cb = cb; |
| 525 | |
| 526 | cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void *)vars); |
| 527 | } |
| 528 | |
| 529 | void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; } |
| 530 | void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; } |
| 531 | void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; } |
| 532 | void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; } |
| 533 | void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; } |
| 534 | |
| 535 | unsigned retro_api_version(void) |
| 536 | { |
| 537 | return RETRO_API_VERSION; |
| 538 | } |
| 539 | |
| 540 | void retro_set_controller_port_device(unsigned port, unsigned device) |
| 541 | { |
| 542 | } |
| 543 | |
| 544 | void retro_get_system_info(struct retro_system_info *info) |
| 545 | { |
| 546 | memset(info, 0, sizeof(*info)); |
| 547 | info->library_name = "PicoDrive"; |
| 548 | #ifndef GIT_VERSION |
| 549 | #define GIT_VERSION "" |
| 550 | #endif |
| 551 | info->library_version = VERSION GIT_VERSION; |
| 552 | info->valid_extensions = "bin|gen|smd|md|32x|cue|iso|sms"; |
| 553 | info->need_fullpath = true; |
| 554 | } |
| 555 | |
| 556 | void retro_get_system_av_info(struct retro_system_av_info *info) |
| 557 | { |
| 558 | memset(info, 0, sizeof(*info)); |
| 559 | info->timing.fps = Pico.m.pal ? 50 : 60; |
| 560 | info->timing.sample_rate = 44100; |
| 561 | info->geometry.base_width = vout_width; |
| 562 | info->geometry.base_height = vout_height; |
| 563 | info->geometry.max_width = vout_width; |
| 564 | info->geometry.max_height = vout_height; |
| 565 | |
| 566 | float common_width = vout_width; |
| 567 | if (user_vout_width != 0) |
| 568 | common_width = user_vout_width; |
| 569 | |
| 570 | info->geometry.aspect_ratio = common_width / vout_height; |
| 571 | } |
| 572 | |
| 573 | /* savestates */ |
| 574 | struct savestate_state { |
| 575 | const char *load_buf; |
| 576 | char *save_buf; |
| 577 | size_t size; |
| 578 | size_t pos; |
| 579 | }; |
| 580 | |
| 581 | size_t state_read(void *p, size_t size, size_t nmemb, void *file) |
| 582 | { |
| 583 | struct savestate_state *state = file; |
| 584 | size_t bsize = size * nmemb; |
| 585 | |
| 586 | if (state->pos + bsize > state->size) { |
| 587 | if (log_cb) |
| 588 | log_cb(RETRO_LOG_ERROR, "savestate error: %u/%u\n", |
| 589 | state->pos + bsize, state->size); |
| 590 | bsize = state->size - state->pos; |
| 591 | if ((int)bsize <= 0) |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | memcpy(p, state->load_buf + state->pos, bsize); |
| 596 | state->pos += bsize; |
| 597 | return bsize; |
| 598 | } |
| 599 | |
| 600 | size_t state_write(void *p, size_t size, size_t nmemb, void *file) |
| 601 | { |
| 602 | struct savestate_state *state = file; |
| 603 | size_t bsize = size * nmemb; |
| 604 | |
| 605 | if (state->pos + bsize > state->size) { |
| 606 | if (log_cb) |
| 607 | log_cb(RETRO_LOG_ERROR, "savestate error: %u/%u\n", |
| 608 | state->pos + bsize, state->size); |
| 609 | bsize = state->size - state->pos; |
| 610 | if ((int)bsize <= 0) |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | memcpy(state->save_buf + state->pos, p, bsize); |
| 615 | state->pos += bsize; |
| 616 | return bsize; |
| 617 | } |
| 618 | |
| 619 | size_t state_skip(void *p, size_t size, size_t nmemb, void *file) |
| 620 | { |
| 621 | struct savestate_state *state = file; |
| 622 | size_t bsize = size * nmemb; |
| 623 | |
| 624 | state->pos += bsize; |
| 625 | return bsize; |
| 626 | } |
| 627 | |
| 628 | size_t state_eof(void *file) |
| 629 | { |
| 630 | struct savestate_state *state = file; |
| 631 | |
| 632 | return state->pos >= state->size; |
| 633 | } |
| 634 | |
| 635 | int state_fseek(void *file, long offset, int whence) |
| 636 | { |
| 637 | struct savestate_state *state = file; |
| 638 | |
| 639 | switch (whence) { |
| 640 | case SEEK_SET: |
| 641 | state->pos = offset; |
| 642 | break; |
| 643 | case SEEK_CUR: |
| 644 | state->pos += offset; |
| 645 | break; |
| 646 | case SEEK_END: |
| 647 | state->pos = state->size + offset; |
| 648 | break; |
| 649 | } |
| 650 | return (int)state->pos; |
| 651 | } |
| 652 | |
| 653 | /* savestate sizes vary wildly depending if cd/32x or |
| 654 | * carthw is active, so run the whole thing to get size */ |
| 655 | size_t retro_serialize_size(void) |
| 656 | { |
| 657 | struct savestate_state state = { 0, }; |
| 658 | int ret; |
| 659 | |
| 660 | ret = PicoStateFP(&state, 1, NULL, state_skip, NULL, state_fseek); |
| 661 | if (ret != 0) |
| 662 | return 0; |
| 663 | |
| 664 | return state.pos; |
| 665 | } |
| 666 | |
| 667 | bool retro_serialize(void *data, size_t size) |
| 668 | { |
| 669 | struct savestate_state state = { 0, }; |
| 670 | int ret; |
| 671 | |
| 672 | state.save_buf = data; |
| 673 | state.size = size; |
| 674 | state.pos = 0; |
| 675 | |
| 676 | ret = PicoStateFP(&state, 1, NULL, state_write, |
| 677 | NULL, state_fseek); |
| 678 | return ret == 0; |
| 679 | } |
| 680 | |
| 681 | bool retro_unserialize(const void *data, size_t size) |
| 682 | { |
| 683 | struct savestate_state state = { 0, }; |
| 684 | int ret; |
| 685 | |
| 686 | state.load_buf = data; |
| 687 | state.size = size; |
| 688 | state.pos = 0; |
| 689 | |
| 690 | ret = PicoStateFP(&state, 0, state_read, NULL, |
| 691 | state_eof, state_fseek); |
| 692 | return ret == 0; |
| 693 | } |
| 694 | |
| 695 | typedef struct patch |
| 696 | { |
| 697 | unsigned int addr; |
| 698 | unsigned short data; |
| 699 | unsigned char comp; |
| 700 | } patch; |
| 701 | |
| 702 | extern void decode(char *buff, patch *dest); |
| 703 | extern uint16_t m68k_read16(uint32_t a); |
| 704 | extern void m68k_write16(uint32_t a, uint16_t d); |
| 705 | |
| 706 | void retro_cheat_reset(void) |
| 707 | { |
| 708 | int i=0; |
| 709 | unsigned int addr; |
| 710 | |
| 711 | for (i = 0; i < PicoPatchCount; i++) |
| 712 | { |
| 713 | addr = PicoPatches[i].addr; |
| 714 | if (addr < Pico.romsize) { |
| 715 | if (PicoPatches[i].active) |
| 716 | *(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data_old; |
| 717 | } else { |
| 718 | if (PicoPatches[i].active) |
| 719 | m68k_write16(PicoPatches[i].addr,PicoPatches[i].data_old); |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | PicoPatchUnload(); |
| 724 | } |
| 725 | |
| 726 | void retro_cheat_set(unsigned index, bool enabled, const char *code) |
| 727 | { |
| 728 | patch pt; |
| 729 | int array_len = PicoPatchCount; |
| 730 | char codeCopy[256]; |
| 731 | char *buff; |
| 732 | |
| 733 | if (code=='\0') return; |
| 734 | strcpy(codeCopy,code); |
| 735 | buff = strtok(codeCopy,"+"); |
| 736 | |
| 737 | while (buff != NULL) |
| 738 | { |
| 739 | decode(buff, &pt); |
| 740 | if (pt.addr == (uint32_t) -1 || pt.data == (uint16_t) -1) |
| 741 | { |
| 742 | log_cb(RETRO_LOG_ERROR,"CHEATS: Invalid code: %s\n",buff); |
| 743 | return; |
| 744 | } |
| 745 | |
| 746 | /* code was good, add it */ |
| 747 | if (array_len < PicoPatchCount + 1) |
| 748 | { |
| 749 | void *ptr; |
| 750 | array_len *= 2; |
| 751 | array_len++; |
| 752 | ptr = realloc(PicoPatches, array_len * sizeof(PicoPatches[0])); |
| 753 | if (ptr == NULL) { |
| 754 | log_cb(RETRO_LOG_ERROR,"CHEATS: Failed to allocate memory for: %s\n",buff); |
| 755 | return; |
| 756 | } |
| 757 | PicoPatches = ptr; |
| 758 | } |
| 759 | strcpy(PicoPatches[PicoPatchCount].code, buff); |
| 760 | |
| 761 | PicoPatches[PicoPatchCount].active = enabled; |
| 762 | PicoPatches[PicoPatchCount].addr = pt.addr; |
| 763 | PicoPatches[PicoPatchCount].data = pt.data; |
| 764 | PicoPatches[PicoPatchCount].comp = pt.comp; |
| 765 | if (PicoPatches[PicoPatchCount].addr < Pico.romsize) |
| 766 | PicoPatches[PicoPatchCount].data_old = *(uint16_t *)(Pico.rom + PicoPatches[PicoPatchCount].addr); |
| 767 | else |
| 768 | PicoPatches[PicoPatchCount].data_old = (uint16_t) m68k_read16(PicoPatches[PicoPatchCount].addr); |
| 769 | PicoPatchCount++; |
| 770 | |
| 771 | buff = strtok(NULL,"+"); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | /* multidisk support */ |
| 776 | static bool disk_ejected; |
| 777 | static unsigned int disk_current_index; |
| 778 | static unsigned int disk_count; |
| 779 | static struct disks_state { |
| 780 | char *fname; |
| 781 | } disks[8]; |
| 782 | |
| 783 | static bool disk_set_eject_state(bool ejected) |
| 784 | { |
| 785 | // TODO? |
| 786 | disk_ejected = ejected; |
| 787 | return true; |
| 788 | } |
| 789 | |
| 790 | static bool disk_get_eject_state(void) |
| 791 | { |
| 792 | return disk_ejected; |
| 793 | } |
| 794 | |
| 795 | static unsigned int disk_get_image_index(void) |
| 796 | { |
| 797 | return disk_current_index; |
| 798 | } |
| 799 | |
| 800 | static bool disk_set_image_index(unsigned int index) |
| 801 | { |
| 802 | enum cd_img_type cd_type; |
| 803 | int ret; |
| 804 | |
| 805 | if (index >= sizeof(disks) / sizeof(disks[0])) |
| 806 | return false; |
| 807 | |
| 808 | if (disks[index].fname == NULL) { |
| 809 | if (log_cb) |
| 810 | log_cb(RETRO_LOG_ERROR, "missing disk #%u\n", index); |
| 811 | |
| 812 | // RetroArch specifies "no disk" with index == count, |
| 813 | // so don't fail here.. |
| 814 | disk_current_index = index; |
| 815 | return true; |
| 816 | } |
| 817 | |
| 818 | if (log_cb) |
| 819 | log_cb(RETRO_LOG_INFO, "switching to disk %u: \"%s\"\n", index, |
| 820 | disks[index].fname); |
| 821 | |
| 822 | ret = -1; |
| 823 | cd_type = PicoCdCheck(disks[index].fname, NULL); |
| 824 | if (cd_type != CIT_NOT_CD) |
| 825 | ret = cdd_load(disks[index].fname, cd_type); |
| 826 | if (ret != 0) { |
| 827 | if (log_cb) |
| 828 | log_cb(RETRO_LOG_ERROR, "Load failed, invalid CD image?\n"); |
| 829 | return 0; |
| 830 | } |
| 831 | |
| 832 | disk_current_index = index; |
| 833 | return true; |
| 834 | } |
| 835 | |
| 836 | static unsigned int disk_get_num_images(void) |
| 837 | { |
| 838 | return disk_count; |
| 839 | } |
| 840 | |
| 841 | static bool disk_replace_image_index(unsigned index, |
| 842 | const struct retro_game_info *info) |
| 843 | { |
| 844 | bool ret = true; |
| 845 | |
| 846 | if (index >= sizeof(disks) / sizeof(disks[0])) |
| 847 | return false; |
| 848 | |
| 849 | if (disks[index].fname != NULL) |
| 850 | free(disks[index].fname); |
| 851 | disks[index].fname = NULL; |
| 852 | |
| 853 | if (info != NULL) { |
| 854 | disks[index].fname = strdup(info->path); |
| 855 | if (index == disk_current_index) |
| 856 | ret = disk_set_image_index(index); |
| 857 | } |
| 858 | |
| 859 | return ret; |
| 860 | } |
| 861 | |
| 862 | static bool disk_add_image_index(void) |
| 863 | { |
| 864 | if (disk_count >= sizeof(disks) / sizeof(disks[0])) |
| 865 | return false; |
| 866 | |
| 867 | disk_count++; |
| 868 | return true; |
| 869 | } |
| 870 | |
| 871 | static struct retro_disk_control_callback disk_control = { |
| 872 | disk_set_eject_state, |
| 873 | disk_get_eject_state, |
| 874 | disk_get_image_index, |
| 875 | disk_set_image_index, |
| 876 | disk_get_num_images, |
| 877 | disk_replace_image_index, |
| 878 | disk_add_image_index, |
| 879 | }; |
| 880 | |
| 881 | static void disk_tray_open(void) |
| 882 | { |
| 883 | if (log_cb) |
| 884 | log_cb(RETRO_LOG_INFO, "cd tray open\n"); |
| 885 | disk_ejected = 1; |
| 886 | } |
| 887 | |
| 888 | static void disk_tray_close(void) |
| 889 | { |
| 890 | if (log_cb) |
| 891 | log_cb(RETRO_LOG_INFO, "cd tray close\n"); |
| 892 | disk_ejected = 0; |
| 893 | } |
| 894 | |
| 895 | |
| 896 | static const char * const biosfiles_us[] = { |
| 897 | "us_scd2_9306", "SegaCDBIOS9303", "us_scd1_9210", "bios_CD_U" |
| 898 | }; |
| 899 | static const char * const biosfiles_eu[] = { |
| 900 | "eu_mcd2_9306", "eu_mcd2_9303", "eu_mcd1_9210", "bios_CD_E" |
| 901 | }; |
| 902 | static const char * const biosfiles_jp[] = { |
| 903 | "jp_mcd2_921222", "jp_mcd1_9112", "jp_mcd1_9111", "bios_CD_J" |
| 904 | }; |
| 905 | |
| 906 | static void make_system_path(char *buf, size_t buf_size, |
| 907 | const char *name, const char *ext) |
| 908 | { |
| 909 | const char *dir = NULL; |
| 910 | |
| 911 | if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir) { |
| 912 | snprintf(buf, buf_size, "%s%c%s%s", dir, SLASH, name, ext); |
| 913 | } |
| 914 | else { |
| 915 | snprintf(buf, buf_size, "%s%s", name, ext); |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | static const char *find_bios(int *region, const char *cd_fname) |
| 920 | { |
| 921 | const char * const *files; |
| 922 | static char path[256]; |
| 923 | int i, count; |
| 924 | FILE *f = NULL; |
| 925 | |
| 926 | if (*region == 4) { // US |
| 927 | files = biosfiles_us; |
| 928 | count = sizeof(biosfiles_us) / sizeof(char *); |
| 929 | } else if (*region == 8) { // EU |
| 930 | files = biosfiles_eu; |
| 931 | count = sizeof(biosfiles_eu) / sizeof(char *); |
| 932 | } else if (*region == 1 || *region == 2) { |
| 933 | files = biosfiles_jp; |
| 934 | count = sizeof(biosfiles_jp) / sizeof(char *); |
| 935 | } else { |
| 936 | return NULL; |
| 937 | } |
| 938 | |
| 939 | for (i = 0; i < count; i++) |
| 940 | { |
| 941 | make_system_path(path, sizeof(path), files[i], ".bin"); |
| 942 | f = fopen(path, "rb"); |
| 943 | if (f != NULL) |
| 944 | break; |
| 945 | |
| 946 | make_system_path(path, sizeof(path), files[i], ".zip"); |
| 947 | f = fopen(path, "rb"); |
| 948 | if (f != NULL) |
| 949 | break; |
| 950 | } |
| 951 | |
| 952 | if (f != NULL) { |
| 953 | if (log_cb) |
| 954 | log_cb(RETRO_LOG_INFO, "using bios: %s\n", path); |
| 955 | fclose(f); |
| 956 | return path; |
| 957 | } |
| 958 | |
| 959 | return NULL; |
| 960 | } |
| 961 | |
| 962 | static void sram_reset() |
| 963 | { |
| 964 | SRam.data = NULL; |
| 965 | SRam.start = 0; |
| 966 | SRam.end = 0; |
| 967 | SRam.flags = '\0'; |
| 968 | SRam.unused2 = '\0'; |
| 969 | SRam.changed = '\0' ; |
| 970 | SRam.eeprom_type = '\0'; |
| 971 | SRam.unused3 = '\0'; |
| 972 | SRam.eeprom_bit_cl = '\0'; |
| 973 | SRam.eeprom_bit_in = '\0'; |
| 974 | SRam.eeprom_bit_out = '\0'; |
| 975 | SRam.size = 0; |
| 976 | } |
| 977 | |
| 978 | bool retro_load_game(const struct retro_game_info *info) |
| 979 | { |
| 980 | enum media_type_e media_type; |
| 981 | static char carthw_path[256]; |
| 982 | size_t i; |
| 983 | |
| 984 | struct retro_input_descriptor desc[] = { |
| 985 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" }, |
| 986 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" }, |
| 987 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" }, |
| 988 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" }, |
| 989 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" }, |
| 990 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "C" }, |
| 991 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Y" }, |
| 992 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "A" }, |
| 993 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "X" }, |
| 994 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "Z" }, |
| 995 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT,"Mode" }, |
| 996 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" }, |
| 997 | |
| 998 | { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" }, |
| 999 | { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" }, |
| 1000 | { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" }, |
| 1001 | { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" }, |
| 1002 | { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" }, |
| 1003 | { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "C" }, |
| 1004 | { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Y" }, |
| 1005 | { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "A" }, |
| 1006 | { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "X" }, |
| 1007 | { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "Z" }, |
| 1008 | { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT,"Mode" }, |
| 1009 | { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" }, |
| 1010 | |
| 1011 | { 0 }, |
| 1012 | }; |
| 1013 | |
| 1014 | struct retro_input_descriptor desc_sms[] = { |
| 1015 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" }, |
| 1016 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" }, |
| 1017 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" }, |
| 1018 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" }, |
| 1019 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Button 1 Start" }, |
| 1020 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Button 2" }, |
| 1021 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Button Pause" }, |
| 1022 | |
| 1023 | { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" }, |
| 1024 | { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" }, |
| 1025 | { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" }, |
| 1026 | { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" }, |
| 1027 | { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Button 1 Start" }, |
| 1028 | { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Button 2" }, |
| 1029 | { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Button Pause" }, |
| 1030 | |
| 1031 | { 0 }, |
| 1032 | }; |
| 1033 | |
| 1034 | sram_reset(); |
| 1035 | |
| 1036 | enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565; |
| 1037 | if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) { |
| 1038 | if (log_cb) |
| 1039 | log_cb(RETRO_LOG_ERROR, "RGB565 support required, sorry\n"); |
| 1040 | return false; |
| 1041 | } |
| 1042 | |
| 1043 | if (info == NULL || info->path == NULL) { |
| 1044 | if (log_cb) |
| 1045 | log_cb(RETRO_LOG_ERROR, "info->path required\n"); |
| 1046 | return false; |
| 1047 | } |
| 1048 | |
| 1049 | for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++) { |
| 1050 | if (disks[i].fname != NULL) { |
| 1051 | free(disks[i].fname); |
| 1052 | disks[i].fname = NULL; |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | disk_current_index = 0; |
| 1057 | disk_count = 1; |
| 1058 | disks[0].fname = strdup(info->path); |
| 1059 | |
| 1060 | make_system_path(carthw_path, sizeof(carthw_path), "carthw", ".cfg"); |
| 1061 | |
| 1062 | media_type = PicoLoadMedia(info->path, carthw_path, |
| 1063 | find_bios, NULL); |
| 1064 | |
| 1065 | switch (media_type) { |
| 1066 | case PM_BAD_DETECT: |
| 1067 | if (log_cb) |
| 1068 | log_cb(RETRO_LOG_ERROR, "Failed to detect ROM/CD image type.\n"); |
| 1069 | return false; |
| 1070 | case PM_BAD_CD: |
| 1071 | if (log_cb) |
| 1072 | log_cb(RETRO_LOG_ERROR, "Invalid CD image\n"); |
| 1073 | return false; |
| 1074 | case PM_BAD_CD_NO_BIOS: |
| 1075 | if (log_cb) |
| 1076 | log_cb(RETRO_LOG_ERROR, "Missing BIOS\n"); |
| 1077 | return false; |
| 1078 | case PM_ERROR: |
| 1079 | if (log_cb) |
| 1080 | log_cb(RETRO_LOG_ERROR, "Load error\n"); |
| 1081 | return false; |
| 1082 | default: |
| 1083 | break; |
| 1084 | } |
| 1085 | |
| 1086 | if (media_type == PM_MARK3) |
| 1087 | environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc_sms); |
| 1088 | else |
| 1089 | environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc); |
| 1090 | |
| 1091 | PicoLoopPrepare(); |
| 1092 | |
| 1093 | PicoWriteSound = snd_write; |
| 1094 | memset(sndBuffer, 0, sizeof(sndBuffer)); |
| 1095 | PsndOut = sndBuffer; |
| 1096 | PsndRerate(0); |
| 1097 | |
| 1098 | return true; |
| 1099 | } |
| 1100 | |
| 1101 | bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info) |
| 1102 | { |
| 1103 | return false; |
| 1104 | } |
| 1105 | |
| 1106 | void retro_unload_game(void) |
| 1107 | { |
| 1108 | } |
| 1109 | |
| 1110 | unsigned retro_get_region(void) |
| 1111 | { |
| 1112 | return Pico.m.pal ? RETRO_REGION_PAL : RETRO_REGION_NTSC; |
| 1113 | } |
| 1114 | |
| 1115 | void *retro_get_memory_data(unsigned type) |
| 1116 | { |
| 1117 | uint8_t* data; |
| 1118 | |
| 1119 | switch(type) |
| 1120 | { |
| 1121 | case RETRO_MEMORY_SAVE_RAM: |
| 1122 | if (PicoAHW & PAHW_MCD) |
| 1123 | data = Pico_mcd->bram; |
| 1124 | else |
| 1125 | data = SRam.data; |
| 1126 | break; |
| 1127 | case RETRO_MEMORY_SYSTEM_RAM: |
| 1128 | if (PicoAHW & PAHW_SMS) |
| 1129 | data = Pico.zram; |
| 1130 | else |
| 1131 | data = Pico.ram; |
| 1132 | break; |
| 1133 | default: |
| 1134 | data = NULL; |
| 1135 | break; |
| 1136 | } |
| 1137 | |
| 1138 | return data; |
| 1139 | } |
| 1140 | |
| 1141 | size_t retro_get_memory_size(unsigned type) |
| 1142 | { |
| 1143 | unsigned int i; |
| 1144 | int sum; |
| 1145 | |
| 1146 | switch(type) |
| 1147 | { |
| 1148 | case RETRO_MEMORY_SAVE_RAM: |
| 1149 | if (PicoAHW & PAHW_MCD) |
| 1150 | // bram |
| 1151 | return 0x2000; |
| 1152 | |
| 1153 | if (Pico.m.frame_count == 0) |
| 1154 | return SRam.size; |
| 1155 | |
| 1156 | // if game doesn't write to sram, don't report it to |
| 1157 | // libretro so that RA doesn't write out zeroed .srm |
| 1158 | for (i = 0, sum = 0; i < SRam.size; i++) |
| 1159 | sum |= SRam.data[i]; |
| 1160 | |
| 1161 | return (sum != 0) ? SRam.size : 0; |
| 1162 | |
| 1163 | case RETRO_MEMORY_SYSTEM_RAM: |
| 1164 | if (PicoAHW & PAHW_SMS) |
| 1165 | return 0x2000; |
| 1166 | else |
| 1167 | return sizeof(Pico.ram); |
| 1168 | |
| 1169 | default: |
| 1170 | return 0; |
| 1171 | } |
| 1172 | |
| 1173 | } |
| 1174 | |
| 1175 | void retro_reset(void) |
| 1176 | { |
| 1177 | PicoReset(); |
| 1178 | } |
| 1179 | |
| 1180 | static const unsigned short retro_pico_map[] = { |
| 1181 | 1 << GBTN_B, |
| 1182 | 1 << GBTN_A, |
| 1183 | 1 << GBTN_MODE, |
| 1184 | 1 << GBTN_START, |
| 1185 | 1 << GBTN_UP, |
| 1186 | 1 << GBTN_DOWN, |
| 1187 | 1 << GBTN_LEFT, |
| 1188 | 1 << GBTN_RIGHT, |
| 1189 | 1 << GBTN_C, |
| 1190 | 1 << GBTN_Y, |
| 1191 | 1 << GBTN_X, |
| 1192 | 1 << GBTN_Z, |
| 1193 | }; |
| 1194 | #define RETRO_PICO_MAP_LEN (sizeof(retro_pico_map) / sizeof(retro_pico_map[0])) |
| 1195 | |
| 1196 | static void snd_write(int len) |
| 1197 | { |
| 1198 | audio_batch_cb(PsndOut, len / 4); |
| 1199 | } |
| 1200 | |
| 1201 | static enum input_device input_name_to_val(const char *name) |
| 1202 | { |
| 1203 | if (strcmp(name, "3 button pad") == 0) |
| 1204 | return PICO_INPUT_PAD_3BTN; |
| 1205 | if (strcmp(name, "6 button pad") == 0) |
| 1206 | return PICO_INPUT_PAD_6BTN; |
| 1207 | if (strcmp(name, "None") == 0) |
| 1208 | return PICO_INPUT_NOTHING; |
| 1209 | |
| 1210 | if (log_cb) |
| 1211 | log_cb(RETRO_LOG_WARN, "invalid picodrive_input: '%s'\n", name); |
| 1212 | return PICO_INPUT_PAD_3BTN; |
| 1213 | } |
| 1214 | |
| 1215 | static void update_variables(void) |
| 1216 | { |
| 1217 | struct retro_variable var; |
| 1218 | |
| 1219 | var.value = NULL; |
| 1220 | var.key = "picodrive_input1"; |
| 1221 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) |
| 1222 | PicoSetInputDevice(0, input_name_to_val(var.value)); |
| 1223 | |
| 1224 | var.value = NULL; |
| 1225 | var.key = "picodrive_input2"; |
| 1226 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) |
| 1227 | PicoSetInputDevice(1, input_name_to_val(var.value)); |
| 1228 | |
| 1229 | var.value = NULL; |
| 1230 | var.key = "picodrive_sprlim"; |
| 1231 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { |
| 1232 | if (strcmp(var.value, "enabled") == 0) |
| 1233 | PicoOpt |= POPT_DIS_SPRITE_LIM; |
| 1234 | else |
| 1235 | PicoOpt &= ~POPT_DIS_SPRITE_LIM; |
| 1236 | } |
| 1237 | |
| 1238 | var.value = NULL; |
| 1239 | var.key = "picodrive_ramcart"; |
| 1240 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { |
| 1241 | if (strcmp(var.value, "enabled") == 0) |
| 1242 | PicoOpt |= POPT_EN_MCD_RAMCART; |
| 1243 | else |
| 1244 | PicoOpt &= ~POPT_EN_MCD_RAMCART; |
| 1245 | } |
| 1246 | |
| 1247 | int OldPicoRegionOverride = PicoRegionOverride; |
| 1248 | var.value = NULL; |
| 1249 | var.key = "picodrive_region"; |
| 1250 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { |
| 1251 | if (strcmp(var.value, "Auto") == 0) |
| 1252 | PicoRegionOverride = 0; |
| 1253 | else if (strcmp(var.value, "Japan NTSC") == 0) |
| 1254 | PicoRegionOverride = 1; |
| 1255 | else if (strcmp(var.value, "Japan PAL") == 0) |
| 1256 | PicoRegionOverride = 2; |
| 1257 | else if (strcmp(var.value, "US") == 0) |
| 1258 | PicoRegionOverride = 4; |
| 1259 | else if (strcmp(var.value, "Europe") == 0) |
| 1260 | PicoRegionOverride = 8; |
| 1261 | } |
| 1262 | |
| 1263 | int OldPicoRegionFPSOverride = PicoRegionFPSOverride; |
| 1264 | var.value = NULL; |
| 1265 | var.key = "picodrive_region_fps"; |
| 1266 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { |
| 1267 | if (strcmp(var.value, "Auto") == 0) |
| 1268 | PicoRegionFPSOverride = 0; |
| 1269 | else if (strcmp(var.value, "NTSC") == 0) |
| 1270 | PicoRegionFPSOverride = 1; |
| 1271 | else if (strcmp(var.value, "PAL") == 0) |
| 1272 | PicoRegionFPSOverride = 2; |
| 1273 | } |
| 1274 | |
| 1275 | // Update region, fps and sound flags if needed |
| 1276 | if (PicoRegionOverride != OldPicoRegionOverride || |
| 1277 | PicoRegionFPSOverride != OldPicoRegionFPSOverride) |
| 1278 | { |
| 1279 | PicoDetectRegion(); |
| 1280 | PicoLoopPrepare(); |
| 1281 | PsndRerate(1); |
| 1282 | } |
| 1283 | |
| 1284 | float old_user_vout_width = user_vout_width; |
| 1285 | var.value = NULL; |
| 1286 | var.key = "picodrive_aspect"; |
| 1287 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { |
| 1288 | if (strcmp(var.value, "4/3") == 0) |
| 1289 | user_vout_width = VOUT_4_3; |
| 1290 | else if (strcmp(var.value, "CRT") == 0) |
| 1291 | user_vout_width = VOUT_CRT; |
| 1292 | else |
| 1293 | user_vout_width = VOUT_PAR; |
| 1294 | } |
| 1295 | |
| 1296 | var.value = NULL; |
| 1297 | var.key = "picodrive_overscan"; |
| 1298 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { |
| 1299 | if (strcmp(var.value, "enabled") == 0) |
| 1300 | show_overscan = true; |
| 1301 | else |
| 1302 | show_overscan = false; |
| 1303 | } |
| 1304 | |
| 1305 | if (user_vout_width != old_user_vout_width) |
| 1306 | { |
| 1307 | // Update the geometry |
| 1308 | struct retro_system_av_info av_info; |
| 1309 | retro_get_system_av_info(&av_info); |
| 1310 | environ_cb(RETRO_ENVIRONMENT_SET_GEOMETRY, &av_info); |
| 1311 | } |
| 1312 | |
| 1313 | #ifdef DRC_SH2 |
| 1314 | var.value = NULL; |
| 1315 | var.key = "picodrive_drc"; |
| 1316 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { |
| 1317 | if (strcmp(var.value, "enabled") == 0) |
| 1318 | PicoOpt |= POPT_EN_DRC; |
| 1319 | else |
| 1320 | PicoOpt &= ~POPT_EN_DRC; |
| 1321 | } |
| 1322 | #endif |
| 1323 | #ifdef _3DS |
| 1324 | if(!ctr_svchack_successful) |
| 1325 | PicoOpt &= ~POPT_EN_DRC; |
| 1326 | #endif |
| 1327 | } |
| 1328 | |
| 1329 | void retro_run(void) |
| 1330 | { |
| 1331 | bool updated = false; |
| 1332 | int pad, i; |
| 1333 | |
| 1334 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated) |
| 1335 | update_variables(); |
| 1336 | |
| 1337 | input_poll_cb(); |
| 1338 | |
| 1339 | PicoPad[0] = PicoPad[1] = 0; |
| 1340 | for (pad = 0; pad < 2; pad++) |
| 1341 | for (i = 0; i < RETRO_PICO_MAP_LEN; i++) |
| 1342 | if (input_state_cb(pad, RETRO_DEVICE_JOYPAD, 0, i)) |
| 1343 | PicoPad[pad] |= retro_pico_map[i]; |
| 1344 | |
| 1345 | PicoPatchApply(); |
| 1346 | PicoFrame(); |
| 1347 | |
| 1348 | video_cb((short *)vout_buf + vout_offset, |
| 1349 | vout_width, vout_height, vout_width * 2); |
| 1350 | } |
| 1351 | |
| 1352 | static void check_system_specs(void) |
| 1353 | { |
| 1354 | /* TODO - set different performance level for 32X - 6 for ARM dynarec, higher for interpreter core */ |
| 1355 | unsigned level = 5; |
| 1356 | environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level); |
| 1357 | } |
| 1358 | |
| 1359 | void retro_init(void) |
| 1360 | { |
| 1361 | struct retro_log_callback log; |
| 1362 | int level; |
| 1363 | |
| 1364 | level = 0; |
| 1365 | environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level); |
| 1366 | |
| 1367 | if (environ_cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &log)) |
| 1368 | log_cb = log.log; |
| 1369 | else |
| 1370 | log_cb = NULL; |
| 1371 | |
| 1372 | environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control); |
| 1373 | |
| 1374 | #ifdef _3DS |
| 1375 | ctr_svchack_successful = ctr_svchack_init(); |
| 1376 | #elif defined(VITA) |
| 1377 | sceBlock = getVMBlock(); |
| 1378 | #endif |
| 1379 | |
| 1380 | PicoOpt = POPT_EN_STEREO|POPT_EN_FM|POPT_EN_PSG|POPT_EN_Z80 |
| 1381 | | POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_MCD_GFX |
| 1382 | | POPT_EN_32X|POPT_EN_PWM |
| 1383 | | POPT_ACC_SPRITES|POPT_DIS_32C_BORDER; |
| 1384 | #ifdef __arm__ |
| 1385 | #ifdef _3DS |
| 1386 | if (ctr_svchack_successful) |
| 1387 | #endif |
| 1388 | PicoOpt |= POPT_EN_DRC; |
| 1389 | #endif |
| 1390 | PsndRate = 44100; |
| 1391 | PicoAutoRgnOrder = 0x184; // US, EU, JP |
| 1392 | |
| 1393 | vout_width = 320; |
| 1394 | vout_height = 240; |
| 1395 | #ifdef _3DS |
| 1396 | vout_buf = linearMemAlign(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2, 0x80); |
| 1397 | #else |
| 1398 | vout_buf = malloc(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2); |
| 1399 | #endif |
| 1400 | |
| 1401 | PicoInit(); |
| 1402 | PicoDrawSetOutFormat(PDF_RGB555, 0); |
| 1403 | PicoDrawSetOutBuf(vout_buf, vout_width * 2); |
| 1404 | |
| 1405 | //PicoMessage = plat_status_msg_busy_next; |
| 1406 | PicoMCDopenTray = disk_tray_open; |
| 1407 | PicoMCDcloseTray = disk_tray_close; |
| 1408 | |
| 1409 | update_variables(); |
| 1410 | } |
| 1411 | |
| 1412 | void retro_deinit(void) |
| 1413 | { |
| 1414 | #ifdef _3DS |
| 1415 | linearFree(vout_buf); |
| 1416 | #else |
| 1417 | free(vout_buf); |
| 1418 | #endif |
| 1419 | vout_buf = NULL; |
| 1420 | PicoExit(); |
| 1421 | } |