zextests.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # z80jit - Z80 CPU emulator [with JIT compilation?], in rpython.
  2. # Copyright (C) 2017 deesix <deesix@tuta.io>
  3. # This program is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <http://www.gnu.org/licenses/>
  13. from os import write
  14. import z80
  15. class DummyIO():
  16. def wr(self, a, b): pass
  17. def rd(self, a): return 0x42
  18. def file_content_as_list_of_bytes(fname):
  19. bx = []
  20. for s in open(fname, 'rb').read():
  21. bx.append(ord(s) & 0xff)
  22. return bx
  23. def ram_64k_with_program(filename, addr=0):
  24. program = file_content_as_list_of_bytes(filename)
  25. mem = [0]*addr + program
  26. mem = mem + [0]*(65536 - len(mem))
  27. assert len(mem) == 0x10000
  28. return mem
  29. def patch_loader_and_handler(mem):
  30. mem[0]=0xC3
  31. mem[1]=0x00
  32. mem[2]=0x01
  33. mem[5]=0xC9
  34. def cpm_call9(cpu):
  35. mem = cpu.mem
  36. p = cpu._get_de()
  37. msg = []
  38. while mem[p] != ord('$'):
  39. msg.append(chr(mem[p]))
  40. p += 1
  41. write(1, "".join(msg))
  42. return 0
  43. def cpm_call2(cpu):
  44. write(1, chr(cpu.e))
  45. return 0
  46. def zextests(filename):
  47. mem = ram_64k_with_program(filename, 0x0100)
  48. cpu = z80.cpu(mem, DummyIO())
  49. patch_loader_and_handler(mem)
  50. pc = 0
  51. while True:
  52. pc = cpu._get_pc()
  53. if pc == 0x5:
  54. if cpu.c == 2: cpm_call2(cpu)
  55. if cpu.c == 9: cpm_call9(cpu)
  56. if cpu.c == 0: break
  57. try:
  58. cpu.execute()
  59. except z80.Error, e:
  60. cpu._set_pc(pc)
  61. print('\n%s at %s\n' % (e, pc))
  62. break
  63. if cpu._get_pc() == 0:
  64. print('')
  65. break
  66. def entry_point(args):
  67. zextests('zexdoc.com')
  68. #zextests('zexall.com')
  69. return 0
  70. def target(*args):
  71. return entry_point, None
  72. if __name__ == "__main__":
  73. entry_point(())