An emulator, assembler, and disassembler for the Sega Game Gear
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 

593 řádky
9.5 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
  88. */
  89. // static uint8_t z80_inst_ld_hl_n(Z80 *z80, uint8_t opcode)
  90. /*
  91. LD (IX+d), n
  92. */
  93. // static uint8_t z80_inst_ld_ix_n(Z80 *z80, uint8_t opcode)
  94. /*
  95. LD (IY+d), n
  96. */
  97. // static uint8_t z80_inst_ld_iy_n(Z80 *z80, uint8_t opcode)
  98. /*
  99. LD A, (BC)
  100. */
  101. // static uint8_t z80_inst_ld_a_bc(Z80 *z80, uint8_t opcode)
  102. /*
  103. LD A, (DE)
  104. */
  105. // static uint8_t z80_inst_ld_a_de(Z80 *z80, uint8_t opcode)
  106. /*
  107. LD A, (nn)
  108. */
  109. // static uint8_t z80_inst_ld_a_nn(Z80 *z80, uint8_t opcode)
  110. /*
  111. LD (BC), A
  112. */
  113. // static uint8_t z80_inst_ld_bc_a(Z80 *z80, uint8_t opcode)
  114. /*
  115. LD (DE), A
  116. */
  117. // static uint8_t z80_inst_ld_de_a(Z80 *z80, uint8_t opcode)
  118. /*
  119. LD (nn), A
  120. */
  121. // static uint8_t z80_inst_ld_nn_a(Z80 *z80, uint8_t opcode)
  122. /*
  123. LD A, I
  124. */
  125. // static uint8_t z80_inst_ld_a_i(Z80 *z80, uint8_t opcode)
  126. /*
  127. LD A, R
  128. */
  129. // static uint8_t z80_inst_ld_a_r(Z80 *z80, uint8_t opcode)
  130. /*
  131. LD I,A
  132. */
  133. // static uint8_t z80_inst_ld_i_a(Z80 *z80, uint8_t opcode)
  134. /*
  135. LD R, A
  136. */
  137. // static uint8_t z80_inst_ld_r_a(Z80 *z80, uint8_t opcode)
  138. /*
  139. LD dd, nn (0x01, 0x11, 0x21, 0x31):
  140. Load nn (16-bit immediate) into dd (16-bit register).
  141. */
  142. static uint8_t z80_inst_ld_dd_nn(Z80 *z80, uint8_t opcode)
  143. {
  144. uint8_t pair = extract_pair(opcode);
  145. set_pair(z80, pair, mmu_read_double(z80->mmu, ++z80->regfile.pc));
  146. z80->regfile.pc += 2;
  147. return 10;
  148. }
  149. // LD IX, nn
  150. // LD IY, nn
  151. // LD HL, (nn)
  152. // LD dd, (nn)
  153. // LD IX, (nn)
  154. // LD IY, (nn)
  155. // LD (nn), HL
  156. // LD (nn), dd
  157. // LD (nn), IX
  158. // LD (nn), IY
  159. // LD SP, HL
  160. // LD SP, IX
  161. // LD SP, IY
  162. // PUSH qq
  163. // PUSH IX
  164. // PUSH IY
  165. // POP qq
  166. // POP IX
  167. // POP IY
  168. // EX DE, HL
  169. // EX AF, AF′
  170. /*
  171. EXX (0xD9):
  172. Exchange the 16-bit registers with their shadows
  173. (BC <=> BC', DE <=> DE', HL <=> HL').
  174. */
  175. static uint8_t z80_inst_exx(Z80 *z80, uint8_t opcode)
  176. {
  177. (void) opcode;
  178. uint16_t bc = get_pair(z80, REG_BC),
  179. de = get_pair(z80, REG_DE),
  180. hl = get_pair(z80, REG_HL);
  181. set_pair(z80, REG_BC, get_pair(z80, REG_BC_));
  182. set_pair(z80, REG_DE, get_pair(z80, REG_DE_));
  183. set_pair(z80, REG_HL, get_pair(z80, REG_HL_));
  184. set_pair(z80, REG_BC_, bc);
  185. set_pair(z80, REG_DE_, de);
  186. set_pair(z80, REG_HL_, hl);
  187. z80->regfile.pc++;
  188. return 4;
  189. }
  190. // EX (SP), HL
  191. // EX (SP), IX
  192. // EX (SP), IY
  193. // LDI
  194. // LDIR
  195. // LDD
  196. // LDDR
  197. // CPI
  198. // CPIR
  199. // CPD
  200. // CPDR
  201. // ADD A, r
  202. // ADD A, n
  203. // ADD A, (HL)
  204. // ADD A, (IX + d)
  205. // ADD A, (IY + d)
  206. // ADC A, s
  207. // SUB s
  208. // SBC A, s
  209. // AND s
  210. // OR s
  211. // XOR s
  212. // CP s
  213. /*
  214. INC r (0x04, 0x0C, 0x14, 0x1C, 0x24, 0x2C, 0x3C):
  215. Increment r (8-bit register).
  216. */
  217. static uint8_t z80_inst_inc_r(Z80 *z80, uint8_t opcode)
  218. {
  219. uint8_t *reg = extract_reg(z80, opcode);
  220. bool halfcarry = !!(((*reg & 0x0F) + 1) & 0x10);
  221. (*reg)++;
  222. update_flags(z80, 0, 0, *reg == 0x80, !!(*reg & 0x08), halfcarry,
  223. !!(*reg & 0x20), *reg == 0, !!(*reg & 0x80), 0xFE);
  224. z80->regfile.pc++;
  225. return 4;
  226. }
  227. // INC (HL)
  228. // INC (IX+d)
  229. // INC (IY+d)
  230. // DEC m
  231. // DAA
  232. // CPL
  233. // NEG
  234. // CCF
  235. // SCF
  236. /*
  237. NOP (0x00):
  238. No operation is performed.
  239. */
  240. static uint8_t z80_inst_nop(Z80 *z80, uint8_t opcode)
  241. {
  242. (void) opcode;
  243. z80->regfile.pc++;
  244. return 4;
  245. }
  246. /*
  247. HALT (0x76):
  248. Suspend CPU operation: execute NOPs until an interrupt or reset.
  249. */
  250. static uint8_t z80_inst_halt(Z80 *z80, uint8_t opcode)
  251. {
  252. (void) z80;
  253. (void) opcode;
  254. return 4;
  255. }
  256. /*
  257. DI (0xF3):
  258. Disable maskable interrupts by resetting both flip-flops.
  259. */
  260. static uint8_t z80_inst_di(Z80 *z80, uint8_t opcode)
  261. {
  262. (void) opcode;
  263. z80->regfile.iff1 = false;
  264. z80->regfile.iff2 = false;
  265. z80->regfile.pc++;
  266. return 4;
  267. }
  268. /*
  269. EI (0xFB):
  270. Enable maskable interrupts by setting both flip-flops.
  271. */
  272. static uint8_t z80_inst_ei(Z80 *z80, uint8_t opcode)
  273. {
  274. (void) opcode;
  275. z80->regfile.iff1 = true;
  276. z80->regfile.iff2 = true;
  277. z80->regfile.pc++;
  278. return 4;
  279. }
  280. /*
  281. IM (0xED46, 0xED4E, 0xED56, 0xED5E, 0xED66, 0xED6E, 0xED76, 0xED7E):
  282. Set the interrupt mode.
  283. */
  284. static uint8_t z80_inst_im(Z80 *z80, uint8_t opcode)
  285. {
  286. switch (opcode) {
  287. case 0x46:
  288. case 0x4E:
  289. case 0x66:
  290. case 0x6E:
  291. z80->regfile.im_a = false; // Interrupt mode 0
  292. z80->regfile.im_b = false;
  293. break;
  294. case 0x56:
  295. case 0x76:
  296. z80->regfile.im_a = true; // Interrupt mode 1
  297. z80->regfile.im_b = false;
  298. break;
  299. case 0x5E:
  300. case 0x7E:
  301. z80->regfile.im_a = true; // Interrupt mode 2
  302. z80->regfile.im_b = true;
  303. break;
  304. }
  305. z80->regfile.pc++;
  306. return 8;
  307. }
  308. // ADD HL, ss
  309. // ADC HL, ss
  310. // SBC HL, ss
  311. // ADD IX, pp
  312. // ADD IY, rr
  313. /*
  314. INC ss (0x03, 0x13, 0x23, 0x33):
  315. Increment ss (16-bit register).
  316. */
  317. static uint8_t z80_inst_inc_ss(Z80 *z80, uint8_t opcode)
  318. {
  319. uint8_t pair = extract_pair(opcode);
  320. set_pair(z80, pair, get_pair(z80, pair) + 1);
  321. z80->regfile.pc++;
  322. return 6;
  323. }
  324. // INC IX
  325. // INC IY
  326. // DEC ss
  327. // DEC IX
  328. // DEC IY
  329. // RLCA
  330. // RLA
  331. // RRCA
  332. // RRA
  333. // RLC r
  334. // RLC (HL)
  335. // RLC (IX+d)
  336. // RLC (IY+d)
  337. // RL m
  338. // RRC m
  339. // RR m
  340. // SLA m
  341. // SRA m
  342. // SRL m
  343. // RLD
  344. // RRD
  345. // BIT b, r
  346. // BIT b, (HL)
  347. // BIT b, (IX+d)
  348. // BIT b, (IY+d)
  349. // SET b, r
  350. // SET b, (HL)
  351. // SET b, (IX+d)
  352. // SET b, (IY+d)
  353. // RES b, m
  354. /*
  355. JP nn (0xC3):
  356. Jump to nn (16-bit immediate).
  357. */
  358. static uint8_t z80_inst_jp_nn(Z80 *z80, uint8_t opcode)
  359. {
  360. (void) opcode;
  361. z80->regfile.pc = mmu_read_double(z80->mmu, ++z80->regfile.pc);
  362. return 10;
  363. }
  364. // JP cc, nn
  365. // JR e
  366. // JR C, e
  367. // JR NC, e
  368. // JR Z, e
  369. // JR NZ, e
  370. // JP (HL)
  371. // JP (IX)
  372. // JP (IY)
  373. // DJNZ, e
  374. // CALL nn
  375. // CALL cc, nn
  376. // RET
  377. // RET cc
  378. // RETI
  379. // RETN
  380. // RST p
  381. // IN A, (n)
  382. // IN r (C)
  383. // INI
  384. // INIR
  385. // IND
  386. // INDR
  387. // OUT (n), A
  388. // OUT (C), r
  389. // OUTI
  390. // OTIR
  391. // OUTD
  392. // OTDR
  393. /*
  394. 0xED:
  395. Handle an extended instruction.
  396. */
  397. static uint8_t z80_prefix_extended(Z80 *z80, uint8_t opcode)
  398. {
  399. opcode = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  400. return (*instruction_table_extended[opcode])(z80, opcode);
  401. }
  402. /*
  403. 0xED:
  404. Handle a bit instruction.
  405. */
  406. static uint8_t z80_prefix_bits(Z80 *z80, uint8_t opcode)
  407. {
  408. opcode = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  409. return (*instruction_table_bits[opcode])(z80, opcode);
  410. }
  411. /*
  412. 0xDD, 0xFD:
  413. Handle an index instruction.
  414. */
  415. static uint8_t z80_prefix_index(Z80 *z80, uint8_t opcode)
  416. {
  417. opcode = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  418. return (*instruction_table_index[opcode])(z80, opcode);
  419. }
  420. /*
  421. 0xDDCB, 0xFDCB:
  422. Handle an index-bit instruction.
  423. */
  424. static uint8_t z80_prefix_index_bits(Z80 *z80, uint8_t opcode)
  425. {
  426. opcode = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  427. return (*instruction_table_index_bits[opcode])(z80, opcode);
  428. }
  429. #include "z80_tables.inc.c"