Files

213 lines
8.2 KiB
Bash
Executable File

#!/bin/sh
# M5 native-pipeline gate: build the real M4 clients through the native TI-COFF
# path and require the same markers as the GSPA-assembled (oracle) path.
#
# scripts/tests/m5/native.sh
#
# 1. xshow + xgfix: tms-clang -> llc -filetype=obj -> GSPLNK -> loader/MAME.
# The M4 image/event gate markers must still hold: the checker pixels, the
# injected `Q` (keycode 193 / keysym 0x71), the client window focus, and
# xerrors=0.
# 2. termtris (six TUs): the same native build feeds the existing M4 termtris
# gate (scripts/tests/m4/termtris.sh) through TERMTRIS_BUILD, so the
# board/move/panels/quit thresholds, focus, loader status and snapshots are
# enforced by the unchanged gate.
# 3. dis34 comparison: build the asm/GSPA oracle of xshow and termtris into
# private work directories, extract both linked .text sections, and compare
# the decoded instruction streams. Layouts are allowed to differ; a raw
# byte compare is not used.
#
# Overridable: M5_NATIVE_WORK MAME_TIMEOUT DOSBOX_TIMEOUT
set -eu
ROOT=$(cd "$(dirname "$0")/../../.." && pwd)
DIR=$ROOT/scripts/m5
WORK=${M5_NATIVE_WORK:-$ROOT/build/m5-native}
XWORK=$WORK/xshow-native
AWORK=$WORK/xshow-asm
TWORK=$WORK/termtris-native
DIS34=${DIS34:-$ROOT/vendor/vt1200/tools/smart34010/dis34}
fail() {
echo "M5 native: FAIL: $*" >&2
exit 1
}
[ -x "$DIS34" ] || fail "missing dis34: $DIS34"
mkdir -p "$WORK"
echo "== xshow + xgfix through the native object path"
M5_XSHOW_WORK=$XWORK "$ROOT/scripts/m5/native.sh" xshow \
|| fail "native xshow build/run failed"
XLOG=$XWORK/mame.log
[ -f "$XLOG" ] || fail "no native xshow MAME log: $XLOG"
[ -f "$XWORK/asm/xshow.o" ] || fail "xshow.c was not compiled to an object"
[ -f "$XWORK/asm/xgfix.o" ] || fail "xgfix.c was not compiled to an object"
[ -f "$XWORK/m4.coff" ] || fail "no native linked image"
MARKER=$(grep -aE '^M4: display=' "$XLOG" | tail -1 || true)
[ -n "$MARKER" ] || fail "no native xshow marker line"
echo " $MARKER"
case "$MARKER" in
*" quit=1 events="*) ;;
*) fail "native xshow quit marker is not 1" ;;
esac
case "$MARKER" in
*" keycode=193 keysym=0x71 "*) ;;
*) fail "native xshow: the injected Q did not produce keycode 193 / 0x71" ;;
esac
case "$MARKER" in
*" xerrors=0 "*) ;;
*) fail "native xshow xerrors is not 0" ;;
esac
case "$MARKER" in
*" focus=0x400001 "*) ;;
*) fail "native xshow input focus is not the client window 0x400001" ;;
esac
case "$MARKER" in
*" last=2"*) ;;
*) fail "native xshow: the last X event is not KeyPress (last=2)" ;;
esac
grep -Fq 'M4: pixel screen(50,50) image(18,30) cell(0,0) bit=0x04019032 value=0 black' "$XLOG" \
|| fail "native xshow: (30,30) is not the black checker cell"
grep -Fq 'M4: pixel screen(70,50) image(38,30) cell(1,0) bit=0x04019046 value=1 white' "$XLOG" \
|| fail "native xshow: (50,30) is not the white checker cell"
grep -Fq 'M4: pixel screen(500,400) image(468,380) cell(14,11) bit=0x040C81F4 value=1 white' "$XLOG" \
|| fail "native xshow: later-band checker cell is wrong"
grep -Eq '^M4: pixel screen\(25,30\) image\(-7,10\) cell\(-1,0\) bit=0x[0-9A-F]+ value=1 white$' "$XLOG" \
|| fail "native xshow: left prefix in band 0 is wrong"
grep -Eq '^M4: pixel screen\(25,62\) image\(-7,42\) cell\(-1,1\) bit=0x[0-9A-F]+ value=0 black$' "$XLOG" \
|| fail "native xshow: left prefix in band 1 is wrong"
grep -q '^M4 MAME: PASS' "$XLOG" || fail "native xshow marker check did not pass"
grep -q 'LOADER: result status=0 (OK)' "$XLOG" \
|| fail "native xshow loader status is not OK"
grep -q 'placed \.text .* 0 differing' "$XLOG" \
|| fail "native xshow loader did not verify the placed .text bytes"
echo "== termtris (six TUs) through the native object path and the M4 gate"
TERMTRIS_BUILD="$ROOT/scripts/m5/native.sh" \
TERMTRIS_WORK="$TWORK" \
"$ROOT/scripts/tests/m4/termtris.sh" \
|| fail "native termtris gate failed"
[ -f "$TWORK/asm/main.o" ] || fail "main.c was not compiled to an object"
[ -f "$TWORK/asm/game.o" ] || fail "game.c was not compiled to an object"
echo "== dis34: compare the native and asm linked xshow .text"
# Build the GSPA oracle into its own directory. M4_SKIP_MAME keeps this a
# build/link comparison; the asm MAME gate is scripts/tests/m4/run.sh.
M4_SKIP_MAME=1 \
M4_SOURCES="xshow xgfix" \
M4_ENTRY="$ROOT/scripts/m4/xshow-entry.asm" \
M4_CLANG_OPT=-O2 \
M4_ROM_SYMBOLS=1 \
LOADER_AT=2500 \
LOADER_END=3400 \
M4_WORK="$AWORK" \
"$ROOT/scripts/m4/run.sh" > "$WORK/xshow-asm.log" 2>&1 \
|| { tail -30 "$WORK/xshow-asm.log" >&2; fail "asm xshow oracle build failed"; }
extract_text() {
python3 - "$1" "$2" <<'PY'
import struct
import sys
path, out = sys.argv[1], sys.argv[2]
d = open(path, "rb").read()
nscns, = struct.unpack_from("<H", d, 2)
opthdr, = struct.unpack_from("<H", d, 16)
base = 20 + opthdr
for i in range(nscns):
o = base + i * 40
name = d[o:o + 8].split(b"\0", 1)[0].decode("latin1")
if name != ".text":
continue
paddr, _vaddr, size, ptr = struct.unpack_from("<IIII", d, o + 8)
open(out, "wb").write(d[ptr:ptr + size // 8])
print(f"{paddr:08X}")
break
else:
sys.exit(f"{path}: no .text section")
PY
}
# Compare decoded instruction text, not raw bytes: the native driver assembles
# the entry/extras as separate objects, and layouts are allowed to differ.
compare_text() {
label=$1
native_coff=$2
asm_coff=$3
stem=$4
NATIVE_ADDR=$(extract_text "$native_coff" "$WORK/$stem-native.text") \
|| fail "could not extract the native $label .text"
ASM_ADDR=$(extract_text "$asm_coff" "$WORK/$stem-asm.text") \
|| fail "could not extract the asm $label .text"
[ "$NATIVE_ADDR" = "$ASM_ADDR" ] \
|| fail "$label native .text base $NATIVE_ADDR != asm $ASM_ADDR"
"$DIS34" -b "$NATIVE_ADDR" -l -o "$WORK/$stem-native.text" \
> "$WORK/$stem-native.dis" 2>&1 \
|| { cat "$WORK/$stem-native.dis" >&2; fail "dis34 failed for native $label"; }
"$DIS34" -b "$ASM_ADDR" -l -o "$WORK/$stem-asm.text" \
> "$WORK/$stem-asm.dis" 2>&1 \
|| { cat "$WORK/$stem-asm.dis" >&2; fail "dis34 failed for asm $label"; }
python3 - "$WORK/$stem-native.dis" "$WORK/$stem-asm.dis" "$NATIVE_ADDR" "$label" <<'PY' \
|| fail "$label native and asm decoded instruction streams differ"
import re
import sys
native_path, asm_path, base, label = (
sys.argv[1], sys.argv[2], int(sys.argv[3], 16), sys.argv[4])
def decode(path):
"""[(bit address, instruction text without the raw-bytes comment)]."""
rows = []
for line in open(path, encoding="latin1"):
m = re.match(r"^([0-9a-fA-F]+):\s+(.*)$", line.strip())
if not m:
continue
text = re.sub(r"\s*;.*$", "", m.group(2)).strip()
rows.append((int(m.group(1), 16), text))
return rows
native = decode(native_path)
asm = decode(asm_path)
if not native:
sys.exit("native disassembly is empty")
if not asm:
sys.exit("asm disassembly is empty")
# Compare the decoded instruction streams (mnemonic and operands). The
# native driver assembles the entry/extras as separate objects in the same
# order as the asm path, so the layouts match today; this comparison does not
# require that or use raw byte equality.
text = [text for _addr, text in native]
asm_text = [text for _addr, text in asm]
if len(text) != len(asm_text):
sys.exit(f"decoded instruction count differs: {len(text)} != {len(asm_text)}")
for i, (a, b) in enumerate(zip(text, asm_text)):
if a != b:
sys.exit(f"decoded instruction {i} differs:\n native: {a}\n asm: {b}")
print(f" dis34 {label}: {len(text)} decoded instructions match "
f"(base 0x{base:08x})")
PY
}
compare_text xshow "$XWORK/m4.coff" "$AWORK/m4.coff" xshow
echo "== dis34: compare the native and asm linked termtris .text"
# scripts/m4/termtris/build.sh is the asm oracle builder; it takes
# TERMTRIS_WORK/TERMTRIS_SDK and stops after the GSPLNK link.
ATWORK=$WORK/termtris-asm
TERMTRIS_WORK="$ATWORK" \
TERMTRIS_SDK="$ATWORK/sdk" \
"$ROOT/scripts/m4/termtris/build.sh" > "$WORK/termtris-asm.log" 2>&1 \
|| { tail -30 "$WORK/termtris-asm.log" >&2; fail "asm termtris oracle build failed"; }
compare_text termtris "$TWORK/termtris.coff" "$ATWORK/termtris.coff" termtris
echo "M5 native: PASS (xshow markers + termtris gate + dis34 instruction streams)"