move blit to core, allow filtering while blitting
[pcsx_rearmed.git] / frontend / libretro.c
CommitLineData
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
23static retro_video_refresh_t video_cb;
24static retro_input_poll_t input_poll_cb;
25static retro_input_state_t input_state_cb;
26static retro_environment_t environ_cb;
27static retro_audio_sample_batch_t audio_batch_cb;
28
29static void *vout_buf;
38c2028e 30static int samples_sent, samples_to_send;
31static int plugins_opened;
32
33/* PCSX ReARMed core calls and stuff */
34int in_type1, in_type2;
35int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
36int in_keystate;
37int in_enable_vibration;
38
39static int vout_open(void)
40{
41 return 0;
42}
43
fa56d360 44static void vout_set_mode(int w, int h, int bpp)
38c2028e 45{
38c2028e 46}
47
48/* FIXME: either teach PCSX to blit to RGB1555 or RetroArch to support RGB565 */
9c59f120 49static 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 59static 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;
64
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
88out:
89 convert(vout_buf, w * h * 2);
90 video_cb(vout_buf, w, h, w * 2);
91 pl_rearmed_cbs.flip_cnt++;
38c2028e 92}
93
94static void vout_close(void)
95{
96}
97
98struct 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
108void pl_frame_limit(void)
109{
110 /* called once per frame, make psxCpu->Execute() above return */
111 stop = 1;
112}
113
114void pl_timing_prepare(int is_pal)
115{
116}
117
118void plat_trigger_vibrate(int is_strong)
119{
120}
121
122void pl_update_gun(int *xn, int *xres, int *y, int *in)
123{
124}
125
126/* sound calls */
07c13dfd 127static int snd_init(void)
38c2028e 128{
07c13dfd 129 return 0;
38c2028e 130}
131
07c13dfd 132static void snd_finish(void)
38c2028e 133{
134}
135
07c13dfd 136static 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 144static void snd_feed(void *buf, int bytes)
38c2028e 145{
146 audio_batch_cb(buf, bytes / 4);
147 samples_sent += bytes / 4;
148}
149
07c13dfd 150void 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 */
160void retro_set_environment(retro_environment_t cb) { environ_cb = cb; }
161void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
162void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
163void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
164void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
165void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
166
167unsigned retro_api_version(void)
168{
169 return RETRO_API_VERSION;
170}
171
172void retro_set_controller_port_device(unsigned port, unsigned device)
173{
174}
175
176void 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
185void 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 */
198size_t retro_serialize_size(void)
199{
200 return 0;
201}
202
203bool retro_serialize(void *data, size_t size)
204{
205 return false;
206}
207
208bool retro_unserialize(const void *data, size_t size)
209{
210 return false;
211}
212
213void retro_cheat_reset(void)
214{
215}
216
217void retro_cheat_set(unsigned index, bool enabled, const char *code)
218{
219}
220
221bool retro_load_game(const struct retro_game_info *info)
222{
223 if (plugins_opened) {
224 ClosePlugins();
225 plugins_opened = 0;
226 }
227
228 set_cd_image(info->path);
229
230 /* have to reload after set_cd_image for correct cdr plugin */
231 if (LoadPlugins() == -1) {
232 printf("faled to load plugins\n");
233 return false;
234 }
235
236 plugins_opened = 1;
237 NetOpened = 0;
238
239 if (OpenPlugins() == -1) {
240 printf("faled to open plugins\n");
241 return false;
242 }
243
244 plugin_call_rearmed_cbs();
245
246 Config.PsxAuto = 1;
247 if (CheckCdrom() == -1) {
248 printf("unsupported/invalid CD image: %s\n", info->path);
249 return false;
250 }
251
252 SysReset();
253
254 if (LoadCdrom() == -1) {
255 printf("could not load CD-ROM!\n");
256 return false;
257 }
258 emu_on_new_cd();
259
260 return true;
261}
262
263bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
264{
265 return false;
266}
267
268void retro_unload_game(void)
269{
270}
271
272unsigned retro_get_region(void)
273{
274 return RETRO_REGION_NTSC;
275}
276
277void *retro_get_memory_data(unsigned id)
278{
279 return NULL;
280}
281
282size_t retro_get_memory_size(unsigned id)
283{
284 return 0;
285}
286
287void retro_reset(void)
288{
289 SysReset();
290}
291
292static const unsigned short retro_psx_map[] = {
293 [RETRO_DEVICE_ID_JOYPAD_B] = 1 << DKEY_CROSS,
294 [RETRO_DEVICE_ID_JOYPAD_Y] = 1 << DKEY_SQUARE,
295 [RETRO_DEVICE_ID_JOYPAD_SELECT] = 1 << DKEY_SELECT,
296 [RETRO_DEVICE_ID_JOYPAD_START] = 1 << DKEY_START,
297 [RETRO_DEVICE_ID_JOYPAD_UP] = 1 << DKEY_UP,
298 [RETRO_DEVICE_ID_JOYPAD_DOWN] = 1 << DKEY_DOWN,
299 [RETRO_DEVICE_ID_JOYPAD_LEFT] = 1 << DKEY_LEFT,
300 [RETRO_DEVICE_ID_JOYPAD_RIGHT] = 1 << DKEY_RIGHT,
301 [RETRO_DEVICE_ID_JOYPAD_A] = 1 << DKEY_CIRCLE,
302 [RETRO_DEVICE_ID_JOYPAD_X] = 1 << DKEY_TRIANGLE,
303 [RETRO_DEVICE_ID_JOYPAD_L] = 1 << DKEY_L1,
304 [RETRO_DEVICE_ID_JOYPAD_R] = 1 << DKEY_R1,
305 [RETRO_DEVICE_ID_JOYPAD_L2] = 1 << DKEY_L2,
306 [RETRO_DEVICE_ID_JOYPAD_R2] = 1 << DKEY_R2,
307 [RETRO_DEVICE_ID_JOYPAD_L3] = 1 << DKEY_L3,
308 [RETRO_DEVICE_ID_JOYPAD_R3] = 1 << DKEY_R3,
309};
310#define RETRO_PSX_MAP_LEN (sizeof(retro_psx_map) / sizeof(retro_psx_map[0]))
311
312void retro_run(void)
313{
314 int i;
315
316 input_poll_cb();
317 in_keystate = 0;
318 for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
319 if (input_state_cb(1, RETRO_DEVICE_JOYPAD, 0, i))
320 in_keystate |= retro_psx_map[i];
321 in_keystate <<= 16;
322 for (i = 0; i < RETRO_PSX_MAP_LEN; i++)
323 if (input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i))
324 in_keystate |= retro_psx_map[i];
325
326 stop = 0;
327 psxCpu->Execute();
328
329 samples_to_send += 44100 / 60;
330}
331
332void retro_init(void)
333{
334 const char *bios[] = { "scph1001", "scph5501", "scph7001" };
335 const char *dir;
336 char path[256];
337 FILE *f = NULL;
338 int i, ret, level;
339
340 ret = emu_core_preinit();
341 ret |= emu_core_init();
342 if (ret != 0) {
343 printf("PCSX init failed, sorry\n");
344 exit(1);
345 }
346
347 vout_buf = malloc(640 * 512 * 2);
348
349 if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
350 {
351 snprintf(Config.BiosDir, sizeof(Config.BiosDir), "%s/", dir);
352
353 for (i = 0; i < sizeof(bios) / sizeof(bios[0]); i++) {
354 snprintf(path, sizeof(path), "%s/%s.bin", dir, bios[i]);
355 f = fopen(path, "r");
356 if (f != NULL) {
357 snprintf(Config.Bios, sizeof(Config.Bios), "%s.bin", bios[i]);
358 break;
359 }
360 }
361 }
362 if (f != NULL) {
363 printf("found BIOS file: %s\n", Config.Bios);
364 fclose(f);
365 }
366 else
367 printf("no BIOS files found.\n");
368
369 level = 1;
370 environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level);
371}
372
373void retro_deinit(void)
374{
375 SysClose();
376 free(vout_buf);
377 vout_buf = NULL;
378}
379