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.
 
 
 
 
 

2201 lines
46 KiB

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