CC := gcc CFLAGS := -std=c11 -O2 -Wall -Wextra -Wno-unused-parameter -g -D_POSIX_C_SOURCE=200809L LDFLAGS := SRCDIR := src BUILD := build # The native binary is every .c except the wasm host layer. SRCS := $(filter-out $(SRCDIR)/wasm.c,$(wildcard $(SRCDIR)/*.c)) OBJS := $(patsubst $(SRCDIR)/%.c,$(BUILD)/%.o,$(SRCS)) BIN := $(BUILD)/sl0pboy # ---- wasm ---- # Only the portable core plus the wasm host layer: render/kitty/control/main # are terminal-and-POSIX bound and have no meaning in a browser. # zig cc carries its own wasm backend, linker and wasi-libc, so there is no # emscripten dependency. Reactor model = no main(), exports stay callable. WASM_SRCS := $(addprefix $(SRCDIR)/,cpu.c ppu.c timer.c gb.c cart.c wasm.c) WASM_OUT := web/sl0pboy.wasm WASM_CC := zig cc # --stack-size: zig defaults the wasm stack to 16MB, which lands in the # module's *initial* memory - so every Game Boy on the page cost 16.25MB # before running an instruction. The machine is a static and the C code has no # deep recursion, so 256KB is plenty: 260 pages -> 8 pages per instance. WASM_FLAGS := -target wasm32-wasi -mexec-model=reactor -O2 -std=c11 \ -Wall -Wextra -Wno-unused-parameter \ -Wl,--no-entry -Wl,--export-dynamic -Wl,--strip-all \ -Wl,-z,stack-size=262144 .PHONY: all clean run wasm all: $(BIN) wasm: $(WASM_OUT) $(WASM_OUT): $(WASM_SRCS) $(wildcard $(SRCDIR)/*.h) @mkdir -p $(dir $@) $(WASM_CC) $(WASM_FLAGS) $(WASM_SRCS) -o $@ @echo "built $@ ($$(du -h $@ | cut -f1))" $(BIN): $(OBJS) $(CC) $(CFLAGS) $(OBJS) -o $@ $(LDFLAGS) $(BUILD)/%.o: $(SRCDIR)/%.c | $(BUILD) $(CC) $(CFLAGS) -c $< -o $@ $(BUILD): mkdir -p $(BUILD) clean: rm -rf $(BUILD) $(WASM_OUT) run: $(BIN) $(BIN) $(ROM)