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