Files

182 lines
7.1 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, 32-bit divisor
-- and hi(N) >= D -> two even-destination DIVUs (the M8.3 64/32
-- path, quotient needs 33+ bits);
-- div32 phase: 16384 iterations, nonzero dividend high word, 32-bit divisor
-- and hi(N) < D -> one even-destination DIVU plus the low-word
-- N - q*D reconstruction (the M9.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).
-- The div32 phase must be measurable and materially faster than the M8.3
-- two-DIVU wide phase: wide_frames*4 >= div32_frames*5 (1.25x).
--
-- All four 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 = 0xF56810CFEDA9D656, div32 = 0x00002C61C63842EE.
-- 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, INT64_MIN (both the small-divisor two-step case and the
-- 0xFFFFFFFF single-DIVU case, signed and unsigned magnitude) and the M9.3
-- single-DIVU cases (0x1_00000000/3, 0x1_FFFFFFFF/0x10000, -2^32/3 and
-- 2^32/-3), 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
-- 11 div32 done 0x6432D0E5 | 12/13 div32 checksum
dofile(os.getenv("M4_LOADER_LUA"))
local ENDF = tonumber(os.getenv("LOADER_END") or "5000")
local BASE = 0x01A20000
local START = 0x51A47A11
local FAST_DONE = 0xFA57D01E
local SLOW_DONE = 0x5104D0E5
local WIDE_DONE = 0x81DE0D0E
local DIV32_DONE = 0x6432D0E5
local STATUS_OK = 0x600D600D
local FAST_HI, FAST_LO = 0x00000001, 0x1E6F9AB4
local SLOW_HI, SLOW_LO = 0x00000000, 0x007512AE
local WIDE_HI, WIDE_LO = 0xF56810CF, 0xEDA9D656
local DIV32_HI, DIV32_LO = 0x00002C61, 0xC63842EE
local frames = 0
local start_f, fast_f, slow_f, wide_f, div32_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 div32_f == nil and rd(11) == DIV32_DONE then
div32_f = frames
end
if div32_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 div32_frames = div32_f - wide_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 div32_basis = div32_frames
if div32_basis < 1 then
div32_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 cs_div32_hi, cs_div32_lo = rd(13), rd(12)
local ratio = slow_frames / basis
local wide_ratio = slow_frames / wide_basis
local div32_ratio = wide_frames / div32_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
and cs_div32_hi == DIV32_HI and cs_div32_lo == DIV32_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
and div32_frames >= 2
and wide_frames * 4 >= div32_basis * 5
print(string.format(
"M7: i64div perf: start=%d fast_done=%d slow_done=%d "
.. "wide_done=%d div32_done=%d fast_frames=%d "
.. "slow_frames=%d wide_frames=%d div32_frames=%d "
.. "ratio=%.1f wide_ratio=%.1f div32_ratio=%.1f "
.. "status=%08X cs_fast=%08X%08X cs_slow=%08X%08X "
.. "cs_wide=%08X%08X cs_div32=%08X%08X "
.. "cs_expected=%08X%08X/%08X%08X/%08X%08X/%08X%08X cs=%s",
start_f, fast_f, slow_f, wide_f, div32_f, fast_frames,
slow_frames, wide_frames, div32_frames, ratio, wide_ratio,
div32_ratio,
status, cs_fast_hi, cs_fast_lo, cs_slow_hi, cs_slow_lo,
cs_wide_hi, cs_wide_lo, cs_div32_hi, cs_div32_lo,
FAST_HI, FAST_LO, SLOW_HI, SLOW_LO, WIDE_HI, WIDE_LO,
DIV32_HI, DIV32_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 "
.. "div32_frames=%d; "
.. "want status %08X, fast %08X%08X, slow %08X%08X, "
.. "wide %08X%08X, div32 %08X%08X, "
.. "slow_frames >= 20 and >= 8*max(fast_frames,1), "
.. "wide_frames >= 2 and "
.. "slow_frames >= 4*max(wide_frames,1), "
.. "div32_frames >= 2 and "
.. "4*wide_frames >= 5*max(div32_frames,1))",
status, cs_ok and "ok" or "BAD",
fast_frames, slow_frames, wide_frames, div32_frames,
STATUS_OK,
FAST_HI, FAST_LO, SLOW_HI, SLOW_LO,
WIDE_HI, WIDE_LO, DIV32_HI, DIV32_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 "
.. "div32_done=%s",
tostring(start_f), tostring(fast_f), tostring(slow_f),
tostring(wide_f), tostring(div32_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")