libretro: call video_cb on every frame
[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/psxmem_map.h"
15 #include "../libpcsxcore/new_dynarec/new_dynarec.h"
16 #include "../plugins/dfsound/out.h"
17 #include "../plugins/gpulib/cspace.h"
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;
31 static int samples_sent, samples_to_send;
32 static int plugins_opened;
33
34 /* memory card data */
35 extern char Mcd1Data[MCD_SIZE];
36 extern char McdDisable[2];
37
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
49 static void vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
50 {
51 }
52
53 #ifdef FRONTEND_SUPPORTS_RGB565
54 static void convert(void *buf, size_t bytes)
55 {
56         unsigned int i, v, *p = buf;
57
58         for (i = 0; i < bytes / 4; i++) {
59                 v = p[i];
60                 p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0);
61         }
62 }
63 #endif
64
65 static unsigned game_width;
66 static unsigned game_height;
67
68 static void vout_flip(const void *vram, int stride, int bgr24, int w, int h)
69 {
70         unsigned short *dest = vout_buf;
71         const unsigned short *src = vram;
72         int dstride = w, h1 = h;
73
74         if (vram == NULL) {
75                 // blanking
76                 memset(vout_buf, 0, dstride * h * 2);
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         }
95
96 out:
97 #ifndef FRONTEND_SUPPORTS_RGB565
98    convert(vout_buf, w * h * 2);
99 #endif
100    game_width = w;
101    game_height = h;
102 }
103
104 static void vout_close(void)
105 {
106 }
107
108 static void *pl_mmap(unsigned int size)
109 {
110         return psxMap(0, size, 0, MAP_TAG_VRAM);
111 }
112
113 static void pl_munmap(void *ptr, unsigned int size)
114 {
115         psxUnmap(ptr, size, MAP_TAG_VRAM);
116 }
117
118 struct rearmed_cbs pl_rearmed_cbs = {
119         .pl_vout_open = vout_open,
120         .pl_vout_set_mode = vout_set_mode,
121         .pl_vout_flip = vout_flip,
122         .pl_vout_close = vout_close,
123         .mmap = pl_mmap,
124         .munmap = pl_munmap,
125         /* from psxcounters */
126         .gpu_hcnt = &hSyncCount,
127         .gpu_frame_count = &frame_counter,
128 };
129
130 void pl_frame_limit(void)
131 {
132         /* called once per frame, make psxCpu->Execute() above return */
133         stop = 1;
134 }
135
136 void pl_timing_prepare(int is_pal)
137 {
138 }
139
140 void plat_trigger_vibrate(int is_strong)
141 {
142 }
143
144 void pl_update_gun(int *xn, int *yn, int *xres, int *yres, int *in)
145 {
146 }
147
148 /* sound calls */
149 static int snd_init(void)
150 {
151         return 0;
152 }
153
154 static void snd_finish(void)
155 {
156 }
157
158 static int snd_busy(void)
159 {
160         if (samples_to_send > samples_sent)
161                 return 0; /* give more samples */
162         else
163                 return 1;
164 }
165
166 static void snd_feed(void *buf, int bytes)
167 {
168         audio_batch_cb(buf, bytes / 4);
169         samples_sent += bytes / 4;
170 }
171
172 void out_register_libretro(struct out_driver *drv)
173 {
174         drv->name = "libretro";
175         drv->init = snd_init;
176         drv->finish = snd_finish;
177         drv->busy = snd_busy;
178         drv->feed = snd_feed;
179 }
180
181 /* libretro */
182 void retro_set_environment(retro_environment_t cb) { environ_cb = cb; }
183 void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
184 void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
185 void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
186 void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
187 void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
188
189 unsigned retro_api_version(void)
190 {
191         return RETRO_API_VERSION;
192 }
193
194 void retro_set_controller_port_device(unsigned port, unsigned device)
195 {
196 }
197
198 void retro_get_system_info(struct retro_system_info *info)
199 {
200         memset(info, 0, sizeof(*info));
201         info->library_name = "PCSX-ReARMed";
202         info->library_version = REV;
203         info->valid_extensions = "bin|cue|img|mdf|pbp|cbn";
204         info->need_fullpath = true;
205 }
206
207 void retro_get_system_av_info(struct retro_system_av_info *info)
208 {
209         memset(info, 0, sizeof(*info));
210         info->timing.fps            = 60;
211         info->timing.sample_rate    = 44100;
212         info->geometry.base_width   = 320;
213         info->geometry.base_height  = 240;
214         info->geometry.max_width    = 640;
215         info->geometry.max_height   = 512;
216         info->geometry.aspect_ratio = 4.0 / 3.0;
217 }
218
219 /* TODO */
220 size_t retro_serialize_size(void) 
221
222         return 0;
223 }
224
225 bool retro_serialize(void *data, size_t size)
226
227         return false;
228 }
229
230 bool retro_unserialize(const void *data, size_t size)
231 {
232         return false;
233 }
234
235 void retro_cheat_reset(void)
236 {
237 }
238
239 void retro_cheat_set(unsigned index, bool enabled, const char *code)
240 {
241 }
242
243 bool retro_load_game(const struct retro_game_info *info)
244 {
245 #ifdef FRONTEND_SUPPORTS_RGB565
246         enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
247         if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) {
248                 fprintf(stderr, "RGB565 supported, using it\n");
249         }
250 #endif
251
252         if (plugins_opened) {
253                 ClosePlugins();
254                 plugins_opened = 0;
255         }
256
257         set_cd_image(info->path);
258
259         /* have to reload after set_cd_image for correct cdr plugin */
260         if (LoadPlugins() == -1) {
261                 printf("faled to load plugins\n");
262                 return false;
263         }
264
265         plugins_opened = 1;
266         NetOpened = 0;
267
268         if (OpenPlugins() == -1) {
269                 printf("faled to open plugins\n");
270                 return false;
271         }
272
273         plugin_call_rearmed_cbs();
274
275         Config.PsxAuto = 1;
276         if (CheckCdrom() == -1) {
277                 printf("unsupported/invalid CD image: %s\n", info->path);
278                 return false;
279         }
280
281         SysReset();
282
283         if (LoadCdrom() == -1) {
284                 printf("could not load CD-ROM!\n");
285                 return false;
286         }
287         emu_on_new_cd(0);
288
289         return true;
290 }
291
292 bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
293 {
294         return false;
295 }
296
297 void retro_unload_game(void) 
298 {
299 }
300
301 unsigned retro_get_region(void)
302 {
303         return RETRO_REGION_NTSC;
304 }
305
306 void *retro_get_memory_data(unsigned id)
307 {
308         return Mcd1Data;
309 }
310
311 size_t retro_get_memory_size(unsigned id)
312 {
313         return MCD_SIZE;
314 }
315
316 void retro_reset(void)
317 {
318         SysReset();
319 }
320
321 static const unsigned short retro_psx_map[] = {
322         [RETRO_DEVICE_ID_JOYPAD_B]      = 1 << DKEY_CROSS,
323         [RETRO_DEVICE_ID_JOYPAD_Y]      = 1 << DKEY_SQUARE,
324         [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
325         [RETRO_DEVICE_ID_JOYPAD_START]  = 1 << DKEY_START,
326         [RETRO_DEVICE_ID_JOYPAD_UP]     = 1 << DKEY_UP,
327         [RETRO_DEVICE_ID_JOYPAD_DOWN]   = 1 << DKEY_DOWN,
328         [RETRO_DEVICE_ID_JOYPAD_LEFT]   = 1 << DKEY_LEFT,
329         [RETRO_DEVICE_ID_JOYPAD_RIGHT]  = 1 << DKEY_RIGHT,
330         [RETRO_DEVICE_ID_JOYPAD_A]      = 1 << DKEY_CIRCLE,
331         [RETRO_DEVICE_ID_JOYPAD_X]      = 1 << DKEY_TRIANGLE,
332         [RETRO_DEVICE_ID_JOYPAD_L]      = 1 << DKEY_L1,
333         [RETRO_DEVICE_ID_JOYPAD_R]      = 1 << DKEY_R1,
334         [RETRO_DEVICE_ID_JOYPAD_L2]     = 1 << DKEY_L2,
335         [RETRO_DEVICE_ID_JOYPAD_R2]     = 1 << DKEY_R2,
336         [RETRO_DEVICE_ID_JOYPAD_L3]     = 1 << DKEY_L3,
337         [RETRO_DEVICE_ID_JOYPAD_R3]     = 1 << DKEY_R3,
338 };
339 #define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
340
341 void retro_run(void) 
342 {
343         int i;
344
345         input_poll_cb();
346         in_keystate = 0;
347         for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
348                 if (input_state_cb(1, RETRO_DEVICE_JOYPAD, 0, i))
349                         in_keystate |= retro_psx_map[i];
350         in_keystate <<= 16;
351         for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
352                 if (input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i))
353                         in_keystate |= retro_psx_map[i];
354
355         stop = 0;
356         psxCpu->Execute();
357
358         samples_to_send += 44100 / 60;
359         video_cb(vout_buf, game_width, game_height, game_width * 2);
360         pl_rearmed_cbs.flip_cnt++;
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);
402
403         McdDisable[0] = 0;
404         McdDisable[1] = 1;
405 }
406
407 void retro_deinit(void)
408 {
409         SysClose();
410         free(vout_buf);
411         vout_buf = NULL;
412 }