"""Run SingleStepTests/z80 vectors against natdan/py-speccy's Z80CPU.""" import sys, json, glob, os SURVEY = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # this kit's root sys.path.insert(0, os.path.join(SURVEY, "natdan_py-speccy")) from z80.z80_cpu import Z80CPU from z80.bus_access import ClockAndBusAccess RAM = bytearray(65536) class Mem: def peekb(self, a): return RAM[a & 0xffff] def peeksb(self, a): v = RAM[a & 0xffff]; return v-256 if v > 127 else v def pokeb(self, a, v): RAM[a & 0xffff] = v & 0xff class Ports: def in_port(self, p): return 0xff def out_port(self, p, v): pass class Bus(ClockAndBusAccess): def is_active_INT(self): return False bus = Bus(Mem(), Ports()); cpu = Z80CPU(bus) def load(st): cpu.set_reg_AF((st['a'] << 8) | st['f']) cpu.set_reg_BC((st['b'] << 8) | st['c']) cpu.set_reg_DE((st['d'] << 8) | st['e']) cpu.set_reg_HL((st['h'] << 8) | st['l']) cpu.set_reg_AFx(st['af_']); cpu.set_reg_BCx(st['bc_']) cpu.set_reg_DEx(st['de_']); cpu.set_reg_HLx(st['hl_']) cpu.regIX = st['ix']; cpu.regIY = st['iy'] cpu.regSP = st['sp']; cpu.regPC = st['pc'] cpu.regI = st['i']; cpu.set_reg_R(st['r']) cpu.ffIFF1 = bool(st['iff1']); cpu.ffIFF2 = bool(st['iff2']) cpu.modeINT = st['im']; cpu.memptr = st['wz'] cpu._prefixOpcode = 0x00 cpu.halted = False cpu.pendingEI = False cpu._flagQ = False cpu._lastFlagQ = False for a, v in st['ram']: RAM[a & 0xffff] = v def check(st): bad = [] if cpu.get_reg_AF() != (st['a'] << 8) | st['f']: bad.append('af') if cpu.get_reg_BC() != (st['b'] << 8) | st['c']: bad.append('bc') if cpu.get_reg_DE() != (st['d'] << 8) | st['e']: bad.append('de') if cpu.get_reg_HL() != (st['h'] << 8) | st['l']: bad.append('hl') if cpu.get_reg_AFx() != st['af_']: bad.append("af'") if cpu.get_reg_BCx() != st['bc_']: bad.append("bc'") if cpu.get_reg_DEx() != st['de_']: bad.append("de'") if cpu.get_reg_HLx() != st['hl_']: bad.append("hl'") if cpu.regIX != st['ix']: bad.append('ix') if cpu.regIY != st['iy']: bad.append('iy') if cpu.regSP != st['sp']: bad.append('sp') if cpu.regPC != st['pc']: bad.append('pc') if cpu.regI != st['i']: bad.append('i') if cpu.get_reg_R() != st['r']: bad.append('r') 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 = 0; tsok = 0; failops = {}; wzfail = 0; 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']) bus.tstates = 0 try: cpu.execute_one_cycle() guard = 0 while cpu._prefixOpcode != 0x00 and guard < 4: cpu.execute_one_cycle(); guard += 1 except Exception: failops.setdefault(op, 0); failops[op] += 1; tot += 1; continue bad = check(case['final']) tot += 1 if not bad: ok += 1 else: failops[op] = failops.get(op, 0) + 1 if bus.tstates == len(case['cycles']): tsok += 1 else: tsops[op]=tsops.get(op,0)+1 if cpu.memptr != case['final']['wz']: wzfail += 1; wzops[op]=wzops.get(op,0)+1 print(f"cases={tot} regs+ram_pass={ok} ({100*ok/tot:.2f}%) tstate_pass={tsok} ({100*tsok/tot:.2f}%) wz_mismatch={wzfail}") print(f"opcodes_with_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)); for op,n in sorted(wzops.items(), key=lambda x:-x[1])[:15]: print(f" WZ {op}: {n}") print("tstate-failing opcodes:", len(tsops)) for op,n in sorted(tsops.items(), key=lambda x:-x[1])[:15]: print(f" TS {op}: {n}")