z80gen.py 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  1. # z80jit - Z80 CPU emulator [with JIT compilation?], in rpython.
  2. # Copyright (C) 2014-2017 Jason Harris <jth@mibot.com>
  3. # Copyright (C) 2017 deesix <deesix@tuta.io>
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <http://www.gnu.org/licenses/>
  14. #-----------------------------------------------------------------------------
  15. """
  16. Z80 Opcode Emulation Generator
  17. """
  18. #-----------------------------------------------------------------------------
  19. import sys
  20. import getopt
  21. import z80da
  22. import memory
  23. #-----------------------------------------------------------------------------
  24. # format is (opcode prefix), (links to other prefixes), 'function preamble'
  25. _prefixes = (
  26. ((), (0xcb, 0xdd, 0xed, 0xfd), '(self):'),
  27. ((0xcb,), (), '(self):'),
  28. ((0xdd,), (0xcb, 0xdd, 0xfd), '(self):'),
  29. ((0xdd, 0xcb, 0x00), (), '(self, d):'),
  30. ((0xed,), (), '(self):'),
  31. ((0xfd,), (0xcb, 0xdd, 0xfd), '(self):'),
  32. ((0xfd, 0xcb, 0x00), (), '(self, d):'),
  33. )
  34. #-----------------------------------------------------------------------------
  35. _indent = 4
  36. class output:
  37. """class for handling file output with auto indenting"""
  38. def __init__(self, ofname):
  39. self.ofile = file('%s' % ofname, 'w')
  40. self.lhs = 0
  41. self.col = 0
  42. def close(self):
  43. self.ofile.close()
  44. def put(self, s):
  45. for c in s:
  46. if c == '\n':
  47. self.ofile.write('\n')
  48. self.col = 0
  49. else:
  50. if self.col == 0:
  51. self.ofile.write(' ' * self.lhs)
  52. self.col = self.lhs
  53. self.ofile.write(c)
  54. self.col += 1
  55. def pad(self, col):
  56. if self.col < col:
  57. self.ofile.write(' ' * (col - self.col))
  58. self.col = col
  59. def indent(self, n):
  60. self.lhs += n * _indent
  61. def outdent(self, n):
  62. self.lhs -= n * _indent
  63. #-----------------------------------------------------------------------------
  64. _r = ('b', 'c', 'd', 'e', 'h', 'l', '(hl)', 'a')
  65. _rp = ('bc', 'de', 'hl', 'sp')
  66. _rp2 = ('bc', 'de', 'hl', 'af')
  67. _direct_rp = ('sp', 'ix', 'iy')
  68. _cc = ('nz', 'z', 'nc', 'c', 'po', 'pe', 'p', 'm')
  69. _alu = ('add', 'adc', 'sub', 'sbc', 'and', 'xor', 'or', 'cp')
  70. _alux = ('a,', 'a,', '', 'a,', '', '', '', '')
  71. _rot = ('rlc', 'rrc', 'rl', 'rr', 'sla', 'sra', 'sll', 'srl')
  72. _rota = ('rlca', 'rrca', 'rla', 'rra', 'daa', 'cpl', 'scf', 'ccf')
  73. _im = ('0', '0', '1', '2', '0', '0', '1', '2')
  74. _bli = (
  75. ('ldi', 'ldd', 'ldir', 'lddr'), ('cpi', 'cpd', 'cpir', 'cpdr'),
  76. ('ini', 'ind', 'inir', 'indr'), ('outi', 'outd', 'otir', 'otdr')
  77. )
  78. #-----------------------------------------------------------------------------
  79. # 8-Bit Load Group
  80. def emit_ld_r_n(out, r):
  81. """load immediate register n"""
  82. if r == '(hl)':
  83. out.put('self.mem[self._get_hl()] = self._get_n()\n')
  84. out.put('return 10\n')
  85. else:
  86. out.put('self.%s = self._get_n()\n' % r)
  87. out.put('return 7\n')
  88. def emit_ld_r_n_alt1(out, r):
  89. assert r in ['ixh', 'ixl', 'iyh', 'iyl']
  90. pair = r[:2]
  91. if r in ['ixh', 'iyh']:
  92. out.put('self.%s = (self._get_n() << 8) + (self.%s & 0xff)\n' % (pair, pair))
  93. else:
  94. out.put('self.%s = (self.%s & 0xff00) + self._get_n()\n' % (pair, pair))
  95. out.put('return 7\n')
  96. def emit_ld_mem_xx_n(out, r):
  97. """ld (xx),n where xx is ix+d, iy+d"""
  98. out.put('d = _signed(self._get_n())\n')
  99. out.put('self.mem[self.%s + d] = self._get_n()\n' % r)
  100. out.put('return 15\n')
  101. def emit_ld_r_r(out, rd, rs):
  102. """load register to register"""
  103. if rd == '(hl)':
  104. out.put('self.mem[self._get_hl()] = self.%s\n' % rs)
  105. out.put('return 7\n')
  106. elif rd == '(ix+d)':
  107. out.put('d = _signed(self._get_n())\n')
  108. out.put('self.mem[self.ix + d] = self.%s\n' % rs)
  109. out.put('return 15\n')
  110. elif rd == '(iy+d)':
  111. out.put('d = _signed(self._get_n())\n')
  112. out.put('self.mem[self.iy + d] = self.%s\n' % rs)
  113. out.put('return 15\n')
  114. elif rs == '(hl)':
  115. out.put('self.%s = self.mem[self._get_hl()]\n' % rd)
  116. out.put('return 7\n')
  117. elif rs == '(ix+d)':
  118. out.put('d = _signed(self._get_n())\n')
  119. out.put('self.%s = self.mem[self.ix + d]\n' % rd)
  120. out.put('return 15\n')
  121. elif rs == '(iy+d)':
  122. out.put('d = _signed(self._get_n())\n')
  123. out.put('self.%s = self.mem[self.iy + d]\n' % rd)
  124. out.put('return 15\n')
  125. else:
  126. out.put('self.%s = self.%s\n' % (rd, rs))
  127. out.put('return 4\n')
  128. def emit_ld_r_r_alt1(out, rd, rs):
  129. ihigh = ('ixh', 'iyh')
  130. ilow = ('ixl', 'iyl')
  131. if rs in ihigh:
  132. source = '((self.i%s >> 8) & 0xff)' % rs[1]
  133. elif rs in ilow:
  134. source = '(self.i%s & 0xff)' % rs[1]
  135. else:
  136. source = 'self.%s' % rs
  137. if rd in ihigh:
  138. source = '(%s << 8)' % source
  139. if rd in ihigh:
  140. dest = 'self.i%s = (self.i%s & 0xff) +' % (rd[1], rd[1])
  141. elif rd in ilow:
  142. dest = 'self.i%s = (self.i%s & 0xff00) +' % (rd[1], rd[1])
  143. else:
  144. dest = 'self.%s =' % rd
  145. out.put('%s %s\n' % (dest, source))
  146. out.put('return 4\n')
  147. def emit_ld_a_mem_xx(out, xx):
  148. """ld a,(xx) where xx in (bc,de,nn)"""
  149. out.put('self.a = self.mem[self._get_%s()] & 0xff\n' % xx)
  150. out.put('return %d\n' % (7,13)[xx == 'nn'])
  151. def emit_ld_mem_xx_a(out, xx):
  152. """ld (xx),a - where xx in (bc,de,nn)"""
  153. out.put('self.mem[self._get_%s()] = self.a\n' % xx)
  154. out.put('return %d\n' % (7,13)[xx == 'nn'])
  155. def emit_ld_ira(out, d, s):
  156. """ld i/r/a, i/r/a"""
  157. out.put('self.%s = self.%s\n' % (d,s))
  158. if d == 'a':
  159. out.put('self.f = (self.f & _CF) | (self.f_sz[self.a]) | (self.iff2 << 2)\n')
  160. out.put('return 9\n')
  161. #-----------------------------------------------------------------------------
  162. # 16-Bit Load Group
  163. def emit_ld_rp_nn(out, rp):
  164. """ld rp,nn"""
  165. if rp in _direct_rp:
  166. out.put('self.%s = self._get_nn()\n' % rp)
  167. else:
  168. out.put('self._set_%s(self._get_nn())\n' % rp)
  169. out.put('return 10\n')
  170. def emit_ld_mem_nn_rp(out, rp):
  171. """ld (nn), rp"""
  172. out.put('nn = self._get_nn()\n')
  173. if rp in _direct_rp:
  174. out.put('self.mem[nn] = self.%s & 0xff\n' % rp)
  175. out.put('self.mem[nn + 1] = self.%s >> 8\n' % rp)
  176. else:
  177. out.put('self.mem[nn] = self.%s\n' % rp[1])
  178. out.put('self.mem[nn + 1] = self.%s\n' % rp[0])
  179. out.put('return 16\n')
  180. def emit_ld_rp_mem_nn(out, rp):
  181. """ld rp,(nn)"""
  182. out.put('nn = self._get_nn()\n')
  183. if rp in _direct_rp:
  184. out.put('self.%s = self.mem[nn + 1] << 8\n' % rp)
  185. out.put('self.%s |= self.mem[nn]\n' % rp)
  186. else:
  187. out.put('self.%s = self.mem[nn + 1]\n' % rp[0])
  188. out.put('self.%s = self.mem[nn]\n' % rp[1])
  189. out.put('return 16\n')
  190. def emit_ld_sp_hl(out):
  191. """ld sp, hl"""
  192. out.put('self.sp = self._get_hl()\n')
  193. out.put('return 6\n')
  194. def emit_pop_rp(out, rp):
  195. """pop rp"""
  196. if rp in _direct_rp:
  197. out.put('self.%s = self._pop()\n' % rp)
  198. else:
  199. out.put('self.%s = self.mem[self.sp + 1]\n' % rp[0])
  200. out.put('self.%s = self.mem[self.sp]\n' % rp[1])
  201. out.put('self.sp = (self.sp + 2) & 0xffff\n')
  202. out.put('return 10\n')
  203. def emit_push_rp(out, rp):
  204. """pop rp"""
  205. if rp in _direct_rp:
  206. out.put('self._push(self.%s)\n' % rp)
  207. else:
  208. out.put('self.mem[self.sp - 1] = self.%s\n' % rp[0])
  209. out.put('self.mem[self.sp - 2] = self.%s\n' % rp[1])
  210. out.put('self.sp = (self.sp - 2) & 0xffff\n')
  211. out.put('return 11\n')
  212. #-----------------------------------------------------------------------------
  213. # Exchange, Block Transfer, and Search Group
  214. def emit_ex_de_hl(out):
  215. """ ex de,hl"""
  216. out.put('self.d, self.h = self.h, self.d\n')
  217. out.put('self.e, self.l = self.l, self.e\n')
  218. out.put('return 6\n')
  219. def emit_ex_af_af(out):
  220. """ ex af,af'"""
  221. out.put('tmp = self._get_af()\n')
  222. out.put('self._set_af(self.alt_af)\n')
  223. out.put('self.alt_af = tmp\n')
  224. out.put('return 4\n')
  225. def emit_ldxx(out, op):
  226. """ldi, ldir, ldd, lddr"""
  227. dirn = ('-', '+')[op in ('ldi', 'ldir')]
  228. out.put('d = self._get_de()\n')
  229. out.put('s = self._get_hl()\n')
  230. out.put('n = self._get_bc() - 1\n')
  231. out.put('val = self.mem[s]\n')
  232. out.put('self.mem[d] = val\n')
  233. out.put('self.f &= (_SF | _ZF | _CF)\n')
  234. out.put('if (self.a + val) & 0x02:\n')
  235. out.put(' self.f |= _YF\n')
  236. out.put('if (self.a + val) & 0x08:\n')
  237. out.put(' self.f |= _XF\n')
  238. out.put('self._set_de(d %s 1)\n' % dirn)
  239. out.put('self._set_hl(s %s 1)\n' % dirn)
  240. out.put('self._set_bc(n)\n')
  241. out.put('if n:\n')
  242. out.put(' self.f |= _VF\n')
  243. if op in ('ldir', 'lddr'):
  244. out.put(' self._dec_pc(2)\n')
  245. out.put(' return 17\n')
  246. out.put('return 12\n')
  247. def emit_cpxx(out, op):
  248. """cpi, cpd, cpir, cpdr"""
  249. dirn = ('-', '+')[op in ('cpi', 'cpir')]
  250. out.put('s = self._get_hl()\n')
  251. out.put('n = self._get_bc() - 1\n')
  252. out.put('val = self.mem[s]\n')
  253. out.put('res = self.a - val\n')
  254. out.put('self.f = (self.f & _CF) | _NF\n')
  255. out.put('self.f |= (self.f_sz[res] & ~(_YF | _XF))\n')
  256. out.put('self.f |= ((self.a ^ val ^ res) & _HF)\n')
  257. out.put('if self.f & _HF:\n')
  258. out.put(' res -= 1\n')
  259. out.put('if res & 0x02:\n')
  260. out.put(' self.f |= _YF\n')
  261. out.put('if res & 0x08:\n')
  262. out.put(' self.f |= _XF\n')
  263. out.put('self._set_hl(s %s 1)\n' % dirn)
  264. out.put('self._set_bc(n)\n')
  265. out.put('if n:\n')
  266. out.put(' self.f |= _VF\n')
  267. if op in ('cpir', 'cpdr'):
  268. out.put('if n and (self.f & _ZF == 0):\n')
  269. out.put(' self._dec_pc(2)\n')
  270. out.put(' return 17\n')
  271. out.put('return 12\n')
  272. def emit_bli(out, op):
  273. """block instructions"""
  274. if op in ('ldi', 'ldir', 'ldd', 'lddr'):
  275. return emit_ldxx(out, op)
  276. if op in ('cpi', 'cpir', 'cpd', 'cpdr'):
  277. return emit_cpxx(out, op)
  278. if op == 'ini':
  279. out.put('assert False, \'unimplemented instruction\'\n')
  280. elif op == 'ind':
  281. out.put('assert False, \'unimplemented instruction\'\n')
  282. elif op == 'inir':
  283. out.put('assert False, \'unimplemented instruction\'\n')
  284. elif op == 'indr':
  285. out.put('assert False, \'unimplemented instruction\'\n')
  286. elif op == 'outi':
  287. out.put('assert False, \'unimplemented instruction\'\n')
  288. elif op == 'outd':
  289. out.put('assert False, \'unimplemented instruction\'\n')
  290. elif op == 'otir':
  291. out.put('assert False, \'unimplemented instruction\'\n')
  292. elif op == 'otdr':
  293. out.put('assert False, \'unimplemented instruction\'\n')
  294. else:
  295. assert False
  296. def emit_ex_mem_sp_r(out, r):
  297. """ex (sp),r"""
  298. out.put('tmp = self._peek(self.sp)\n')
  299. if r == 'hl':
  300. out.put('self._poke(self.sp, self._get_hl())\n')
  301. out.put('self._set_hl(tmp)\n')
  302. else:
  303. out.put('self._poke(self.sp, self.%s)\n' % r)
  304. out.put('self.%s = tmp\n' % r)
  305. out.put('return 19\n')
  306. def emit_exx(out):
  307. """exx"""
  308. out.put('tmp = self._get_bc()\n')
  309. out.put('self._set_bc(self.alt_bc)\n')
  310. out.put('self.alt_bc = tmp\n')
  311. out.put('tmp = self._get_de()\n')
  312. out.put('self._set_de(self.alt_de)\n')
  313. out.put('self.alt_de = tmp\n')
  314. out.put('tmp = self._get_hl()\n')
  315. out.put('self._set_hl(self.alt_hl)\n')
  316. out.put('self.alt_hl = tmp\n')
  317. out.put('return 4\n')
  318. #-----------------------------------------------------------------------------
  319. # 8-Bit Arithmetic Group
  320. def emit_inc_dec_r(out, r, op ):
  321. """inc/dec register"""
  322. delta = ('+ 1','- 1')[op == 'dec']
  323. flags = ('self.f_szhv_inc', 'self.f_szhv_dec')[op == 'dec']
  324. if r == '(hl)':
  325. out.put('hl = self._get_hl()\n')
  326. out.put('n = (self.mem[hl] %s) & 0xff\n' % delta)
  327. out.put('self.mem[hl] = n\n')
  328. out.put('self.f = (self.f & _CF) | %s[n]\n' % flags)
  329. out.put('return 11\n')
  330. elif r == '(ix+d)':
  331. out.put('adr = self.ix + _signed(self._get_n())\n')
  332. out.put('n = (self.mem[adr] %s) & 0xff\n' % delta)
  333. out.put('self.mem[adr] = n\n')
  334. out.put('self.f = (self.f & _CF) | %s[n]\n' % flags)
  335. out.put('return 19\n')
  336. elif r == '(iy+d)':
  337. out.put('adr = self.iy + _signed(self._get_n())\n')
  338. out.put('n = (self.mem[adr] %s) & 0xff\n' % delta)
  339. out.put('self.mem[adr] = n\n')
  340. out.put('self.f = (self.f & _CF) | %s[n]\n' % flags)
  341. out.put('return 19\n')
  342. else:
  343. out.put('n = (self.%s %s) & 0xff\n' % (r, delta))
  344. out.put('self.%s = n\n' % r)
  345. out.put('self.f = (self.f & _CF) | %s[n]\n' % flags)
  346. out.put('return 4\n')
  347. def emit_inc_dec_r_alt1(out, r, op):
  348. delta = ('+ 1','- 1')[op == 'dec']
  349. flags = ('self.f_szhv_inc', 'self.f_szhv_dec')[op == 'dec']
  350. assert r in ['ixh', 'ixl', 'iyh', 'iyl']
  351. pair = r[:2]
  352. if r[2] == 'h':
  353. out.put('n = ((self.%s >> 8) %s) & 0xff\n' % (pair, delta))
  354. out.put('self.%s = (n << 8) + (self.%s & 0xff)\n' % (pair, pair))
  355. else:
  356. out.put('n = ((self.%s & 0xff) %s) & 0xff\n' % (pair, delta))
  357. out.put('self.%s = (self.%s & 0xff00) + n\n' % (pair, pair))
  358. out.put('self.f = (self.f & _CF) | %s[n]\n' % flags)
  359. out.put('return 4\n')
  360. def emit_alu_r(out, op, r):
  361. """alu operation with register"""
  362. if r == '(ix+d)':
  363. out.put('val = self.mem[self.ix + _signed(self._get_n())]\n')
  364. tclks = 15
  365. elif r == '(iy+d)':
  366. out.put('val = self.mem[self.iy + _signed(self._get_n())]\n')
  367. tclks = 15
  368. elif r == '(hl)':
  369. out.put('val = self.mem[self._get_hl()]\n')
  370. tclks = 7
  371. else:
  372. out.put('val = self.%s\n' % r)
  373. tclks = 4
  374. if op == 'add':
  375. out.put('result = self.a + val\n')
  376. out.put('self._add_flags(result, val)\n')
  377. out.put('self.a = result & 0xff\n')
  378. out.put('return %d\n' % tclks)
  379. elif op == 'adc':
  380. out.put('result = self.a + val + (self.f & _CF)\n')
  381. out.put('self._add_flags(result, val)\n')
  382. out.put('self.a = result & 0xff\n')
  383. out.put('return %d\n' % tclks)
  384. elif op == 'sub':
  385. out.put('result = self.a - val\n')
  386. out.put('self._sub_flags(result, val)\n')
  387. out.put('self.a = result & 0xff\n')
  388. out.put('return %d\n' % tclks)
  389. elif op == 'sbc':
  390. out.put('result = self.a - val - (self.f & _CF)\n')
  391. out.put('self._sub_flags(result, val)\n')
  392. out.put('self.a = result & 0xff\n')
  393. out.put('return %d\n' % tclks)
  394. elif op == 'and':
  395. out.put('self.a &= val\n')
  396. out.put('self.f = self.f_szp[self.a] | _HF\n')
  397. out.put('return %d\n' % tclks)
  398. elif op == 'xor':
  399. out.put('self.a ^= val\n')
  400. out.put('self.f = self.f_szp[self.a]\n')
  401. out.put('return %d\n' % tclks)
  402. elif op == 'or':
  403. out.put('self.a |= val\n')
  404. out.put('self.f = self.f_szp[self.a]\n')
  405. out.put('return %d\n' % tclks)
  406. elif op == 'cp':
  407. out.put('result = self.a - val\n')
  408. out.put('self._sub_flags(result, val)\n')
  409. out.put('return %d\n' % tclks)
  410. else:
  411. assert False
  412. def emit_alu_r_alt1(out, op, r):
  413. pyop = ''
  414. update_flags = ''
  415. maybe_carry = ''
  416. if op in ('add', 'adc'):
  417. pyop = '+'
  418. update_flags = 'self._add_flags(result, val)'
  419. if op == 'adc':
  420. maybe_carry = ' + (self.f & _CF)'
  421. if op in ('sub', 'cp', 'sbc'):
  422. pyop = '-'
  423. update_flags = 'self._sub_flags(result, val)'
  424. if op == 'sbc':
  425. maybe_carry = ' - (self.f & _CF)'
  426. if op == 'and':
  427. pyop = '&'
  428. update_flags = 'self.f = self.f_szp[result] | _HF'
  429. elif op == 'or':
  430. pyop = '|'
  431. update_flags = 'self.f = self.f_szp[result]'
  432. elif op == 'xor':
  433. pyop = '^'
  434. update_flags = 'self.f = self.f_szp[result]'
  435. if r in ('ixh', 'iyh'):
  436. r = '((self.i%s >> 8) & 0xff)' % r[1]
  437. elif r in ('ixl', 'iyl'):
  438. r = '(self.i%s & 0xff)' % r[1]
  439. out.put('val = %s\n' % (r,))
  440. out.put('result = self.a %s val%s\n' % (pyop, maybe_carry))
  441. if update_flags:
  442. out.put(update_flags + '\n')
  443. if op != 'cp':
  444. out.put('self.a = result & 0xff\n')
  445. out.put('return 4\n')
  446. def emit_alu_n(out, op):
  447. """alu operation with immediate"""
  448. out.put('val = self._get_n()\n')
  449. if op == 'add':
  450. out.put('result = self.a + val\n')
  451. out.put('self._add_flags(result, val)\n')
  452. out.put('self.a = result & 0xff\n')
  453. elif op == 'adc':
  454. out.put('result = self.a + val + (self.f & _CF)\n')
  455. out.put('self._add_flags(result, val)\n')
  456. out.put('self.a = result & 0xff\n')
  457. elif op == 'sub':
  458. out.put('result = self.a - val\n')
  459. out.put('self._sub_flags(result, val)\n')
  460. out.put('self.a = result & 0xff\n')
  461. elif op == 'sbc':
  462. out.put('result = self.a - val - (self.f & _CF)\n')
  463. out.put('self._sub_flags(result, val)\n')
  464. out.put('self.a = result & 0xff\n')
  465. elif op == 'and':
  466. out.put('self.a &= val\n')
  467. out.put('self.f = self.f_szp[self.a] | _HF\n')
  468. elif op == 'xor':
  469. out.put('self.a ^= val\n')
  470. out.put('self.f = self.f_szp[self.a]\n')
  471. elif op == 'or':
  472. out.put('self.a |= val\n')
  473. out.put('self.f = self.f_szp[self.a]\n')
  474. elif op == 'cp':
  475. out.put('result = self.a - val\n')
  476. out.put('self._sub_flags(result, val)\n')
  477. else:
  478. assert False
  479. out.put('return 7\n')
  480. #-----------------------------------------------------------------------------
  481. # General-Purpose Arithmetic and CPU Control Groups
  482. def emit_nop(out):
  483. """nop"""
  484. out.put('return 4\n')
  485. def emit_di(out):
  486. """disable interrupts"""
  487. out.put('self.iff1 = 0\n')
  488. out.put('self.iff2 = 0\n')
  489. out.put('return 4\n')
  490. def emit_ei(out):
  491. """enable interrupts"""
  492. out.put('self.iff1 = 1\n')
  493. out.put('self.iff2 = 1\n')
  494. out.put('return 4\n')
  495. def emit_im(out, n):
  496. """im n"""
  497. out.put('self.im = %s\n' % n)
  498. out.put('return 4\n')
  499. def emit_halt(out):
  500. """halt"""
  501. out.put('self._enter_halt()\n')
  502. out.put('return 4\n')
  503. def emit_daa(out):
  504. """daa"""
  505. out.put('cf = bool(self.f & _CF)\n')
  506. out.put('nf = bool(self.f & _NF)\n')
  507. out.put('hf = bool(self.f & _HF)\n')
  508. out.put('lo = self.a & 0x0f\n')
  509. out.put('hi = self.a >> 4\n')
  510. out.put('if cf:\n')
  511. out.put(' diff = [0x66, 0x60][(lo <= 9) and (not hf)]\n')
  512. out.put('else:\n')
  513. out.put(' if lo >= 10:\n')
  514. out.put(' diff = [0x66, 0x06][hi <= 8]\n')
  515. out.put(' else:\n')
  516. out.put(' if hi >= 10:\n')
  517. out.put(' diff = [0x60, 0x66][hf]\n')
  518. out.put(' else:\n')
  519. out.put(' diff = [0x00, 0x06][hf]\n')
  520. out.put('if nf:\n')
  521. out.put(' self.a = (self.a - diff) & 0xff\n')
  522. out.put('else:\n')
  523. out.put(' self.a = (self.a + diff) & 0xff\n')
  524. out.put('self.f = self.f_szp[self.a] | (self.f & _NF)\n')
  525. out.put('if cf:\n')
  526. out.put(' self.f |= _CF\n')
  527. out.put('if (lo <= 9) and (hi >= 10):\n')
  528. out.put(' self.f |= _CF\n')
  529. out.put('if (lo > 9) and (hi >= 9):\n')
  530. out.put(' self.f |= _CF\n')
  531. out.put('if nf and hf and (lo <= 5):\n')
  532. out.put(' self.f |= _HF\n')
  533. out.put('if (not nf) and (lo >= 10):\n')
  534. out.put(' self.f |= _HF\n')
  535. out.put('return 4\n')
  536. def emit_neg(out):
  537. """neg"""
  538. out.put('result = -self.a\n')
  539. out.put('self._neg_flags(result, self.a)\n')
  540. out.put('self.a = result & 0xff\n')
  541. out.put('return 4\n')
  542. #-----------------------------------------------------------------------------
  543. # 16-Bit Arithmetic Group
  544. def emit_op_rp_rp(out, op, d, s):
  545. """add/adc/sub hl/ix/iy,rp"""
  546. if s in _direct_rp:
  547. out.put('s = self.%s\n' % s)
  548. else:
  549. out.put('s = self._get_%s()\n' % s)
  550. if d in _direct_rp:
  551. out.put('d = self.%s\n' % d)
  552. else:
  553. out.put('d = self._get_%s()\n' % d)
  554. if op == 'add':
  555. out.put('res = d + s\n')
  556. out.put('self._add16_flags(res, d, s)\n')
  557. elif op == 'sbc':
  558. out.put('res = d - s - (self.f & _CF)\n')
  559. out.put('self._sub16_flags(res, d, s)\n')
  560. elif op == 'adc':
  561. out.put('res = d + s + (self.f & _CF)\n')
  562. out.put('self._adc16_flags(res, d, s)\n')
  563. if d in _direct_rp:
  564. out.put('self.%s = res & 0xffff\n' % d)
  565. else:
  566. out.put('self._set_%s(res & 0xffff)\n' % d)
  567. out.put('return 11\n')
  568. def emit_dec_rp(out, rp):
  569. """dec ss"""
  570. if rp in _direct_rp:
  571. out.put('self.%s = (self.%s - 1) & 0xffff\n' % (rp, rp))
  572. else:
  573. out.put('self._set_%s(self._get_%s() - 1)\n' % (rp, rp))
  574. out.put('return 6\n')
  575. def emit_inc_rp(out, rp):
  576. """inc ss"""
  577. if rp in _direct_rp:
  578. out.put('self.%s = (self.%s + 1) & 0xffff\n' % (rp, rp))
  579. else:
  580. out.put('self._set_%s(self._get_%s() + 1)\n' % (rp, rp))
  581. out.put('return 6\n')
  582. #-----------------------------------------------------------------------------
  583. # Rotate and Shift Group
  584. def emit_rota(out, op):
  585. """rotate a"""
  586. if op == 'rlca':
  587. out.put('self.a = ((self.a << 1) | (self.a >> 7)) & 0xff\n')
  588. out.put('self.f = (self.f & (_SF | _ZF | _PF)) | (self.a & (_YF | _XF | _CF))\n')
  589. elif op == 'rrca':
  590. out.put('self.f = (self.f & (_SF | _ZF | _PF)) | (self.a & _CF)\n')
  591. out.put('self.a = ((self.a >> 1) | (self.a << 7)) & 0xff\n')
  592. out.put('self.f |= (self.a & (_YF | _XF))\n')
  593. elif op == 'rla':
  594. out.put('res = (self.a << 1) | (self.f & _CF)\n')
  595. out.put('c = [0, _CF][(self.a & 0x80) != 0]\n')
  596. out.put('self.f = (self.f & (_SF | _ZF | _PF)) | c | (res & (_YF | _XF))\n')
  597. out.put('self.a = res & 0xff\n')
  598. elif op == 'rra':
  599. out.put('res = (self.a >> 1) | (self.f << 7)\n')
  600. out.put('c = [0, _CF][(self.a & 0x01) != 0]\n')
  601. out.put('self.f = (self.f & (_SF | _ZF | _PF)) | c | (res & (_YF | _XF))\n')
  602. out.put('self.a = res & 0xff\n')
  603. elif op == 'daa':
  604. return emit_daa(out)
  605. elif op == 'cpl':
  606. out.put('self.a ^= 0xff\n')
  607. out.put('self.f = (self.f & (_SF | _ZF | _PF | _CF)) | _HF | _NF | (self.a & (_YF | _XF))\n')
  608. elif op == 'scf':
  609. out.put('self.f = (self.f & (_SF | _ZF | _PF)) | _CF | (self.a & (_YF | _XF))\n')
  610. elif op == 'ccf':
  611. out.put('self.f = ((self.f & (_SF | _ZF | _PF | _CF)) | ((self.f & _CF) << 4) | (self.a & (_YF | _XF))) ^ _CF\n')
  612. else:
  613. assert False
  614. out.put('return 4\n')
  615. def emit_rot_r_x(out, op, r, x):
  616. """rotate operation on r - optionally store in x also"""
  617. if r == '(ix+d)':
  618. out.put('res = self.mem[self.ix + d]\n')
  619. elif r == '(iy+d)':
  620. out.put('res = self.mem[self.iy + d]\n')
  621. elif r == '(hl)':
  622. out.put('res = self.mem[self._get_hl()]\n')
  623. else:
  624. out.put('res = self.%s\n' % r)
  625. if op == 'rlc':
  626. out.put('cf = [0, _CF][(res & 0x80) != 0]\n')
  627. out.put('res = ((res << 1) | (res >> 7)) & 0xff\n')
  628. elif op == 'rrc':
  629. out.put('cf = [0, _CF][(res & 0x01) != 0]\n')
  630. out.put('res = ((res >> 1) | (res << 7)) & 0xff\n')
  631. elif op == 'rl':
  632. out.put('cf = [0, _CF][(res & 0x80) != 0]\n')
  633. out.put('res = ((res << 1) | (self.f & _CF)) & 0xff\n')
  634. elif op == 'rr':
  635. out.put('cf = [0, _CF][(res & 0x01) != 0]\n')
  636. out.put('res = ((res >> 1) | (self.f << 7)) & 0xff\n')
  637. elif op == 'sla':
  638. out.put('cf = [0, _CF][(res & 0x80) != 0]\n')
  639. out.put('res = (res << 1) & 0xff\n')
  640. elif op == 'sra':
  641. out.put('cf = [0, _CF][(res & 0x01) != 0]\n')
  642. out.put('res = ((res >> 1) | (res & 0x80)) & 0xff\n')
  643. elif op == 'sll':
  644. out.put('cf = [0, _CF][(res & 0x80) != 0]\n')
  645. out.put('res = ((res << 1) | 0x01) & 0xff\n')
  646. elif op == 'srl':
  647. out.put('cf = [0, _CF][(res & 0x01) != 0]\n')
  648. out.put('res = (res >> 1) & 0xff\n')
  649. else:
  650. assert False
  651. out.put('self.f = self.f_szp[res] | cf\n')
  652. if x != '':
  653. out.put('self.%s = res\n' % x)
  654. if r == '(ix+d)':
  655. out.put('self.mem[self.ix + d] = res\n')
  656. out.put('return 11\n')
  657. elif r == '(iy+d)':
  658. out.put('self.mem[self.iy + d] = res\n')
  659. out.put('return 11\n')
  660. elif r == '(hl)':
  661. out.put('self.mem[self._get_hl()] = res\n')
  662. out.put('return 11\n')
  663. else:
  664. out.put('self.%s = res\n' % r)
  665. out.put('return 4\n')
  666. def emit_rxd(out, op):
  667. """rld, rrd"""
  668. out.put('adr = self._get_hl()\n')
  669. out.put('n = self.mem[adr]\n')
  670. if op == 'rrd':
  671. out.put('self.mem[adr] = ((n >> 4) | (self.a << 4)) & 0xff\n')
  672. out.put('self.a = (self.a & 0xf0) | (n & 0x0f)\n')
  673. elif op == 'rld':
  674. out.put('self.mem[adr] = ((n << 4) | (self.a & 0x0f)) & 0xff\n')
  675. out.put('self.a = (self.a & 0xf0) | (n >> 4)\n')
  676. else:
  677. assert False
  678. out.put('self.f = (self.f & _CF) | self.f_szp[self.a]\n')
  679. out.put('return 14\n')
  680. #-----------------------------------------------------------------------------
  681. # Bit Set, Reset, and Test Group
  682. def emit_bit_b_r(out, b, r):
  683. """bit test operation on r"""
  684. if r == '(ix+d)':
  685. out.put('bit = self.mem[self.ix + d] & (1 << %d)\n' % b)
  686. t = 8
  687. elif r == '(iy+d)':
  688. out.put('bit = self.mem[self.iy + d] & (1 << %d)\n' % b)
  689. t = 8
  690. elif r == '(hl)':
  691. out.put('bit = self.mem[self._get_hl()] & (1 << %d)\n' % b)
  692. t = 8
  693. else:
  694. out.put('bit = self.%s & (1 << %d)\n' % (r, b))
  695. t = 4
  696. out.put('zf = [0, _ZF][bit == 0]\n')
  697. out.put('self.f = (self.f & _CF) | _HF | zf\n')
  698. out.put('return %d\n' % t)
  699. def emit_set_b_r(out, b, r, x):
  700. """bit set operation on r"""
  701. if r == '(ix+d)':
  702. out.put('n = self.ix + d\n')
  703. out.put('val = self.mem[n] | (1 << %d)\n' % b)
  704. out.put('self.mem[n] = val\n')
  705. t = 11
  706. elif r == '(iy+d)':
  707. out.put('n = self.iy + d\n')
  708. out.put('val = self.mem[n] | (1 << %d)\n' % b)
  709. out.put('self.mem[n] = val\n')
  710. t = 11
  711. elif r == '(hl)':
  712. out.put('n = self._get_hl()\n')
  713. out.put('val = self.mem[n] | (1 << %d)\n' % b)
  714. out.put('self.mem[n] = val\n')
  715. t = 11
  716. else:
  717. out.put('val = self.%s | (1 << %d)\n' % (r, b))
  718. out.put('self.%s = val\n' % r)
  719. t = 4
  720. if x != '':
  721. out.put('self.%s = val\n' % x)
  722. out.put('return %d\n' % t)
  723. def emit_res_b_r(out, b, r, x):
  724. """bit reset operation on r"""
  725. if r == '(ix+d)':
  726. out.put('n = self.ix + d\n')
  727. out.put('val = self.mem[n] & ~(1 << %d)\n' % b)
  728. out.put('self.mem[n] = val\n')
  729. t = 11
  730. elif r == '(iy+d)':
  731. out.put('n = self.iy + d\n')
  732. out.put('val = self.mem[n] & ~(1 << %d)\n' % b)
  733. out.put('self.mem[n] = val\n')
  734. t = 11
  735. elif r == '(hl)':
  736. out.put('n = self._get_hl()\n')
  737. out.put('val = self.mem[n] & ~(1 << %d)\n' % b)
  738. out.put('self.mem[n] = val\n')
  739. t = 11
  740. else:
  741. out.put('val = self.%s & ~(1 << %d)\n' % (r, b))
  742. out.put('self.%s = val\n' % r)
  743. t = 4
  744. if x != '':
  745. out.put('self.%s = val\n' % x)
  746. out.put('return %d\n' % t)
  747. #-----------------------------------------------------------------------------
  748. # Jump Group
  749. def emit_jr_e(out):
  750. """jump relative"""
  751. out.put('self._inc_pc(_signed(self._get_n()))\n')
  752. out.put('return 12\n')
  753. def emit_jr_cc_d(out, cc):
  754. """jump relative on condition"""
  755. out.put('e = self._get_n()\n')
  756. if cc == 'nz':
  757. out.put('if (self.f & _ZF) == 0:\n')
  758. elif cc == 'z':
  759. out.put('if self.f & _ZF:\n')
  760. elif cc == 'nc':
  761. out.put('if (self.f & _CF) == 0:\n')
  762. elif cc == 'c':
  763. out.put('if self.f & _CF:\n')
  764. else:
  765. assert False
  766. out.put(' self._inc_pc(_signed(e))\n')
  767. out.put(' return 12\n')
  768. out.put('return 7\n')
  769. def emit_jp_nn(out):
  770. """jp nn"""
  771. out.put('self.pc = self._get_nn()\n')
  772. out.put('return 10\n')
  773. def emit_jp_cc_nn(out, cc):
  774. """jp cc,nn"""
  775. out.put('nn = self._get_nn()\n')
  776. if cc == 'nz':
  777. out.put('if (self.f & _ZF) == 0:\n')
  778. elif cc == 'z':
  779. out.put('if self.f & _ZF:\n')
  780. elif cc == 'nc':
  781. out.put('if (self.f & _CF) == 0:\n')
  782. elif cc == 'c':
  783. out.put('if self.f & _CF:\n')
  784. elif cc == 'po':
  785. out.put('if (self.f & _PF) == 0:\n')
  786. elif cc == 'pe':
  787. out.put('if self.f & _PF:\n')
  788. elif cc == 'p':
  789. out.put('if (self.f & _SF) == 0:\n')
  790. elif cc == 'm':
  791. out.put('if self.f & _SF:\n')
  792. else:
  793. assert False
  794. out.put(' self.pc = nn\n')
  795. out.put('return 10\n')
  796. def emit_jp_rp(out, rp):
  797. """jp rp"""
  798. if rp in _direct_rp:
  799. out.put('self.pc = self.%s\n' % rp)
  800. else:
  801. out.put('self.pc = self._get_%s()\n' % rp)
  802. out.put('return 4\n')
  803. def emit_djnz(out):
  804. """djnz e"""
  805. out.put('e = self._get_n()\n')
  806. out.put('self.b = (self.b - 1) & 0xff\n')
  807. out.put('if self.b:\n')
  808. out.put(' self._inc_pc(_signed(e))\n')
  809. out.put(' return 13\n')
  810. out.put('return 8\n')
  811. #-----------------------------------------------------------------------------
  812. # Call And Return Group
  813. def emit_call_nn(out):
  814. """call nn"""
  815. out.put('nn = self._get_nn()\n')
  816. out.put('self._push(self.pc)\n')
  817. out.put('self.pc = nn\n')
  818. out.put('return 17\n')
  819. def emit_call_cc_nn(out, cc):
  820. """call cc,nn"""
  821. out.put('nn = self._get_nn()\n')
  822. if cc == 'nz':
  823. out.put('if (self.f & _ZF) == 0:\n')
  824. elif cc == 'z':
  825. out.put('if self.f & _ZF:\n')
  826. elif cc == 'nc':
  827. out.put('if (self.f & _CF) == 0:\n')
  828. elif cc == 'c':
  829. out.put('if self.f & _CF:\n')
  830. elif cc == 'po':
  831. out.put('if (self.f & _PF) == 0:\n')
  832. elif cc == 'pe':
  833. out.put('if self.f & _PF:\n')
  834. elif cc == 'p':
  835. out.put('if (self.f & _SF) == 0:\n')
  836. elif cc == 'm':
  837. out.put('if self.f & _SF:\n')
  838. else:
  839. assert False
  840. out.put(' self._push(self.pc)\n')
  841. out.put(' self.pc = nn\n')
  842. out.put(' return 17\n')
  843. out.put('return 10\n')
  844. def emit_rst(out, p):
  845. out.put('self._push(self.pc)\n')
  846. out.put('self.pc = 0x%02x\n' % p)
  847. out.put('return 11\n')
  848. def emit_ret_cc(out, cc):
  849. """ret cc"""
  850. if cc == 'nz':
  851. out.put('if (self.f & _ZF) == 0:\n')
  852. elif cc == 'z':
  853. out.put('if self.f & _ZF:\n')
  854. elif cc == 'nc':
  855. out.put('if (self.f & _CF) == 0:\n')
  856. elif cc == 'c':
  857. out.put('if self.f & _CF:\n')
  858. elif cc == 'po':
  859. out.put('if (self.f & _PF) == 0:\n')
  860. elif cc == 'pe':
  861. out.put('if self.f & _PF:\n')
  862. elif cc == 'p':
  863. out.put('if (self.f & _SF) == 0:\n')
  864. elif cc == 'm':
  865. out.put('if self.f & _SF:\n')
  866. else:
  867. assert False
  868. out.put(' self.pc = self._pop()\n')
  869. out.put(' return 11\n')
  870. out.put('return 5\n')
  871. def emit_ret(out):
  872. """ret"""
  873. out.put('self.pc = self._pop()\n')
  874. out.put('return 10\n')
  875. #-----------------------------------------------------------------------------
  876. # Input and Output Group
  877. def emit_in_r_c(out, r):
  878. """in r,(c)"""
  879. out.put('val = self.io.rd(self._get_bc())\n')
  880. if r != '':
  881. out.put('self.%s = val\n' % r)
  882. out.put('self.f = (self.f & _CF) | self.f_szp[val]\n')
  883. out.put('return 8\n')
  884. def emit_in_a_n(out):
  885. """in a,(n)"""
  886. out.put('self.a = self.io.rd((self.a << 8) | self._get_n())\n')
  887. out.put('return 7\n')
  888. def emit_out_n_a(out):
  889. """out (n),a"""
  890. out.put('self.io.wr((self.a << 8) | self._get_n(), self.a)\n')
  891. out.put('return 7\n')
  892. def emit_out_c_r(out, r):
  893. if r == '':
  894. out.put('self.io.wr(self._get_bc(), 0)\n')
  895. else:
  896. out.put('self.io.wr(self._get_bc(), self.%s)\n' % r)
  897. out.put('return 8\n')
  898. #-----------------------------------------------------------------------------
  899. def emit_unimplemented(out):
  900. """unimplemented instruction - crash"""
  901. out.put('raise Error, \'unimplemented instruction\'\n')
  902. #-----------------------------------------------------------------------------
  903. def emit_normal(out, code):
  904. """
  905. Normal decode with no prefixes
  906. """
  907. m0 = code[0]
  908. x = (m0 >> 6) & 3
  909. y = (m0 >> 3) & 7
  910. z = (m0 >> 0) & 7
  911. p = (m0 >> 4) & 3
  912. q = (m0 >> 3) & 1
  913. if x == 0:
  914. if z == 0:
  915. if y == 0:
  916. return emit_nop(out)
  917. elif y == 1:
  918. return emit_ex_af_af(out)
  919. elif y == 2:
  920. return emit_djnz(out)
  921. elif y == 3:
  922. return emit_jr_e(out)
  923. else:
  924. return emit_jr_cc_d(out, _cc[y - 4])
  925. elif z == 1:
  926. if q == 0:
  927. return emit_ld_rp_nn(out, _rp[p])
  928. elif q == 1:
  929. return emit_op_rp_rp(out, 'add', 'hl', _rp[p])
  930. elif z == 2:
  931. if q == 0:
  932. if p == 0:
  933. return emit_ld_mem_xx_a(out, 'bc')
  934. elif p == 1:
  935. return emit_ld_mem_xx_a(out, 'de')
  936. elif p == 2:
  937. return emit_ld_mem_nn_rp(out, 'hl')
  938. else:
  939. return emit_ld_mem_xx_a(out, 'nn')
  940. else:
  941. if p == 0:
  942. return emit_ld_a_mem_xx(out, 'bc')
  943. elif p == 1:
  944. return emit_ld_a_mem_xx(out, 'de')
  945. elif p == 2:
  946. emit_ld_rp_mem_nn(out, 'hl')
  947. else:
  948. return emit_ld_a_mem_xx(out, 'nn')
  949. elif z == 3:
  950. if q == 0:
  951. return emit_inc_rp(out, _rp[p])
  952. else:
  953. return emit_dec_rp(out, _rp[p])
  954. elif z == 4:
  955. return emit_inc_dec_r(out, _r[y], 'inc')
  956. elif z == 5:
  957. return emit_inc_dec_r(out, _r[y], 'dec')
  958. elif z == 6:
  959. return emit_ld_r_n(out, _r[y])
  960. else:
  961. return emit_rota(out, _rota[y])
  962. elif x == 1:
  963. if (z == 6) and (y == 6):
  964. return emit_halt(out)
  965. else:
  966. return emit_ld_r_r(out, _r[y], _r[z])
  967. elif x == 2:
  968. return emit_alu_r(out, _alu[y], _r[z])
  969. else:
  970. if z == 0:
  971. return emit_ret_cc(out, _cc[y])
  972. elif z == 1:
  973. if q == 0:
  974. return emit_pop_rp(out, _rp2[p])
  975. else:
  976. if p == 0:
  977. return emit_ret(out)
  978. elif p == 1:
  979. return emit_exx(out)
  980. elif p == 2:
  981. return emit_jp_rp(out, 'hl')
  982. else:
  983. return emit_ld_sp_hl(out)
  984. elif z == 2:
  985. return emit_jp_cc_nn(out, _cc[y])
  986. elif z == 3:
  987. if y == 0:
  988. return emit_jp_nn(out)
  989. elif y == 2:
  990. return emit_out_n_a(out)
  991. elif y == 3:
  992. return emit_in_a_n(out)
  993. elif y == 4:
  994. return emit_ex_mem_sp_r(out, 'hl')
  995. elif y == 5:
  996. return emit_ex_de_hl(out)
  997. elif y == 6:
  998. return emit_di(out)
  999. else:
  1000. return emit_ei(out)
  1001. elif z == 4:
  1002. return emit_call_cc_nn(out, _cc[y])
  1003. elif z == 5:
  1004. if q == 0:
  1005. return emit_push_rp(out, _rp2[p])
  1006. else:
  1007. if p == 0:
  1008. return emit_call_nn(out)
  1009. elif z == 6:
  1010. return emit_alu_n(out, _alu[y])
  1011. else:
  1012. return emit_rst(out, (y << 3))
  1013. #-----------------------------------------------------------------------------
  1014. def emit_index(out, code, ir):
  1015. """
  1016. Decode with index register substitutions
  1017. """
  1018. m0 = code[0]
  1019. x = (m0 >> 6) & 3
  1020. y = (m0 >> 3) & 7
  1021. z = (m0 >> 0) & 7
  1022. p = (m0 >> 4) & 3
  1023. q = (m0 >> 3) & 1
  1024. # if using (hl) then: (hl)->(ix+d), h and l are unaffected.
  1025. alt0_r = list(_r)
  1026. alt0_r[6] = '(%s+d)' % ir
  1027. # if not using (hl) then: hl->ix, h->ixh, l->ixl
  1028. alt1_r = list(_r)
  1029. alt1_r[4] = '%sh' % ir
  1030. alt1_r[5] = '%sl' % ir
  1031. alt_rp = list(_rp)
  1032. alt_rp[2] = ir
  1033. alt_rp2 = list(_rp2)
  1034. alt_rp2[2] = ir
  1035. if x == 0:
  1036. if z == 0:
  1037. if y == 0:
  1038. return emit_nop(out)
  1039. elif y == 1:
  1040. return emit_ex_af_af(out)
  1041. elif y == 2:
  1042. return emit_djnz(out)
  1043. elif y == 3:
  1044. #return ('jr', '%04x' % dj, 3)
  1045. return emit_unimplemented(out)
  1046. else:
  1047. return emit_jr_cc_d(out, _cc[y - 4])
  1048. elif z == 1:
  1049. if q == 0:
  1050. return emit_ld_rp_nn(out, alt_rp[p])
  1051. elif q == 1:
  1052. return emit_op_rp_rp(out, 'add', ir, alt_rp[p])
  1053. elif z == 2:
  1054. if q == 0:
  1055. if p == 0:
  1056. #return ('ld', '(bc),a', 2)
  1057. return emit_unimplemented(out)
  1058. elif p == 1:
  1059. #return ('ld', '(de),a', 2)
  1060. return emit_unimplemented(out)
  1061. elif p == 2:
  1062. return emit_ld_mem_nn_rp(out, ir)
  1063. else:
  1064. #return ('ld', '(%04x),a' % nn, 4)
  1065. return emit_unimplemented(out)
  1066. else:
  1067. if p == 0:
  1068. #return ('ld', 'a,(bc)', 2)
  1069. return emit_unimplemented(out)
  1070. elif p == 1:
  1071. #return ('ld', 'a,(de)', 2)
  1072. return emit_unimplemented(out)
  1073. elif p == 2:
  1074. return emit_ld_rp_mem_nn(out, ir)
  1075. else:
  1076. #return ('ld', 'a,(%04x)' % nn, 4)
  1077. return emit_unimplemented(out)
  1078. elif z == 3:
  1079. if q == 0:
  1080. return emit_inc_rp(out, alt_rp[p])
  1081. else:
  1082. return emit_dec_rp(out, alt_rp[p])
  1083. elif z == 4:
  1084. if y == 6:
  1085. return emit_inc_dec_r(out, alt0_r[y], 'inc')
  1086. else:
  1087. return emit_inc_dec_r_alt1(out, alt1_r[y], 'inc')
  1088. elif z == 5:
  1089. if y == 6:
  1090. return emit_inc_dec_r(out, alt0_r[y], 'dec')
  1091. else:
  1092. return emit_inc_dec_r_alt1(out, alt1_r[y], 'dec')
  1093. elif z == 6:
  1094. if y == 6:
  1095. return emit_ld_mem_xx_n(out, ir)
  1096. else:
  1097. return emit_ld_r_n_alt1(out, alt1_r[y])
  1098. else:
  1099. #return (_rota[y], '', 2)
  1100. return emit_unimplemented(out)
  1101. elif x == 1:
  1102. if (z == 6) and (y == 6):
  1103. #return ('halt', '', 2)
  1104. return emit_unimplemented(out)
  1105. else:
  1106. if (y == 6) or (z == 6):
  1107. #return ('ld', '%s,%s' % (alt0_r[y], alt0_r[z]), 3)
  1108. return emit_ld_r_r(out, alt0_r[y], alt0_r[z])
  1109. else:
  1110. return emit_ld_r_r_alt1(out, alt1_r[y], alt1_r[z])
  1111. elif x == 2:
  1112. if z == 6:
  1113. return emit_alu_r(out, _alu[y], alt0_r[z])
  1114. else:
  1115. return emit_alu_r_alt1(out, _alu[y], alt1_r[z])
  1116. else:
  1117. if z == 0:
  1118. #return ('ret', _cc[y], 2)
  1119. return emit_unimplemented(out)
  1120. elif z == 1:
  1121. if q == 0:
  1122. return emit_pop_rp(out, alt_rp2[p])
  1123. else:
  1124. if p == 0:
  1125. #return ('ret', '', 2)
  1126. return emit_unimplemented(out)
  1127. elif p == 1:
  1128. return emit_exx(out)
  1129. elif p == 2:
  1130. return emit_jp_rp(out, ir)
  1131. else:
  1132. #return ('ld', 'sp,%s' % ir, 2)
  1133. return emit_unimplemented(out)
  1134. elif z == 2:
  1135. return emit_jp_cc_nn(out, _cc[y])
  1136. elif z == 3:
  1137. if y == 0:
  1138. #return ('jp', '%04x' % nn, 4)
  1139. return emit_unimplemented(out)
  1140. elif y == 2:
  1141. return emit_out_n_a(out)
  1142. elif y == 3:
  1143. return emit_in_a_n(out)
  1144. elif y == 4:
  1145. return emit_ex_mem_sp_r(out, ir)
  1146. elif y == 5:
  1147. #return ('ex', 'de,hl', 2)
  1148. return emit_unimplemented(out)
  1149. elif y == 6:
  1150. #return ('di', '', 2)
  1151. return emit_unimplemented(out)
  1152. else:
  1153. #return ('ei', '', 2)
  1154. return emit_unimplemented(out)
  1155. elif z == 4:
  1156. return emit_call_cc_nn(out, _cc[y])
  1157. elif z == 5:
  1158. if q == 0:
  1159. return emit_push_rp(out, alt_rp2[p])
  1160. else:
  1161. if p == 0:
  1162. return emit_call_nn(out)
  1163. elif z == 6:
  1164. #return (_alu[y], '%s%02x' % (_alux[y], n0), 3)
  1165. return emit_unimplemented(out)
  1166. else:
  1167. return emit_rst(out, (y << 3))
  1168. #-----------------------------------------------------------------------------
  1169. def emit_cb_prefix(out, code):
  1170. """
  1171. 0xCB <opcode>
  1172. """
  1173. m0 = code[0]
  1174. x = (m0 >> 6) & 3
  1175. y = (m0 >> 3) & 7
  1176. z = (m0 >> 0) & 7
  1177. if x == 0:
  1178. return emit_rot_r_x(out, _rot[y], _r[z], '')
  1179. elif x == 1:
  1180. return emit_bit_b_r(out, y, _r[z])
  1181. elif x == 2:
  1182. return emit_res_b_r(out, y, _r[z], '')
  1183. else:
  1184. return emit_set_b_r(out, y, _r[z], '')
  1185. #-----------------------------------------------------------------------------
  1186. def emit_ddcb_fdcb_prefix(out, code, ir):
  1187. """
  1188. 0xDDCB <d> <opcode>
  1189. 0xFDCB <d> <opcode>
  1190. """
  1191. m1 = code[1]
  1192. x = (m1 >> 6) & 3
  1193. y = (m1 >> 3) & 7
  1194. z = (m1 >> 0) & 7
  1195. if x == 0:
  1196. if z == 6:
  1197. return emit_rot_r_x(out, _rot[y], '(%s+d)' % ir, '')
  1198. else:
  1199. return emit_rot_r_x(out, _rot[y], '(%s+d)' % ir, _r[z])
  1200. elif x == 1:
  1201. return emit_bit_b_r(out, y, '(%s+d)' % ir)
  1202. elif x == 2:
  1203. if z == 6:
  1204. return emit_res_b_r(out, y, '(%s+d)' % ir, '')
  1205. else:
  1206. return emit_res_b_r(out, y, '(%s+d)' % ir, _r[z])
  1207. else:
  1208. if z == 6:
  1209. return emit_set_b_r(out, y, '(%s+d)' % ir, '')
  1210. else:
  1211. return emit_set_b_r(out, y, '(%s+d)' % ir, _r[z])
  1212. #-----------------------------------------------------------------------------
  1213. def emit_ed_prefix(out, code):
  1214. """
  1215. 0xED <opcode>
  1216. 0xED <opcode> <nn>
  1217. """
  1218. m0 = code[0]
  1219. #m1 = code[1]
  1220. #m2 = code[2]
  1221. x = (m0 >> 6) & 3
  1222. y = (m0 >> 3) & 7
  1223. z = (m0 >> 0) & 7
  1224. p = (m0 >> 4) & 3
  1225. q = (m0 >> 3) & 1
  1226. #nn = (m2 << 8) + m1
  1227. if x == 1:
  1228. if z == 0:
  1229. if y == 6:
  1230. return emit_in_r_c(out, '')
  1231. else:
  1232. return emit_in_r_c(out, _r[y])
  1233. elif z == 1:
  1234. if y == 6:
  1235. return emit_out_c_r(out, '')
  1236. else:
  1237. return emit_out_c_r(out, _r[y])
  1238. elif z == 2:
  1239. if q == 0:
  1240. return emit_op_rp_rp(out, 'sbc', 'hl', _rp[p])
  1241. else:
  1242. return emit_op_rp_rp(out, 'adc', 'hl', _rp[p])
  1243. elif z == 3:
  1244. if q == 0:
  1245. return emit_ld_mem_nn_rp(out, _rp[p])
  1246. else:
  1247. return emit_ld_rp_mem_nn(out, _rp[p])
  1248. elif z == 4:
  1249. return emit_neg(out)
  1250. elif z == 5:
  1251. if y == 1:
  1252. #return ('reti', '', 2)
  1253. return emit_unimplemented(out)
  1254. else:
  1255. #return ('retn', '', 2)
  1256. return emit_unimplemented(out)
  1257. elif z == 6:
  1258. return emit_im(out, _im[y])
  1259. else:
  1260. if y == 0:
  1261. return emit_ld_ira(out, 'i', 'a')
  1262. elif y == 1:
  1263. return emit_ld_ira(out, 'r', 'a')
  1264. elif y == 2:
  1265. return emit_ld_ira(out, 'a', 'i')
  1266. elif y == 3:
  1267. return emit_ld_ira(out, 'a', 'r')
  1268. elif y == 4:
  1269. return emit_rxd(out, 'rrd')
  1270. elif y == 5:
  1271. return emit_rxd(out, 'rld')
  1272. else:
  1273. return emit_nop(out)
  1274. elif x == 2:
  1275. if (z <= 3) and (y >= 4):
  1276. return emit_bli(out, _bli[z][y - 4])
  1277. return emit_nop(out)
  1278. #-----------------------------------------------------------------------------
  1279. def emit_dd_fd_prefix(out, code, ir):
  1280. """
  1281. 0xDD <x>
  1282. 0xFD <x>
  1283. """
  1284. m0 = code[0]
  1285. if m0 in (0xdd, 0xed, 0xfd):
  1286. emit_nop(out)
  1287. elif m0 == 0xcb:
  1288. return emit_ddcb_fdcb_prefix(out, code[1:], ir)
  1289. else:
  1290. return emit_index(out, code, ir)
  1291. #-----------------------------------------------------------------------------
  1292. def emit_instruction_code(out, code):
  1293. """emit the code for an instruction"""
  1294. m0 = code[0]
  1295. if m0 == 0xcb:
  1296. return emit_cb_prefix(out, code[1:])
  1297. elif m0 == 0xdd:
  1298. return emit_dd_fd_prefix(out, code[1:], 'ix')
  1299. elif m0 == 0xed:
  1300. return emit_ed_prefix(out, code[1:])
  1301. elif m0 == 0xfd:
  1302. return emit_dd_fd_prefix(out, code[1:], 'iy')
  1303. else:
  1304. return emit_normal(out, code)
  1305. #-----------------------------------------------------------------------------
  1306. def emit_triple_quote(out, comment):
  1307. out.put('"""%s"""\n' % comment)
  1308. #-----------------------------------------------------------------------------
  1309. def emit_opcode_table(out, idic, prefix, links, preamble):
  1310. """emit a function table for each opcode with this prefix"""
  1311. label = '_%s' % ''.join(['%02x' % byte for byte in prefix])
  1312. out.put('G_opcodes%s = [\n' % (label, '')[len(label) == 1])
  1313. out.indent(1)
  1314. for opcode in range(0x100):
  1315. code = list(prefix)
  1316. code.append(opcode)
  1317. # disassemble the instruction
  1318. mem = memory.ram(4)
  1319. mem.load(0, code)
  1320. (operation, operands, nbytes) = z80da.disassemble(mem, 0)
  1321. # add the instruction to the dictionary
  1322. inst = ' '.join((operation, operands))
  1323. label = ''.join(['%02x' % byte for byte in code])
  1324. if opcode in links:
  1325. out.put('cpu._execute_%s,' % label)
  1326. out.pad(36)
  1327. out.put('# 0x%02x execute %s prefix\n' % (opcode, label))
  1328. else:
  1329. # add the inst/label to the dictionary if it is unique
  1330. if idic.has_key(inst) == False:
  1331. idic[inst] = (label, code, preamble)
  1332. out.put('cpu._ins_%s,' % idic[inst][0])
  1333. out.pad(36)
  1334. out.put('# 0x%02x %s\n' % (opcode, inst))
  1335. out.outdent(1)
  1336. out.put(']\n')
  1337. #-----------------------------------------------------------------------------
  1338. def emit_instruction_function(out, instruction, x):
  1339. """emit the functon header and code for an instruction"""
  1340. (label, code, preamble) = x
  1341. out.indent(1)
  1342. out.put('def _ins_%s%s # %s\n' % (label, preamble, instruction))
  1343. out.indent(1)
  1344. #emit_triple_quote(out, instruction)
  1345. emit_instruction_code(out, code)
  1346. out.outdent(2)
  1347. #-----------------------------------------------------------------------------
  1348. # flag lookup tables
  1349. _CF = 0x01
  1350. _NF = 0x02
  1351. _PF = 0x04
  1352. _VF = _PF
  1353. _XF = 0x08
  1354. _HF = 0x10
  1355. _YF = 0x20
  1356. _ZF = 0x40
  1357. _SF = 0x80
  1358. def pop(x):
  1359. """return number of 1's in x"""
  1360. p = 0
  1361. while x != 0:
  1362. p += x & 1
  1363. x >>= 1
  1364. return p
  1365. def emit_table(out, name, data):
  1366. out.put('self.%s = [\n' % name)
  1367. out.indent(1)
  1368. for x in range(16):
  1369. for y in range(16):
  1370. out.put('0x%02x, ' % data[(x * 16) + y])
  1371. out.put('\n')
  1372. out.outdent(1)
  1373. out.put(']\n')
  1374. def emit_flag_tables(out):
  1375. SZ = []
  1376. SZ_BIT = []
  1377. SZP = []
  1378. SZHV_inc = []
  1379. SZHV_dec = []
  1380. for i in range(0x100):
  1381. p = pop(i)
  1382. if i:
  1383. SZ.append(i & _SF)
  1384. SZ_BIT.append(i & _SF)
  1385. else:
  1386. SZ.append(_ZF)
  1387. SZ_BIT.append(_ZF | _PF)
  1388. # undocumented flag bits 5+3
  1389. SZ[i] |= (i & (_YF | _XF))
  1390. SZ_BIT[i] |= (i & (_YF | _XF))
  1391. # parity
  1392. SZP.append(SZ[i])
  1393. if (p & 1) == 0:
  1394. SZP[i] |= _PF
  1395. # increment
  1396. SZHV_inc.append(SZ[i])
  1397. if i == 0x80:
  1398. SZHV_inc[i] |= _VF
  1399. if (i & 0x0f) == 0:
  1400. SZHV_inc[i] |= _HF
  1401. # decrement
  1402. SZHV_dec.append(SZ[i] | _NF)
  1403. if i == 0x7f:
  1404. SZHV_dec[i] |= _VF
  1405. if (i & 0x0f) == 0x0f:
  1406. SZHV_dec[i] |= _HF
  1407. out.indent(2)
  1408. emit_table(out, 'f_sz', SZ)
  1409. emit_table(out, 'f_szp', SZP)
  1410. emit_table(out, 'f_szhv_inc', SZHV_inc)
  1411. emit_table(out, 'f_szhv_dec', SZHV_dec)
  1412. out.outdent(2)
  1413. #-----------------------------------------------------------------------------
  1414. def _idic():
  1415. # It's populated as a side-effect of 'emit_opcode_table'
  1416. # TODO: untangle concerns
  1417. from os import devnull
  1418. idic = {}
  1419. nullout = output(devnull)
  1420. for (prefix, links, preamble) in _prefixes:
  1421. emit_opcode_table(nullout, idic, prefix, links, preamble)
  1422. nullout.close()
  1423. return idic
  1424. def generate(ofname):
  1425. """generate the opcode emulation file"""
  1426. out = output(ofname)
  1427. # generate flag tables
  1428. emit_flag_tables(out)
  1429. # generate the instruction functions
  1430. idic = _idic()
  1431. for (k, v) in idic.iteritems():
  1432. emit_instruction_function(out, k, v)
  1433. # generate the opcode tables
  1434. for (prefix, links, preamble) in _prefixes:
  1435. emit_opcode_table(out, idic, prefix, links, preamble)
  1436. out.close()
  1437. #-----------------------------------------------------------------------------
  1438. def usage():
  1439. print 'usage:'
  1440. print '%s -o [OUTPUT]' % sys.argv[0]
  1441. sys.exit(2)
  1442. #-----------------------------------------------------------------------------
  1443. def main():
  1444. ofname = 'z80bh.py'
  1445. try:
  1446. optlist, arglist = getopt.gnu_getopt(sys.argv[1:], 'o:')
  1447. except getopt.GetoptError:
  1448. usage()
  1449. for opt in optlist:
  1450. if opt[0] == '-o':
  1451. ofname = opt[1]
  1452. if len(arglist) != 0:
  1453. usage()
  1454. generate(ofname)
  1455. #-----------------------------------------------------------------------------
  1456. if __name__ == "__main__":
  1457. main()
  1458. #-----------------------------------------------------------------------------