Files

147 lines
4.2 KiB
Lua

-- conformance-check.lua -- load the conformance COFF with the M4 loader and
-- publish the shim's result block.
--
-- The loader is scripts/m4/run.sh's loader (vendor/vt1200/dwt/loader/loader.lua),
-- loaded through M4_LOADER_LUA. The test's entry writes:
-- 01B00000 status
-- 01B00020 out_len
-- 01B00040 magic 0x434F4E46
-- 01B00060 err_len
-- 01B10000 output bytes (bit addresses, 8 bits apart; 16 KiB ends at
-- 01B2FFFF, so it cannot reach stderr)
-- 01B30000 stderr bytes (bit addresses, 8 bits apart; 16 KiB)
--
-- The block is above the linker-managed .bss region (01A00000..01A80000), so
-- test globals cannot overwrite it. The runner rejects len >= the 16 KiB cap
-- (or errlen >= the cap) because the shim truncates there.
--
-- The result is written to CONF_RESULT in a stable, shell-readable form:
-- status=<8 hex>
-- len=<decimal>
-- magic=<8 hex>
-- loader=<8 hex> (loader result block status at 01882D00)
-- out=<uppercase hex bytes>
-- errlen=<decimal>
-- err=<uppercase hex bytes>
--
-- The runner compares this file against the manifest/expected result. A
-- missing magic is a failure, never a pass.
dofile(os.getenv("M4_LOADER_LUA"))
local machine = manager.machine
local cpu = machine.devices[':maincpu']
local prog = cpu.spaces['program']
local ENDF = tonumber(os.getenv("LOADER_END") or "2700")
local AT = tonumber(os.getenv("LOADER_AT") or "2500")
local RESULT = os.getenv("CONF_RESULT") or "conformance-result.txt"
local STATUS = 0x01B00000
local LEN = 0x01B00020
local MAGIC = 0x01B00040
local ERRLEN = 0x01B00060
local OUT = 0x01B10000
local ERR = 0x01B30000
local CAP = 16384
local MAGIC_VAL = 0x434F4E46
local LOADER_STATUS = 0x01882D00
local frames = 0
local done = false
local function rd32(a)
local ok, v = pcall(function() return prog:read_u32(a) end)
if ok then return v end
return 0xDEADBEEF
end
local function rd8(a)
local ok, v = pcall(function() return prog:read_u8(a) end)
if ok then return v end
return 0
end
-- Read raw_len bytes at bit address `base` (8 bits apart). Keep the raw
-- length for the runner (it rejects len >= CAP), but never loop over an
-- implausible value. Returns the full hex dump and its 128-byte preview.
local function dump_bytes(base, raw_len)
local bytes = {}
local shown = {}
local n = 0
local nread = raw_len
if nread < 0 or nread > CAP then
nread = 0
end
for i = 0, nread - 1 do
local b = rd8(base + i * 8)
bytes[#bytes + 1] = string.format("%02X", b)
if i < 128 then
shown[#shown + 1] = string.format("%02X", b)
end
n = n + 1
end
local shownhex = table.concat(shown)
if n > 128 then
shownhex = shownhex .. "..."
end
return table.concat(bytes), shownhex
end
local function dump(tag)
local status = rd32(STATUS)
local raw_len = rd32(LEN)
local raw_errlen = rd32(ERRLEN)
local magic = rd32(MAGIC)
local loader = rd32(LOADER_STATUS)
local outhex, shownhex = dump_bytes(OUT, raw_len)
local errhex, errshownhex = dump_bytes(ERR, raw_errlen)
print(string.format(
"CONF: %s status=%08X len=%d magic=%08X loader=%08X out=%s errlen=%d err=%s",
tag, status, raw_len, magic, loader, shownhex,
raw_errlen, errshownhex))
local f, err = io.open(RESULT, "w")
if not f then
print("CONF: cannot write result " .. tostring(RESULT) ..
": " .. tostring(err))
machine:exit()
return
end
f:write(string.format("status=%08X\n", status))
f:write(string.format("len=%d\n", raw_len))
f:write(string.format("magic=%08X\n", magic))
f:write(string.format("loader=%08X\n", loader))
f:write("out=" .. outhex .. "\n")
f:write(string.format("errlen=%d\n", raw_errlen))
f:write("err=" .. errhex .. "\n")
f:close()
if magic == MAGIC_VAL and loader == 0 then
print("CONF MAME: PASS")
else
print("CONF MAME: FAIL")
end
end
emu.register_frame_done(function()
frames = frames + 1
if frames <= AT or done then
return
end
local magic = rd32(MAGIC)
local loader = rd32(LOADER_STATUS)
-- A successful run publishes the magic. A loader error after injection
-- is final even if the app never starts. Otherwise wait for ENDF.
if magic == MAGIC_VAL or (frames > AT + 60 and loader ~= 0)
or frames >= ENDF then
done = true
dump(frames >= ENDF and "timeout" or "result")
machine:exit()
end
end, "m5-conformance-check")