"""Read the tests whose records are packed: DMA fits, walk masks, HV pairs. python tools/timing.py A=FILE B=FILE ... A DMA record is (HV before << 16) | HV after, with the 68000 held until the end. One H step is 3420/210 MCLK on average, so a size's cost carries about +/-16 MCLK; the fit over 11 sizes and 4 repeats is what has the resolution. """ import sys import decode STEP = 3420 / 210 def hstep(h): return h if h <= 0xB6 else h - 0xE4 + 182.5 # $E4 is half a step def steps(hv0, hv1): x0, x1 = (hstep(hv0 & 0xFF) - hstep(0xA5)) % 210, (hstep(hv1 & 0xFF) - hstep(0xA5)) % 210 return ((hv1 >> 8) - (hv0 >> 8) & 0xFF) * 210 + x1 - x0 def recs(run, tag): t = next((t for t in run["tests"] if t["tag"] == tag), None) return t["records"] if t else [] def fit(xs, ys): n = len(xs) mx, my = sum(xs) / n, sum(ys) / n b = sum((x - mx) * (y - my) for x, y in zip(xs, ys)) / sum((x - mx) ** 2 for x in xs) a = my - b * mx return a, b, max(abs(y - a - b * x) for x, y in zip(xs, ys)) def dma(run, tag, base): by = {} for i, rep, v in recs(run, tag): by.setdefault(i - base, []).append(steps(v >> 16, v & 0xFFFF) * STEP) return by def resets(path): """Mean drift of the 18 reads against a clean 196 MCLK a read. A refresh adds 14 MCLK from the read after it on, so the step in this curve dates the first refreshes after a reset. The H counter's 16 MCLK averages out over presses: read it over 16 or more. Entry 1 is the cold boot.""" rows = decode.parse(path)["resets"][1:] if not rows: return drift = [0.0] * 18 for _, hv in rows: for k in range(18): d = ((hstep(hv[k] & 0xFF) - hstep(hv[0] & 0xFF)) % 210) * STEP # V is no use here drift[k] += d - 196 * k print(f"RESET {path}: {len(rows)} resets; MCLK, drift") print(" " + " ".join(f"{196 * k}:{x / len(rows):.0f}" for k, x in enumerate(drift))) if __name__ == "__main__": if "--resets" in sys.argv: for p in sys.argv[2:]: resets(p) sys.exit() R = {n: decode.latest(decode.parse(p)) for n, p in (x.split("=", 1) for x in sys.argv[1:])} for tag, base in (("DMPR", 200), ("DMPW", 300)): print(tag, "mean MCLK from HV before to HV after, by words") tab = {n: dma(r, tag, base) for n, r in R.items()} sizes = sorted(next(iter(tab.values()))) print(f"{'':>6}" + "".join(f"{n:>18}" for n in R)) for s in sizes: print(f"{s:6}" + "".join(f"{sum(t[s]) / len(t[s]):10.0f} +{max(t[s]) - min(t[s]):<6.0f}" for t in tab.values())) for n, t in tab.items(): xs = [s for s in sizes for _ in t[s]] ys = [y for s in sizes for y in t[s]] a, b, w = fit(xs, ys) print(f" {n}: {a:.0f} + {b:.2f} per word, worst {w:.0f}") for tag in ("BSK1", "BSK2", "SPY1", "SPX1", "SPY0", "SPX0"): print(tag) for n, r in R.items(): print(f" {n:10}", " ".join(f"{i}.{rep}:{v:08X}" if v > 0xFFFF else f"{i}.{rep}:{v:04X}" for i, rep, v in recs(r, tag))) for tag in ("VPOS", "RDV0", "RDV1", "IDL1", "IDL2", "YMPT"): print(tag) for n, r in R.items(): print(f" {n:10}", " ".join(f"{i}.{rep}:{v:X}" if tag in ("VPOS", "YMPT") else f"{i}.{rep}:{v}" for i, rep, v in recs(r, tag))) print("VSTP last H of old V, first H of new V") for n, r in R.items(): for i, name in ((900, "picture"), (903, "blank")): v = [x[2] for x in recs(r, "VSTP") if x[2] and x[0] == i] if v: print(f" {n:10} {name:8} old {max(x >> 16 & 0xFF for x in v):02X} new {min(x & 0xFF for x in v):02X} ({len(v)} of 32 saw the step)") print("RDLN reads: 720 active on, 721 blank on, 722 active off, 723 blank off") for n, r in R.items(): print(f" {n:10}", " ".join(f"{i}.{rep}:{v}" for i, rep, v in recs(r, "RDLN"))) print("FFLG first NOP with F set, its HV; INTF NOP that took VINT, HV in handler") for n, r in R.items(): f = recs(r, "FFLG")[1:] # the first holds an F left pending by the test before pos = lambda v: (hstep(v >> 16 & 0xFF) - hstep(0xA5)) % 210 clear = [pos(v) for _, _, v in f if not v & 0x80] shown = [pos(v) for _, _, v in f if v & 0x80] i = recs(r, "INTF") hit = sorted(pos(v) for _, _, v in i) print(f" {n:10} F clear to step {max(clear, default=-1)}, set from {min(shown, default=-1)}; handler HV steps {hit[0] if hit else 0}-{hit[-1] if hit else 0}, NOPs", " ".join(str(v & 0xFFFF) for _, _, v in i)) for tag in ("BSK3", "BSK4", "BSK5", "BSK6"): print(tag, "granted, of 32, per repeat; masks") for n, r in R.items(): print(f" {n:10}", " ".join(f"{v:08X}({bin(v).count('1')})" for _, _, v in recs(r, tag))) print("YMP2 610 flag set port p; 613 port 0; 612 cleared: port 1, port 0; 611 port p after data write; 614 latch 0/20/60 frames") for n, r in R.items(): print(f" {n:10}", " ".join(f"{i}.{rep}:{v:X}" for i, rep, v in recs(r, "YMP2"))) print("YMBY busy steps out of 64, NOP steps") for n, r in R.items(): y = recs(r, "YMBY") print(f" {n:10}", " ".join(f"{bin(a[2]).count('1') + bin(b[2]).count('1')}" for a, b in zip(y[::2], y[1::2])), f"{y[0][2]:X}" if y else "") print("YMDK port 1 after port 0 read 1 and the flag cleared: 0 1 2 4 8 16 32 64 128 lines | 1 2 3 4 6 8 10 12 15 18 frames") for n, r in R.items(): y = recs(r, "YMDK") if y: print(f" {n:10}", " ".join(f"{v:X}" for i, _, v in y if i == 630), "|", " ".join(f"{v:X}" for i, _, v in y if i == 631)) for tag in ("SFY1", "SFX1", "SFY0", "SFX0"): print(tag, " ".join(f"{n}={next(iter(x[2] for x in recs(r, tag)), 0):032b}" for n, r in R.items())) print("DMA cost less the code's own") for tag, base in (("DMPR", 200), ("DMPW", 300), ("DMPC", 1000), ("DMPS", 1100)): for n, r in R.items(): b = [steps(v >> 16, v & 0xFFFF) * STEP for _, _, v in recs(r, "DMB0")] base0 = sum(b) / len(b) if b else 0 t = dma(r, tag, base) print(f" {tag} {n:10} code {base0:.0f} " + " ".join(f"{s}:{sum(t[s]) / len(t[s]) - base0:.0f}" for s in sorted(t)))