Files
xteink-web-emulator/Makefile
T
2026-07-20 18:00:59 -04:00

80 lines
2.9 KiB
Makefile

# Canonical firmware: clone https://github.com/crosspoint-reader/crosspoint-reader
# next to this repo, then build its PlatformIO `default` environment.
FIRMWARE_REPO ?= ../crosspoint-reader
FIRMWARE_DIR ?= $(FIRMWARE_REPO)/.pio/build/default
SD_SRC ?= $(FIRMWARE_REPO)/sdcard
QEMU_SRC ?= _scratch/qemu-src
QEMU_BIN = $(QEMU_SRC)/build/qemu-system-riscv32
QEMU_WASM_SRC ?= _scratch/qemu-wasm-src
# Working tree for the fast dev loop: edit here, `make web-dev` compiles it incrementally.
DEV_SRC ?= _scratch/qemu-fix-src
# Commit the 0002 patch is diffed against when running `make patch`.
PATCH_BASE ?= 610f8c69bc91dcb4ba81de1b7d2e74cc2adfb31e
WASM_OUT ?= dist/wasm
VARIANT ?= x3
.PHONY: firmware sdimage qemu qemu-wasm qemu-wasm-dev web web-dev web-test patch run run-upstream chip clean
firmware:
esptool --chip esp32c3 merge-bin -o flash.bin \
0x0 $(FIRMWARE_DIR)/bootloader.bin \
0x8000 $(FIRMWARE_DIR)/partitions.bin \
0x10000 $(FIRMWARE_DIR)/firmware.bin
truncate -s 16M flash.bin
cp $(FIRMWARE_DIR)/firmware.elf firmware.elf
# Local-disk implementation of the SD block interface.
sdimage:
scripts/mksd.sh $(SD_SRC) sd.img
qemu:
QEMU_SRC=$(QEMU_SRC) scripts/build-qemu.sh
qemu-wasm:
QEMU_WASM_SRC=$(QEMU_WASM_SRC) WASM_OUT=$(WASM_OUT) scripts/build-qemu-wasm.sh
# Incremental build straight from $(DEV_SRC); no patch reset, so edits compile fast.
qemu-wasm-dev:
QEMU_WASM_DEV=1 QEMU_WASM_SRC=$(DEV_SRC) WASM_OUT=$(WASM_OUT) scripts/build-qemu-wasm.sh
# Regenerate the wasm patch from $(DEV_SRC) once the dev tree is where you want it.
patch:
cd $(DEV_SRC) && git diff --binary $(PATCH_BASE) > $(CURDIR)/qemu/patches/0002-qemu-wasm.patch
# Serve over HTTPS by default so headless/LAN access is cross-origin isolated. Override with WEB_TLS=.
WEB_TLS ?= --tls
web: qemu-wasm
python3 scripts/serve-web.py $(WEB_TLS)
# Fast iteration: incremental compile from $(DEV_SRC), then serve. Run `make patch` before committing.
web-dev: qemu-wasm-dev
python3 scripts/serve-web.py $(WEB_TLS)
web-test:
node scripts/test-web.mjs
# Select X4 with `make run VARIANT=x4`. Add `-display none` for headless.
run: qemu
@sd=sd.img; tmpdir=; \
if [ ! -f "$$sd" ]; then \
tmpdir=$$(mktemp -d); mkdir "$$tmpdir/empty"; sd="$$tmpdir/sd.img"; \
SIZE_MB=64 scripts/mksd.sh "$$tmpdir/empty" "$$sd" >/dev/null; \
echo "Using a blank ephemeral SD card; writes will be discarded when QEMU exits."; \
fi; \
trap 'rm -rf "$$tmpdir"' EXIT; \
$(QEMU_BIN) -machine xteink,variant=$(VARIANT) -L $(QEMU_SRC)/pc-bios \
-serial stdio -drive file=flash.bin,if=mtd,format=raw \
-drive file="$$sd",if=sd,format=raw
# Unpatched release binary retained as the fast generic ESP32-C3 baseline.
run-upstream:
nix run .#run-upstream -- flash.bin
# Legacy Wokwi protocol model; retained as a small host-tested reference.
chip:
$(MAKE) -C chip
clean:
$(MAKE) -C chip clean
rm -f flash.bin flash16.bin firmware.elf