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