From fe788058727f9d80c1908844b286fd31cedc821d Mon Sep 17 00:00:00 2001 From: arti Date: Sun, 15 Nov 2026 05:09:53 +0000 Subject: [PATCH] M7.3: add the real-hardware marker gate with MAME acceptance --- scripts/m7/hw-check.lua | 111 ++++++++++ scripts/m7/hwmark.c | 74 +++++++ scripts/tests/m7/hardware.sh | 383 +++++++++++++++++++++++++++++++++++ 3 files changed, 568 insertions(+) create mode 100644 scripts/m7/hw-check.lua create mode 100644 scripts/m7/hwmark.c create mode 100755 scripts/tests/m7/hardware.sh diff --git a/scripts/m7/hw-check.lua b/scripts/m7/hw-check.lua new file mode 100644 index 0000000..0a87471 --- /dev/null +++ b/scripts/m7/hw-check.lua @@ -0,0 +1,111 @@ +-- hw-check.lua -- M7.3 real-hardware marker gate, MAME-equivalent acceptance. +-- +-- Loads the same COFF that hostload.py sends to the real unit +-- (scripts/m7/hwmark.c, linked at .text 018C0000 / .data 018D0000 / +-- .bss 018D8000) through vendor/vt1200/dwt/loader/loader.lua, then requires +-- exactly what hostload.py's verify_markers() requires: +-- +-- 018E0000 start == the loader result block's entry (018C0000) +-- 018E0020 data == 0xDEADBEEF (copied .data) +-- 018E0040 data2 == 0x12345678 (copied .data) +-- 018E0060 bss == 0 (zeroed .bss; loader pre-fills A5A5) +-- 018E0080 count advances (the child is scheduled, not jumped) +-- +-- and that the loader result status is 0. LOADER_AT/LOADER_END come from the +-- M4-style environment; the counter is sampled after the child has run, then +-- again one frame before the loader exits. + +dofile(os.getenv("M4_LOADER_LUA")) + +local AT = tonumber(os.getenv("LOADER_AT") or "2500") +local ENDF = tonumber(os.getenv("LOADER_END") or "2700") +local RESULT = 0x01882D00 + +local CELLS = { + { 0x018E0000, "start" }, + { 0x018E0020, "data" }, + { 0x018E0040, "data2" }, + { 0x018E0060, "bss" }, + { 0x018E0080, "count" }, +} + +-- First sample: a few frames after injection, so the child has started. +-- Second sample: the last frame before loader.lua inspects and exits. +local SAMPLE0 = math.max(AT + 5, ENDF - 11) +local SAMPLE1 = ENDF - 1 + +local frames = 0 +local first_count = nil +local checked = false + +local function r32(prog, addr) + local ok, v = pcall(function() return prog:read_u32(addr) end) + if ok then return v end + return nil +end + +emu.register_frame_done(function() + frames = frames + 1 + + if frames == SAMPLE0 and first_count == nil then + local prog = manager.machine.devices[':maincpu'].spaces['program'] + first_count = r32(prog, 0x018E0080) + end + + if frames == SAMPLE1 and not checked then + checked = true + local prog = manager.machine.devices[':maincpu'].spaces['program'] + local v = {} + for _, cell in ipairs(CELLS) do + v[cell[2]] = r32(prog, cell[1]) + end + local status = r32(prog, RESULT + 0x000) + local entry = r32(prog, RESULT + 0x020) + local count0 = first_count or 0 + local count1 = v.count or 0 + + print(string.format( + "M7 HW: start=0x%08X data=0x%08X data2=0x%08X bss=0x%08X count=%d->%d status=%d entry=0x%08X", + v.start or 0, v.data or 0, v.data2 or 0, v.bss or 0, + count0, count1, status or -1, entry or 0)) + + local problems = {} + if status ~= 0 then + problems[#problems + 1] = string.format("loader status=%s, want 0", + tostring(status)) + end + if entry ~= 0x018C0000 then + problems[#problems + 1] = string.format( + "loader entry=0x%08X, want 0x018C0000", entry or 0) + end + if v.start ~= entry then + problems[#problems + 1] = string.format( + "start=0x%08X, want the entry 0x%08X", v.start or 0, entry or 0) + end + if v.data ~= 0xDEADBEEF then + problems[#problems + 1] = string.format("data=0x%08X, want 0xDEADBEEF", + v.data or 0) + end + if v.data2 ~= 0x12345678 then + problems[#problems + 1] = string.format( + "data2=0x%08X, want 0x12345678", v.data2 or 0) + end + if v.bss ~= 0 then + problems[#problems + 1] = string.format("bss=0x%08X, want 0", + v.bss or 0) + end + if count0 <= 0 or count1 <= count0 then + problems[#problems + 1] = string.format( + "count did not advance (%d -> %d)", count0, count1) + end + + if #problems == 0 then + print("M7 HW MAME: PASS") + else + for _, p in ipairs(problems) do + print("M7 HW: FAIL: " .. p) + end + print("M7 HW MAME: FAIL") + end + end +end, "m7-hw-check") diff --git a/scripts/m7/hwmark.c b/scripts/m7/hwmark.c new file mode 100644 index 0000000..e2f5698 --- /dev/null +++ b/scripts/m7/hwmark.c @@ -0,0 +1,74 @@ +/* + * hwmark.c -- M7.3 real-hardware marker client, built at the proven VT1200 + * hardware origins: + * + * .text 018C0000h + * .data 018D0000h + * .bss 018D8000h + * + * This is the COFF image that dwt/loader/hostload.py pokes and that + * dwt/loader/loader.asm places through create_process. The marker cells are + * the ones hostload.py's MARKER_CELLS names (all TMS34010 *bit* addresses): + * + * 018E0000 _start address at run time (must equal the loader entry) + * 018E0020 d_magic read from .data (must be DEADBEEF) + * 018E0040 d_two read from .data (must be 12345678) + * 018E0060 b_zero read from .bss (must be 0) + * 018E0080 b_counter, a live counter (must advance) + * + * The M7.3 brief prefers a C-only entry when the compiler can express it, so + * `start` is an ordinary C function and the COFF entry point is `_start`. + * create_process() supplies the process stack; the MAME acceptance run proves + * the generated prologue's A14/SP use is valid there, and the real-unit run + * (VT1200_HOST) is the confirmation on hardware. The `.data` pointer + * initializer below is supported as an ordinary pointer relocation, so no + * `ptrtoint` constant initializer and no assembly entry are needed. + * + * The loader zeroes .bss after the harness has pre-filled it with A5A5, so a + * zero at 018E0060 is the loader's work, not fresh RAM. The delay loop is + * marker.asm's 2000h `dsjs` delay: the child stays scheduled but does not burn + * the cooperative scheduler with an unbroken hot loop. + */ + +#define MARK(n) (*(volatile unsigned int *)(0x018E0000u + (n) * 0x20u)) + +/* Initialised .data: the loader must copy these bytes from the COFF. */ +unsigned int d_magic = 0xDEADBEEFu; +unsigned int d_two = 0x12345678u; + +/* Uninitialised .bss: the loader must zero these. */ +unsigned int b_zero; +unsigned int b_counter; + +void start(void); + +/* + * The entry address as a relocated pointer, not a ptrtoint constant. The + * loader's result block reports the same `_start` bit address as the COFF + * entry point, so the runtime start cell proves placement, not a guess. + */ +void (*const start_address)(void) = start; + +/* A volatile .bss sink keeps the delay loop from being folded away. */ +static volatile unsigned int delay_sink; + +void start(void) { + unsigned int count = 0; + + MARK(0) = (unsigned int)start_address; + MARK(1) = d_magic; + MARK(2) = d_two; + MARK(3) = b_zero; + + for (;;) { + unsigned int spin; + + count++; + b_counter = count; + MARK(4) = count; + + for (spin = 0; spin < 0x2000u; spin++) { + delay_sink = spin; + } + } +} diff --git a/scripts/tests/m7/hardware.sh b/scripts/tests/m7/hardware.sh new file mode 100755 index 0000000..242b0fc --- /dev/null +++ b/scripts/tests/m7/hardware.sh @@ -0,0 +1,383 @@ +#!/bin/sh +# M7.3 real-hardware marker gate with MAME-equivalent acceptance. +# +# Builds scripts/m7/hwmark.c (a C-only `_start`) as a TI-COFF object, links it +# with tools/tms-link at the proven VT1200 hardware origins, and proves the +# image in MAME through the same dwt loader that hostload.py drives: +# +# .text 018C0000h .data 018D0000h .bss 018D8000h +# 018E0000 start = entry (018C0000) +# 018E0020 data = DEADBEEF (loader copied .data) +# 018E0040 data2 = 12345678 (loader copied .data) +# 018E0060 bss = 0 (loader zeroed the A5-prefilled .bss) +# 018E0080 count advances (create_process scheduled a live child) +# +# GSPLNK 2.00 links the same object with explicit section origins as the +# layout oracle; the gate requires identical allocated bytes and placement. +# +# Real unit: +# +# VT1200_HOST=192.168.99.2 scripts/tests/m7/hardware.sh +# +# runs dwt/loader/hostload.py --host "$VT1200_HOST" --coff +# --marker --out --tag hwmark and propagates its failure. Without +# VT1200_HOST the gate prints `HARDWARE: DEFERRED` and passes on the MAME +# evidence; it never reports a hardware pass without a unit. +# +# Run this gate alone and serially (it owns MAME/DOSBox and uses fixed +# hardware origins). +# +# Overridable: LLVM_BIN TMS_CLANG TMS_LLC TMS_LINK MAME MAME_TIMEOUT DOSBOX +# DOSBOX_CONF SDK_IMG M7_HW_WORK M7_HW_COFF M7_SDK LOADER_AT +# LOADER_END VT1200_HOST +set -eu + +ROOT=$(cd "$(dirname "$0")/../../.." && pwd) +DIR=$ROOT/scripts/m7 + +LLVM_BIN=${LLVM_BIN:-$ROOT/build/llvm-clean-build/bin} +TMS_CLANG=${TMS_CLANG:-$ROOT/scripts/tms-clang} +TMS_LLC=${TMS_LLC:-$LLVM_BIN/llc} +TMS_LINK=${TMS_LINK:-$ROOT/tools/tms-link/tms-link} +MAME=${MAME:-$ROOT/build/mame-src/vt1200} +MAME_TIMEOUT=${MAME_TIMEOUT:-900} +DOSBOX=${DOSBOX:-dosbox} +DOSBOX_CONF=${DOSBOX_CONF:-$ROOT/vendor/vt1200/harness/sdk-scratch/dosbox-bench.conf} +SDK_IMG=${SDK_IMG:-$ROOT/vendor/vt1200/tools/tms34010-sdk/1987-05 TMS34010 SDK/1987-11-06 TMS34010 Simulator-C Tools-ASM Tools.img} + +WORK=${M7_HW_WORK:-$ROOT/build/m7-hardware} +COFF=${M7_HW_COFF:-$WORK/hw-marker.coff} +SDK=${M7_SDK:-$ROOT/build/m4-sdk} +LOADER_LUA=$ROOT/vendor/vt1200/dwt/loader/loader.lua +LOADER_BIN=$ROOT/vendor/vt1200/dwt/loader/build/loader.bin +HOSTLOAD=$ROOT/vendor/vt1200/dwt/loader/hostload.py +LOADER_AT=${LOADER_AT:-2500} +LOADER_END=${LOADER_END:-2700} +HOST=${VT1200_HOST:-} + +fail() { + echo "M7 hardware: FAIL: $*" >&2 + exit 1 +} + +[ -x "$TMS_CLANG" ] || fail "missing tms-clang: $TMS_CLANG" +[ -x "$TMS_LLC" ] || fail "missing llc: $TMS_LLC" +[ -x "$TMS_LINK" ] || fail "missing tms-link: $TMS_LINK" +[ -x "$MAME" ] || fail "missing MAME binary: $MAME" +[ -f "$LOADER_LUA" ] || fail "missing COFF loader: $LOADER_LUA" +[ -f "$LOADER_BIN" ] || fail "missing COFF loader payload: $LOADER_BIN" +[ -f "$HOSTLOAD" ] || fail "missing hostload.py: $HOSTLOAD" +[ -f "$DOSBOX_CONF" ] || fail "missing DOSBox config: $DOSBOX_CONF" +command -v "$DOSBOX" >/dev/null 2>&1 || fail "missing DOSBox: $DOSBOX" + +rm -rf "$WORK" +mkdir -p "$WORK/run/nvram" "$WORK/run/cfg" "$WORK/evidence" "$WORK/dos" + +echo "== compile the C-only marker client at the hardware origins" +"$TMS_CLANG" -O0 -o "$WORK/hwmark.ll" "$DIR/hwmark.c" \ + || fail "tms-clang failed" +"$TMS_LLC" -march=tms34010 -O0 -filetype=obj \ + "$WORK/hwmark.ll" -o "$WORK/hwmark.o" \ + || fail "llc -filetype=obj failed" + +echo "== link with tools/tms-link (.text 018C0000 .data 018D0000 .bss 018D8000)" +"$TMS_LINK" -o "$COFF" --map "$WORK/hwmark.map" --entry _start \ + --text 018C0000 --data 018D0000 --bss 018D8000 \ + --prog-len 18000 --ram-len 8000 \ + "$WORK/hwmark.o" \ + || fail "tms-link failed" + +ENTRY_ADDR=$(awk '$2 == "_start" { print $1; exit }' "$WORK/hwmark.map") +[ "$ENTRY_ADDR" = "018C0000" ] \ + || fail "tms-link entry _start is '$ENTRY_ADDR', want 018C0000" +grep -q 'ENTRY POINT SYMBOL: "_start" address: 018C0000' "$WORK/hwmark.map" \ + || fail "tms-link map has no _start entry point at 018C0000" + +echo "== inspect the linked COFF (origins, .data sentinels, no relocations)" +python3 - "$COFF" <<'PY' || fail "linked COFF does not match the hardware marker contract" +import struct +import sys + +path = sys.argv[1] +d = open(path, "rb").read() +if len(d) < 20: + sys.exit(f"{path}: shorter than a COFF file header") +machine, nscns, _time, symptr, nsyms, opthdr, flags = struct.unpack_from( + " \ + SECTIONS[".data"]["paddr"]: + sys.exit(f"{path}: .text overlaps .data") +if SECTIONS[".data"]["paddr"] + SECTIONS[".data"]["size"] > 0x018D8000: + sys.exit(f"{path}: .data crosses into the .bss region") +if SECTIONS[".bss"]["paddr"] + SECTIONS[".bss"]["size"] > 0x018E0000: + sys.exit(f"{path}: .bss crosses into the marker cells at 018E0000") + +# Symbol records: the client's data/bss objects must be in the right section, +# and the .data bytes at their symbol offsets must hold the sentinels. +string_base = symptr + nsyms * 18 +syms = {} +i = 0 +while i < nsyms: + off = symptr + i * 18 + if off + 18 > len(d): + sys.exit(f"{path}: symbol {i} past EOF") + name = d[off:off + 8] + if name[:4] == b"\0\0\0\0": + soff, = struct.unpack_from("/dev/null 2>&1 || fail "mtools (mcopy) is required" + [ -f "$SDK_IMG" ] || fail "SDK image not found: $SDK_IMG" + mkdir -p "$SDK" + mcopy -s -n -i "$SDK_IMG" "::/A2/*" "$SDK/" + mcopy -s -n -i "$SDK_IMG" "::/C2/*" "$SDK/" +fi +[ -f "$SDK/GSPLNK.EXE" ] || fail "GSPLNK.EXE is not extracted in $SDK" + +cp "$SDK/GSPLNK.EXE" "$WORK/dos/" +cp "$WORK/hwmark.o" "$WORK/dos/HWMARK.OBJ" +{ + printf '%s\n' '-a' + printf '%s\n' '-o HWMARK.OUT' + printf '%s\n' '-m HWMARK.MAP' + printf '%s\n' '-e _start' + printf '%s\n' 'HWMARK.OBJ' + printf '%s\n' 'SECTIONS' + printf '%s\n' '{' + printf '%s\n' ' .text 018C0000h: {}' + printf '%s\n' ' .data 018D0000h: {}' + printf '%s\n' ' .bss 018D8000h: {}' + printf '%s\n' '}' +} > "$WORK/dos/HWMARK.CMD" +sed 's/$/\r/' "$WORK/dos/HWMARK.CMD" > "$WORK/dos/HWMARK.NEW" +mv "$WORK/dos/HWMARK.NEW" "$WORK/dos/HWMARK.CMD" + +rm -f "$WORK/dos/HWMARK.OUT" "$WORK/dos/HWMARK.MAP" +SDL_VIDEODRIVER=dummy SDL_AUDIODRIVER=dummy \ + timeout "${DOSBOX_TIMEOUT:-300}" "$DOSBOX" -conf "$DOSBOX_CONF" \ + -noprimaryconf -nolocalconf \ + -c "mount c $WORK/dos" -c "c:" \ + -c "gsplnk HWMARK.CMD" \ + -c "exit" -exit > "$WORK/dosbox.log" 2>&1 \ + || { tail -20 "$WORK/dosbox.log" >&2; fail "DOSBox failed"; } +[ -f "$WORK/dos/HWMARK.OUT" ] \ + || { tail -30 "$WORK/dosbox.log" >&2; fail "GSPLNK produced no HWMARK.OUT"; } +cp "$WORK/dos/HWMARK.OUT" "$WORK/hwmark-gsplnk.coff" + +python3 - "$COFF" "$WORK/hwmark-gsplnk.coff" <<'PY' \ + || fail "tms-link output differs from the GSPLNK oracle" +import struct +import sys + + +def parse(path): + d = open(path, "rb").read() + machine, nscns, _time, _symptr, _nsyms, opthdr, flags = \ + struct.unpack_from(" "$WORK/mame.log" 2>&1 \ + || { tail -30 "$WORK/mame.log" >&2; fail "MAME run failed"; } + +grep -q 'LOADER: result status=0 (OK)' "$WORK/mame.log" \ + || { grep 'LOADER: result' "$WORK/mame.log" >&2 || true; \ + fail "COFF loader did not report status 0"; } +grep -qE 'placed \.text at 018C0000: .* 0 differing' "$WORK/mame.log" \ + || fail "COFF loader could not verify the placed .text bytes" +grep -qE 'placed \.data at 018D0000: .* 0 differing' "$WORK/mame.log" \ + || fail "COFF loader could not verify the placed .data bytes" +grep -q '^M7 HW MAME: PASS$' "$WORK/mame.log" \ + || { grep -E '^M7 HW' "$WORK/mame.log" >&2 || true; \ + tail -30 "$WORK/mame.log" >&2; \ + fail "MAME marker cells or loader status did not match"; } + +grep '^M7 HW: ' "$WORK/mame.log" | tail -1 | sed 's/^/ /' +grep '^LOADER: marker app: ' "$WORK/mame.log" | tail -1 | sed 's/^/ /' || true + +if [ -n "$HOST" ]; then + echo "== real hardware: hostload.py --marker at $HOST" + if ! python3 "$HOSTLOAD" --host "$HOST" --coff "$COFF" --marker \ + --out "$WORK/evidence/hostload" --tag hwmark \ + > "$WORK/evidence/hostload-hwmark.log" 2>&1; then + cat "$WORK/evidence/hostload-hwmark.log" >&2 + fail "real-hardware marker run failed (host $HOST)" + fi + cat "$WORK/evidence/hostload-hwmark.log" + echo "HARDWARE: PASS (host $HOST)" + echo "M7 hardware: PASS (MAME-equivalent + real hardware)" +else + echo "HARDWARE: DEFERRED" + echo "HARDWARE: set VT1200_HOST= to run hostload.py --marker over rdebug" + echo "M7 hardware: PASS (MAME-equivalent; hardware deferred)" +fi