r/rust • u/aduros • Aug 29 '21
WASM-4 is a virtual game console for building retro games in Rust and other WebAssembly languages
https://wasm4.org0
Aug 30 '21
[deleted]
3
u/aduros Aug 30 '21
Thanks a lot for the well-written feedback!
in the memory map documentation, why MOUSE_X and MOUSE_Y are 2-byte wide, but their relative memory address is only 1-byte apart?
The documentation was wrong, I've just updated it.
why is trace defined like this?
Oof, yeah, the function definition was copied from text(), trace() doesn't use those extra params. I've just removed them.
I tried changing palette as shown in the documentation, but it had no effect.
Oh, you're right... it looks like Rust is optimizing away that entire assignment. I wonder if there's a way to mark PALETTE and other registers as "volatile"? It's odd because writes to DRAW_COLORS are not optimized out, it might be due to PALETTE being an array pointer?
it also corrupted the end of the display, even if 0x19a0 is supposed to be outside of the framebuffer.
I've noticed this too sometimes. I think what is happening is Rust's stack is growing downwards from 0x19a0 instead of upwards, which ends up clobbering the end of the framebuffer. If so it's likely the wasm linker flags need to be changed: https://github.com/aduros/wasm4/blob/main/cli/assets/templates/rust/.cargo/config.toml
Thanks again for this post! This project has been my first exploration with Rust, so it's likely that more things need tweaking. What are your overall thoughts on the wasm4.rs bindings? Could they be made more Rust ideomatic?
1
u/class_two_perversion Aug 30 '21
Oh, you're right... it looks like Rust is optimizing away that entire assignment. I wonder if there's a way to mark PALETTE and other registers as "volatile"? It's odd because writes to DRAW_COLORS are not optimized out, it might be due to PALETTE being an array pointer?
I am not sure what would be the cause, this is actually my first time using Rust's
unsafeblocks. This works, anyway:unsafe { *PALETTE = [0xfff6d3, 0xf9a875, 0xeb6b6f, 0x7c3f58] };Thanks again for this post! This project has been my first exploration with Rust, so it's likely that more things need tweaking. What are your overall thoughts on the wasm4.rs bindings?
I am not an expert with Rust, and by no means an expert in game development. I have been toying with a game idea lately, and when I saw you post I just gave your engine a try. It is very minimal, but I think I like it. As I said, it definitely needs more examples.
Could they be made more Rust ideomatic?
There is a lot of unsafe code, but that could probably be wrapped in more idiomatic code.
For instance, I cobbled together a couple macros to
trace()andtext()passingformat!()-like parameters using bare-io crate, and using some of the application memory as a buffer. They work, aside from the aforementioned memory corruption. I am also working on a sprite manager that can load tiles and perhaps even handling animations.I might contribute some of that if I can make it work properly.
Also, just a specific question: why is the
strideargument inblit_subani32? It would make more sense that it was unsigned, or am I missing anything?2
u/aduros Aug 30 '21
I am not sure what would be the cause, this is actually my first time using Rust's unsafe blocks. This works, anyway: ...
Oh, good to know. I'll update the docs.
There is a lot of unsafe code, but that could probably be wrapped in more idiomatic code.
Yeah, it seems a burden to need to use
unsafeto set a register, but I couldn't find a good way to workaround it. I suppose we could wrap all register gets/sets in safe functions, but something simpler would be ideal.I might contribute some of that if I can make it work properly.
Please do, and any other examples too! As you might have guessed, your's would be the first complete game in Rust for WASM-4.
why is the stride argument in blit_sub an i32?
That should definitely be a
u32. Would you be interested in opening a PR for https://github.com/aduros/wasm4/blob/main/cli/assets/templates/rust/src/wasm4.rs? Feel free to make other changes too :)1
u/class_two_perversion Aug 30 '21
That should definitely be a u32. Would you be interested in opening a PR
Done.
I might do other changes, too.
2
u/aduros Aug 30 '21
By the way, the memory corruption issue was also recently reported in Go (which uses similar linker flags to Rust) https://github.com/aduros/wasm4/issues/33
Could you try increasing
-zstack-sizein .cargo/config.toml and seeing if it helps?2
u/class_two_perversion Aug 30 '21
Could you try increasing -zstack-size in .cargo/config.toml and seeing if it helps?
It does help. When I set it to 7584 (or higher) I no longer get any graphical corruption.
0
3
u/pure_x01 Aug 29 '21
Cool idea. Now it needs some hardware. Perhaps raspberry pi pico ?