From c4810048c1f76d0088e785f06afbb34baa4afb3a Mon Sep 17 00:00:00 2001 From: theverygaming Date: Mon, 13 Feb 2023 10:00:06 +0100 Subject: [PATCH] emscripten --- .gitignore | 5 +++++ Makefile | 10 +++++++++- src/main.c | 17 +++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 3a9e9dd..fa0df25 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,8 @@ fox32 fox32.exe SDL2.dll + +fox32.html +fox32.wasm +fox32.js +fox32.data diff --git a/Makefile b/Makefile index 4c9a3ad..4d796b7 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,11 @@ SDL2_CONFIG = sdl2-config CFLAGS = -g -Ofast -std=c99 -Wall -Wextra `$(SDL2_CONFIG) --cflags --libs` CC_WIN = x86_64-w64-mingw32-gcc CFLAGS_WIN = -g -Ofast -std=c99 -Wall -Wextra -lmingw32 -lSDL2main -lSDL2 +CC_WASM = emcc +CFLAGS_WASM = -O3 -std=c99 -Wall -Wextra -s TOTAL_MEMORY=70057984 -sALLOW_MEMORY_GROWTH=1 -sUSE_SDL=2 --preload-file fox32os.img TARGET=fox32 TARGET_WIN=fox32.exe +TARGET_WASM=fox32.html CFILES = src/main.c \ src/bus.c \ @@ -29,5 +32,10 @@ $(TARGET_WIN): $(CFILES) sed -i -e 's/fox32_rom/fox32rom/' fox32rom.h $(CC_WIN) -o $@ $(filter %.c, $^) $(CFLAGS_WIN) +$(TARGET_WASM): $(CFILES) $(FOX32ROM_IN) + xxd -i $(FOX32ROM_IN) $(FOX32ROM_OUT) + sed -i -e 's/fox32_rom/fox32rom/' fox32rom.h + $(CC_WASM) -o $@ $(filter %.c, $^) $(CFLAGS_WASM) + clean: - rm -rf fox32 fox32.exe + rm -rf $(TARGET) $(TARGET_WIN) $(TARGET_WASM) diff --git a/src/main.c b/src/main.c index cb31e2d..332b172 100644 --- a/src/main.c +++ b/src/main.c @@ -8,6 +8,10 @@ #include #include #include +#ifdef __EMSCRIPTEN__ +#include +#include +#endif #include "bus.h" #include "cpu.h" @@ -50,6 +54,7 @@ int main(int argc, char *argv[]) { memcpy(vm.memory_rom, fox32rom, sizeof(fox32rom)); size_t disk_id = 0; +#ifndef __EMSCRIPTEN__ for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "--help") == 0) { fprintf(stderr, @@ -87,6 +92,9 @@ int main(int argc, char *argv[]) { return 1; } } +#else + new_disk("fox32os.img", disk_id++); +#endif if (!vm.headless) { if (SDL_Init(SDL_INIT_VIDEO) != 0) { @@ -116,6 +124,10 @@ int main(int argc, char *argv[]) { tick_start = SDL_GetTicks(); tick_end = SDL_GetTicks(); +#ifdef __EMSCRIPTEN__ + emscripten_set_main_loop(main_loop, FPS, 1); +#endif + while (!done && !bus_requests_exit) { main_loop(); @@ -132,6 +144,11 @@ int main(int argc, char *argv[]) { } void main_loop(void) { +#ifdef __EMSCRIPTEN__ + if (done || bus_requests_exit) { + emscripten_cancel_main_loop(); + } +#endif int dt = SDL_GetTicks() - tick_start; tick_start = SDL_GetTicks(); if (!dt)