-- runtime-check.lua -- read the M6 runtime gate marker words and the shim's -- printf output block, and compare them with the expected values. -- -- RUNTIME_MARKERS is the bit address of _runtime_markers from the linker map; -- each marker is one 32-bit word at base + n*0x20 bits. The shim's CONF -- output block is fixed: length at 01B00020h, bytes at 01B10000h (one byte per -- 8 bits). The checker compares the whole output byte-for-byte, including the -- newlines, so the format implementation is the runtime oracle. -- -- The marker read runs one frame before loader.lua's inspection/exit frame. dofile(os.getenv("M2_LOADER_LUA")) local ENDF = tonumber(os.getenv("LOADER_END") or "3400") local BASE = tonumber(os.getenv("RUNTIME_MARKERS"), 16) local LEN_ADDR = 0x01B00020 local OUT = 0x01B10000 local CAP = 16384 local N = 13 -- markers 0..12 are written by the driver local SENT = 15 -- marker 15 is written after runtime_run returns local frames = 0 local checked = false local expect = { [0] = 31, -- printf("%d %s %f %lld\n", ...) length [1] = 9, -- sprintf("%d|%s|%.1f", 7, "abc", 2.5) length [2] = 19, -- sprintf("%lld", 1234567890123456789) length [3] = 300, -- sprintf truncation: full length, not the stored one [4] = 255, -- strlen after truncation (SPRINTF_CAP-1) [5] = 1, -- the truncated buffer is NUL-terminated [6] = 4, -- (int)(1.5 + 2.5), flib FD$ADD + FD$DTOI [7] = 3, -- (int)(1.5 * 2.5), flib FD$MUL + FD$DTOI [8] = 1, -- memcpy result equals the source [9] = 1, -- overlapping memmove forward ("aabcdef") [10] = 1, -- overlapping memmove backward ("3456567") [11] = 17, -- printf("S[%s] (%d)\n", ...) length [12] = 1, -- rts.lib _isascii('A') nonzero, (0xC8) zero } local lines = { "-42 hi 3.250000 -1234567890123", "[ 42][-0042][7 ][+5][ 5][ff][0xff][10][010][ abc]", "c=[ A][B ][C]", "9223372036854775807 -9223372036854775808 " .. "18446744073709551615 0 -1 4294967295 " .. "[-0000000000000000005]", "int=[-123456789][123456789abcdef0][110642547423257157360][00042][42 ]", "-3.250000 -3.25 3 1.3 3.25 3.25 +3.250000", "0.000000|00003.25|0.00| 3.25| 3.250000|3.|3.250000", "cap=2147483520.000000", "prec=[010][][][][][][0][0][ ]", "round=10.000000 ncap=-2147483520.000000", "S[7|abc|2.5] (9)", "T(300,255,1)", "mem=abcdefg abcdefg", "mov=aabcdef", "mov2=3456567", "rts=1", "flib 4.000000 3.750000", } local want_out = table.concat(lines, "\n") .. "\n" local function read_string(space, addr, len) local s = {} for i = 0, len - 1 do s[#s + 1] = string.char(space:read_u8(addr + i * 8)) end return table.concat(s) end local function escape(s) return (s:gsub("\n", "\\n")) end emu.register_frame_done(function() frames = frames + 1 if frames == ENDF - 1 and not checked then checked = true local prog = manager.machine.devices[':maincpu'].spaces['program'] local got = {} local bad = 0 for i = 0, N - 1 do got[i] = prog:read_u32(BASE + i * 0x20) if got[i] ~= expect[i] then bad = bad + 1 end end got[SENT] = prog:read_u32(BASE + SENT * 0x20) if got[SENT] ~= 0x5A5A5A5A then bad = bad + 1 end io.write(string.format("M6 runtime:")) for i = 0, N - 1 do io.write(string.format(" %08X", got[i])) end io.write(string.format(" %08X\n", got[SENT])) local len = prog:read_u32(LEN_ADDR) if len > CAP then print(string.format("M6 runtime: output length %d > cap %d", len, CAP)) bad = bad + 1 len = 0 end local out = read_string(prog, OUT, len) print(string.format("M6 runtime out(%d): '%s'", len, escape(out))) if out ~= want_out then local diff = 1 while diff <= #out and diff <= #want_out and out:sub(diff, diff) == want_out:sub(diff, diff) do diff = diff + 1 end print(string.format( "M6 runtime: output differs at byte %d: got '%s' want '%s'", diff, escape(out:sub(diff, diff + 20)), escape(want_out:sub(diff, diff + 20)))) bad = bad + 1 end if bad == 0 then print("M6 RUNTIME MAME: PASS") else for i = 0, N - 1 do if got[i] ~= expect[i] then print(string.format( "M6 runtime: marker %d got %08X want %08X", i, got[i], expect[i])) end end if got[SENT] ~= 0x5A5A5A5A then print(string.format( "M6 runtime: sentinel got %08X want 5A5A5A5A", got[SENT])) end print("M6 RUNTIME MAME: FAIL") end end end, "runtime-check")