Compare commits

..

1 Commits

Author SHA1 Message Date
169ae3d908 compile loader to c as well 2023-04-05 22:57:38 +02:00
8 changed files with 1154 additions and 49 deletions

View File

@@ -1,11 +1,11 @@
WASM3_C := $(wildcard wasm3/source/*.c) WASM3_C := $(wildcard wasm3/source/*.c)
WASM3_O := $(WASM3_C:.c=.o) WASM3_O := $(WASM3_C:.c=.o)
uw8-wasm3: main.o $(WASM3_O) platform.o wasm-rt-impl.o uw8-wasm3: main.o $(WASM3_O) platform.o loader.o wasm-rt-impl.o
gcc -g -lm -lSDL2 -o uw8-wasm3 $^ gcc -g -lm -lSDL2 -o uw8-wasm3 $^
run: uw8-wasm3 .PHONY run: uw8-wasm3 .PHONY
./uw8-wasm3 never_sleeps.uw8 ./uw8-wasm3 skipahead.uw8
run-ts: run-ts:
deno run --allow-read main.ts deno run --allow-read main.ts

1080
loader.c Normal file

File diff suppressed because it is too large Load Diff

54
loader.h Normal file
View File

@@ -0,0 +1,54 @@
/* Automatically generated by wasm2c */
#ifndef LOADER_H_GENERATED_
#define LOADER_H_GENERATED_
#include <stdint.h>
#include "wasm-rt.h"
/* TODO(binji): only use stdint.h types in header */
#ifndef WASM_RT_CORE_TYPES_DEFINED
#define WASM_RT_CORE_TYPES_DEFINED
typedef uint8_t u8;
typedef int8_t s8;
typedef uint16_t u16;
typedef int16_t s16;
typedef uint32_t u32;
typedef int32_t s32;
typedef uint64_t u64;
typedef int64_t s64;
typedef float f32;
typedef double f64;
#endif
#ifdef __cplusplus
extern "C" {
#endif
struct Z_env_instance_t;
extern wasm_rt_memory_t* Z_envZ_memory(struct Z_env_instance_t*);
typedef struct Z_loader_instance_t {
/* import: 'env' 'memory' */
wasm_rt_memory_t *Z_envZ_memory;
u32 w2c_g0;
u32 w2c_g1;
u32 w2c_g2;
} Z_loader_instance_t;
void Z_loader_init_module(void);
void Z_loader_instantiate(Z_loader_instance_t*, struct Z_env_instance_t*);
void Z_loader_free(Z_loader_instance_t*);
/* export: 'load_uw8' */
u32 Z_loaderZ_load_uw8(Z_loader_instance_t*, u32);
/* export: 'uncompress' */
u32 Z_loaderZ_uncompress(Z_loader_instance_t*, u32, u32);
#ifdef __cplusplus
}
#endif
#endif /* LOADER_H_GENERATED_ */

Binary file not shown.

41
main.c
View File

@@ -1,5 +1,6 @@
#include "wasm3/source/wasm3.h" #include "wasm3/source/wasm3.h"
#include "wasm3/source/m3_env.h" #include "wasm3/source/m3_env.h"
#include "loader.h"
#include "platform.h" #include "platform.h"
#include "SDL2/SDL.h" #include "SDL2/SDL.h"
#include "SDL2/SDL_video.h" #include "SDL2/SDL_video.h"
@@ -261,15 +262,21 @@ void linkPlatformFunctions(IM3Runtime runtime, IM3Module cartMod, Z_platform_ins
} }
} }
void* loadUw8(uint32_t* sizeOut, IM3Runtime runtime, IM3Function loadFunc, const char* filename) { void* loadUw8(uint32_t* sizeOut, IM3Runtime runtime, const char* filename) {
size_t uw8Size; size_t uw8Size;
void* uw8 = loadFile(&uw8Size, filename); void* uw8 = loadFile(&uw8Size, filename);
uint8_t* memory = m3_GetMemory(runtime, NULL, 0);
memcpy(memory, uw8, uw8Size); wasm_rt_memory_t memory;
verifyM3(runtime, m3_CallV(loadFunc, (uint32_t)uw8Size)); memory.data = m3_GetMemory(runtime, NULL, 0);
verifyM3(runtime, m3_GetResultsV(loadFunc, sizeOut)); memory.max_pages = memory.pages = 4;
memory.size = 4 * 65536;
Z_loader_instance_t loader;
Z_loader_instantiate(&loader, (struct Z_env_instance_t*)&memory);
memcpy(memory.data, uw8, uw8Size);
*sizeOut = Z_loaderZ_load_uw8(&loader, (uint32_t)uw8Size);
void* wasm = malloc(*sizeOut); void* wasm = malloc(*sizeOut);
memcpy(wasm, memory, *sizeOut); memcpy(wasm, memory.data, *sizeOut);
return wasm; return wasm;
} }
@@ -345,32 +352,20 @@ int main(int argc, const char** argv) {
uint32_t* pixels32 = malloc(320*240*4); uint32_t* pixels32 = malloc(320*240*4);
wasm_rt_init();
Z_loader_init_module();
Z_platform_init_module();
IM3Environment env = m3_NewEnvironment(); IM3Environment env = m3_NewEnvironment();
IM3Runtime loaderRuntime = m3_NewRuntime(env, 65536, NULL); IM3Runtime loaderRuntime = m3_NewRuntime(env, 65536, NULL);
loaderRuntime->memory.maxPages = 4; loaderRuntime->memory.maxPages = 4;
verifyM3(loaderRuntime, ResizeMemory(loaderRuntime, 4)); verifyM3(loaderRuntime, ResizeMemory(loaderRuntime, 4));
size_t loaderSize;
void* loaderWasm = loadFile(&loaderSize, "loader.wasm");
IM3Module loaderMod;
verifyM3(loaderRuntime, m3_ParseModule(env, &loaderMod, loaderWasm, loaderSize));
loaderMod->memoryImported = true;
verifyM3(loaderRuntime, m3_LoadModule(loaderRuntime, loaderMod));
verifyM3(loaderRuntime, m3_CompileModule(loaderMod));
verifyM3(loaderRuntime, m3_RunStart(loaderMod));
IM3Function loadFunc;
verifyM3(loaderRuntime, m3_FindFunction(&loadFunc, loaderRuntime, "load_uw8"));
uint32_t cartSize; uint32_t cartSize;
void* cartWasm = loadUw8(&cartSize, loaderRuntime, loadFunc, argv[1]); void* cartWasm = loadUw8(&cartSize, loaderRuntime, argv[1]);
m3_FreeRuntime(loaderRuntime); m3_FreeRuntime(loaderRuntime);
wasm_rt_init();
Z_platform_init_module();
bool quit = false; bool quit = false;
while(!quit) { while(!quit) {
Uw8Runtime runtime; Uw8Runtime runtime;

24
main.ts
View File

@@ -1,24 +0,0 @@
let U8 = (...a) => new Uint8Array(...a);
let memory = new WebAssembly.Memory({ initial: 4 });
let memU8 = U8(memory.buffer);
let importObject = {
env: {
memory
}
};
let loaderWasm = await Deno.readFile("loader.wasm");
let loader = (await WebAssembly.instantiate(loaderWasm, importObject)).instance;
let platformUw8 = await Deno.readFile("platform.uw8");
console.log("platform.uw8 size: " + platformUw8.byteLength);
memU8.set(U8(platformUw8));
let platformSize = loader.exports.load_uw8(platformUw8.byteLength);
let platformWasm = new ArrayBuffer(platformSize);
U8(platformWasm).set(memU8.slice(0, platformSize));
console.log("Unpacked platform size: " + platformSize);
console.log("First byte: " + U8(platformWasm)[0]);

Binary file not shown.

BIN
skipahead.uw8 Normal file

Binary file not shown.