aduros / wasm4

Build retro games using WebAssembly for a fantasy console.
https://wasm4.org
ISC License
1.14k stars 168 forks source link

Could we get usable API functions for ALL registers vs RAW pointers? #251

Open joshgoebel opened 2 years ago

joshgoebel commented 2 years ago

This would make it easier to port WASM-4 goes too and from other platforms since one only needs to stub out the API.

Examples (in Zig)

Now:

pub const SYSTEM_FLAGS: *u8 = @intToPtr(*u8, 0x1f);

After:


pub const SYSTEM_FLAGS: *u8 = @intToPtr(*u8, 0x1f);
pub fn setSystemFlags(flag: u8) void {
  SYSTEM_FLAGS.* |= flag;
}
pub fn unsetSystemFlags(flag: u8) void {
  // ...
}

Porting to another platform would then only require implementing an equivalent (or stub) setSystemFlags function... and of course the compiler would likely optimize this down to nothing (by inlining the code) anyways, so I don't see a downside?

JerwuQu commented 2 years ago

Unless I've mistaken your intention, I don't see how this makes porting games easier unless you also force developers to use the getters and setters (and to never touch registers directly). This becomes pretty impractical for things like FRAMEBUFFER where voodoo with the raw data is less uncommon. Therefore, I'd say implementing the memory registers in a non-WASM runtime is actually easier than implementing the functions you proposed: just point the pointers in wasm.h to variables rather than absolute memory addresses and do what the current WASM-4 runtimes do with the data in them. I've started work on exactly this just for fun and it works great so far, so shoot me a message if you have any questions about how it could be done in practice.