"""CP/M harness driving natdan/py-speccy's Z80CPU against zexdoc/zexall.com.""" import sys, 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 0 def out_port(self, p, v): pass class Bus(ClockAndBusAccess): def is_active_INT(self): return False rom = sys.argv[1] with open(rom, "rb") as fh: data = fh.read() RAM[0x100:0x100+len(data)] = data RAM[0x0000] = 0xC9 # RET at 0 (we trap before executing) RAM[0x0005] = 0xC9 # RET at BDOS entry bus = Bus(Mem(), Ports()) cpu = Z80CPU(bus) cpu.reset() cpu.regPC = 0x100 cpu.regSP = 0xF000 def bdos(c, e, de): if c == 2: sys.stdout.write(chr(e)); sys.stdout.flush() elif c == 9: i = de; out = [] while RAM[i & 0xffff] != ord('$'): out.append(chr(RAM[i & 0xffff])); i += 1 sys.stdout.write("".join(out)); sys.stdout.flush() n = 0 while True: pc = cpu.regPC if pc == 0x0005: bdos(cpu.regC, cpu.regE, cpu.get_reg_DE()) elif pc == 0x0000: print("\n[harness] jumped to 0x0000 -- exerciser finished") break cpu.execute_one_cycle() n += 1 if n % 200_000_000 == 0: print(f"\n[harness] {n/1e6:.0f}M instructions", flush=True)