"""Run SingleStepTests/z80 vectors against folkertvanheusden/PyMSX's z80 core.""" import sys, json, glob, os, logging SURVEY = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # this kit's root sys.path.insert(0, os.path.join(SURVEY, "folkertvanheusden_PyMSX")) logging.basicConfig(level=logging.CRITICAL) from z80 import z80 RAM = bytearray(65536) def rm(a): return RAM[a & 0xffff] def wm(a, v): RAM[a & 0xffff] = v & 0xff def rio(a): return 0xff def wio(a, v): pass class DummyScreen: def IE0(self): return False def interrupt(self): pass def start(self): pass cpu = z80(rm, wm, rio, wio, DummyScreen()) def load(st): cpu.a, cpu.f = 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.a_, cpu.f_ = st['af_'] >> 8, st['af_'] & 0xff cpu.b_, cpu.c_ = st['bc_'] >> 8, st['bc_'] & 0xff cpu.d_, cpu.e_ = st['de_'] >> 8, st['de_'] & 0xff cpu.h_, cpu.l_ = st['hl_'] >> 8, st['hl_'] & 0xff cpu.ix, cpu.iy = st['ix'], st['iy'] cpu.sp, cpu.pc = st['sp'], st['pc'] cpu.i, cpu.r = st['i'], st['r'] cpu.iff1, cpu.iff2 = st['iff1'], st['iff2'] cpu.im = st['im']; cpu.memptr = st['wz'] cpu.int = False; cpu.interrupt_cycles = 0 for a, v in st['ram']: RAM[a & 0xffff] = v def check(st): bad = [] for name, got, want in ( ('a',cpu.a,st['a']),('f',cpu.f,st['f']),('b',cpu.b,st['b']),('c',cpu.c,st['c']), ('d',cpu.d,st['d']),('e',cpu.e,st['e']),('h',cpu.h,st['h']),('l',cpu.l,st['l']), ('ix',cpu.ix,st['ix']),('iy',cpu.iy,st['iy']),('sp',cpu.sp,st['sp']),('pc',cpu.pc,st['pc']), ('i',cpu.i,st['i']), ("af'",(cpu.a_<<8)|cpu.f_,st['af_']),("bc'",(cpu.b_<<8)|cpu.c_,st['bc_']), ("de'",(cpu.d_<<8)|cpu.e_,st['de_']),("hl'",(cpu.h_<<8)|cpu.l_,st['hl_'])): if got != want: bad.append(name) for a, v in st['ram']: if RAM[a & 0xffff] != v: bad.append(f'ram[{a:04x}]'); break return bad files = sorted(glob.glob(os.path.join(SURVEY, "sst-z80/v1/*.json"))) if len(sys.argv) > 1: files = files[:int(sys.argv[1])] tot=ok=tsok=wzfail=rfail=0; failops={}; wzops={}; tsops={} for fn in files: op = os.path.basename(fn)[:-5] for case in json.load(open(fn)): RAM[:] = bytes(65536) load(case['initial']) try: cpu.step() except Exception: tot+=1; failops[op]=failops.get(op,0)+1; continue tot += 1 bad = check(case['final']) if not bad: ok += 1 else: failops[op] = failops.get(op,0)+1 if cpu.interrupt_cycles == len(case['cycles']): tsok += 1 else: tsops[op]=tsops.get(op,0)+1 if cpu.r != case['final']['r']: rfail += 1 if cpu.memptr != case['final']['wz']: wzfail += 1; wzops[op]=wzops.get(op,0)+1 print(f"R-register mismatches: {rfail} ({100*rfail/tot:.2f}%)") print(f"cases={tot} regs+ram_pass(excl R)={ok} ({100*ok/tot:.2f}%) tstate_pass={tsok} ({100*tsok/tot:.2f}%) wz_mismatch={wzfail}") print(f"opcodes_with_reg_failures={len(failops)} / {len(files)}") for op,n in sorted(failops.items(), key=lambda x:-x[1])[:15]: print(f" REG {op}: {n}") print("wz-failing opcodes:", len(wzops)) print("tstate-failing opcodes:", len(tsops))