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