"""Compare bus transaction sequences against SingleStepTests' per-T-state cycles array.""" import sys, json, glob, os SURVEY = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # this kit's root RAM = bytearray(65536); EV = []; PORTQ = [] SRC = sys.argv[1] # path to z80_python src tree LIMIT = int(sys.argv[2]) if len(sys.argv) > 2 else 0 sys.path.insert(0, SRC) from z80_python.cpu import Z80CPU class Host(Z80CPU): def read_byte(s, a): v = RAM[a & 0xffff]; EV.append(("MR", a & 0xffff, v)); return v def write_byte(s, a, v): EV.append(("MW", a & 0xffff, v & 0xff)); RAM[a & 0xffff] = v & 0xff def read_port(s, a): v = PORTQ.pop(0) if PORTQ else 0xff EV.append(("PR", a & 0xffff, v)); return v def write_port(s, a, v): EV.append(("PW", a & 0xffff, v & 0xff)) cpu = Host() PIN = {"r-m-": "MR", "-wm-": "MW", "r--i": "PR", "-w-i": "PW"} def load(st): cpu.a, cpu.f.byte = st['a'], st['f'] cpu.b, cpu.c = st['b'], st['c'] cpu.d, cpu.e = st['d'], st['e'] cpu.h, cpu.l = st['h'], st['l'] cpu.af_, cpu.bc_, cpu.de_, cpu.hl_ = st['af_'], st['bc_'], st['de_'], st['hl_'] cpu.ix, cpu.iy, cpu.sp, cpu.pc = st['ix'], st['iy'], st['sp'], st['pc'] cpu.wz = st['wz']; cpu.i, cpu.r = st['i'], st['r'] cpu.iff1, cpu.iff2 = bool(st['iff1']), bool(st['iff2']) cpu.im = st['im']; cpu.halted = False for a, v in st['ram']: RAM[a & 0xffff] = v files = sorted(glob.glob(os.path.join(SURVEY, "sst-z80/v1/*.json"))) if LIMIT: files = files[:LIMIT] tot = seq = addr_only = 0 badops = {} for fn in files: op = os.path.basename(fn)[:-5] for case in json.load(open(fn)): cyc = case['cycles']; want = [] for k, c in enumerate(cyc): if c[2] not in PIN: continue kind = PIN[c[2]] if kind in ("MW", "PW"): val = c[1] # write value is inline else: val = cyc[k+1][1] if k+1 < len(cyc) else None # read latches next cycle want.append((kind, c[0] & 0xffff, val)) RAM[:] = bytes(65536) load(case['initial']) EV.clear() PORTQ[:] = [v for t, _, v in want if t == "PR"] try: cpu.step() except Exception: tot += 1; badops[op] = badops.get(op, 0) + 1; continue got = list(EV); tot += 1 if [(t, a) for t, a, _ in got] == [(t, a) for t, a, _ in want]: addr_only += 1 if [(t, a, v) for t, a, v in got] == [(t, a, v) for t, a, v in want]: seq += 1 else: badops[op] = badops.get(op, 0) + 1 else: badops[op] = badops.get(op, 0) + 1 print(f"SRC={SRC}") print(f" cases={tot}") print(f" transaction (type,address) sequence : {addr_only} ({100*addr_only/tot:.3f}%)") print(f" transaction (type,address,value) : {seq} ({100*seq/tot:.3f}%)") print(f" opcodes with any mismatch: {len(badops)} / {len(files)}") for o, n in sorted(badops.items(), key=lambda x: -x[1])[:12]: print(f" {o}: {n}")