An emulator, assembler, and disassembler for the Sega Game Gear
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

z80_ops.inc.c 35 KiB

8年前
8年前
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  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->regfile.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->regfile.pc);
  52. z80->regfile.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, get_pair(z80, REG_HL));
  63. z80->regfile.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->regfile.pc);
  75. *reg = mmu_read_byte(z80->mmu, addr);
  76. z80->regfile.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. uint16_t addr = get_pair(z80, REG_HL);
  87. mmu_write_byte(z80->mmu, addr, *reg);
  88. z80->regfile.pc++;
  89. return 7;
  90. }
  91. /*
  92. LD (IXY+d), r (0xDD70, 0xDD71, 0xDD72, 0xDD73, 0xDD74, 0xDD75, 0xDD77,
  93. 0xFD70, 0xFD71, 0xFD72, 0xFD73, 0xFD74, 0xFD75, 0xFD77):
  94. Load r (8-bit register) into (IX+d) or (IY+d).
  95. */
  96. static uint8_t z80_inst_ld_ixy_r(Z80 *z80, uint8_t opcode)
  97. {
  98. uint8_t *reg = extract_reg(z80, opcode << 3);
  99. uint16_t addr = get_index_addr(z80, ++z80->regfile.pc);
  100. mmu_write_byte(z80->mmu, addr, *reg);
  101. z80->regfile.pc++;
  102. return 19;
  103. }
  104. /*
  105. LD (HL), n (0x36):
  106. Load n (8-bit immediate) into the memory address pointed to by HL.
  107. */
  108. static uint8_t z80_inst_ld_hl_n(Z80 *z80, uint8_t opcode)
  109. {
  110. (void) opcode;
  111. uint16_t addr = get_pair(z80, REG_HL);
  112. uint8_t byte = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  113. mmu_write_byte(z80->mmu, addr, byte);
  114. z80->regfile.pc++;
  115. return 10;
  116. }
  117. /*
  118. LD (IXY+d), n (0xDD36, 0xFD36):
  119. Load n (8-bit immediate) into (IX+d) or (IY+d).
  120. */
  121. static uint8_t z80_inst_ld_ixy_n(Z80 *z80, uint8_t opcode)
  122. {
  123. (void) opcode;
  124. uint16_t addr = get_index_addr(z80, ++z80->regfile.pc);
  125. uint8_t byte = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  126. mmu_write_byte(z80->mmu, addr, byte);
  127. z80->regfile.pc++;
  128. return 19;
  129. }
  130. /*
  131. LD A, (BC/DE)
  132. */
  133. // static uint8_t z80_inst_ld_a_bcde(Z80 *z80, uint8_t opcode)
  134. /*
  135. LD A, (nn) (0x3A):
  136. Load memory at address nn into A.
  137. */
  138. static uint8_t z80_inst_ld_a_nn(Z80 *z80, uint8_t opcode)
  139. {
  140. (void) opcode;
  141. uint16_t addr = mmu_read_double(z80->mmu, ++z80->regfile.pc);
  142. z80->regfile.a = mmu_read_byte(z80->mmu, addr);
  143. z80->regfile.pc += 2;
  144. return 13;
  145. }
  146. /*
  147. LD (BC/DE), A (0x02, 0x12):
  148. Load A into the memory address pointed to by BC or DE.
  149. */
  150. static uint8_t z80_inst_ld_bcde_a(Z80 *z80, uint8_t opcode)
  151. {
  152. uint16_t addr = get_pair(z80, extract_pair(opcode));
  153. mmu_write_byte(z80->mmu, addr, z80->regfile.a);
  154. z80->regfile.pc++;
  155. return 7;
  156. }
  157. /*
  158. LD (nn), A (0x32):
  159. Load A into memory address nn.
  160. */
  161. static uint8_t z80_inst_ld_nn_a(Z80 *z80, uint8_t opcode)
  162. {
  163. (void) opcode;
  164. uint16_t addr = mmu_read_double(z80->mmu, ++z80->regfile.pc);
  165. mmu_write_byte(z80->mmu, addr, z80->regfile.a);
  166. z80->regfile.pc += 2;
  167. return 13;
  168. }
  169. /*
  170. LD A, I
  171. */
  172. // static uint8_t z80_inst_ld_a_i(Z80 *z80, uint8_t opcode)
  173. /*
  174. LD A, R
  175. */
  176. // static uint8_t z80_inst_ld_a_r(Z80 *z80, uint8_t opcode)
  177. /*
  178. LD I, A
  179. */
  180. // static uint8_t z80_inst_ld_i_a(Z80 *z80, uint8_t opcode)
  181. /*
  182. LD R, A
  183. */
  184. // static uint8_t z80_inst_ld_r_a(Z80 *z80, uint8_t opcode)
  185. /*
  186. LD dd, nn (0x01, 0x11, 0x21, 0x31):
  187. Load nn (16-bit immediate) into dd (16-bit register).
  188. */
  189. static uint8_t z80_inst_ld_dd_nn(Z80 *z80, uint8_t opcode)
  190. {
  191. uint8_t pair = extract_pair(opcode);
  192. set_pair(z80, pair, mmu_read_double(z80->mmu, ++z80->regfile.pc));
  193. z80->regfile.pc += 2;
  194. return 10;
  195. }
  196. /*
  197. LD IXY, nn (0xDD21, 0xFD21):
  198. Load nn (16-bit immediate) into IX or IY.
  199. */
  200. static uint8_t z80_inst_ld_ixy_nn(Z80 *z80, uint8_t opcode)
  201. {
  202. (void) opcode;
  203. *z80->last_index = mmu_read_double(z80->mmu, ++z80->regfile.pc);
  204. z80->regfile.pc += 2;
  205. return 14;
  206. }
  207. /*
  208. LD HL, (nn) (0x2A):
  209. Load memory at address nn into HL.
  210. */
  211. static uint8_t z80_inst_ld_hl_nn(Z80 *z80, uint8_t opcode)
  212. {
  213. (void) opcode;
  214. uint16_t addr = mmu_read_double(z80->mmu, ++z80->regfile.pc);
  215. uint16_t value = mmu_read_double(z80->mmu, addr);
  216. set_pair(z80, REG_HL, value);
  217. z80->regfile.pc += 2;
  218. return 16;
  219. }
  220. /*
  221. LD dd, (nn) (0xED4B, 0xED5B, 0xED6B, 0xED7B):
  222. Load memory at address nn into dd (16-bit register).
  223. */
  224. static uint8_t z80_inst_ld_dd_inn(Z80 *z80, uint8_t opcode)
  225. {
  226. (void) opcode;
  227. uint16_t addr = mmu_read_double(z80->mmu, ++z80->regfile.pc);
  228. uint16_t value = mmu_read_double(z80->mmu, addr);
  229. set_pair(z80, extract_pair(opcode), value);
  230. z80->regfile.pc += 2;
  231. return 20;
  232. }
  233. // LD IXY, (nn)
  234. // static uint8_t z80_inst_ld_ixy_inn(Z80 *z80, uint8_t opcode)
  235. /*
  236. LD (nn), HL: (0x22):
  237. Load HL into memory address nn.
  238. */
  239. static uint8_t z80_inst_ld_nn_hl(Z80 *z80, uint8_t opcode)
  240. {
  241. (void) opcode;
  242. uint16_t addr = mmu_read_double(z80->mmu, ++z80->regfile.pc);
  243. mmu_write_double(z80->mmu, addr, get_pair(z80, REG_HL));
  244. z80->regfile.pc += 2;
  245. return 16;
  246. }
  247. /*
  248. LD (nn), dd (0xED43, 0xED53, 0xED63, 0xED73);
  249. Load dd (16-bit register) into memory address nn.
  250. */
  251. static uint8_t z80_inst_ld_nn_dd(Z80 *z80, uint8_t opcode)
  252. {
  253. (void) opcode;
  254. uint16_t addr = mmu_read_double(z80->mmu, ++z80->regfile.pc);
  255. mmu_write_double(z80->mmu, addr, extract_pair(opcode));
  256. z80->regfile.pc += 2;
  257. return 16;
  258. }
  259. // LD (nn), IXY
  260. // LD SP, HL
  261. // LD SP, IXY
  262. /*
  263. PUSH qq (0xC5, 0xD5, 0xE5, 0xF5):
  264. Push qq onto the stack, and decrement SP by two.
  265. */
  266. static uint8_t z80_inst_push_qq(Z80 *z80, uint8_t opcode)
  267. {
  268. uint8_t pair = extract_pair(opcode);
  269. stack_push(z80, get_pair(z80, pair));
  270. z80->regfile.pc++;
  271. return 11;
  272. }
  273. /*
  274. PUSH IXY (0xDDE5, 0xFDE5):
  275. Push IX or IY onto the stack, and decrement SP by two.
  276. */
  277. static uint8_t z80_inst_push_ixy(Z80 *z80, uint8_t opcode)
  278. {
  279. (void) opcode;
  280. stack_push(z80, *z80->last_index);
  281. z80->regfile.pc++;
  282. return 15;
  283. }
  284. /*
  285. POP qq (0xC1, 0xD1, 0xE1, 0xF1):
  286. Pop qq from the stack, and increment SP by two.
  287. */
  288. static uint8_t z80_inst_pop_qq(Z80 *z80, uint8_t opcode)
  289. {
  290. uint8_t pair = extract_pair(opcode);
  291. set_pair(z80, pair, stack_pop(z80));
  292. z80->regfile.pc++;
  293. return 10;
  294. }
  295. /*
  296. POP IXY (0xDDE1, 0xFDE1):
  297. Pop IX or IY from the stack, and increment SP by two.
  298. */
  299. static uint8_t z80_inst_pop_ixy(Z80 *z80, uint8_t opcode)
  300. {
  301. (void) opcode;
  302. *z80->last_index = stack_pop(z80);
  303. z80->regfile.pc++;
  304. return 14;
  305. }
  306. /*
  307. EX DE, HL (0xEB):
  308. Exchange DE with HL.
  309. */
  310. static uint8_t z80_inst_ex_de_hl(Z80 *z80, uint8_t opcode)
  311. {
  312. (void) opcode;
  313. uint16_t de = get_pair(z80, REG_DE);
  314. set_pair(z80, REG_DE, get_pair(z80, REG_HL));
  315. set_pair(z80, REG_HL, de);
  316. z80->regfile.pc++;
  317. return 4;
  318. }
  319. /*
  320. EX AF, AF' (0x08):
  321. Exchange AF with AF'.
  322. */
  323. static uint8_t z80_inst_ex_af_af(Z80 *z80, uint8_t opcode)
  324. {
  325. (void) opcode;
  326. uint16_t af = get_pair(z80, REG_AF);
  327. set_pair(z80, REG_AF, get_pair(z80, REG_AF_));
  328. set_pair(z80, REG_AF_, af);
  329. z80->regfile.pc++;
  330. return 4;
  331. }
  332. /*
  333. EXX (0xD9):
  334. Exchange the 16-bit registers with their shadows
  335. (BC <=> BC', DE <=> DE', HL <=> HL').
  336. */
  337. static uint8_t z80_inst_exx(Z80 *z80, uint8_t opcode)
  338. {
  339. (void) opcode;
  340. uint16_t bc = get_pair(z80, REG_BC),
  341. de = get_pair(z80, REG_DE),
  342. hl = get_pair(z80, REG_HL);
  343. set_pair(z80, REG_BC, get_pair(z80, REG_BC_));
  344. set_pair(z80, REG_DE, get_pair(z80, REG_DE_));
  345. set_pair(z80, REG_HL, get_pair(z80, REG_HL_));
  346. set_pair(z80, REG_BC_, bc);
  347. set_pair(z80, REG_DE_, de);
  348. set_pair(z80, REG_HL_, hl);
  349. z80->regfile.pc++;
  350. return 4;
  351. }
  352. /*
  353. EX (SP), HL (0xE3):
  354. Exchange the memory pointed to by SP with HL.
  355. */
  356. static uint8_t z80_inst_ex_sp_hl(Z80 *z80, uint8_t opcode)
  357. {
  358. (void) opcode;
  359. uint16_t hl = get_pair(z80, REG_HL), sp = get_pair(z80, REG_SP);
  360. set_pair(z80, REG_HL, mmu_read_double(z80->mmu, sp));
  361. mmu_write_double(z80->mmu, sp, hl);
  362. z80->regfile.pc++;
  363. return 19;
  364. }
  365. // EX (SP), IXY
  366. /*
  367. LDI (0xEDA0):
  368. LD (DE), (HL); INC HL; INC DE; DEC BC;
  369. */
  370. static uint8_t z80_inst_ldi(Z80 *z80, uint8_t opcode)
  371. {
  372. (void) opcode;
  373. uint16_t hl = get_pair(z80, REG_HL),
  374. de = get_pair(z80, REG_DE),
  375. bc = get_pair(z80, REG_BC);
  376. uint16_t value = mmu_read_double(z80->mmu, hl);
  377. mmu_write_double(z80->mmu, de, value);
  378. set_pair(z80, REG_HL, ++hl);
  379. set_pair(z80, REG_DE, ++de);
  380. set_pair(z80, REG_BC, --bc);
  381. update_flags(z80, 0, 0, bc == 0, 0, 0, 0, 0, 0, 0x16);
  382. z80->regfile.pc++;
  383. return 16;
  384. }
  385. /*
  386. LDIR (0xEDB0):
  387. LDI; JR PV, -2
  388. */
  389. static uint8_t z80_inst_ldir(Z80 *z80, uint8_t opcode)
  390. {
  391. z80_inst_ldi(z80, opcode);
  392. if (get_pair(z80, REG_BC) == 0)
  393. return 16;
  394. z80->regfile.pc -= 2;
  395. return 21;
  396. }
  397. /*
  398. LDD (0xEDA8):
  399. LD (DE), (HL); DEC HL; DEC DE; DEC BC;
  400. */
  401. static uint8_t z80_inst_ldd(Z80 *z80, uint8_t opcode)
  402. {
  403. (void) opcode;
  404. uint16_t hl = get_pair(z80, REG_HL),
  405. de = get_pair(z80, REG_DE),
  406. bc = get_pair(z80, REG_BC);
  407. uint16_t value = mmu_read_double(z80->mmu, hl);
  408. mmu_write_double(z80->mmu, de, value);
  409. set_pair(z80, REG_HL, --hl);
  410. set_pair(z80, REG_DE, --de);
  411. set_pair(z80, REG_BC, --bc);
  412. update_flags(z80, 0, 0, bc == 0, 0, 0, 0, 0, 0, 0x16);
  413. z80->regfile.pc++;
  414. return 16;
  415. }
  416. /*
  417. LDDR (0xEDB8):
  418. LDD; JR PV, -2
  419. */
  420. static uint8_t z80_inst_lddr(Z80 *z80, uint8_t opcode)
  421. {
  422. z80_inst_ldd(z80, opcode);
  423. if (get_pair(z80, REG_BC) == 0)
  424. return 16;
  425. z80->regfile.pc -= 2;
  426. return 21;
  427. }
  428. // CPI
  429. // CPIR
  430. // CPD
  431. // CPDR
  432. // ADD A, r
  433. // ADD A, n
  434. // ADD A, (HL)
  435. // ADD A, (IXY+d)
  436. // ADC A, r
  437. // ADC A, n
  438. // ADC A, (HL)
  439. // ADC A, (IXY+d)
  440. // SUB r
  441. /*
  442. SUB n (0xD6):
  443. Subtract n (8-bit immediate) from A.
  444. */
  445. static uint8_t z80_inst_sub_n(Z80 *z80, uint8_t opcode)
  446. {
  447. (void) opcode;
  448. uint8_t imm = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  449. uint8_t orig = z80->regfile.a;
  450. uint8_t a = z80->regfile.a -= imm;
  451. bool c = (orig - imm) != a;
  452. bool v = (orig - imm) != ((int8_t) a);
  453. bool h = !!(((orig & 0x0F) - (imm & 0x0F)) & 0x10);
  454. update_flags(z80, c, 1, v, !!(a & 0x08), h, !!(a & 0x20), a == 0,
  455. !!(a & 0x80), 0xFF);
  456. z80->regfile.pc++;
  457. return 7;
  458. }
  459. // SUB (HL)
  460. // SUB (IXY+d)
  461. // SBC A, r
  462. // SBC A, n
  463. // SBC A, (HL)
  464. // SBC A, (IXY+d)
  465. // AND r
  466. /*
  467. AND n (0xE6):
  468. Bitwise AND A with n (8-bit immediate).
  469. */
  470. static uint8_t z80_inst_and_n(Z80 *z80, uint8_t opcode)
  471. {
  472. (void) opcode;
  473. uint8_t imm = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  474. uint8_t a = (z80->regfile.a &= imm);
  475. bool parity = !(__builtin_popcount(a) % 2);
  476. update_flags(z80, 0, 0, parity, !!(a & 0x08), 1, !!(a & 0x20), a == 0,
  477. !!(a & 0x80), 0xFF);
  478. z80->regfile.pc++;
  479. return 7;
  480. }
  481. // AND (HL)
  482. // AND (IXY+d)
  483. /*
  484. OR r (0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB7):
  485. Bitwise OR A with r (8-bit register).
  486. */
  487. static uint8_t z80_inst_or_r(Z80 *z80, uint8_t opcode)
  488. {
  489. uint8_t *reg = extract_reg(z80, opcode << 3);
  490. uint8_t a = (z80->regfile.a |= *reg);
  491. bool parity = !(__builtin_popcount(a) % 2);
  492. update_flags(z80, 0, 0, parity, !!(a & 0x08), 0, !!(a & 0x20), a == 0,
  493. !!(a & 0x80), 0xFF);
  494. z80->regfile.pc++;
  495. return 4;
  496. }
  497. /*
  498. OR n (0xF6):
  499. Bitwise OR A with n (8-bit immediate).
  500. */
  501. static uint8_t z80_inst_or_n(Z80 *z80, uint8_t opcode)
  502. {
  503. (void) opcode;
  504. uint8_t imm = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  505. uint8_t a = (z80->regfile.a |= imm);
  506. bool parity = !(__builtin_popcount(a) % 2);
  507. update_flags(z80, 0, 0, parity, !!(a & 0x08), 0, !!(a & 0x20), a == 0,
  508. !!(a & 0x80), 0xFF);
  509. z80->regfile.pc++;
  510. return 7;
  511. }
  512. // OR (HL)
  513. /*
  514. OR (IXY+d) (0xDDB6, 0xFDB6):
  515. Bitwise OR A with (IX+d) or (IY+d).
  516. */
  517. static uint8_t z80_inst_or_ixy(Z80 *z80, uint8_t opcode)
  518. {
  519. (void) opcode;
  520. uint8_t addr = get_index_addr(z80, ++z80->regfile.pc);
  521. uint8_t val = mmu_read_byte(z80->mmu, addr);
  522. uint8_t a = (z80->regfile.a |= val);
  523. bool parity = !(__builtin_popcount(a) % 2);
  524. update_flags(z80, 0, 0, parity, !!(a & 0x08), 0, !!(a & 0x20), a == 0,
  525. !!(a & 0x80), 0xFF);
  526. z80->regfile.pc++;
  527. return 7;
  528. }
  529. /*
  530. XOR r (0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAF):
  531. Bitwise XOR A with r (8-bit register).
  532. */
  533. static uint8_t z80_inst_xor_r(Z80 *z80, uint8_t opcode)
  534. {
  535. uint8_t *reg = extract_reg(z80, opcode << 3);
  536. uint8_t a = (z80->regfile.a ^= *reg);
  537. bool parity = !(__builtin_popcount(a) % 2);
  538. update_flags(z80, 0, 0, parity, !!(a & 0x08), 0, !!(a & 0x20), a == 0,
  539. !!(a & 0x80), 0xFF);
  540. z80->regfile.pc++;
  541. return 4;
  542. }
  543. /*
  544. XOR n (0xEE):
  545. Bitwise XOR A with n (8-bit immediate).
  546. */
  547. static uint8_t z80_inst_xor_n(Z80 *z80, uint8_t opcode)
  548. {
  549. (void) opcode;
  550. uint8_t imm = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  551. uint8_t a = (z80->regfile.a ^= imm);
  552. bool parity = !(__builtin_popcount(a) % 2);
  553. update_flags(z80, 0, 0, parity, !!(a & 0x08), 0, !!(a & 0x20), a == 0,
  554. !!(a & 0x80), 0xFF);
  555. z80->regfile.pc++;
  556. return 7;
  557. }
  558. // XOR (HL)
  559. // XOR (IXY+d)
  560. /*
  561. CP r (0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBF):
  562. Set flags as if r (8-bit register) had been subtracted from A.
  563. */
  564. static uint8_t z80_inst_cp_r(Z80 *z80, uint8_t opcode)
  565. {
  566. uint8_t *reg = extract_reg(z80, opcode << 3);
  567. uint8_t d = z80->regfile.a - *reg;
  568. bool c = (z80->regfile.a - *reg) != d;
  569. bool v = (z80->regfile.a - *reg) != ((int8_t) d);
  570. bool h = !!(((z80->regfile.a & 0x0F) - (*reg & 0x0F)) & 0x10);
  571. update_flags(z80, c, 1, v, !!(*reg & 0x08), h, !!(*reg & 0x20), d == 0,
  572. !!(d & 0x80), 0xFF);
  573. z80->regfile.pc++;
  574. return 4;
  575. }
  576. /*
  577. CP n (0xFE):
  578. Set flags as if n (8-bit immediate) had been subtracted from A.
  579. */
  580. static uint8_t z80_inst_cp_n(Z80 *z80, uint8_t opcode)
  581. {
  582. (void) opcode;
  583. uint8_t n = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  584. uint8_t d = z80->regfile.a - n;
  585. bool c = (z80->regfile.a - n) != d;
  586. bool v = (z80->regfile.a - n) != ((int8_t) d);
  587. bool h = !!(((z80->regfile.a & 0x0F) - (n & 0x0F)) & 0x10);
  588. update_flags(z80, c, 1, v, !!(n & 0x08), h, !!(n & 0x20), d == 0,
  589. !!(d & 0x80), 0xFF);
  590. z80->regfile.pc++;
  591. return 7;
  592. }
  593. /*
  594. CP (HL) (0xBE):
  595. Set flags as if the memory pointed to by HL had been subtracted from A.
  596. */
  597. static uint8_t z80_inst_cp_hl(Z80 *z80, uint8_t opcode)
  598. {
  599. (void) opcode;
  600. uint8_t n = mmu_read_byte(z80->mmu, get_pair(z80, REG_HL));
  601. uint8_t d = z80->regfile.a - n;
  602. bool c = (z80->regfile.a - n) != d;
  603. bool v = (z80->regfile.a - n) != ((int8_t) d);
  604. bool h = !!(((z80->regfile.a & 0x0F) - (n & 0x0F)) & 0x10);
  605. update_flags(z80, c, 1, v, !!(n & 0x08), h, !!(n & 0x20), d == 0,
  606. !!(d & 0x80), 0xFF);
  607. z80->regfile.pc++;
  608. return 7;
  609. }
  610. // CP (IXY+d)
  611. /*
  612. INC r (0x04, 0x0C, 0x14, 0x1C, 0x24, 0x2C, 0x3C):
  613. Increment r (8-bit register).
  614. */
  615. static uint8_t z80_inst_inc_r(Z80 *z80, uint8_t opcode)
  616. {
  617. uint8_t *reg = extract_reg(z80, opcode);
  618. bool halfcarry = !!(((*reg & 0x0F) + 1) & 0x10);
  619. (*reg)++;
  620. update_flags(z80, 0, 0, *reg == 0x80, !!(*reg & 0x08), halfcarry,
  621. !!(*reg & 0x20), *reg == 0, !!(*reg & 0x80), 0xFE);
  622. z80->regfile.pc++;
  623. return 4;
  624. }
  625. /*
  626. INC (HL) (0x34):
  627. Increment the memory address pointed to by HL.
  628. */
  629. static uint8_t z80_inst_inc_hl(Z80 *z80, uint8_t opcode)
  630. {
  631. (void) opcode;
  632. uint8_t byte = mmu_read_byte(z80->mmu, get_pair(z80, REG_HL));
  633. bool halfcarry = !!(((byte & 0x0F) + 1) & 0x10);
  634. mmu_write_byte(z80->mmu, get_pair(z80, REG_HL), ++byte);
  635. update_flags(z80, 0, 0, byte == 0x80, !!(byte & 0x08), halfcarry,
  636. !!(byte & 0x20), byte == 0, !!(byte & 0x80), 0xFE);
  637. z80->regfile.pc++;
  638. return 11;
  639. }
  640. // INC (IXY+d)
  641. /*
  642. DEC r (0x05, 0x0D, 0x15, 0x1D, 0x25, 0x2D, 0x3D):
  643. Decrement r (8-bit register).
  644. */
  645. static uint8_t z80_inst_dec_r(Z80 *z80, uint8_t opcode)
  646. {
  647. uint8_t *reg = extract_reg(z80, opcode);
  648. bool halfcarry = !!(((*reg & 0x0F) - 1) & 0x10);
  649. (*reg)--;
  650. update_flags(z80, 0, 1, *reg == 0x7F, !!(*reg & 0x08), halfcarry,
  651. !!(*reg & 0x20), *reg == 0, !!(*reg & 0x80), 0xFE);
  652. z80->regfile.pc++;
  653. return 4;
  654. }
  655. /*
  656. DEC (HL) (0x35):
  657. Decrement the memory address pointed to by HL.
  658. */
  659. static uint8_t z80_inst_dec_hl(Z80 *z80, uint8_t opcode)
  660. {
  661. (void) opcode;
  662. uint8_t byte = mmu_read_byte(z80->mmu, get_pair(z80, REG_HL));
  663. bool halfcarry = !!(((byte & 0x0F) + 1) & 0x10);
  664. mmu_write_byte(z80->mmu, get_pair(z80, REG_HL), --byte);
  665. update_flags(z80, 0, 1, byte == 0x7F, !!(byte & 0x08), halfcarry,
  666. !!(byte & 0x20), byte == 0, !!(byte & 0x80), 0xFE);
  667. z80->regfile.pc++;
  668. return 11;
  669. }
  670. // DEC (IXY+d)
  671. // DAA
  672. // CPL
  673. // NEG
  674. // CCF
  675. // SCF
  676. /*
  677. NOP (0x00):
  678. No operation is performed.
  679. */
  680. static uint8_t z80_inst_nop(Z80 *z80, uint8_t opcode)
  681. {
  682. (void) opcode;
  683. z80->regfile.pc++;
  684. return 4;
  685. }
  686. /*
  687. HALT (0x76):
  688. Suspend CPU operation: execute NOPs until an interrupt or reset.
  689. */
  690. static uint8_t z80_inst_halt(Z80 *z80, uint8_t opcode)
  691. {
  692. (void) z80;
  693. (void) opcode;
  694. return 4;
  695. }
  696. /*
  697. DI (0xF3):
  698. Disable maskable interrupts by resetting both flip-flops.
  699. */
  700. static uint8_t z80_inst_di(Z80 *z80, uint8_t opcode)
  701. {
  702. (void) opcode;
  703. z80->regfile.iff1 = false;
  704. z80->regfile.iff2 = false;
  705. z80->regfile.pc++;
  706. return 4;
  707. }
  708. /*
  709. EI (0xFB):
  710. Enable maskable interrupts by setting both flip-flops.
  711. */
  712. static uint8_t z80_inst_ei(Z80 *z80, uint8_t opcode)
  713. {
  714. (void) opcode;
  715. z80->regfile.iff1 = true;
  716. z80->regfile.iff2 = true;
  717. z80->regfile.pc++;
  718. return 4;
  719. }
  720. /*
  721. IM (0xED46, 0xED4E, 0xED56, 0xED5E, 0xED66, 0xED6E, 0xED76, 0xED7E):
  722. Set the interrupt mode.
  723. */
  724. static uint8_t z80_inst_im(Z80 *z80, uint8_t opcode)
  725. {
  726. switch (opcode) {
  727. case 0x46:
  728. case 0x4E:
  729. case 0x66:
  730. case 0x6E:
  731. z80->regfile.im_a = false; // Interrupt mode 0
  732. z80->regfile.im_b = false;
  733. break;
  734. case 0x56:
  735. case 0x76:
  736. z80->regfile.im_a = true; // Interrupt mode 1
  737. z80->regfile.im_b = false;
  738. break;
  739. case 0x5E:
  740. case 0x7E:
  741. z80->regfile.im_a = true; // Interrupt mode 2
  742. z80->regfile.im_b = true;
  743. break;
  744. }
  745. z80->regfile.pc++;
  746. return 8;
  747. }
  748. /*
  749. ADD HL, ss (0x09, 0x19, 0x29, 0x39):
  750. Add ss to HL.
  751. */
  752. static uint8_t z80_inst_add_hl_ss(Z80 *z80, uint8_t opcode)
  753. {
  754. uint8_t pair = extract_pair(opcode);
  755. uint16_t lh = get_pair(z80, REG_HL), rh = get_pair(z80, pair);
  756. uint16_t value = lh + rh;
  757. set_pair(z80, REG_HL, value);
  758. bool h = !!(((lh & 0x0FFF) + (rh & 0x0FFF)) & 0x1000);
  759. update_flags(z80, (lh + rh) != value, 0, 0, !!(value & 0x0800), h,
  760. !!(value & 0x2000), 0, 0, 0x3B);
  761. z80->regfile.pc++;
  762. return 11;
  763. }
  764. // ADC HL, ss
  765. /*
  766. SBC HL, ss (0xED42, 0xED52, 0xED62, 0xED72):
  767. Subtract ss with carry from HL.
  768. */
  769. static uint8_t z80_inst_sbc_hl_ss(Z80 *z80, uint8_t opcode)
  770. {
  771. uint8_t pair = extract_pair(opcode);
  772. uint16_t minuend = get_pair(z80, REG_HL);
  773. uint16_t subtrahend = get_pair(z80, pair) + get_flag(z80, FLAG_CARRY);
  774. uint16_t value = minuend - subtrahend;
  775. set_pair(z80, REG_HL, value);
  776. bool c = (minuend - subtrahend) != value;
  777. bool ov = (minuend - subtrahend) != ((int16_t) value); // TODO: verify these
  778. bool hc = !!(((minuend & 0x0FFF) - (subtrahend & 0x0FFF)) & 0x1000);
  779. update_flags(z80, c, 1, ov, !!(value & 0x0800), hc,
  780. !!(value & 0x2000), value == 0, !!(value & 0x8000), 0xFF);
  781. z80->regfile.pc++;
  782. return 15;
  783. }
  784. // ADD IXY, pp
  785. /*
  786. INC ss (0x03, 0x13, 0x23, 0x33):
  787. Increment ss (16-bit register).
  788. */
  789. static uint8_t z80_inst_inc_ss(Z80 *z80, uint8_t opcode)
  790. {
  791. uint8_t pair = extract_pair(opcode);
  792. set_pair(z80, pair, get_pair(z80, pair) + 1);
  793. z80->regfile.pc++;
  794. return 6;
  795. }
  796. // INC IXY
  797. /*
  798. DEC ss (0x0B, 0x1B, 0x2B, 0x3B):
  799. Decrement ss (16-bit register).
  800. */
  801. static uint8_t z80_inst_dec_ss(Z80 *z80, uint8_t opcode)
  802. {
  803. uint8_t pair = extract_pair(opcode);
  804. set_pair(z80, pair, get_pair(z80, pair) - 1);
  805. z80->regfile.pc++;
  806. return 6;
  807. }
  808. // DEC IXY
  809. // RLCA
  810. // RLA
  811. // RRCA
  812. // RRA
  813. // RLC r
  814. // RLC (HL)
  815. // RLC (IXY+d)
  816. // RL r
  817. // RL (HL)
  818. // RL (IXY+d)
  819. // RRC r
  820. // RRC (HL)
  821. // RRC (IXY+d)
  822. // RR r
  823. // RR (HL)
  824. // RR (IXY+d)
  825. // SLA r
  826. // SLA (HL)
  827. // SLA (IXY+d)
  828. // SRA r
  829. // SRA (HL)
  830. // SRA (IXY+d)
  831. // SRL r
  832. // SRL (HL)
  833. // SRL (IXY+d)
  834. // RLD
  835. // RRD
  836. /*
  837. BIT b, r (0xCB40, 0xCB41, 0xCB42, 0xCB43, 0xCB44, 0xCB45, 0xCB47, 0xCB48,
  838. 0xCB49, 0xCB4A, 0xCB4B, 0xCB4C, 0xCB4D, 0xCB4F, 0xCB50, 0xCB51, 0xCB52,
  839. 0xCB53, 0xCB54, 0xCB55, 0xCB57, 0xCB58, 0xCB59, 0xCB5A, 0xCB5B, 0xCB5C,
  840. 0xCB5D, 0xCB5F, 0xCB60, 0xCB61, 0xCB62, 0xCB63, 0xCB64, 0xCB65, 0xCB67,
  841. 0xCB68, 0xCB69, 0xCB6A, 0xCB6B, 0xCB6C, 0xCB6D, 0xCB6F, 0xCB70, 0xCB71,
  842. 0xCB72, 0xCB73, 0xCB74, 0xCB75, 0xCB77, 0xCB78, 0xCB79, 0xCB7A, 0xCB7B,
  843. 0xCB7C, 0xCB7D, 0xCB7F):
  844. Test bit b of r (8-bit register).
  845. */
  846. static uint8_t z80_inst_bit_b_r(Z80 *z80, uint8_t opcode)
  847. {
  848. uint8_t *reg = extract_reg(z80, opcode << 3);
  849. uint8_t bit = (opcode >> 3) & 0x07;
  850. bool z = (((*reg) >> bit) & 1) == 0;
  851. if (z)
  852. update_flags(z80, 0, 0, 1, 0, 1, 0, 1, 0, 0xFE);
  853. else
  854. update_flags(z80, 0, 0, 0, bit == 3, 1, bit == 5, 0, bit == 7, 0xFE);
  855. z80->regfile.pc++;
  856. return 8;
  857. }
  858. /*
  859. BIT b, (HL) (0xCB46, 0xCB4E, 0xCB56, 0xCB5E, 0xCB66, 0xCB6E, 0xCB76,
  860. 0xCB7E):
  861. Test bit b of (HL).
  862. */
  863. static uint8_t z80_inst_bit_b_hl(Z80 *z80, uint8_t opcode)
  864. {
  865. uint8_t val = mmu_read_byte(z80->mmu, get_pair(z80, REG_HL));
  866. uint8_t bit = (opcode >> 3) & 0x07;
  867. bool z = ((val >> bit) & 1) == 0;
  868. if (z)
  869. update_flags(z80, 0, 0, 1, 0, 1, 0, 1, 0, 0xFE);
  870. else
  871. update_flags(z80, 0, 0, 0, bit == 3, 1, bit == 5, 0, bit == 7, 0xFE);
  872. z80->regfile.pc++;
  873. return 8;
  874. }
  875. /*
  876. BIT b, (IXY+d) (0xDDCB40-0xDDCB7F, 0xFDCB40-0xFDCB7F):
  877. Test bit b of (IX+d) or (IY+d).
  878. */
  879. static uint8_t z80_inst_bit_b_ixy(Z80 *z80, uint8_t opcode)
  880. {
  881. uint16_t addr = get_index_addr(z80, z80->regfile.pc - 1);
  882. uint8_t val = mmu_read_byte(z80->mmu, addr);
  883. uint8_t bit = (opcode >> 3) & 0x07;
  884. bool z = ((val >> bit) & 1) == 0;
  885. if (z)
  886. update_flags(z80, 0, 0, 1, 0, 1, 0, 1, 0, 0xFE);
  887. else
  888. update_flags(z80, 0, 0, 0, bit == 3, 1, bit == 5, 0, bit == 7, 0xFE);
  889. z80->regfile.pc++;
  890. return 8;
  891. }
  892. // SET b, r
  893. // SET b, (HL)
  894. // SET b, (IXY+d)
  895. // RES b, r
  896. /*
  897. RES b, (HL) (0xCB86, 0xCB8E, 0xCB96, 0xCB9E, 0xCBA6, 0xCBAE, 0xCBB6,
  898. 0xCBBE):
  899. Reset bit b of (HL).
  900. */
  901. static uint8_t z80_inst_res_b_hl(Z80 *z80, uint8_t opcode)
  902. {
  903. uint8_t val = mmu_read_byte(z80->mmu, get_pair(z80, REG_HL));
  904. uint8_t bit = (opcode >> 3) & 0x07;
  905. val &= ~(1 << bit);
  906. mmu_write_byte(z80->mmu, get_pair(z80, REG_HL), val);
  907. z80->regfile.pc++;
  908. return 8;
  909. }
  910. // RES b, (IXY+d)
  911. /*
  912. JP nn (0xC3):
  913. Jump to nn (16-bit immediate).
  914. */
  915. static uint8_t z80_inst_jp_nn(Z80 *z80, uint8_t opcode)
  916. {
  917. (void) opcode;
  918. z80->regfile.pc = mmu_read_double(z80->mmu, ++z80->regfile.pc);
  919. return 10;
  920. }
  921. /*
  922. JP cc, nn (0xC2, 0xCA, 0xD2, 0xDA, 0xE2, 0xEA, 0xF2, 0xFA):
  923. Jump to nn (16-bit immediate) if cc (condition) is true.
  924. */
  925. static uint8_t z80_inst_jp_cc_nn(Z80 *z80, uint8_t opcode)
  926. {
  927. if (extract_cond(z80, opcode))
  928. z80->regfile.pc = mmu_read_double(z80->mmu, ++z80->regfile.pc);
  929. else
  930. z80->regfile.pc += 3;
  931. return 10;
  932. }
  933. /*
  934. JR e (0x18):
  935. Relative jump e (signed 8-bit immediate) bytes.
  936. */
  937. static uint8_t z80_inst_jr_e(Z80 *z80, uint8_t opcode)
  938. {
  939. (void) opcode;
  940. int8_t jump = mmu_read_byte(z80->mmu, z80->regfile.pc + 1);
  941. z80->regfile.pc += jump + 2;
  942. return 12;
  943. }
  944. /*
  945. JR cc, e (0x20, 0x28, 0x30, 0x38):
  946. Relative jump e (signed 8-bit immediate) bytes if cc (condition) is true.
  947. */
  948. static uint8_t z80_inst_jr_cc_e(Z80 *z80, uint8_t opcode)
  949. {
  950. if (extract_cond(z80, opcode - 0x20)) {
  951. int8_t jump = mmu_read_byte(z80->mmu, z80->regfile.pc + 1);
  952. z80->regfile.pc += jump + 2;
  953. return 12;
  954. } else {
  955. z80->regfile.pc += 2;
  956. return 7;
  957. }
  958. }
  959. // JP (HL)
  960. // JP (IXY)
  961. /*
  962. DJNZ, e (0x10):
  963. Decrement b and relative jump e (signed 8-bit immediate) if non-zero.
  964. */
  965. static uint8_t z80_inst_djnz_e(Z80 *z80, uint8_t opcode)
  966. {
  967. (void) opcode;
  968. z80->regfile.b--;
  969. if (z80->regfile.b != 0) {
  970. int8_t jump = mmu_read_byte(z80->mmu, z80->regfile.pc + 1);
  971. z80->regfile.pc += jump + 2;
  972. return 13;
  973. } else {
  974. z80->regfile.pc += 2;
  975. return 8;
  976. }
  977. }
  978. /*
  979. CALL nn (0xCD):
  980. Push PC+3 onto the stack and jump to nn (16-bit immediate).
  981. */
  982. static uint8_t z80_inst_call_nn(Z80 *z80, uint8_t opcode)
  983. {
  984. (void) opcode;
  985. stack_push(z80, z80->regfile.pc + 3);
  986. z80->regfile.pc = mmu_read_double(z80->mmu, ++z80->regfile.pc);
  987. return 17;
  988. }
  989. /*
  990. CALL cc, nn (0xC4, 0xCC, 0xD4, 0xDC, 0xE4, 0xEC, 0xF4, 0xFC):
  991. Push PC+3 onto the stack and jump to nn (16-bit immediate) if cc is true.
  992. */
  993. static uint8_t z80_inst_call_cc_nn(Z80 *z80, uint8_t opcode)
  994. {
  995. if (extract_cond(z80, opcode)) {
  996. stack_push(z80, z80->regfile.pc + 3);
  997. z80->regfile.pc = mmu_read_double(z80->mmu, ++z80->regfile.pc);
  998. return 17;
  999. } else {
  1000. z80->regfile.pc += 3;
  1001. return 10;
  1002. }
  1003. }
  1004. /*
  1005. RET (0xC9):
  1006. Pop PC from the stack.
  1007. */
  1008. static uint8_t z80_inst_ret(Z80 *z80, uint8_t opcode)
  1009. {
  1010. (void) opcode;
  1011. z80->regfile.pc = stack_pop(z80);
  1012. return 10;
  1013. }
  1014. /*
  1015. RET cc (0xC0, 0xC8, 0xD0, 0xD8, 0xE0, 0xE8, 0xF0, 0xF8):
  1016. Pop PC from the stack if cc is true.
  1017. */
  1018. static uint8_t z80_inst_ret_cc(Z80 *z80, uint8_t opcode)
  1019. {
  1020. if (extract_cond(z80, opcode)) {
  1021. z80->regfile.pc = stack_pop(z80);
  1022. return 11;
  1023. } else {
  1024. z80->regfile.pc++;
  1025. return 5;
  1026. }
  1027. }
  1028. /*
  1029. RETI (0xED4D):
  1030. Pop PC from the stack.
  1031. */
  1032. static uint8_t z80_inst_reti(Z80 *z80, uint8_t opcode)
  1033. {
  1034. (void) opcode;
  1035. z80->regfile.pc = stack_pop(z80);
  1036. return 14;
  1037. }
  1038. /*
  1039. RETN (0xED45, 0xED55, 0xED5D, 0xED65, 0xED6D, 0xED75, 0xED7D):
  1040. Pop PC from the stack, and copy to IFF2 to IFF1.
  1041. */
  1042. static uint8_t z80_inst_retn(Z80 *z80, uint8_t opcode)
  1043. {
  1044. (void) opcode;
  1045. z80->regfile.pc = stack_pop(z80);
  1046. z80->regfile.iff1 = z80->regfile.iff2;
  1047. return 14;
  1048. }
  1049. /*
  1050. RST p (0xC7, 0xCF, 0xD7, 0xDF, 0xE7, 0xEF, 0xF7, 0xFF):
  1051. Push PC+1 onto the stack and jump to p (opcode & 0x38).
  1052. */
  1053. static uint8_t z80_inst_rst_p(Z80 *z80, uint8_t opcode)
  1054. {
  1055. stack_push(z80, z80->regfile.pc + 1);
  1056. z80->regfile.pc = opcode & 0x38;
  1057. return 11;
  1058. }
  1059. /*
  1060. IN A, (n) (0xDB):
  1061. Read a byte from port n into a.
  1062. */
  1063. static uint8_t z80_inst_in_a_n(Z80 *z80, uint8_t opcode)
  1064. {
  1065. (void) opcode;
  1066. uint8_t port = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  1067. z80->regfile.a = read_port(z80, port);
  1068. z80->regfile.pc++;
  1069. return 11;
  1070. }
  1071. /*
  1072. IN r, (C) (0xED40, 0xED48, 0xED50, 0xED58, 0xED60, 0xED68, 0xED70, 0xED78):
  1073. Read a byte from port C into r, or affect flags only if 0xED70.
  1074. */
  1075. static uint8_t z80_inst_in_r_c(Z80 *z80, uint8_t opcode)
  1076. {
  1077. uint8_t data = read_port(z80, z80->regfile.c);
  1078. bool parity = !(__builtin_popcount(data) % 2);
  1079. if (opcode != 0x70)
  1080. *extract_reg(z80, opcode) = data;
  1081. update_flags(z80, 0, 0, parity, !!(data & 0x08), 0, !!(data & 0x20),
  1082. data == 0, !!(data & 0x80), 0xFE);
  1083. z80->regfile.pc++;
  1084. return 12;
  1085. }
  1086. /*
  1087. INI (0xEDA2):
  1088. IN (HL), (C); INC HL; DEC B
  1089. */
  1090. static uint8_t z80_inst_ini(Z80 *z80, uint8_t opcode)
  1091. {
  1092. (void) opcode;
  1093. uint8_t data = read_port(z80, z80->regfile.c), *b = &z80->regfile.b;
  1094. uint16_t hl = get_pair(z80, REG_HL);
  1095. bool h = !!(((*b & 0x0F) - 1) & 0x10);
  1096. mmu_write_byte(z80->mmu, hl, data);
  1097. set_pair(z80, REG_HL, hl + 1);
  1098. (*b)--;
  1099. update_flags(z80, 0, 1, *b == 0x7F, !!(*b & 0x08), h, !!(*b & 0x20),
  1100. *b == 0, !!(*b & 0x80), 0xFE);
  1101. z80->regfile.pc++;
  1102. return 16;
  1103. }
  1104. /*
  1105. INIR (0xEDB2):
  1106. INI; JR NZ, -2
  1107. */
  1108. static uint8_t z80_inst_inir(Z80 *z80, uint8_t opcode)
  1109. {
  1110. z80_inst_ini(z80, opcode);
  1111. if (z80->regfile.b == 0)
  1112. return 16;
  1113. z80->regfile.pc -= 2;
  1114. return 21;
  1115. }
  1116. /*
  1117. IND (0xEDAA):
  1118. IN (HL), (C); DEC HL; DEC B
  1119. */
  1120. static uint8_t z80_inst_ind(Z80 *z80, uint8_t opcode)
  1121. {
  1122. (void) opcode;
  1123. uint8_t data = read_port(z80, z80->regfile.c), *b = &z80->regfile.b;
  1124. uint16_t hl = get_pair(z80, REG_HL);
  1125. bool h = !!(((*b & 0x0F) - 1) & 0x10);
  1126. mmu_write_byte(z80->mmu, hl, data);
  1127. set_pair(z80, REG_HL, hl - 1);
  1128. (*b)--;
  1129. update_flags(z80, 0, 1, *b == 0x7F, !!(*b & 0x08), h, !!(*b & 0x20),
  1130. *b == 0, !!(*b & 0x80), 0xFE);
  1131. z80->regfile.pc++;
  1132. return 16;
  1133. }
  1134. /*
  1135. INDR (0xEDBA):
  1136. IND; JR NZ, -2
  1137. */
  1138. static uint8_t z80_inst_indr(Z80 *z80, uint8_t opcode)
  1139. {
  1140. z80_inst_ind(z80, opcode);
  1141. if (z80->regfile.b == 0)
  1142. return 16;
  1143. z80->regfile.pc -= 2;
  1144. return 21;
  1145. }
  1146. /*
  1147. OUT (n), A (0xD3):
  1148. Write a byte from a into port n.
  1149. */
  1150. static uint8_t z80_inst_out_n_a(Z80 *z80, uint8_t opcode)
  1151. {
  1152. (void) opcode;
  1153. uint8_t port = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  1154. write_port(z80, port, z80->regfile.a);
  1155. z80->regfile.pc++;
  1156. return 11;
  1157. }
  1158. /*
  1159. OUT (C), r (0xED41, 0xED49, 0xED51, 0xED59, 0xED61, 0xED69, 0xED71,
  1160. 0xED79):
  1161. Write a byte from r (8-bit reg, or 0 if 0xED71) into port C.
  1162. */
  1163. static uint8_t z80_inst_out_c_r(Z80 *z80, uint8_t opcode)
  1164. {
  1165. uint8_t value = opcode != 0x71 ? *extract_reg(z80, opcode) : 0;
  1166. write_port(z80, z80->regfile.c, value);
  1167. z80->regfile.pc++;
  1168. return 12;
  1169. }
  1170. /*
  1171. OUTI (0xEDA3):
  1172. OUT (C), (HL); INC HL; DEC B
  1173. */
  1174. static uint8_t z80_inst_outi(Z80 *z80, uint8_t opcode)
  1175. {
  1176. (void) opcode;
  1177. uint16_t hl = get_pair(z80, REG_HL);
  1178. uint8_t *b = &z80->regfile.b;
  1179. bool h = !!(((*b & 0x0F) - 1) & 0x10);
  1180. write_port(z80, z80->regfile.c, mmu_read_byte(z80->mmu, hl));
  1181. set_pair(z80, REG_HL, hl + 1);
  1182. (*b)--;
  1183. update_flags(z80, 0, 1, *b == 0x7F, !!(*b & 0x08), h, !!(*b & 0x20),
  1184. *b == 0, !!(*b & 0x80), 0xFE);
  1185. z80->regfile.pc++;
  1186. return 16;
  1187. }
  1188. /*
  1189. OTIR (0xEDB3):
  1190. OUTI; JR NZ, -2
  1191. */
  1192. static uint8_t z80_inst_otir(Z80 *z80, uint8_t opcode)
  1193. {
  1194. z80_inst_outi(z80, opcode);
  1195. if (z80->regfile.b == 0)
  1196. return 16;
  1197. z80->regfile.pc -= 2;
  1198. return 21;
  1199. }
  1200. /*
  1201. OUTD (0xEDAB):
  1202. OUT (C), (HL); DEC HL; DEC B
  1203. */
  1204. static uint8_t z80_inst_outd(Z80 *z80, uint8_t opcode)
  1205. {
  1206. (void) opcode;
  1207. uint16_t hl = get_pair(z80, REG_HL);
  1208. uint8_t *b = &z80->regfile.b;
  1209. bool h = !!(((*b & 0x0F) - 1) & 0x10);
  1210. write_port(z80, z80->regfile.c, mmu_read_byte(z80->mmu, hl));
  1211. set_pair(z80, REG_HL, hl - 1);
  1212. (*b)--;
  1213. update_flags(z80, 0, 1, *b == 0x7F, !!(*b & 0x08), h, !!(*b & 0x20),
  1214. *b == 0, !!(*b & 0x80), 0xFE);
  1215. z80->regfile.pc++;
  1216. return 16;
  1217. }
  1218. /*
  1219. OTDR (0xEDBB):
  1220. OUTD; JR NZ, -2
  1221. */
  1222. static uint8_t z80_inst_otdr(Z80 *z80, uint8_t opcode)
  1223. {
  1224. z80_inst_outd(z80, opcode);
  1225. if (z80->regfile.b == 0)
  1226. return 16;
  1227. z80->regfile.pc -= 2;
  1228. return 21;
  1229. }
  1230. /*
  1231. NOP2:
  1232. No operation is performed twice; i.e., 2 NOPs-worth of cycles are spent.
  1233. Used for unimplemented extended and index instructions.
  1234. */
  1235. static uint8_t z80_inst_nop2(Z80 *z80, uint8_t opcode)
  1236. {
  1237. (void) opcode;
  1238. z80->regfile.pc++;
  1239. return 8;
  1240. }
  1241. /*
  1242. 0xED:
  1243. Handle an extended instruction.
  1244. */
  1245. static uint8_t z80_prefix_extended(Z80 *z80, uint8_t opcode)
  1246. {
  1247. opcode = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  1248. return (*instruction_table_extended[opcode])(z80, opcode);
  1249. }
  1250. /*
  1251. 0xED:
  1252. Handle a bit instruction.
  1253. */
  1254. static uint8_t z80_prefix_bits(Z80 *z80, uint8_t opcode)
  1255. {
  1256. opcode = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  1257. return (*instruction_table_bits[opcode])(z80, opcode);
  1258. }
  1259. /*
  1260. 0xDD, 0xFD:
  1261. Handle an index instruction.
  1262. */
  1263. static uint8_t z80_prefix_index(Z80 *z80, uint8_t opcode)
  1264. {
  1265. z80->last_index = (opcode == 0xDD) ? &z80->regfile.ix : &z80->regfile.iy;
  1266. opcode = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  1267. return (*instruction_table_index[opcode])(z80, opcode);
  1268. }
  1269. /*
  1270. 0xDDCB, 0xFDCB:
  1271. Handle an index-bit instruction.
  1272. */
  1273. static uint8_t z80_prefix_index_bits(Z80 *z80, uint8_t opcode)
  1274. {
  1275. opcode = mmu_read_byte(z80->mmu, z80->regfile.pc += 2);
  1276. return (*instruction_table_index_bits[opcode])(z80, opcode);
  1277. }
  1278. #include "z80_tables.inc.c"