psx-sdk-rs icon indicating copy to clipboard operation
psx-sdk-rs copied to clipboard

Add demo using bevy

Open ayrtonm opened this issue 10 months ago • 7 comments

Bevy is adding no_std support so it will be possible to use a subset of it on the PS1. Once it supports platforms that lack CAS and other atomic operations it'd be nice to add a demo using bevy to this repo.

ayrtonm avatar Feb 07 '25 21:02 ayrtonm

This should be possible to play with now using Bevy/main.

BenjaminBrienen avatar Mar 07 '25 13:03 BenjaminBrienen

nice, thanks for the heads up. I don't have time to test it out for a good while but if anyone is using it and puts together some demo PRs are welcome.

ayrtonm avatar Mar 13 '25 15:03 ayrtonm

Did some experimentation with this, there's definitely work to be done, but unfortunately I think there's an LLVM bug that will prevent Bevy from working for now. First off, there's a patch to foldhash I've developed to give it portable-atomic support:

[patch.crates-io.foldhash]
git = "https://github.com/bushrat011899/foldhash"
branch = "portable_atomic"

This allows the following crates to compile in a test project:

  • bevy_ptr
  • bevy_platform
  • bevy_utils
  • bevy_math
  • bevy_color

Unfortunately, bevy_tasks and bevy_reflect throw an LLVM error:

rustc-LLVM ERROR: Cannot select: 0x1ee0b4c1ef8: ch = MipsISD::Sync 0x1ee0bfb15c0, Constant:i32<0>, C:\Users\Zac.rustup\toolchains\nightly-2024-12-21-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\sync\atomic.rs:3693:23 0x1ee0b4c45c8: i32 = Constant<0> In function: _ZN4core4sync6atomic5fence17h2f4ec7e180987d61E

That particular part of the core crate relates to core::sync::atomic::fence:

pub fn fence(order: Ordering) {
    // SAFETY: using an atomic fence is safe.
    unsafe {
        match order {
            Acquire => intrinsics::atomic_fence_acquire(),
            Release => intrinsics::atomic_fence_release(),
            AcqRel => intrinsics::atomic_fence_acqrel(),
            SeqCst => intrinsics::atomic_fence_seqcst(),
            Relaxed => panic!("there is no such thing as a relaxed fence"),
        }
    }
}

I believe this is related to llvm-project/#61166, but I'm not sure. Definitely something to do with incomplete atomic support. I did try following these instructions for patching and building rustc to add atomic support to the PSX target, which might resolve the LLVM error, but I'm not certain. I had issues getting cargo to work with the custom rustc toolchain (probably an API mismatch as I'd have a version of cargo too new for that specific custom rustc).

I've uploaded my experimentation here. If you compile as-is it'll work, and with the fail feature enabled it will...fail. fail just enables the bevy_tasks dependency which throws the LLVM error in question. If you have any insights please let me know! I'd love to try and get this working even if I do have to upstream some more patches to crates like foldhash.

bushrat011899 avatar May 23 '25 13:05 bushrat011899

Ahh I see, it's #6, I should've looked first! I'm not sure where fences are being used internally to cause the miscompilation, I'll see if I can find it, but I don't have high hopes.

bushrat011899 avatar May 23 '25 14:05 bushrat011899

This may or may not be relevant (I'll try to check out your bevy expts anyway), but I'm currently trying to debug why there's a psx-sdk-rs issue on VBlank (wait never completes) when initialising controllers and using the CD-ROM. I can get controllers working on their own, and CD reading on its own, but having more than one peripheral / interrupt mask set seems to be causing issues.

I hadn't noticed #6 , but it sounds like it might help explain what I am seeing, at least its another avenue to investigate, so thanks!

hornc avatar May 23 '25 22:05 hornc

Woo, #41 is now enough for Bevy to work on the PS1! Still needs the patched LLVM, but I've updated the instructions to nightly-2025-05-23.

bushrat011899 avatar May 26 '25 09:05 bushrat011899

Here is a silly example. It requires a patched LLVM and several other horrors, but it runs!

Image

bushrat011899 avatar May 26 '25 11:05 bushrat011899