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