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.
 
 
 
 
 

752 lines
14 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->regfile.pc++;
  42. return 4;
  43. }
  44. /*
  45. LD r, n (0x06, 0x0E, 0x16, 0x1E, 0x26, 0x2E, 0x3E):
  46. Load n (8-bit immediate) into r (8-bit register).
  47. */
  48. static uint8_t z80_inst_ld_r_n(Z80 *z80, uint8_t opcode)
  49. {
  50. uint8_t *reg = extract_reg(z80, opcode);
  51. *reg = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  52. z80->regfile.pc++;
  53. return 7;
  54. }
  55. /*
  56. LD r, (HL) (0x46, 0x4E, 0x56, 0x5E, 0x66, 0x6E, 0x7E):
  57. Load the contents of HL into r (8-bit register).
  58. */
  59. static uint8_t z80_inst_ld_r_hl(Z80 *z80, uint8_t opcode)
  60. {
  61. uint8_t *reg = extract_reg(z80, opcode);
  62. *reg = mmu_read_byte(z80->mmu, get_pair(z80, REG_HL));
  63. z80->regfile.pc++;
  64. return 7;
  65. }
  66. /*
  67. LD r, (IX+d)
  68. */
  69. // static uint8_t z80_inst_ld_r_ix(Z80 *z80, uint8_t opcode)
  70. /*
  71. LD r, (IY+d)
  72. */
  73. // static uint8_t z80_inst_ld_r_iy(Z80 *z80, uint8_t opcode)
  74. /*
  75. LD (HL), r
  76. */
  77. // static uint8_t z80_inst_ld_hl_r(Z80 *z80, uint8_t opcode)
  78. /*
  79. LD (IX+d), r
  80. */
  81. // static uint8_t z80_inst_ld_ix_r(Z80 *z80, uint8_t opcode)
  82. /*
  83. LD (IY+d), r
  84. */
  85. // static uint8_t z80_inst_ld_iy_r(Z80 *z80, uint8_t opcode)
  86. /*
  87. LD (HL), n (0x36):
  88. Load n (8-bit immediate) into the memory address pointed to by HL.
  89. */
  90. static uint8_t z80_inst_ld_hl_n(Z80 *z80, uint8_t opcode)
  91. {
  92. (void) opcode;
  93. uint16_t addr = get_pair(z80, REG_HL);
  94. uint8_t byte = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  95. mmu_write_byte(z80->mmu, addr, byte);
  96. z80->regfile.pc++;
  97. return 10;
  98. }
  99. /*
  100. LD (IX+d), n
  101. */
  102. // static uint8_t z80_inst_ld_ix_n(Z80 *z80, uint8_t opcode)
  103. /*
  104. LD (IY+d), n
  105. */
  106. // static uint8_t z80_inst_ld_iy_n(Z80 *z80, uint8_t opcode)
  107. /*
  108. LD A, (BC)
  109. */
  110. // static uint8_t z80_inst_ld_a_bc(Z80 *z80, uint8_t opcode)
  111. /*
  112. LD A, (DE)
  113. */
  114. // static uint8_t z80_inst_ld_a_de(Z80 *z80, uint8_t opcode)
  115. /*
  116. LD A, (nn)
  117. */
  118. // static uint8_t z80_inst_ld_a_nn(Z80 *z80, uint8_t opcode)
  119. /*
  120. LD (BC), A
  121. */
  122. // static uint8_t z80_inst_ld_bc_a(Z80 *z80, uint8_t opcode)
  123. /*
  124. LD (DE), A
  125. */
  126. // static uint8_t z80_inst_ld_de_a(Z80 *z80, uint8_t opcode)
  127. /*
  128. LD (nn), A (0x32):
  129. Load a into memory address nn.
  130. */
  131. static uint8_t z80_inst_ld_nn_a(Z80 *z80, uint8_t opcode)
  132. {
  133. (void) opcode;
  134. uint16_t addr = mmu_read_double(z80->mmu, ++z80->regfile.pc);
  135. mmu_write_byte(z80->mmu, addr, z80->regfile.a);
  136. z80->regfile.pc += 2;
  137. return 13;
  138. }
  139. /*
  140. LD A, I
  141. */
  142. // static uint8_t z80_inst_ld_a_i(Z80 *z80, uint8_t opcode)
  143. /*
  144. LD A, R
  145. */
  146. // static uint8_t z80_inst_ld_a_r(Z80 *z80, uint8_t opcode)
  147. /*
  148. LD I,A
  149. */
  150. // static uint8_t z80_inst_ld_i_a(Z80 *z80, uint8_t opcode)
  151. /*
  152. LD R, A
  153. */
  154. // static uint8_t z80_inst_ld_r_a(Z80 *z80, uint8_t opcode)
  155. /*
  156. LD dd, nn (0x01, 0x11, 0x21, 0x31):
  157. Load nn (16-bit immediate) into dd (16-bit register).
  158. */
  159. static uint8_t z80_inst_ld_dd_nn(Z80 *z80, uint8_t opcode)
  160. {
  161. uint8_t pair = extract_pair(opcode);
  162. set_pair(z80, pair, mmu_read_double(z80->mmu, ++z80->regfile.pc));
  163. z80->regfile.pc += 2;
  164. return 10;
  165. }
  166. // LD IX, nn
  167. // LD IY, nn
  168. // LD HL, (nn)
  169. // LD dd, (nn)
  170. // LD IX, (nn)
  171. // LD IY, (nn)
  172. // LD (nn), HL
  173. // LD (nn), dd
  174. // LD (nn), IX
  175. // LD (nn), IY
  176. // LD SP, HL
  177. // LD SP, IX
  178. // LD SP, IY
  179. /*
  180. PUSH qq (0xC5, 0xD5, 0xE5, 0xF5):
  181. Push qq onto the stack, and decrement SP by two.
  182. */
  183. static uint8_t z80_inst_push_qq(Z80 *z80, uint8_t opcode)
  184. {
  185. uint8_t pair = extract_pair(opcode);
  186. stack_push(z80, pair);
  187. z80->regfile.pc++;
  188. return 11;
  189. }
  190. // PUSH IX
  191. // PUSH IY
  192. // POP qq
  193. // POP IX
  194. // POP IY
  195. // EX DE, HL
  196. // EX AF, AF′
  197. /*
  198. EXX (0xD9):
  199. Exchange the 16-bit registers with their shadows
  200. (BC <=> BC', DE <=> DE', HL <=> HL').
  201. */
  202. static uint8_t z80_inst_exx(Z80 *z80, uint8_t opcode)
  203. {
  204. (void) opcode;
  205. uint16_t bc = get_pair(z80, REG_BC),
  206. de = get_pair(z80, REG_DE),
  207. hl = get_pair(z80, REG_HL);
  208. set_pair(z80, REG_BC, get_pair(z80, REG_BC_));
  209. set_pair(z80, REG_DE, get_pair(z80, REG_DE_));
  210. set_pair(z80, REG_HL, get_pair(z80, REG_HL_));
  211. set_pair(z80, REG_BC_, bc);
  212. set_pair(z80, REG_DE_, de);
  213. set_pair(z80, REG_HL_, hl);
  214. z80->regfile.pc++;
  215. return 4;
  216. }
  217. // EX (SP), HL
  218. // EX (SP), IX
  219. // EX (SP), IY
  220. // LDI
  221. // LDIR
  222. // LDD
  223. // LDDR
  224. // CPI
  225. // CPIR
  226. // CPD
  227. // CPDR
  228. // ADD A, r
  229. // ADD A, n
  230. // ADD A, (HL)
  231. // ADD A, (IX + d)
  232. // ADD A, (IY + d)
  233. // ADC A, s
  234. // SUB s
  235. // SBC A, s
  236. // AND s
  237. // OR s
  238. // XOR s
  239. /*
  240. XOR r (0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAF):
  241. Bitwise XOR a with r (8-bit register).
  242. */
  243. static uint8_t z80_inst_xor_r(Z80 *z80, uint8_t opcode)
  244. {
  245. uint8_t *reg = extract_reg(z80, opcode);
  246. uint8_t a = (z80->regfile.a ^= *reg);
  247. bool parity = !(__builtin_popcount(a) % 2);
  248. update_flags(z80, 0, 0, parity, !!(a & 0x08), 0, !!(a & 0x20), a == 0,
  249. !!(a & 0x80), 0xFF);
  250. z80->regfile.pc++;
  251. return 4;
  252. }
  253. // CP s
  254. /*
  255. CP r (0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBF):
  256. Set flags as if r (8-bit register) had been subtracted from a.
  257. */
  258. static uint8_t z80_inst_cp_r(Z80 *z80, uint8_t opcode)
  259. {
  260. uint8_t *reg = extract_reg(z80, opcode);
  261. uint8_t d = z80->regfile.a - *reg;
  262. bool c = (z80->regfile.a - *reg) != d;
  263. bool v = (z80->regfile.a - *reg) != ((int8_t) d);
  264. bool h = !!(((z80->regfile.a & 0x0F) - (*reg & 0x0F)) & 0x10);
  265. update_flags(z80, c, 1, v, !!(*reg & 0x08), h, !!(*reg & 0x20), d == 0,
  266. !!(d & 0x80), 0xFF);
  267. z80->regfile.pc++;
  268. return 4;
  269. }
  270. /*
  271. CP n (0xFE):
  272. Set flags as if n (8-bit immediate) had been subtracted from a.
  273. */
  274. static uint8_t z80_inst_cp_n(Z80 *z80, uint8_t opcode)
  275. {
  276. (void) opcode;
  277. uint8_t n = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  278. uint8_t d = z80->regfile.a - n;
  279. bool c = (z80->regfile.a - n) != d;
  280. bool v = (z80->regfile.a - n) != ((int8_t) d);
  281. bool h = !!(((z80->regfile.a & 0x0F) - (n & 0x0F)) & 0x10);
  282. update_flags(z80, c, 1, v, !!(n & 0x08), h, !!(n & 0x20), d == 0,
  283. !!(d & 0x80), 0xFF);
  284. z80->regfile.pc++;
  285. return 7;
  286. }
  287. /*
  288. INC r (0x04, 0x0C, 0x14, 0x1C, 0x24, 0x2C, 0x3C):
  289. Increment r (8-bit register).
  290. */
  291. static uint8_t z80_inst_inc_r(Z80 *z80, uint8_t opcode)
  292. {
  293. uint8_t *reg = extract_reg(z80, opcode);
  294. bool halfcarry = !!(((*reg & 0x0F) + 1) & 0x10);
  295. (*reg)++;
  296. update_flags(z80, 0, 0, *reg == 0x80, !!(*reg & 0x08), halfcarry,
  297. !!(*reg & 0x20), *reg == 0, !!(*reg & 0x80), 0xFE);
  298. z80->regfile.pc++;
  299. return 4;
  300. }
  301. // INC (HL)
  302. // INC (IX+d)
  303. // INC (IY+d)
  304. // DEC m
  305. // DAA
  306. // CPL
  307. // NEG
  308. // CCF
  309. // SCF
  310. /*
  311. NOP (0x00):
  312. No operation is performed.
  313. */
  314. static uint8_t z80_inst_nop(Z80 *z80, uint8_t opcode)
  315. {
  316. (void) opcode;
  317. z80->regfile.pc++;
  318. return 4;
  319. }
  320. /*
  321. HALT (0x76):
  322. Suspend CPU operation: execute NOPs until an interrupt or reset.
  323. */
  324. static uint8_t z80_inst_halt(Z80 *z80, uint8_t opcode)
  325. {
  326. (void) z80;
  327. (void) opcode;
  328. return 4;
  329. }
  330. /*
  331. DI (0xF3):
  332. Disable maskable interrupts by resetting both flip-flops.
  333. */
  334. static uint8_t z80_inst_di(Z80 *z80, uint8_t opcode)
  335. {
  336. (void) opcode;
  337. z80->regfile.iff1 = false;
  338. z80->regfile.iff2 = false;
  339. z80->regfile.pc++;
  340. return 4;
  341. }
  342. /*
  343. EI (0xFB):
  344. Enable maskable interrupts by setting both flip-flops.
  345. */
  346. static uint8_t z80_inst_ei(Z80 *z80, uint8_t opcode)
  347. {
  348. (void) opcode;
  349. z80->regfile.iff1 = true;
  350. z80->regfile.iff2 = true;
  351. z80->regfile.pc++;
  352. return 4;
  353. }
  354. /*
  355. IM (0xED46, 0xED4E, 0xED56, 0xED5E, 0xED66, 0xED6E, 0xED76, 0xED7E):
  356. Set the interrupt mode.
  357. */
  358. static uint8_t z80_inst_im(Z80 *z80, uint8_t opcode)
  359. {
  360. switch (opcode) {
  361. case 0x46:
  362. case 0x4E:
  363. case 0x66:
  364. case 0x6E:
  365. z80->regfile.im_a = false; // Interrupt mode 0
  366. z80->regfile.im_b = false;
  367. break;
  368. case 0x56:
  369. case 0x76:
  370. z80->regfile.im_a = true; // Interrupt mode 1
  371. z80->regfile.im_b = false;
  372. break;
  373. case 0x5E:
  374. case 0x7E:
  375. z80->regfile.im_a = true; // Interrupt mode 2
  376. z80->regfile.im_b = true;
  377. break;
  378. }
  379. z80->regfile.pc++;
  380. return 8;
  381. }
  382. // ADD HL, ss
  383. // ADC HL, ss
  384. // SBC HL, ss
  385. // ADD IX, pp
  386. // ADD IY, rr
  387. /*
  388. INC ss (0x03, 0x13, 0x23, 0x33):
  389. Increment ss (16-bit register).
  390. */
  391. static uint8_t z80_inst_inc_ss(Z80 *z80, uint8_t opcode)
  392. {
  393. uint8_t pair = extract_pair(opcode);
  394. set_pair(z80, pair, get_pair(z80, pair) + 1);
  395. z80->regfile.pc++;
  396. return 6;
  397. }
  398. // INC IX
  399. // INC IY
  400. // DEC ss
  401. // DEC IX
  402. // DEC IY
  403. // RLCA
  404. // RLA
  405. // RRCA
  406. // RRA
  407. // RLC r
  408. // RLC (HL)
  409. // RLC (IX+d)
  410. // RLC (IY+d)
  411. // RL m
  412. // RRC m
  413. // RR m
  414. // SLA m
  415. // SRA m
  416. // SRL m
  417. // RLD
  418. // RRD
  419. // BIT b, r
  420. // BIT b, (HL)
  421. // BIT b, (IX+d)
  422. // BIT b, (IY+d)
  423. // SET b, r
  424. // SET b, (HL)
  425. // SET b, (IX+d)
  426. // SET b, (IY+d)
  427. // RES b, m
  428. /*
  429. JP nn (0xC3):
  430. Jump to nn (16-bit immediate).
  431. */
  432. static uint8_t z80_inst_jp_nn(Z80 *z80, uint8_t opcode)
  433. {
  434. (void) opcode;
  435. z80->regfile.pc = mmu_read_double(z80->mmu, ++z80->regfile.pc);
  436. return 10;
  437. }
  438. /*
  439. JP cc, nn (0xC2, 0xCA, 0xD2, 0xDA, 0xE2, 0xEA, 0xF2, 0xFA):
  440. Jump to nn (16-bit immediate) if cc (condition) is true.
  441. */
  442. static uint8_t z80_inst_jp_cc_nn(Z80 *z80, uint8_t opcode)
  443. {
  444. if (extract_cond(z80, opcode))
  445. z80->regfile.pc = mmu_read_double(z80->mmu, ++z80->regfile.pc);
  446. else
  447. z80->regfile.pc += 3;
  448. return 10;
  449. }
  450. /*
  451. JR e (0x18):
  452. Relative jump e (signed 8-bit immediate) bytes.
  453. */
  454. static uint8_t z80_inst_jr_e(Z80 *z80, uint8_t opcode)
  455. {
  456. (void) opcode;
  457. int8_t jump = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  458. z80->regfile.pc += jump + 2;
  459. return 12;
  460. }
  461. /*
  462. JR cc, e (0x20, 0x28, 0x30, 0x38):
  463. Relative jump e (signed 8-bit immediate) bytes if cc (condition) is true.
  464. */
  465. static uint8_t z80_inst_jr_cc_e(Z80 *z80, uint8_t opcode)
  466. {
  467. if (extract_cond(z80, opcode - 0x20)) {
  468. int8_t jump = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  469. z80->regfile.pc += jump + 2;
  470. return 12;
  471. } else {
  472. z80->regfile.pc += 2;
  473. return 7;
  474. }
  475. }
  476. // JP (HL)
  477. // JP (IX)
  478. // JP (IY)
  479. // DJNZ, e
  480. /*
  481. CALL nn (0xCD):
  482. Push PC+3 onto the stack and jump to nn (16-bit immediate).
  483. */
  484. static uint8_t z80_inst_call_nn(Z80 *z80, uint8_t opcode)
  485. {
  486. (void) opcode;
  487. stack_push(z80, z80->regfile.pc + 3);
  488. z80->regfile.pc = mmu_read_double(z80->mmu, ++z80->regfile.pc);
  489. return 17;
  490. }
  491. /*
  492. CALL cc, nn (0xC4, 0xCC, 0xD4, 0xDC, 0xE4, 0xEC, 0xF4, 0xFC):
  493. Push PC+3 onto the stack and jump to nn (16-bit immediate) if cc is true.
  494. */
  495. static uint8_t z80_inst_call_cc_nn(Z80 *z80, uint8_t opcode)
  496. {
  497. if (extract_cond(z80, opcode)) {
  498. stack_push(z80, z80->regfile.pc + 3);
  499. z80->regfile.pc = mmu_read_double(z80->mmu, ++z80->regfile.pc);
  500. return 17;
  501. } else {
  502. z80->regfile.pc += 3;
  503. return 10;
  504. }
  505. }
  506. // RET
  507. // RET cc
  508. // RETI
  509. // RETN
  510. // RST p
  511. /*
  512. IN A, (n): (0xDB):
  513. Read a byte from port n into a.
  514. */
  515. static uint8_t z80_inst_in_a_n(Z80 *z80, uint8_t opcode)
  516. {
  517. (void) opcode;
  518. uint8_t port = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  519. z80->regfile.a = read_port(z80, port);
  520. z80->regfile.pc++;
  521. return 11;
  522. }
  523. // IN r (C)
  524. // INI
  525. // INIR
  526. // IND
  527. // INDR
  528. /*
  529. OUT (n), A: (0xD3):
  530. Write a byte from a into port n.
  531. */
  532. static uint8_t z80_inst_out_n_a(Z80 *z80, uint8_t opcode)
  533. {
  534. (void) opcode;
  535. uint8_t port = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  536. write_port(z80, port, z80->regfile.a);
  537. z80->regfile.pc++;
  538. return 11;
  539. }
  540. // OUT (C), r
  541. // OUTI
  542. // OTIR
  543. // OUTD
  544. // OTDR
  545. /*
  546. 0xED:
  547. Handle an extended instruction.
  548. */
  549. static uint8_t z80_prefix_extended(Z80 *z80, uint8_t opcode)
  550. {
  551. opcode = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  552. return (*instruction_table_extended[opcode])(z80, opcode);
  553. }
  554. /*
  555. 0xED:
  556. Handle a bit instruction.
  557. */
  558. static uint8_t z80_prefix_bits(Z80 *z80, uint8_t opcode)
  559. {
  560. opcode = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  561. return (*instruction_table_bits[opcode])(z80, opcode);
  562. }
  563. /*
  564. 0xDD, 0xFD:
  565. Handle an index instruction.
  566. */
  567. static uint8_t z80_prefix_index(Z80 *z80, uint8_t opcode)
  568. {
  569. opcode = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  570. return (*instruction_table_index[opcode])(z80, opcode);
  571. }
  572. /*
  573. 0xDDCB, 0xFDCB:
  574. Handle an index-bit instruction.
  575. */
  576. static uint8_t z80_prefix_index_bits(Z80 *z80, uint8_t opcode)
  577. {
  578. opcode = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  579. return (*instruction_table_index_bits[opcode])(z80, opcode);
  580. }
  581. #include "z80_tables.inc.c"