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