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.
 
 
 
 
 

1565 lines
35 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->last_index = 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->last_index = 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->last_index);
  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->last_index = 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->last_index);
  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->last_index = 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->last_index, sp = z80->regs.sp;
  426. *z80->last_index = 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. update_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. update_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. // CPI
  488. // CPIR
  489. // CPD
  490. // CPDR
  491. // ADD A, r
  492. // ADD A, n
  493. // ADD A, (HL)
  494. // ADD A, (IXY+d)
  495. // ADC A, r
  496. // ADC A, n
  497. // ADC A, (HL)
  498. // ADC A, (IXY+d)
  499. // SUB r
  500. /*
  501. SUB n (0xD6):
  502. Subtract n (8-bit immediate) from A.
  503. */
  504. static uint8_t z80_inst_sub_n(Z80 *z80, uint8_t opcode)
  505. {
  506. (void) opcode;
  507. uint8_t imm = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  508. uint8_t orig = z80->regs.a;
  509. uint8_t a = z80->regs.a -= imm;
  510. bool c = (orig - imm) != a;
  511. bool v = (orig - imm) != ((int8_t) a);
  512. bool h = !!(((orig & 0x0F) - (imm & 0x0F)) & 0x10);
  513. update_flags(z80, c, 1, v, !!(a & 0x08), h, !!(a & 0x20), a == 0,
  514. !!(a & 0x80), 0xFF);
  515. z80->regs.pc++;
  516. return 7;
  517. }
  518. // SUB (HL)
  519. // SUB (IXY+d)
  520. // SBC A, r
  521. // SBC A, n
  522. // SBC A, (HL)
  523. // SBC A, (IXY+d)
  524. // AND r
  525. /*
  526. AND n (0xE6):
  527. Bitwise AND A with n (8-bit immediate).
  528. */
  529. static uint8_t z80_inst_and_n(Z80 *z80, uint8_t opcode)
  530. {
  531. (void) opcode;
  532. uint8_t imm = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  533. uint8_t a = (z80->regs.a &= imm);
  534. bool parity = !(__builtin_popcount(a) % 2);
  535. update_flags(z80, 0, 0, parity, !!(a & 0x08), 1, !!(a & 0x20), a == 0,
  536. !!(a & 0x80), 0xFF);
  537. z80->regs.pc++;
  538. return 7;
  539. }
  540. // AND (HL)
  541. // AND (IXY+d)
  542. /*
  543. OR r (0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB7):
  544. Bitwise OR A with r (8-bit register).
  545. */
  546. static uint8_t z80_inst_or_r(Z80 *z80, uint8_t opcode)
  547. {
  548. uint8_t *reg = extract_reg(z80, opcode << 3);
  549. uint8_t a = (z80->regs.a |= *reg);
  550. bool parity = !(__builtin_popcount(a) % 2);
  551. update_flags(z80, 0, 0, parity, !!(a & 0x08), 0, !!(a & 0x20), a == 0,
  552. !!(a & 0x80), 0xFF);
  553. z80->regs.pc++;
  554. return 4;
  555. }
  556. /*
  557. OR n (0xF6):
  558. Bitwise OR A with n (8-bit immediate).
  559. */
  560. static uint8_t z80_inst_or_n(Z80 *z80, uint8_t opcode)
  561. {
  562. (void) opcode;
  563. uint8_t imm = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  564. uint8_t a = (z80->regs.a |= imm);
  565. bool parity = !(__builtin_popcount(a) % 2);
  566. update_flags(z80, 0, 0, parity, !!(a & 0x08), 0, !!(a & 0x20), a == 0,
  567. !!(a & 0x80), 0xFF);
  568. z80->regs.pc++;
  569. return 7;
  570. }
  571. // OR (HL)
  572. /*
  573. OR (IXY+d) (0xDDB6, 0xFDB6):
  574. Bitwise OR A with (IX+d) or (IY+d).
  575. */
  576. static uint8_t z80_inst_or_ixy(Z80 *z80, uint8_t opcode)
  577. {
  578. (void) opcode;
  579. uint8_t addr = get_index_addr(z80, ++z80->regs.pc);
  580. uint8_t val = mmu_read_byte(z80->mmu, addr);
  581. uint8_t a = (z80->regs.a |= val);
  582. bool parity = !(__builtin_popcount(a) % 2);
  583. update_flags(z80, 0, 0, parity, !!(a & 0x08), 0, !!(a & 0x20), a == 0,
  584. !!(a & 0x80), 0xFF);
  585. z80->regs.pc++;
  586. return 7;
  587. }
  588. /*
  589. XOR r (0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAF):
  590. Bitwise XOR A with r (8-bit register).
  591. */
  592. static uint8_t z80_inst_xor_r(Z80 *z80, uint8_t opcode)
  593. {
  594. uint8_t *reg = extract_reg(z80, opcode << 3);
  595. uint8_t a = (z80->regs.a ^= *reg);
  596. bool parity = !(__builtin_popcount(a) % 2);
  597. update_flags(z80, 0, 0, parity, !!(a & 0x08), 0, !!(a & 0x20), a == 0,
  598. !!(a & 0x80), 0xFF);
  599. z80->regs.pc++;
  600. return 4;
  601. }
  602. /*
  603. XOR n (0xEE):
  604. Bitwise XOR A with n (8-bit immediate).
  605. */
  606. static uint8_t z80_inst_xor_n(Z80 *z80, uint8_t opcode)
  607. {
  608. (void) opcode;
  609. uint8_t imm = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  610. uint8_t a = (z80->regs.a ^= imm);
  611. bool parity = !(__builtin_popcount(a) % 2);
  612. update_flags(z80, 0, 0, parity, !!(a & 0x08), 0, !!(a & 0x20), a == 0,
  613. !!(a & 0x80), 0xFF);
  614. z80->regs.pc++;
  615. return 7;
  616. }
  617. // XOR (HL)
  618. // XOR (IXY+d)
  619. /*
  620. CP r (0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBF):
  621. Set flags as if r (8-bit register) had been subtracted from A.
  622. */
  623. static uint8_t z80_inst_cp_r(Z80 *z80, uint8_t opcode)
  624. {
  625. uint8_t *reg = extract_reg(z80, opcode << 3);
  626. uint8_t d = z80->regs.a - *reg;
  627. bool c = (z80->regs.a - *reg) != d;
  628. bool v = (z80->regs.a - *reg) != ((int8_t) d);
  629. bool h = !!(((z80->regs.a & 0x0F) - (*reg & 0x0F)) & 0x10);
  630. update_flags(z80, c, 1, v, !!(*reg & 0x08), h, !!(*reg & 0x20), d == 0,
  631. !!(d & 0x80), 0xFF);
  632. z80->regs.pc++;
  633. return 4;
  634. }
  635. /*
  636. CP n (0xFE):
  637. Set flags as if n (8-bit immediate) had been subtracted from A.
  638. */
  639. static uint8_t z80_inst_cp_n(Z80 *z80, uint8_t opcode)
  640. {
  641. (void) opcode;
  642. uint8_t n = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  643. uint8_t d = z80->regs.a - n;
  644. bool c = (z80->regs.a - n) != d;
  645. bool v = (z80->regs.a - n) != ((int8_t) d);
  646. bool h = !!(((z80->regs.a & 0x0F) - (n & 0x0F)) & 0x10);
  647. update_flags(z80, c, 1, v, !!(n & 0x08), h, !!(n & 0x20), d == 0,
  648. !!(d & 0x80), 0xFF);
  649. z80->regs.pc++;
  650. return 7;
  651. }
  652. /*
  653. CP (HL) (0xBE):
  654. Set flags as if the memory pointed to by HL had been subtracted from A.
  655. */
  656. static uint8_t z80_inst_cp_hl(Z80 *z80, uint8_t opcode)
  657. {
  658. (void) opcode;
  659. uint8_t n = mmu_read_byte(z80->mmu, z80->regs.hl);
  660. uint8_t d = z80->regs.a - n;
  661. bool c = (z80->regs.a - n) != d;
  662. bool v = (z80->regs.a - n) != ((int8_t) d);
  663. bool h = !!(((z80->regs.a & 0x0F) - (n & 0x0F)) & 0x10);
  664. update_flags(z80, c, 1, v, !!(n & 0x08), h, !!(n & 0x20), d == 0,
  665. !!(d & 0x80), 0xFF);
  666. z80->regs.pc++;
  667. return 7;
  668. }
  669. // CP (IXY+d)
  670. /*
  671. INC r (0x04, 0x0C, 0x14, 0x1C, 0x24, 0x2C, 0x3C):
  672. Increment r (8-bit register).
  673. */
  674. static uint8_t z80_inst_inc_r(Z80 *z80, uint8_t opcode)
  675. {
  676. uint8_t *reg = extract_reg(z80, opcode);
  677. bool halfcarry = !!(((*reg & 0x0F) + 1) & 0x10);
  678. (*reg)++;
  679. update_flags(z80, 0, 0, *reg == 0x80, !!(*reg & 0x08), halfcarry,
  680. !!(*reg & 0x20), *reg == 0, !!(*reg & 0x80), 0xFE);
  681. z80->regs.pc++;
  682. return 4;
  683. }
  684. /*
  685. INC (HL) (0x34):
  686. Increment the memory address pointed to by HL.
  687. */
  688. static uint8_t z80_inst_inc_hl(Z80 *z80, uint8_t opcode)
  689. {
  690. (void) opcode;
  691. uint8_t byte = mmu_read_byte(z80->mmu, z80->regs.hl);
  692. bool halfcarry = !!(((byte & 0x0F) + 1) & 0x10);
  693. mmu_write_byte(z80->mmu, z80->regs.hl, ++byte);
  694. update_flags(z80, 0, 0, byte == 0x80, !!(byte & 0x08), halfcarry,
  695. !!(byte & 0x20), byte == 0, !!(byte & 0x80), 0xFE);
  696. z80->regs.pc++;
  697. return 11;
  698. }
  699. // INC (IXY+d)
  700. /*
  701. DEC r (0x05, 0x0D, 0x15, 0x1D, 0x25, 0x2D, 0x3D):
  702. Decrement r (8-bit register).
  703. */
  704. static uint8_t z80_inst_dec_r(Z80 *z80, uint8_t opcode)
  705. {
  706. uint8_t *reg = extract_reg(z80, opcode);
  707. bool halfcarry = !!(((*reg & 0x0F) - 1) & 0x10);
  708. (*reg)--;
  709. update_flags(z80, 0, 1, *reg == 0x7F, !!(*reg & 0x08), halfcarry,
  710. !!(*reg & 0x20), *reg == 0, !!(*reg & 0x80), 0xFE);
  711. z80->regs.pc++;
  712. return 4;
  713. }
  714. /*
  715. DEC (HL) (0x35):
  716. Decrement the memory address pointed to by HL.
  717. */
  718. static uint8_t z80_inst_dec_hl(Z80 *z80, uint8_t opcode)
  719. {
  720. (void) opcode;
  721. uint8_t byte = mmu_read_byte(z80->mmu, z80->regs.hl);
  722. bool halfcarry = !!(((byte & 0x0F) + 1) & 0x10);
  723. mmu_write_byte(z80->mmu, z80->regs.hl, --byte);
  724. update_flags(z80, 0, 1, byte == 0x7F, !!(byte & 0x08), halfcarry,
  725. !!(byte & 0x20), byte == 0, !!(byte & 0x80), 0xFE);
  726. z80->regs.pc++;
  727. return 11;
  728. }
  729. // DEC (IXY+d)
  730. // DAA
  731. // CPL
  732. // NEG
  733. // CCF
  734. // SCF
  735. /*
  736. NOP (0x00):
  737. No operation is performed.
  738. */
  739. static uint8_t z80_inst_nop(Z80 *z80, uint8_t opcode)
  740. {
  741. (void) opcode;
  742. z80->regs.pc++;
  743. return 4;
  744. }
  745. /*
  746. HALT (0x76):
  747. Suspend CPU operation: execute NOPs until an interrupt or reset.
  748. */
  749. static uint8_t z80_inst_halt(Z80 *z80, uint8_t opcode)
  750. {
  751. (void) z80;
  752. (void) opcode;
  753. return 4;
  754. }
  755. /*
  756. DI (0xF3):
  757. Disable maskable interrupts by resetting both flip-flops.
  758. */
  759. static uint8_t z80_inst_di(Z80 *z80, uint8_t opcode)
  760. {
  761. (void) opcode;
  762. z80->regs.iff1 = false;
  763. z80->regs.iff2 = false;
  764. z80->regs.pc++;
  765. return 4;
  766. }
  767. /*
  768. EI (0xFB):
  769. Enable maskable interrupts by setting both flip-flops.
  770. */
  771. static uint8_t z80_inst_ei(Z80 *z80, uint8_t opcode)
  772. {
  773. (void) opcode;
  774. z80->regs.iff1 = true;
  775. z80->regs.iff2 = true;
  776. z80->regs.pc++;
  777. return 4;
  778. }
  779. /*
  780. IM (0xED46, 0xED4E, 0xED56, 0xED5E, 0xED66, 0xED6E, 0xED76, 0xED7E):
  781. Set the interrupt mode.
  782. */
  783. static uint8_t z80_inst_im(Z80 *z80, uint8_t opcode)
  784. {
  785. switch (opcode) {
  786. case 0x46:
  787. case 0x4E:
  788. case 0x66:
  789. case 0x6E:
  790. z80->regs.im_a = false; // Interrupt mode 0
  791. z80->regs.im_b = false;
  792. break;
  793. case 0x56:
  794. case 0x76:
  795. z80->regs.im_a = true; // Interrupt mode 1
  796. z80->regs.im_b = false;
  797. break;
  798. case 0x5E:
  799. case 0x7E:
  800. z80->regs.im_a = true; // Interrupt mode 2
  801. z80->regs.im_b = true;
  802. break;
  803. }
  804. z80->regs.pc++;
  805. return 8;
  806. }
  807. /*
  808. ADD HL, ss (0x09, 0x19, 0x29, 0x39):
  809. Add ss to HL.
  810. */
  811. static uint8_t z80_inst_add_hl_ss(Z80 *z80, uint8_t opcode)
  812. {
  813. uint16_t lh = z80->regs.hl, rh = *extract_pair(z80, opcode);
  814. uint16_t value = lh + rh;
  815. z80->regs.hl = value;
  816. bool h = !!(((lh & 0x0FFF) + (rh & 0x0FFF)) & 0x1000);
  817. update_flags(z80, (lh + rh) != value, 0, 0, !!(value & 0x0800), h,
  818. !!(value & 0x2000), 0, 0, 0x3B);
  819. z80->regs.pc++;
  820. return 11;
  821. }
  822. // ADC HL, ss
  823. /*
  824. SBC HL, ss (0xED42, 0xED52, 0xED62, 0xED72):
  825. Subtract ss with carry from HL.
  826. */
  827. static uint8_t z80_inst_sbc_hl_ss(Z80 *z80, uint8_t opcode)
  828. {
  829. uint16_t minu = z80->regs.hl;
  830. uint16_t subtra = *extract_pair(z80, opcode) + get_flag(z80, FLAG_CARRY);
  831. uint16_t value = minu - subtra;
  832. z80->regs.hl = value;
  833. bool c = (minu - subtra) != value;
  834. bool ov = (minu - subtra) != ((int16_t) value);
  835. bool hc = !!(((minu & 0x0FFF) - (subtra & 0x0FFF)) & 0x1000);
  836. update_flags(z80, c, 1, ov, !!(value & 0x0800), hc,
  837. !!(value & 0x2000), value == 0, !!(value & 0x8000), 0xFF);
  838. z80->regs.pc++;
  839. return 15;
  840. }
  841. // ADD IXY, pp
  842. /*
  843. INC ss (0x03, 0x13, 0x23, 0x33):
  844. Increment ss (16-bit register).
  845. */
  846. static uint8_t z80_inst_inc_ss(Z80 *z80, uint8_t opcode)
  847. {
  848. (*extract_pair(z80, opcode))++;
  849. z80->regs.pc++;
  850. return 6;
  851. }
  852. // INC IXY
  853. /*
  854. DEC ss (0x0B, 0x1B, 0x2B, 0x3B):
  855. Decrement ss (16-bit register).
  856. */
  857. static uint8_t z80_inst_dec_ss(Z80 *z80, uint8_t opcode)
  858. {
  859. (*extract_pair(z80, opcode))--;
  860. z80->regs.pc++;
  861. return 6;
  862. }
  863. // DEC IXY
  864. // RLCA
  865. // RLA
  866. // RRCA
  867. // RRA
  868. // RLC r
  869. // RLC (HL)
  870. // RLC (IXY+d)
  871. // RL r
  872. // RL (HL)
  873. // RL (IXY+d)
  874. // RRC r
  875. // RRC (HL)
  876. // RRC (IXY+d)
  877. // RR r
  878. // RR (HL)
  879. // RR (IXY+d)
  880. // SLA r
  881. // SLA (HL)
  882. // SLA (IXY+d)
  883. // SRA r
  884. // SRA (HL)
  885. // SRA (IXY+d)
  886. // SRL r
  887. // SRL (HL)
  888. // SRL (IXY+d)
  889. // RLD
  890. // RRD
  891. /*
  892. BIT b, r (0xCB40, 0xCB41, 0xCB42, 0xCB43, 0xCB44, 0xCB45, 0xCB47, 0xCB48,
  893. 0xCB49, 0xCB4A, 0xCB4B, 0xCB4C, 0xCB4D, 0xCB4F, 0xCB50, 0xCB51, 0xCB52,
  894. 0xCB53, 0xCB54, 0xCB55, 0xCB57, 0xCB58, 0xCB59, 0xCB5A, 0xCB5B, 0xCB5C,
  895. 0xCB5D, 0xCB5F, 0xCB60, 0xCB61, 0xCB62, 0xCB63, 0xCB64, 0xCB65, 0xCB67,
  896. 0xCB68, 0xCB69, 0xCB6A, 0xCB6B, 0xCB6C, 0xCB6D, 0xCB6F, 0xCB70, 0xCB71,
  897. 0xCB72, 0xCB73, 0xCB74, 0xCB75, 0xCB77, 0xCB78, 0xCB79, 0xCB7A, 0xCB7B,
  898. 0xCB7C, 0xCB7D, 0xCB7F):
  899. Test bit b of r (8-bit register).
  900. */
  901. static uint8_t z80_inst_bit_b_r(Z80 *z80, uint8_t opcode)
  902. {
  903. uint8_t *reg = extract_reg(z80, opcode << 3);
  904. uint8_t bit = (opcode >> 3) & 0x07;
  905. bool z = (((*reg) >> bit) & 1) == 0;
  906. if (z)
  907. update_flags(z80, 0, 0, 1, 0, 1, 0, 1, 0, 0xFE);
  908. else
  909. update_flags(z80, 0, 0, 0, bit == 3, 1, bit == 5, 0, bit == 7, 0xFE);
  910. z80->regs.pc++;
  911. return 8;
  912. }
  913. /*
  914. BIT b, (HL) (0xCB46, 0xCB4E, 0xCB56, 0xCB5E, 0xCB66, 0xCB6E, 0xCB76,
  915. 0xCB7E):
  916. Test bit b of (HL).
  917. */
  918. static uint8_t z80_inst_bit_b_hl(Z80 *z80, uint8_t opcode)
  919. {
  920. uint8_t val = mmu_read_byte(z80->mmu, z80->regs.hl);
  921. uint8_t bit = (opcode >> 3) & 0x07;
  922. bool z = ((val >> bit) & 1) == 0;
  923. if (z)
  924. update_flags(z80, 0, 0, 1, 0, 1, 0, 1, 0, 0xFE);
  925. else
  926. update_flags(z80, 0, 0, 0, bit == 3, 1, bit == 5, 0, bit == 7, 0xFE);
  927. z80->regs.pc++;
  928. return 8;
  929. }
  930. /*
  931. BIT b, (IXY+d) (0xDDCB40-0xDDCB7F, 0xFDCB40-0xFDCB7F):
  932. Test bit b of (IX+d) or (IY+d).
  933. */
  934. static uint8_t z80_inst_bit_b_ixy(Z80 *z80, uint8_t opcode)
  935. {
  936. uint16_t addr = get_index_addr(z80, z80->regs.pc - 1);
  937. uint8_t val = mmu_read_byte(z80->mmu, addr);
  938. uint8_t bit = (opcode >> 3) & 0x07;
  939. bool z = ((val >> bit) & 1) == 0;
  940. if (z)
  941. update_flags(z80, 0, 0, 1, 0, 1, 0, 1, 0, 0xFE);
  942. else
  943. update_flags(z80, 0, 0, 0, bit == 3, 1, bit == 5, 0, bit == 7, 0xFE);
  944. z80->regs.pc++;
  945. return 8;
  946. }
  947. // SET b, r
  948. // SET b, (HL)
  949. // SET b, (IXY+d)
  950. // RES b, r
  951. /*
  952. RES b, (HL) (0xCB86, 0xCB8E, 0xCB96, 0xCB9E, 0xCBA6, 0xCBAE, 0xCBB6,
  953. 0xCBBE):
  954. Reset bit b of (HL).
  955. */
  956. static uint8_t z80_inst_res_b_hl(Z80 *z80, uint8_t opcode)
  957. {
  958. uint8_t val = mmu_read_byte(z80->mmu, z80->regs.hl);
  959. uint8_t bit = (opcode >> 3) & 0x07;
  960. val &= ~(1 << bit);
  961. mmu_write_byte(z80->mmu, z80->regs.hl, val);
  962. z80->regs.pc++;
  963. return 8;
  964. }
  965. // RES b, (IXY+d)
  966. /*
  967. JP nn (0xC3):
  968. Jump to nn (16-bit immediate).
  969. */
  970. static uint8_t z80_inst_jp_nn(Z80 *z80, uint8_t opcode)
  971. {
  972. (void) opcode;
  973. z80->regs.pc = mmu_read_double(z80->mmu, ++z80->regs.pc);
  974. return 10;
  975. }
  976. /*
  977. JP cc, nn (0xC2, 0xCA, 0xD2, 0xDA, 0xE2, 0xEA, 0xF2, 0xFA):
  978. Jump to nn (16-bit immediate) if cc (condition) is true.
  979. */
  980. static uint8_t z80_inst_jp_cc_nn(Z80 *z80, uint8_t opcode)
  981. {
  982. if (extract_cond(z80, opcode))
  983. z80->regs.pc = mmu_read_double(z80->mmu, ++z80->regs.pc);
  984. else
  985. z80->regs.pc += 3;
  986. return 10;
  987. }
  988. /*
  989. JR e (0x18):
  990. Relative jump e (signed 8-bit immediate) bytes.
  991. */
  992. static uint8_t z80_inst_jr_e(Z80 *z80, uint8_t opcode)
  993. {
  994. (void) opcode;
  995. int8_t jump = mmu_read_byte(z80->mmu, z80->regs.pc + 1);
  996. z80->regs.pc += jump + 2;
  997. return 12;
  998. }
  999. /*
  1000. JR cc, e (0x20, 0x28, 0x30, 0x38):
  1001. Relative jump e (signed 8-bit immediate) bytes if cc (condition) is true.
  1002. */
  1003. static uint8_t z80_inst_jr_cc_e(Z80 *z80, uint8_t opcode)
  1004. {
  1005. if (extract_cond(z80, opcode - 0x20)) {
  1006. int8_t jump = mmu_read_byte(z80->mmu, z80->regs.pc + 1);
  1007. z80->regs.pc += jump + 2;
  1008. return 12;
  1009. } else {
  1010. z80->regs.pc += 2;
  1011. return 7;
  1012. }
  1013. }
  1014. /*
  1015. JP (HL) (0xE9):
  1016. Jump to HL (*NOT* the memory pointed to by HL).
  1017. */
  1018. static uint8_t z80_inst_jp_hl(Z80 *z80, uint8_t opcode)
  1019. {
  1020. (void) opcode;
  1021. z80->regs.pc = z80->regs.hl;
  1022. return 4;
  1023. }
  1024. /*
  1025. JP (IXY) (0xDDE9, 0xFDE9):
  1026. Jump to IX or IY.
  1027. */
  1028. static uint8_t z80_inst_jp_ixy(Z80 *z80, uint8_t opcode)
  1029. {
  1030. (void) opcode;
  1031. z80->regs.pc = *z80->last_index;
  1032. return 8;
  1033. }
  1034. /*
  1035. DJNZ, e (0x10):
  1036. Decrement B and relative jump e bytes (signed 8-bit immediate) if non-zero.
  1037. */
  1038. static uint8_t z80_inst_djnz_e(Z80 *z80, uint8_t opcode)
  1039. {
  1040. (void) opcode;
  1041. z80->regs.b--;
  1042. if (z80->regs.b != 0) {
  1043. int8_t jump = mmu_read_byte(z80->mmu, z80->regs.pc + 1);
  1044. z80->regs.pc += jump + 2;
  1045. return 13;
  1046. } else {
  1047. z80->regs.pc += 2;
  1048. return 8;
  1049. }
  1050. }
  1051. /*
  1052. CALL nn (0xCD):
  1053. Push PC+3 onto the stack and jump to nn (16-bit immediate).
  1054. */
  1055. static uint8_t z80_inst_call_nn(Z80 *z80, uint8_t opcode)
  1056. {
  1057. (void) opcode;
  1058. stack_push(z80, z80->regs.pc + 3);
  1059. z80->regs.pc = mmu_read_double(z80->mmu, ++z80->regs.pc);
  1060. return 17;
  1061. }
  1062. /*
  1063. CALL cc, nn (0xC4, 0xCC, 0xD4, 0xDC, 0xE4, 0xEC, 0xF4, 0xFC):
  1064. Push PC+3 onto the stack and jump to nn (16-bit immediate) if cc is true.
  1065. */
  1066. static uint8_t z80_inst_call_cc_nn(Z80 *z80, uint8_t opcode)
  1067. {
  1068. if (extract_cond(z80, opcode)) {
  1069. stack_push(z80, z80->regs.pc + 3);
  1070. z80->regs.pc = mmu_read_double(z80->mmu, ++z80->regs.pc);
  1071. return 17;
  1072. } else {
  1073. z80->regs.pc += 3;
  1074. return 10;
  1075. }
  1076. }
  1077. /*
  1078. RET (0xC9):
  1079. Pop PC from the stack.
  1080. */
  1081. static uint8_t z80_inst_ret(Z80 *z80, uint8_t opcode)
  1082. {
  1083. (void) opcode;
  1084. z80->regs.pc = stack_pop(z80);
  1085. return 10;
  1086. }
  1087. /*
  1088. RET cc (0xC0, 0xC8, 0xD0, 0xD8, 0xE0, 0xE8, 0xF0, 0xF8):
  1089. Pop PC from the stack if cc is true.
  1090. */
  1091. static uint8_t z80_inst_ret_cc(Z80 *z80, uint8_t opcode)
  1092. {
  1093. if (extract_cond(z80, opcode)) {
  1094. z80->regs.pc = stack_pop(z80);
  1095. return 11;
  1096. } else {
  1097. z80->regs.pc++;
  1098. return 5;
  1099. }
  1100. }
  1101. /*
  1102. RETI (0xED4D):
  1103. Pop PC from the stack.
  1104. */
  1105. static uint8_t z80_inst_reti(Z80 *z80, uint8_t opcode)
  1106. {
  1107. (void) opcode;
  1108. z80->regs.pc = stack_pop(z80);
  1109. return 14;
  1110. }
  1111. /*
  1112. RETN (0xED45, 0xED55, 0xED5D, 0xED65, 0xED6D, 0xED75, 0xED7D):
  1113. Pop PC from the stack, and copy to IFF2 to IFF1.
  1114. */
  1115. static uint8_t z80_inst_retn(Z80 *z80, uint8_t opcode)
  1116. {
  1117. (void) opcode;
  1118. z80->regs.pc = stack_pop(z80);
  1119. z80->regs.iff1 = z80->regs.iff2;
  1120. return 14;
  1121. }
  1122. /*
  1123. RST p (0xC7, 0xCF, 0xD7, 0xDF, 0xE7, 0xEF, 0xF7, 0xFF):
  1124. Push PC+1 onto the stack and jump to p (opcode & 0x38).
  1125. */
  1126. static uint8_t z80_inst_rst_p(Z80 *z80, uint8_t opcode)
  1127. {
  1128. stack_push(z80, z80->regs.pc + 1);
  1129. z80->regs.pc = opcode & 0x38;
  1130. return 11;
  1131. }
  1132. /*
  1133. IN A, (n) (0xDB):
  1134. Read a byte from port n into A.
  1135. */
  1136. static uint8_t z80_inst_in_a_n(Z80 *z80, uint8_t opcode)
  1137. {
  1138. (void) opcode;
  1139. uint8_t port = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  1140. z80->regs.a = read_port(z80, port);
  1141. z80->regs.pc++;
  1142. return 11;
  1143. }
  1144. /*
  1145. IN r, (C) (0xED40, 0xED48, 0xED50, 0xED58, 0xED60, 0xED68, 0xED70, 0xED78):
  1146. Read a byte from port C into r, or affect flags only if 0xED70.
  1147. */
  1148. static uint8_t z80_inst_in_r_c(Z80 *z80, uint8_t opcode)
  1149. {
  1150. uint8_t data = read_port(z80, z80->regs.c);
  1151. bool parity = !(__builtin_popcount(data) % 2);
  1152. if (opcode != 0x70)
  1153. *extract_reg(z80, opcode) = data;
  1154. update_flags(z80, 0, 0, parity, !!(data & 0x08), 0, !!(data & 0x20),
  1155. data == 0, !!(data & 0x80), 0xFE);
  1156. z80->regs.pc++;
  1157. return 12;
  1158. }
  1159. /*
  1160. INI (0xEDA2):
  1161. IN (HL), (C); INC HL; DEC B
  1162. */
  1163. static uint8_t z80_inst_ini(Z80 *z80, uint8_t opcode)
  1164. {
  1165. (void) opcode;
  1166. uint8_t data = read_port(z80, z80->regs.c), *b = &z80->regs.b;
  1167. bool h = !!(((*b & 0x0F) - 1) & 0x10);
  1168. mmu_write_byte(z80->mmu, z80->regs.hl, data);
  1169. z80->regs.hl++;
  1170. (*b)--;
  1171. update_flags(z80, 0, 1, *b == 0x7F, !!(*b & 0x08), h, !!(*b & 0x20),
  1172. *b == 0, !!(*b & 0x80), 0xFE);
  1173. z80->regs.pc++;
  1174. return 16;
  1175. }
  1176. /*
  1177. INIR (0xEDB2):
  1178. INI; JR NZ, -2
  1179. */
  1180. static uint8_t z80_inst_inir(Z80 *z80, uint8_t opcode)
  1181. {
  1182. z80_inst_ini(z80, opcode);
  1183. if (z80->regs.b == 0)
  1184. return 16;
  1185. z80->regs.pc -= 2;
  1186. return 21;
  1187. }
  1188. /*
  1189. IND (0xEDAA):
  1190. IN (HL), (C); DEC HL; DEC B
  1191. */
  1192. static uint8_t z80_inst_ind(Z80 *z80, uint8_t opcode)
  1193. {
  1194. (void) opcode;
  1195. uint8_t data = read_port(z80, z80->regs.c), *b = &z80->regs.b;
  1196. bool h = !!(((*b & 0x0F) - 1) & 0x10);
  1197. mmu_write_byte(z80->mmu, z80->regs.hl, data);
  1198. z80->regs.hl--;
  1199. (*b)--;
  1200. update_flags(z80, 0, 1, *b == 0x7F, !!(*b & 0x08), h, !!(*b & 0x20),
  1201. *b == 0, !!(*b & 0x80), 0xFE);
  1202. z80->regs.pc++;
  1203. return 16;
  1204. }
  1205. /*
  1206. INDR (0xEDBA):
  1207. IND; JR NZ, -2
  1208. */
  1209. static uint8_t z80_inst_indr(Z80 *z80, uint8_t opcode)
  1210. {
  1211. z80_inst_ind(z80, opcode);
  1212. if (z80->regs.b == 0)
  1213. return 16;
  1214. z80->regs.pc -= 2;
  1215. return 21;
  1216. }
  1217. /*
  1218. OUT (n), A (0xD3):
  1219. Write a byte from A into port n.
  1220. */
  1221. static uint8_t z80_inst_out_n_a(Z80 *z80, uint8_t opcode)
  1222. {
  1223. (void) opcode;
  1224. uint8_t port = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  1225. write_port(z80, port, z80->regs.a);
  1226. z80->regs.pc++;
  1227. return 11;
  1228. }
  1229. /*
  1230. OUT (C), r (0xED41, 0xED49, 0xED51, 0xED59, 0xED61, 0xED69, 0xED71,
  1231. 0xED79):
  1232. Write a byte from r (8-bit reg, or 0 if 0xED71) into port C.
  1233. */
  1234. static uint8_t z80_inst_out_c_r(Z80 *z80, uint8_t opcode)
  1235. {
  1236. uint8_t value = opcode != 0x71 ? *extract_reg(z80, opcode) : 0;
  1237. write_port(z80, z80->regs.c, value);
  1238. z80->regs.pc++;
  1239. return 12;
  1240. }
  1241. /*
  1242. OUTI (0xEDA3):
  1243. OUT (C), (HL); INC HL; DEC B
  1244. */
  1245. static uint8_t z80_inst_outi(Z80 *z80, uint8_t opcode)
  1246. {
  1247. (void) opcode;
  1248. uint8_t *b = &z80->regs.b;
  1249. bool h = !!(((*b & 0x0F) - 1) & 0x10);
  1250. write_port(z80, z80->regs.c, mmu_read_byte(z80->mmu, z80->regs.hl));
  1251. z80->regs.hl++;
  1252. (*b)--;
  1253. update_flags(z80, 0, 1, *b == 0x7F, !!(*b & 0x08), h, !!(*b & 0x20),
  1254. *b == 0, !!(*b & 0x80), 0xFE);
  1255. z80->regs.pc++;
  1256. return 16;
  1257. }
  1258. /*
  1259. OTIR (0xEDB3):
  1260. OUTI; JR NZ, -2
  1261. */
  1262. static uint8_t z80_inst_otir(Z80 *z80, uint8_t opcode)
  1263. {
  1264. z80_inst_outi(z80, opcode);
  1265. if (z80->regs.b == 0)
  1266. return 16;
  1267. z80->regs.pc -= 2;
  1268. return 21;
  1269. }
  1270. /*
  1271. OUTD (0xEDAB):
  1272. OUT (C), (HL); DEC HL; DEC B
  1273. */
  1274. static uint8_t z80_inst_outd(Z80 *z80, uint8_t opcode)
  1275. {
  1276. (void) opcode;
  1277. uint8_t *b = &z80->regs.b;
  1278. bool h = !!(((*b & 0x0F) - 1) & 0x10);
  1279. write_port(z80, z80->regs.c, mmu_read_byte(z80->mmu, z80->regs.hl));
  1280. z80->regs.hl--;
  1281. (*b)--;
  1282. update_flags(z80, 0, 1, *b == 0x7F, !!(*b & 0x08), h, !!(*b & 0x20),
  1283. *b == 0, !!(*b & 0x80), 0xFE);
  1284. z80->regs.pc++;
  1285. return 16;
  1286. }
  1287. /*
  1288. OTDR (0xEDBB):
  1289. OUTD; JR NZ, -2
  1290. */
  1291. static uint8_t z80_inst_otdr(Z80 *z80, uint8_t opcode)
  1292. {
  1293. z80_inst_outd(z80, opcode);
  1294. if (z80->regs.b == 0)
  1295. return 16;
  1296. z80->regs.pc -= 2;
  1297. return 21;
  1298. }
  1299. /*
  1300. NOP2:
  1301. No operation is performed twice; i.e., 2 NOPs-worth of cycles are spent.
  1302. Used for unimplemented extended and index instructions.
  1303. */
  1304. static uint8_t z80_inst_nop2(Z80 *z80, uint8_t opcode)
  1305. {
  1306. (void) opcode;
  1307. z80->regs.pc++;
  1308. return 8;
  1309. }
  1310. /*
  1311. 0xED:
  1312. Handle an extended instruction.
  1313. */
  1314. static uint8_t z80_prefix_extended(Z80 *z80, uint8_t opcode)
  1315. {
  1316. opcode = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  1317. return (*instruction_table_extended[opcode])(z80, opcode);
  1318. }
  1319. /*
  1320. 0xED:
  1321. Handle a bit instruction.
  1322. */
  1323. static uint8_t z80_prefix_bits(Z80 *z80, uint8_t opcode)
  1324. {
  1325. opcode = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  1326. return (*instruction_table_bits[opcode])(z80, opcode);
  1327. }
  1328. /*
  1329. 0xDD, 0xFD:
  1330. Handle an index instruction.
  1331. */
  1332. static uint8_t z80_prefix_index(Z80 *z80, uint8_t opcode)
  1333. {
  1334. z80->last_index = (opcode == 0xDD) ? &z80->regs.ix : &z80->regs.iy;
  1335. opcode = mmu_read_byte(z80->mmu, ++z80->regs.pc);
  1336. return (*instruction_table_index[opcode])(z80, opcode);
  1337. }
  1338. /*
  1339. 0xDDCB, 0xFDCB:
  1340. Handle an index-bit instruction.
  1341. */
  1342. static uint8_t z80_prefix_index_bits(Z80 *z80, uint8_t opcode)
  1343. {
  1344. opcode = mmu_read_byte(z80->mmu, z80->regs.pc += 2);
  1345. return (*instruction_table_index_bits[opcode])(z80, opcode);
  1346. }
  1347. #include "z80_tables.inc.c"