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