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