# VirtualXT -- Web frontend Makefile
#
# Builds a standalone web frontend using Emscripten.
# The emulator core (src/frontend/core.c) is compiled unchanged;
# this Makefile only adds tools/web/main.c as the libretro host glue.
#
# Usage:
#   make -C tools/web
#   or from project root:
#   make web

CC      = emcc
AR      = emar
CFLAGS  = -std=gnu11 -Wall -Wextra -Wpedantic -I../../src -I../../tools/libretro -O2
EMFLAGS = -s WASM=1 \
          -s MODULARIZE=1 \
          -s EXPORT_NAME="VirtualXTModule" \
          -s ALLOW_MEMORY_GROWTH=1 \
          -s NO_EXIT_RUNTIME=1 \
          -s INITIAL_MEMORY=33554432 \
          -s EXPORTED_FUNCTIONS='["_wasm_initialize","_wasm_run","_wasm_send_key","_wasm_send_mouse","_wasm_create_vfs_file","_wasm_mount_disk","_wasm_load_hd","_wasm_framebuffer_ptr","_wasm_framebuffer_width","_wasm_framebuffer_height","_wasm_framebuffer_pitch","_wasm_audio_read","_wasm_disk_led_active","_wasm_set_options","_wasm_string_alloc","_wasm_string_free","_wasm_set_crt","_wasm_set_noise","_wasm_set_video","_wasm_set_turbo","_wasm_reset","_wasm_reload","_wasm_hostfs_clear","_wasm_hostfs_add_file","_wasm_hostfs_set_root","_wasm_hostfs_set_launch_path","_malloc","_free"]' \
          -s EXPORTED_RUNTIME_METHODS='["ccall","cwrap","HEAPU8"]'

CORE_SRCS := ../../src/utils/utils.c \
             ../../src/vxt/peripheral.c ../../src/vxt/cpu.c ../../src/vxt/alu.c \
             ../../src/vxt/div.c ../../src/vxt/machine.c ../../src/vxt/cpu_v20.c \
             ../../src/vxt/cpu_cycles.c ../../src/vxt/cpu_remap.c \
             ../../src/modules/module_table.c \
             ../../src/modules/rom/rom.c ../../src/modules/disk/disk.c \
             ../../src/modules/chipset/chipset.c ../../src/modules/chipset/pic.c \
             ../../src/modules/chipset/pit.c ../../src/modules/chipset/dma.c \
             ../../src/modules/chipset/ppi.c \
             ../../src/modules/cga/cga.c \
             ../../src/modules/ega/ega.c \
             ../../src/modules/hgc/hgc.c \
             ../../src/modules/uart/uart.c \
             ../../src/modules/mouse/mouse.c \
             ../../src/modules/joystick/joystick.c \
             ../../src/modules/hostfs/hostfs.c \
             ../../src/modules/ems/ems.c \
             ../../src/modules/rtc/rtc.c

FRONT_SRCS := ../../src/frontend/core.c \
              ../../src/frontend/ini.c \
              ../../src/frontend/crt.c \
              ../../src/frontend/audio_mixer.c

WEB_SRCS := main.c

SRCS := $(CORE_SRCS) $(FRONT_SRCS) $(WEB_SRCS)

# Build objects in a local build/ dir to avoid conflicts with root Makefile
BUILD_DIR := build
OBJS := $(patsubst %.c,$(BUILD_DIR)/%.o,$(notdir $(SRCS)))

# VPATH lets make find source files by basename
VPATH := $(sort $(dir $(SRCS)))

OUT_JS  := virtualxt.js
OUT_WASM:= virtualxt.wasm

.PHONY: all clean

all: $(OUT_JS)

$(OUT_JS): $(OBJS)
	$(CC) $(EMFLAGS) -o $@ $(OBJS)

$(BUILD_DIR)/%.o: %.c | $(BUILD_DIR)
	$(CC) $(CFLAGS) -c -o $@ $<

$(BUILD_DIR):
	mkdir -p $(BUILD_DIR)

clean:
	rm -rf $(BUILD_DIR) $(OUT_JS) $(OUT_WASM)
