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