libretro: try to fix enhancement
[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
805fe9bc 8#define _GNU_SOURCE 1 // strcasestr
38c2028e 9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12
13#include "../libpcsxcore/misc.h"
14#include "../libpcsxcore/psxcounters.h"
87e5b45f 15#include "../libpcsxcore/psxmem_map.h"
38c2028e 16#include "../libpcsxcore/new_dynarec/new_dynarec.h"
23ea11bd 17#include "../libpcsxcore/cdrom.h"
18#include "../libpcsxcore/cdriso.h"
6e921e1d 19#include "../libpcsxcore/cheat.h"
07c13dfd 20#include "../plugins/dfsound/out.h"
805fe9bc 21#include "../plugins/dfinput/externals.h"
c82f907a 22#include "cspace.h"
38c2028e 23#include "main.h"
24#include "plugin.h"
25#include "plugin_lib.h"
26#include "revision.h"
27#include "libretro.h"
28
29static retro_video_refresh_t video_cb;
30static retro_input_poll_t input_poll_cb;
31static retro_input_state_t input_state_cb;
32static retro_environment_t environ_cb;
33static retro_audio_sample_batch_t audio_batch_cb;
34
35static void *vout_buf;
70d56ca3 36static int vout_width, vout_height;
37static int vout_doffs_old, vout_fb_dirty;
1a6164a1 38static bool vout_can_dupe;
70d56ca3 39
38c2028e 40static int samples_sent, samples_to_send;
41static int plugins_opened;
17f84149 42static int is_pal_mode;
38c2028e 43
f9f60dae
TK
44/* memory card data */
45extern char Mcd1Data[MCD_SIZE];
46extern char McdDisable[2];
38c2028e 47
48/* PCSX ReARMed core calls and stuff */
49int in_type1, in_type2;
50int in_a1[2] = { 127, 127 }, in_a2[2] = { 127, 127 };
51int in_keystate;
52int in_enable_vibration;
53
805fe9bc 54/* PSX max resolution is 640x512, but with enhancement it's 1024x512 */
55#define VOUT_MAX_WIDTH 1024
56#define VOUT_MAX_HEIGHT 512
57
7df396ea
TK
58static void init_memcard(char *mcd_data)
59{
60 unsigned off = 0;
61 unsigned i;
62
63 memset(mcd_data, 0, MCD_SIZE);
64
65 mcd_data[off++] = 'M';
66 mcd_data[off++] = 'C';
67 off += 0x7d;
68 mcd_data[off++] = 0x0e;
69
70 for (i = 0; i < 15; i++) {
71 mcd_data[off++] = 0xa0;
72 off += 0x07;
73 mcd_data[off++] = 0xff;
74 mcd_data[off++] = 0xff;
75 off += 0x75;
76 mcd_data[off++] = 0xa0;
77 }
78
79 for (i = 0; i < 20; i++) {
80 mcd_data[off++] = 0xff;
81 mcd_data[off++] = 0xff;
82 mcd_data[off++] = 0xff;
83 mcd_data[off++] = 0xff;
84 off += 0x04;
85 mcd_data[off++] = 0xff;
86 mcd_data[off++] = 0xff;
87 off += 0x76;
88 }
89}
90
38c2028e 91static int vout_open(void)
92{
93 return 0;
94}
95
e4c83ca6 96static void vout_set_mode(int w, int h, int raw_w, int raw_h, int bpp)
38c2028e 97{
70d56ca3 98 vout_width = w;
99 vout_height = h;
38c2028e 100}
101
cc4f9b70 102#ifndef FRONTEND_SUPPORTS_RGB565
9c59f120 103static void convert(void *buf, size_t bytes)
38c2028e 104{
105 unsigned int i, v, *p = buf;
106
9c59f120 107 for (i = 0; i < bytes / 4; i++) {
38c2028e 108 v = p[i];
109 p[i] = (v & 0x001f001f) | ((v >> 1) & 0x7fe07fe0);
110 }
111}
46aa5b98 112#endif
38c2028e 113
fa56d360 114static void vout_flip(const void *vram, int stride, int bgr24, int w, int h)
38c2028e 115{
fa56d360 116 unsigned short *dest = vout_buf;
117 const unsigned short *src = vram;
70d56ca3 118 int dstride = vout_width, h1 = h;
119 int doffs;
38c2028e 120
fa56d360 121 if (vram == NULL) {
122 // blanking
cc56203b 123 memset(vout_buf, 0, dstride * h * 2);
fa56d360 124 goto out;
125 }
126
70d56ca3 127 doffs = (vout_height - h) * dstride;
128 doffs += (dstride - w) / 2 & ~1;
129 if (doffs != vout_doffs_old) {
130 // clear borders
131 memset(vout_buf, 0, dstride * h * 2);
132 vout_doffs_old = doffs;
133 }
134 dest += doffs;
135
fa56d360 136 if (bgr24)
137 {
138 // XXX: could we switch to RETRO_PIXEL_FORMAT_XRGB8888 here?
139 for (; h1-- > 0; dest += dstride, src += stride)
140 {
141 bgr888_to_rgb565(dest, src, w * 3);
142 }
143 }
144 else
145 {
146 for (; h1-- > 0; dest += dstride, src += stride)
147 {
148 bgr555_to_rgb565(dest, src, w * 2);
149 }
150 }
38c2028e 151
fa56d360 152out:
46aa5b98 153#ifndef FRONTEND_SUPPORTS_RGB565
70d56ca3 154 convert(vout_buf, vout_width * vout_height * 2);
46aa5b98 155#endif
70d56ca3 156 vout_fb_dirty = 1;
fa56d360 157 pl_rearmed_cbs.flip_cnt++;
38c2028e 158}
159
160static void vout_close(void)
161{
162}
163
cc56203b 164static void *pl_mmap(unsigned int size)
165{
87e5b45f 166 return psxMap(0, size, 0, MAP_TAG_VRAM);
cc56203b 167}
168
169static void pl_munmap(void *ptr, unsigned int size)
170{
87e5b45f 171 psxUnmap(ptr, size, MAP_TAG_VRAM);
cc56203b 172}
173
38c2028e 174struct rearmed_cbs pl_rearmed_cbs = {
175 .pl_vout_open = vout_open,
176 .pl_vout_set_mode = vout_set_mode,
177 .pl_vout_flip = vout_flip,
178 .pl_vout_close = vout_close,
cc56203b 179 .mmap = pl_mmap,
180 .munmap = pl_munmap,
38c2028e 181 /* from psxcounters */
182 .gpu_hcnt = &hSyncCount,
183 .gpu_frame_count = &frame_counter,
184};
185
186void pl_frame_limit(void)
187{
188 /* called once per frame, make psxCpu->Execute() above return */
189 stop = 1;
190}
191
192void pl_timing_prepare(int is_pal)
193{
17f84149 194 is_pal_mode = is_pal;
38c2028e 195}
196
197void plat_trigger_vibrate(int is_strong)
198{
199}
200
e4c83ca6 201void pl_update_gun(int *xn, int *yn, int *xres, int *yres, int *in)
38c2028e 202{
203}
204
205/* sound calls */
07c13dfd 206static int snd_init(void)
38c2028e 207{
07c13dfd 208 return 0;
38c2028e 209}
210
07c13dfd 211static void snd_finish(void)
38c2028e 212{
213}
214
07c13dfd 215static int snd_busy(void)
38c2028e 216{
217 if (samples_to_send > samples_sent)
218 return 0; /* give more samples */
219 else
220 return 1;
221}
222
07c13dfd 223static void snd_feed(void *buf, int bytes)
38c2028e 224{
225 audio_batch_cb(buf, bytes / 4);
226 samples_sent += bytes / 4;
227}
228
07c13dfd 229void out_register_libretro(struct out_driver *drv)
230{
231 drv->name = "libretro";
232 drv->init = snd_init;
233 drv->finish = snd_finish;
234 drv->busy = snd_busy;
235 drv->feed = snd_feed;
236}
237
38c2028e 238/* libretro */
e268a47e 239void retro_set_environment(retro_environment_t cb)
240{
e268a47e 241 static const struct retro_variable vars[] = {
7d68d030 242 { "frameskip", "Frameskip; 0|1|2|3" },
9f5a28d9 243 { "region", "Region; Auto|NTSC|PAL" },
7d68d030 244#ifdef __ARM_NEON__
9f5a28d9 245 { "neon_interlace_enable", "Enable interlacing mode(s); disabled|enabled" },
094ec204 246#if 0
e268a47e 247 { "neon_enhancement_enable", "Enhanced resolution (slow); disabled|enabled" },
094ec204 248#endif
7d68d030 249#endif
e268a47e 250 { NULL, NULL },
251 };
252
253 environ_cb = cb;
254
255 cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void*)vars);
e268a47e 256}
257
38c2028e 258void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
259void retro_set_audio_sample(retro_audio_sample_t cb) { (void)cb; }
260void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { audio_batch_cb = cb; }
261void retro_set_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
262void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
263
264unsigned retro_api_version(void)
265{
266 return RETRO_API_VERSION;
267}
268
269void retro_set_controller_port_device(unsigned port, unsigned device)
270{
271}
272
273void retro_get_system_info(struct retro_system_info *info)
274{
275 memset(info, 0, sizeof(*info));
276 info->library_name = "PCSX-ReARMed";
0ed87574 277 info->library_version = "r19";
9fba39a9 278 info->valid_extensions = "bin|cue|img|mdf|pbp|toc|cbn|m3u";
38c2028e 279 info->need_fullpath = true;
280}
281
282void retro_get_system_av_info(struct retro_system_av_info *info)
283{
284 memset(info, 0, sizeof(*info));
17f84149 285 info->timing.fps = is_pal_mode ? 50 : 60;
38c2028e 286 info->timing.sample_rate = 44100;
287 info->geometry.base_width = 320;
288 info->geometry.base_height = 240;
805fe9bc 289 info->geometry.max_width = VOUT_MAX_WIDTH;
290 info->geometry.max_height = VOUT_MAX_HEIGHT;
38c2028e 291 info->geometry.aspect_ratio = 4.0 / 3.0;
292}
293
6e921e1d 294/* savestates */
38c2028e 295size_t retro_serialize_size(void)
296{
6e921e1d 297 // it's currently 4380651 bytes, but have some reserved for future
298 return 0x430000;
299}
300
301struct save_fp {
302 char *buf;
303 size_t pos;
304 int is_write;
305};
306
307static void *save_open(const char *name, const char *mode)
308{
309 struct save_fp *fp;
310
311 if (name == NULL || mode == NULL)
312 return NULL;
313
314 fp = malloc(sizeof(*fp));
315 if (fp == NULL)
316 return NULL;
317
318 fp->buf = (char *)name;
319 fp->pos = 0;
320 fp->is_write = (mode[0] == 'w' || mode[1] == 'w');
321
322 return fp;
323}
324
325static int save_read(void *file, void *buf, u32 len)
326{
327 struct save_fp *fp = file;
328 if (fp == NULL || buf == NULL)
329 return -1;
330
331 memcpy(buf, fp->buf + fp->pos, len);
332 fp->pos += len;
333 return len;
334}
335
336static int save_write(void *file, const void *buf, u32 len)
337{
338 struct save_fp *fp = file;
339 if (fp == NULL || buf == NULL)
340 return -1;
341
342 memcpy(fp->buf + fp->pos, buf, len);
343 fp->pos += len;
344 return len;
345}
346
347static long save_seek(void *file, long offs, int whence)
348{
349 struct save_fp *fp = file;
350 if (fp == NULL)