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