libretro.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  1. /*
  2. * libretro core glue for PicoDrive
  3. * (C) notaz, 2013
  4. * (C) aliaspider, 2016
  5. * (C) Daniel De Matteis, 2013
  6. * (C) kub, 2020
  7. *
  8. * This work is licensed under the terms of MAME license.
  9. * See COPYING file in the top-level directory.
  10. */
  11. #define _GNU_SOURCE 1 // mremap
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <stdarg.h>
  15. #include <string.h>
  16. #ifndef _WIN32
  17. #ifndef NO_MMAP
  18. #ifdef __SWITCH__
  19. #include "switch/mman.h"
  20. #else
  21. #include <sys/mman.h>
  22. #endif
  23. #endif
  24. #else
  25. #include <io.h>
  26. #include <windows.h>
  27. #include <sys/types.h>
  28. #endif
  29. #include <errno.h>
  30. #ifdef __MACH__
  31. #include <libkern/OSCacheControl.h>
  32. #endif
  33. #ifdef USE_LIBRETRO_VFS
  34. #include "file_stream_transforms.h"
  35. #endif
  36. #if defined(RENDER_GSKIT_PS2)
  37. #include <malloc.h>
  38. #include "libretro-common/include/libretro_gskit_ps2.h"
  39. #include "ps2/asm.h"
  40. #endif
  41. #ifdef _3DS
  42. #include "3ds/3ds_utils.h"
  43. #define MEMOP_MAP 4
  44. #define MEMOP_UNMAP 5
  45. #define MEMOP_PROT 6
  46. int svcDuplicateHandle(unsigned int* out, unsigned int original);
  47. int svcCloseHandle(unsigned int handle);
  48. int svcControlProcessMemory(unsigned int process, void* addr0, void* addr1,
  49. unsigned int size, unsigned int type, unsigned int perm);
  50. void* linearMemAlign(size_t size, size_t alignment);
  51. void linearFree(void* mem);
  52. static int ctr_svchack_successful = 0;
  53. #elif defined(VITA)
  54. #define TARGET_SIZE_2 24 // 2^24 = 16 megabytes
  55. #include <psp2/kernel/sysmem.h>
  56. static int sceBlock;
  57. int getVMBlock();
  58. int _newlib_vm_size_user = 1 << TARGET_SIZE_2;
  59. #endif
  60. #include "libretro_core_options.h"
  61. #include <pico/pico_int.h>
  62. #include <pico/state.h>
  63. #include <pico/patch.h>
  64. #include <pico/sound/mix.h>
  65. #include "../common/input_pico.h"
  66. #include "../common/version.h"
  67. #include <libretro.h>
  68. static retro_log_printf_t log_cb;
  69. static retro_video_refresh_t video_cb;
  70. static retro_input_poll_t input_poll_cb;
  71. static retro_input_state_t input_state_cb;
  72. static retro_environment_t environ_cb;
  73. static retro_audio_sample_batch_t audio_batch_cb;
  74. #define VOUT_MAX_WIDTH 320
  75. #define VOUT_MAX_HEIGHT 240
  76. #define INITIAL_SND_RATE 44100
  77. static const float VOUT_PAR = 0.0;
  78. static const float VOUT_4_3 = (224.0f * (4.0f / 3.0f));
  79. static const float VOUT_CRT = (224.0f * 1.29911f);
  80. static bool show_overscan = false;
  81. static bool old_show_overscan = false;
  82. /* Required to allow on the fly changes to 'show overscan' */
  83. static int vm_current_start_line = -1;
  84. static int vm_current_line_count = -1;
  85. static int vm_current_start_col = -1;
  86. static int vm_current_col_count = -1;
  87. static int vout_16bit = 1;
  88. static int vout_format = PDF_RGB555;
  89. static void *vout_buf;
  90. static int vout_width, vout_height, vout_offset;
  91. static float user_vout_width = 0.0;
  92. #if defined(RENDER_GSKIT_PS2)
  93. #define VOUT_8BIT_WIDTH 328
  94. #define VOUT_8BIT_HEIGHT 256
  95. RETRO_HW_RENDER_INTEFACE_GSKIT_PS2 *ps2 = NULL;
  96. static void *retro_palette;
  97. static struct retro_hw_ps2_insets padding;
  98. #endif
  99. static short ALIGNED(4) sndBuffer[2*INITIAL_SND_RATE/50];
  100. static void snd_write(int len);
  101. char **g_argv;
  102. #ifdef _WIN32
  103. #define SLASH '\\'
  104. #else
  105. #define SLASH '/'
  106. #endif
  107. /* Frameskipping Support */
  108. static unsigned frameskip_type = 0;
  109. static unsigned frameskip_threshold = 0;
  110. static uint16_t frameskip_counter = 0;
  111. static bool retro_audio_buff_active = false;
  112. static unsigned retro_audio_buff_occupancy = 0;
  113. static bool retro_audio_buff_underrun = false;
  114. /* Maximum number of consecutive frames that
  115. * can be skipped */
  116. #define FRAMESKIP_MAX 60
  117. static unsigned audio_latency = 0;
  118. static bool update_audio_latency = false;
  119. static void retro_audio_buff_status_cb(
  120. bool active, unsigned occupancy, bool underrun_likely)
  121. {
  122. retro_audio_buff_active = active;
  123. retro_audio_buff_occupancy = occupancy;
  124. retro_audio_buff_underrun = underrun_likely;
  125. }
  126. static void init_frameskip(void)
  127. {
  128. if (frameskip_type > 0)
  129. {
  130. struct retro_audio_buffer_status_callback buf_status_cb;
  131. buf_status_cb.callback = retro_audio_buff_status_cb;
  132. if (!environ_cb(RETRO_ENVIRONMENT_SET_AUDIO_BUFFER_STATUS_CALLBACK,
  133. &buf_status_cb))
  134. {
  135. if (log_cb)
  136. log_cb(RETRO_LOG_WARN, "Frameskip disabled - frontend does not support audio buffer status monitoring.\n");
  137. retro_audio_buff_active = false;
  138. retro_audio_buff_occupancy = 0;
  139. retro_audio_buff_underrun = false;
  140. audio_latency = 0;
  141. }
  142. else
  143. {
  144. /* Frameskip is enabled - increase frontend
  145. * audio latency to minimise potential
  146. * buffer underruns */
  147. float frame_time_msec = 1000.0f / (Pico.m.pal ? 50.0f : 60.0f);
  148. /* Set latency to 6x current frame time... */
  149. audio_latency = (unsigned)((6.0f * frame_time_msec) + 0.5f);
  150. /* ...then round up to nearest multiple of 32 */
  151. audio_latency = (audio_latency + 0x1F) & ~0x1F;
  152. }
  153. }
  154. else
  155. {
  156. environ_cb(RETRO_ENVIRONMENT_SET_AUDIO_BUFFER_STATUS_CALLBACK, NULL);
  157. audio_latency = 0;
  158. }
  159. update_audio_latency = true;
  160. }
  161. /* functions called by the core */
  162. void cache_flush_d_inval_i(void *start, void *end)
  163. {
  164. #ifdef __arm__
  165. size_t len = (char *)end - (char *)start;
  166. (void)len;
  167. #if defined(__BLACKBERRY_QNX__)
  168. msync(start, end - start, MS_SYNC | MS_CACHE_ONLY | MS_INVALIDATE_ICACHE);
  169. #elif defined(__MACH__)
  170. sys_dcache_flush(start, len);
  171. sys_icache_invalidate(start, len);
  172. #elif defined(_3DS)
  173. ctr_flush_invalidate_cache();
  174. #elif defined(VITA)
  175. sceKernelSyncVMDomain(sceBlock, start, len);
  176. #else
  177. __clear_cache(start, end);
  178. #endif
  179. #endif
  180. }
  181. #ifdef RENDER_GSKIT_PS2
  182. /* ee-gcc is version 3.2, where these aren't yet defined */
  183. void __builtin___clear_cache(void *b, void *e)
  184. {
  185. #if 0 /* which of these is overall faster for lots of small cache updates? */
  186. SyncDCache(b, e);
  187. #else
  188. FlushCache(0); /* WRITEBACK_DCACHE */
  189. #endif
  190. FlushCache(2); /* INVALIDATE_ICACHE */
  191. }
  192. int __builtin_parity(unsigned v)
  193. {
  194. /* credits to bit twiddling hacks, https://graphics.stanford.edu/~seander/bithacks.html */
  195. v ^= v >> 16;
  196. v ^= v >> 8;
  197. v ^= v >> 4;
  198. return (0x6996 >> (v&0xf)) & 1;
  199. }
  200. #endif
  201. #ifdef __MACH__
  202. /* calls to this may be generated by the compiler, but it's missing in libc? */
  203. void __clear_cache(void *start, void *end)
  204. {
  205. size_t len = (char *)end - (char *)start;
  206. sys_dcache_flush(start, len);
  207. sys_icache_invalidate(start, len);
  208. }
  209. #endif
  210. #ifdef _WIN32
  211. /* mmap() replacement for Windows
  212. *
  213. * Author: Mike Frysinger <vapier@gentoo.org>
  214. * Placed into the public domain
  215. */
  216. /* References:
  217. * CreateFileMapping: http://msdn.microsoft.com/en-us/library/aa366537(VS.85).aspx
  218. * CloseHandle: http://msdn.microsoft.com/en-us/library/ms724211(VS.85).aspx
  219. * MapViewOfFile: http://msdn.microsoft.com/en-us/library/aa366761(VS.85).aspx
  220. * UnmapViewOfFile: http://msdn.microsoft.com/en-us/library/aa366882(VS.85).aspx
  221. */
  222. #define PROT_READ 0x1
  223. #define PROT_WRITE 0x2
  224. /* This flag is only available in WinXP+ */
  225. #ifdef FILE_MAP_EXECUTE
  226. #define PROT_EXEC 0x4
  227. #else
  228. #define PROT_EXEC 0x0
  229. #define FILE_MAP_EXECUTE 0
  230. #endif
  231. #define MAP_SHARED 0x01
  232. #define MAP_PRIVATE 0x02
  233. #define MAP_ANONYMOUS 0x20
  234. #define MAP_ANON MAP_ANONYMOUS
  235. #define MAP_FAILED ((void *) -1)
  236. #ifdef __USE_FILE_OFFSET64
  237. # define DWORD_HI(x) (x >> 32)
  238. # define DWORD_LO(x) ((x) & 0xffffffff)
  239. #else
  240. # define DWORD_HI(x) (0)
  241. # define DWORD_LO(x) (x)
  242. #endif
  243. static void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
  244. {
  245. uint32_t flProtect, dwDesiredAccess;
  246. off_t end;
  247. HANDLE mmap_fd, h;
  248. void *ret;
  249. if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
  250. return MAP_FAILED;
  251. if (fd == -1) {
  252. if (!(flags & MAP_ANON) || offset)
  253. return MAP_FAILED;
  254. } else if (flags & MAP_ANON)
  255. return MAP_FAILED;
  256. if (prot & PROT_WRITE) {
  257. if (prot & PROT_EXEC)
  258. flProtect = PAGE_EXECUTE_READWRITE;
  259. else
  260. flProtect = PAGE_READWRITE;
  261. } else if (prot & PROT_EXEC) {
  262. if (prot & PROT_READ)
  263. flProtect = PAGE_EXECUTE_READ;
  264. else if (prot & PROT_EXEC)
  265. flProtect = PAGE_EXECUTE;
  266. } else
  267. flProtect = PAGE_READONLY;
  268. end = length + offset;
  269. if (fd == -1)
  270. mmap_fd = INVALID_HANDLE_VALUE;
  271. else
  272. mmap_fd = (HANDLE)_get_osfhandle(fd);
  273. h = CreateFileMapping(mmap_fd, NULL, flProtect, DWORD_HI(end), DWORD_LO(end), NULL);
  274. if (h == NULL)
  275. return MAP_FAILED;
  276. if (prot & PROT_WRITE)
  277. dwDesiredAccess = FILE_MAP_WRITE;
  278. else
  279. dwDesiredAccess = FILE_MAP_READ;
  280. if (prot & PROT_EXEC)
  281. dwDesiredAccess |= FILE_MAP_EXECUTE;
  282. if (flags & MAP_PRIVATE)
  283. dwDesiredAccess |= FILE_MAP_COPY;
  284. ret = MapViewOfFile(h, dwDesiredAccess, DWORD_HI(offset), DWORD_LO(offset), length);
  285. if (ret == NULL) {
  286. CloseHandle(h);
  287. ret = MAP_FAILED;
  288. }
  289. return ret;
  290. }
  291. static void munmap(void *addr, size_t length)
  292. {
  293. UnmapViewOfFile(addr);
  294. /* ruh-ro, we leaked handle from CreateFileMapping() ... */
  295. }
  296. #elif defined(NO_MMAP)
  297. #define PROT_EXEC 0x04
  298. #define MAP_FAILED 0
  299. #define PROT_READ 0
  300. #define PROT_WRITE 0
  301. #define MAP_PRIVATE 0
  302. #define MAP_ANONYMOUS 0
  303. void* mmap(void *desired_addr, size_t len, int mmap_prot, int mmap_flags, int fildes, size_t off)
  304. {
  305. return calloc(1, len);
  306. }
  307. void munmap(void *base_addr, size_t len)
  308. {
  309. free(base_addr);
  310. }
  311. int mprotect(void *addr, size_t len, int prot)
  312. {
  313. /* stub - not really needed at this point since this codepath has no dynarecs */
  314. return 0;
  315. }
  316. #endif
  317. #ifndef MAP_ANONYMOUS
  318. #define MAP_ANONYMOUS MAP_ANON
  319. #endif
  320. #ifdef _3DS
  321. typedef struct
  322. {
  323. unsigned int requested_map;
  324. void* buffer;
  325. }pico_mmap_t;
  326. pico_mmap_t pico_mmaps[] = {
  327. {0x02000000, 0},
  328. {0x06000000, 0},
  329. {NULL, 0}
  330. };
  331. void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed)
  332. {
  333. (void)is_fixed;
  334. if (ctr_svchack_successful)
  335. {
  336. pico_mmap_t* pico_mmap;
  337. for (pico_mmap = pico_mmaps; pico_mmap->requested_map; pico_mmap++)
  338. {
  339. if ((pico_mmap->requested_map == addr))
  340. {
  341. unsigned int ptr_aligned, tmp;
  342. unsigned int currentHandle;
  343. unsigned int perm = 0b011;
  344. if (need_exec)
  345. perm = 0b111;
  346. size = (size + 0xFFF) & ~0xFFF;
  347. pico_mmap->buffer = malloc(size + 0x1000);
  348. ptr_aligned = (((unsigned int)pico_mmap->buffer) + 0xFFF) & ~0xFFF;
  349. svcDuplicateHandle(&currentHandle, 0xFFFF8001);
  350. if(svcControlProcessMemory(currentHandle, pico_mmap->requested_map, ptr_aligned, size, MEMOP_MAP, perm) < 0)
  351. {
  352. if (log_cb)
  353. log_cb(RETRO_LOG_ERROR, "could not map memory @0x%08X\n", pico_mmap->requested_map);
  354. exit(1);
  355. }
  356. svcCloseHandle(currentHandle);
  357. return (void*)pico_mmap->requested_map;
  358. }
  359. }
  360. }
  361. return malloc(size);
  362. }
  363. void *plat_mremap(void *ptr, size_t oldsize, size_t newsize)
  364. {
  365. if (ctr_svchack_successful)
  366. {
  367. pico_mmap_t* pico_mmap;
  368. for (pico_mmap = pico_mmaps; pico_mmap->requested_map; pico_mmap++)
  369. {
  370. if ((pico_mmap->requested_map == (unsigned int)ptr))
  371. {
  372. unsigned int ptr_aligned;
  373. unsigned int currentHandle;
  374. void* tmp;
  375. oldsize = (oldsize + 0xFFF) & ~0xFFF;
  376. newsize = (newsize + 0xFFF) & ~0xFFF;
  377. ptr_aligned = (((unsigned int)pico_mmap->buffer) + 0xFFF) & ~0xFFF;
  378. svcDuplicateHandle(&currentHandle, 0xFFFF8001);
  379. svcControlProcessMemory(currentHandle, pico_mmap->requested_map, ptr_aligned, oldsize, MEMOP_UNMAP, 0b011);
  380. tmp = realloc(pico_mmap->buffer, newsize + 0x1000);
  381. if(!tmp)
  382. return NULL;
  383. pico_mmap->buffer = tmp;
  384. ptr_aligned = (((unsigned int)pico_mmap->buffer) + 0xFFF) & ~0xFFF;
  385. svcControlProcessMemory(currentHandle, pico_mmap->requested_map, ptr_aligned, newsize, MEMOP_MAP, 0x3);
  386. svcCloseHandle(currentHandle);
  387. return ptr;
  388. }
  389. }
  390. }
  391. return realloc(ptr, newsize);
  392. }
  393. void plat_munmap(void *ptr, size_t size)
  394. {
  395. if (ctr_svchack_successful)
  396. {
  397. pico_mmap_t* pico_mmap;
  398. for (pico_mmap = pico_mmaps; pico_mmap->requested_map; pico_mmap++)
  399. {
  400. if ((pico_mmap->requested_map == (unsigned int)ptr))
  401. {
  402. unsigned int ptr_aligned;
  403. unsigned int currentHandle;
  404. size = (size + 0xFFF) & ~0xFFF;
  405. ptr_aligned = (((unsigned int)pico_mmap->buffer) + 0xFFF) & ~0xFFF;
  406. svcDuplicateHandle(&currentHandle, 0xFFFF8001);
  407. svcControlProcessMemory(currentHandle, (void*)pico_mmap->requested_map, (void*)ptr_aligned, size, MEMOP_UNMAP, 0b011);
  408. svcCloseHandle(currentHandle);
  409. free(pico_mmap->buffer);
  410. pico_mmap->buffer = NULL;
  411. return;
  412. }
  413. }
  414. }
  415. free(ptr);
  416. }
  417. #else
  418. void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed)
  419. {
  420. int flags = MAP_PRIVATE | MAP_ANONYMOUS;
  421. void *req, *ret;
  422. req = (void *)(uintptr_t)addr;
  423. ret = mmap(req, size, PROT_READ | PROT_WRITE, flags, -1, 0);
  424. if (ret == MAP_FAILED) {
  425. if (log_cb)
  426. log_cb(RETRO_LOG_ERROR, "mmap(%08lx, %zd) failed: %d\n", addr, size, errno);
  427. return NULL;
  428. }
  429. if (addr != 0 && ret != (void *)(uintptr_t)addr) {
  430. if (log_cb)
  431. log_cb(RETRO_LOG_WARN, "warning: wanted to map @%08lx, got %p\n",
  432. addr, ret);
  433. if (is_fixed) {
  434. munmap(ret, size);
  435. return NULL;
  436. }
  437. }
  438. return ret;
  439. }
  440. void *plat_mremap(void *ptr, size_t oldsize, size_t newsize)
  441. {
  442. #if defined(__linux__) && !defined(__SWITCH__)
  443. void *ret = mremap(ptr, oldsize, newsize, 0);
  444. if (ret == MAP_FAILED)
  445. return NULL;
  446. return ret;
  447. #else
  448. void *tmp, *ret;
  449. size_t preserve_size;
  450. preserve_size = oldsize;
  451. if (preserve_size > newsize)
  452. preserve_size = newsize;
  453. tmp = malloc(preserve_size);
  454. if (tmp == NULL)
  455. return NULL;
  456. memcpy(tmp, ptr, preserve_size);
  457. munmap(ptr, oldsize);
  458. ret = mmap(ptr, newsize, PROT_READ | PROT_WRITE,
  459. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  460. if (ret == MAP_FAILED) {
  461. free(tmp);
  462. return NULL;
  463. }
  464. memcpy(ret, tmp, preserve_size);
  465. free(tmp);
  466. return ret;
  467. #endif
  468. }
  469. void plat_munmap(void *ptr, size_t size)
  470. {
  471. if (ptr != NULL)
  472. munmap(ptr, size);
  473. }
  474. #endif
  475. // if NULL is returned, static buffer is used
  476. void *plat_mem_get_for_drc(size_t size)
  477. {
  478. void *mem = NULL;
  479. #ifdef VITA
  480. sceKernelGetMemBlockBase(sceBlock, &mem);
  481. #endif
  482. return mem;
  483. }
  484. int plat_mem_set_exec(void *ptr, size_t size)
  485. {
  486. int ret = -1;
  487. #ifdef _WIN32
  488. DWORD oldProtect = 0;
  489. ret = VirtualProtect(ptr, size, PAGE_EXECUTE_READWRITE, &oldProtect);
  490. if (ret == 0 && log_cb)
  491. log_cb(RETRO_LOG_ERROR, "VirtualProtect(%p, %d) failed: %d\n", ptr, (int)size,
  492. GetLastError());
  493. #elif defined(_3DS)
  494. if (ctr_svchack_successful)
  495. {
  496. unsigned int currentHandle;
  497. svcDuplicateHandle(&currentHandle, 0xFFFF8001);
  498. ret = svcControlProcessMemory(currentHandle, ptr, 0x0,
  499. size, MEMOP_PROT, 0b111);
  500. svcCloseHandle(currentHandle);
  501. ctr_flush_invalidate_cache();
  502. }
  503. else
  504. {
  505. if (log_cb)
  506. log_cb(RETRO_LOG_ERROR, "plat_mem_set_exec called with no svcControlProcessMemory access\n");
  507. exit(1);
  508. }
  509. #elif defined(VITA)
  510. ret = sceKernelOpenVMDomain();
  511. #else
  512. ret = mprotect(ptr, size, PROT_READ | PROT_WRITE | PROT_EXEC);
  513. if (ret != 0 && log_cb)
  514. log_cb(RETRO_LOG_ERROR, "mprotect(%p, %zd) failed: %d\n", ptr, size, errno);
  515. #endif
  516. return ret;
  517. }
  518. void emu_video_mode_change(int start_line, int line_count, int start_col, int col_count)
  519. {
  520. struct retro_system_av_info av_info;
  521. vm_current_start_line = start_line;
  522. vm_current_line_count = line_count;
  523. vm_current_start_col = start_col;
  524. vm_current_col_count = col_count;
  525. // 8bit renderes create a 328x256 CLUT image, while 16bit creates 320x240 RGB
  526. vout_16bit = vout_format == PDF_RGB555 || (PicoIn.AHW & PAHW_32X);
  527. #if defined(RENDER_GSKIT_PS2)
  528. // calculate the borders of the real image inside the picodrive image
  529. vout_width = (vout_16bit ? VOUT_MAX_WIDTH : VOUT_8BIT_WIDTH);
  530. vout_height = (vout_16bit ? VOUT_MAX_HEIGHT : VOUT_8BIT_HEIGHT);
  531. vout_offset = (vout_16bit ? 0 : 8); // 8bit has 8 px overlap area on the left
  532. padding = (struct retro_hw_ps2_insets){start_line, vout_offset, vout_height - line_count - start_line, vout_width - col_count - vout_offset};
  533. int pxsz = (vout_16bit ? 2 : 1); // pixel size: RGB = 16 bits, CLUT = 8 bits
  534. memset(vout_buf, 0, pxsz * vout_width * vout_height);
  535. memset(retro_palette, 0, gsKit_texture_size_ee(16, 16, GS_PSM_CT16));
  536. PicoDrawSetOutBuf(vout_buf, pxsz * vout_width);
  537. if (ps2) {
  538. // prepare image as texture for rendering
  539. ps2->coreTexture->Width = vout_width;
  540. ps2->coreTexture->Height = vout_height;
  541. ps2->coreTexture->PSM = (vout_16bit ? GS_PSM_CT16 : GS_PSM_T8);
  542. ps2->padding = padding;
  543. }
  544. #else
  545. vout_width = col_count;
  546. memset(vout_buf, 0, VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
  547. if (vout_16bit)
  548. PicoDrawSetOutBuf(vout_buf, vout_width * 2);
  549. if (show_overscan)
  550. {
  551. vout_height = line_count + (start_line * 2);
  552. vout_offset = 0;
  553. }
  554. else
  555. {
  556. vout_height = line_count;
  557. /* Note: We multiply by 2 here to account for pitch */
  558. vout_offset = vout_width * start_line * 2;
  559. }
  560. /* Redundant sanity check... */
  561. vout_height = (vout_height > VOUT_MAX_HEIGHT) ?
  562. VOUT_MAX_HEIGHT : vout_height;
  563. vout_offset = (vout_offset > vout_width * (VOUT_MAX_HEIGHT - 1) * 2) ?
  564. vout_width * (VOUT_MAX_HEIGHT - 1) * 2 : vout_offset;
  565. #endif
  566. Pico.m.dirtyPal = 1;
  567. // Update the geometry
  568. retro_get_system_av_info(&av_info);
  569. environ_cb(RETRO_ENVIRONMENT_SET_GEOMETRY, &av_info);
  570. }
  571. void emu_32x_startup(void)
  572. {
  573. PicoDrawSetOutFormat(vout_format, 0);
  574. if ((vm_current_start_line != -1) &&
  575. (vm_current_line_count != -1) &&
  576. (vm_current_start_col != -1) &&
  577. (vm_current_col_count != -1))
  578. emu_video_mode_change(
  579. vm_current_start_line, vm_current_line_count,
  580. vm_current_start_col, vm_current_col_count);
  581. }
  582. void lprintf(const char *fmt, ...)
  583. {
  584. char buffer[256];
  585. va_list ap;
  586. va_start(ap, fmt);
  587. vsprintf(buffer, fmt, ap);
  588. /* TODO - add 'level' param for warning/error messages? */
  589. if (log_cb)
  590. log_cb(RETRO_LOG_INFO, "%s", buffer);
  591. va_end(ap);
  592. }
  593. /* libretro */
  594. bool libretro_supports_bitmasks = false;
  595. void retro_set_environment(retro_environment_t cb)
  596. {
  597. #ifdef USE_LIBRETRO_VFS
  598. struct retro_vfs_interface_info vfs_iface_info;
  599. #endif
  600. environ_cb = cb;
  601. libretro_set_core_options(environ_cb);
  602. #ifdef USE_LIBRETRO_VFS
  603. vfs_iface_info.required_interface_version = 1;
  604. vfs_iface_info.iface = NULL;
  605. if (environ_cb(RETRO_ENVIRONMENT_GET_VFS_INTERFACE, &vfs_iface_info))
  606. filestream_vfs_init(&vfs_iface_info);
  607. #endif
  608. }
  609. void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
  610. void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
  611. void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
  612. void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
  613. void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
  614. unsigned retro_api_version(void)
  615. {
  616. return RETRO_API_VERSION;
  617. }
  618. void retro_set_controller_port_device(unsigned port, unsigned device)
  619. {
  620. }
  621. void retro_get_system_info(struct retro_system_info *info)
  622. {
  623. memset(info, 0, sizeof(*info));
  624. info->library_name = "PicoDrive";
  625. #ifndef GIT_VERSION
  626. #define _GIT_VERSION ""
  627. #else
  628. #define _GIT_VERSION "-" GIT_VERSION
  629. #endif
  630. info->library_version = VERSION _GIT_VERSION;
  631. info->valid_extensions = "bin|gen|smd|md|32x|cue|iso|chd|sms";
  632. info->need_fullpath = true;
  633. }
  634. void retro_get_system_av_info(struct retro_system_av_info *info)
  635. {
  636. float common_width;
  637. memset(info, 0, sizeof(*info));
  638. info->timing.fps = Pico.m.pal ? 50 : 60;
  639. info->timing.sample_rate = PicoIn.sndRate;
  640. info->geometry.base_width = vout_width;
  641. info->geometry.base_height = vout_height;
  642. info->geometry.max_width = vout_width;
  643. info->geometry.max_height = vout_height;
  644. common_width = vout_width;
  645. if (user_vout_width != 0)
  646. common_width = user_vout_width;
  647. info->geometry.aspect_ratio = common_width / vout_height;
  648. }
  649. /* savestates */
  650. struct savestate_state {
  651. const char *load_buf;
  652. char *save_buf;
  653. size_t size;
  654. size_t pos;
  655. };
  656. size_t state_read(void *p, size_t size, size_t nmemb, void *file)
  657. {
  658. struct savestate_state *state = file;
  659. size_t bsize = size * nmemb;
  660. if (state->pos + bsize > state->size) {
  661. if (log_cb)
  662. log_cb(RETRO_LOG_ERROR, "savestate error: %u/%u\n",
  663. state->pos + bsize, state->size);
  664. bsize = state->size - state->pos;
  665. if ((int)bsize <= 0)
  666. return 0;
  667. }
  668. memcpy(p, state->load_buf + state->pos, bsize);
  669. state->pos += bsize;
  670. return bsize;
  671. }
  672. size_t state_write(void *p, size_t size, size_t nmemb, void *file)
  673. {
  674. struct savestate_state *state = file;
  675. size_t bsize = size * nmemb;
  676. if (state->pos + bsize > state->size) {
  677. if (log_cb)
  678. log_cb(RETRO_LOG_ERROR, "savestate error: %u/%u\n",
  679. state->pos + bsize, state->size);
  680. bsize = state->size - state->pos;
  681. if ((int)bsize <= 0)
  682. return 0;
  683. }
  684. memcpy(state->save_buf + state->pos, p, bsize);
  685. state->pos += bsize;
  686. return bsize;
  687. }
  688. size_t state_skip(void *p, size_t size, size_t nmemb, void *file)
  689. {
  690. struct savestate_state *state = file;
  691. size_t bsize = size * nmemb;
  692. state->pos += bsize;
  693. return bsize;
  694. }
  695. size_t state_eof(void *file)
  696. {
  697. struct savestate_state *state = file;
  698. return state->pos >= state->size;
  699. }
  700. int state_fseek(void *file, long offset, int whence)
  701. {
  702. struct savestate_state *state = file;
  703. switch (whence) {
  704. case SEEK_SET:
  705. state->pos = offset;
  706. break;
  707. case SEEK_CUR:
  708. state->pos += offset;
  709. break;
  710. case SEEK_END:
  711. state->pos = state->size + offset;
  712. break;
  713. }
  714. return (int)state->pos;
  715. }
  716. /* savestate sizes vary wildly depending if cd/32x or
  717. * carthw is active, so run the whole thing to get size */
  718. size_t retro_serialize_size(void)
  719. {
  720. struct savestate_state state = { 0, };
  721. int ret;
  722. ret = PicoStateFP(&state, 1, NULL, state_skip, NULL, state_fseek);
  723. if (ret != 0)
  724. return 0;
  725. return state.pos;
  726. }
  727. bool retro_serialize(void *data, size_t size)
  728. {
  729. struct savestate_state state = { 0, };
  730. int ret;
  731. state.save_buf = data;
  732. state.size = size;
  733. state.pos = 0;
  734. ret = PicoStateFP(&state, 1, NULL, state_write,
  735. NULL, state_fseek);
  736. return ret == 0;
  737. }
  738. bool retro_unserialize(const void *data, size_t size)
  739. {
  740. struct savestate_state state = { 0, };
  741. int ret;
  742. state.load_buf = data;
  743. state.size = size;
  744. state.pos = 0;
  745. ret = PicoStateFP(&state, 0, state_read, NULL,
  746. state_eof, state_fseek);
  747. return ret == 0;
  748. }
  749. typedef struct patch
  750. {
  751. unsigned int addr;
  752. unsigned short data;
  753. unsigned char comp;
  754. } patch;
  755. extern void decode(char *buff, patch *dest);
  756. extern uint16_t m68k_read16(uint32_t a);
  757. extern void m68k_write16(uint32_t a, uint16_t d);
  758. void retro_cheat_reset(void)
  759. {
  760. int i=0;
  761. unsigned int addr;
  762. for (i = 0; i < PicoPatchCount; i++)
  763. {
  764. addr = PicoPatches[i].addr;
  765. if (addr < Pico.romsize) {
  766. if (PicoPatches[i].active)
  767. *(unsigned short *)(Pico.rom + addr) = PicoPatches[i].data_old;
  768. } else {
  769. if (PicoPatches[i].active)
  770. m68k_write16(PicoPatches[i].addr,PicoPatches[i].data_old);
  771. }
  772. }
  773. PicoPatchUnload();
  774. }
  775. void retro_cheat_set(unsigned index, bool enabled, const char *code)
  776. {
  777. patch pt;
  778. int array_len = PicoPatchCount;
  779. char codeCopy[256];
  780. char *buff;
  781. if (*code == '\0')
  782. return;
  783. strcpy(codeCopy, code);
  784. buff = strtok(codeCopy,"+");
  785. while (buff != NULL)
  786. {
  787. decode(buff, &pt);
  788. if (pt.addr == (uint32_t) -1 || pt.data == (uint16_t) -1)
  789. {
  790. log_cb(RETRO_LOG_ERROR,"CHEATS: Invalid code: %s\n",buff);
  791. return;
  792. }
  793. /* code was good, add it */
  794. if (array_len < PicoPatchCount + 1)
  795. {
  796. void *ptr;
  797. array_len *= 2;
  798. array_len++;
  799. ptr = realloc(PicoPatches, array_len * sizeof(PicoPatches[0]));
  800. if (ptr == NULL) {
  801. log_cb(RETRO_LOG_ERROR,"CHEATS: Failed to allocate memory for: %s\n",buff);
  802. return;
  803. }
  804. PicoPatches = ptr;
  805. }
  806. strcpy(PicoPatches[PicoPatchCount].code, buff);
  807. PicoPatches[PicoPatchCount].active = enabled;
  808. PicoPatches[PicoPatchCount].addr = pt.addr;
  809. PicoPatches[PicoPatchCount].data = pt.data;
  810. PicoPatches[PicoPatchCount].comp = pt.comp;
  811. if (PicoPatches[PicoPatchCount].addr < Pico.romsize)
  812. PicoPatches[PicoPatchCount].data_old = *(uint16_t *)(Pico.rom + PicoPatches[PicoPatchCount].addr);
  813. else
  814. PicoPatches[PicoPatchCount].data_old = (uint16_t) m68k_read16(PicoPatches[PicoPatchCount].addr);
  815. PicoPatchCount++;
  816. buff = strtok(NULL,"+");
  817. }
  818. }
  819. /* multidisk support */
  820. static bool disk_ejected;
  821. static unsigned int disk_current_index;
  822. static unsigned int disk_count;
  823. static struct disks_state {
  824. char *fname;
  825. } disks[8];
  826. static bool disk_set_eject_state(bool ejected)
  827. {
  828. // TODO?
  829. disk_ejected = ejected;
  830. return true;
  831. }
  832. static bool disk_get_eject_state(void)
  833. {
  834. return disk_ejected;
  835. }
  836. static unsigned int disk_get_image_index(void)
  837. {
  838. return disk_current_index;
  839. }
  840. static bool disk_set_image_index(unsigned int index)
  841. {
  842. enum cd_track_type cd_type;
  843. int ret;
  844. if (index >= sizeof(disks) / sizeof(disks[0]))
  845. return false;
  846. if (disks[index].fname == NULL) {
  847. if (log_cb)
  848. log_cb(RETRO_LOG_ERROR, "missing disk #%u\n", index);
  849. // RetroArch specifies "no disk" with index == count,
  850. // so don't fail here..
  851. disk_current_index = index;
  852. return true;
  853. }
  854. if (log_cb)
  855. log_cb(RETRO_LOG_INFO, "switching to disk %u: \"%s\"\n", index,
  856. disks[index].fname);
  857. ret = -1;
  858. cd_type = PicoCdCheck(disks[index].fname, NULL);
  859. if (cd_type >= 0 && cd_type != CT_UNKNOWN)
  860. ret = cdd_load(disks[index].fname, cd_type);
  861. if (ret != 0) {
  862. if (log_cb)
  863. log_cb(RETRO_LOG_ERROR, "Load failed, invalid CD image?\n");
  864. return 0;
  865. }
  866. disk_current_index = index;
  867. return true;
  868. }
  869. static unsigned int disk_get_num_images(void)
  870. {
  871. return disk_count;
  872. }
  873. static bool disk_replace_image_index(unsigned index,
  874. const struct retro_game_info *info)
  875. {
  876. bool ret = true;
  877. if (index >= sizeof(disks) / sizeof(disks[0]))
  878. return false;
  879. if (disks[index].fname != NULL)
  880. free(disks[index].fname);
  881. disks[index].fname = NULL;
  882. if (info != NULL) {
  883. disks[index].fname = strdup(info->path);
  884. if (index == disk_current_index)
  885. ret = disk_set_image_index(index);
  886. }
  887. return ret;
  888. }
  889. static bool disk_add_image_index(void)
  890. {
  891. if (disk_count >= sizeof(disks) / sizeof(disks[0]))
  892. return false;
  893. disk_count++;
  894. return true;
  895. }
  896. static struct retro_disk_control_callback disk_control = {
  897. disk_set_eject_state,
  898. disk_get_eject_state,
  899. disk_get_image_index,
  900. disk_set_image_index,
  901. disk_get_num_images,
  902. disk_replace_image_index,
  903. disk_add_image_index,
  904. };
  905. static void disk_tray_open(void)
  906. {
  907. if (log_cb)
  908. log_cb(RETRO_LOG_INFO, "cd tray open\n");
  909. disk_ejected = 1;
  910. }
  911. static void disk_tray_close(void)
  912. {
  913. if (log_cb)
  914. log_cb(RETRO_LOG_INFO, "cd tray close\n");
  915. disk_ejected = 0;
  916. }
  917. static const char * const biosfiles_us[] = {
  918. "us_scd2_9306", "SegaCDBIOS9303", "us_scd1_9210", "bios_CD_U"
  919. };
  920. static const char * const biosfiles_eu[] = {
  921. "eu_mcd2_9306", "eu_mcd2_9303", "eu_mcd1_9210", "bios_CD_E"
  922. };
  923. static const char * const biosfiles_jp[] = {
  924. "jp_mcd2_921222", "jp_mcd1_9112", "jp_mcd1_9111", "bios_CD_J"
  925. };
  926. static void make_system_path(char *buf, size_t buf_size,
  927. const char *name, const char *ext)
  928. {
  929. const char *dir = NULL;
  930. if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir) {
  931. snprintf(buf, buf_size, "%s%c%s%s", dir, SLASH, name, ext);
  932. }
  933. else {
  934. snprintf(buf, buf_size, "%s%s", name, ext);
  935. }
  936. }
  937. static const char *find_bios(int *region, const char *cd_fname)
  938. {
  939. const char * const *files;
  940. static char path[256];
  941. int i, count;
  942. FILE *f = NULL;
  943. if (*region == 4) { // US
  944. files = biosfiles_us;
  945. count = sizeof(biosfiles_us) / sizeof(char *);
  946. } else if (*region == 8) { // EU
  947. files = biosfiles_eu;
  948. count = sizeof(biosfiles_eu) / sizeof(char *);
  949. } else if (*region == 1 || *region == 2) {
  950. files = biosfiles_jp;
  951. count = sizeof(biosfiles_jp) / sizeof(char *);
  952. } else {
  953. return NULL;
  954. }
  955. for (i = 0; i < count; i++)
  956. {
  957. make_system_path(path, sizeof(path), files[i], ".bin");
  958. f = fopen(path, "rb");
  959. if (f != NULL)
  960. break;
  961. make_system_path(path, sizeof(path), files[i], ".zip");
  962. f = fopen(path, "rb");
  963. if (f != NULL)
  964. break;
  965. }
  966. if (f != NULL) {
  967. if (log_cb)
  968. log_cb(RETRO_LOG_INFO, "using bios: %s\n", path);
  969. fclose(f);
  970. return path;
  971. }
  972. return NULL;
  973. }
  974. static void set_memory_maps(void)
  975. {
  976. if (PicoIn.AHW & PAHW_MCD)
  977. {
  978. const size_t SCD_BIT = 1ULL << 31ULL;
  979. const uint64_t mem = RETRO_MEMDESC_SYSTEM_RAM;
  980. struct retro_memory_map mmaps;
  981. struct retro_memory_descriptor descs[] = {
  982. { mem, PicoMem.ram, 0, 0xFF0000, 0, 0, 0x10000, "68KRAM" },
  983. /* virtual address using SCD_BIT so all 512M of prg_ram can be accessed */
  984. /* at address $80020000 */
  985. { mem, Pico_mcd->prg_ram, 0, SCD_BIT | 0x020000, 0, 0, 0x80000, "PRGRAM" },
  986. };
  987. mmaps.descriptors = descs;
  988. mmaps.num_descriptors = sizeof(descs) / sizeof(descs[0]);
  989. environ_cb(RETRO_ENVIRONMENT_SET_MEMORY_MAPS, &mmaps);
  990. }
  991. }
  992. bool retro_load_game(const struct retro_game_info *info)
  993. {
  994. enum media_type_e media_type;
  995. static char carthw_path[256];
  996. size_t i;
  997. struct retro_input_descriptor desc[] = {
  998. { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
  999. { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
  1000. { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
  1001. { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
  1002. { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
  1003. { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "C" },
  1004. { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Y" },
  1005. { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "A" },
  1006. { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "X" },
  1007. { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "Z" },
  1008. { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT,"Mode" },
  1009. { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
  1010. { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
  1011. { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
  1012. { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
  1013. { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
  1014. { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
  1015. { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "C" },
  1016. { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_X, "Y" },
  1017. { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_Y, "A" },
  1018. { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "X" },
  1019. { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "Z" },
  1020. { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT,"Mode" },
  1021. { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
  1022. { 0 },
  1023. };
  1024. struct retro_input_descriptor desc_sms[] = {
  1025. { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
  1026. { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
  1027. { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
  1028. { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
  1029. { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Button 1 Start" },
  1030. { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Button 2" },
  1031. { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Button Pause" },
  1032. { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "D-Pad Left" },
  1033. { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "D-Pad Up" },
  1034. { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "D-Pad Down" },
  1035. { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "D-Pad Right" },
  1036. { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "Button 1 Start" },
  1037. { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "Button 2" },
  1038. { 1, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Button Pause" },
  1039. { 0 },
  1040. };
  1041. enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
  1042. if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
  1043. if (log_cb)
  1044. log_cb(RETRO_LOG_ERROR, "RGB565 support required, sorry\n");
  1045. return false;
  1046. }
  1047. if (info == NULL || info->path == NULL) {
  1048. if (log_cb)
  1049. log_cb(RETRO_LOG_ERROR, "info->path required\n");
  1050. return false;
  1051. }
  1052. for (i = 0; i < sizeof(disks) / sizeof(disks[0]); i++) {
  1053. if (disks[i].fname != NULL) {
  1054. free(disks[i].fname);
  1055. disks[i].fname = NULL;
  1056. }
  1057. }
  1058. disk_current_index = 0;
  1059. disk_count = 1;
  1060. disks[0].fname = strdup(info->path);
  1061. make_system_path(carthw_path, sizeof(carthw_path), "carthw", ".cfg");
  1062. media_type = PicoLoadMedia(info->path, carthw_path,
  1063. find_bios, NULL);
  1064. switch (media_type) {
  1065. case PM_BAD_DETECT:
  1066. if (log_cb)
  1067. log_cb(RETRO_LOG_ERROR, "Failed to detect ROM/CD image type.\n");
  1068. return false;
  1069. case PM_BAD_CD:
  1070. if (log_cb)
  1071. log_cb(RETRO_LOG_ERROR, "Invalid CD image\n");
  1072. return false;
  1073. case PM_BAD_CD_NO_BIOS:
  1074. if (log_cb)
  1075. log_cb(RETRO_LOG_ERROR, "Missing BIOS\n");
  1076. return false;
  1077. case PM_ERROR:
  1078. if (log_cb)
  1079. log_cb(RETRO_LOG_ERROR, "Load error\n");
  1080. return false;
  1081. default:
  1082. break;
  1083. }
  1084. if (media_type == PM_MARK3)
  1085. environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc_sms);
  1086. else
  1087. environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);
  1088. PicoLoopPrepare();
  1089. PicoIn.writeSound = snd_write;
  1090. memset(sndBuffer, 0, sizeof(sndBuffer));
  1091. PicoIn.sndOut = sndBuffer;
  1092. PsndRerate(0);
  1093. PicoDrawSetOutFormat(vout_format, 0);
  1094. /* Setup retro memory maps */
  1095. set_memory_maps();
  1096. init_frameskip();
  1097. return true;
  1098. }
  1099. bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
  1100. {
  1101. return false;
  1102. }
  1103. void retro_unload_game(void)
  1104. {
  1105. }
  1106. unsigned retro_get_region(void)
  1107. {
  1108. return Pico.m.pal ? RETRO_REGION_PAL : RETRO_REGION_NTSC;
  1109. }
  1110. void *retro_get_memory_data(unsigned type)
  1111. {
  1112. uint8_t* data;
  1113. switch(type)
  1114. {
  1115. case RETRO_MEMORY_SAVE_RAM:
  1116. /* Note: MCD RAM cart uses Pico.sv.data */
  1117. if ((PicoIn.AHW & PAHW_MCD) &&
  1118. !(PicoIn.opt & POPT_EN_MCD_RAMCART))
  1119. data = Pico_mcd->bram;
  1120. else
  1121. data = Pico.sv.data;
  1122. break;
  1123. case RETRO_MEMORY_SYSTEM_RAM:
  1124. if (PicoIn.AHW & PAHW_SMS)
  1125. data = PicoMem.zram;
  1126. else
  1127. data = PicoMem.ram;
  1128. break;
  1129. default:
  1130. data = NULL;
  1131. break;
  1132. }
  1133. return data;
  1134. }
  1135. size_t retro_get_memory_size(unsigned type)
  1136. {
  1137. unsigned int i;
  1138. int sum;
  1139. switch(type)
  1140. {
  1141. case RETRO_MEMORY_SAVE_RAM:
  1142. if (PicoIn.AHW & PAHW_MCD)
  1143. {
  1144. if (PicoIn.opt & POPT_EN_MCD_RAMCART)
  1145. return 0x12000;
  1146. else /* bram */
  1147. return 0x2000;
  1148. }
  1149. if (Pico.m.frame_count == 0)
  1150. return Pico.sv.size;
  1151. // if game doesn't write to sram, don't report it to
  1152. // libretro so that RA doesn't write out zeroed .srm
  1153. for (i = 0, sum = 0; i < Pico.sv.size; i++)
  1154. sum |= Pico.sv.data[i];
  1155. return (sum != 0) ? Pico.sv.size : 0;
  1156. case RETRO_MEMORY_SYSTEM_RAM:
  1157. if (PicoIn.AHW & PAHW_SMS)
  1158. return 0x2000;
  1159. else
  1160. return sizeof(PicoMem.ram);
  1161. default:
  1162. return 0;
  1163. }
  1164. }
  1165. void retro_reset(void)
  1166. {
  1167. PicoReset();
  1168. }
  1169. static const unsigned short retro_pico_map[] = {
  1170. 1 << GBTN_B,
  1171. 1 << GBTN_A,
  1172. 1 << GBTN_MODE,
  1173. 1 << GBTN_START,
  1174. 1 << GBTN_UP,
  1175. 1 << GBTN_DOWN,
  1176. 1 << GBTN_LEFT,
  1177. 1 << GBTN_RIGHT,
  1178. 1 << GBTN_C,
  1179. 1 << GBTN_Y,
  1180. 1 << GBTN_X,
  1181. 1 << GBTN_Z,
  1182. };
  1183. #define RETRO_PICO_MAP_LEN (sizeof(retro_pico_map) / sizeof(retro_pico_map[0]))
  1184. static void snd_write(int len)
  1185. {
  1186. audio_batch_cb(PicoIn.sndOut, len / 4);
  1187. }
  1188. static enum input_device input_name_to_val(const char *name)
  1189. {
  1190. if (strcmp(name, "3 button pad") == 0)
  1191. return PICO_INPUT_PAD_3BTN;
  1192. if (strcmp(name, "6 button pad") == 0)
  1193. return PICO_INPUT_PAD_6BTN;
  1194. if (strcmp(name, "None") == 0)
  1195. return PICO_INPUT_NOTHING;
  1196. if (log_cb)
  1197. log_cb(RETRO_LOG_WARN, "invalid picodrive_input: '%s'\n", name);
  1198. return PICO_INPUT_PAD_3BTN;
  1199. }
  1200. static void update_variables(bool first_run)
  1201. {
  1202. struct retro_variable var;
  1203. int OldPicoRegionOverride;
  1204. float old_user_vout_width;
  1205. unsigned old_frameskip_type;
  1206. int old_vout_format;
  1207. double new_sound_rate;
  1208. unsigned short old_snd_filter;
  1209. int32_t old_snd_filter_range;
  1210. var.value = NULL;
  1211. var.key = "picodrive_input1";
  1212. if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
  1213. PicoSetInputDevice(0, input_name_to_val(var.value));
  1214. var.value = NULL;
  1215. var.key = "picodrive_input2";
  1216. if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
  1217. PicoSetInputDevice(1, input_name_to_val(var.value));
  1218. var.value = NULL;
  1219. var.key = "picodrive_sprlim";
  1220. if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
  1221. if (strcmp(var.value, "enabled") == 0)
  1222. PicoIn.opt |= POPT_DIS_SPRITE_LIM;
  1223. else
  1224. PicoIn.opt &= ~POPT_DIS_SPRITE_LIM;
  1225. }
  1226. var.value = NULL;
  1227. var.key = "picodrive_ramcart";
  1228. if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
  1229. if (strcmp(var.value, "enabled") == 0)
  1230. PicoIn.opt |= POPT_EN_MCD_RAMCART;
  1231. else
  1232. PicoIn.opt &= ~POPT_EN_MCD_RAMCART;
  1233. }
  1234. OldPicoRegionOverride = PicoIn.regionOverride;
  1235. var.value = NULL;
  1236. var.key = "picodrive_region";
  1237. if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
  1238. if (strcmp(var.value, "Auto") == 0)
  1239. PicoIn.regionOverride = 0;
  1240. else if (strcmp(var.value, "Japan NTSC") == 0)
  1241. PicoIn.regionOverride = 1;
  1242. else if (strcmp(var.value, "Japan PAL") == 0)
  1243. PicoIn.regionOverride = 2;
  1244. else if (strcmp(var.value, "US") == 0)
  1245. PicoIn.regionOverride = 4;
  1246. else if (strcmp(var.value, "Europe") == 0)
  1247. PicoIn.regionOverride = 8;
  1248. }
  1249. // Update region, fps and sound flags if needed
  1250. if (Pico.rom && PicoIn.regionOverride != OldPicoRegionOverride)
  1251. {
  1252. PicoDetectRegion();
  1253. PicoLoopPrepare();
  1254. PsndRerate(1);
  1255. }
  1256. old_user_vout_width = user_vout_width;
  1257. var.value = NULL;
  1258. var.key = "picodrive_aspect";
  1259. if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
  1260. if (strcmp(var.value, "4/3") == 0)
  1261. user_vout_width = VOUT_4_3;
  1262. else if (strcmp(var.value, "CRT") == 0)
  1263. user_vout_width = VOUT_CRT;
  1264. else
  1265. user_vout_width = VOUT_PAR;
  1266. }
  1267. if (user_vout_width != old_user_vout_width)
  1268. {
  1269. // Update the geometry
  1270. struct retro_system_av_info av_info;
  1271. retro_get_system_av_info(&av_info);
  1272. environ_cb(RETRO_ENVIRONMENT_SET_GEOMETRY, &av_info);
  1273. }
  1274. old_show_overscan = show_overscan;
  1275. var.value = NULL;
  1276. var.key = "picodrive_overscan";
  1277. show_overscan = false;
  1278. if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
  1279. if (strcmp(var.value, "enabled") == 0)
  1280. show_overscan = true;
  1281. }
  1282. var.value = NULL;
  1283. var.key = "picodrive_overclk68k";
  1284. if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
  1285. PicoIn.overclockM68k = 0;
  1286. if (var.value[0] == '+')
  1287. PicoIn.overclockM68k = atoi(var.value + 1);
  1288. }
  1289. #ifdef DRC_SH2
  1290. var.value = NULL;
  1291. var.key = "picodrive_drc";
  1292. if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
  1293. if (strcmp(var.value, "enabled") == 0)
  1294. PicoIn.opt |= POPT_EN_DRC;
  1295. else
  1296. PicoIn.opt &= ~POPT_EN_DRC;
  1297. }
  1298. #endif
  1299. #ifdef _3DS
  1300. if(!ctr_svchack_successful)
  1301. PicoIn.opt &= ~POPT_EN_DRC;
  1302. #endif
  1303. old_snd_filter = PicoIn.opt & POPT_EN_SNDFILTER;
  1304. var.value = NULL;
  1305. var.key = "picodrive_audio_filter";
  1306. PicoIn.opt &= ~POPT_EN_SNDFILTER;
  1307. if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
  1308. if (strcmp(var.value, "low-pass") == 0)
  1309. PicoIn.opt |= POPT_EN_SNDFILTER;
  1310. }
  1311. old_snd_filter_range = PicoIn.sndFilterAlpha;
  1312. var.value = NULL;
  1313. var.key = "picodrive_lowpass_range";
  1314. PicoIn.sndFilterAlpha = (60 * 0x10000 / 100);
  1315. if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
  1316. PicoIn.sndFilterAlpha = (atoi(var.value) * 0x10000 / 100);
  1317. }
  1318. if (((old_snd_filter ^ PicoIn.opt) & POPT_EN_SNDFILTER) ||
  1319. old_snd_filter_range != PicoIn.sndFilterAlpha) {
  1320. mix_reset (PicoIn.opt & POPT_EN_SNDFILTER ? PicoIn.sndFilterAlpha : 0);
  1321. }
  1322. old_frameskip_type = frameskip_type;
  1323. frameskip_type = 0;
  1324. var.key = "picodrive_frameskip";
  1325. if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
  1326. if (strcmp(var.value, "auto") == 0)
  1327. frameskip_type = 1;
  1328. else if (strcmp(var.value, "manual") == 0)
  1329. frameskip_type = 2;
  1330. }
  1331. frameskip_threshold = 33;
  1332. var.key = "picodrive_frameskip_threshold";
  1333. var.value = NULL;
  1334. if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
  1335. frameskip_threshold = strtol(var.value, NULL, 10);
  1336. old_vout_format = vout_format;
  1337. var.value = NULL;
  1338. var.key = "picodrive_renderer";
  1339. if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
  1340. if (strcmp(var.value, "fast") == 0)
  1341. vout_format = PDF_NONE;
  1342. else if (strcmp(var.value, "good") == 0)
  1343. vout_format = PDF_8BIT;
  1344. else if (strcmp(var.value, "accurate") == 0)
  1345. vout_format = PDF_RGB555;
  1346. PicoIn.opt &= ~POPT_ALT_RENDERER;
  1347. if (vout_format == PDF_NONE)
  1348. PicoIn.opt |= POPT_ALT_RENDERER;
  1349. PicoDrawSetOutFormat(vout_format, 0);
  1350. }
  1351. var.value = NULL;
  1352. var.key = "picodrive_sound_rate";
  1353. if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) {
  1354. new_sound_rate = atoi(var.value);
  1355. if (new_sound_rate != PicoIn.sndRate) {
  1356. /* Update the sound rate */
  1357. PicoIn.sndRate = new_sound_rate;
  1358. PsndRerate(1);
  1359. struct retro_system_av_info av_info;
  1360. retro_get_system_av_info(&av_info);
  1361. environ_cb(RETRO_ENVIRONMENT_SET_SYSTEM_AV_INFO, &av_info);
  1362. }
  1363. }
  1364. /* setup video if required */
  1365. if (show_overscan != old_show_overscan || vout_format != old_vout_format)
  1366. {
  1367. if ((vm_current_start_line != -1) &&
  1368. (vm_current_line_count != -1) &&
  1369. (vm_current_start_col != -1) &&
  1370. (vm_current_col_count != -1))
  1371. emu_video_mode_change(
  1372. vm_current_start_line, vm_current_line_count,
  1373. vm_current_start_col, vm_current_col_count);
  1374. }
  1375. /* Reinitialise frameskipping, if required */
  1376. if (((frameskip_type != old_frameskip_type) ||
  1377. (Pico.rom && PicoIn.regionOverride != OldPicoRegionOverride)) &&
  1378. !first_run)
  1379. init_frameskip();
  1380. }
  1381. void retro_run(void)
  1382. {
  1383. bool updated = false;
  1384. int pad, i;
  1385. static void *buff;
  1386. int16_t input;
  1387. PicoIn.skipFrame = 0;
  1388. if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
  1389. update_variables(false);
  1390. input_poll_cb();
  1391. PicoIn.pad[0] = PicoIn.pad[1] = 0;
  1392. for (pad = 0; pad < 2; pad++) {
  1393. if (libretro_supports_bitmasks) {
  1394. input = input_state_cb(pad, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_MASK);
  1395. for (i = 0; i < RETRO_PICO_MAP_LEN; i++)
  1396. if (input & (1 << i))
  1397. PicoIn.pad[pad] |= retro_pico_map[i];
  1398. } else {
  1399. for (i = 0; i < RETRO_PICO_MAP_LEN; i++)
  1400. if (input_state_cb(pad, RETRO_DEVICE_JOYPAD, 0, i))
  1401. PicoIn.pad[pad] |= retro_pico_map[i];
  1402. }
  1403. }
  1404. if (PicoPatches)
  1405. PicoPatchApply();
  1406. /* Check whether current frame should
  1407. * be skipped */
  1408. if ((frameskip_type > 0) && retro_audio_buff_active) {
  1409. switch (frameskip_type)
  1410. {
  1411. case 1: /* auto */
  1412. PicoIn.skipFrame = retro_audio_buff_underrun ? 1 : 0;
  1413. break;
  1414. case 2: /* manual */
  1415. PicoIn.skipFrame = (retro_audio_buff_occupancy < frameskip_threshold) ? 1 : 0;
  1416. break;
  1417. default:
  1418. PicoIn.skipFrame = 0;
  1419. break;
  1420. }
  1421. if (!PicoIn.skipFrame || (frameskip_counter >= FRAMESKIP_MAX)) {
  1422. PicoIn.skipFrame = 0;
  1423. frameskip_counter = 0;
  1424. } else
  1425. frameskip_counter++;
  1426. }
  1427. /* If frameskip settings have changed, update
  1428. * frontend audio latency */
  1429. if (update_audio_latency) {
  1430. environ_cb(RETRO_ENVIRONMENT_SET_MINIMUM_AUDIO_LATENCY,
  1431. &audio_latency);
  1432. update_audio_latency = false;
  1433. }
  1434. PicoFrame();
  1435. /* If frame was skipped, call video_cb() with
  1436. * a NULL buffer and return immediately */
  1437. if (PicoIn.skipFrame) {
  1438. video_cb(NULL, vout_width, vout_height, vout_width * 2);
  1439. return;
  1440. }
  1441. #if defined(RENDER_GSKIT_PS2)
  1442. buff = (uint32_t *)RETRO_HW_FRAME_BUFFER_VALID;
  1443. if (!ps2) {
  1444. // get access to the graphics hardware
  1445. if (!environ_cb(RETRO_ENVIRONMENT_GET_HW_RENDER_INTERFACE, (void **)&ps2) || !ps2) {
  1446. printf("Failed to get HW rendering interface!\n");
  1447. return;
  1448. }
  1449. if (ps2->interface_version != RETRO_HW_RENDER_INTERFACE_GSKIT_PS2_VERSION) {
  1450. printf("HW render interface mismatch, expected %u, got %u!\n",
  1451. RETRO_HW_RENDER_INTERFACE_GSKIT_PS2_VERSION, ps2->interface_version);
  1452. return;
  1453. }
  1454. ps2->coreTexture->ClutPSM = GS_PSM_CT16;
  1455. ps2->coreTexture->Filter = GS_FILTER_LINEAR;
  1456. ps2->coreTexture->Clut = retro_palette;
  1457. ps2->coreTexture->Mem = vout_buf;
  1458. ps2->coreTexture->Width = vout_width;
  1459. ps2->coreTexture->Height = vout_height;
  1460. ps2->coreTexture->PSM = (vout_16bit ? GS_PSM_CT16 : GS_PSM_T8);
  1461. ps2->padding = padding;
  1462. }
  1463. // CLUT update needed?
  1464. if (!vout_16bit && Pico.m.dirtyPal) {
  1465. PicoDrawUpdateHighPal();
  1466. // Rotate CLUT. PS2 is special since entries in CLUT are not in sequence.
  1467. unsigned short int *pal=(void *)retro_palette;
  1468. for (i = 0; i < 256; i+=8) {
  1469. if ((i&0x18) == 0x08)
  1470. memcpy(pal+i,Pico.est.HighPal+i+8,16);
  1471. else if ((i&0x18) == 0x10)
  1472. memcpy(pal+i,Pico.est.HighPal+i-8,16);
  1473. else
  1474. memcpy(pal+i,Pico.est.HighPal+i,16);
  1475. }
  1476. }
  1477. #else
  1478. if (!vout_16bit) {
  1479. /* The 8 bit renderers write a CLUT image in Pico.est.Draw2FB, while libretro wants RGB in vout_buf.
  1480. * We need to manually copy that to vout_buf, applying the CLUT on the way. Especially
  1481. * with the fast renderer this is improving performance, at the expense of accuracy.
  1482. */
  1483. /* This section is mostly copied from pemu_finalize_frame in platform/linux/emu.c */
  1484. unsigned short *pd = (unsigned short *)vout_buf;
  1485. /* Skip the leftmost 8 columns (it is used as an overlap area for rendering) */
  1486. unsigned char *ps = Pico.est.Draw2FB + 8;
  1487. unsigned short *pal = Pico.est.HighPal;
  1488. int x;
  1489. if (Pico.m.dirtyPal)
  1490. PicoDrawUpdateHighPal();
  1491. /* Copy up to the max height to include the overscan area, and skip the leftmost 8 columns again */
  1492. for (i = 0; i < VOUT_MAX_HEIGHT; i++, ps += 8) {
  1493. for (x = 0; x < vout_width; x+=4) {
  1494. *pd++ = pal[*ps++];
  1495. *pd++ = pal[*ps++];
  1496. *pd++ = pal[*ps++];
  1497. *pd++ = pal[*ps++];
  1498. }
  1499. ps += 320-vout_width; /* Advance to next line in case of 32col mode */
  1500. }
  1501. }
  1502. buff = (char*)vout_buf + vout_offset;
  1503. #endif
  1504. video_cb((short *)buff,
  1505. vout_width, vout_height, vout_width * 2);
  1506. }
  1507. void retro_init(void)
  1508. {
  1509. struct retro_log_callback log;
  1510. int level;
  1511. level = 0;
  1512. environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
  1513. if (environ_cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &log))
  1514. log_cb = log.log;
  1515. else
  1516. log_cb = NULL;
  1517. environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control);
  1518. if (environ_cb(RETRO_ENVIRONMENT_GET_INPUT_BITMASKS, NULL))
  1519. libretro_supports_bitmasks = true;
  1520. #ifdef _3DS
  1521. ctr_svchack_successful = ctr_svchack_init();
  1522. check_rosalina();
  1523. #elif defined(VITA)
  1524. sceBlock = getVMBlock();
  1525. #endif
  1526. PicoIn.opt = POPT_EN_STEREO|POPT_EN_FM|POPT_EN_PSG|POPT_EN_Z80|POPT_EN_YM2413
  1527. | POPT_EN_MCD_PCM|POPT_EN_MCD_CDDA|POPT_EN_MCD_GFX
  1528. | POPT_EN_32X|POPT_EN_PWM
  1529. | POPT_ACC_SPRITES|POPT_DIS_32C_BORDER;
  1530. #ifdef __arm__
  1531. #ifdef _3DS
  1532. if (ctr_svchack_successful)
  1533. #endif
  1534. PicoIn.opt |= POPT_EN_DRC;
  1535. #endif
  1536. struct retro_variable var = { .key = "picodrive_sound_rate" };
  1537. if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
  1538. PicoIn.sndRate = atoi(var.value);
  1539. else
  1540. PicoIn.sndRate = INITIAL_SND_RATE;
  1541. PicoIn.autoRgnOrder = 0x184; // US, EU, JP
  1542. vout_width = VOUT_MAX_WIDTH;
  1543. vout_height = VOUT_MAX_HEIGHT;
  1544. #ifdef _3DS
  1545. vout_buf = linearMemAlign(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2, 0x80);
  1546. #elif defined(RENDER_GSKIT_PS2)
  1547. vout_buf = memalign(4096, VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
  1548. retro_palette = memalign(128, gsKit_texture_size_ee(16, 16, GS_PSM_CT16));
  1549. #else
  1550. vout_buf = malloc(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2);
  1551. #endif
  1552. PicoInit();
  1553. //PicoIn.osdMessage = plat_status_msg_busy_next;
  1554. PicoIn.mcdTrayOpen = disk_tray_open;
  1555. PicoIn.mcdTrayClose = disk_tray_close;
  1556. frameskip_type = 0;
  1557. frameskip_threshold = 0;
  1558. frameskip_counter = 0;
  1559. retro_audio_buff_active = false;
  1560. retro_audio_buff_occupancy = 0;
  1561. retro_audio_buff_underrun = false;
  1562. audio_latency = 0;
  1563. update_audio_latency = false;
  1564. update_variables(true);
  1565. }
  1566. void retro_deinit(void)
  1567. {
  1568. #ifdef _3DS
  1569. linearFree(vout_buf);
  1570. #elif defined(RENDER_GSKIT_PS2)
  1571. free(vout_buf);
  1572. free(retro_palette);
  1573. ps2 = NULL;
  1574. #else
  1575. free(vout_buf);
  1576. #endif
  1577. vout_buf = NULL;
  1578. PicoExit();
  1579. libretro_supports_bitmasks = false;
  1580. }