diff -urN a/code/src/glide2gl/src/Glide64/Framebuffer_glide64.c src/code/src/glide2gl/src/Glide64/Framebuffer_glide64.c --- a/code/src/glide2gl/src/Glide64/Framebuffer_glide64.c +++ b/code/src/glide2gl/src/Glide64/Framebuffer_glide64.c @@ -974,11 +974,20 @@ (gDP.colorImage.width * gDP.colorImage.height) << g_gdp.fb_size >> 1); } +/* PlayAgain: 0 while Netplay plays (pa_set_gpu_readback, pa_n64.c). What + * this copies into RDRAM is the GPU's pixels: two homes on two GPUs would + * write two different pictures, so under lockstep every home leaves RDRAM as + * the game left it instead, the same way everywhere. */ +extern int pa_gpu_readback; + void CopyFrameBuffer(int32_t buffer) { uint32_t height = 0; uint32_t width = gDP.colorImage.width;//*gfx_info.VI_WIDTH_REG; + if (!pa_gpu_readback) + return; + if (fb_emulation_enabled && !(settings.hacks&hack_PPL)) { int ind = (rdp.ci_count > 0) ? (rdp.ci_count-1) : 0; @@ -1198,7 +1207,7 @@ fb_info.ul_y = 0; fb_info.lr_y = fb_info.height - 1; fb_info.opaque = 1; - fb_info.addr = *gfx_info.VI_ORIGIN_REG; + fb_info.addr = *gfx_info.VI_ORIGIN_REG & BMASK; /* PlayAgain: never past RDRAM */ fb_info.size = *gfx_info.VI_STATUS_REG & 3; rdp.last_bg = fb_info.addr; diff -urN a/code/src/libretro/libretronew.c src/code/src/libretro/libretronew.c --- a/code/src/libretro/libretronew.c +++ b/code/src/libretro/libretronew.c @@ -1158,6 +1158,7 @@ format_mempak(saved_memory.mempack[3]); format_disk(saved_memory.disk); +#ifndef __EMSCRIPTEN__ /* PlayAgain: no files; the host writes saved_memory through pa_save_ram_ptr() */ if (file_exists("game.savememory")) { int size = 0; @@ -1195,6 +1196,7 @@ memcpy(saved_memory.flashram, fla, size); printf("fla loaded\n"); } +#endif } @@ -1426,8 +1428,20 @@ int export_save_memory_counter = 0; void neil_toast_message(char* message); +#ifdef __EMSCRIPTEN__ +void pa_save_ram_settled(void); /* PlayAgain's shim: the host persists saved_memory itself */ +#endif + bool neil_export_save_memory() { +#ifdef __EMSCRIPTEN__ + if (export_save_memory_counter == 1) + pa_save_ram_settled(); + if (export_save_memory_counter > 0) + export_save_memory_counter--; + return true; +#endif + if (export_save_memory_counter == 1) { FILE* file = fopen("game.savememory", "wb"); // w for write, b for binary @@ -1519,6 +1533,7 @@ bool neilInterlacedMode = false; +#ifdef HAVE_THR_AL /* PlayAgain: Angrylion is not built (no licence text in the tree) */ uint32_t* get_video_buffer() { @@ -1545,4 +1560,5 @@ } } return buffer; -} \ No newline at end of file +} +#endif diff -urN a/code/src/libretro-common/include/glsym/glsym.h src/code/src/libretro-common/include/glsym/glsym.h --- a/code/src/libretro-common/include/glsym/glsym.h +++ b/code/src/libretro-common/include/glsym/glsym.h @@ -26,6 +26,11 @@ #ifdef _WIN32 #define GLEW_STATIC #include +#elif defined(__EMSCRIPTEN__) +/* PlayAgain: no SDL port; Emscripten's own GL headers (WebGL2 behind -sFULL_ES3). */ +#define GL_GLEXT_PROTOTYPES +#include +#include #else #define GL_GLEXT_PROTOTYPES #include diff -urN a/code/src/mupen64plus-core/src/main/savestates.c src/code/src/mupen64plus-core/src/main/savestates.c --- a/code/src/mupen64plus-core/src/main/savestates.c +++ b/code/src/mupen64plus-core/src/main/savestates.c @@ -526,5 +526,6 @@ * of state saving operation */ StateChanged(M64CORE_STATE_SAVECOMPLETE, 1); - return 1; + /* PlayAgain: the byte count written (callers only test non-zero). */ + return (int)(curr - data); } diff -urN a/code/src/mupen64plus-core/src/plugin/audio_libretro/audio_backend_libretro.c src/code/src/mupen64plus-core/src/plugin/audio_libretro/audio_backend_libretro.c --- a/code/src/mupen64plus-core/src/plugin/audio_libretro/audio_backend_libretro.c +++ b/code/src/mupen64plus-core/src/plugin/audio_libretro/audio_backend_libretro.c @@ -102,7 +102,9 @@ #endif } +#ifndef __EMSCRIPTEN__ /* PlayAgain: the web build has no SDL; samples go to the ring below */ #include +#endif int sound_device_id = 0; //SDL needs this diff -urN a/code/src/mupen64plus-core/src/r4300/fpu.h src/code/src/mupen64plus-core/src/r4300/fpu.h --- a/code/src/mupen64plus-core/src/r4300/fpu.h +++ b/code/src/mupen64plus-core/src/r4300/fpu.h @@ -24,6 +24,7 @@ #include #include +#include #include "cp1_private.h" #include "r4300.h" @@ -59,6 +60,29 @@ #define FCR31_CMP_BIT UINT32_C(0x800000) +/* PlayAgain: every NaN an operation makes is the R4300's own default NaN + * (0x7FBFFFFF single, 0x7FF7FFFFFFFFFFFF double), as the console gives with + * its exceptions masked. WebAssembly leaves a NaN result's sign and payload + * to the engine and the CPU (0 * inf is 0xFFC00000 on x86 and 0x7FC00000 on + * ARM, and a two-NaN operation follows operand order, which an optimizing + * tier may swap), so without this two homes could store different words. + * The test and the store are on the bits, as integers: a compiler may treat + * one NaN as good as another (a float "x != x ? NaN : x" folds to x), an + * integer it may not. */ +M64P_FPU_INLINE void pa_store_s(float *target, float value) +{ + uint32_t bits; + memcpy(&bits, &value, sizeof bits); + if ((bits & UINT32_C(0x7FFFFFFF)) > UINT32_C(0x7F800000)) bits = UINT32_C(0x7FBFFFFF); + memcpy(target, &bits, sizeof bits); +} +M64P_FPU_INLINE void pa_store_d(double *target, double value) +{ + uint64_t bits; + memcpy(&bits, &value, sizeof bits); + if ((bits & UINT64_C(0x7FFFFFFFFFFFFFFF)) > UINT64_C(0x7FF0000000000000)) bits = UINT64_C(0x7FF7FFFFFFFFFFFF); + memcpy(target, &bits, sizeof bits); +} M64P_FPU_INLINE void set_rounding(void) { @@ -103,12 +127,12 @@ } M64P_FPU_INLINE void cvt_d_s(const float *source,double *dest) { - *dest = (double) *source; + pa_store_d(dest, (double) *source); /* PlayAgain: the default NaN */ } M64P_FPU_INLINE void cvt_s_d(const double *source,float *dest) { set_rounding(); - *dest = (float) *source; + pa_store_s(dest, (float) *source); /* PlayAgain: the default NaN */ } M64P_FPU_INLINE void round_l_s(const float *source,int64_t *dest) @@ -394,27 +418,27 @@ M64P_FPU_INLINE void add_s(const float *source1,const float *source2,float *target) { set_rounding(); - *target=(*source1)+(*source2); + pa_store_s(target, (*source1)+(*source2)); /* PlayAgain: the default NaN */ } M64P_FPU_INLINE void sub_s(const float *source1,const float *source2,float *target) { set_rounding(); - *target=(*source1)-(*source2); + pa_store_s(target, (*source1)-(*source2)); /* PlayAgain: the default NaN */ } M64P_FPU_INLINE void mul_s(const float *source1,const float *source2,float *target) { set_rounding(); - *target=(*source1)*(*source2); + pa_store_s(target, (*source1)*(*source2)); /* PlayAgain: the default NaN */ } M64P_FPU_INLINE void div_s(const float *source1,const float *source2,float *target) { set_rounding(); - *target=(*source1)/(*source2); + pa_store_s(target, (*source1)/(*source2)); /* PlayAgain: the default NaN */ } M64P_FPU_INLINE void sqrt_s(const float *source,float *target) { set_rounding(); - *target=sqrtf(*source); + pa_store_s(target, sqrtf(*source)); /* PlayAgain: the default NaN */ } M64P_FPU_INLINE void abs_s(const float *source,float *target) { @@ -431,27 +455,27 @@ M64P_FPU_INLINE void add_d(const double *source1,const double *source2,double *target) { set_rounding(); - *target=(*source1)+(*source2); + pa_store_d(target, (*source1)+(*source2)); /* PlayAgain: the default NaN */ } M64P_FPU_INLINE void sub_d(const double *source1,const double *source2,double *target) { set_rounding(); - *target=(*source1)-(*source2); + pa_store_d(target, (*source1)-(*source2)); /* PlayAgain: the default NaN */ } M64P_FPU_INLINE void mul_d(const double *source1,const double *source2,double *target) { set_rounding(); - *target=(*source1)*(*source2); + pa_store_d(target, (*source1)*(*source2)); /* PlayAgain: the default NaN */ } M64P_FPU_INLINE void div_d(const double *source1,const double *source2,double *target) { set_rounding(); - *target=(*source1)/(*source2); + pa_store_d(target, (*source1)/(*source2)); /* PlayAgain: the default NaN */ } M64P_FPU_INLINE void sqrt_d(const double *source,double *target) { set_rounding(); - *target=sqrt(*source); + pa_store_d(target, sqrt(*source)); /* PlayAgain: the default NaN */ } M64P_FPU_INLINE void abs_d(const double *source,double *target) { diff -urN a/code/src/mupen64plus-core/src/si/si_controller.c src/code/src/mupen64plus-core/src/si/si_controller.c --- a/code/src/mupen64plus-core/src/si/si_controller.c +++ b/code/src/mupen64plus-core/src/si/si_controller.c @@ -44,7 +44,10 @@ { int i; - if (si->regs[SI_PIF_ADDR_WR64B_REG] != 0x1FC007C0) + /* PlayAgain: the PIF answers in every KSEG mirror (bare-metal code writes 0xBFC007C0), + * and a DMA that would leave RDRAM is dropped, not read out of the host's memory. */ + if ((si->regs[SI_PIF_ADDR_WR64B_REG] & 0x1FFFFFFF) != 0x1FC007C0 + || si->regs[SI_DRAM_ADDR_REG] + PIF_RAM_SIZE > si->ri->rdram.dram_size) { DebugMessage(M64MSG_ERROR, "dma_si_write(): unknown SI use"); return; @@ -72,7 +75,8 @@ { int i; - if (si->regs[SI_PIF_ADDR_RD64B_REG] != 0x1FC007C0) + if ((si->regs[SI_PIF_ADDR_RD64B_REG] & 0x1FFFFFFF) != 0x1FC007C0 + || si->regs[SI_DRAM_ADDR_REG] + PIF_RAM_SIZE > si->ri->rdram.dram_size) { DebugMessage(M64MSG_ERROR, "dma_si_read(): unknown SI use"); return; @@ -143,7 +147,8 @@ switch (reg) { case SI_DRAM_ADDR_REG: - si->regs[SI_DRAM_ADDR_REG] = MASKED_WRITE(&si->regs[SI_DRAM_ADDR_REG], value, mask); + /* PlayAgain: a 24-bit DRAM address, as on hardware (a KSEG0 address is not an offset) */ + si->regs[SI_DRAM_ADDR_REG] = MASKED_WRITE(&si->regs[SI_DRAM_ADDR_REG], value, mask) & 0xFFFFFF; break; case SI_PIF_ADDR_RD64B_REG: diff -urN a/code/src/mupen64plus-core/src/vi/vi_controller.c src/code/src/mupen64plus-core/src/vi/vi_controller.c --- a/code/src/mupen64plus-core/src/vi/vi_controller.c +++ b/code/src/mupen64plus-core/src/vi/vi_controller.c @@ -137,6 +137,12 @@ case VI_CURRENT_REG: clear_rcp_interrupt(vi->r4300, MI_INTR_VI); return 0; + + case VI_ORIGIN_REG: + /* PlayAgain: the register holds a 24-bit DRAM address, as on hardware; a game + * that writes a KSEG address (0xA0100000) must not send Glide64 outside RDRAM. */ + vi->regs[VI_ORIGIN_REG] = MASKED_WRITE(&vi->regs[VI_ORIGIN_REG], word, mask) & 0xffffff; + return 0; } vi->regs[reg] = MASKED_WRITE(&vi->regs[reg], word, mask);