Files

175 lines
4.9 KiB
Lua

-- bare-check.lua -- run a freestanding conformance COFF on the VT1200 CPU
-- without booting the firmware or using the COFF loader.
--
-- The test harness maps the image itself: each section is copied to its COFF
-- paddr, .bss is zeroed, the result block is cleared, and PC/SP are set. This
-- is the fast tier for codegen/semantics tests; the full loader/X11/runtime
-- tiers still use loader.lua.
--
-- Required env: BARE_COFF, BARE_ENTRY, BARE_SP (bit addresses), CONF_RESULT.
-- Optional: BARE_END (frame budget, default 1000).
local machine = manager.machine
local cpu = machine.devices[':maincpu']
local prog = cpu.spaces['program']
local COFF = os.getenv("BARE_COFF") or ""
local ENTRY = tonumber(os.getenv("BARE_ENTRY") or "0", 16)
local SP = tonumber(os.getenv("BARE_SP") or "0", 16)
local ENDF = tonumber(os.getenv("BARE_END") or "1000")
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 ST_IE = 0x200000
local function slurp(path)
local f = io.open(path, "rb")
if not f then error("cannot open " .. path) end
local d = f:read("*a")
f:close()
return d
end
local function u16(d, o) return string.unpack("<I2", d, o + 1) end
local function u32(d, o) return string.unpack("<I4", d, o + 1) end
local function parse_coff(d)
local c = {}
c.magic = u16(d, 0)
c.nscns = u16(d, 2)
c.opthdr = u16(d, 16)
c.entry = c.opthdr ~= 0 and u32(d, 20 + 16) or 0
c.sections = {}
local base = 20 + c.opthdr
for i = 0, c.nscns - 1 do
local o = base + i * 40
local name = d:sub(o + 1, o + 8):gsub("%z", "")
c.sections[#c.sections + 1] = {
name = name,
paddr = u32(d, o + 8),
size = u32(d, o + 16),
scnptr = u32(d, o + 20),
flags = u32(d, o + 36),
}
end
return c
end
local function poke_bytes(bitaddr, data)
local n = math.floor((#data + 1) / 2)
for i = 0, n - 1 do
local lo = data:byte(i * 2 + 1) or 0
local hi = data:byte(i * 2 + 2) or 0
prog:write_u16(bitaddr + i * 16, lo + hi * 256)
end
return n
end
local function zero_bytes(bitaddr, nbytes)
for i = 0, math.floor((nbytes + 1) / 2) - 1 do
prog:write_u16(bitaddr + i * 16, 0)
end
end
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
local function dump(tag)
local status = rd32(STATUS)
local raw_len = rd32(LEN)
local magic = rd32(MAGIC)
local raw_errlen = rd32(ERRLEN)
local bytes, n = {}, 0
local nread = (raw_len >= 0 and raw_len <= CAP) and raw_len or 0
for i = 0, nread - 1 do
bytes[#bytes + 1] = string.format("%02X", rd8(OUT + i * 8))
n = n + 1
end
local errbytes = {}
local erread = (raw_errlen >= 0 and raw_errlen <= CAP) and raw_errlen or 0
for i = 0, erread - 1 do
errbytes[#errbytes + 1] = string.format("%02X", rd8(ERR + i * 8))
end
print(string.format(
"CONF: %s status=%08X len=%d magic=%08X loader=%08X out=%s errlen=%d err=%s",
tag, status, raw_len, magic, 0, table.concat(bytes),
raw_errlen, table.concat(errbytes)))
local f = io.open(RESULT, "w")
if f then
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("loader=00000000\n")
f:write("out=" .. table.concat(bytes) .. "\n")
f:write(string.format("errlen=%d\n", raw_errlen))
f:write("err=" .. table.concat(errbytes) .. "\n")
f:close()
end
if magic == MAGIC_VAL then
print("BARE MAME: PASS")
else
print("BARE MAME: FAIL")
end
end
local frames = 0
local started = false
local done = false
local function setup()
local d = slurp(COFF)
local c = parse_coff(d)
if c.magic ~= 0x0090 then
print(string.format("BARE: bad COFF magic %04X", c.magic))
machine:exit()
return
end
for _, s in ipairs(c.sections) do
if s.name == ".bss" then
zero_bytes(s.paddr, math.floor((s.size + 7) / 8))
elseif s.size > 0 then
local nb = math.floor((s.size + 7) / 8)
poke_bytes(s.paddr, d:sub(s.scnptr + 1, s.scnptr + nb))
end
end
-- Clear the result block; the entry also does it, but this is idempotent.
prog:write_u32(STATUS, 0); prog:write_u32(LEN, 0)
prog:write_u32(MAGIC, 0); prog:write_u32(ERRLEN, 0)
cpu.state['SP'].value = SP
local st = cpu.state['ST'].value
cpu.state['ST'].value = st & ~ST_IE
cpu.state['PC'].value = (ENTRY ~= 0) and ENTRY or c.entry
print(string.format("BARE: entry=%08X sp=%08X sections=%d", cpu.state['PC'].value, SP, c.nscns))
end
emu.register_frame_done(function()
frames = frames + 1
if not started then
started = true
setup()
return
end
if done then return end
if rd32(MAGIC) == MAGIC_VAL or frames >= ENDF then
done = true
dump(frames >= ENDF and "timeout" or "result")
machine:exit()
end
end, "m5-bare-check")