| 1 | #include <sys/types.h> |
| 2 | |
| 3 | #include <libretro.h> |
| 4 | #include <retro_math.h> |
| 5 | |
| 6 | #include "libretro_core_options.h" |
| 7 | |
| 8 | #include "mednafen/settings.h" |
| 9 | #include "mednafen/git.h" |
| 10 | #include "mednafen/wswan/wswan.h" |
| 11 | #include "mednafen/mempatcher.h" |
| 12 | #include "mednafen/wswan/gfx.h" |
| 13 | #include "mednafen/wswan/interrupt.h" |
| 14 | #include "mednafen/wswan/wswan-memory.h" |
| 15 | #include "mednafen/wswan/start.inc" |
| 16 | #include "mednafen/wswan/sound.h" |
| 17 | #include "mednafen/wswan/v30mz.h" |
| 18 | #include "mednafen/wswan/rtc.h" |
| 19 | #include "mednafen/wswan/eeprom.h" |
| 20 | |
| 21 | #if defined(_3DS) |
| 22 | void* linearMemAlign(size_t size, size_t alignment); |
| 23 | void linearFree(void* mem); |
| 24 | #endif |
| 25 | |
| 26 | #define MEDNAFEN_CORE_NAME_MODULE "wswan" |
| 27 | #define MEDNAFEN_CORE_NAME "Beetle WonderSwan" |
| 28 | #define MEDNAFEN_CORE_VERSION "v0.9.35.1" |
| 29 | #define MEDNAFEN_CORE_EXTENSIONS "ws|wsc|pc2" |
| 30 | #define MEDNAFEN_CORE_TIMING_FPS (3072000.0 / (159.0 * 256.0)) |
| 31 | #define MEDNAFEN_CORE_GEOMETRY_BASE_W 224 |
| 32 | #define MEDNAFEN_CORE_GEOMETRY_BASE_H 144 |
| 33 | #define MEDNAFEN_CORE_GEOMETRY_MAX_W 224 |
| 34 | #define MEDNAFEN_CORE_GEOMETRY_MAX_H 144 |
| 35 | #define MEDNAFEN_CORE_GEOMETRY_ASPECT_RATIO (14.0 / 9.0) |
| 36 | #define FB_WIDTH 224 |
| 37 | #define FB_HEIGHT 144 |
| 38 | #define FB_MAX_HEIGHT FB_HEIGHT |
| 39 | |
| 40 | /* Forward declarations */ |
| 41 | void MDFN_LoadGameCheats(void *override_ptr); |
| 42 | void MDFN_FlushGameCheats(int nosave); |
| 43 | |
| 44 | /* core options */ |
| 45 | static int RETRO_SAMPLE_RATE = 44100; |
| 46 | |
| 47 | static int RETRO_PIX_BYTES = 2; |
| 48 | static int RETRO_PIX_DEPTH = 15; |
| 49 | |
| 50 | struct retro_perf_callback perf_cb; |
| 51 | retro_log_printf_t log_cb; |
| 52 | static retro_video_refresh_t video_cb; |
| 53 | static retro_audio_sample_t audio_cb; |
| 54 | static retro_audio_sample_batch_t audio_batch_cb; |
| 55 | static retro_environment_t environ_cb; |
| 56 | static retro_input_poll_t input_poll_cb; |
| 57 | static retro_input_state_t input_state_cb; |
| 58 | |
| 59 | static bool libretro_supports_bitmasks = false; |
| 60 | |
| 61 | typedef enum |
| 62 | { |
| 63 | DISPLAY_ROTATION_MANUAL = 0, |
| 64 | DISPLAY_ROTATION_PORTRAIT, |
| 65 | DISPLAY_ROTATION_LANDSCAPE |
| 66 | } retro_display_rotation_t; |
| 67 | |
| 68 | static retro_display_rotation_t retro_display_rotation = DISPLAY_ROTATION_MANUAL; |
| 69 | |
| 70 | static bool rotate_tall = false; |
| 71 | static bool select_pressed_last_frame = false; |
| 72 | static bool hw_rotate_enabled = false; |
| 73 | |
| 74 | static unsigned rotate_joymap = 0; |
| 75 | |
| 76 | static MDFN_Surface *surf = NULL; |
| 77 | static uint16_t *rotate_buf = NULL; |
| 78 | |
| 79 | #define ROTATE_PIXEL_BUF(typename_t, src, width, height, dst) \ |
| 80 | { \ |
| 81 | typename_t *in_ptr = (typename_t*)src; \ |
| 82 | typename_t *out_ptr = (typename_t*)dst; \ |
| 83 | size_t x, y; \ |
| 84 | for (x = 0; x < width; x++) \ |
| 85 | for (y = 0; y < height; y++) \ |
| 86 | *(out_ptr + y + (((width - 1) - x) * height)) = *(in_ptr + x + (y * width)); \ |
| 87 | } |
| 88 | |
| 89 | static int16_t *audio_samples_buf = NULL; |
| 90 | static int32_t audio_samples_buf_size = 0; |
| 91 | |
| 92 | #define RETRO_60HZ_FPS ((4.0 * MEDNAFEN_CORE_TIMING_FPS) / 5.0) |
| 93 | #define RETRO_60HZ_CYCLE_INDEX 4 |
| 94 | |
| 95 | typedef struct |
| 96 | { |
| 97 | int16_t *samples_buf; |
| 98 | int32_t samples_buf_size; |
| 99 | int32_t samples_buf_pos; |
| 100 | int32_t samples_per_frame; |
| 101 | } retro_60hz_audio_t; |
| 102 | |
| 103 | static bool retro_60hz_enabled = false; |
| 104 | static uint16_t retro_60hz_counter = 0; |
| 105 | static retro_60hz_audio_t retro_60hz_audio = {0}; |
| 106 | |
| 107 | static void retro_60hz_deinit(void) |
| 108 | { |
| 109 | if (retro_60hz_audio.samples_buf) |
| 110 | free(retro_60hz_audio.samples_buf); |
| 111 | |
| 112 | retro_60hz_audio.samples_buf = NULL; |
| 113 | retro_60hz_audio.samples_buf_size = 0; |
| 114 | retro_60hz_audio.samples_buf_pos = 0; |
| 115 | retro_60hz_audio.samples_per_frame = 0; |
| 116 | |
| 117 | retro_60hz_counter = 0; |
| 118 | } |
| 119 | |
| 120 | static void retro_60hz_init(void) |
| 121 | { |
| 122 | retro_60hz_deinit(); |
| 123 | |
| 124 | if (retro_60hz_enabled) |
| 125 | { |
| 126 | /* Get expected number of samples per 60Hz |
| 127 | * frame |
| 128 | * > Round down - any excess samples will be read |
| 129 | * out at the end of each 4/5 frame cycle */ |
| 130 | retro_60hz_audio.samples_per_frame = ((int32_t)(RETRO_SAMPLE_RATE / |
| 131 | RETRO_60HZ_FPS)); |
| 132 | |
| 133 | /* Initial buffer size should be *twice* that of |
| 134 | * the expected number of samples per 75Hz frame */ |
| 135 | retro_60hz_audio.samples_buf_size = ((int32_t)(RETRO_SAMPLE_RATE / |
| 136 | MEDNAFEN_CORE_TIMING_FPS) + 1) << 2; |
| 137 | retro_60hz_audio.samples_buf = (int16_t*)malloc( |
| 138 | retro_60hz_audio.samples_buf_size * sizeof(int16_t)); |
| 139 | |
| 140 | if (!retro_60hz_audio.samples_buf) |
| 141 | { |
| 142 | retro_60hz_deinit(); |
| 143 | retro_60hz_enabled = false; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | static void retro_60hz_cache_audio_samples(int16_t *samples, int32_t frames) |
| 149 | { |
| 150 | int32_t buffer_capacity = retro_60hz_audio.samples_buf_size - |
| 151 | retro_60hz_audio.samples_buf_pos; |
| 152 | int32_t samples_to_write = frames << 1; |
| 153 | |
| 154 | /* Resize buffer if necessary */ |
| 155 | if (buffer_capacity < samples_to_write) |
| 156 | { |
| 157 | int16_t *tmp_buffer = NULL; |
| 158 | int32_t tmp_buffer_size; |
| 159 | |
| 160 | tmp_buffer_size = retro_60hz_audio.samples_buf_size + |
| 161 | (samples_to_write - buffer_capacity); |
| 162 | tmp_buffer_size = (tmp_buffer_size << 1) - (tmp_buffer_size >> 1); |
| 163 | tmp_buffer = (int16_t *)malloc(tmp_buffer_size * sizeof(int16_t)); |
| 164 | |
| 165 | memcpy(tmp_buffer, retro_60hz_audio.samples_buf, |
| 166 | retro_60hz_audio.samples_buf_pos * sizeof(int16_t)); |
| 167 | |
| 168 | free(retro_60hz_audio.samples_buf); |
| 169 | retro_60hz_audio.samples_buf = tmp_buffer; |
| 170 | retro_60hz_audio.samples_buf_size = tmp_buffer_size; |
| 171 | } |
| 172 | |
| 173 | /* Copy samples */ |
| 174 | memcpy(retro_60hz_audio.samples_buf + |
| 175 | retro_60hz_audio.samples_buf_pos, |
| 176 | samples, samples_to_write * sizeof(int16_t)); |
| 177 | retro_60hz_audio.samples_buf_pos += samples_to_write; |
| 178 | } |
| 179 | |
| 180 | static bool audio_low_pass_enabled = false; |
| 181 | static int64_t audio_low_pass_acc_left = 0; |
| 182 | static int64_t audio_low_pass_acc_right = 0; |
| 183 | |
| 184 | static void audio_low_pass_apply(int16_t *samples, int32_t frames) |
| 185 | { |
| 186 | while (frames-- > 0) |
| 187 | { |
| 188 | int64_t drop_current_left; |
| 189 | int64_t drop_current_right; |
| 190 | |
| 191 | /* Left channel */ |
| 192 | drop_current_left = ((*samples << 16) - audio_low_pass_acc_left) >> 3; |
| 193 | audio_low_pass_acc_left += drop_current_left; |
| 194 | *samples++ = (int16_t)((audio_low_pass_acc_left >> 16) & 0xFFFF); |
| 195 | |
| 196 | /* Right channel */ |
| 197 | drop_current_right = ((*samples << 16) - audio_low_pass_acc_right) >> 3; |
| 198 | audio_low_pass_acc_right += drop_current_right; |
| 199 | *samples++ = (int16_t)((audio_low_pass_acc_right >> 16) & 0xFFFF); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /* Mono palettes */ |
| 204 | |
| 205 | struct ws_mono_palette |
| 206 | { |
| 207 | const char *name; |
| 208 | uint32 start; |
| 209 | uint32 end; |
| 210 | }; |
| 211 | |
| 212 | static struct ws_mono_palette ws_mono_palettes[] = { |
| 213 | { "default", 0x000000, 0xFFFFFF }, |
| 214 | { "wonderswan", 0x3E3D20, 0x9B9D66 }, |
| 215 | { "wondeswan_color", 0x1B201E, 0xD7D49D }, |
| 216 | { "swancrystal", 0x151108, 0xFFFCCA }, |
| 217 | { "gb_dmg", 0x00420C, 0x578200 }, |
| 218 | { "gb_pocket", 0x2A3325, 0xA7B19A }, |
| 219 | { "gb_light", 0x00778D, 0x01CBDF }, |
| 220 | { "blossom_pink", 0x180F0F, 0xF09898 }, |
| 221 | { "bubbles_blue", 0x0D1418, 0x88D0F0 }, |
| 222 | { "buttercup_green", 0x12160D, 0xB8E088 }, |
| 223 | { "digivice", 0x000000, 0x8C8C73 }, |
| 224 | { "game_com", 0x000000, 0xA7BF6B }, |
| 225 | { "gameking", 0x184221, 0x8CCE94 }, |
| 226 | { "game_master", 0x2D2D2B, 0x829FA6 }, |
| 227 | { "golden_wild", 0x120F0A, 0xB99F65 }, |
| 228 | { "greenscale", 0x0C360C, 0x9CBE0C }, |
| 229 | { "hokage_orange", 0x170D08, 0xEA8352 }, |
| 230 | { "labo_fawn", 0x15110B, 0xD7AA73 }, |
| 231 | { "legendary_super_saiyan", 0x101509, 0xA5DB5A }, |
| 232 | { "microvision", 0x303030, 0xA0A0A0 }, |
| 233 | { "million_live_gold", 0x141109, 0xCDB261 }, |
| 234 | { "odyssey_gold", 0x131000, 0xC2A000 }, |
| 235 | { "shiny_sky_blue", 0x0E1216, 0x8CB6DF }, |
| 236 | { "slime_blue", 0x040E14, 0x2F8CCC }, |
| 237 | { "ti_83", 0x181810, 0x9CA684 }, |
| 238 | { "travel_wood", 0x482810, 0xF8D8B0 }, |
| 239 | { "virtual_boy", 0x000000, 0xE30000 }, |
| 240 | { NULL, 0, 0 }, |
| 241 | }; |
| 242 | |
| 243 | static uint32 mono_pal_start = 0x000000; |
| 244 | static uint32 mono_pal_end = 0xFFFFFF; |
| 245 | |
| 246 | static bool find_mono_palette(const char* name, |
| 247 | uint32 *start, uint32 *end) |
| 248 | { |
| 249 | bool palette_found = false; |
| 250 | unsigned i; |
| 251 | |
| 252 | for (i = 0; ws_mono_palettes[i].name; i++) |
| 253 | { |
| 254 | if (!strcmp(ws_mono_palettes[i].name, name)) |
| 255 | { |
| 256 | *start = ws_mono_palettes[i].start; |
| 257 | *end = ws_mono_palettes[i].end; |
| 258 | palette_found = true; |
| 259 | break; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | if (!palette_found) |
| 264 | { |
| 265 | *start = 0x000000; |
| 266 | *end = 0xFFFFFF; |
| 267 | } |
| 268 | |
| 269 | return palette_found; |
| 270 | } |
| 271 | |
| 272 | /* Frameskipping */ |
| 273 | |
| 274 | static unsigned frameskip_type = 0; |
| 275 | static unsigned frameskip_threshold = 0; |
| 276 | static uint16_t frameskip_counter = 0; |
| 277 | |
| 278 | static bool retro_audio_buff_active = false; |
| 279 | static unsigned retro_audio_buff_occupancy = 0; |
| 280 | static bool retro_audio_buff_underrun = false; |
| 281 | /* Maximum number of consecutive frames that |
| 282 | * can be skipped */ |
| 283 | #define FRAMESKIP_MAX 30 |
| 284 | |
| 285 | static unsigned audio_latency = 0; |
| 286 | static bool update_audio_latency = false; |
| 287 | |
| 288 | static void retro_audio_buff_status_cb( |
| 289 | bool active, unsigned occupancy, bool underrun_likely) |
| 290 | { |
| 291 | retro_audio_buff_active = active; |
| 292 | retro_audio_buff_occupancy = occupancy; |
| 293 | retro_audio_buff_underrun = underrun_likely; |
| 294 | } |
| 295 | |
| 296 | static void init_frameskip(void) |
| 297 | { |
| 298 | if (frameskip_type > 0) |
| 299 | { |
| 300 | struct retro_audio_buffer_status_callback buf_status_cb; |
| 301 | |
| 302 | buf_status_cb.callback = retro_audio_buff_status_cb; |
| 303 | if (!environ_cb(RETRO_ENVIRONMENT_SET_AUDIO_BUFFER_STATUS_CALLBACK, |
| 304 | &buf_status_cb)) |
| 305 | { |
| 306 | if (log_cb) |
| 307 | log_cb(RETRO_LOG_WARN, "Frameskip disabled - frontend does not support audio buffer status monitoring.\n"); |
| 308 | |
| 309 | retro_audio_buff_active = false; |
| 310 | retro_audio_buff_occupancy = 0; |
| 311 | retro_audio_buff_underrun = false; |
| 312 | audio_latency = 0; |
| 313 | } |
| 314 | else |
| 315 | { |
| 316 | float fps; |
| 317 | float latency_factor; |
| 318 | float frame_time_msec; |
| 319 | |
| 320 | if (retro_60hz_enabled) |
| 321 | { |
| 322 | fps = (float)RETRO_60HZ_FPS; |
| 323 | latency_factor = 6.0f; |
| 324 | } |
| 325 | else |
| 326 | { |
| 327 | fps = (float)MEDNAFEN_CORE_TIMING_FPS; |
| 328 | latency_factor = 8.0f; |
| 329 | } |
| 330 | |
| 331 | /* Frameskip is enabled - increase frontend |
| 332 | * audio latency to minimise potential |
| 333 | * buffer underruns */ |
| 334 | frame_time_msec = 1000.0f / fps; |
| 335 | |
| 336 | /* Set latency to an integer multiple of |
| 337 | * the current frame time... |
| 338 | * > At 60Hz we normally use a 6x multiplier |
| 339 | * > The native (unusually high) 75Hz of the |
| 340 | * WonderSwan seems to place greater demands |
| 341 | * on the frontend, so we increase the |
| 342 | * multiplier to 8x; this gives the frontend |
| 343 | * more room to manoeuvre, and improves the |
| 344 | * efficacy of the 'Auto' frameskip setting */ |
| 345 | audio_latency = (unsigned)((latency_factor * frame_time_msec) + 0.5f); |
| 346 | |
| 347 | /* ...then round up to nearest multiple of 32 */ |
| 348 | audio_latency = (audio_latency + 0x1F) & ~0x1F; |
| 349 | } |
| 350 | } |
| 351 | else |
| 352 | { |
| 353 | environ_cb(RETRO_ENVIRONMENT_SET_AUDIO_BUFFER_STATUS_CALLBACK, NULL); |
| 354 | audio_latency = 0; |
| 355 | } |
| 356 | |
| 357 | update_audio_latency = true; |
| 358 | } |
| 359 | |
| 360 | /* Cygne |
| 361 | * |
| 362 | * Copyright notice for this file: |
| 363 | * Copyright (C) 2002 Dox [email protected] |
| 364 | * |
| 365 | * This program is free software; you can redistribute it and/or modify |
| 366 | * it under the terms of the GNU General Public License as published by |
| 367 | * the Free Software Foundation; either version 2 of the License, or |
| 368 | * (at your option) any later version. |
| 369 | * |
| 370 | * This program is distributed in the hope that it will be useful, |
| 371 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 372 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 373 | * GNU General Public License for more details. |
| 374 | * |
| 375 | * You should have received a copy of the GNU General Public License |
| 376 | * along with this program; if not, write to the Free Software |
| 377 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 378 | */ |
| 379 | |
| 380 | int wsc = 1; /*color/mono*/ |
| 381 | uint32 rom_size; |
| 382 | |
| 383 | uint16 WSButtonStatus; |
| 384 | |
| 385 | static void Reset(void) |
| 386 | { |
| 387 | int u0; |
| 388 | |
| 389 | v30mz_reset(); /* Reset CPU */ |
| 390 | WSwan_MemoryReset(); |
| 391 | WSwan_GfxReset(); |
| 392 | WSwan_SoundReset(); |
| 393 | WSwan_InterruptReset(); |
| 394 | WSwan_RTCReset(); |
| 395 | WSwan_EEPROMReset(); |
| 396 | |
| 397 | for(u0=0;u0<0xc9;u0++) |
| 398 | { |
| 399 | if(u0 != 0xC4 && u0 != 0xC5 && u0 != 0xBA && u0 != 0xBB) |
| 400 | WSwan_writeport(u0,startio[u0]); |
| 401 | } |
| 402 | |
| 403 | v30mz_set_reg(NEC_SS,0); |
| 404 | v30mz_set_reg(NEC_SP,0x2000); |
| 405 | } |
| 406 | |
| 407 | static uint8 *chee = NULL; |
| 408 | |
| 409 | static void Emulate(EmulateSpecStruct *espec, |
| 410 | int skip_frame, int update_sample_rate) |
| 411 | { |
| 412 | espec->surface = surf; |
| 413 | espec->DisplayRect.w = 224; |
| 414 | espec->DisplayRect.h = 144; |
| 415 | espec->skip = skip_frame; |
| 416 | espec->SoundBufSize = 0; |
| 417 | |
| 418 | if (update_sample_rate) |
| 419 | WSwan_SetSoundRate(RETRO_SAMPLE_RATE); |
| 420 | |
| 421 | WSButtonStatus = chee[0] | (chee[1] << 8); |
| 422 | |
| 423 | MDFNMP_ApplyPeriodicCheats(); |
| 424 | |
| 425 | while (!wsExecuteLine(espec->surface, espec->skip)); |
| 426 | |
| 427 | espec->SoundBufSize = WSwan_SoundFlush(&audio_samples_buf, |
| 428 | &audio_samples_buf_size); |
| 429 | |
| 430 | if (audio_low_pass_enabled) |
| 431 | audio_low_pass_apply(audio_samples_buf, |
| 432 | espec->SoundBufSize); |
| 433 | |
| 434 | v30mz_timestamp = 0; |
| 435 | } |
| 436 | |
| 437 | #if 0 |
| 438 | typedef struct |
| 439 | { |
| 440 | const uint8 id; |
| 441 | const char *name; |
| 442 | } DLEntry; |
| 443 | |
| 444 | static const DLEntry Developers[] = |
| 445 | { |
| 446 | { 0x01, "Bandai" }, |
| 447 | { 0x02, "Taito" }, |
| 448 | { 0x03, "Tomy" }, |
| 449 | { 0x04, "Koei" }, |
| 450 | { 0x05, "Data East" }, |
| 451 | { 0x06, "Asmik" }, /* Asmik Ace? */ |
| 452 | { 0x07, "Media Entertainment" }, |
| 453 | { 0x08, "Nichibutsu" }, |
| 454 | { 0x0A, "Coconuts Japan" }, |
| 455 | { 0x0B, "Sammy" }, |
| 456 | { 0x0C, "Sunsoft" }, |
| 457 | { 0x0D, "Mebius" }, |
| 458 | { 0x0E, "Banpresto" }, |
| 459 | { 0x10, "Jaleco" }, |
| 460 | { 0x11, "Imagineer" }, |
| 461 | { 0x12, "Konami" }, |
| 462 | { 0x16, "Kobunsha" }, |
| 463 | { 0x17, "Bottom Up" }, |
| 464 | { 0x18, "Kaga Tech" }, |
| 465 | { 0x19, "Sunrise" }, |
| 466 | { 0x1A, "Cyber Front" }, |
| 467 | { 0x1B, "Mega House" }, |
| 468 | { 0x1D, "Interbec" }, |
| 469 | { 0x1E, "Nihon Application" }, |
| 470 | { 0x1F, "Bandai Visual" }, |
| 471 | { 0x20, "Athena" }, |
| 472 | { 0x21, "KID" }, |
| 473 | { 0x22, "HAL Corporation" }, |
| 474 | { 0x23, "Yuki Enterprise" }, |
| 475 | { 0x24, "Omega Micott" }, |
| 476 | { 0x25, "Layup" }, |
| 477 | { 0x26, "Kadokawa Shoten" }, |
| 478 | { 0x27, "Shall Luck" }, |
| 479 | { 0x28, "Squaresoft" }, |
| 480 | { 0x2B, "Tom Create" }, |
| 481 | { 0x2D, "Namco" }, |
| 482 | { 0x2E, "Movic" }, /* ???? */ |
| 483 | { 0x2F, "E3 Staff" }, /* ???? */ |
| 484 | { 0x31, "Vanguard" }, |
| 485 | { 0x32, "Megatron" }, |
| 486 | { 0x33, "Wiz" }, |
| 487 | { 0x36, "Capcom" } |
| 488 | }; |
| 489 | #endif |
| 490 | |
| 491 | static uint32 SRAMSize; |
| 492 | |
| 493 | static int Load(const uint8_t *data, size_t size) |
| 494 | { |
| 495 | uint32 pow_size = 0; |
| 496 | uint32 real_rom_size = 0; |
| 497 | uint8 header[10]; |
| 498 | |
| 499 | if(size < 65536) |
| 500 | return 0; |
| 501 | |
| 502 | real_rom_size = (size + 0xFFFF) & ~0xFFFF; |
| 503 | pow_size = next_pow2(real_rom_size); |
| 504 | rom_size = pow_size + (pow_size == 0); |
| 505 | |
| 506 | wsCartROM = (uint8 *)calloc(1, rom_size); |
| 507 | |
| 508 | /* This real_rom_size vs rom_size funny business |
| 509 | * is intended primarily for handling |
| 510 | * WSR files. */ |
| 511 | if(real_rom_size < rom_size) |
| 512 | memset(wsCartROM, 0xFF, rom_size - real_rom_size); |
| 513 | |
| 514 | memcpy(wsCartROM + (rom_size - real_rom_size), data, size); |
| 515 | |
| 516 | memcpy(header, wsCartROM + rom_size - 10, 10); |
| 517 | |
| 518 | SRAMSize = 0; |
| 519 | eeprom_size = 0; |
| 520 | |
| 521 | switch(header[5]) |
| 522 | { |
| 523 | case 0x01: SRAMSize = 8 * 1024; break; |
| 524 | case 0x02: SRAMSize = 32 * 1024; break; |
| 525 | case 0x03: SRAMSize = 128 * 1024; break; |
| 526 | case 0x04: SRAMSize = 256 * 1024; break; /* Dicing Knight!, Judgement Silver */ |
| 527 | case 0x05: SRAMSize = 512 * 1024; break; /* Wonder Gate */ |
| 528 | |
| 529 | case 0x10: eeprom_size = 128; break; |
| 530 | case 0x20: eeprom_size = 2 *1024; break; |
| 531 | case 0x50: eeprom_size = 1024; break; |
| 532 | } |
| 533 | |
| 534 | if((header[8] | (header[9] << 8)) == 0x8de1 && (header[0]==0x01)&&(header[2]==0x27)) /* Detective Conan */ |
| 535 | { |
| 536 | /* WS cpu is using cache/pipeline or there's protected ROM bank where pointing CS */ |
| 537 | wsCartROM[0xfffe8]=0xea; |
| 538 | wsCartROM[0xfffe9]=0x00; |
| 539 | wsCartROM[0xfffea]=0x00; |
| 540 | wsCartROM[0xfffeb]=0x00; |
| 541 | wsCartROM[0xfffec]=0x20; |
| 542 | } |
| 543 | |
| 544 | #if 0 |
| 545 | if(header[6] & 0x1) |
| 546 | EmulatedWSwan.rotated = MDFN_ROTATE90; |
| 547 | #endif |
| 548 | |
| 549 | MDFNMP_Init(16384, (1 << 20) / 1024); |
| 550 | |
| 551 | v30mz_init(WSwan_readmem20, WSwan_writemem20, WSwan_readport, WSwan_writeport); |
| 552 | WSwan_MemoryInit(MDFN_GetSettingB("wswan.language"), wsc, SRAMSize, false); /* EEPROM and SRAM are loaded in this func. */ |
| 553 | WSwan_GfxInit(); |
| 554 | |
| 555 | WSwan_SoundInit(); |
| 556 | |
| 557 | wsMakeTiles(); |
| 558 | |
| 559 | Reset(); |
| 560 | |
| 561 | return 1; |
| 562 | } |
| 563 | |
| 564 | static void CloseGame(void) |
| 565 | { |
| 566 | WSwan_MemoryKill(); |
| 567 | |
| 568 | WSwan_SoundKill(); |
| 569 | |
| 570 | if(wsCartROM) |
| 571 | { |
| 572 | free(wsCartROM); |
| 573 | wsCartROM = NULL; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | int StateAction(StateMem *sm, int load, int data_only) |
| 578 | { |
| 579 | if(!v30mz_StateAction(sm, load, data_only)) |
| 580 | return 0; |
| 581 | /* Call MemoryStateAction before others StateActions... */ |
| 582 | if(!WSwan_MemoryStateAction(sm, load, data_only)) |
| 583 | return 0; |
| 584 | if(!WSwan_GfxStateAction(sm, load, data_only)) |
| 585 | return 0; |
| 586 | if(!WSwan_RTCStateAction(sm, load, data_only)) |
| 587 | return 0; |
| 588 | if(!WSwan_InterruptStateAction(sm, load, data_only)) |
| 589 | return 0; |
| 590 | if(!WSwan_SoundStateAction(sm, load, data_only)) |
| 591 | return 0; |
| 592 | if(!WSwan_EEPROMStateAction(sm, load, data_only)) |
| 593 | return 0; |
| 594 | return 1; |
| 595 | } |
| 596 | |
| 597 | static void DoSimpleCommand(int cmd) |
| 598 | { |
| 599 | switch(cmd) |
| 600 | { |
| 601 | case MDFN_MSC_POWER: |
| 602 | case MDFN_MSC_RESET: |
| 603 | Reset(); |
| 604 | break; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | static bool update_audio = false; |
| 609 | static bool update_video = false; |
| 610 | |
| 611 | static bool MDFNI_LoadGame( |
| 612 | const char *force_module, const uint8_t *data, |
| 613 | size_t size) |
| 614 | { |
| 615 | if(Load(data, size) <= 0) |
| 616 | return false; |
| 617 | |
| 618 | MDFN_LoadGameCheats(NULL); |
| 619 | MDFNMP_InstallReadPatches(); |
| 620 | |
| 621 | return true; |
| 622 | } |
| 623 | |
| 624 | static void MDFNI_CloseGame(void) |
| 625 | { |
| 626 | MDFN_FlushGameCheats(0); |
| 627 | |
| 628 | CloseGame(); |
| 629 | |
| 630 | MDFNMP_Kill(); |
| 631 | } |
| 632 | |
| 633 | static void check_system_specs(void) |
| 634 | { |
| 635 | /* TODO - should theoretically be level 4, but apparently |
| 636 | * doesn't run at fullspeed on PSP (yet) */ |
| 637 | unsigned level = 4; |
| 638 | environ_cb(RETRO_ENVIRONMENT_SET_PERFORMANCE_LEVEL, &level); |
| 639 | } |
| 640 | |
| 641 | static void check_depth(void) |
| 642 | { |
| 643 | if (RETRO_PIX_DEPTH == 24) |
| 644 | { |
| 645 | enum retro_pixel_format rgb888 = RETRO_PIXEL_FORMAT_XRGB8888; |
| 646 | |
| 647 | if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &rgb888)) |
| 648 | { |
| 649 | if (log_cb) log_cb(RETRO_LOG_ERROR, "Pixel format XRGB8888 not supported by platform.\n"); |
| 650 | |
| 651 | RETRO_PIX_BYTES = 2; |
| 652 | RETRO_PIX_DEPTH = 15; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | #if defined(FRONTEND_SUPPORTS_RGB565) |
| 657 | if (RETRO_PIX_BYTES == 2) |
| 658 | { |
| 659 | enum retro_pixel_format rgb565 = RETRO_PIXEL_FORMAT_RGB565; |
| 660 | |
| 661 | if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &rgb565)) |
| 662 | { |
| 663 | if (log_cb) log_cb(RETRO_LOG_INFO, "Frontend supports RGB565 - will use that instead of XRGB1555.\n"); |
| 664 | |
| 665 | RETRO_PIX_DEPTH = 16; |
| 666 | } |
| 667 | } |
| 668 | #endif |
| 669 | } |
| 670 | |
| 671 | static void rotate_display(void) |
| 672 | { |
| 673 | if (hw_rotate_enabled) |
| 674 | { |
| 675 | struct retro_game_geometry new_geom = { |
| 676 | FB_WIDTH, |
| 677 | FB_HEIGHT, |
| 678 | FB_WIDTH, |
| 679 | FB_HEIGHT, |
| 680 | rotate_tall ? (9.0 / 14.0) : (14.0 / 9.0) |
| 681 | }; |
| 682 | unsigned rot_angle = rotate_tall ? |
| 683 | 1 : /* 90 degrees */ |
| 684 | 0; /* 0 degrees */ |
| 685 | |
| 686 | environ_cb(RETRO_ENVIRONMENT_SET_GEOMETRY, (void*)&new_geom); |
| 687 | environ_cb(RETRO_ENVIRONMENT_SET_ROTATION, (void*)&rot_angle); |
| 688 | } |
| 689 | else |
| 690 | { |
| 691 | struct retro_game_geometry new_geom = { |
| 692 | rotate_tall ? FB_HEIGHT : FB_WIDTH, |
| 693 | rotate_tall ? FB_WIDTH : FB_HEIGHT, |
| 694 | FB_WIDTH, |
| 695 | FB_WIDTH, |
| 696 | rotate_tall ? (9.0 / 14.0) : (14.0 / 9.0) |
| 697 | }; |
| 698 | |
| 699 | environ_cb(RETRO_ENVIRONMENT_SET_GEOMETRY, (void*)&new_geom); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | static void check_variables(int startup) |
| 704 | { |
| 705 | struct retro_variable var = {0}; |
| 706 | uint32 prev_mono_pal_start; |
| 707 | uint32 prev_mono_pal_end; |
| 708 | unsigned prev_frameskip_type; |
| 709 | bool update_60hz_mode = false; |
| 710 | |
| 711 | var.key = "wswan_rotate_display", |
| 712 | var.value = NULL; |
| 713 | |
| 714 | retro_display_rotation = DISPLAY_ROTATION_MANUAL; |
| 715 | |
| 716 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) |
| 717 | { |
| 718 | if (!strcmp(var.value, "portrait")) |
| 719 | retro_display_rotation = DISPLAY_ROTATION_PORTRAIT; |
| 720 | else if (!strcmp(var.value, "landscape")) |
| 721 | retro_display_rotation = DISPLAY_ROTATION_LANDSCAPE; |
| 722 | } |
| 723 | |
| 724 | switch (retro_display_rotation) |
| 725 | { |
| 726 | case DISPLAY_ROTATION_PORTRAIT: |
| 727 | if (!rotate_tall) |
| 728 | { |
| 729 | rotate_tall = true; |
| 730 | rotate_display(); |
| 731 | } |
| 732 | break; |
| 733 | case DISPLAY_ROTATION_LANDSCAPE: |
| 734 | if (rotate_tall) |
| 735 | { |
| 736 | rotate_tall = false; |
| 737 | rotate_display(); |
| 738 | } |
| 739 | break; |
| 740 | default: |
| 741 | break; |
| 742 | } |
| 743 | |
| 744 | var.key = "wswan_rotate_keymap", |
| 745 | var.value = NULL; |
| 746 | |
| 747 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) |
| 748 | { |
| 749 | if (!strcmp(var.value, "disabled")) |
| 750 | rotate_joymap = 0; |
| 751 | else if (!strcmp(var.value, "enabled")) |
| 752 | rotate_joymap = 1; |
| 753 | else if (!strcmp(var.value, "auto")) |
| 754 | rotate_joymap = 2; |
| 755 | } |
| 756 | |
| 757 | var.key = "wswan_mono_palette", |
| 758 | var.value = NULL; |
| 759 | |
| 760 | prev_mono_pal_start = mono_pal_start; |
| 761 | prev_mono_pal_end = mono_pal_end; |
| 762 | |
| 763 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) |
| 764 | find_mono_palette(var.value, &mono_pal_start, &mono_pal_end); |
| 765 | |
| 766 | if ((mono_pal_start != prev_mono_pal_start) || |
| 767 | (mono_pal_end != prev_mono_pal_end)) |
| 768 | WSwan_SetMonoPalette(RETRO_PIX_DEPTH, mono_pal_start, mono_pal_end); |
| 769 | |
| 770 | var.key = "wswan_frameskip"; |
| 771 | var.value = NULL; |
| 772 | |
| 773 | prev_frameskip_type = frameskip_type; |
| 774 | frameskip_type = 0; |
| 775 | |
| 776 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) |
| 777 | { |
| 778 | if (strcmp(var.value, "auto") == 0) |
| 779 | frameskip_type = 1; |
| 780 | else if (strcmp(var.value, "manual") == 0) |
| 781 | frameskip_type = 2; |
| 782 | } |
| 783 | |
| 784 | var.key = "wswan_frameskip_threshold"; |
| 785 | var.value = NULL; |
| 786 | |
| 787 | frameskip_threshold = 33; |
| 788 | |
| 789 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) |
| 790 | frameskip_threshold = strtol(var.value, NULL, 10); |
| 791 | |
| 792 | var.key = "wswan_60hz_mode"; |
| 793 | var.value = NULL; |
| 794 | |
| 795 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) |
| 796 | { |
| 797 | bool old_value = retro_60hz_enabled; |
| 798 | |
| 799 | if (!strcmp(var.value, "disabled")) |
| 800 | retro_60hz_enabled = false; |
| 801 | else if (!strcmp(var.value, "enabled")) |
| 802 | retro_60hz_enabled = true; |
| 803 | |
| 804 | if (!startup && (old_value != retro_60hz_enabled)) |
| 805 | { |
| 806 | update_video = true; |
| 807 | update_60hz_mode = true; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | var.key = "wswan_sound_sample_rate"; |
| 812 | var.value = NULL; |
| 813 | |
| 814 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) |
| 815 | { |
| 816 | int old_value = RETRO_SAMPLE_RATE; |
| 817 | |
| 818 | RETRO_SAMPLE_RATE = atoi(var.value); |
| 819 | RETRO_SAMPLE_RATE = (RETRO_SAMPLE_RATE < 11025) ? |
| 820 | 11025 : RETRO_SAMPLE_RATE; |
| 821 | RETRO_SAMPLE_RATE = (RETRO_SAMPLE_RATE > 48000) ? |
| 822 | 48000 : RETRO_SAMPLE_RATE; |
| 823 | |
| 824 | if (!startup && (old_value != RETRO_SAMPLE_RATE)) |
| 825 | { |
| 826 | update_audio = true; |
| 827 | /* If audio sample rate changes, must reinitialise |
| 828 | * 60Hz mode (will be a no-op if 60Hz mode is |
| 829 | * currently inactive) */ |
| 830 | update_60hz_mode = true; |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | var.key = "wswan_sound_low_pass"; |
| 835 | var.value = NULL; |
| 836 | |
| 837 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) |
| 838 | { |
| 839 | if (!strcmp(var.value, "disabled")) |
| 840 | audio_low_pass_enabled = false; |
| 841 | else if (!strcmp(var.value, "enabled")) |
| 842 | audio_low_pass_enabled = true; |
| 843 | } |
| 844 | |
| 845 | if (startup) |
| 846 | { |
| 847 | var.key = "wswan_gfx_colors"; |
| 848 | var.value = NULL; |
| 849 | |
| 850 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) |
| 851 | { |
| 852 | if (!strcmp(var.value, "16bit")) |
| 853 | { |
| 854 | RETRO_PIX_BYTES = 2; |
| 855 | RETRO_PIX_DEPTH = 16; |
| 856 | } |
| 857 | else if (!strcmp(var.value, "24bit")) |
| 858 | { |
| 859 | RETRO_PIX_BYTES = 4; |
| 860 | RETRO_PIX_DEPTH = 24; |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | if (update_60hz_mode) |
| 866 | retro_60hz_init(); |
| 867 | |
| 868 | /* (Re)Initialise frameskipping, if required */ |
| 869 | if (startup || |
| 870 | (frameskip_type != prev_frameskip_type) || |
| 871 | update_60hz_mode) |
| 872 | init_frameskip(); |
| 873 | } |
| 874 | |
| 875 | void retro_init(void) |
| 876 | { |
| 877 | struct retro_log_callback log; |
| 878 | if (environ_cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &log)) |
| 879 | log_cb = log.log; |
| 880 | else |
| 881 | log_cb = NULL; |
| 882 | |
| 883 | frameskip_type = 0; |
| 884 | frameskip_threshold = 0; |
| 885 | frameskip_counter = 0; |
| 886 | retro_audio_buff_active = false; |
| 887 | retro_audio_buff_occupancy = 0; |
| 888 | retro_audio_buff_underrun = false; |
| 889 | audio_latency = 0; |
| 890 | update_audio_latency = false; |
| 891 | audio_low_pass_enabled = false; |
| 892 | audio_low_pass_acc_left = 0; |
| 893 | audio_low_pass_acc_right = 0; |
| 894 | |
| 895 | check_system_specs(); |
| 896 | check_variables(true); |
| 897 | check_depth(); |
| 898 | |
| 899 | if (environ_cb(RETRO_ENVIRONMENT_GET_INPUT_BITMASKS, NULL)) |
| 900 | libretro_supports_bitmasks = true; |
| 901 | } |
| 902 | |
| 903 | void retro_reset(void) |
| 904 | { |
| 905 | DoSimpleCommand(MDFN_MSC_RESET); |
| 906 | } |
| 907 | |
| 908 | bool retro_load_game_special(unsigned a, const struct retro_game_info *b, size_t c) |
| 909 | { |
| 910 | return false; |
| 911 | } |
| 912 | |
| 913 | #define MAX_PLAYERS 1 |
| 914 | #define MAX_BUTTONS 11 |
| 915 | static uint16_t input_buf; |
| 916 | |
| 917 | bool retro_load_game(const struct retro_game_info *info) |
| 918 | { |
| 919 | const unsigned rot_angle = 0; |
| 920 | |
| 921 | struct retro_input_descriptor desc[] = { |
| 922 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "X Cursor Left" }, |
| 923 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "X Cursor Up" }, |
| 924 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "X Cursor Down" }, |
| 925 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "X Cursor Right" }, |
| 926 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "Y Cursor Left" }, |
| 927 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "Y Cursor Up" }, |
| 928 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "Y Cursor Down" }, |
| 929 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "Y Cursor Right" }, |
| 930 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" }, |
| 931 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" }, |
| 932 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" }, |
| 933 | { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Rotate screen + active D-Pad" }, |
| 934 | |
| 935 | { 0 }, |
| 936 | }; |
| 937 | |
| 938 | if (!info) |
| 939 | goto error; |
| 940 | |
| 941 | environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc); |
| 942 | |
| 943 | if (!MDFNI_LoadGame(MEDNAFEN_CORE_NAME_MODULE, |
| 944 | (const uint8_t*)info->data, info->size)) |
| 945 | goto error; |
| 946 | |
| 947 | chee = (uint8 *)&input_buf; |
| 948 | surf = (MDFN_Surface*)calloc(1, sizeof(*surf)); |
| 949 | |
| 950 | if (!surf) |
| 951 | goto error; |
| 952 | |
| 953 | surf->width = FB_WIDTH; |
| 954 | surf->height = FB_HEIGHT; |
| 955 | surf->pitch = FB_WIDTH; |
| 956 | surf->depth = RETRO_PIX_DEPTH; |
| 957 | |
| 958 | #if defined(_3DS) |
| 959 | surf->pixels = (uint16_t*)linearMemAlign(FB_WIDTH * FB_HEIGHT * sizeof(uint32_t), 128); |
| 960 | #else |
| 961 | surf->pixels = (uint16_t*)calloc(1, FB_WIDTH * FB_HEIGHT * sizeof(uint32_t)); |
| 962 | #endif |
| 963 | |
| 964 | if (!surf->pixels) |
| 965 | goto error; |
| 966 | |
| 967 | /* Check whether 'hardware' rotation (via frontend |
| 968 | * gfx driver) is supported */ |
| 969 | hw_rotate_enabled = environ_cb(RETRO_ENVIRONMENT_SET_ROTATION, (void*)&rot_angle); |
| 970 | |
| 971 | if (!hw_rotate_enabled && !rotate_buf) |
| 972 | { |
| 973 | #if defined(_3DS) |
| 974 | rotate_buf = (uint16_t*)linearMemAlign(FB_WIDTH * FB_HEIGHT * sizeof(uint32_t), 128); |
| 975 | #else |
| 976 | rotate_buf = (uint16_t*)calloc(1, FB_WIDTH * FB_HEIGHT * sizeof(uint32_t)); |
| 977 | #endif |
| 978 | |
| 979 | if (!rotate_buf) |
| 980 | goto error; |
| 981 | } |
| 982 | |
| 983 | rotate_tall = false; |
| 984 | select_pressed_last_frame = false; |
| 985 | rotate_joymap = 0; |
| 986 | |
| 987 | check_variables(false); |
| 988 | |
| 989 | /* Allocate an audio buffer of sufficient size |
| 990 | * for the expected number of samples per frame |
| 991 | * (size will be increased automatically if |
| 992 | * configuration changes) */ |
| 993 | audio_samples_buf_size = ((int32_t)(RETRO_SAMPLE_RATE / |
| 994 | MEDNAFEN_CORE_TIMING_FPS) + 1) << 1; |
| 995 | audio_samples_buf = (int16_t*)malloc(audio_samples_buf_size * sizeof(int16_t)); |
| 996 | |
| 997 | if (!audio_samples_buf) |
| 998 | goto error; |
| 999 | |
| 1000 | retro_60hz_init(); |
| 1001 | |
| 1002 | WSwan_SetPixelFormat(RETRO_PIX_DEPTH, |
| 1003 | mono_pal_start, mono_pal_end); |
| 1004 | WSwan_SetSoundRate(RETRO_SAMPLE_RATE); |
| 1005 | |
| 1006 | return true; |
| 1007 | |
| 1008 | error: |
| 1009 | if (surf) |
| 1010 | { |
| 1011 | if (surf->pixels) |
| 1012 | #if defined(_3DS) |
| 1013 | linearFree(surf->pixels); |
| 1014 | #else |
| 1015 | free(surf->pixels); |
| 1016 | #endif |
| 1017 | free(surf); |
| 1018 | } |
| 1019 | surf = NULL; |
| 1020 | |
| 1021 | if (rotate_buf) |
| 1022 | #if defined(_3DS) |
| 1023 | linearFree(rotate_buf); |
| 1024 | #else |
| 1025 | free(rotate_buf); |
| 1026 | #endif |
| 1027 | rotate_buf = NULL; |
| 1028 | |
| 1029 | if (audio_samples_buf) |
| 1030 | free(audio_samples_buf); |
| 1031 | audio_samples_buf = NULL; |
| 1032 | audio_samples_buf_size = 0; |
| 1033 | |
| 1034 | return false; |
| 1035 | } |
| 1036 | |
| 1037 | void retro_unload_game(void) |
| 1038 | { |
| 1039 | MDFNI_CloseGame(); |
| 1040 | |
| 1041 | if (surf) |
| 1042 | { |
| 1043 | if (surf->pixels) |
| 1044 | #if defined(_3DS) |
| 1045 | linearFree(surf->pixels); |
| 1046 | #else |
| 1047 | free(surf->pixels); |
| 1048 | #endif |
| 1049 | free(surf); |
| 1050 | } |
| 1051 | surf = NULL; |
| 1052 | |
| 1053 | if (rotate_buf) |
| 1054 | #if defined(_3DS) |
| 1055 | linearFree(rotate_buf); |
| 1056 | #else |
| 1057 | free(rotate_buf); |
| 1058 | #endif |
| 1059 | rotate_buf = NULL; |
| 1060 | |
| 1061 | if (audio_samples_buf) |
| 1062 | free(audio_samples_buf); |
| 1063 | audio_samples_buf = NULL; |
| 1064 | audio_samples_buf_size = 0; |
| 1065 | |
| 1066 | retro_60hz_deinit(); |
| 1067 | } |
| 1068 | |
| 1069 | static void update_input(void) |
| 1070 | { |
| 1071 | static unsigned map[2][11] = { |
| 1072 | { |
| 1073 | RETRO_DEVICE_ID_JOYPAD_UP, /* X Cursor horizontal-layout games */ |
| 1074 | RETRO_DEVICE_ID_JOYPAD_RIGHT, /* X Cursor horizontal-layout games */ |
| 1075 | RETRO_DEVICE_ID_JOYPAD_DOWN, /* X Cursor horizontal-layout games */ |
| 1076 | RETRO_DEVICE_ID_JOYPAD_LEFT, /* X Cursor horizontal-layout games */ |
| 1077 | RETRO_DEVICE_ID_JOYPAD_R2, /* Y Cursor UP vertical-layout games */ |
| 1078 | RETRO_DEVICE_ID_JOYPAD_R, /* Y Cursor RIGHT vertical-layout games */ |
| 1079 | RETRO_DEVICE_ID_JOYPAD_L2, /* Y Cursor DOWN vertical-layout games */ |
| 1080 | RETRO_DEVICE_ID_JOYPAD_L, /* Y Cursor LEFT vertical-layout games */ |
| 1081 | RETRO_DEVICE_ID_JOYPAD_START, |
| 1082 | RETRO_DEVICE_ID_JOYPAD_A, |
| 1083 | RETRO_DEVICE_ID_JOYPAD_B, |
| 1084 | }, |
| 1085 | { |
| 1086 | RETRO_DEVICE_ID_JOYPAD_Y, /* X Cursor horizontal-layout games */ |
| 1087 | RETRO_DEVICE_ID_JOYPAD_X, /* X Cursor horizontal-layout games */ |
| 1088 | RETRO_DEVICE_ID_JOYPAD_A, /* X Cursor horizontal-layout games */ |
| 1089 | RETRO_DEVICE_ID_JOYPAD_B, /* X Cursor horizontal-layout games */ |
| 1090 | RETRO_DEVICE_ID_JOYPAD_LEFT, /* Y Cursor UP vertical-layout games */ |
| 1091 | RETRO_DEVICE_ID_JOYPAD_UP, /* Y Cursor RIGHT vertical-layout games */ |
| 1092 | RETRO_DEVICE_ID_JOYPAD_RIGHT, /* Y Cursor DOWN vertical-layout games */ |
| 1093 | RETRO_DEVICE_ID_JOYPAD_DOWN, /* Y Cursor LEFT vertical-layout games */ |
| 1094 | RETRO_DEVICE_ID_JOYPAD_START, |
| 1095 | RETRO_DEVICE_ID_JOYPAD_L, |
| 1096 | RETRO_DEVICE_ID_JOYPAD_R, |
| 1097 | } |
| 1098 | }; |
| 1099 | unsigned i; |
| 1100 | int16_t bitmask = 0; |
| 1101 | bool select_button; |
| 1102 | bool joymap; |
| 1103 | #ifdef MSB_FIRST |
| 1104 | union { |
| 1105 | uint8_t b[2]; |
| 1106 | uint16_t s; |
| 1107 | } u; |
| 1108 | #endif |
| 1109 | |
| 1110 | input_buf = 0; |
| 1111 | |
| 1112 | if (libretro_supports_bitmasks) |
| 1113 | bitmask = input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_MASK); |
| 1114 | else |
| 1115 | { |
| 1116 | for (i = 0; i < (RETRO_DEVICE_ID_JOYPAD_R3+1); i++) |
| 1117 | bitmask |= input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, i) ? 1 << i : 0; |
| 1118 | } |
| 1119 | |
| 1120 | select_button = bitmask & (1 << RETRO_DEVICE_ID_JOYPAD_SELECT); |
| 1121 | if ((retro_display_rotation == DISPLAY_ROTATION_MANUAL) && |
| 1122 | select_button && |
| 1123 | !select_pressed_last_frame) |
| 1124 | { |
| 1125 | rotate_tall = !rotate_tall; |
| 1126 | rotate_display(); |
| 1127 | } |
| 1128 | select_pressed_last_frame = select_button; |
| 1129 | |
| 1130 | joymap = (rotate_joymap == 2) ? rotate_tall : (rotate_joymap ? true : false); |
| 1131 | |
| 1132 | for (i = 0; i < MAX_BUTTONS; i++) |
| 1133 | input_buf |= map[joymap][i] != -1u && ((1 << map[joymap][i]) & bitmask) ? (1 << i) : 0; |
| 1134 | |
| 1135 | #ifdef MSB_FIRST |
| 1136 | u.s = input_buf; |
| 1137 | input_buf = u.b[0] | u.b[1] << 8; |
| 1138 | #endif |
| 1139 | } |
| 1140 | |
| 1141 | void retro_run(void) |
| 1142 | { |
| 1143 | int total; |
| 1144 | unsigned width, height; |
| 1145 | int32 SoundBufSize; |
| 1146 | EmulateSpecStruct spec; |
| 1147 | bool updated = false; |
| 1148 | int skip_frame = 0; |
| 1149 | int update_sample_rate; |
| 1150 | |
| 1151 | if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated) |
| 1152 | check_variables(false); |
| 1153 | update_sample_rate = update_audio; |
| 1154 | |
| 1155 | input_poll_cb(); |
| 1156 | |
| 1157 | update_input(); |
| 1158 | |
| 1159 | /* Check whether current frame should |
| 1160 | * be skipped */ |
| 1161 | if ((frameskip_type > 0) && retro_audio_buff_active) |
| 1162 | { |
| 1163 | switch (frameskip_type) |
| 1164 | { |
| 1165 | case 1: /* auto */ |
| 1166 | skip_frame = retro_audio_buff_underrun ? 1 : 0; |
| 1167 | break; |
| 1168 | case 2: /* manual */ |
| 1169 | skip_frame = (retro_audio_buff_occupancy < frameskip_threshold) ? 1 : 0; |
| 1170 | break; |
| 1171 | default: |
| 1172 | skip_frame = 0; |
| 1173 | break; |
| 1174 | } |
| 1175 | |
| 1176 | if (!skip_frame || |
| 1177 | (frameskip_counter >= FRAMESKIP_MAX)) |
| 1178 | { |
| 1179 | skip_frame = 0; |
| 1180 | frameskip_counter = 0; |
| 1181 | } |
| 1182 | else |
| 1183 | frameskip_counter++; |
| 1184 | } |
| 1185 | |
| 1186 | /* If frameskip settings have changed, update |
| 1187 | * frontend audio latency */ |
| 1188 | if (update_audio_latency) |
| 1189 | { |
| 1190 | environ_cb(RETRO_ENVIRONMENT_SET_MINIMUM_AUDIO_LATENCY, |
| 1191 | &audio_latency); |
| 1192 | update_audio_latency = false; |
| 1193 | } |
| 1194 | |
| 1195 | if (retro_60hz_enabled) |
| 1196 | { |
| 1197 | /* If we are running in 60Hz mode, then: |
| 1198 | * - Audio data must be buffered |
| 1199 | * - On every 4th call of retro_run(), an |
| 1200 | * extra frame must be emulated */ |
| 1201 | |
| 1202 | /* Emulate 'force skipped frame' */ |
| 1203 | if (retro_60hz_counter == 0) |
| 1204 | { |
| 1205 | Emulate(&spec, 1, update_sample_rate); |
| 1206 | update_sample_rate = 0; |
| 1207 | retro_60hz_cache_audio_samples(audio_samples_buf, |
| 1208 | spec.SoundBufSize); |
| 1209 | } |
| 1210 | |
| 1211 | /* Run 'regular' frame */ |
| 1212 | Emulate(&spec, skip_frame, update_sample_rate); |
| 1213 | retro_60hz_cache_audio_samples(audio_samples_buf, |
| 1214 | spec.SoundBufSize); |
| 1215 | |
| 1216 | retro_60hz_counter++; |
| 1217 | } |
| 1218 | else |
| 1219 | Emulate(&spec, skip_frame, update_sample_rate); |
| 1220 | |
| 1221 | if (update_audio || update_video) |
| 1222 | { |
| 1223 | struct retro_system_av_info system_av_info; |
| 1224 | retro_get_system_av_info(&system_av_info); |
| 1225 | environ_cb(RETRO_ENVIRONMENT_SET_SYSTEM_AV_INFO, &system_av_info); |
| 1226 | update_audio = false; |
| 1227 | update_video = false; |
| 1228 | } |
| 1229 | |
| 1230 | width = spec.DisplayRect.w; |
| 1231 | height = spec.DisplayRect.h; |
| 1232 | |
| 1233 | if (hw_rotate_enabled || !rotate_tall) |
| 1234 | { |
| 1235 | if (!skip_frame) |
| 1236 | video_cb(surf->pixels, width, height, FB_WIDTH * RETRO_PIX_BYTES); |
| 1237 | else |
| 1238 | video_cb(NULL, width, height, FB_WIDTH * RETRO_PIX_BYTES); |
| 1239 | } |
| 1240 | else |
| 1241 | { |
| 1242 | if (!skip_frame) |
| 1243 | { |
| 1244 | /* Perform software-based display rotation */ |
| 1245 | if (RETRO_PIX_BYTES == 4) |
| 1246 | ROTATE_PIXEL_BUF( |
| 1247 | uint32_t, surf->pixels, width, height, rotate_buf) |
| 1248 | else |
| 1249 | ROTATE_PIXEL_BUF( |
| 1250 | uint16_t, surf->pixels, width, height, rotate_buf) |
| 1251 | |
| 1252 | video_cb(rotate_buf, height, width, FB_HEIGHT * RETRO_PIX_BYTES); |
| 1253 | } |
| 1254 | else |
| 1255 | video_cb(NULL, height, width, FB_HEIGHT * RETRO_PIX_BYTES); |
| 1256 | } |
| 1257 | |
| 1258 | if (retro_60hz_enabled) |
| 1259 | { |
| 1260 | /* If we are running in 60Hz mode, then |
| 1261 | * read out the expected number of samples |
| 1262 | * on each frame plus empty the buffer at |
| 1263 | * the end of each 4/5 frame cycle */ |
| 1264 | int32_t frames_available = retro_60hz_audio.samples_buf_pos >> 1; |
| 1265 | int32_t frames_to_read = retro_60hz_audio.samples_per_frame; |
| 1266 | int32_t samples_to_read; |
| 1267 | |
| 1268 | frames_to_read = (frames_to_read > frames_available) ? |
| 1269 | frames_available : frames_to_read; |
| 1270 | |
| 1271 | if (retro_60hz_counter >= RETRO_60HZ_CYCLE_INDEX) |
| 1272 | { |
| 1273 | frames_to_read = frames_available; |
| 1274 | retro_60hz_counter = 0; |
| 1275 | } |
| 1276 | samples_to_read = frames_to_read << 1; |
| 1277 | |
| 1278 | for (total = 0; total < frames_to_read; ) |
| 1279 | total += audio_batch_cb( |
| 1280 | retro_60hz_audio.samples_buf + (total << 1), |
| 1281 | frames_to_read - total); |
| 1282 | |
| 1283 | /* Remove uploaded samples from the buffer */ |
| 1284 | if (frames_to_read < frames_available) |
| 1285 | memmove(retro_60hz_audio.samples_buf, |
| 1286 | retro_60hz_audio.samples_buf + samples_to_read, |
| 1287 | (retro_60hz_audio.samples_buf_pos - samples_to_read) * |
| 1288 | sizeof(int16_t)); |
| 1289 | |
| 1290 | retro_60hz_audio.samples_buf_pos -= samples_to_read; |
| 1291 | } |
| 1292 | else |
| 1293 | for (total = 0; total < spec.SoundBufSize; ) |
| 1294 | total += audio_batch_cb( |
| 1295 | audio_samples_buf + (total << 1), |
| 1296 | spec.SoundBufSize - total); |
| 1297 | } |
| 1298 | |
| 1299 | void retro_get_system_info(struct retro_system_info *info) |
| 1300 | { |
| 1301 | memset(info, 0, sizeof(*info)); |
| 1302 | info->library_name = MEDNAFEN_CORE_NAME; |
| 1303 | #ifndef GIT_VERSION |
| 1304 | #define GIT_VERSION "" |
| 1305 | #endif |
| 1306 | info->library_version = MEDNAFEN_CORE_VERSION GIT_VERSION; |
| 1307 | info->need_fullpath = false; |
| 1308 | info->valid_extensions = MEDNAFEN_CORE_EXTENSIONS; |
| 1309 | info->block_extract = false; |
| 1310 | } |
| 1311 | |
| 1312 | void retro_get_system_av_info(struct retro_system_av_info *info) |
| 1313 | { |
| 1314 | memset(info, 0, sizeof(*info)); |
| 1315 | |
| 1316 | info->timing.fps = retro_60hz_enabled ? |
| 1317 | RETRO_60HZ_FPS : MEDNAFEN_CORE_TIMING_FPS; |
| 1318 | info->timing.sample_rate = RETRO_SAMPLE_RATE; |
| 1319 | |
| 1320 | if (hw_rotate_enabled || !rotate_tall) |
| 1321 | { |
| 1322 | info->geometry.base_width = MEDNAFEN_CORE_GEOMETRY_BASE_W; |
| 1323 | info->geometry.base_height = MEDNAFEN_CORE_GEOMETRY_BASE_H; |
| 1324 | } |
| 1325 | else |
| 1326 | { |
| 1327 | info->geometry.base_width = MEDNAFEN_CORE_GEOMETRY_BASE_H; |
| 1328 | info->geometry.base_height = MEDNAFEN_CORE_GEOMETRY_BASE_W; |
| 1329 | } |
| 1330 | |
| 1331 | info->geometry.max_width = MEDNAFEN_CORE_GEOMETRY_MAX_W; |
| 1332 | if (hw_rotate_enabled) |
| 1333 | info->geometry.max_height = MEDNAFEN_CORE_GEOMETRY_MAX_H; |
| 1334 | else |
| 1335 | info->geometry.max_height = MEDNAFEN_CORE_GEOMETRY_MAX_W; |
| 1336 | |
| 1337 | if (rotate_tall) |
| 1338 | info->geometry.aspect_ratio = 1.0f / MEDNAFEN_CORE_GEOMETRY_ASPECT_RATIO; |
| 1339 | else |
| 1340 | info->geometry.aspect_ratio = MEDNAFEN_CORE_GEOMETRY_ASPECT_RATIO; |
| 1341 | } |
| 1342 | |
| 1343 | void retro_deinit(void) |
| 1344 | { |
| 1345 | if (surf) |
| 1346 | { |
| 1347 | if (surf->pixels) |
| 1348 | #if defined(_3DS) |
| 1349 | linearFree(surf->pixels); |
| 1350 | #else |
| 1351 | free(surf->pixels); |
| 1352 | #endif |
| 1353 | free(surf); |
| 1354 | } |
| 1355 | surf = NULL; |
| 1356 | |
| 1357 | if (rotate_buf) |
| 1358 | #if defined(_3DS) |
| 1359 | linearFree(rotate_buf); |
| 1360 | #else |
| 1361 | free(rotate_buf); |
| 1362 | #endif |
| 1363 | rotate_buf = NULL; |
| 1364 | |
| 1365 | if (audio_samples_buf) |
| 1366 | free(audio_samples_buf); |
| 1367 | audio_samples_buf = NULL; |
| 1368 | audio_samples_buf_size = 0; |
| 1369 | |
| 1370 | retro_60hz_deinit(); |
| 1371 | retro_60hz_enabled = false; |
| 1372 | |
| 1373 | libretro_supports_bitmasks = false; |
| 1374 | } |
| 1375 | |
| 1376 | unsigned retro_get_region(void) |
| 1377 | { |
| 1378 | return RETRO_REGION_NTSC; /* No real regions of sorts for this handheld, so just set to NTSC. */ |
| 1379 | } |
| 1380 | |
| 1381 | unsigned retro_api_version(void) |
| 1382 | { |
| 1383 | return RETRO_API_VERSION; |
| 1384 | } |
| 1385 | |
| 1386 | void retro_set_controller_port_device(unsigned in_port, unsigned device) { } |
| 1387 | |
| 1388 | void retro_set_environment(retro_environment_t cb) |
| 1389 | { |
| 1390 | bool option_cats_supported = false; |
| 1391 | |
| 1392 | environ_cb = cb; |
| 1393 | |
| 1394 | libretro_set_core_options(environ_cb, |
| 1395 | &option_cats_supported); |
| 1396 | } |
| 1397 | |
| 1398 | void retro_set_audio_sample(retro_audio_sample_t cb) |
| 1399 | { |
| 1400 | } |
| 1401 | |
| 1402 | void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) |
| 1403 | { |
| 1404 | audio_batch_cb = cb; |
| 1405 | } |
| 1406 | |
| 1407 | void retro_set_input_poll(retro_input_poll_t cb) |
| 1408 | { |
| 1409 | input_poll_cb = cb; |
| 1410 | } |
| 1411 | |
| 1412 | void retro_set_input_state(retro_input_state_t cb) |
| 1413 | { |
| 1414 | input_state_cb = cb; |
| 1415 | } |
| 1416 | |
| 1417 | void retro_set_video_refresh(retro_video_refresh_t cb) |
| 1418 | { |
| 1419 | video_cb = cb; |
| 1420 | } |
| 1421 | |
| 1422 | size_t retro_serialize_size(void) |
| 1423 | { |
| 1424 | StateMem st; |
| 1425 | |
| 1426 | st.data = NULL; |
| 1427 | st.loc = 0; |
| 1428 | st.len = 0; |
| 1429 | st.malloced = 0; |
| 1430 | st.initial_malloc = 0; |
| 1431 | |
| 1432 | if (!MDFNSS_SaveSM(&st, 0, 0, NULL, NULL, NULL)) |
| 1433 | return 0; |
| 1434 | |
| 1435 | free(st.data); |
| 1436 | |
| 1437 | return st.len; |
| 1438 | } |
| 1439 | |
| 1440 | bool retro_serialize(void *data, size_t size) |
| 1441 | { |
| 1442 | StateMem st; |
| 1443 | bool ret = false; |
| 1444 | uint8_t *_dat = (uint8_t*)malloc(size); |
| 1445 | |
| 1446 | if (!_dat) |
| 1447 | return false; |
| 1448 | |
| 1449 | /* Mednafen can realloc the buffer so we need to ensure this is safe. */ |
| 1450 | st.data = _dat; |
| 1451 | st.loc = 0; |
| 1452 | st.len = 0; |
| 1453 | st.malloced = size; |
| 1454 | st.initial_malloc = 0; |
| 1455 | |
| 1456 | ret = MDFNSS_SaveSM(&st, 0, 0, NULL, NULL, NULL); |
| 1457 | |
| 1458 | memcpy(data, st.data, size); |
| 1459 | free(st.data); |
| 1460 | |
| 1461 | return ret; |
| 1462 | } |
| 1463 | |
| 1464 | bool retro_unserialize(const void *data, size_t size) |
| 1465 | { |
| 1466 | StateMem st; |
| 1467 | |
| 1468 | st.data = (uint8_t*)data; |
| 1469 | st.loc = 0; |
| 1470 | st.len = size; |
| 1471 | st.malloced = 0; |
| 1472 | st.initial_malloc = 0; |
| 1473 | |
| 1474 | return MDFNSS_LoadSM(&st, 0, 0); |
| 1475 | } |
| 1476 | |
| 1477 | void *retro_get_memory_data(unsigned type) |
| 1478 | { |
| 1479 | switch (type) |
| 1480 | { |
| 1481 | case RETRO_MEMORY_SAVE_RAM: |
| 1482 | if (eeprom_size) |
| 1483 | return (uint8_t*)wsEEPROM; |
| 1484 | else if (SRAMSize) |
| 1485 | return wsSRAM; |
| 1486 | break; |
| 1487 | case RETRO_MEMORY_SYSTEM_RAM: |
| 1488 | return (uint8_t*)wsRAM; |
| 1489 | default: |
| 1490 | break; |
| 1491 | } |
| 1492 | |
| 1493 | return NULL; |
| 1494 | } |
| 1495 | |
| 1496 | size_t retro_get_memory_size(unsigned type) |
| 1497 | { |
| 1498 | switch (type) |
| 1499 | { |
| 1500 | case RETRO_MEMORY_SAVE_RAM: |
| 1501 | if (eeprom_size) |
| 1502 | return eeprom_size; |
| 1503 | else if (SRAMSize) |
| 1504 | return SRAMSize; |
| 1505 | break; |
| 1506 | case RETRO_MEMORY_SYSTEM_RAM: |
| 1507 | return wsRAMSize; |
| 1508 | default: |
| 1509 | break; |
| 1510 | } |
| 1511 | |
| 1512 | return 0; |
| 1513 | } |
| 1514 | |
| 1515 | void retro_cheat_reset(void) { } |
| 1516 | void retro_cheat_set(unsigned a, bool b, const char *c) { } |