#ifndef PW_WENV_H #define PW_WENV_H #include "audio.h" #include "cart.h" #include "fb.h" #include "input.h" #include "pico8.h" #include "wasmrt.h" #define PW_ENV_GPIO_SIZE 128 /** * A host execution context. * * Contains _borrowed_ references to various components required by the * environment host functions. */ struct pw_env_ctx { struct pw_framebuffer *fb; struct pw_audio *audio; struct pw_input *input; struct pw_cart *cart; struct w2c_pico8 *instance; }; /** * An invocation frame exception handler. */ struct pw_env_frame { struct pw_env_frame *parent; jmp_buf buf; }; /** * The Web Assembly host environment. * * This holds the required state to implement the host functions imported by * the PICO-8 `.wasm` module. */ struct w2c_env { struct wasm_rt_memory_t memory; struct wasm_rt_funcref_table_t table; struct pw_env_ctx context; struct pw_env_data { struct pw_env_frame *frame; uint32_t dynamictop; uint32_t tablebase; uint32_t mainloop; uint32_t errstr; uint32_t tempret; uint32_t cartpos; struct { uint32_t index; uint32_t pos; } cartname; uint32_t command; uint32_t commandp; struct { bool pointerlock; bool fssync; volatile bool shutdown; } state; uint32_t btn; uint8_t gpio[PW_ENV_GPIO_SIZE]; } data; }; /** * The Web Assembly host globals. */ struct w2c_global { double infinity; double nan; }; /** * Allocates a new host environment with the specified Web Assembly memory size. */ struct w2c_env *pw_env_init(size_t memory); /** * Frees the host environement. */ void pw_env_free(struct w2c_env *env); /** * Sets a host environment's execution context. */ bool pw_env_link(struct w2c_env *env, struct pw_env_ctx context); /** * Starts the Wasm module. */ bool pw_env_start(struct w2c_env *env); /** * Progresses the Wasm module by a frame. * * `shutdown` is set to true when the cart exits. */ bool pw_env_iterate(struct w2c_env *env, bool *shutdown); #endif