An emulator, assembler, and disassembler for the Sega Game Gear
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

z80_ops.inc.c 54 KiB

vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 8 Jahren
vor 9 Jahren
vor 9 Jahren
vor 9 Jahren
vor 8 Jahren
vor 8 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373
  1. /* Copyright (C) 2014-2016 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. Released under the terms of the MIT License. See LICENSE for details. */
  3. /*
  4. This file contains code to implement the Z80 instruction set. Since there
  5. are a lot of functions, it is kept separate from the main z80.c file. It is
  6. included in the middle of z80.c and should not be compiled separately.
  7. Most of this information can be found in the Z80 User Manual, Revision 06.
  8. Undocumented opcodes, flags, and some additional details come from:
  9. - http://clrhome.org/table/
  10. - http://www.z80.info/z80sflag.htm
  11. */
  12. typedef uint8_t (*DispatchTable[256])(Z80*, uint8_t);
  13. static DispatchTable instruction_table;
  14. static DispatchTable instruction_table_extended;
  15. static DispatchTable instruction_table_bits;
  16. static DispatchTable instruction_table_index;
  17. static DispatchTable instruction_table_index_bits;
  18. /*
  19. Unimplemented opcode handler.
  20. */
  21. static uint8_t z80_inst_unimplemented(Z80 *z80, uint8_t opcode)
  22. {
  23. z80->except = true;
  24. z80->exc_code = Z80_EXC_UNIMPLEMENTED_OPCODE;
  25. z80->exc_data = opcode;
  26. return 4;
  27. }
  28. /*
  29. LD r, r' (0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x47, 0x48, 0x49, 0x4A, 0x4B,
  30. 0x4C, 0x4D, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x57, 0x58, 0x59,
  31. 0x5A, 0x5B, 0x5C, 0x5D, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x67,
  32. 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6F, 0x78, 0x79, 0x7A, 0x7B, 0x7C,
  33. 0x7D, 0x7F):
  34. Load r' (8-bit register) into r (8-bit register).
  35. */
  36. static uint8_t z80_inst_ld_r_r(Z80 *z80, uint8_t opcode)
  37. {
  38. uint8_t *dst = extract_reg(z80, opcode),
  39. *src = extract_reg(z80, opcode << 3);
  40. *dst = *src;
  41. z80->regs.pc++;
  42. return 4;
  43. }
  44. /*
  45. LD r, n (0x06, 0x0E, 0x16, 0x1E, 0x26, 0x2E, 0x3E):
  46. Load n (8-bit immediate) into r (8-bit register).
  47. */
  48. static uint8_t z80_inst_ld_r_n(Z80 *z80, uint8_t opcode)
  49. {
  50. uint8_t *reg = extract_reg(z80, opcode);
  51. *reg = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  52. z80->regs.pc++;
  53. return 7;
  54. }
  55. /*
  56. LD r, (HL) (0x46, 0x4E, 0x56, 0x5E, 0x66, 0x6E, 0x7E):
  57. Load the memory pointed to by HL into r (8-bit register).
  58. */
  59. static uint8_t z80_inst_ld_r_hl(Z80 *z80, uint8_t opcode)
  60. {
  61. uint8_t *reg = extract_reg(z80, opcode);
  62. *reg = mmu_read_byte(z80->mmu, z80->regs.hl);
  63. z80->regs.pc++;
  64. return 7;
  65. }
  66. /*
  67. LD r, (IXY+d) (0xDD46, 0xDD4E, 0xDD56, 0xDD5E, 0xDD66, 0xDD6E, 0xDD7E,
  68. 0xFD46, 0xFD4E, 0xFD56, 0xFD5E, 0xFD66, 0xFD6E, 0xFD7E):
  69. Load (IX+d) or (IY+d) into r (8-bit register).
  70. */
  71. static uint8_t z80_inst_ld_r_ixy(Z80 *z80, uint8_t opcode)
  72. {
  73. uint8_t *reg = extract_reg(z80, opcode);
  74. uint16_t addr = get_index_addr(z80, ++z80->regs.pc);
  75. *reg = mmu_read_byte(z80->mmu, addr);
  76. z80->regs.pc++;
  77. return 19;
  78. }
  79. /*
  80. LD (HL), r (0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x77):
  81. Load r (8-bit register) into the memory pointed to by HL.
  82. */
  83. static uint8_t z80_inst_ld_hl_r(Z80 *z80, uint8_t opcode)
  84. {
  85. uint8_t *reg = extract_reg(z80, opcode << 3);
  86. mmu_write_byte(z80->mmu, z80->regs.hl, *reg);
  87. z80->regs.pc++;
  88. return 7;
  89. }
  90. /*
  91. LD (IXY+d), r (0xDD70, 0xDD71, 0xDD72, 0xDD73, 0xDD74, 0xDD75, 0xDD77,
  92. 0xFD70, 0xFD71, 0xFD72, 0xFD73, 0xFD74, 0xFD75, 0xFD77):
  93. Load r (8-bit register) into (IX+d) or (IY+d).
  94. */
  95. static uint8_t z80_inst_ld_ixy_r(Z80 *z80, uint8_t opcode)
  96. {
  97. uint8_t *reg = extract_reg(z80, opcode << 3);
  98. uint16_t addr = get_index_addr(z80, ++z80->regs.pc);
  99. mmu_write_byte(z80->mmu, addr, *reg);
  100. z80->regs.pc++;
  101. return 19;
  102. }
  103. /*
  104. LD (HL), n (0x36):
  105. Load n (8-bit immediate) into the memory address pointed to by HL.
  106. */
  107. static uint8_t z80_inst_ld_hl_n(Z80 *z80, uint8_t opcode)
  108. {
  109. (void) opcode;
  110. uint8_t byte = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  111. mmu_write_byte(z80->mmu, z80->regs.hl, byte);
  112. z80->regs.pc++;
  113. return 10;
  114. }
  115. /*
  116. LD (IXY+d), n (0xDD36, 0xFD36):
  117. Load n (8-bit immediate) into (IX+d) or (IY+d).
  118. */
  119. static uint8_t z80_inst_ld_ixy_n(Z80 *z80, uint8_t opcode)
  120. {
  121. (void) opcode;
  122. uint16_t addr = get_index_addr(z80, ++z80->regs.pc);
  123. uint8_t byte = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  124. mmu_write_byte(z80->mmu, addr, byte);
  125. z80->regs.pc++;
  126. return 19;
  127. }
  128. /*
  129. LD A, (BC/DE) (0x0A, 0x1A):
  130. Load the memory pointed to BC or DE into A.
  131. */
  132. static uint8_t z80_inst_ld_a_bcde(Z80 *z80, uint8_t opcode)
  133. {
  134. uint16_t addr = *extract_pair(z80, opcode);
  135. z80->regs.a = mmu_read_byte(z80->mmu, addr);
  136. z80->regs.pc++;
  137. return 7;
  138. }
  139. /*
  140. LD A, (nn) (0x3A):
  141. Load memory at address nn into A.
  142. */
  143. static uint8_t z80_inst_ld_a_nn(Z80 *z80, uint8_t opcode)
  144. {
  145. (void) opcode;
  146. uint16_t addr = mmu_read_double(z80->mmu, ++z80->regs.pc);
  147. z80->regs.a = mmu_read_byte(z80->mmu, addr);
  148. z80->regs.pc += 2;
  149. return 13;
  150. }
  151. /*
  152. LD (BC/DE), A (0x02, 0x12):
  153. Load A into the memory address pointed to by BC or DE.
  154. */
  155. static uint8_t z80_inst_ld_bcde_a(Z80 *z80, uint8_t opcode)
  156. {
  157. uint16_t addr = *extract_pair(z80, opcode);
  158. mmu_write_byte(z80->mmu, addr, z80->regs.a);
  159. z80->regs.pc++;
  160. return 7;
  161. }
  162. /*
  163. LD (nn), A (0x32):
  164. Load A into memory address nn.
  165. */
  166. static uint8_t z80_inst_ld_nn_a(Z80 *z80, uint8_t opcode)
  167. {
  168. (void) opcode;
  169. uint16_t addr = mmu_read_double(z80->mmu, ++z80->regs.pc);
  170. mmu_write_byte(z80->mmu, addr, z80->regs.a);
  171. z80->regs.pc += 2;
  172. return 13;
  173. }
  174. /*
  175. LD A, I (0xED57):
  176. Load I into A.
  177. TODO
  178. */
  179. // static uint8_t z80_inst_ld_a_i(Z80 *z80, uint8_t opcode)
  180. /*
  181. LD A, R (0xED5F):
  182. Load R into A.
  183. TODO
  184. */
  185. // static uint8_t z80_inst_ld_a_r(Z80 *z80, uint8_t opcode)
  186. /*
  187. LD I, A (0xED47):
  188. Load A into I.
  189. */
  190. static uint8_t z80_inst_ld_i_a(Z80 *z80, uint8_t opcode)
  191. {
  192. (void) opcode;
  193. z80->regs.i = z80->regs.a;
  194. z80->regs.pc++;
  195. return 9;
  196. }
  197. /*
  198. LD R, A (0xED4F):
  199. Load A into R.
  200. */
  201. static uint8_t z80_inst_ld_r_a(Z80 *z80, uint8_t opcode)
  202. {
  203. (void) opcode;
  204. z80->regs.r = z80->regs.a;
  205. z80->regs.pc++;
  206. return 9;
  207. }
  208. /*
  209. LD dd, nn (0x01, 0x11, 0x21, 0x31):
  210. Load nn (16-bit immediate) into dd (16-bit register).
  211. */
  212. static uint8_t z80_inst_ld_dd_nn(Z80 *z80, uint8_t opcode)
  213. {
  214. *extract_pair(z80, opcode) = mmu_read_double(z80->mmu, ++z80->regs.pc);
  215. z80->regs.pc += 2;
  216. return 10;
  217. }
  218. /*
  219. LD IXY, nn (0xDD21, 0xFD21):
  220. Load nn (16-bit immediate) into IX or IY.
  221. */
  222. static uint8_t z80_inst_ld_ixy_nn(Z80 *z80, uint8_t opcode)
  223. {
  224. (void) opcode;
  225. *z80->regs.ixy = mmu_read_double(z80->mmu, ++z80->regs.pc);
  226. z80->regs.pc += 2;
  227. return 14;
  228. }
  229. /*
  230. LD HL, (nn) (0x2A):
  231. Load memory at address nn into HL.
  232. */
  233. static uint8_t z80_inst_ld_hl_inn(Z80 *z80, uint8_t opcode)
  234. {
  235. (void) opcode;
  236. uint16_t addr = mmu_read_double(z80->mmu, ++z80->regs.pc);
  237. z80->regs.hl = mmu_read_double(z80->mmu, addr);
  238. z80->regs.pc += 2;
  239. return 16;
  240. }
  241. /*
  242. LD dd, (nn) (0xED4B, 0xED5B, 0xED6B, 0xED7B):
  243. Load memory at address nn into dd (16-bit register).
  244. */
  245. static uint8_t z80_inst_ld_dd_inn(Z80 *z80, uint8_t opcode)
  246. {
  247. uint16_t addr = mmu_read_double(z80->mmu, ++z80->regs.pc);
  248. *extract_pair(z80, opcode) = mmu_read_double(z80->mmu, addr);
  249. z80->regs.pc += 2;
  250. return 20;
  251. }
  252. /*
  253. LD IXY, (nn) (0xDD2A, 0xFD2A):
  254. Load memory at address nn into IX or IY.
  255. */
  256. static uint8_t z80_inst_ld_ixy_inn(Z80 *z80, uint8_t opcode)
  257. {
  258. (void) opcode;
  259. uint16_t addr = mmu_read_double(z80->mmu, ++z80->regs.pc);
  260. *z80->regs.ixy = mmu_read_double(z80->mmu, addr);
  261. z80->regs.pc += 2;
  262. return 20;
  263. }
  264. /*
  265. LD (nn), HL: (0x22):
  266. Load HL into memory address nn.
  267. */
  268. static uint8_t z80_inst_ld_inn_hl(Z80 *z80, uint8_t opcode)
  269. {
  270. (void) opcode;
  271. uint16_t addr = mmu_read_double(z80->mmu, ++z80->regs.pc);
  272. mmu_write_double(z80->mmu, addr, z80->regs.hl);
  273. z80->regs.pc += 2;
  274. return 16;
  275. }
  276. /*
  277. LD (nn), dd (0xED43, 0xED53, 0xED63, 0xED73);
  278. Load dd (16-bit register) into memory address nn.
  279. */
  280. static uint8_t z80_inst_ld_inn_dd(Z80 *z80, uint8_t opcode)
  281. {
  282. uint16_t addr = mmu_read_double(z80->mmu, ++z80->regs.pc);
  283. mmu_write_double(z80->mmu, addr, *extract_pair(z80, opcode));
  284. z80->regs.pc += 2;
  285. return 20;
  286. }
  287. /*
  288. LD (nn), IXY (0xDD22, 0xFD22):
  289. Load IX or IY into memory address nn.
  290. */
  291. static uint8_t z80_inst_ld_inn_ixy(Z80 *z80, uint8_t opcode)
  292. {
  293. (void) opcode;
  294. uint16_t addr = mmu_read_double(z80->mmu, ++z80->regs.pc);
  295. mmu_write_double(z80->mmu, addr, *z80->regs.ixy);
  296. z80->regs.pc += 2;
  297. return 20;
  298. }
  299. /*
  300. LD SP, HL (0xF9):
  301. Load HL into SP.
  302. */
  303. static uint8_t z80_inst_ld_sp_hl(Z80 *z80, uint8_t opcode)
  304. {
  305. (void) opcode;
  306. z80->regs.sp = z80->regs.hl;
  307. z80->regs.pc++;
  308. return 6;
  309. }
  310. /*
  311. LD SP, IXY (0xDDF9, 0xFDF9):
  312. Load IX or IY into SP.
  313. */
  314. static uint8_t z80_inst_ld_sp_ixy(Z80 *z80, uint8_t opcode)
  315. {
  316. (void) opcode;
  317. *z80->regs.ixy = z80->regs.hl;
  318. z80->regs.pc++;
  319. return 10;
  320. }
  321. /*
  322. PUSH qq (0xC5, 0xD5, 0xE5, 0xF5):
  323. Push qq onto the stack, and decrement SP by two.
  324. */
  325. static uint8_t z80_inst_push_qq(Z80 *z80, uint8_t opcode)
  326. {
  327. stack_push(z80, *extract_pair_qq(z80, opcode));
  328. z80->regs.pc++;
  329. return 11;
  330. }
  331. /*
  332. PUSH IXY (0xDDE5, 0xFDE5):
  333. Push IX or IY onto the stack, and decrement SP by two.
  334. */
  335. static uint8_t z80_inst_push_ixy(Z80 *z80, uint8_t opcode)
  336. {
  337. (void) opcode;
  338. stack_push(z80, *z80->regs.ixy);
  339. z80->regs.pc++;
  340. return 15;
  341. }
  342. /*
  343. POP qq (0xC1, 0xD1, 0xE1, 0xF1):
  344. Pop qq from the stack, and increment SP by two.
  345. */
  346. static uint8_t z80_inst_pop_qq(Z80 *z80, uint8_t opcode)
  347. {
  348. *extract_pair_qq(z80, opcode) = stack_pop(z80);
  349. z80->regs.pc++;
  350. return 10;
  351. }
  352. /*
  353. POP IXY (0xDDE1, 0xFDE1):
  354. Pop IX or IY from the stack, and increment SP by two.
  355. */
  356. static uint8_t z80_inst_pop_ixy(Z80 *z80, uint8_t opcode)
  357. {
  358. (void) opcode;
  359. *z80->regs.ixy = stack_pop(z80);
  360. z80->regs.pc++;
  361. return 14;
  362. }
  363. /*
  364. EX DE, HL (0xEB):
  365. Exchange DE with HL.
  366. */
  367. static uint8_t z80_inst_ex_de_hl(Z80 *z80, uint8_t opcode)
  368. {
  369. (void) opcode;
  370. uint16_t temp = z80->regs.de;
  371. z80->regs.de = z80->regs.hl;
  372. z80->regs.hl = temp;
  373. z80->regs.pc++;
  374. return 4;
  375. }
  376. /*
  377. EX AF, AF' (0x08):
  378. Exchange AF with AF'.
  379. */
  380. static uint8_t z80_inst_ex_af_af(Z80 *z80, uint8_t opcode)
  381. {
  382. (void) opcode;
  383. uint16_t temp = z80->regs.af;
  384. z80->regs.af = z80->regs.af_;
  385. z80->regs.af_ = temp;
  386. z80->regs.pc++;
  387. return 4;
  388. }
  389. /*
  390. EXX (0xD9):
  391. Exchange the 16-bit registers with their shadows
  392. (BC <=> BC', DE <=> DE', HL <=> HL').
  393. */
  394. static uint8_t z80_inst_exx(Z80 *z80, uint8_t opcode)
  395. {
  396. (void) opcode;
  397. uint16_t bc = z80->regs.bc, de = z80->regs.de, hl = z80->regs.hl;
  398. z80->regs.bc = z80->regs.bc_;
  399. z80->regs.de = z80->regs.de_;
  400. z80->regs.hl = z80->regs.hl_;
  401. z80->regs.bc_ = bc;
  402. z80->regs.de_ = de;
  403. z80->regs.hl_ = hl;
  404. z80->regs.pc++;
  405. return 4;
  406. }
  407. /*
  408. EX (SP), HL (0xE3):
  409. Exchange the memory pointed to by SP with HL.
  410. */
  411. static uint8_t z80_inst_ex_sp_hl(Z80 *z80, uint8_t opcode)
  412. {
  413. (void) opcode;
  414. uint16_t hl = z80->regs.hl, sp = z80->regs.sp;
  415. z80->regs.hl = mmu_read_double(z80->mmu, sp);
  416. mmu_write_double(z80->mmu, sp, hl);
  417. z80->regs.pc++;
  418. return 19;
  419. }
  420. /*
  421. EX (SP), IXY (0xDDE3, 0xFDE3):
  422. Exchange the memory pointed to by SP with IX or IY.
  423. */
  424. static uint8_t z80_inst_ex_sp_ixy(Z80 *z80, uint8_t opcode)
  425. {
  426. (void) opcode;
  427. uint16_t ixy = *z80->regs.ixy, sp = z80->regs.sp;
  428. *z80->regs.ixy = mmu_read_double(z80->mmu, sp);
  429. mmu_write_double(z80->mmu, sp, ixy);
  430. z80->regs.pc++;
  431. return 23;
  432. }
  433. /*
  434. LDI (0xEDA0):
  435. LD (DE), (HL); INC HL; INC DE; DEC BC;
  436. */
  437. static uint8_t z80_inst_ldi(Z80 *z80, uint8_t opcode)
  438. {
  439. (void) opcode;
  440. uint8_t value = mmu_read_byte(z80->mmu, z80->regs.hl);
  441. mmu_write_byte(z80->mmu, z80->regs.de, value);
  442. z80->regs.hl++;
  443. z80->regs.de++;
  444. z80->regs.bc--;
  445. set_flags_blockxfer(z80, value);
  446. z80->regs.pc++;
  447. return 16;
  448. }
  449. /*
  450. LDIR (0xEDB0):
  451. LDI; JR PV, -2
  452. */
  453. static uint8_t z80_inst_ldir(Z80 *z80, uint8_t opcode)
  454. {
  455. z80_inst_ldi(z80, opcode);
  456. if (z80->regs.bc == 0)
  457. return 16;
  458. z80->regs.pc -= 2;
  459. return 21;
  460. }
  461. /*
  462. LDD (0xEDA8):
  463. LD (DE), (HL); DEC HL; DEC DE; DEC BC;
  464. */
  465. static uint8_t z80_inst_ldd(Z80 *z80, uint8_t opcode)
  466. {
  467. (void) opcode;
  468. uint8_t value = mmu_read_byte(z80->mmu, z80->regs.hl);
  469. mmu_write_byte(z80->mmu, z80->regs.de, value);
  470. z80->regs.hl--;
  471. z80->regs.de--;
  472. z80->regs.bc--;
  473. set_flags_blockxfer(z80, value);
  474. z80->regs.pc++;
  475. return 16;
  476. }
  477. /*
  478. LDDR (0xEDB8):
  479. LDD; JR PV, -2
  480. */
  481. static uint8_t z80_inst_lddr(Z80 *z80, uint8_t opcode)
  482. {
  483. z80_inst_ldd(z80, opcode);
  484. if (z80->regs.bc == 0)
  485. return 16;
  486. z80->regs.pc -= 2;
  487. return 21;
  488. }
  489. // TODO: CPI
  490. // TODO: CPIR
  491. // TODO: CPD
  492. // TODO: CPDR
  493. /*
  494. ADD A, r (0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x87):
  495. Add r (8-bit register) to A.
  496. */
  497. static uint8_t z80_inst_add_a_r(Z80 *z80, uint8_t opcode)
  498. {
  499. uint8_t value = *extract_reg(z80, opcode << 3);
  500. set_flags_add8(z80, value);
  501. z80->regs.a += value;
  502. z80->regs.pc++;
  503. return 4;
  504. }
  505. /*
  506. ADD A, n (0xC6):
  507. Add n (8-bit immediate) to A.
  508. */
  509. static uint8_t z80_inst_add_a_n(Z80 *z80, uint8_t opcode)
  510. {
  511. (void) opcode;
  512. uint8_t value = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  513. set_flags_add8(z80, value);
  514. z80->regs.a += value;
  515. z80->regs.pc++;
  516. return 7;
  517. }
  518. /*
  519. ADD A, (HL) (0x86):
  520. Add (HL) to A.
  521. */
  522. static uint8_t z80_inst_add_a_hl(Z80 *z80, uint8_t opcode)
  523. {
  524. (void) opcode;
  525. uint8_t value = mmu_read_byte(z80->mmu, z80->regs.hl);
  526. set_flags_add8(z80, value);
  527. z80->regs.a += value;
  528. z80->regs.pc++;
  529. return 7;
  530. }
  531. /*
  532. ADD A, (IXY+d) (0xDD86, 0xFD86):
  533. Add (IX+d) or (IY+d) to A.
  534. */
  535. static uint8_t z80_inst_add_a_ixy(Z80 *z80, uint8_t opcode)
  536. {
  537. (void) opcode;
  538. uint16_t addr = get_index_addr(z80, ++z80->regs.pc);
  539. uint8_t value = mmu_read_byte(z80->mmu, addr);
  540. set_flags_add8(z80, value);
  541. z80->regs.a += value;
  542. z80->regs.pc++;
  543. return 19;
  544. }
  545. /*
  546. ADC A, r (0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8F):
  547. Add r (8-bit register) plus the carry flag to A.
  548. */
  549. static uint8_t z80_inst_adc_a_r(Z80 *z80, uint8_t opcode)
  550. {
  551. uint16_t value = *extract_reg(z80, opcode << 3);
  552. value += get_flag(z80, FLAG_CARRY);
  553. set_flags_add8(z80, value);
  554. z80->regs.a += value;
  555. z80->regs.pc++;
  556. return 4;
  557. }
  558. /*
  559. ADC A, n (0xCE):
  560. Add n (8-bit immediate) plus the carry flag to A.
  561. */
  562. static uint8_t z80_inst_adc_a_n(Z80 *z80, uint8_t opcode)
  563. {
  564. (void) opcode;
  565. uint16_t value = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  566. value += get_flag(z80, FLAG_CARRY);
  567. set_flags_add8(z80, value);
  568. z80->regs.a += value;
  569. z80->regs.pc++;
  570. return 7;
  571. }
  572. /*
  573. ADC A, (HL) (0x8E):
  574. Add (HL) plus the carry flag to A.
  575. */
  576. static uint8_t z80_inst_adc_a_hl(Z80 *z80, uint8_t opcode)
  577. {
  578. (void) opcode;
  579. uint16_t value = mmu_read_byte(z80->mmu, z80->regs.hl);
  580. value += get_flag(z80, FLAG_CARRY);
  581. set_flags_add8(z80, value);
  582. z80->regs.a += value;
  583. z80->regs.pc++;
  584. return 7;
  585. }
  586. /*
  587. ADC A, (IXY+d) (0xDD8E, 0xFD8E):
  588. Add (IX+d) or (IY+d) plus the carry flag to A.
  589. */
  590. static uint8_t z80_inst_adc_a_ixy(Z80 *z80, uint8_t opcode)
  591. {
  592. (void) opcode;
  593. uint16_t addr = get_index_addr(z80, ++z80->regs.pc);
  594. uint16_t value = mmu_read_byte(z80->mmu, addr);
  595. value += get_flag(z80, FLAG_CARRY);
  596. set_flags_add8(z80, value);
  597. z80->regs.a += value;
  598. z80->regs.pc++;
  599. return 19;
  600. }
  601. /*
  602. SUB r (0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x97):
  603. Subtract r (8-bit register) from A.
  604. */
  605. static uint8_t z80_inst_sub_r(Z80 *z80, uint8_t opcode)
  606. {
  607. uint8_t value = *extract_reg(z80, opcode << 3);
  608. set_flags_sub8(z80, value);
  609. z80->regs.a -= value;
  610. z80->regs.pc++;
  611. return 4;
  612. }
  613. /*
  614. SUB n (0xD6):
  615. Subtract n (8-bit immediate) from A.
  616. */
  617. static uint8_t z80_inst_sub_n(Z80 *z80, uint8_t opcode)
  618. {
  619. (void) opcode;
  620. uint8_t value = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  621. set_flags_sub8(z80, value);
  622. z80->regs.a -= value;
  623. z80->regs.pc++;
  624. return 7;
  625. }
  626. /*
  627. SUB (HL)
  628. Subtract (HL) from A.
  629. */
  630. static uint8_t z80_inst_sub_hl(Z80 *z80, uint8_t opcode)
  631. {
  632. (void) opcode;
  633. uint8_t value = mmu_read_byte(z80->mmu, z80->regs.hl);
  634. set_flags_sub8(z80, value);
  635. z80->regs.a -= value;
  636. z80->regs.pc++;
  637. return 7;
  638. }
  639. /*
  640. SUB (IXY+d)
  641. Subtract (IX+d) or (IY+d) from A.
  642. */
  643. static uint8_t z80_inst_sub_ixy(Z80 *z80, uint8_t opcode)
  644. {
  645. (void) opcode;
  646. uint16_t addr = get_index_addr(z80, ++z80->regs.pc);
  647. uint8_t value = mmu_read_byte(z80->mmu, addr);
  648. set_flags_sub8(z80, value);
  649. z80->regs.a -= value;
  650. z80->regs.pc++;
  651. return 19;
  652. }
  653. /*
  654. SBC A, r (0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9F):
  655. Subtract r (8-bit register) plus the carry flag from A.
  656. */
  657. static uint8_t z80_inst_sbc_a_r(Z80 *z80, uint8_t opcode)
  658. {
  659. uint8_t value = *extract_reg(z80, opcode << 3);
  660. value += get_flag(z80, FLAG_CARRY);
  661. set_flags_sub8(z80, value);
  662. z80->regs.a -= value;
  663. z80->regs.pc++;
  664. return 4;
  665. }
  666. /*
  667. SBC A, n (0xDE):
  668. Subtract n (8-bit immediate) plus the carry flag from A.
  669. */
  670. static uint8_t z80_inst_sbc_a_n(Z80 *z80, uint8_t opcode)
  671. {
  672. (void) opcode;
  673. uint8_t value = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  674. value += get_flag(z80, FLAG_CARRY);
  675. set_flags_sub8(z80, value);
  676. z80->regs.a -= value;
  677. z80->regs.pc++;
  678. return 7;
  679. }
  680. /*
  681. SBC A, (HL) (0x9E):
  682. Subtract (HL) plus the carry flag from A.
  683. */
  684. static uint8_t z80_inst_sbc_a_hl(Z80 *z80, uint8_t opcode)
  685. {
  686. (void) opcode;
  687. uint8_t value = mmu_read_byte(z80->mmu, z80->regs.hl);
  688. value += get_flag(z80, FLAG_CARRY);
  689. set_flags_sub8(z80, value);
  690. z80->regs.a -= value;
  691. z80->regs.pc++;
  692. return 7;
  693. }
  694. /*
  695. SBC A, (IXY+d) (0xDD9E, 0xFD9E):
  696. Subtract (IX+d) or (IY+d) plus the carry flag from A.
  697. */
  698. static uint8_t z80_inst_sbc_a_ixy(Z80 *z80, uint8_t opcode)
  699. {
  700. (void) opcode;
  701. uint16_t addr = get_index_addr(z80, ++z80->regs.pc);
  702. uint8_t value = mmu_read_byte(z80->mmu, addr);
  703. value += get_flag(z80, FLAG_CARRY);
  704. set_flags_sub8(z80, value);
  705. z80->regs.a -= value;
  706. z80->regs.pc++;
  707. return 19;
  708. }
  709. /*
  710. AND r (0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA7):
  711. Bitwise AND A with r (8-bit register).
  712. */
  713. static uint8_t z80_inst_and_r(Z80 *z80, uint8_t opcode)
  714. {
  715. uint8_t value = *extract_reg(z80, opcode << 3);
  716. uint8_t res = z80->regs.a &= value;
  717. set_flags_bitwise(z80, res, true);
  718. z80->regs.pc++;
  719. return 4;
  720. }
  721. /*
  722. AND n (0xE6):
  723. Bitwise AND A with n (8-bit immediate).
  724. */
  725. static uint8_t z80_inst_and_n(Z80 *z80, uint8_t opcode)
  726. {
  727. (void) opcode;
  728. uint8_t value = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  729. uint8_t res = z80->regs.a &= value;
  730. set_flags_bitwise(z80, res, true);
  731. z80->regs.pc++;
  732. return 7;
  733. }
  734. /*
  735. AND (HL) (0xA6):
  736. Bitwise AND A with (HL).
  737. */
  738. static uint8_t z80_inst_and_hl(Z80 *z80, uint8_t opcode)
  739. {
  740. (void) opcode;
  741. uint8_t value = mmu_read_byte(z80->mmu, z80->regs.hl);
  742. uint8_t res = z80->regs.a &= value;
  743. set_flags_bitwise(z80, res, true);
  744. z80->regs.pc++;
  745. return 7;
  746. }
  747. /*
  748. AND (IXY+d) (0xDDA6, 0xFDA6):
  749. Bitwise AND A with (IX+d) or (IY+d).
  750. */
  751. static uint8_t z80_inst_and_ixy(Z80 *z80, uint8_t opcode)
  752. {
  753. (void) opcode;
  754. uint16_t addr = get_index_addr(z80, ++z80->regs.pc);
  755. uint8_t value = mmu_read_byte(z80->mmu, addr);
  756. uint8_t res = z80->regs.a &= value;
  757. set_flags_bitwise(z80, res, true);
  758. z80->regs.pc++;
  759. return 19;
  760. }
  761. /*
  762. OR r (0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB7):
  763. Bitwise OR A with r (8-bit register).
  764. */
  765. static uint8_t z80_inst_or_r(Z80 *z80, uint8_t opcode)
  766. {
  767. uint8_t value = *extract_reg(z80, opcode << 3);
  768. uint8_t res = z80->regs.a |= value;
  769. set_flags_bitwise(z80, res, false);
  770. z80->regs.pc++;
  771. return 4;
  772. }
  773. /*
  774. OR n (0xF6):
  775. Bitwise OR A with n (8-bit immediate).
  776. */
  777. static uint8_t z80_inst_or_n(Z80 *z80, uint8_t opcode)
  778. {
  779. (void) opcode;
  780. uint8_t value = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  781. uint8_t res = z80->regs.a |= value;
  782. set_flags_bitwise(z80, res, false);
  783. z80->regs.pc++;
  784. return 7;
  785. }
  786. /*
  787. OR (HL) (0xB6):
  788. Bitwise OR A with (HL).
  789. */
  790. static uint8_t z80_inst_or_hl(Z80 *z80, uint8_t opcode)
  791. {
  792. (void) opcode;
  793. uint8_t value = mmu_read_byte(z80->mmu, z80->regs.hl);
  794. uint8_t res = z80->regs.a |= value;
  795. set_flags_bitwise(z80, res, false);
  796. z80->regs.pc++;
  797. return 7;
  798. }
  799. /*
  800. OR (IXY+d) (0xDDB6, 0xFDB6):
  801. Bitwise OR A with (IX+d) or (IY+d).
  802. */
  803. static uint8_t z80_inst_or_ixy(Z80 *z80, uint8_t opcode)
  804. {
  805. (void) opcode;
  806. uint16_t addr = get_index_addr(z80, ++z80->regs.pc);
  807. uint8_t value = mmu_read_byte(z80->mmu, addr);
  808. uint8_t res = z80->regs.a |= value;
  809. set_flags_bitwise(z80, res, false);
  810. z80->regs.pc++;
  811. return 19;
  812. }
  813. /*
  814. XOR r (0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAF):
  815. Bitwise XOR A with r (8-bit register).
  816. */
  817. static uint8_t z80_inst_xor_r(Z80 *z80, uint8_t opcode)
  818. {
  819. uint8_t value = *extract_reg(z80, opcode << 3);
  820. uint8_t res = z80->regs.a ^= value;
  821. set_flags_bitwise(z80, res, false);
  822. z80->regs.pc++;
  823. return 4;
  824. }
  825. /*
  826. XOR n (0xEE):
  827. Bitwise XOR A with n (8-bit immediate).
  828. */
  829. static uint8_t z80_inst_xor_n(Z80 *z80, uint8_t opcode)
  830. {
  831. (void) opcode;
  832. uint8_t value = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  833. uint8_t res = z80->regs.a ^= value;
  834. set_flags_bitwise(z80, res, false);
  835. z80->regs.pc++;
  836. return 7;
  837. }
  838. /*
  839. XOR (HL) (0xAE):
  840. Bitwise XOR A with (HL).
  841. */
  842. static uint8_t z80_inst_xor_hl(Z80 *z80, uint8_t opcode)
  843. {
  844. (void) opcode;
  845. uint8_t value = mmu_read_byte(z80->mmu, z80->regs.hl);
  846. uint8_t res = z80->regs.a ^= value;
  847. set_flags_bitwise(z80, res, false);
  848. z80->regs.pc++;
  849. return 7;
  850. }
  851. /*
  852. XOR (IXY+d) (0xDDAE, 0xFDAE):
  853. Bitwise XOR A with (IX+d) or (IY+d).
  854. */
  855. static uint8_t z80_inst_xor_ixy(Z80 *z80, uint8_t opcode)
  856. {
  857. (void) opcode;
  858. uint16_t addr = get_index_addr(z80, ++z80->regs.pc);
  859. uint8_t value = mmu_read_byte(z80->mmu, addr);
  860. uint8_t res = z80->regs.a ^= value;
  861. set_flags_bitwise(z80, res, false);
  862. z80->regs.pc++;
  863. return 19;
  864. }
  865. /*
  866. CP r (0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBF):
  867. Set flags as if r (8-bit register) had been subtracted from A.
  868. */
  869. static uint8_t z80_inst_cp_r(Z80 *z80, uint8_t opcode)
  870. {
  871. uint8_t value = *extract_reg(z80, opcode << 3);
  872. set_flags_cp(z80, value);
  873. z80->regs.pc++;
  874. return 4;
  875. }
  876. /*
  877. CP n (0xFE):
  878. Set flags as if n (8-bit immediate) had been subtracted from A.
  879. */
  880. static uint8_t z80_inst_cp_n(Z80 *z80, uint8_t opcode)
  881. {
  882. (void) opcode;
  883. uint8_t value = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  884. set_flags_cp(z80, value);
  885. z80->regs.pc++;
  886. return 7;
  887. }
  888. /*
  889. CP (HL) (0xBE):
  890. Set flags as if (HL) had been subtracted from A.
  891. */
  892. static uint8_t z80_inst_cp_hl(Z80 *z80, uint8_t opcode)
  893. {
  894. (void) opcode;
  895. uint8_t value = mmu_read_byte(z80->mmu, z80->regs.hl);
  896. set_flags_cp(z80, value);
  897. z80->regs.pc++;
  898. return 7;
  899. }
  900. /*
  901. CP (IXY+d) (0xDDBE, 0xFDBE):
  902. Set flags as if (IX+d) or (IY+d) had been subtracted from A.
  903. */
  904. static uint8_t z80_inst_cp_ixy(Z80 *z80, uint8_t opcode)
  905. {
  906. (void) opcode;
  907. uint16_t addr = get_index_addr(z80, ++z80->regs.pc);
  908. uint8_t value = mmu_read_byte(z80->mmu, addr);
  909. set_flags_cp(z80, value);
  910. z80->regs.pc++;
  911. return 19;
  912. }
  913. /*
  914. INC r (0x04, 0x0C, 0x14, 0x1C, 0x24, 0x2C, 0x3C):
  915. Increment r (8-bit register).
  916. */
  917. static uint8_t z80_inst_inc_r(Z80 *z80, uint8_t opcode)
  918. {
  919. uint8_t *reg = extract_reg(z80, opcode);
  920. set_flags_inc(z80, *reg);
  921. (*reg)++;
  922. z80->regs.pc++;
  923. return 4;
  924. }
  925. /*
  926. INC (HL) (0x34):
  927. Increment the memory address pointed to by HL.
  928. */
  929. static uint8_t z80_inst_inc_hl(Z80 *z80, uint8_t opcode)
  930. {
  931. (void) opcode;
  932. uint8_t byte = mmu_read_byte(z80->mmu, z80->regs.hl);
  933. set_flags_inc(z80, byte);
  934. mmu_write_byte(z80->mmu, z80->regs.hl, ++byte);
  935. z80->regs.pc++;
  936. return 11;
  937. }
  938. /*
  939. INC (IXY+d) (0xDD34, 0xFD34):
  940. Increment (IX+d) or (IY+d).
  941. */
  942. static uint8_t z80_inst_inc_ixy(Z80 *z80, uint8_t opcode)
  943. {
  944. (void) opcode;
  945. uint16_t addr = get_index_addr(z80, ++z80->regs.pc);
  946. uint8_t byte = mmu_read_byte(z80->mmu, addr);
  947. set_flags_inc(z80, byte);
  948. mmu_write_byte(z80->mmu, addr, ++byte);
  949. z80->regs.pc++;
  950. return 23;
  951. }
  952. /*
  953. DEC r (0x05, 0x0D, 0x15, 0x1D, 0x25, 0x2D, 0x3D):
  954. Decrement r (8-bit register).
  955. */
  956. static uint8_t z80_inst_dec_r(Z80 *z80, uint8_t opcode)
  957. {
  958. uint8_t *reg = extract_reg(z80, opcode);
  959. set_flags_dec(z80, *reg);
  960. (*reg)--;
  961. z80->regs.pc++;
  962. return 4;
  963. }
  964. /*
  965. DEC (HL) (0x35):
  966. Decrement the memory address pointed to by HL.
  967. */
  968. static uint8_t z80_inst_dec_hl(Z80 *z80, uint8_t opcode)
  969. {
  970. (void) opcode;
  971. uint8_t byte = mmu_read_byte(z80->mmu, z80->regs.hl);
  972. set_flags_dec(z80, byte);
  973. mmu_write_byte(z80->mmu, z80->regs.hl, --byte);
  974. z80->regs.pc++;
  975. return 11;
  976. }
  977. /*
  978. DEC (IXY+d) (0xDD35, 0xFD35):
  979. Decrement (IX+d) or (IY+d).
  980. */
  981. static uint8_t z80_inst_dec_ixy(Z80 *z80, uint8_t opcode)
  982. {
  983. (void) opcode;
  984. uint16_t addr = get_index_addr(z80, ++z80->regs.pc);
  985. uint8_t byte = mmu_read_byte(z80->mmu, addr);
  986. set_flags_dec(z80, byte);
  987. mmu_write_byte(z80->mmu, addr, --byte);
  988. z80->regs.pc++;
  989. return 23;
  990. }
  991. /*
  992. DAA (0x27):
  993. Adjust A for BCD addition and subtraction.
  994. */
  995. static uint8_t z80_inst_daa(Z80 *z80, uint8_t opcode)
  996. {
  997. (void) opcode;
  998. uint8_t a = z80->regs.a, adjust = 0x00;
  999. bool n = get_flag(z80, FLAG_SUBTRACT);
  1000. if ((a & 0x0F) > 0x09 || get_flag(z80, FLAG_HALFCARRY))
  1001. adjust += 0x06;
  1002. uint8_t temp = n ? (a - adjust) : (a + adjust);
  1003. if ((temp >> 4) > 0x09 || get_flag(z80, FLAG_CARRY))
  1004. adjust += 0x60;
  1005. z80->regs.a += n ? -adjust : adjust;
  1006. set_flags_daa(z80, a, adjust);
  1007. z80->regs.pc++;
  1008. return 4;
  1009. }
  1010. /*
  1011. CPL (0x2F):
  1012. Invert A.
  1013. */
  1014. static uint8_t z80_inst_cpl(Z80 *z80, uint8_t opcode)
  1015. {
  1016. (void) opcode;
  1017. z80->regs.a = ~z80->regs.a;
  1018. set_flags_cpl(z80);
  1019. z80->regs.pc++;
  1020. return 4;
  1021. }
  1022. /*
  1023. NEG (0xED44, 0xED4C, 0xED54, 0xED5C, 0xED64, 0xED6C, 0xED74, 0xED7C):
  1024. Negate A.
  1025. */
  1026. static uint8_t z80_inst_neg(Z80 *z80, uint8_t opcode)
  1027. {
  1028. (void) opcode;
  1029. z80->regs.a = -z80->regs.a;
  1030. set_flags_neg(z80);
  1031. z80->regs.pc++;
  1032. return 8;
  1033. }
  1034. /*
  1035. CCF (0x3F):
  1036. Invert the carry flag.
  1037. */
  1038. static uint8_t z80_inst_ccf(Z80 *z80, uint8_t opcode)
  1039. {
  1040. (void) opcode;
  1041. set_flags_ccf(z80);
  1042. z80->regs.pc++;
  1043. return 4;
  1044. }
  1045. /*
  1046. SCF (0x37):
  1047. Set the carry flag.
  1048. */
  1049. static uint8_t z80_inst_scf(Z80 *z80, uint8_t opcode)
  1050. {
  1051. (void) opcode;
  1052. set_flags_scf(z80);
  1053. z80->regs.pc++;
  1054. return 4;
  1055. }
  1056. /*
  1057. NOP (0x00):
  1058. No operation is performed.
  1059. */
  1060. static uint8_t z80_inst_nop(Z80 *z80, uint8_t opcode)
  1061. {
  1062. (void) opcode;
  1063. z80->regs.pc++;
  1064. return 4;
  1065. }
  1066. /*
  1067. HALT (0x76):
  1068. Suspend CPU operation: execute NOPs until an interrupt or reset.
  1069. */
  1070. static uint8_t z80_inst_halt(Z80 *z80, uint8_t opcode)
  1071. {
  1072. (void) z80;
  1073. (void) opcode;
  1074. return 4;
  1075. }
  1076. /*
  1077. DI (0xF3):
  1078. Disable maskable interrupts by resetting both flip-flops.
  1079. */
  1080. static uint8_t z80_inst_di(Z80 *z80, uint8_t opcode)
  1081. {
  1082. (void) opcode;
  1083. z80->regs.iff1 = false;
  1084. z80->regs.iff2 = false;
  1085. z80->regs.pc++;
  1086. return 4;
  1087. }
  1088. /*
  1089. EI (0xFB):
  1090. Enable maskable interrupts by setting both flip-flops.
  1091. */
  1092. static uint8_t z80_inst_ei(Z80 *z80, uint8_t opcode)
  1093. {
  1094. (void) opcode;
  1095. z80->regs.iff1 = true;
  1096. z80->regs.iff2 = true;
  1097. z80->irq_wait = true;
  1098. z80->regs.pc++;
  1099. return 4;
  1100. }
  1101. /*
  1102. IM (0xED46, 0xED4E, 0xED56, 0xED5E, 0xED66, 0xED6E, 0xED76, 0xED7E):
  1103. Set the interrupt mode.
  1104. */
  1105. static uint8_t z80_inst_im(Z80 *z80, uint8_t opcode)
  1106. {
  1107. switch (opcode) {
  1108. case 0x46:
  1109. case 0x4E:
  1110. case 0x66:
  1111. case 0x6E:
  1112. z80->regs.im_a = false; // Interrupt mode 0
  1113. z80->regs.im_b = false;
  1114. break;
  1115. case 0x56:
  1116. case 0x76:
  1117. z80->regs.im_a = true; // Interrupt mode 1
  1118. z80->regs.im_b = false;
  1119. break;
  1120. case 0x5E:
  1121. case 0x7E:
  1122. z80->regs.im_a = true; // Interrupt mode 2
  1123. z80->regs.im_b = true;
  1124. break;
  1125. }
  1126. z80->regs.pc++;
  1127. return 8;
  1128. }
  1129. /*
  1130. ADD HL, ss (0x09, 0x19, 0x29, 0x39):
  1131. Add ss to HL.
  1132. */
  1133. static uint8_t z80_inst_add_hl_ss(Z80 *z80, uint8_t opcode)
  1134. {
  1135. uint16_t lh = z80->regs.hl, rh = *extract_pair(z80, opcode);
  1136. z80->regs.hl += rh;
  1137. set_flags_add16(z80, lh, rh);
  1138. z80->regs.pc++;
  1139. return 11;
  1140. }
  1141. /*
  1142. ADC HL, ss (0xED4A, 0xED5A, 0xED6A, 0xED7A):
  1143. Add ss plus the carry flag to HL.
  1144. */
  1145. static uint8_t z80_inst_adc_hl_ss(Z80 *z80, uint8_t opcode)
  1146. {
  1147. uint16_t lh = z80->regs.hl;
  1148. uint32_t rh = *extract_pair(z80, opcode) + get_flag(z80, FLAG_CARRY);
  1149. z80->regs.hl += rh;
  1150. set_flags_adc16(z80, lh, rh);
  1151. z80->regs.pc++;
  1152. return 15;
  1153. }
  1154. /*
  1155. SBC HL, ss (0xED42, 0xED52, 0xED62, 0xED72):
  1156. Subtract ss with carry from HL.
  1157. */
  1158. static uint8_t z80_inst_sbc_hl_ss(Z80 *z80, uint8_t opcode)
  1159. {
  1160. uint16_t lh = z80->regs.hl;
  1161. uint32_t rh = *extract_pair(z80, opcode) + get_flag(z80, FLAG_CARRY);
  1162. z80->regs.hl -= rh;
  1163. set_flags_sbc16(z80, lh, rh);
  1164. z80->regs.pc++;
  1165. return 15;
  1166. }
  1167. /*
  1168. ADD IXY, pp (0xDD09, 0xDD19, 0xDD29, 0xDD39, 0xFD09, 0xFD19, 0xFD29,
  1169. 0xFD39):
  1170. Add pp to IX or IY.
  1171. */
  1172. static uint8_t z80_inst_add_ixy_pp(Z80 *z80, uint8_t opcode)
  1173. {
  1174. uint16_t lh = *z80->regs.ixy, rh = *extract_pair_pp(z80, opcode);
  1175. *z80->regs.ixy += rh;
  1176. set_flags_add16(z80, lh, rh);
  1177. z80->regs.pc++;
  1178. return 15;
  1179. }
  1180. /*
  1181. INC ss (0x03, 0x13, 0x23, 0x33):
  1182. Increment ss (16-bit register).
  1183. */
  1184. static uint8_t z80_inst_inc_ss(Z80 *z80, uint8_t opcode)
  1185. {
  1186. (*extract_pair(z80, opcode))++;
  1187. z80->regs.pc++;
  1188. return 6;
  1189. }
  1190. /*
  1191. INC IXY (0xDD23, 0xFD23):
  1192. Increment IX or IY.
  1193. */
  1194. static uint8_t z80_inst_inc_xy(Z80 *z80, uint8_t opcode)
  1195. {
  1196. (void) opcode;
  1197. (*z80->regs.ixy)++;
  1198. z80->regs.pc++;
  1199. return 10;
  1200. }
  1201. /*
  1202. DEC ss (0x0B, 0x1B, 0x2B, 0x3B):
  1203. Decrement ss (16-bit register).
  1204. */
  1205. static uint8_t z80_inst_dec_ss(Z80 *z80, uint8_t opcode)
  1206. {
  1207. (*extract_pair(z80, opcode))--;
  1208. z80->regs.pc++;
  1209. return 6;
  1210. }
  1211. /*
  1212. DEC IXY (0xDD2B, 0xFD2B):
  1213. Decrement IX or IY.
  1214. */
  1215. static uint8_t z80_inst_dec_xy(Z80 *z80, uint8_t opcode)
  1216. {
  1217. (void) opcode;
  1218. (*z80->regs.ixy)--;
  1219. z80->regs.pc++;
  1220. return 10;
  1221. }
  1222. /*
  1223. RLCA (0x07):
  1224. Rotate A left one bit. Bit 7 is copied to bit 0 and the carry flag.
  1225. */
  1226. static uint8_t z80_inst_rlca(Z80 *z80, uint8_t opcode)
  1227. {
  1228. (void) opcode;
  1229. uint8_t bit = (z80->regs.a & 0x80) >> 7;
  1230. z80->regs.a <<= 1;
  1231. z80->regs.a |= bit;
  1232. set_flags_bitrota(z80, bit);
  1233. z80->regs.pc++;
  1234. return 4;
  1235. }
  1236. /*
  1237. RLA (0x17):
  1238. Rotate A left one bit. Carry flag is copied to bit 0, and bit 7 is copied
  1239. to the carry flag.
  1240. */
  1241. static uint8_t z80_inst_rla(Z80 *z80, uint8_t opcode)
  1242. {
  1243. (void) opcode;
  1244. uint8_t carry = get_flag(z80, FLAG_CARRY);
  1245. uint8_t bit = (z80->regs.a & 0x80) >> 7;
  1246. z80->regs.a <<= 1;
  1247. z80->regs.a |= carry;
  1248. set_flags_bitrota(z80, bit);
  1249. z80->regs.pc++;
  1250. return 4;
  1251. }
  1252. /*
  1253. RRCA (0x0F):
  1254. Rotate A right one bit. Bit 0 is copied to bit 7 and the carry flag.
  1255. */
  1256. static uint8_t z80_inst_rrca(Z80 *z80, uint8_t opcode)
  1257. {
  1258. (void) opcode;
  1259. uint8_t bit = z80->regs.a & 0x01;
  1260. z80->regs.a >>= 1;
  1261. z80->regs.a |= (bit << 7);
  1262. set_flags_bitrota(z80, bit);
  1263. z80->regs.pc++;
  1264. return 4;
  1265. }
  1266. /*
  1267. RRA (0x1F):
  1268. Rotate A right one bit. Carry flag is copied to bit 7, and bit 0 is copied
  1269. to the carry flag.
  1270. */
  1271. static uint8_t z80_inst_rra(Z80 *z80, uint8_t opcode)
  1272. {
  1273. (void) opcode;
  1274. uint8_t carry = get_flag(z80, FLAG_CARRY);
  1275. uint8_t bit = z80->regs.a & 0x01;
  1276. z80->regs.a >>= 1;
  1277. z80->regs.a |= (carry << 7);
  1278. set_flags_bitrota(z80, bit);
  1279. z80->regs.pc++;
  1280. return 4;
  1281. }
  1282. /*
  1283. RLC r (0xCB00, 0xCB01, 0xCB02, 0xCB03, 0xCB04, 0xCB05, 0xCB07):
  1284. Rotate r left one bit. Bit 7 is copied to bit 0 and the carry flag.
  1285. */
  1286. static uint8_t z80_inst_rlc_r(Z80 *z80, uint8_t opcode)
  1287. {
  1288. uint8_t *reg = extract_reg(z80, opcode << 3);
  1289. uint8_t bit = ((*reg) & 0x80) >> 7;
  1290. (*reg) <<= 1;
  1291. (*reg) |= bit;
  1292. set_flags_bitshift(z80, *reg, bit);
  1293. z80->regs.pc++;
  1294. return 8;
  1295. }
  1296. /*
  1297. RLC (HL) (0xCB06):
  1298. Rotate (HL) left one bit. Bit 7 is copied to bit 0 and the carry flag.
  1299. */
  1300. static uint8_t z80_inst_rlc_hl(Z80 *z80, uint8_t opcode)
  1301. {
  1302. (void) opcode;
  1303. uint8_t val = mmu_read_byte(z80->mmu, z80->regs.hl);
  1304. uint8_t bit = (val & 0x80) >> 7;
  1305. val <<= 1;
  1306. val |= bit;
  1307. mmu_write_byte(z80->mmu, z80->regs.hl, val);
  1308. set_flags_bitshift(z80, val, bit);
  1309. z80->regs.pc++;
  1310. return 15;
  1311. }
  1312. /*
  1313. RLC (IXY+d)
  1314. TODO
  1315. */
  1316. /*
  1317. RL r (0xCB10, 0xCB11, 0xCB12, 0xCB13, 0xCB14, 0xCB15, 0xCB17):
  1318. Rotate r left one bit. Carry flag is copied to bit 0, and bit 7 is copied
  1319. to the carry flag.
  1320. */
  1321. static uint8_t z80_inst_rl_r(Z80 *z80, uint8_t opcode)
  1322. {
  1323. uint8_t *reg = extract_reg(z80, opcode << 3);
  1324. uint8_t carry = get_flag(z80, FLAG_CARRY);
  1325. uint8_t bit = ((*reg) & 0x80) >> 7;
  1326. (*reg) <<= 1;
  1327. (*reg) |= carry;
  1328. set_flags_bitshift(z80, *reg, bit);
  1329. z80->regs.pc++;
  1330. return 8;
  1331. }
  1332. /*
  1333. RL (HL) (0xCB16):
  1334. Rotate (HL) left one bit. Carry flag is copied to bit 0, and bit 7 is
  1335. copied to the carry flag.
  1336. */
  1337. static uint8_t z80_inst_rl_hl(Z80 *z80, uint8_t opcode)
  1338. {
  1339. (void) opcode;
  1340. uint8_t val = mmu_read_byte(z80->mmu, z80->regs.hl);
  1341. uint8_t carry = get_flag(z80, FLAG_CARRY);
  1342. uint8_t bit = (val & 0x80) >> 7;
  1343. val <<= 1;
  1344. val |= carry;
  1345. mmu_write_byte(z80->mmu, z80->regs.hl, val);
  1346. set_flags_bitshift(z80, val, bit);
  1347. z80->regs.pc++;
  1348. return 15;
  1349. }
  1350. /*
  1351. RL (IXY+d)
  1352. TODO
  1353. */
  1354. /*
  1355. RRC r (0xCB08, 0xCB09, 0xCB0A, 0xCB0B, 0xCB0C, 0xCB0D, 0xCB0F):
  1356. Rotate r right one bit. Bit 0 is copied to bit 7 and the carry flag.
  1357. */
  1358. static uint8_t z80_inst_rrc_r(Z80 *z80, uint8_t opcode)
  1359. {
  1360. uint8_t *reg = extract_reg(z80, opcode << 3);
  1361. uint8_t bit = (*reg) & 0x01;
  1362. (*reg) >>= 1;
  1363. (*reg) |= (bit << 7);
  1364. set_flags_bitshift(z80, *reg, bit);
  1365. z80->regs.pc++;
  1366. return 8;
  1367. }
  1368. /*
  1369. RRC (HL) (0xCB0E):
  1370. Rotate (HL) right one bit. Bit 0 is copied to bit 7 and the carry flag.
  1371. */
  1372. static uint8_t z80_inst_rrc_hl(Z80 *z80, uint8_t opcode)
  1373. {
  1374. (void) opcode;
  1375. uint8_t val = mmu_read_byte(z80->mmu, z80->regs.hl);
  1376. uint8_t bit = (val) & 0x01;
  1377. val >>= 1;
  1378. val |= (bit << 7);
  1379. mmu_write_byte(z80->mmu, z80->regs.hl, val);
  1380. set_flags_bitshift(z80, val, bit);
  1381. z80->regs.pc++;
  1382. return 15;
  1383. }
  1384. /*
  1385. RRC (IXY+d)
  1386. TODO
  1387. */
  1388. /*
  1389. RR r (0xCB18, 0xCB19, 0xCB1A, 0xCB1B, 0xCB1C, 0xCB1D, 0xCB1F):
  1390. Rotate r right one bit. Carry flag is copied to bit 7, and bit 0 is copied
  1391. to the carry flag.
  1392. */
  1393. static uint8_t z80_inst_rr_r(Z80 *z80, uint8_t opcode)
  1394. {
  1395. uint8_t *reg = extract_reg(z80, opcode << 3);
  1396. uint8_t carry = get_flag(z80, FLAG_CARRY);
  1397. uint8_t bit = (*reg) & 0x01;
  1398. (*reg) >>= 1;
  1399. (*reg) |= (carry << 7);
  1400. set_flags_bitshift(z80, *reg, bit);
  1401. z80->regs.pc++;
  1402. return 8;
  1403. }
  1404. /*
  1405. RR (HL) (0xCB1E):
  1406. Rotate (HL) right one bit. Carry flag is copied to bit 7, and bit 0 is
  1407. copied to the carry flag.
  1408. */
  1409. static uint8_t z80_inst_rr_hl(Z80 *z80, uint8_t opcode)
  1410. {
  1411. (void) opcode;
  1412. uint8_t val = mmu_read_byte(z80->mmu, z80->regs.hl);
  1413. uint8_t carry = get_flag(z80, FLAG_CARRY);
  1414. uint8_t bit = (val) & 0x01;
  1415. val >>= 1;
  1416. val |= (carry << 7);
  1417. mmu_write_byte(z80->mmu, z80->regs.hl, val);
  1418. set_flags_bitshift(z80, val, bit);
  1419. z80->regs.pc++;
  1420. return 15;
  1421. }
  1422. /*
  1423. RR (IXY+d)
  1424. TODO
  1425. */
  1426. /*
  1427. SLA r (0xCB20, 0xCB21, 0xCB22, 0xCB23, 0xCB24, 0xCB25, 0xCB27):
  1428. Shift r left one bit. 0 is copied to bit 0, and bit 7 is copied to the
  1429. carry flag.
  1430. */
  1431. static uint8_t z80_inst_sla_r(Z80 *z80, uint8_t opcode)
  1432. {
  1433. uint8_t *reg = extract_reg(z80, opcode << 3);
  1434. uint8_t msb = ((*reg) & 0x80) >> 7;
  1435. (*reg) <<= 1;
  1436. set_flags_bitshift(z80, *reg, msb);
  1437. z80->regs.pc++;
  1438. return 8;
  1439. }
  1440. /*
  1441. SLA (HL) (0xCB26):
  1442. Shift (HL) left one bit. 0 is copied to bit 0, and bit 7 is copied to the
  1443. carry flag.
  1444. */
  1445. static uint8_t z80_inst_sla_hl(Z80 *z80, uint8_t opcode)
  1446. {
  1447. (void) opcode;
  1448. uint8_t val = mmu_read_byte(z80->mmu, z80->regs.hl);
  1449. uint8_t msb = (val & 0x80) >> 7;
  1450. val <<= 1;
  1451. mmu_write_byte(z80->mmu, z80->regs.hl, val);
  1452. set_flags_bitshift(z80, val, msb);
  1453. z80->regs.pc++;
  1454. return 15;
  1455. }
  1456. /*
  1457. SLA (IXY+d)
  1458. TODO
  1459. */
  1460. /*
  1461. SRA r (0xCB28, 0xCB29, 0xCB2A, 0xCB2B, 0xCB2C, 0xCB2D, 0xCB2F):
  1462. Arithmetic shift r right one bit. The previous bit 7 is copied to the new
  1463. bit 7, and bit 0 is copied to the carry flag.
  1464. */
  1465. static uint8_t z80_inst_sra_r(Z80 *z80, uint8_t opcode)
  1466. {
  1467. uint8_t *reg = extract_reg(z80, opcode << 3);
  1468. uint8_t msb = (*reg) & 0x80, lsb = (*reg) & 0x01;
  1469. (*reg) >>= 1;
  1470. (*reg) |= msb;
  1471. set_flags_bitshift(z80, *reg, lsb);
  1472. z80->regs.pc++;
  1473. return 8;
  1474. }
  1475. /*
  1476. SRA (HL) (0xCB2E):
  1477. Arithmetic shift (HL) right one bit. The previous bit 7 is copied to the
  1478. new bit 7, and bit 0 is copied to the carry flag.
  1479. */
  1480. static uint8_t z80_inst_sra_hl(Z80 *z80, uint8_t opcode)
  1481. {
  1482. (void) opcode;
  1483. uint8_t val = mmu_read_byte(z80->mmu, z80->regs.hl);
  1484. uint8_t msb = val & 0x80, lsb = val & 0x01;
  1485. val >>= 1;
  1486. val |= msb;
  1487. mmu_write_byte(z80->mmu, z80->regs.hl, val);
  1488. set_flags_bitshift(z80, val, lsb);
  1489. z80->regs.pc++;
  1490. return 8;
  1491. }
  1492. /*
  1493. SRA (IXY+d)
  1494. TODO
  1495. */
  1496. /*
  1497. SL1 r (0xCB30, 0xCB31, 0xCB32, 0xCB33, 0xCB34, 0xCB35, 0xCB37):
  1498. Shift r left one bit. 1 is copied to bit 0, and bit 7 is copied to the
  1499. carry flag.
  1500. */
  1501. static uint8_t z80_inst_sl1_r(Z80 *z80, uint8_t opcode)
  1502. {
  1503. uint8_t *reg = extract_reg(z80, opcode << 3);
  1504. uint8_t msb = ((*reg) & 0x80) >> 7;
  1505. (*reg) <<= 1;
  1506. (*reg) |= 1;
  1507. set_flags_bitshift(z80, *reg, msb);
  1508. z80->regs.pc++;
  1509. return 8;
  1510. }
  1511. /*
  1512. SL1 (HL) (0xCB36):
  1513. Shift (HL) left one bit. 1 is copied to bit 0, and bit 7 is copied to the
  1514. carry flag.
  1515. */
  1516. static uint8_t z80_inst_sl1_hl(Z80 *z80, uint8_t opcode)
  1517. {
  1518. (void) opcode;
  1519. uint8_t val = mmu_read_byte(z80->mmu, z80->regs.hl);
  1520. uint8_t msb = (val & 0x80) >> 7;
  1521. val <<= 1;
  1522. val |= 1;
  1523. mmu_write_byte(z80->mmu, z80->regs.hl, val);
  1524. set_flags_bitshift(z80, val, msb);
  1525. z80->regs.pc++;
  1526. return 15;
  1527. }
  1528. /*
  1529. SL1 (IXY+d)
  1530. TODO
  1531. */
  1532. /*
  1533. SRL r (0xCB38, 0xCB39, 0xCB3A, 0xCB3B, 0xCB3C, 0xCB3D, 0xCB3F):
  1534. Logical shift r right one bit. 0 is copied to bit 7, and bit 0 is copied to
  1535. the carry flag.
  1536. */
  1537. static uint8_t z80_inst_srl_r(Z80 *z80, uint8_t opcode)
  1538. {
  1539. uint8_t *reg = extract_reg(z80, opcode << 3);
  1540. uint8_t lsb = (*reg) & 0x01;
  1541. (*reg) >>= 1;
  1542. set_flags_bitshift(z80, *reg, lsb);
  1543. z80->regs.pc++;
  1544. return 8;
  1545. }
  1546. /*
  1547. SRL (HL) (0xCB3E):
  1548. Logical shift (HL) right one bit. 0 is copied to bit 7, and bit 0 is copied
  1549. to the carry flag.
  1550. */
  1551. static uint8_t z80_inst_srl_hl(Z80 *z80, uint8_t opcode)
  1552. {
  1553. (void) opcode;
  1554. uint8_t val = mmu_read_byte(z80->mmu, z80->regs.hl);
  1555. uint8_t lsb = val & 0x01;
  1556. val >>= 1;
  1557. mmu_write_byte(z80->mmu, z80->regs.hl, val);
  1558. set_flags_bitshift(z80, val, lsb);
  1559. z80->regs.pc++;
  1560. return 8;
  1561. }
  1562. /*
  1563. SRL (IXY+d)
  1564. TODO
  1565. */
  1566. /*
  1567. RLD (0xED6F):
  1568. Low nibble of (HL) is copied to high nibble of (HL). Old high nibble of
  1569. (HL) is copied to low nibble of A. Old low nibble of A is copied to low
  1570. nibble of (HL).
  1571. */
  1572. static uint8_t z80_inst_rld(Z80 *z80, uint8_t opcode)
  1573. {
  1574. (void) opcode;
  1575. uint8_t hl = mmu_read_byte(z80->mmu, z80->regs.hl);
  1576. uint8_t newhl = (hl << 4) | (z80->regs.a & 0x0F);
  1577. z80->regs.a = (z80->regs.a & 0xF0) | (hl >> 4);
  1578. mmu_write_byte(z80->mmu, z80->regs.hl, newhl);
  1579. set_flags_rd(z80);
  1580. return 18;
  1581. }
  1582. /*
  1583. RRD (0xED67):
  1584. Low nibble of A is copied to high nibble of (HL). Old high nibble of
  1585. (HL) is copied to low nibble of (HL). Old low nibble of (HL) is copied to
  1586. low nibble of A.
  1587. */
  1588. static uint8_t z80_inst_rrd(Z80 *z80, uint8_t opcode)
  1589. {
  1590. (void) opcode;
  1591. uint8_t hl = mmu_read_byte(z80->mmu, z80->regs.hl);
  1592. uint8_t newhl = (z80->regs.a << 4) | (hl >> 4);
  1593. z80->regs.a = (z80->regs.a & 0xF0) | (hl & 0x0F);
  1594. mmu_write_byte(z80->mmu, z80->regs.hl, newhl);
  1595. set_flags_rd(z80);
  1596. return 18;
  1597. }
  1598. /*
  1599. BIT b, r (0xCB40, 0xCB41, 0xCB42, 0xCB43, 0xCB44, 0xCB45, 0xCB47, 0xCB48,
  1600. 0xCB49, 0xCB4A, 0xCB4B, 0xCB4C, 0xCB4D, 0xCB4F, 0xCB50, 0xCB51, 0xCB52,
  1601. 0xCB53, 0xCB54, 0xCB55, 0xCB57, 0xCB58, 0xCB59, 0xCB5A, 0xCB5B, 0xCB5C,
  1602. 0xCB5D, 0xCB5F, 0xCB60, 0xCB61, 0xCB62, 0xCB63, 0xCB64, 0xCB65, 0xCB67,
  1603. 0xCB68, 0xCB69, 0xCB6A, 0xCB6B, 0xCB6C, 0xCB6D, 0xCB6F, 0xCB70, 0xCB71,
  1604. 0xCB72, 0xCB73, 0xCB74, 0xCB75, 0xCB77, 0xCB78, 0xCB79, 0xCB7A, 0xCB7B,
  1605. 0xCB7C, 0xCB7D, 0xCB7F):
  1606. Test bit b of r (8-bit register).
  1607. */
  1608. static uint8_t z80_inst_bit_b_r(Z80 *z80, uint8_t opcode)
  1609. {
  1610. uint8_t val = *extract_reg(z80, opcode << 3);
  1611. uint8_t bit = (opcode >> 3) & 0x07;
  1612. set_flags_bit(z80, val, bit);
  1613. z80->regs.pc++;
  1614. return 8;
  1615. }
  1616. /*
  1617. BIT b, (HL) (0xCB46, 0xCB4E, 0xCB56, 0xCB5E, 0xCB66, 0xCB6E, 0xCB76,
  1618. 0xCB7E):
  1619. Test bit b of (HL).
  1620. */
  1621. static uint8_t z80_inst_bit_b_hl(Z80 *z80, uint8_t opcode)
  1622. {
  1623. uint8_t val = mmu_read_byte(z80->mmu, z80->regs.hl);
  1624. uint8_t bit = (opcode >> 3) & 0x07;
  1625. set_flags_bit(z80, val, bit);
  1626. z80->regs.pc++;
  1627. return 12;
  1628. }
  1629. /*
  1630. BIT b, (IXY+d) (0xDDCB40-0xDDCB7F, 0xFDCB40-0xFDCB7F):
  1631. Test bit b of (IX+d) or (IY+d).
  1632. */
  1633. static uint8_t z80_inst_bit_b_ixy(Z80 *z80, uint8_t opcode)
  1634. {
  1635. uint16_t addr = get_index_addr(z80, z80->regs.pc - 1);
  1636. uint8_t val = mmu_read_byte(z80->mmu, addr);
  1637. uint8_t bit = (opcode >> 3) & 0x07;
  1638. set_flags_bit(z80, val, bit);
  1639. z80->regs.pc++;
  1640. return 20;
  1641. }
  1642. /*
  1643. SET b, r (0xCBC0, 0xCBC1, 0xCBC2, 0xCBC3, 0xCBC4, 0xCBC5, 0xCBC7, 0xCBC8,
  1644. 0xCBC9, 0xCBCA, 0xCBCB, 0xCBCC, 0xCBCD, 0xCBCF, 0xCBD0, 0xCBD1, 0xCBD2,
  1645. 0xCBD3, 0xCBD4, 0xCBD5, 0xCBD7, 0xCBD8, 0xCBD9, 0xCBDA, 0xCBDB, 0xCBDC,
  1646. 0xCBDD, 0xCBDF, 0xCBE0, 0xCBE1, 0xCBE2, 0xCBE3, 0xCBE4, 0xCBE5, 0xCBE7,
  1647. 0xCBE8, 0xCBE9, 0xCBEA, 0xCBEB, 0xCBEC, 0xCBED, 0xCBEF, 0xCBF0, 0xCBF1,
  1648. 0xCBF2, 0xCBF3, 0xCBF4, 0xCBF5, 0xCBF7, 0xCBF8, 0xCBF9, 0xCBFA, 0xCBFB,
  1649. 0xCBFC, 0xCBFD, 0xCBFF):
  1650. Set bit b of r.
  1651. */
  1652. static uint8_t z80_inst_set_b_r(Z80 *z80, uint8_t opcode)
  1653. {
  1654. uint8_t *reg = extract_reg(z80, opcode << 3);
  1655. uint8_t bit = (opcode >> 3) & 0x07;
  1656. *reg |= 1 << bit;
  1657. z80->regs.pc++;
  1658. return 8;
  1659. }
  1660. /*
  1661. SET b, (HL) (0xCBC6, 0xCBCE, 0xCBD6, 0xCBDE, 0xCBE6, 0xCBEE, 0xCBF6,
  1662. 0xCBFE):
  1663. Reset bit b of (HL).
  1664. */
  1665. static uint8_t z80_inst_set_b_hl(Z80 *z80, uint8_t opcode)
  1666. {
  1667. uint8_t val = mmu_read_byte(z80->mmu, z80->regs.hl);
  1668. uint8_t bit = (opcode >> 3) & 0x07;
  1669. val |= 1 << bit;
  1670. mmu_write_byte(z80->mmu, z80->regs.hl, val);
  1671. z80->regs.pc++;
  1672. return 15;
  1673. }
  1674. /*
  1675. SET b, (IXY+d) (0xDDCBC6, 0xDDCBCE, 0xDDCBD6, 0xDDCBDE, 0xDDCBE6, 0xDDCBEE,
  1676. 0xDDCBF6, 0xDDCBFE, 0xFDCBC6, 0xFDCBCE, 0xFDCBD6, 0xFDCBDE, 0xFDCBE6,
  1677. 0xFDCBEE, 0xFDCBF6, 0xFDCBFE):
  1678. Set bit b of (IX+d) or (IY+d).
  1679. */
  1680. static uint8_t z80_inst_set_b_ixy(Z80 *z80, uint8_t opcode)
  1681. {
  1682. uint16_t addr = get_index_addr(z80, z80->regs.pc - 1);
  1683. uint8_t val = mmu_read_byte(z80->mmu, addr);
  1684. uint8_t bit = (opcode >> 3) & 0x07;
  1685. val |= 1 << bit;
  1686. mmu_write_byte(z80->mmu, addr, val);
  1687. z80->regs.pc++;
  1688. return 15;
  1689. }
  1690. /*
  1691. RES b, r (0xCB80, 0xCB81, 0xCB82, 0xCB83, 0xCB84, 0xCB85, 0xCB87, 0xCB88,
  1692. 0xCB89, 0xCB8A, 0xCB8B, 0xCB8C, 0xCB8D, 0xCB8F, 0xCB90, 0xCB91, 0xCB92,
  1693. 0xCB93, 0xCB94, 0xCB95, 0xCB97, 0xCB98, 0xCB99, 0xCB9A, 0xCB9B, 0xCB9C,
  1694. 0xCB9D, 0xCB9F, 0xCBA0, 0xCBA1, 0xCBA2, 0xCBA3, 0xCBA4, 0xCBA5, 0xCBA7,
  1695. 0xCBA8, 0xCBA9, 0xCBAA, 0xCBAB, 0xCBAC, 0xCBAD, 0xCBAF, 0xCBB0, 0xCBB1,
  1696. 0xCBB2, 0xCBB3, 0xCBB4, 0xCBB5, 0xCBB7, 0xCBB8, 0xCBB9, 0xCBBA, 0xCBBB,
  1697. 0xCBBC, 0xCBBD, 0xCBBF):
  1698. Reset bit b of r.
  1699. */
  1700. static uint8_t z80_inst_res_b_r(Z80 *z80, uint8_t opcode)
  1701. {
  1702. uint8_t *reg = extract_reg(z80, opcode << 3);
  1703. uint8_t bit = (opcode >> 3) & 0x07;
  1704. *reg &= ~(1 << bit);
  1705. z80->regs.pc++;
  1706. return 8;
  1707. }
  1708. /*
  1709. RES b, (HL) (0xCB86, 0xCB8E, 0xCB96, 0xCB9E, 0xCBA6, 0xCBAE, 0xCBB6,
  1710. 0xCBBE):
  1711. Reset bit b of (HL).
  1712. */
  1713. static uint8_t z80_inst_res_b_hl(Z80 *z80, uint8_t opcode)
  1714. {
  1715. uint8_t val = mmu_read_byte(z80->mmu, z80->regs.hl);
  1716. uint8_t bit = (opcode >> 3) & 0x07;
  1717. val &= ~(1 << bit);
  1718. mmu_write_byte(z80->mmu, z80->regs.hl, val);
  1719. z80->regs.pc++;
  1720. return 15;
  1721. }
  1722. /*
  1723. RES b, (IXY+d) (0xDDCB86, 0xDDCB8E, 0xDDCB96, 0xDDCB9E, 0xDDCBA6, 0xDDCBAE,
  1724. 0xDDCBB6, 0xDDCBBE, 0xFDCB86, 0xFDCB8E, 0xFDCBA6, 0xFDCBAE, 0xFDCBB6,
  1725. 0xFDCBBE, 0xFDCBC6, 0xFDCBCE):
  1726. Set bit b of (IX+d) or (IY+d).
  1727. */
  1728. static uint8_t z80_inst_res_b_ixy(Z80 *z80, uint8_t opcode)
  1729. {
  1730. uint16_t addr = get_index_addr(z80, z80->regs.pc - 1);
  1731. uint8_t val = mmu_read_byte(z80->mmu, addr);
  1732. uint8_t bit = (opcode >> 3) & 0x07;
  1733. val &= ~(1 << bit);
  1734. mmu_write_byte(z80->mmu, addr, val);
  1735. z80->regs.pc++;
  1736. return 15;
  1737. }
  1738. /*
  1739. JP nn (0xC3):
  1740. Jump to nn (16-bit immediate).
  1741. */
  1742. static uint8_t z80_inst_jp_nn(Z80 *z80, uint8_t opcode)
  1743. {
  1744. (void) opcode;
  1745. z80->regs.pc = mmu_read_double(z80->mmu, ++z80->regs.pc);
  1746. return 10;
  1747. }
  1748. /*
  1749. JP cc, nn (0xC2, 0xCA, 0xD2, 0xDA, 0xE2, 0xEA, 0xF2, 0xFA):
  1750. Jump to nn (16-bit immediate) if cc (condition) is true.
  1751. */
  1752. static uint8_t z80_inst_jp_cc_nn(Z80 *z80, uint8_t opcode)
  1753. {
  1754. if (extract_cond(z80, opcode))
  1755. z80->regs.pc = mmu_read_double(z80->mmu, ++z80->regs.pc);
  1756. else
  1757. z80->regs.pc += 3;
  1758. return 10;
  1759. }
  1760. /*
  1761. JR e (0x18):
  1762. Relative jump e (signed 8-bit immediate) bytes.
  1763. */
  1764. static uint8_t z80_inst_jr_e(Z80 *z80, uint8_t opcode)
  1765. {
  1766. (void) opcode;
  1767. int8_t jump = mmu_read_byte(z80->mmu, z80->regs.pc + 1);
  1768. z80->regs.pc += jump + 2;
  1769. return 12;
  1770. }
  1771. /*
  1772. JR cc, e (0x20, 0x28, 0x30, 0x38):
  1773. Relative jump e (signed 8-bit immediate) bytes if cc (condition) is true.
  1774. */
  1775. static uint8_t z80_inst_jr_cc_e(Z80 *z80, uint8_t opcode)
  1776. {
  1777. if (extract_cond(z80, opcode - 0x20)) {
  1778. int8_t jump = mmu_read_byte(z80->mmu, z80->regs.pc + 1);
  1779. z80->regs.pc += jump + 2;
  1780. return 12;
  1781. } else {
  1782. z80->regs.pc += 2;
  1783. return 7;
  1784. }
  1785. }
  1786. /*
  1787. JP (HL) (0xE9):
  1788. Jump to HL (*NOT* the memory pointed to by HL).
  1789. */
  1790. static uint8_t z80_inst_jp_hl(Z80 *z80, uint8_t opcode)
  1791. {
  1792. (void) opcode;
  1793. z80->regs.pc = z80->regs.hl;
  1794. return 4;
  1795. }
  1796. /*
  1797. JP (IXY) (0xDDE9, 0xFDE9):
  1798. Jump to IX or IY.
  1799. */
  1800. static uint8_t z80_inst_jp_ixy(Z80 *z80, uint8_t opcode)
  1801. {
  1802. (void) opcode;
  1803. z80->regs.pc = *z80->regs.ixy;
  1804. return 8;
  1805. }
  1806. /*
  1807. DJNZ, e (0x10):
  1808. Decrement B and relative jump e bytes (signed 8-bit immediate) if non-zero.
  1809. */
  1810. static uint8_t z80_inst_djnz_e(Z80 *z80, uint8_t opcode)
  1811. {
  1812. (void) opcode;
  1813. z80->regs.b--;
  1814. if (z80->regs.b != 0) {
  1815. int8_t jump = mmu_read_byte(z80->mmu, z80->regs.pc + 1);
  1816. z80->regs.pc += jump + 2;
  1817. return 13;
  1818. } else {
  1819. z80->regs.pc += 2;
  1820. return 8;
  1821. }
  1822. }
  1823. /*
  1824. CALL nn (0xCD):
  1825. Push PC+3 onto the stack and jump to nn (16-bit immediate).
  1826. */
  1827. static uint8_t z80_inst_call_nn(Z80 *z80, uint8_t opcode)
  1828. {
  1829. (void) opcode;
  1830. stack_push(z80, z80->regs.pc + 3);
  1831. z80->regs.pc = mmu_read_double(z80->mmu, ++z80->regs.pc);
  1832. return 17;
  1833. }
  1834. /*
  1835. CALL cc, nn (0xC4, 0xCC, 0xD4, 0xDC, 0xE4, 0xEC, 0xF4, 0xFC):
  1836. Push PC+3 onto the stack and jump to nn (16-bit immediate) if cc is true.
  1837. */
  1838. static uint8_t z80_inst_call_cc_nn(Z80 *z80, uint8_t opcode)
  1839. {
  1840. if (extract_cond(z80, opcode)) {
  1841. stack_push(z80, z80->regs.pc + 3);
  1842. z80->regs.pc = mmu_read_double(z80->mmu, ++z80->regs.pc);
  1843. return 17;
  1844. } else {
  1845. z80->regs.pc += 3;
  1846. return 10;
  1847. }
  1848. }
  1849. /*
  1850. RET (0xC9):
  1851. Pop PC from the stack.
  1852. */
  1853. static uint8_t z80_inst_ret(Z80 *z80, uint8_t opcode)
  1854. {
  1855. (void) opcode;
  1856. z80->regs.pc = stack_pop(z80);
  1857. return 10;
  1858. }
  1859. /*
  1860. RET cc (0xC0, 0xC8, 0xD0, 0xD8, 0xE0, 0xE8, 0xF0, 0xF8):
  1861. Pop PC from the stack if cc is true.
  1862. */
  1863. static uint8_t z80_inst_ret_cc(Z80 *z80, uint8_t opcode)
  1864. {
  1865. if (extract_cond(z80, opcode)) {
  1866. z80->regs.pc = stack_pop(z80);
  1867. return 11;
  1868. } else {
  1869. z80->regs.pc++;
  1870. return 5;
  1871. }
  1872. }
  1873. /*
  1874. RETI (0xED4D):
  1875. Pop PC from the stack.
  1876. */
  1877. static uint8_t z80_inst_reti(Z80 *z80, uint8_t opcode)
  1878. {
  1879. (void) opcode;
  1880. z80->regs.pc = stack_pop(z80);
  1881. return 14;
  1882. }
  1883. /*
  1884. RETN (0xED45, 0xED55, 0xED5D, 0xED65, 0xED6D, 0xED75, 0xED7D):
  1885. Pop PC from the stack, and copy to IFF2 to IFF1.
  1886. */
  1887. static uint8_t z80_inst_retn(Z80 *z80, uint8_t opcode)
  1888. {
  1889. (void) opcode;
  1890. z80->regs.pc = stack_pop(z80);
  1891. z80->regs.iff1 = z80->regs.iff2;
  1892. return 14;
  1893. }
  1894. /*
  1895. RST p (0xC7, 0xCF, 0xD7, 0xDF, 0xE7, 0xEF, 0xF7, 0xFF):
  1896. Push PC+1 onto the stack and jump to p (opcode & 0x38).
  1897. */
  1898. static uint8_t z80_inst_rst_p(Z80 *z80, uint8_t opcode)
  1899. {
  1900. stack_push(z80, z80->regs.pc + 1);
  1901. z80->regs.pc = opcode & 0x38;
  1902. return 11;
  1903. }
  1904. /*
  1905. IN A, (n) (0xDB):
  1906. Read a byte from port n into A.
  1907. */
  1908. static uint8_t z80_inst_in_a_n(Z80 *z80, uint8_t opcode)
  1909. {
  1910. (void) opcode;
  1911. uint8_t port = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  1912. z80->regs.a = io_port_read(z80->io, port);
  1913. z80->regs.pc++;
  1914. return 11;
  1915. }
  1916. /*
  1917. IN r, (C) (0xED40, 0xED48, 0xED50, 0xED58, 0xED60, 0xED68, 0xED70, 0xED78):
  1918. Read a byte from port C into r, or affect flags only if 0xED70.
  1919. */
  1920. static uint8_t z80_inst_in_r_c(Z80 *z80, uint8_t opcode)
  1921. {
  1922. uint8_t data = io_port_read(z80->io, z80->regs.c);
  1923. if (opcode != 0x70)
  1924. *extract_reg(z80, opcode) = data;
  1925. set_flags_in(z80, data);
  1926. z80->regs.pc++;
  1927. return 12;
  1928. }
  1929. /*
  1930. INI (0xEDA2):
  1931. IN (HL), (C); INC HL; DEC B
  1932. */
  1933. static uint8_t z80_inst_ini(Z80 *z80, uint8_t opcode)
  1934. {
  1935. (void) opcode;
  1936. uint8_t data = io_port_read(z80->io, z80->regs.c);
  1937. mmu_write_byte(z80->mmu, z80->regs.hl, data);
  1938. set_flags_blockio(z80);
  1939. z80->regs.hl++;
  1940. z80->regs.b--;
  1941. z80->regs.pc++;
  1942. return 16;
  1943. }
  1944. /*
  1945. INIR (0xEDB2):
  1946. INI; JR NZ, -2
  1947. */
  1948. static uint8_t z80_inst_inir(Z80 *z80, uint8_t opcode)
  1949. {
  1950. z80_inst_ini(z80, opcode);
  1951. if (z80->regs.b == 0)
  1952. return 16;
  1953. z80->regs.pc -= 2;
  1954. return 21;
  1955. }
  1956. /*
  1957. IND (0xEDAA):
  1958. IN (HL), (C); DEC HL; DEC B
  1959. */
  1960. static uint8_t z80_inst_ind(Z80 *z80, uint8_t opcode)
  1961. {
  1962. (void) opcode;
  1963. uint8_t data = io_port_read(z80->io, z80->regs.c);
  1964. mmu_write_byte(z80->mmu, z80->regs.hl, data);
  1965. set_flags_blockio(z80);
  1966. z80->regs.hl--;
  1967. z80->regs.b--;
  1968. z80->regs.pc++;
  1969. return 16;
  1970. }
  1971. /*
  1972. INDR (0xEDBA):
  1973. IND; JR NZ, -2
  1974. */
  1975. static uint8_t z80_inst_indr(Z80 *z80, uint8_t opcode)
  1976. {
  1977. z80_inst_ind(z80, opcode);
  1978. if (z80->regs.b == 0)
  1979. return 16;
  1980. z80->regs.pc -= 2;
  1981. return 21;
  1982. }
  1983. /*
  1984. OUT (n), A (0xD3):
  1985. Write a byte from A into port n.
  1986. */
  1987. static uint8_t z80_inst_out_n_a(Z80 *z80, uint8_t opcode)
  1988. {
  1989. (void) opcode;
  1990. uint8_t port = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  1991. io_port_write(z80->io, port, z80->regs.a);
  1992. z80->regs.pc++;
  1993. return 11;
  1994. }
  1995. /*
  1996. OUT (C), r (0xED41, 0xED49, 0xED51, 0xED59, 0xED61, 0xED69, 0xED71,
  1997. 0xED79):
  1998. Write a byte from r (8-bit reg, or 0 if 0xED71) into port C.
  1999. */
  2000. static uint8_t z80_inst_out_c_r(Z80 *z80, uint8_t opcode)
  2001. {
  2002. uint8_t value = opcode != 0x71 ? *extract_reg(z80, opcode) : 0;
  2003. io_port_write(z80->io, z80->regs.c, value);
  2004. z80->regs.pc++;
  2005. return 12;
  2006. }
  2007. /*
  2008. OUTI (0xEDA3):
  2009. OUT (C), (HL); INC HL; DEC B
  2010. */
  2011. static uint8_t z80_inst_outi(Z80 *z80, uint8_t opcode)
  2012. {
  2013. (void) opcode;
  2014. uint8_t data = mmu_read_byte(z80->mmu, z80->regs.hl);
  2015. io_port_write(z80->io, z80->regs.c, data);
  2016. set_flags_blockio(z80);
  2017. z80->regs.hl++;
  2018. z80->regs.b--;
  2019. z80->regs.pc++;
  2020. return 16;
  2021. }
  2022. /*
  2023. OTIR (0xEDB3):
  2024. OUTI; JR NZ, -2
  2025. */
  2026. static uint8_t z80_inst_otir(Z80 *z80, uint8_t opcode)
  2027. {
  2028. z80_inst_outi(z80, opcode);
  2029. if (z80->regs.b == 0)
  2030. return 16;
  2031. z80->regs.pc -= 2;
  2032. return 21;
  2033. }
  2034. /*
  2035. OUTD (0xEDAB):
  2036. OUT (C), (HL); DEC HL; DEC B
  2037. */
  2038. static uint8_t z80_inst_outd(Z80 *z80, uint8_t opcode)
  2039. {
  2040. (void) opcode;
  2041. uint8_t data = mmu_read_byte(z80->mmu, z80->regs.hl);
  2042. io_port_write(z80->io, z80->regs.c, data);
  2043. set_flags_blockio(z80);
  2044. z80->regs.hl--;
  2045. z80->regs.b--;
  2046. z80->regs.pc++;
  2047. return 16;
  2048. }
  2049. /*
  2050. OTDR (0xEDBB):
  2051. OUTD; JR NZ, -2
  2052. */
  2053. static uint8_t z80_inst_otdr(Z80 *z80, uint8_t opcode)
  2054. {
  2055. z80_inst_outd(z80, opcode);
  2056. if (z80->regs.b == 0)
  2057. return 16;
  2058. z80->regs.pc -= 2;
  2059. return 21;
  2060. }
  2061. /*
  2062. NOP2:
  2063. No operation is performed twice; i.e., 2 NOPs-worth of cycles are spent.
  2064. Used for unimplemented extended and index instructions.
  2065. */
  2066. static uint8_t z80_inst_nop2(Z80 *z80, uint8_t opcode)
  2067. {
  2068. (void) opcode;
  2069. z80->regs.pc++;
  2070. return 8;
  2071. }
  2072. /*
  2073. 0xED:
  2074. Handle an extended instruction.
  2075. */
  2076. static uint8_t z80_prefix_extended(Z80 *z80, uint8_t opcode)
  2077. {
  2078. opcode = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  2079. return (*instruction_table_extended[opcode])(z80, opcode);
  2080. }
  2081. /*
  2082. 0xED:
  2083. Handle a bit instruction.
  2084. */
  2085. static uint8_t z80_prefix_bits(Z80 *z80, uint8_t opcode)
  2086. {
  2087. opcode = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  2088. return (*instruction_table_bits[opcode])(z80, opcode);
  2089. }
  2090. /*
  2091. 0xDD, 0xFD:
  2092. Handle an index instruction.
  2093. */
  2094. static uint8_t z80_prefix_index(Z80 *z80, uint8_t opcode)
  2095. {
  2096. if (opcode == 0xDD) {
  2097. z80->regs.ixy = &z80->regs.ix;
  2098. z80->regs.ih = &z80->regs.ixh;
  2099. z80->regs.il = &z80->regs.ixl;
  2100. } else {
  2101. z80->regs.ixy = &z80->regs.iy;
  2102. z80->regs.ih = &z80->regs.iyh;
  2103. z80->regs.il = &z80->regs.iyl;
  2104. }
  2105. opcode = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  2106. return (*instruction_table_index[opcode])(z80, opcode);
  2107. }
  2108. /*
  2109. 0xDDCB, 0xFDCB:
  2110. Handle an index-bit instruction.
  2111. */
  2112. static uint8_t z80_prefix_index_bits(Z80 *z80, uint8_t opcode)
  2113. {
  2114. opcode = mmu_read_byte(z80->mmu, z80->regs.pc += 2);
  2115. return (*instruction_table_index_bits[opcode])(z80, opcode);
  2116. }
  2117. #include "z80_tables.inc.c"