Files
arti 46417f53ec m7.2: strengthen Task 3 debug/oracle/cinit gates
- debug.sh: add a pointer/array runtime-GEP function (debug_ptr) with
  asserted line rows/low PC, and build the same TU with tms-clang -g -O2
  through --bitptr/opt/--scale/--lower-bitptr/instcombine; require the
  -O2 object's .debug_* sections and dwarfdump its debug.c/function/low PC.
  Read .debug_addr entries by index and assert the CU low/high PC and
  .debug_line file index.
- oracle.sh: load both the GSPLNK and tms-link images in MAME with the same
  markers; require a minimum count and the names the checks depend on, and
  document the absolute/undefined C_EXT exclusion.
- cinit.sh: assert the object relocation (0x10, .bss/_cinit_target base,
  addend 0), compare the 28-byte optional header with GSPLNK, and link
  CINIT.O alone to exercise the synthesized public cinit symbol.
- tmscoff.py: point the order-relaxation comment at the Task 1 lit test that
  still pins the llc section order.
2026-11-14 22:54:19 +00:00

425 lines
18 KiB
Bash
Executable File

#!/bin/sh
# M7.2 `.cinit` gate: a C TI init-record table linked and processed at run time.
#
# scripts/tests/m7/cinit.sh
#
# 1. scripts/m7/cinit.c emits a packed TI `.cinit` record
# (`u16 count; u32 destination; u16 data[count];`, terminated by a zero
# count) with a pointer-typed union member holding the destination *bit*
# address. scripts/m7/cinit-entry.asm defines the public `cinit` symbol at
# offset 0 of its empty `.cinit` section and walks the table exactly like
# the termtris `_start`.
# 2. GSPA assembles the entry; GSPLNK 2.00 links ENTRY.O + CINIT.O (the
# oracle) and tms-link links the same objects.
# 3. The gate requires identical allocated `.text`/`.data`/`.cinit`/`.bss`
# bytes and placement, an identical public C_EXT set, `cinit` at the
# `.cinit` origin, and the record's destination field equal to the
# `_cinit_target` bit address.
# 4. MAME loads the tms-link image; the entry's copy loop writes
# 0x1111/0x2222/0x3333 to cinit_target and cinit_probe() returns 0x6666.
#
# Overridable: LLVM_BIN TMS_CLANG TMS_LLC TMS_LINK M7COFF M7_CINIT_WORK M7_SDK
# SDK_IMG DOSBOX DOSBOX_CONF MAME MAME_TIMEOUT LOADER_AT
# LOADER_END
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}
M7COFF=${M7COFF:-$ROOT/build/llvm-clean/llvm/test/CodeGen/TMS34010/m7coff.py}
WORK=${M7_CINIT_WORK:-$ROOT/build/m7-cinit}
SDK=${M7_SDK:-$WORK/sdk}
SDK_IMG=${SDK_IMG:-$ROOT/vendor/vt1200/tools/tms34010-sdk/1987-05 TMS34010 SDK/1987-11-06 TMS34010 Simulator-C Tools-ASM Tools.img}
DOSBOX=${DOSBOX:-dosbox}
DOSBOX_CONF=${DOSBOX_CONF:-$ROOT/vendor/vt1200/harness/sdk-scratch/dosbox-bench.conf}
MAME=${MAME:-$ROOT/build/mame-src/vt1200}
MAME_TIMEOUT=${MAME_TIMEOUT:-900}
LOADER_LUA=$ROOT/vendor/vt1200/dwt/loader/loader.lua
LOADER_BIN=$ROOT/vendor/vt1200/dwt/loader/build/loader.bin
LOADER_AT=${LOADER_AT:-2500}
LOADER_END=${LOADER_END:-2700}
fail() {
echo "M7 cinit: 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"
[ -f "$M7COFF" ] \
|| fail "independent TI-COFF reader not found: $M7COFF (set M7COFF=...)"
[ -f "$DOSBOX_CONF" ] || fail "missing DOSBox config: $DOSBOX_CONF"
command -v "$DOSBOX" >/dev/null 2>&1 || fail "missing DOSBox: $DOSBOX"
[ -x "$MAME" ] || fail "missing MAME binary: $MAME"
[ -f "$LOADER_LUA" ] || fail "missing COFF loader: $LOADER_LUA"
[ -f "$LOADER_BIN" ] || fail "missing loader payload: $LOADER_BIN"
rm -rf "$WORK"
mkdir -p "$WORK/dos" "$WORK/run/nvram" "$WORK/run/cfg" "$WORK/evidence"
WORK=$(cd "$WORK" && pwd)
echo "== compile scripts/m7/cinit.c and llc -filetype=obj"
( cd "$DIR" && "$TMS_CLANG" -O0 -o "$WORK/cinit.ll" cinit.c ) \
|| fail "tms-clang failed"
"$TMS_LLC" -march=tms34010 -O0 -filetype=obj \
"$WORK/cinit.ll" -o "$WORK/cinit.o" \
|| fail "llc -filetype=obj failed"
python3 - "$M7COFF" "$WORK/cinit.o" <<'PY' || fail "C .cinit object is not the TI record shape"
import os
import sys
sys.dont_write_bytecode = True
sys.path.insert(0, os.path.dirname(sys.argv[1]))
import m7coff # noqa: E402
c = m7coff.parse(sys.argv[2])
sections = [s["name"] for s in c["sections"]]
if ".cinit" not in sections:
sys.exit("C object has no .cinit section: %r" % sections)
sec = m7coff.section(c, ".cinit")
if sec["flags"] != 0x40:
sys.exit(".cinit flags %#x, want 0x40" % sec["flags"])
if sec["size"] != 112:
sys.exit(".cinit size %d bits, want 112 (14 bytes)" % sec["size"])
if sec["raw"][0:2] != b"\x03\x00":
sys.exit(".cinit count bytes %r, want 0300" % sec["raw"][0:2])
if sec["raw"][6:12] != b"\x11\x11\x22\x22\x33\x33":
sys.exit(".cinit data bytes %r" % sec["raw"][6:12])
if sec["raw"][12:14] != b"\x00\x00":
sys.exit(".cinit terminator %r, want 0000" % sec["raw"][12:14])
if len(sec["relocs"]) != 1:
sys.exit(".cinit has %d relocations, want exactly 1 (destination)"
% len(sec["relocs"]))
raddr, symidx, rtype = sec["relocs"][0]
if raddr != 0x10 or rtype != 0x11:
sys.exit(".cinit relocation %s, want (0x10, sym, 0x11)"
% ((hex(raddr), symidx, hex(rtype)),))
field = int.from_bytes(sec["raw"][raddr // 8:raddr // 8 + 4], "little")
if field != 0:
sys.exit(".cinit relocation addend %#x, want 0" % field)
by_index = {s["index"]: s for s in c["symbols"]}
target = by_index.get(symidx)
if target is None:
sys.exit(".cinit relocation names missing symbol record %d" % symidx)
# The writer canonicalizes a reference to the .bss section base to the .bss
# section symbol; _cinit_target is that section's offset-0 symbol (value 0).
bss_index = m7coff.section(c, ".bss")["index"]
if not ((target["name"] == ".bss" and target["secno"] == bss_index and
target["sclass"] == 3) or
(target["name"] == "_cinit_target" and
target["secno"] == bss_index and target["value"] == 0)):
sys.exit(".cinit relocation target %r (sec %d) is not the .bss/"
"_cinit_target section base"
% (target["name"], target["secno"]))
print(" C object .cinit: %d bits, reloc (%#x, %s, addend 0), data %s"
% (sec["size"], raddr, target["name"], sec["raw"][6:12].hex()))
PY
echo "== extract the GSPA/GSPLNK toolchain"
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 "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/GSPA.EXE" ] || fail "GSPA.EXE is not extracted in $SDK"
[ -f "$SDK/GSPLNK.EXE" ] || fail "GSPLNK.EXE is not extracted in $SDK"
DOS=$WORK/dos
cp "$SDK/GSPA.EXE" "$SDK/GSPLNK.EXE" "$DOS/"
cp "$WORK/cinit.o" "$DOS/CINIT.O"
sed 's/\r$//' "$DIR/cinit-entry.asm" | sed 's/$/\r/' > "$DOS/ENTRY.ASM"
cat > "$DOS/CINIT.CMD" <<'CMD'
-a
-o CINIT.OUT
-m CINIT.MAP
-e _start
ENTRY.O CINIT.O
MEMORY
{
prog: origin = 018A0000h, length = 8000h
ram: origin = 01A00000h, length = 80000h
}
SECTIONS
{
.text: {} > prog
.data: {} > prog
.cinit: {} > prog
.bss: {} > ram
}
CMD
sed 's/$/\r/' "$DOS/CINIT.CMD" > "$DOS/CINIT.CMD.crlf"
mv "$DOS/CINIT.CMD.crlf" "$DOS/CINIT.CMD"
echo "== assemble ENTRY.ASM and link with GSPLNK 2.00 (oracle)"
rm -f "$DOS/ENTRY.O" "$DOS/ENTRY.LST" "$DOS/CINIT.OUT" "$DOS/CINIT.MAP"
SDL_VIDEODRIVER=dummy SDL_AUDIODRIVER=dummy \
timeout "${DOSBOX_TIMEOUT:-300}" "$DOSBOX" -conf "$DOSBOX_CONF" \
-noprimaryconf -nolocalconf \
-c "mount c $DOS" -c "c:" \
-c "gspa ENTRY ENTRY.O ENTRY.LST -l" \
-c "gsplnk CINIT.CMD" \
-c "exit" -exit > "$WORK/dosbox.log" 2>&1 \
|| { tail -20 "$WORK/dosbox.log" >&2; fail "DOSBox failed"; }
[ -f "$DOS/ENTRY.LST" ] || fail "GSPA produced no ENTRY.LST"
sed 's/\r$//' "$DOS/ENTRY.LST" > "$WORK/entry.lst"
grep -q 'No Errors' "$WORK/entry.lst" \
|| { grep -i -B2 -A2 'error' "$WORK/entry.lst" >&2 || true; \
fail "GSPA reported errors"; }
grep -q 'No Warnings' "$WORK/entry.lst" \
|| { grep -i -B2 -A2 'warning' "$WORK/entry.lst" >&2 || true; \
fail "GSPA reported warnings"; }
[ -f "$DOS/CINIT.OUT" ] \
|| { tail -30 "$WORK/dosbox.log" >&2; fail "GSPLNK produced no CINIT.OUT"; }
cp "$DOS/CINIT.OUT" "$WORK/oracle.coff"
sed 's/\r$//' "$DOS/CINIT.MAP" > "$WORK/oracle.map"
echo "== link the same objects with tms-link"
"$TMS_LINK" -o "$WORK/tms-link.coff" --map "$WORK/tms-link.map" \
--entry _start --text 018A0000 --bss 01A00000 \
"$DOS/ENTRY.O" "$DOS/CINIT.O" \
|| fail "tms-link failed"
python3 - "$M7COFF" "$WORK/oracle.coff" "$WORK/tms-link.coff" \
"$WORK/cinit-env" <<'PY' || fail "tms-link .cinit output differs from GSPLNK"
import os
import struct
import sys
sys.dont_write_bytecode = True
sys.path.insert(0, os.path.dirname(sys.argv[1]))
import m7coff # noqa: E402
oracle_path, ours_path, env_path = sys.argv[2], sys.argv[3], sys.argv[4]
a = m7coff.parse(oracle_path)
b = m7coff.parse(ours_path)
if [s["name"] for s in a["sections"]] != [s["name"] for s in b["sections"]]:
sys.exit("section lists differ: %r != %r"
% ([s["name"] for s in a["sections"]],
[s["name"] for s in b["sections"]]))
if [s["name"] for s in a["sections"]] != [".text", ".data", ".cinit", ".bss"]:
sys.exit("sections %r, want .text/.data/.cinit/.bss"
% [s["name"] for s in a["sections"]])
for sa, sb in zip(a["sections"], b["sections"]):
if sa["paddr"] != sb["paddr"] or sa["size"] != sb["size"]:
sys.exit("%s paddr/size %#x/%d != %#x/%d"
% (sa["name"], sa["paddr"], sa["size"], sb["paddr"],
sb["size"]))
if sa["flags"] != sb["flags"]:
sys.exit("%s flags %#x != %#x"
% (sa["name"], sa["flags"], sb["flags"]))
if sa["name"] == ".bss":
continue
if sa["raw"] != sb["raw"]:
sys.exit("%s bytes differ: %s != %s"
% (sa["name"], sa["raw"].hex(), sb["raw"].hex()))
if sb["nreloc"]:
sys.exit("%s still has %d relocations" % (sb["name"], sb["nreloc"]))
if a["flags"] != b["flags"]:
sys.exit("file flags %#x != %#x" % (a["flags"], b["flags"]))
if a["opthdr"] != 28 or b["opthdr"] != 28:
sys.exit("optional header sizes %d != %d" % (a["opthdr"], b["opthdr"]))
opt_a = struct.unpack_from("<HHIIIIII", a["data"], 20)
opt_b = struct.unpack_from("<HHIIIIII", b["data"], 20)
if opt_a != opt_b:
sys.exit("optional header %r != %r" % (opt_a, opt_b))
def ext(obj):
return {s["name"]: s["value"] for s in obj["symbols"]
if s["sclass"] == 2 and s["secno"] > 0}
ea, eb = ext(a), ext(b)
if ea != eb:
missing = sorted(set(ea) - set(eb))
extra = sorted(set(eb) - set(ea))
wrong = sorted(n for n in set(ea) & set(eb) if ea[n] != eb[n])
sys.exit("public C_EXT mismatch: missing=%r extra=%r wrong=%r"
% (missing, extra,
[(n, hex(ea[n]), hex(eb[n])) for n in wrong]))
def sym(obj, name):
for s in obj["symbols"]:
if s["name"] == name:
return s
sys.exit("missing symbol %s" % name)
cinit_sec = m7coff.section(b, ".cinit")
cinit_sym = sym(b, "cinit")
table_sym = sym(b, "_cinit_table")
target_sym = sym(b, "_cinit_target")
if cinit_sym["secno"] != cinit_sec["index"] or cinit_sym["value"] != cinit_sec["paddr"]:
sys.exit("cinit symbol %#x sec %d is not the .cinit origin %#x"
% (cinit_sym["value"], cinit_sym["secno"], cinit_sec["paddr"]))
if table_sym["value"] != cinit_sec["paddr"]:
sys.exit("_cinit_table %#x is not the .cinit origin %#x"
% (table_sym["value"], cinit_sec["paddr"]))
if target_sym["secno"] == 0:
sys.exit("_cinit_target is not defined in .bss")
raw = cinit_sec["raw"]
count = int.from_bytes(raw[0:2], "little")
dest = int.from_bytes(raw[2:6], "little")
words = [int.from_bytes(raw[6 + i * 2:8 + i * 2], "little")
for i in range(3)]
term = int.from_bytes(raw[12:14], "little")
if count != 3 or words != [0x1111, 0x2222, 0x3333] or term != 0:
sys.exit("record count=%d words=%r term=%d, want 3/[0x1111,0x2222,"
"0x3333]/0" % (count, words, term))
if dest != target_sym["value"]:
sys.exit("record destination %#x is not the _cinit_target bit address %#x"
% (dest, target_sym["value"]))
if dest % 8:
sys.exit("record destination %#x is not byte aligned" % dest)
with open(env_path, "w", encoding="ascii") as fh:
fh.write("CINIT_TABLE=%08X\n" % cinit_sec["paddr"])
fh.write("CINIT_DEST=%08X\n" % target_sym["value"])
print(" oracle == tms-link: .text=%d .data=%d .cinit=%d .bss=%d bits"
% (a["sections"][0]["size"], a["sections"][1]["size"],
a["sections"][2]["size"], a["sections"][3]["size"]))
print(" .cinit at %08X: count=%d destination=%08X (_cinit_target) "
"words=%04X/%04X/%04X end=%d cinit=%08X"
% (cinit_sec["paddr"], count, dest, words[0], words[1], words[2],
term, cinit_sym["value"]))
print(" public C_EXT set equal (%d names)" % len(ea))
PY
echo "== exercise the linker-synthesized cinit symbol"
# CINIT.O alone has the table but no input defines `cinit`; tms-link must
# synthesize the public symbol at the .cinit origin. (The entry-defined path
# above is the real startup shape; this link-only case proves the other branch
# of the spec's "the linker defines the public cinit symbol".)
"$TMS_LINK" -o "$WORK/tms-link-synth.coff" \
--map "$WORK/tms-link-synth.map" --entry _cinit_probe \
--text 018A0000 --bss 01A00000 \
"$DOS/CINIT.O" \
|| fail "tms-link synthetic-cinit link failed"
python3 - "$M7COFF" "$DOS/CINIT.O" "$WORK/tms-link-synth.coff" \
"$WORK/tms-link.coff" <<'PY' \
|| fail "the linker did not synthesize cinit at the .cinit origin"
import os
import sys
sys.dont_write_bytecode = True
sys.path.insert(0, os.path.dirname(sys.argv[1]))
import m7coff # noqa: E402
inp_path, synth_path, main_path = sys.argv[2], sys.argv[3], sys.argv[4]
inp = m7coff.parse(inp_path)
if any(s["name"] == "cinit" and s["sclass"] == 2 and s["secno"] > 0
for s in inp["symbols"]):
sys.exit("CINIT.O already defines cinit; the synthesis case is invalid")
out = m7coff.parse(synth_path)
sec = m7coff.section(out, ".cinit")
csym = next((s for s in out["symbols"]
if s["name"] == "cinit" and s["sclass"] == 2), None)
if csym is None:
sys.exit("synthetic-cinit link has no public cinit symbol")
if csym["value"] != sec["paddr"] or csym["secno"] != sec["index"]:
sys.exit("synthetic cinit %#x sec %d is not the .cinit origin %#x"
% (csym["value"], csym["secno"], sec["paddr"]))
tsym = next((s for s in out["symbols"]
if s["name"] == "_cinit_table" and s["sclass"] == 2), None)
if tsym is None or tsym["value"] != sec["paddr"]:
sys.exit("synthetic link _cinit_table is not the .cinit origin")
target = next((s for s in out["symbols"]
if s["name"] == "_cinit_target" and s["sclass"] == 2), None)
if target is None:
sys.exit("synthetic link has no _cinit_target")
dest = int.from_bytes(sec["raw"][2:6], "little")
if dest != target["value"]:
sys.exit("synthetic link destination %#x is not _cinit_target %#x"
% (dest, target["value"]))
main = m7coff.parse(main_path)
main_sec = m7coff.section(main, ".cinit")
# The destination differs because this link has no entry-object .bss stack, so
# _cinit_target is at the ram origin. The count, data words and terminator
# must be byte-identical to the entry-defined link.
if (sec["raw"][:2] + sec["raw"][6:]) != (main_sec["raw"][:2] +
main_sec["raw"][6:]):
sys.exit("synthetic-cinit count/data/terminator differ from the "
"entry-defined link")
print(" synthesized cinit=%08X at the .cinit origin (no input defines it), "
"destination=%08X" % (csym["value"], dest))
PY
. "$WORK/cinit-env"
echo "== run the tms-link .cinit image in MAME"
cat > "$WORK/cinit-check.lua" <<'LUA'
dofile(os.getenv("M4_LOADER_LUA"))
local TABLE = tonumber(os.getenv("CINIT_TABLE"), 16)
local DEST = tonumber(os.getenv("CINIT_DEST"), 16)
local frames = 0
emu.register_frame_done(function()
frames = frames + 1
if frames == tonumber(os.getenv("LOADER_END") or "2700") - 1 then
local prog = manager.machine.devices[':maincpu'].spaces['program']
local count = prog:read_u16(TABLE)
local dest = prog:read_u32(TABLE + 16)
local w0 = prog:read_u16(TABLE + 48)
local w1 = prog:read_u16(TABLE + 64)
local w2 = prog:read_u16(TABLE + 80)
local term = prog:read_u16(TABLE + 96)
local d0 = prog:read_u16(DEST)
local d1 = prog:read_u16(DEST + 16)
local d2 = prog:read_u16(DEST + 32)
local marker = prog:read_u32(0x018A8000)
print(string.format(
"CINIT: table=%08X count=%d dest=%08X words=%04X/%04X/%04X " ..
"end=%d data=%04X/%04X/%04X marker=%d",
TABLE, count, dest, w0, w1, w2, term, d0, d1, d2, marker))
if count == 3 and dest == DEST and w0 == 0x1111 and
w1 == 0x2222 and w2 == 0x3333 and term == 0 and
d0 == 0x1111 and d1 == 0x2222 and d2 == 0x3333 and
marker == 0x6666 then
print("CINIT MAME: PASS")
else
print("CINIT MAME: FAIL (want count=3 dest=target " ..
"words=1111/2222/3333 end=0 marker=26214)")
end
end
end, "cinit-check")
LUA
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="$WORK/tms-link.coff" \
LOADER_OUT="$WORK/evidence" \
M4_LOADER_LUA="$LOADER_LUA" \
CINIT_TABLE="$CINIT_TABLE" CINIT_DEST="$CINIT_DEST" \
timeout "$MAME_TIMEOUT" "$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 "$WORK/cinit-check.lua" -autoboot_delay 0 \
> "$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 -q 'placed \.cinit .* 0 differing' "$WORK/mame.log" \
|| fail "COFF loader could not verify the placed .cinit bytes"
grep -q 'placed \.text .* 0 differing' "$WORK/mame.log" \
|| fail "COFF loader could not verify the placed .text bytes"
grep -Eq '^CINIT: table=.* count=3 dest=.* words=1111/2222/3333 end=0 data=1111/2222/3333 marker=26214$' \
"$WORK/mame.log" \
|| { grep '^CINIT:' "$WORK/mame.log" >&2 || true; \
fail ".cinit record or copied words are wrong in MAME"; }
grep -q '^CINIT MAME: PASS$' "$WORK/mame.log" \
|| fail "MAME .cinit marker check failed"
echo "M7 cinit: PASS (C TI record table, cinit at .cinit origin, destination"
echo " resolved to the _cinit_target bit address, copy loop in MAME,"
echo " bytes/placement equal to GSPLNK)"