Files

148 lines
5.5 KiB
Lua

-- i64div-perf-check.lua -- measure the M7.4 i64 divide/remainder fast path in
-- MAME.
--
-- Loads the gate COFF with vendor/vt1200/dwt/loader/loader.lua (through the
-- M4_LOADER_LUA variable scripts/m4/run.sh exports) and polls the marker
-- words once per frame. A MAME frame is a fixed number of emulated 50 MHz
-- cycles, so the number of frames between the start marker and each phase's
-- completion marker is a reproducible cycle proxy for the equal-iteration
-- workloads in scripts/m7/i64div-perf.c:
--
-- fast phase: 16384 iterations, every divide fits 32 bits -> one DIVU each;
-- slow phase: 16384 iterations, both high words nonzero -> the 64-step loop;
-- wide phase: 16384 iterations, nonzero dividend high word but a 32-bit
-- divisor -> two even-destination DIVUs (the M8.3 64/32 path).
--
-- The gate requires the fast phase to complete in materially fewer frames
-- (slow_frames >= 8 * max(fast_frames, 1)) and the slow phase to be long
-- enough to measure (>= 20 frames). A sub-frame fast phase is reported as
-- fast_frames=0 and compared as one frame, which can only make the check
-- harder. The wide phase must also be materially faster than the loop
-- (slow_frames >= 4 * max(wide_frames, 1)) and measurable (>= 2 frames).
--
-- All three phase checksums are required to equal the deterministic values
-- below. They were computed from the same LCG/arithmetic with an independent
-- host implementation of scripts/m7/i64div-perf.c (see the header comment
-- there):
-- fast = 0x000000011E6F9AB4, slow = 0x00000000007512AE,
-- wide = 0x00025AF70AF974E0.
-- The in-image self-check (marker 7) also covers large 32-bit magnitudes
-- (0xFFFFFFFF/7, -123/7 and INT32_MIN/3) plus the 64/32 shapes with hi >= d,
-- hi == d, hi < d and INT64_MIN, so a wrong quotient or remainder fails before
-- the timing comparison.
--
-- Markers at 01A20000h, 0x20 bits apart (free RAM above the 4 KiB entry stack;
-- scripts/m7/i64div-map-check.py asserts they are outside .text/.data/.bss):
-- 0 start 0x51A47A11 | 1/2 fast checksum | 3 fast done 0xFA57D01E
-- 4/5 slow checksum | 6 slow done 0x5104D0E5 | 7 status 0x600D600D
-- 8 wide done 0x81DE0D0E | 9/10 wide checksum
dofile(os.getenv("M4_LOADER_LUA"))
local ENDF = tonumber(os.getenv("LOADER_END") or "4000")
local BASE = 0x01A20000
local START = 0x51A47A11
local FAST_DONE = 0xFA57D01E
local SLOW_DONE = 0x5104D0E5
local WIDE_DONE = 0x81DE0D0E
local STATUS_OK = 0x600D600D
local FAST_HI, FAST_LO = 0x00000001, 0x1E6F9AB4
local SLOW_HI, SLOW_LO = 0x00000000, 0x007512AE
local WIDE_HI, WIDE_LO = 0x00025AF7, 0x0AF974E0
local frames = 0
local start_f, fast_f, slow_f, wide_f
local reported = false
local function rd(n)
local prog = manager.machine.devices[':maincpu'].spaces['program']
return prog:read_u32(BASE + n * 0x20)
end
emu.register_frame_done(function()
frames = frames + 1
if start_f == nil and rd(0) == START then
start_f = frames
end
if start_f ~= nil and fast_f == nil and rd(3) == FAST_DONE then
fast_f = frames
end
if fast_f ~= nil and slow_f == nil and rd(6) == SLOW_DONE then
slow_f = frames
end
if slow_f ~= nil and wide_f == nil and rd(8) == WIDE_DONE then
wide_f = frames
end
if wide_f ~= nil and not reported then
reported = true
local fast_frames = fast_f - start_f
local slow_frames = slow_f - fast_f
local wide_frames = wide_f - slow_f
local basis = fast_frames
if basis < 1 then
basis = 1
end
local wide_basis = wide_frames
if wide_basis < 1 then
wide_basis = 1
end
local status = rd(7)
local cs_fast_hi, cs_fast_lo = rd(2), rd(1)
local cs_slow_hi, cs_slow_lo = rd(5), rd(4)
local cs_wide_hi, cs_wide_lo = rd(10), rd(9)
local ratio = slow_frames / basis
local wide_ratio = slow_frames / wide_basis
local cs_ok = cs_fast_hi == FAST_HI and cs_fast_lo == FAST_LO
and cs_slow_hi == SLOW_HI and cs_slow_lo == SLOW_LO
and cs_wide_hi == WIDE_HI and cs_wide_lo == WIDE_LO
local ok = status == STATUS_OK and cs_ok
and slow_frames >= 20
and slow_frames >= 8 * basis
and wide_frames >= 2
and slow_frames >= 4 * wide_basis
print(string.format(
"M7: i64div perf: start=%d fast_done=%d slow_done=%d "
.. "wide_done=%d fast_frames=%d slow_frames=%d "
.. "wide_frames=%d ratio=%.1f wide_ratio=%.1f "
.. "status=%08X cs_fast=%08X%08X cs_slow=%08X%08X "
.. "cs_wide=%08X%08X "
.. "cs_expected=%08X%08X/%08X%08X/%08X%08X cs=%s",
start_f, fast_f, slow_f, wide_f, fast_frames, slow_frames,
wide_frames, ratio, wide_ratio,
status, cs_fast_hi, cs_fast_lo, cs_slow_hi, cs_slow_lo,
cs_wide_hi, cs_wide_lo,
FAST_HI, FAST_LO, SLOW_HI, SLOW_LO, WIDE_HI, WIDE_LO,
cs_ok and "ok" or "BAD"))
if ok then
print("M7 MAME: PASS")
else
print(string.format(
"M7 MAME: FAIL (status=%08X checksums=%s "
.. "fast_frames=%d slow_frames=%d wide_frames=%d; "
.. "want status %08X, fast %08X%08X, slow %08X%08X, "
.. "wide %08X%08X, slow_frames >= 20 and "
.. ">= 8*max(fast_frames,1), wide_frames >= 2 and "
.. "slow_frames >= 4*max(wide_frames,1))",
status, cs_ok and "ok" or "BAD",
fast_frames, slow_frames, wide_frames, STATUS_OK,
FAST_HI, FAST_LO, SLOW_HI, SLOW_LO,
WIDE_HI, WIDE_LO))
end
elseif frames == ENDF - 1 and not reported then
reported = true
local seen = string.format(
"start=%s fast_done=%s slow_done=%s wide_done=%s",
tostring(start_f), tostring(fast_f), tostring(slow_f),
tostring(wide_f))
print(string.format(
"M7: i64div perf: incomplete at frame %d at marker 0x%08X",
frames, BASE))
print("M7 MAME: FAIL (timed out: " .. seen .. ")")
end
end, "i64div-perf-check")