zextests.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. try:
  16. from rpython.rlib.jit import JitDriver
  17. except ImportError:
  18. # Mock for CPython compatibility.
  19. class JitDriver(object):
  20. def __init__(self,**kw): pass
  21. def jit_merge_point(self,**kw): pass
  22. def can_enter_jit(self,**kw): pass
  23. class DummyIO():
  24. def wr(self, a, b): pass
  25. def rd(self, a): return 0x42
  26. def file_content_as_list_of_bytes(fname):
  27. bx = []
  28. for s in open(fname, 'rb').read():
  29. bx.append(ord(s) & 0xff)
  30. return bx
  31. def ram_64k_with_program(filename, addr=0):
  32. program = file_content_as_list_of_bytes(filename)
  33. mem = [0]*addr + program
  34. mem = mem + [0]*(65536 - len(mem))
  35. assert len(mem) == 0x10000
  36. return mem
  37. def patch_loader_and_handler(mem):
  38. mem[0]=0xC3
  39. mem[1]=0x00
  40. mem[2]=0x01
  41. mem[5]=0xC9
  42. def cpm_call9(cpu):
  43. mem = cpu.mem
  44. p = cpu._get_de()
  45. msg = []
  46. while mem[p] != ord('$'):
  47. msg.append(chr(mem[p]))
  48. p += 1
  49. write(1, "".join(msg))
  50. return 0
  51. def cpm_call2(cpu):
  52. write(1, chr(cpu.e))
  53. return 0
  54. jitdriver = JitDriver(greens=['pc'], reds=['mem', 'cpu'])
  55. def zextests(filename):
  56. mem = ram_64k_with_program(filename, 0x0100)
  57. cpu = z80.cpu(mem, DummyIO())
  58. patch_loader_and_handler(mem)
  59. pc = 0
  60. while True:
  61. pc = cpu._get_pc()
  62. if pc == 0x5:
  63. if cpu.c == 2: cpm_call2(cpu)
  64. if cpu.c == 9: cpm_call9(cpu)
  65. if cpu.c == 0: break
  66. jitdriver.jit_merge_point(pc=pc, mem=mem, cpu=cpu)#, filename=filename)
  67. try:
  68. cpu.execute()
  69. except z80.Error, e:
  70. cpu._set_pc(pc)
  71. print('\n%s at %s\n' % (e, pc))
  72. break
  73. if cpu._get_pc() == 0:
  74. print('')
  75. break
  76. if cpu._get_pc() < pc:
  77. jitdriver.can_enter_jit(pc=cpu._get_pc(), mem=mem, cpu=cpu)
  78. def entry_point(args):
  79. zextests('zexdoc.com')
  80. #zextests('zexall.com')
  81. return 0
  82. def target(*args):
  83. return entry_point, None
  84. if __name__ == "__main__":
  85. entry_point(())