-- Boot regression: confirm Human68k still reaches a live console after the
-- CRTC clear-timing and VRAM wait-state changes. Counts non-zero bytes in the
-- text plane (TVRAM plane 0 at $E00000) -- a real console lights up hundreds
-- of bytes; a hung or blank machine leaves it near zero.
local cpu = manager.machine.devices[":maincpu"]
local mem = cpu.spaces["program"]
local frame = 0

local function tvramNZ()
    local n = 0
    for i = 0, 8191 do
        if mem:read_u8(0xE00000 + i) ~= 0 then n = n + 1 end
    end
    return n
end

emu.register_frame_done(function()
    frame = frame + 1
    if frame % 300 == 0 and frame < 1200 then
        io.write(string.format("BOOTPROG frame=%d tvramNZ=%d pc=%06X\n",
                 frame, tvramNZ(), cpu.state["CURPC"].value))
        io.flush()
    elseif frame == 1200 then
        local g = 0
        for i = 0, 4095 do
            if mem:read_u8(0xC00000 + i) ~= 0 then g = g + 1 end
        end
        io.write(string.format("BOOTCHECK frame=%d tvramNZ=%d gvramNZ=%d pc=%06X\n",
                 frame, tvramNZ(), g, cpu.state["CURPC"].value))
        io.flush()
        manager.machine:exit()
    end
end)
