277 lines
12 KiB
Bash
Executable File
277 lines
12 KiB
Bash
Executable File
#!/bin/sh
|
|
# M6 runtime gate: the linked flib.lib/rts.lib runtime and the shared shim's
|
|
# printf/sprintf surface.
|
|
#
|
|
# scripts/tests/m6/runtime.sh
|
|
#
|
|
# Compiles the C driver scripts/m6/runtime-main.c and the shared conformance
|
|
# shim sdk/shim.c (a separate translation unit),
|
|
# assembles/links them with GSPA/GSPLNK 2.00 and the 1990 flib.lib/rts.lib
|
|
# under DOSBox, loads the COFF with the existing dwt loader and runs it in
|
|
# MAME. Both -O0 and -O2 run by default; RUNTIME_OPT=-O0 (or -O2) selects one.
|
|
#
|
|
# What it proves at runtime:
|
|
# - printf("%d %s %f %lld", ...) formats the M6 conversions and returns the
|
|
# character count;
|
|
# - sprintf writes into a caller buffer, NUL-terminates, returns the full
|
|
# (untruncated) length, and truncates safely at the shim's SPRINTF_CAP;
|
|
# - the M5 integer formats/flags/width/alternate forms and the new
|
|
# width/precision (including `*`) are exact;
|
|
# - i64/unsigned-i64 boundaries, %ld/%lu and zero-padded %lld;
|
|
# - %f sign/space/plus/zero/width/precision, half-up rounding and the
|
|
# documented i32 range cap;
|
|
# - memcpy/memmove (including overlapping backward copy);
|
|
# - `double` add/multiply reach flib.lib (FD$ADD/FD$MUL) and the compiler's
|
|
# fptosi path (FD$DTOI) at both optimization levels, and `_isascii`
|
|
# (rts.lib) is called; the gate checks the linked symbol names in the
|
|
# GSPLNK map, so both runtime library links are part of the evidence.
|
|
#
|
|
# The shim writes printf output to its CONF_* block (length at 01B00020h, bytes
|
|
# at 01B10000h) and the checker compares those bytes exactly with the expected
|
|
# text.
|
|
#
|
|
# M6_LINKER=tms-link optionally links the GSPA-assembled M6.OBJ with the 1990
|
|
# flib.lib/rts.lib through tools/tms-link instead of GSPLNK; the GSPLNK default
|
|
# remains the oracle.
|
|
#
|
|
# Overridable: LLVM_BIN TMS_LLC TMS_CLANG TMS_OPT TMS_BITADDR MAME DOSBOX
|
|
# DOSBOX_CONF SDK_IMG RUNTIME_WORK RUNTIME_COFF RUNTIME_SDK
|
|
# LOADER_AT LOADER_END MAME_TIMEOUT DOSBOX_TIMEOUT RUNTIME_OPT
|
|
# M6_LINKER TMS_LINK
|
|
set -eu
|
|
|
|
ROOT=$(cd "$(dirname "$0")/../../.." && pwd)
|
|
DIR=$ROOT/scripts/m6
|
|
SHIM=$ROOT/sdk/shim.c
|
|
|
|
LLVM_BIN=${LLVM_BIN:-$ROOT/build/llvm-clean-build/bin}
|
|
TMS_LLC=${TMS_LLC:-$LLVM_BIN/llc}
|
|
TMS_CLANG=${TMS_CLANG:-$LLVM_BIN/clang}
|
|
TMS_OPT=${TMS_OPT:-$LLVM_BIN/opt}
|
|
TMS_BITADDR=${TMS_BITADDR:-$ROOT/tools/tms-bitaddr/tms-bitaddr}
|
|
TMS_LINK=${TMS_LINK:-$ROOT/tools/tms-link/tms-link}
|
|
M7COFF=${M7COFF:-$ROOT/build/llvm-clean/llvm/test/CodeGen/TMS34010/m7coff.py}
|
|
|
|
MAME=${MAME:-$ROOT/build/mame-src/vt1200}
|
|
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}
|
|
|
|
BASE_WORK=${RUNTIME_WORK:-$ROOT/build/m6-runtime}
|
|
SDK=${RUNTIME_SDK:-$BASE_WORK/sdk}
|
|
LOADER_LUA=$ROOT/vendor/vt1200/dwt/loader/loader.lua
|
|
LOADER_BIN=${LOADER_BIN:-$ROOT/vendor/vt1200/dwt/loader/build/loader.bin}
|
|
LOADER_AT=${LOADER_AT:-2500}
|
|
LOADER_END=${LOADER_END:-3400}
|
|
RUNTIME_OPTS=${RUNTIME_OPT:--O0 -O2}
|
|
LINKER=${M6_LINKER:-gsplnk}
|
|
|
|
fail() {
|
|
echo "m6 runtime: FAIL: $*" >&2
|
|
exit 1
|
|
}
|
|
|
|
for tool in "$TMS_LLC" "$TMS_CLANG" "$TMS_OPT" "$TMS_BITADDR"; do
|
|
[ -x "$tool" ] || fail "missing tool: $tool"
|
|
done
|
|
case $LINKER in
|
|
gsplnk|tms-link) ;;
|
|
*) fail "M6_LINKER must be 'gsplnk' or 'tms-link', got '$LINKER'" ;;
|
|
esac
|
|
if [ "$LINKER" = tms-link ]; then
|
|
[ -x "$TMS_LINK" ] || fail "missing tms-link: $TMS_LINK"
|
|
[ -f "$M7COFF" ] || fail "missing independent COFF reader: $M7COFF"
|
|
fi
|
|
[ -f "$SHIM" ] || fail "missing shared shim: $SHIM"
|
|
[ -x "$MAME" ] || fail "MAME binary not built: $MAME"
|
|
[ -f "$LOADER_LUA" ] || fail "COFF loader missing: $LOADER_LUA"
|
|
[ -f "$LOADER_BIN" ] || fail "COFF loader payload missing: $LOADER_BIN"
|
|
|
|
mkdir -p "$BASE_WORK" "$SDK"
|
|
|
|
echo "== extract the 1987 GSPA/GSPLNK tools"
|
|
if [ ! -f "$SDK/GSPA.EXE" ] || [ ! -f "$SDK/GSPLNK.EXE" ]; then
|
|
command -v mcopy >/dev/null 2>&1 || fail "mtools (mcopy) is required"
|
|
[ -f "$SDK_IMG" ] || fail "1987 SDK image not found: $SDK_IMG"
|
|
mcopy -n -i "$SDK_IMG" "::/A2/GSPA.EXE" "::/A2/GSPLNK.EXE" "$SDK/"
|
|
fi
|
|
[ -f "$SDK/GSPA.EXE" ] || fail "GSPA.EXE not extracted into $SDK"
|
|
[ -f "$SDK/GSPLNK.EXE" ] || fail "GSPLNK.EXE not extracted into $SDK"
|
|
|
|
echo "== extract the 1990 flib.lib/rts.lib"
|
|
LIB_SDK=${M6_LIB_SDK:-$ROOT/build/m6-sdk}
|
|
if [ ! -f "$LIB_SDK/flib.lib" ] || [ ! -f "$LIB_SDK/rts.lib" ]; then
|
|
mkdir -p "$LIB_SDK"
|
|
M6_SDK_OUT="$LIB_SDK" "$DIR/flib-extract.sh" \
|
|
> "$BASE_WORK/flib-extract.log" 2>&1 \
|
|
|| { cat "$BASE_WORK/flib-extract.log" >&2; \
|
|
fail "flib-extract.sh failed"; }
|
|
fi
|
|
cp "$LIB_SDK/flib.lib" "$LIB_SDK/rts.lib" "$SDK/"
|
|
|
|
compile_c() {
|
|
src=$1
|
|
out=$2
|
|
opt=$3
|
|
export TMS_CLANG TMS_OPT TMS_BITADDR
|
|
"$ROOT/scripts/tms-clang" "$opt" -o "$out.ll" "$src" \
|
|
|| fail "$opt: tms-clang failed for $src"
|
|
"$TMS_LLC" -march=tms34010 "$opt" -filetype=asm "$out.ll" -o "$out.s" \
|
|
|| fail "$opt: llc failed for $out.ll"
|
|
}
|
|
|
|
run_one() {
|
|
opt=$1
|
|
tag=$(echo "$opt" | tr -d '-')
|
|
WORK=$BASE_WORK/$tag
|
|
COFF=${RUNTIME_COFF:-$WORK/runtime.coff}
|
|
|
|
mkdir -p "$WORK/evidence" "$WORK/run/nvram" "$WORK/run/cfg"
|
|
|
|
echo "== [$opt] compile the C driver and the shared shim (separate TUs)"
|
|
compile_c "$DIR/runtime-main.c" "$WORK/runtime-main" "$opt"
|
|
echo " runtime-main.s: $(grep -c . "$WORK/runtime-main.s") lines"
|
|
compile_c "$SHIM" "$WORK/runtime-shim" "$opt"
|
|
echo " runtime-shim.s: $(grep -c . "$WORK/runtime-shim.s") lines"
|
|
|
|
# The shim's printf/sprintf definitions and the flib call sites must be in
|
|
# the generated assembly; a missing %f/%lld body would silently fall back to
|
|
# the unknown-conversion output and the checker would catch it, but check
|
|
# the call sites here so a miscompile is reported at the compile step too.
|
|
grep -q '^_printf:' "$WORK/runtime-shim.s" || fail "$opt: shim has no _printf"
|
|
grep -q '^_sprintf:' "$WORK/runtime-shim.s" || fail "$opt: shim has no _sprintf"
|
|
grep -q 'FD\$ADD' "$WORK/runtime-main.s" \
|
|
|| fail "$opt: runtime-main.s has no FD\$ADD call (double add not lowered)"
|
|
grep -q 'FD\$MUL' "$WORK/runtime-main.s" \
|
|
|| fail "$opt: runtime-main.s has no FD\$MUL call (double multiply not lowered)"
|
|
grep -q 'FD\$DTOI' "$WORK/runtime-main.s" \
|
|
|| fail "$opt: runtime-main.s has no FD\$DTOI call (fptosi not lowered)"
|
|
grep -q 'FD\$DTOI' "$WORK/runtime-shim.s" \
|
|
|| fail "$opt: runtime-shim.s has no FD\$DTOI call (%f int conversion)"
|
|
|
|
echo "== [$opt] assemble/link with GSPA/GSPLNK under DOSBox"
|
|
# Give each TU its own LBB prefix (the M3/M4/M6 pattern).
|
|
sed 's/LBB\([0-9][0-9]*_[0-9][0-9]*\)/LBBM\1/g' \
|
|
"$WORK/runtime-main.s" > "$WORK/runtime-main.unique.s"
|
|
sed 's/LBB\([0-9][0-9]*_[0-9][0-9]*\)/LBBS\1/g' \
|
|
"$WORK/runtime-shim.s" > "$WORK/runtime-shim.unique.s"
|
|
cat "$DIR/runtime-entry.asm" "$WORK/runtime-main.unique.s" \
|
|
"$WORK/runtime-shim.unique.s" > "$SDK/M6.ASM"
|
|
sed 's/\r$//' "$DIR/runtime-link.cmd" | sed 's/$/\r/' > "$SDK/M6.CMD"
|
|
rm -f "$SDK/M6.OBJ" "$SDK/M6.OUT" "$SDK/M6.MAP" "$SDK/M6.LST" "$SDK/M6.LNK"
|
|
|
|
if [ "$LINKER" = tms-link ]; then
|
|
# Produce the GSPLNK oracle from the same M6.OBJ and libraries, then
|
|
# link the same inputs with tms-link and compare the allocated image.
|
|
SDL_VIDEODRIVER=dummy SDL_AUDIODRIVER=dummy \
|
|
timeout "${DOSBOX_TIMEOUT:-300}" "$DOSBOX" -conf "$DOSBOX_CONF" \
|
|
-noprimaryconf -nolocalconf \
|
|
-c "mount c $SDK" -c "c:" \
|
|
-c "gspa M6 M6.obj M6.lst -l" \
|
|
-c "gsplnk M6.cmd > M6.LNK" \
|
|
-c "exit" -exit > "$WORK/dosbox.log" 2>&1 \
|
|
|| { tail -20 "$WORK/dosbox.log" >&2; fail "$opt: DOSBox failed"; }
|
|
[ -f "$SDK/M6.OUT" ] \
|
|
|| { if [ -f "$SDK/M6.LNK" ]; then cat "$SDK/M6.LNK" >&2; fi; \
|
|
fail "$opt: GSPLNK oracle produced no M6.OUT"; }
|
|
mv "$SDK/M6.OUT" "$SDK/M6.GSP.OUT"
|
|
mv "$SDK/M6.MAP" "$SDK/M6.GSP.MAP"
|
|
echo "== [$opt] link M6.OBJ + flib.lib/rts.lib with tools/tms-link"
|
|
"$TMS_LINK" -o "$SDK/M6.OUT" --map "$SDK/M6.MAP" --entry _start \
|
|
--text 018A0000 --bss 01A00000 \
|
|
--prog-len 40000h --ram-len 80000h \
|
|
"$SDK/M6.OBJ" "$SDK/flib.lib" "$SDK/rts.lib" \
|
|
|| fail "$opt: tms-link failed"
|
|
python3 "$ROOT/scripts/tests/m8/coff-parity.py" \
|
|
"$M7COFF" "$SDK/M6.GSP.OUT" "$SDK/M6.OUT" \
|
|
|| fail "$opt: tms-link image differs from the GSPLNK oracle"
|
|
else
|
|
SDL_VIDEODRIVER=dummy SDL_AUDIODRIVER=dummy \
|
|
timeout "${DOSBOX_TIMEOUT:-300}" "$DOSBOX" -conf "$DOSBOX_CONF" \
|
|
-noprimaryconf -nolocalconf \
|
|
-c "mount c $SDK" -c "c:" \
|
|
-c "gspa M6 M6.obj M6.lst -l" \
|
|
-c "gsplnk M6.cmd > M6.LNK" \
|
|
-c "exit" -exit > "$WORK/dosbox.log" 2>&1 \
|
|
|| { tail -20 "$WORK/dosbox.log" >&2; fail "$opt: DOSBox failed"; }
|
|
fi
|
|
|
|
[ -f "$SDK/M6.LST" ] || fail "$opt: GSPA produced no listing (see $WORK/dosbox.log)"
|
|
sed 's/\r$//' "$SDK/M6.LST" > "$WORK/m6.lst"
|
|
grep -q 'No Errors' "$WORK/m6.lst" \
|
|
|| { grep -i -B2 -A2 'error' "$WORK/m6.lst" >&2 || true; \
|
|
fail "$opt: GSPA reported errors"; }
|
|
grep -q 'No Warnings' "$WORK/m6.lst" \
|
|
|| { grep -i -B2 -A2 'warning' "$WORK/m6.lst" >&2 || true; \
|
|
fail "$opt: GSPA reported warnings"; }
|
|
[ -f "$SDK/M6.OUT" ] \
|
|
|| { if [ -f "$SDK/M6.LNK" ]; then cat "$SDK/M6.LNK" >&2; fi; \
|
|
fail "$opt: GSPLNK produced no M6.OUT"; }
|
|
[ -f "$SDK/M6.MAP" ] || fail "$opt: GSPLNK produced no M6.MAP"
|
|
sed 's/\r$//' "$SDK/M6.MAP" > "$WORK/m6.map"
|
|
cp "$SDK/M6.OUT" "$COFF"
|
|
cp "$SDK/M6.ASM" "$WORK/m6.asm"
|
|
|
|
entry=$(awk '$2 == "_start" { print $1; exit }' "$WORK/m6.map")
|
|
[ "$entry" = "018A0000" ] || fail "$opt: linked _start is '$entry', want 018A0000"
|
|
markers=$(awk '$2 == "_runtime_markers" { print $1; exit }' "$WORK/m6.map")
|
|
[ -n "$markers" ] || fail "$opt: linker map has no _runtime_markers symbol"
|
|
echo " _start=$entry _runtime_markers=$markers"
|
|
|
|
# The runtime link is part of the evidence: printf/sprintf are the shim's,
|
|
# the flib entries resolved from the 1990 library, and _isascii is a
|
|
# scalar rts.lib entry called at run time. (The 1990 _div/_ldiv wrappers
|
|
# in rts.lib return div_t/ldiv_t with the sret form, not M6's hidden slot,
|
|
# so they are deliberately not called.)
|
|
for sym in _printf _sprintf FD\$ADD FD\$MUL FD\$DTOI _isascii; do
|
|
grep -q " ${sym}\$" "$WORK/m6.map" \
|
|
|| fail "$opt: linker map has no $sym"
|
|
done
|
|
|
|
echo "== [$opt] run the COFF in MAME"
|
|
rm -rf "$WORK/run/nvram"
|
|
mkdir -p "$WORK/run/nvram"
|
|
|
|
VT1200_MONITOR=VR319 \
|
|
VT1200_SKIP_LEVEL1=1 \
|
|
LOADER_AT="$LOADER_AT" LOADER_END="$LOADER_END" \
|
|
LOADER_BIN="$LOADER_BIN" LOADER_COFF="$COFF" LOADER_OUT="$WORK/evidence" \
|
|
M2_LOADER_LUA="$LOADER_LUA" \
|
|
RUNTIME_MARKERS="$markers" \
|
|
timeout "${MAME_TIMEOUT:-900}" "$MAME" vt1200 \
|
|
-rompath "$ROOT/vendor/vt1200/harness/mame/roms" \
|
|
-video none -sound none -nothrottle -skip_gameinfo \
|
|
-nvram_directory "$WORK/run/nvram" \
|
|
-cfg_directory "$WORK/run/cfg" \
|
|
-debug -debugger none \
|
|
-autoboot_script "$DIR/runtime-check.lua" -autoboot_delay 0 \
|
|
> "$WORK/mame.log" 2>&1 \
|
|
|| { tail -30 "$WORK/mame.log" >&2; fail "$opt: MAME run failed"; }
|
|
|
|
MARKERS=$(grep -E '^M6 runtime:' "$WORK/mame.log" | tail -1 || true)
|
|
[ -n "$MARKERS" ] || { tail -30 "$WORK/mame.log" >&2; \
|
|
fail "$opt: MAME produced no runtime markers"; }
|
|
echo " $MARKERS"
|
|
grep -E '^M6 runtime out\(' "$WORK/mame.log" \
|
|
| sed 's/^/ /' || true
|
|
|
|
grep -q 'LOADER: result status=0 (OK)' "$WORK/mame.log" \
|
|
|| { grep 'LOADER: result' "$WORK/mame.log" >&2 || true; \
|
|
fail "$opt: COFF loader did not report status 0"; }
|
|
grep -q 'placed \.text .* 0 differing' "$WORK/mame.log" \
|
|
|| fail "$opt: COFF loader could not verify the placed .text bytes"
|
|
grep -q '^M6 RUNTIME MAME: PASS' "$WORK/mame.log" \
|
|
|| { grep -E 'M6 runtime: (marker|sentinel|output)|M6 RUNTIME MAME' \
|
|
"$WORK/mame.log" >&2 || true; \
|
|
tail -30 "$WORK/mame.log" >&2; \
|
|
fail "$opt: runtime marker/output check failed"; }
|
|
|
|
echo "M6 RUNTIME ($opt): PASS"
|
|
}
|
|
|
|
for opt in $RUNTIME_OPTS; do
|
|
run_one "$opt"
|
|
done
|
|
|
|
echo "M6 RUNTIME: PASS"
|