X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=platform%2Flibretro.c;h=72b7a1be126a84fedc4c10916dffcaff7d68681a;hb=f18d0e083ce89df07ef7a0c0cf5dc200638ad5d2;hp=f3252ab5623343cdc0ee5845d5e7454ca5ebb818;hpb=27f190ea0097fa6a6f51bfacfd38a984d797e59b;p=picodrive.git diff --git a/platform/libretro.c b/platform/libretro.c index f3252ab..72b7a1b 100644 --- a/platform/libretro.c +++ b/platform/libretro.c @@ -6,12 +6,18 @@ * See COPYING file in the top-level directory. */ +#ifdef PSP +#define NO_MMAP +#endif + #define _GNU_SOURCE 1 // mremap #include #include #include #ifndef _WIN32 +#ifndef NO_MMAP #include +#endif #else #include #include @@ -28,18 +34,17 @@ #include "common/version.h" #include "libretro.h" +static retro_log_printf_t log_cb; static retro_video_refresh_t video_cb; static retro_input_poll_t input_poll_cb; static retro_input_state_t input_state_cb; static retro_environment_t environ_cb; static retro_audio_sample_batch_t audio_batch_cb; -static FILE *emu_log; - #define VOUT_MAX_WIDTH 320 #define VOUT_MAX_HEIGHT 240 static void *vout_buf; -static int vout_width, vout_height; +static int vout_width, vout_height, vout_offset; static short __attribute__((aligned(4))) sndBuffer[2*44100/50]; @@ -162,6 +167,30 @@ static void munmap(void *addr, size_t length) UnmapViewOfFile(addr); /* ruh-ro, we leaked handle from CreateFileMapping() ... */ } +#elif defined(NO_MMAP) +#define PROT_EXEC 0x04 +#define MAP_FAILED 0 +#define PROT_READ 0 +#define PROT_WRITE 0 +#define MAP_PRIVATE 0 +#define MAP_ANONYMOUS 0 + +void* mmap(void *desired_addr, size_t len, int mmap_prot, int mmap_flags, int fildes, size_t off) +{ + return malloc(len); +} + +void munmap(void *base_addr, size_t len) +{ + free(base_addr); +} + +int mprotect(void *addr, size_t len, int prot) +{ + /* stub - not really needed at this point since this codepath has no dynarecs */ + return 0; +} + #endif #ifndef MAP_ANONYMOUS @@ -176,13 +205,15 @@ void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed) req = (void *)addr; ret = mmap(req, size, PROT_READ | PROT_WRITE, flags, -1, 0); if (ret == MAP_FAILED) { - lprintf("mmap(%08lx, %zd) failed: %d\n", addr, size, errno); + if (log_cb) + log_cb(RETRO_LOG_ERROR, "mmap(%08lx, %zd) failed: %d\n", addr, size, errno); return NULL; } if (addr != 0 && ret != (void *)addr) { - lprintf("warning: wanted to map @%08lx, got %p\n", - addr, ret); + if (log_cb) + log_cb(RETRO_LOG_WARN, "warning: wanted to map @%08lx, got %p\n", + addr, ret); if (is_fixed) { munmap(ret, size); @@ -236,12 +267,12 @@ int plat_mem_set_exec(void *ptr, size_t size) { #ifdef _WIN32 int ret = VirtualProtect(ptr,size,PAGE_EXECUTE_READWRITE,0); - if (ret == 0) - lprintf("mprotect(%p, %zd) failed: %d\n", ptr, size, 0); + if (ret == 0 && log_cb) + log_cb(RETRO_LOG_ERROR, "mprotect(%p, %zd) failed: %d\n", ptr, size, 0); #else int ret = mprotect(ptr, size, PROT_READ | PROT_WRITE | PROT_EXEC); - if (ret != 0) - lprintf("mprotect(%p, %zd) failed: %d\n", ptr, size, errno); + if (ret != 0 && log_cb) + log_cb(RETRO_LOG_ERROR, "mprotect(%p, %zd) failed: %d\n", ptr, size, errno); #endif return ret; } @@ -251,40 +282,26 @@ void emu_video_mode_change(int start_line, int line_count, int is_32cols) memset(vout_buf, 0, 320 * 240 * 2); vout_width = is_32cols ? 256 : 320; PicoDrawSetOutBuf(vout_buf, vout_width * 2); -} -void emu_32x_startup(void) -{ + vout_height = line_count; + vout_offset = vout_width * start_line; } -#ifndef ANDROID - -void lprintf(const char *fmt, ...) +void emu_32x_startup(void) { - va_list list; - - va_start(list, fmt); - fprintf(emu_log, "PicoDrive: "); - vfprintf(emu_log, fmt, list); - va_end(list); - fflush(emu_log); } -#else - -#include - void lprintf(const char *fmt, ...) { - va_list list; - - va_start(list, fmt); - __android_log_vprint(ANDROID_LOG_INFO, "PicoDrive", fmt, list); - va_end(list); + char buffer[256]; + va_list ap; + va_start(ap, fmt); + vsprintf(buffer, fmt, ap); + /* TODO - add 'level' param for warning/error messages? */ + if (log_cb) + log_cb(RETRO_LOG_INFO, "%s\n", fmt); } -#endif - /* libretro */ void retro_set_environment(retro_environment_t cb) { @@ -335,10 +352,10 @@ void retro_get_system_av_info(struct retro_system_av_info *info) info->timing.fps = Pico.m.pal ? 50 : 60; info->timing.sample_rate = 44100; info->geometry.base_width = 320; - info->geometry.base_height = 240; + info->geometry.base_height = vout_height; info->geometry.max_width = VOUT_MAX_WIDTH; info->geometry.max_height = VOUT_MAX_HEIGHT; - info->geometry.aspect_ratio = 4.0 / 3.0; + info->geometry.aspect_ratio = 0.0f; } /* savestates */ @@ -355,8 +372,9 @@ size_t state_read(void *p, size_t size, size_t nmemb, void *file) size_t bsize = size * nmemb; if (state->pos + bsize > state->size) { - lprintf("savestate error: %u/%u\n", - state->pos + bsize, state->size); + if (log_cb) + log_cb(RETRO_LOG_ERROR, "savestate error: %u/%u\n", + state->pos + bsize, state->size); bsize = state->size - state->pos; if ((int)bsize <= 0) return 0; @@ -373,8 +391,9 @@ size_t state_write(void *p, size_t size, size_t nmemb, void *file) size_t bsize = size * nmemb; if (state->pos + bsize > state->size) { - lprintf("savestate error: %u/%u\n", - state->pos + bsize, state->size); + if (log_cb) + log_cb(RETRO_LOG_ERROR, "savestate error: %u/%u\n", + state->pos + bsize, state->size); bsize = state->size - state->pos; if ((int)bsize <= 0) return 0; @@ -497,14 +516,15 @@ static unsigned int disk_get_image_index(void) static bool disk_set_image_index(unsigned int index) { - cd_img_type cd_type; + enum cd_img_type cd_type; int ret; if (index >= sizeof(disks) / sizeof(disks[0])) return false; if (disks[index].fname == NULL) { - lprintf("missing disk #%u\n", index); + if (log_cb) + log_cb(RETRO_LOG_ERROR, "missing disk #%u\n", index); // RetroArch specifies "no disk" with index == count, // so don't fail here.. @@ -512,15 +532,17 @@ static bool disk_set_image_index(unsigned int index) return true; } - lprintf("switching to disk %u: \"%s\"\n", index, - disks[index].fname); + if (log_cb) + log_cb(RETRO_LOG_INFO, "switching to disk %u: \"%s\"\n", index, + disks[index].fname); ret = -1; cd_type = PicoCdCheck(disks[index].fname, NULL); if (cd_type != CIT_NOT_CD) - ret = Insert_CD(disks[index].fname, cd_type); + ret = cdd_load(disks[index].fname, cd_type); if (ret != 0) { - lprintf("Load failed, invalid CD image?\n"); + if (log_cb) + log_cb(RETRO_LOG_ERROR, "Load failed, invalid CD image?\n"); return 0; } @@ -575,13 +597,15 @@ static struct retro_disk_control_callback disk_control = { static void disk_tray_open(void) { - lprintf("cd tray open\n"); + if (log_cb) + log_cb(RETRO_LOG_INFO, "cd tray open\n"); disk_ejected = 1; } static void disk_tray_close(void) { - lprintf("cd tray close\n"); + if (log_cb) + log_cb(RETRO_LOG_INFO, "cd tray close\n"); disk_ejected = 0; } @@ -643,7 +667,8 @@ static const char *find_bios(int *region, const char *cd_fname) } if (f != NULL) { - lprintf("using bios: %s\n", path); + if (log_cb) + log_cb(RETRO_LOG_INFO, "using bios: %s\n", path); fclose(f); return path; } @@ -659,12 +684,14 @@ bool retro_load_game(const struct retro_game_info *info) enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565; if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) { - lprintf("RGB565 support required, sorry\n"); + if (log_cb) + log_cb(RETRO_LOG_ERROR, "RGB565 support required, sorry\n"); return false; } if (info == NULL || info->path == NULL) { - lprintf("info->path required\n"); + if (log_cb) + log_cb(RETRO_LOG_ERROR, "info->path required\n"); return false; } @@ -686,16 +713,20 @@ bool retro_load_game(const struct retro_game_info *info) switch (media_type) { case PM_BAD_DETECT: - lprintf("Failed to detect ROM/CD image type.\n"); + if (log_cb) + log_cb(RETRO_LOG_ERROR, "Failed to detect ROM/CD image type.\n"); return false; case PM_BAD_CD: - lprintf("Invalid CD image\n"); + if (log_cb) + log_cb(RETRO_LOG_ERROR, "Invalid CD image\n"); return false; case PM_BAD_CD_NO_BIOS: - lprintf("Missing BIOS\n"); + if (log_cb) + log_cb(RETRO_LOG_ERROR, "Missing BIOS\n"); return false; case PM_ERROR: - lprintf("Load error\n"); + if (log_cb) + log_cb(RETRO_LOG_ERROR, "Load error\n"); return false; default: break; @@ -738,14 +769,25 @@ void *retro_get_memory_data(unsigned id) size_t retro_get_memory_size(unsigned id) { + unsigned int i; + int sum; + if (id != RETRO_MEMORY_SAVE_RAM) return 0; if (PicoAHW & PAHW_MCD) // bram return 0x2000; - else + + if (Pico.m.frame_count == 0) return SRam.size; + + // if game doesn't write to sram, don't report it to + // libretro so that RA doesn't write out zeroed .srm + for (i = 0, sum = 0; i < SRam.size; i++) + sum |= SRam.data[i]; + + return (sum != 0) ? SRam.size : 0; } void retro_reset(void) @@ -783,7 +825,8 @@ static enum input_device input_name_to_val(const char *name) if (strcmp(name, "None") == 0) return PICO_INPUT_NOTHING; - lprintf("invalid picodrive_input: '%s'\n", name); + if (log_cb) + log_cb(RETRO_LOG_WARN, "invalid picodrive_input: '%s'\n", name); return PICO_INPUT_PAD_3BTN; } @@ -849,24 +892,23 @@ void retro_run(void) PicoFrame(); - video_cb(vout_buf, vout_width, vout_height, vout_width * 2); + video_cb((short *)vout_buf + vout_offset, + vout_width, vout_height, vout_width * 2); } void retro_init(void) { + struct retro_log_callback log; int level; -#ifdef IOS - emu_log = fopen("/User/Documents/PicoDrive.log", "w"); - if (emu_log == NULL) - emu_log = fopen("PicoDrive.log", "w"); - if (emu_log == NULL) -#endif - emu_log = stdout; - level = 0; environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level); + if (environ_cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &log)) + log_cb = log.log; + else + log_cb = NULL; + environ_cb(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE, &disk_control); PicoOpt = POPT_EN_STEREO|POPT_EN_FM|POPT_EN_PSG|POPT_EN_Z80 @@ -878,7 +920,6 @@ void retro_init(void) #endif PsndRate = 44100; PicoAutoRgnOrder = 0x184; // US, EU, JP - PicoCDBuffers = 0; vout_width = 320; vout_height = 240;