An emulator, assembler, and disassembler for the Sega Game Gear
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

2438 lines
55 KiB

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