Commit graph

29 commits

Author SHA1 Message Date
刘皓
ee31cccb81
Serialize PRNG state in libretro save states
Save states in libretro builds now also contain the state of the
pseudorandom number generator used to implement the WASI `random_get`
function for better save state determinism.

I've also changed the PRNG from MT19937 to PCG.
2025-06-11 15:54:21 -04:00
刘皓
6bddd82618
Change sandbox_str() to return a guard object instead of a pointer to a static string in big-endian libretro builds 2025-06-03 13:14:51 -04:00
刘皓
8ca753d85d
Continue implementing save state deserialization in libretro builds 2025-05-27 16:59:41 -04:00
刘皓
e957af931c
Implement save state serialization for WASI file descriptors in libretro builds 2025-05-23 23:59:29 -04:00
刘皓
134e08ce6a
Allow the entire codebase to build with -fno-exceptions -fno-rtti in libretro builds 2025-05-16 21:59:54 -04:00
刘皓
28fb36af98
Capitalize /Dist, /Game, /Save and /System in libretro builds for consistency 2025-05-13 17:53:59 -04:00
刘皓
67e61917a7
Apply big-endian fixes to binding-sandbox/wasi.cpp as well 2025-05-12 12:39:08 -04:00
刘皓
3ba12e5672
Fix handling of big-endian platforms in libretro builds
In big-endian libretro builds, the WebAssembly memory is reversed, so no
byte-swapping is required to read from/write to WebAssembly memory
(which is little-endian).

However, that means the ways to get and set values in WebAssembly memory
are endianness-dependent, so I've added the correct such ways for
big-endian platforms.
2025-05-10 18:55:14 -04:00
刘皓
b8d785b7e1
Implement growing VM memory in libretro builds
The binding coroutines in libretro builds are constructed on the VM
stack, so reallocating the VM memory would corrupt the memory of any
currently existing coroutines.

I've changed it so that the coroutines are no longer constructed on the
VM stack so that they're unaffected by VM memory reallocations, and
added a "slot" mechanism for storing variables on the VM stack. (Any
Ruby `VALUE`s used by a coroutine have to be stored on the VM stack so
that the Ruby garbage collector doesn't free them while they're being
used, which is why the slot mechanism is necessary.)
2025-05-09 22:49:13 -04:00
刘皓
3e3df818aa
Use C getcwd() instead of Ruby Dir.pwd() to get CWD in libretro builds 2025-05-06 18:02:03 -04:00
刘皓
13c0657691
Handle current working directory properly in libretro builds
Any relative paths that the game tries to access in libretro builds will
now be relative to whatever is the current working directory in the Ruby
sandbox, which will also now be initialized to the game directory during
initialization. Before, all of the bindings that took paths were
hardcoded to prepend the path with the game directory.
2025-05-06 16:55:28 -04:00
刘皓
74e5cc763c
Implement Graphics.play_movie in libretro builds 2025-05-03 18:27:44 -04:00
刘皓
0d07aff3e2
Remove exceptions from the sandbox implementation in libretro builds 2025-04-25 13:31:32 -04:00
刘皓
2b3a97e83c
Fix frame duping bugs in libretro builds
* Fixed a bug where frames are still duped when the frontend is
  fast-forwarding

* Fixed a bug where manual frame duping (without
  `RETRO_ENVIRONMENT_GET_CAN_DUPE`) causes screen flickering during a
  `Graphics.transition` call
2025-04-25 10:45:11 -04:00
刘皓
8457ac1598
Rename mkxp-threads to mkxp-polyfill 2025-04-02 13:06:17 -04:00
刘皓
322d61b604
Allow adjusting Ruby GC parameters in libretro builds
There seems to be a memory leak somewhere. I'm tweaking the GC
parameters to be more conservative to help track down the leak.
2025-03-21 11:56:45 -04:00
刘皓
eeef9ff943
Don't make sandbox_malloc into a coroutine
Okay, the coroutine implementation of `sandbox_malloc` is clearly
broken. It would be working if Asyncify instrumented the `memory.grow`
WebAssembly instruction, but it doesn't instrument it.

This commit reverts commit 42c4ff9497 and
also increases the default VM memory allocation from 64 MiB to 96 MiB to
account for the lack of ability to increase the memory allocation at run
time. I'll find some new way to implement increasing the memory
allocation later.
2025-03-18 13:49:41 -04:00
刘皓
9ed11ecffa
Use libretro's PlayStation 3 Docker image instead of ScummVM's 2025-02-22 16:04:51 -05:00
刘皓
42c4ff9497
Make sandbox_malloc into a coroutine
According to AddressSanitizer, when `sandbox_malloc` causes the
WebAssembly memory to grow in size, every single coroutine on the
sandbox stack gets corrupted. So if `sandbox_malloc` is going to cause
the memory to grow in size, we need to yield so that there are no
coroutines on the sandbox stack while the reallocation occurs.
2025-02-17 00:38:40 -05:00
刘皓
206e8508b6
Replace WABT's WebAssembly runtime with a custom implementation 2025-02-11 20:40:55 -05:00
刘皓
d7e002f9b0
Compile Ruby with -Oz -DNDEBUG in libretro builds 2025-02-02 17:05:15 -05:00
刘皓
84ca884f84
Reimplement WASI filesystem on top of existing mkxp-z filesystem implementation
This allows more flexibility when loading games in libretro builds,
since we can now load games either from a directory or from a ZIP or 7Z
archive. Also, the path cache is now active for all filesystem calls
made from inside Ruby.
2025-01-25 22:03:52 -05:00
刘皓
66177c3981
Remove useless try/catch blocks in destructors in sandbox-bindgen 2025-01-23 22:50:17 -05:00
刘皓
12a74bb9e7
Don't call __wasm_call_dtors() when sandbox is destroyed
This line is sometimes throwing an exception when the sandbox is
destroyed because the sandbox is being destroyed while it's in the
middle of yielding, and I guess it's undefined behaviour to call
`__wasm_call_dtors()` while the sandbox is yielding.

As evidenced by the try/catch block, I anticipated that
`__wasm_call_dtors()` could throw an exception. But I forgot that you
can't catch exceptions in a destructor! Let's just not call
`__wasm_call_dtors()` then. What harm can that do anyways, given that we
clean up all the memory used by the sandbox in the immediately following
lines in the destructor?
2025-01-23 22:17:39 -05:00
刘皓
91fdc3028b
Fix arguments passed to ruby_exec_node() in libretro builds
This is really stupid. But it seems this undefined behaviour here was
causing the sporadic Ruby VM crashes I experienced before.
2025-01-22 22:45:54 -05:00
刘皓
7d753e2655
Implement even more of binding-sandbox
I've made it so that `Graphics.update` pauses the Ruby VM and returns to
the libretro frontend. Once the libretro frontend calls `retro_run()`
again, the Ruby VM resumes. This allows the libretro frontend to control
the rendering loop.
2025-01-22 21:52:55 -05:00
刘皓
835795a0c6
Use different Ruby command-line arguments in libretro builds
I've decided to stop gambling with ways to make `-e` not crash Ruby on
startup in libretro builds (see commit
1473416a5a for context). Making Ruby load
a dummy script seems to work better.
2025-01-19 11:56:47 -05:00
刘皓
1473416a5a
Fix crashes when starting up libretro Ruby sandbox (hopefully)
Guys, I think I'm going insane. Every time I build the libretro Ruby
sandbox with a different version of Ruby, or even when I build Ruby at a
different path on my computer, there's some chance that the builds
produced with that version of Ruby and/or that path on my computer
result in Ruby crashing on startup in libretro builds. I've been
tweaking these command-line arguments that are passed to Ruby for a
while now, and I *think* these are the correct ones that will stop Ruby
from crashing.
2025-01-17 01:23:18 -05:00
刘皓
510f1b0211
Move sandbox files to binding-sandbox directory 2025-01-16 22:16:14 -05:00
Renamed from src/sandbox/sandbox.cpp (Browse further)