Commit | Line | Data |
---|---|---|
38c2028e | 1 | /* |
2 | * (C) notaz, 2012 | |
3 | * | |
4 | * This work is licensed under the terms of the GNU GPLv2 or later. | |
5 | * See the COPYING file in the top-level directory. | |
6 | */ | |
7 | ||
8 | #include <stdio.h> | |
9 | #include <stdlib.h> | |
10 | #include <string.h> | |
11 | ||
12 | #include "../libpcsxcore/misc.h" | |
13 | #include "../libpcsxcore/psxcounters.h" | |
87e5b45f | 14 | #include "../libpcsxcore/psxmem_map.h" |
38c2028e | 15 | #include "../libpcsxcore/new_dynarec/new_dynarec.h" |
07c13dfd | 16 | #include "../plugins/dfsound/out.h" |
fa56d360 | 17 | #include "../plugins/gpulib/cspace.h" |
38c2028e | 18 | #include "main.h" |
19 | #include "plugin.h" | |
20 | #include "plugin_lib.h" | |
21 | #include "revision.h" | |
22 | #include "libretro.h" | |
23 | ||
24 | static retro_video_refresh_t video_cb; | |
25 | static retro_input_poll_t input_poll_cb; | |
26 | static retro_input_state_t input_state_cb; | |
27 | static retro_environment_t environ_cb; | |
28 | static retro_audio_sample_batch_t audio_batch_cb; | |
29 | ||
30 | static void *vout_buf; | |
70d56ca3 | 31 | static int vout_width, vout_height; |
32 | static int vout_doffs_old, vout_fb_dirty; | |
33 | ||
38c2028e | 34 | static int samples_sent, samples_to_send; |
35 | static int plugins_opened; | |
36 | ||
f9f60dae TK |
37 | /* memory card data */ |
38 | extern char Mcd1Data[MCD_SIZE]; | |
39 | extern char McdDisable[2]; | |
38c2028e | 40 | |
41 | /* PCSX ReARMed core calls and stuff */ | |
42 | int in_type1, in_type2; | |
43 | int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 }; | |
44 | int in_keystate; | |
45 | int in_enable_vibration; | |
46 | ||
7df396ea TK |
47 | static void init_memcard(char *mcd_data) |
48 | { | |
49 | unsigned off = 0; | |
50 | unsigned i; | |
51 | ||
52 | memset(mcd_data, 0, MCD_SIZE); | |
53 | ||
54 | mcd_data[off++] = 'M'; | |
55 | mcd_data[off++] = 'C'; | |
56 | off += 0x7d; | |
57 | mcd_data[off++] = 0x0e; | |
58 | ||
59 | for (i = 0; i < 15; i++) { | |
60 | mcd_data[off++] = 0xa0; | |
61 | off += 0x07; | |
62 | mcd_data[off++] = 0xff; | |
63 | mcd_data[off++] = 0xff; | |
64 | off += 0x75; | |
65 | mcd_data[off++] = 0xa0; | |
66 | } | |
67 | ||
68 | for (i = 0; i < 20; i++) { | |
69 | mcd_data[off++] = 0xff; | |
70 | mcd_data[off++] = 0xff; | |
71 | mcd_data[off++] = 0xff; | |
72 | mcd_data[off++] = 0xff; | |
73 | off += 0x04; | |
74 | mcd_data[off++] = 0xff; | |
75 | mcd_data[off++] = 0xff; | |
76 | off += 0x76; | |
77 | } | |
78 | } | |
79 | ||
38c2028e | 80 | static int vout_open(void) |
81 | { | |
82 | return 0; | |
83 | } | |
84 | ||
e4c83ca6 | 85 | static void vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp) |
38c2028e | 86 | { |
70d56ca3 | 87 | vout_width = w; |
88 | vout_height = h; | |
38c2028e | 89 | } |
90 | ||
cc4f9b70 | 91 | #ifndef FRONTEND_SUPPORTS_RGB565 |
9c59f120 | 92 | static void convert(void *buf, size_t bytes) |
38c2028e | 93 | { |
94 | unsigned int i, v, *p = buf; | |
95 | ||
9c59f120 | 96 | for (i = 0; i < bytes / 4; i++) { |
38c2028e | 97 | v = p[i]; |
98 | p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0); | |
99 | } | |
100 | } | |
46aa5b98 | 101 | #endif |
38c2028e | 102 | |
fa56d360 | 103 | static void vout_flip(const void *vram, int stride, int bgr24, int w, int h) |
38c2028e | 104 | { |
fa56d360 | 105 | unsigned short *dest = vout_buf; |
106 | const unsigned short *src = vram; | |
70d56ca3 | 107 | int dstride = vout_width, h1 = h; |
108 | int doffs; | |
38c2028e | 109 | |
fa56d360 | 110 | if (vram == NULL) { |
111 | // blanking | |
cc56203b | 112 | memset(vout_buf, 0, dstride * h * 2); |
fa56d360 | 113 | goto out; |
114 | } | |
115 | ||
70d56ca3 | 116 | doffs = (vout_height - h) * dstride; |
117 | doffs += (dstride - w) / 2 & ~1; | |
118 | if (doffs != vout_doffs_old) { | |
119 | // clear borders | |
120 | memset(vout_buf, 0, dstride * h * 2); | |
121 | vout_doffs_old = doffs; | |
122 | } | |
123 | dest += doffs; | |
124 | ||
fa56d360 | 125 | if (bgr24) |
126 | { | |
127 | // XXX: could we switch to RETRO_PIXEL_FORMAT_XRGB8888 here? | |
128 | for (; h1-- > 0; dest += dstride, src += stride) | |
129 | { | |
130 | bgr888_to_rgb565(dest, src, w * 3); | |
131 | } | |
132 | } | |
133 | else | |
134 | { | |
135 | for (; h1-- > 0; dest += dstride, src += stride) | |
136 | { | |
137 | bgr555_to_rgb565(dest, src, w * 2); | |
138 | } | |
139 | } | |
38c2028e | 140 | |
fa56d360 | 141 | out: |
46aa5b98 | 142 | #ifndef FRONTEND_SUPPORTS_RGB565 |
70d56ca3 | 143 | convert(vout_buf, vout_width * vout_height * 2); |
46aa5b98 | 144 | #endif |
70d56ca3 | 145 | vout_fb_dirty = 1; |
fa56d360 | 146 | pl_rearmed_cbs.flip_cnt++; |
38c2028e | 147 | } |
148 | ||
149 | static void vout_close(void) | |
150 | { | |
151 | } | |
152 | ||
cc56203b | 153 | static void *pl_mmap(unsigned int size) |
154 | { | |
87e5b45f | 155 | return psxMap(0, size, 0, MAP_TAG_VRAM); |
cc56203b | 156 | } |
157 | ||
158 | static void pl_munmap(void *ptr, unsigned int size) | |
159 | { | |
87e5b45f | 160 | psxUnmap(ptr, size, MAP_TAG_VRAM); |
cc56203b | 161 | } |
162 | ||
38c2028e | 163 | struct rearmed_cbs pl_rearmed_cbs = { |
164 | .pl_vout_open = vout_open, | |
165 | .pl_vout_set_mode = vout_set_mode, | |
166 | .pl_vout_flip = vout_flip, | |
167 | .pl_vout_close = vout_close, | |
cc56203b | 168 | .mmap = pl_mmap, |
169 | .munmap = pl_munmap, | |
38c2028e | 170 | /* from psxcounters */ |
171 | .gpu_hcnt = &hSyncCount, | |
172 | .gpu_frame_count = &frame_counter, | |
173 | }; | |
174 | ||
175 | void pl_frame_limit(void) | |
176 | { | |
177 | /* called once per frame, make psxCpu->Execute() above return */ | |
178 | stop = 1; | |
179 | } | |
180 | ||
181 | void pl_timing_prepare(int is_pal) | |
182 | { | |
183 | } | |
184 | ||
185 | void plat_trigger_vibrate(int is_strong) | |
186 | { | |
187 | } | |
188 | ||
e4c83ca6 | 189 | void pl_update_gun(int *xn, int *yn, int *xres, int *yres, int *in) |
38c2028e | 190 | { |
191 | } | |
192 | ||
193 | /* sound calls */ | |
07c13dfd | 194 | static int snd_init(void) |
38c2028e | 195 | { |
07c13dfd | 196 | return 0; |
38c2028e | 197 | } |
198 | ||
07c13dfd | 199 | static void snd_finish(void) |
38c2028e | 200 | { |
201 | } | |
202 | ||
07c13dfd | 203 | static int snd_busy(void) |
38c2028e | 204 | { |
205 | if (samples_to_send > samples_sent) | |
206 | return 0; /* give more samples */ | |
207 | else | |
208 | return 1; | |
209 | } | |
210 | ||
07c13dfd | 211 | static void snd_feed(void *buf, int bytes) |
38c2028e | 212 | { |
213 | audio_batch_cb(buf, bytes / 4); | |
214 | samples_sent += bytes / 4; | |
215 | } | |
216 | ||
07c13dfd | 217 | void out_register_libretro(struct out_driver *drv) |
218 | { | |
219 | drv->name = "libretro"; | |
220 | drv->init = snd_init; | |
221 | drv->finish = snd_finish; | |
222 | drv->busy = snd_busy; | |
223 | drv->feed = snd_feed; | |
224 | } | |
225 | ||
38c2028e | 226 | /* libretro */ |
227 | void retro_set_environment(retro_environment_t cb) { environ_cb = cb; } | |
228 | void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; } | |
229 | void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; } | |
230 | void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; } | |
231 | void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; } | |
232 | void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; } | |
233 | ||
234 | unsigned retro_api_version(void) | |
235 | { | |
236 | return RETRO_API_VERSION; | |
237 | } | |
238 | ||
239 | void retro_set_controller_port_device(unsigned port, unsigned device) | |
240 | { | |
241 | } | |
242 | ||
243 | void retro_get_system_info(struct retro_system_info *info) | |
244 | { | |
245 | memset(info, 0, sizeof(*info)); | |
246 | info->library_name = "PCSX-ReARMed"; | |
247 | info->library_version = REV; | |
248 | info->valid_extensions = "bin|cue|img|mdf|pbp|cbn"; | |
249 | info->need_fullpath = true; | |
250 | } | |
251 | ||
252 | void retro_get_system_av_info(struct retro_system_av_info *info) | |
253 | { | |
254 | memset(info, 0, sizeof(*info)); | |
255 | info->timing.fps = 60; | |
256 | info->timing.sample_rate = 44100; | |
257 | info->geometry.base_width = 320; | |
258 | info->geometry.base_height = 240; | |
259 | info->geometry.max_width = 640; | |
260 | info->geometry.max_height = 512; | |
261 | info->geometry.aspect_ratio = 4.0 / 3.0; | |
262 | } | |
263 | ||
264 | /* TODO */ | |
265 | size_t retro_serialize_size(void) | |
266 | { | |
267 | return 0; | |
268 | } | |
269 | ||
270 | bool retro_serialize(void *data, size_t size) | |
271 | { | |
272 | return false; | |
273 | } | |
274 | ||
275 | bool retro_unserialize(const void *data, size_t size) | |
276 | { | |
277 | return false; | |
278 | } | |
279 | ||
280 | void retro_cheat_reset(void) | |
281 | { | |
282 | } | |
283 | ||
284 | void retro_cheat_set(unsigned index, bool enabled, const char *code) | |
285 | { | |
286 | } | |
287 | ||
288 | bool retro_load_game(const struct retro_game_info *info) | |
289 | { | |
46aa5b98 | 290 | #ifdef FRONTEND_SUPPORTS_RGB565 |
c19aba43 | 291 | enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565; |
9f766dbe | 292 | if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) { |
f29fbd53 | 293 | SysPrintf("RGB565 supported, using it\n"); |
c19aba43 | 294 | } |
46aa5b98 | 295 | #endif |
c19aba43 | 296 | |
38c2028e | 297 | if (plugins_opened) { |
298 | ClosePlugins(); | |
299 | plugins_opened = 0; | |
300 | } | |
301 | ||
302 | set_cd_image(info->path); | |
303 | ||
304 | /* have to reload after set_cd_image for correct cdr plugin */ | |
305 | if (LoadPlugins() == -1) { | |
f29fbd53 | 306 | SysPrintf("failed to load plugins\n"); |
38c2028e | 307 | return false; |
308 | } | |
309 | ||
310 | plugins_opened = 1; | |
311 | NetOpened = 0; | |
312 | ||
313 | if (OpenPlugins() == -1) { | |
f29fbd53 | 314 | SysPrintf("failed to open plugins\n"); |
38c2028e | 315 | return false; |
316 | } | |
317 | ||
318 | plugin_call_rearmed_cbs(); | |
319 | ||
320 | Config.PsxAuto = 1; | |
321 | if (CheckCdrom() == -1) { | |
f29fbd53 | 322 | SysPrintf("unsupported/invalid CD image: %s\n", info->path); |
38c2028e | 323 | return false; |
324 | } | |
325 | ||
326 | SysReset(); | |
327 | ||
328 | if (LoadCdrom() == -1) { | |
f29fbd53 | 329 | SysPrintf("could not load CD-ROM!\n"); |
38c2028e | 330 | return false; |
331 | } | |
79f216e3 | 332 | emu_on_new_cd(0); |
38c2028e | 333 | |
334 | return true; | |
335 | } | |
336 | ||
337 | bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info) | |
338 | { | |
339 | return false; | |
340 | } | |
341 | ||
342 | void retro_unload_game(void) | |
343 | { | |
344 | } | |
345 | ||
346 | unsigned retro_get_region(void) | |
347 | { | |
348 | return RETRO_REGION_NTSC; | |
349 | } | |
350 | ||
351 | void *retro_get_memory_data(unsigned id) | |
352 | { | |
f9f60dae | 353 | return Mcd1Data; |
38c2028e | 354 | } |
355 | ||
356 | size_t retro_get_memory_size(unsigned id) | |
357 | { | |
f9f60dae | 358 | return MCD_SIZE; |
38c2028e | 359 | } |
360 | ||
361 | void retro_reset(void) | |
362 | { | |
363 | SysReset(); | |
364 | } | |
365 | ||
366 | static const unsigned short retro_psx_map[] = { | |
367 | [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS, | |
368 | [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE, | |
369 | [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT, | |
370 | [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START, | |
371 | [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP, | |
372 | [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN, | |
373 | [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT, | |
374 | [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT, | |
375 | [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE, | |
376 | [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE, | |
377 | [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1, | |
378 | [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1, | |
379 | [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2, | |
380 | [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2, | |
381 | [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3, | |
382 | [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3, | |
383 | }; | |
384 | #define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0])) | |
385 | ||
386 | void retro_run(void) | |
387 | { | |
388 | int i; | |
389 | ||
390 | input_poll_cb(); | |
391 | in_keystate = 0; | |
392 | for (i = 0; i < RETRO_PSX_MAP_LEN; i++) | |
393 | if (input_state_cb(1, RETRO_DEVICE_JOYPAD, 0, i)) | |
394 | in_keystate |= retro_psx_map[i]; | |
395 | in_keystate <<= 16; | |
396 | for (i = 0; i < RETRO_PSX_MAP_LEN; i++) | |
397 | if (input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i)) | |
398 | in_keystate |= retro_psx_map[i]; | |
399 | ||
400 | stop = 0; | |
401 | psxCpu->Execute(); | |
402 | ||
403 | samples_to_send += 44100 / 60; | |
e2bdb933 | 404 | |
70d56ca3 | 405 | video_cb(vout_fb_dirty ? vout_buf : NULL, vout_width, vout_height, vout_width * 2); |
406 | vout_fb_dirty = 0; | |
38c2028e | 407 | } |
408 | ||
409 | void retro_init(void) | |
410 | { | |
411 | const char *bios[] = { "scph1001", "scph5501", "scph7001" }; | |
412 | const char *dir; | |
413 | char path[256]; | |
414 | FILE *f = NULL; | |
415 | int i, ret, level; | |
416 | ||
417 | ret = emu_core_preinit(); | |
418 | ret |= emu_core_init(); | |
419 | if (ret != 0) { | |
f29fbd53 | 420 | SysPrintf("PCSX init failed.\n"); |
38c2028e | 421 | exit(1); |
422 | } | |
423 | ||
424 | vout_buf = malloc(640 * 512 * 2); | |
425 | ||
426 | if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir) | |
427 | { | |
428 | snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s/", dir); | |
429 | ||
430 | for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++) { | |
431 | snprintf(path, sizeof(path), "%s/%s.bin", dir, bios[i]); | |
432 | f = fopen(path, "r"); | |
433 | if (f != NULL) { | |
434 | snprintf(Config.Bios, sizeof(Config.Bios), "%s.bin", bios[i]); | |
435 | break; | |
436 | } | |
437 | } | |
438 | } | |
439 | if (f != NULL) { | |
f29fbd53 | 440 | SysPrintf("found BIOS file: %s\n", Config.Bios); |
38c2028e | 441 | fclose(f); |
442 | } | |
443 | else | |
f29fbd53 | 444 | SysPrintf("no BIOS files found.\n"); |
38c2028e | 445 | |
446 | level = 1; | |
447 | environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level); | |
f9f60dae | 448 | |
5ca2ec64 | 449 | /* Set how much slower PSX CPU runs * 100 (so that 200 is 2 times) |
450 | * we have to do this because cache misses and some IO penalties | |
451 | * are not emulated. Warning: changing this may break compatibility. */ | |
452 | #ifdef __ARM_ARCH_7A__ | |
453 | cycle_multiplier = 175; | |
454 | #else | |
455 | cycle_multiplier = 200; | |
456 | #endif | |
457 | ||
f9f60dae TK |
458 | McdDisable[0] = 0; |
459 | McdDisable[1] = 1; | |
7df396ea | 460 | init_memcard(Mcd1Data); |
38c2028e | 461 | } |
462 | ||
463 | void retro_deinit(void) | |
464 | { | |
465 | SysClose(); | |
466 | free(vout_buf); | |
467 | vout_buf = NULL; | |
468 | } |