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.

пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 9 година
пре 9 година
пре 9 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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 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, get_pair(z80, REG_HL));
  63. z80->regfile.pc++;
  64. return 7;
  65. }
  66. /*
  67. LD r, (IXY+d)
  68. */
  69. // static uint8_t z80_inst_ld_r_ixy(Z80 *z80, uint8_t opcode)
  70. /*
  71. LD (HL), r (0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x77):
  72. Load r (8-bit register) into the memory pointed to by HL.
  73. */
  74. static uint8_t z80_inst_ld_hl_r(Z80 *z80, uint8_t opcode)
  75. {
  76. uint8_t *reg = extract_reg(z80, opcode << 3);
  77. uint16_t addr = get_pair(z80, REG_HL);
  78. mmu_write_byte(z80->mmu, addr, *reg);
  79. z80->regfile.pc++;
  80. return 7;
  81. }
  82. /*
  83. LD (IXY+d), r
  84. */
  85. // static uint8_t z80_inst_ld_ixy_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 (IXY+d), n
  101. */
  102. // static uint8_t z80_inst_ld_ixy_n(Z80 *z80, uint8_t opcode)
  103. /*
  104. LD A, (BC/DE)
  105. */
  106. // static uint8_t z80_inst_ld_a_bcde(Z80 *z80, uint8_t opcode)
  107. /*
  108. LD A, (nn) (0x3A):
  109. Load memory at address nn into A.
  110. */
  111. static uint8_t z80_inst_ld_a_nn(Z80 *z80, uint8_t opcode)
  112. {
  113. (void) opcode;
  114. uint16_t addr = mmu_read_double(z80->mmu, ++z80->regfile.pc);
  115. z80->regfile.a = mmu_read_byte(z80->mmu, addr);
  116. z80->regfile.pc += 2;
  117. return 13;
  118. }
  119. /*
  120. LD (BC/DE), A (0x02, 0x12):
  121. Load A into the memory address pointed to by BC or DE.
  122. */
  123. static uint8_t z80_inst_ld_bcde_a(Z80 *z80, uint8_t opcode)
  124. {
  125. uint16_t addr = get_pair(z80, extract_pair(opcode));
  126. mmu_write_byte(z80->mmu, addr, z80->regfile.a);
  127. z80->regfile.pc++;
  128. return 7;
  129. }
  130. /*
  131. LD (nn), A (0x32):
  132. Load A into memory address nn.
  133. */
  134. static uint8_t z80_inst_ld_nn_a(Z80 *z80, uint8_t opcode)
  135. {
  136. (void) opcode;
  137. uint16_t addr = mmu_read_double(z80->mmu, ++z80->regfile.pc);
  138. mmu_write_byte(z80->mmu, addr, z80->regfile.a);
  139. z80->regfile.pc += 2;
  140. return 13;
  141. }
  142. /*
  143. LD A, I
  144. */
  145. // static uint8_t z80_inst_ld_a_i(Z80 *z80, uint8_t opcode)
  146. /*
  147. LD A, R
  148. */
  149. // static uint8_t z80_inst_ld_a_r(Z80 *z80, uint8_t opcode)
  150. /*
  151. LD I,A
  152. */
  153. // static uint8_t z80_inst_ld_i_a(Z80 *z80, uint8_t opcode)
  154. /*
  155. LD R, A
  156. */
  157. // static uint8_t z80_inst_ld_r_a(Z80 *z80, uint8_t opcode)
  158. /*
  159. LD dd, nn (0x01, 0x11, 0x21, 0x31):
  160. Load nn (16-bit immediate) into dd (16-bit register).
  161. */
  162. static uint8_t z80_inst_ld_dd_nn(Z80 *z80, uint8_t opcode)
  163. {
  164. uint8_t pair = extract_pair(opcode);
  165. set_pair(z80, pair, mmu_read_double(z80->mmu, ++z80->regfile.pc));
  166. z80->regfile.pc += 2;
  167. return 10;
  168. }
  169. // LD IXY, nn
  170. // LD HL, (nn)
  171. // LD dd, (nn)
  172. // LD IXY, (nn)
  173. /*
  174. LD (nn), HL: (0x22):
  175. Load HL into memory address nn.
  176. */
  177. static uint8_t z80_inst_ld_nn_hl(Z80 *z80, uint8_t opcode)
  178. {
  179. (void) opcode;
  180. uint16_t addr = mmu_read_double(z80->mmu, ++z80->regfile.pc);
  181. mmu_write_double(z80->mmu, addr, get_pair(z80, REG_HL));
  182. z80->regfile.pc += 2;
  183. return 16;
  184. }
  185. // LD (nn), dd
  186. // LD (nn), IXY
  187. // LD SP, HL
  188. // LD SP, IXY
  189. /*
  190. PUSH qq (0xC5, 0xD5, 0xE5, 0xF5):
  191. Push qq onto the stack, and decrement SP by two.
  192. */
  193. static uint8_t z80_inst_push_qq(Z80 *z80, uint8_t opcode)
  194. {
  195. uint8_t pair = extract_pair(opcode);
  196. stack_push(z80, get_pair(z80, pair));
  197. z80->regfile.pc++;
  198. return 11;
  199. }
  200. /*
  201. PUSH IXY (0xDDE5, 0xFDE5):
  202. Push IX or IY onto the stack, and decrement SP by two.
  203. */
  204. static uint8_t z80_inst_push_ixy(Z80 *z80, uint8_t opcode)
  205. {
  206. (void) opcode;
  207. stack_push(z80, *extract_index(z80));
  208. z80->regfile.pc++;
  209. return 15;
  210. }
  211. /*
  212. POP qq (0xC1, 0xD1, 0xE1, 0xF1):
  213. Pop qq from the stack, and increment SP by two.
  214. */
  215. static uint8_t z80_inst_pop_qq(Z80 *z80, uint8_t opcode)
  216. {
  217. uint8_t pair = extract_pair(opcode);
  218. set_pair(z80, pair, stack_pop(z80));
  219. z80->regfile.pc++;
  220. return 10;
  221. }
  222. /*
  223. POP IXY (0xDDE1, 0xFDE1):
  224. Pop IX or IY from the stack, and increment SP by two.
  225. */
  226. static uint8_t z80_inst_pop_ixy(Z80 *z80, uint8_t opcode)
  227. {
  228. (void) opcode;
  229. *extract_index(z80) = stack_pop(z80);
  230. z80->regfile.pc++;
  231. return 14;
  232. }
  233. /*
  234. EX DE, HL (0xEB):
  235. Exchange DE with HL.
  236. */
  237. static uint8_t z80_inst_ex_de_hl(Z80 *z80, uint8_t opcode)
  238. {
  239. (void) opcode;
  240. uint16_t de = get_pair(z80, REG_DE);
  241. set_pair(z80, REG_DE, get_pair(z80, REG_HL));
  242. set_pair(z80, REG_HL, de);
  243. z80->regfile.pc++;
  244. return 4;
  245. }
  246. // EX AF, AF′
  247. /*
  248. EXX (0xD9):
  249. Exchange the 16-bit registers with their shadows
  250. (BC <=> BC', DE <=> DE', HL <=> HL').
  251. */
  252. static uint8_t z80_inst_exx(Z80 *z80, uint8_t opcode)
  253. {
  254. (void) opcode;
  255. uint16_t bc = get_pair(z80, REG_BC),
  256. de = get_pair(z80, REG_DE),
  257. hl = get_pair(z80, REG_HL);
  258. set_pair(z80, REG_BC, get_pair(z80, REG_BC_));
  259. set_pair(z80, REG_DE, get_pair(z80, REG_DE_));
  260. set_pair(z80, REG_HL, get_pair(z80, REG_HL_));
  261. set_pair(z80, REG_BC_, bc);
  262. set_pair(z80, REG_DE_, de);
  263. set_pair(z80, REG_HL_, hl);
  264. z80->regfile.pc++;
  265. return 4;
  266. }
  267. // EX (SP), HL
  268. // EX (SP), IXY
  269. /*
  270. LDI (0xEDA0):
  271. LD (DE), (HL); INC HL; INC DE; DEC BC;
  272. */
  273. static uint8_t z80_inst_ldi(Z80 *z80, uint8_t opcode)
  274. {
  275. (void) opcode;
  276. uint16_t hl = get_pair(z80, REG_HL),
  277. de = get_pair(z80, REG_DE),
  278. bc = get_pair(z80, REG_BC);
  279. uint16_t value = mmu_read_double(z80->mmu, hl);
  280. mmu_write_double(z80->mmu, de, value);
  281. set_pair(z80, REG_HL, ++hl);
  282. set_pair(z80, REG_DE, ++de);
  283. set_pair(z80, REG_BC, --bc);
  284. update_flags(z80, 0, 0, bc == 0, 0, 0, 0, 0, 0, 0x16);
  285. z80->regfile.pc++;
  286. return 16;
  287. }
  288. /*
  289. LDIR (0xEDB0):
  290. LDI; JR PV, -2
  291. */
  292. static uint8_t z80_inst_ldir(Z80 *z80, uint8_t opcode)
  293. {
  294. z80_inst_ldi(z80, opcode);
  295. if (get_pair(z80, REG_BC) == 0)
  296. return 16;
  297. z80->regfile.pc -= 2;
  298. return 21;
  299. }
  300. /*
  301. LDD (0xEDA8):
  302. LD (DE), (HL); DEC HL; DEC DE; DEC BC;
  303. */
  304. static uint8_t z80_inst_ldd(Z80 *z80, uint8_t opcode)
  305. {
  306. (void) opcode;
  307. uint16_t hl = get_pair(z80, REG_HL),
  308. de = get_pair(z80, REG_DE),
  309. bc = get_pair(z80, REG_BC);
  310. uint16_t value = mmu_read_double(z80->mmu, hl);
  311. mmu_write_double(z80->mmu, de, value);
  312. set_pair(z80, REG_HL, --hl);
  313. set_pair(z80, REG_DE, --de);
  314. set_pair(z80, REG_BC, --bc);
  315. update_flags(z80, 0, 0, bc == 0, 0, 0, 0, 0, 0, 0x16);
  316. z80->regfile.pc++;
  317. return 16;
  318. }
  319. /*
  320. LDDR (0xEDB8):
  321. LDD; JR PV, -2
  322. */
  323. static uint8_t z80_inst_lddr(Z80 *z80, uint8_t opcode)
  324. {
  325. z80_inst_ldd(z80, opcode);
  326. if (get_pair(z80, REG_BC) == 0)
  327. return 16;
  328. z80->regfile.pc -= 2;
  329. return 21;
  330. }
  331. // CPI
  332. // CPIR
  333. // CPD
  334. // CPDR
  335. // ADD A, r
  336. // ADD A, n
  337. // ADD A, (HL)
  338. // ADD A, (IXY+d)
  339. // ADC A, s
  340. // SUB s
  341. /*
  342. SUB n (0xD6):
  343. Subtract n (8-bit immediate) from A.
  344. */
  345. static uint8_t z80_inst_sub_n(Z80 *z80, uint8_t opcode)
  346. {
  347. (void) opcode;
  348. uint8_t imm = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  349. uint8_t orig = z80->regfile.a;
  350. uint8_t a = z80->regfile.a -= imm;
  351. bool c = (orig - imm) != a;
  352. bool v = (orig - imm) != ((int8_t) a);
  353. bool h = !!(((orig & 0x0F) - (imm & 0x0F)) & 0x10);
  354. update_flags(z80, c, 1, v, !!(a & 0x08), h, !!(a & 0x20), a == 0,
  355. !!(a & 0x80), 0xFF);
  356. z80->regfile.pc++;
  357. return 7;
  358. }
  359. // SBC A, s
  360. // AND s
  361. // OR s
  362. /*
  363. OR r (0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB7):
  364. Bitwise OR A with r (8-bit register).
  365. */
  366. static uint8_t z80_inst_or_r(Z80 *z80, uint8_t opcode)
  367. {
  368. uint8_t *reg = extract_reg(z80, opcode << 3);
  369. uint8_t a = (z80->regfile.a |= *reg);
  370. bool parity = !(__builtin_popcount(a) % 2);
  371. update_flags(z80, 0, 0, parity, !!(a & 0x08), 0, !!(a & 0x20), a == 0,
  372. !!(a & 0x80), 0xFF);
  373. z80->regfile.pc++;
  374. return 4;
  375. }
  376. /*
  377. OR n (0xF6):
  378. Bitwise OR A with n (8-bit immediate).
  379. */
  380. static uint8_t z80_inst_or_n(Z80 *z80, uint8_t opcode)
  381. {
  382. (void) opcode;
  383. uint8_t imm = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  384. uint8_t a = (z80->regfile.a |= imm);
  385. bool parity = !(__builtin_popcount(a) % 2);
  386. update_flags(z80, 0, 0, parity, !!(a & 0x08), 0, !!(a & 0x20), a == 0,
  387. !!(a & 0x80), 0xFF);
  388. z80->regfile.pc++;
  389. return 7;
  390. }
  391. // XOR s
  392. /*
  393. XOR r (0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAF):
  394. Bitwise XOR A with r (8-bit register).
  395. */
  396. static uint8_t z80_inst_xor_r(Z80 *z80, uint8_t opcode)
  397. {
  398. uint8_t *reg = extract_reg(z80, opcode << 3);
  399. uint8_t a = (z80->regfile.a ^= *reg);
  400. bool parity = !(__builtin_popcount(a) % 2);
  401. update_flags(z80, 0, 0, parity, !!(a & 0x08), 0, !!(a & 0x20), a == 0,
  402. !!(a & 0x80), 0xFF);
  403. z80->regfile.pc++;
  404. return 4;
  405. }
  406. // CP s
  407. /*
  408. CP r (0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBF):
  409. Set flags as if r (8-bit register) had been subtracted from A.
  410. */
  411. static uint8_t z80_inst_cp_r(Z80 *z80, uint8_t opcode)
  412. {
  413. uint8_t *reg = extract_reg(z80, opcode << 3);
  414. uint8_t d = z80->regfile.a - *reg;
  415. bool c = (z80->regfile.a - *reg) != d;
  416. bool v = (z80->regfile.a - *reg) != ((int8_t) d);
  417. bool h = !!(((z80->regfile.a & 0x0F) - (*reg & 0x0F)) & 0x10);
  418. update_flags(z80, c, 1, v, !!(*reg & 0x08), h, !!(*reg & 0x20), d == 0,
  419. !!(d & 0x80), 0xFF);
  420. z80->regfile.pc++;
  421. return 4;
  422. }
  423. /*
  424. CP n (0xFE):
  425. Set flags as if n (8-bit immediate) had been subtracted from A.
  426. */
  427. static uint8_t z80_inst_cp_n(Z80 *z80, uint8_t opcode)
  428. {
  429. (void) opcode;
  430. uint8_t n = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  431. uint8_t d = z80->regfile.a - n;
  432. bool c = (z80->regfile.a - n) != d;
  433. bool v = (z80->regfile.a - n) != ((int8_t) d);
  434. bool h = !!(((z80->regfile.a & 0x0F) - (n & 0x0F)) & 0x10);
  435. update_flags(z80, c, 1, v, !!(n & 0x08), h, !!(n & 0x20), d == 0,
  436. !!(d & 0x80), 0xFF);
  437. z80->regfile.pc++;
  438. return 7;
  439. }
  440. /*
  441. INC r (0x04, 0x0C, 0x14, 0x1C, 0x24, 0x2C, 0x3C):
  442. Increment r (8-bit register).
  443. */
  444. static uint8_t z80_inst_inc_r(Z80 *z80, uint8_t opcode)
  445. {
  446. uint8_t *reg = extract_reg(z80, opcode);
  447. bool halfcarry = !!(((*reg & 0x0F) + 1) & 0x10);
  448. (*reg)++;
  449. update_flags(z80, 0, 0, *reg == 0x80, !!(*reg & 0x08), halfcarry,
  450. !!(*reg & 0x20), *reg == 0, !!(*reg & 0x80), 0xFE);
  451. z80->regfile.pc++;
  452. return 4;
  453. }
  454. // INC (HL)
  455. // INC (IXY+d)
  456. /*
  457. DEC r (0x05, 0x0D, 0x15, 0x1D, 0x25, 0x2D, 0x3D):
  458. Decrement r (8-bit register).
  459. */
  460. static uint8_t z80_inst_dec_r(Z80 *z80, uint8_t opcode)
  461. {
  462. uint8_t *reg = extract_reg(z80, opcode);
  463. bool halfcarry = !!(((*reg & 0x0F) - 1) & 0x10);
  464. (*reg)--;
  465. update_flags(z80, 0, 1, *reg == 0x7F, !!(*reg & 0x08), halfcarry,
  466. !!(*reg & 0x20), *reg == 0, !!(*reg & 0x80), 0xFE);
  467. z80->regfile.pc++;
  468. return 4;
  469. }
  470. // DEC (HL)
  471. // DEC (IXY+d)
  472. // DAA
  473. // CPL
  474. // NEG
  475. // CCF
  476. // SCF
  477. /*
  478. NOP (0x00):
  479. No operation is performed.
  480. */
  481. static uint8_t z80_inst_nop(Z80 *z80, uint8_t opcode)
  482. {
  483. (void) opcode;
  484. z80->regfile.pc++;
  485. return 4;
  486. }
  487. /*
  488. HALT (0x76):
  489. Suspend CPU operation: execute NOPs until an interrupt or reset.
  490. */
  491. static uint8_t z80_inst_halt(Z80 *z80, uint8_t opcode)
  492. {
  493. (void) z80;
  494. (void) opcode;
  495. return 4;
  496. }
  497. /*
  498. DI (0xF3):
  499. Disable maskable interrupts by resetting both flip-flops.
  500. */
  501. static uint8_t z80_inst_di(Z80 *z80, uint8_t opcode)
  502. {
  503. (void) opcode;
  504. z80->regfile.iff1 = false;
  505. z80->regfile.iff2 = false;
  506. z80->regfile.pc++;
  507. return 4;
  508. }
  509. /*
  510. EI (0xFB):
  511. Enable maskable interrupts by setting both flip-flops.
  512. */
  513. static uint8_t z80_inst_ei(Z80 *z80, uint8_t opcode)
  514. {
  515. (void) opcode;
  516. z80->regfile.iff1 = true;
  517. z80->regfile.iff2 = true;
  518. z80->regfile.pc++;
  519. return 4;
  520. }
  521. /*
  522. IM (0xED46, 0xED4E, 0xED56, 0xED5E, 0xED66, 0xED6E, 0xED76, 0xED7E):
  523. Set the interrupt mode.
  524. */
  525. static uint8_t z80_inst_im(Z80 *z80, uint8_t opcode)
  526. {
  527. switch (opcode) {
  528. case 0x46:
  529. case 0x4E:
  530. case 0x66:
  531. case 0x6E:
  532. z80->regfile.im_a = false; // Interrupt mode 0
  533. z80->regfile.im_b = false;
  534. break;
  535. case 0x56:
  536. case 0x76:
  537. z80->regfile.im_a = true; // Interrupt mode 1
  538. z80->regfile.im_b = false;
  539. break;
  540. case 0x5E:
  541. case 0x7E:
  542. z80->regfile.im_a = true; // Interrupt mode 2
  543. z80->regfile.im_b = true;
  544. break;
  545. }
  546. z80->regfile.pc++;
  547. return 8;
  548. }
  549. /*
  550. ADD HL, ss (0x09, 0x19, 0x29, 0x39):
  551. Add ss to HL.
  552. */
  553. static uint8_t z80_inst_add_hl_ss(Z80 *z80, uint8_t opcode)
  554. {
  555. uint8_t pair = extract_pair(opcode);
  556. uint16_t lh = get_pair(z80, REG_HL), rh = get_pair(z80, pair);
  557. uint16_t value = lh + rh;
  558. set_pair(z80, REG_HL, value);
  559. bool h = !!(((lh & 0x0FFF) + (rh & 0x0FFF)) & 0x1000);
  560. update_flags(z80, (lh + rh) != value, 0, 0, !!(value & 0x0800), h,
  561. !!(value & 0x2000), 0, 0, 0x3B);
  562. z80->regfile.pc++;
  563. return 11;
  564. }
  565. // ADC HL, ss
  566. /*
  567. SBC HL, ss (0xED42, 0xED52, 0xED62, 0xED72):
  568. Subtract ss with carry from HL.
  569. */
  570. static uint8_t z80_inst_sbc_hl_ss(Z80 *z80, uint8_t opcode)
  571. {
  572. uint8_t pair = extract_pair(opcode);
  573. uint16_t minuend = get_pair(z80, REG_HL);
  574. uint16_t subtrahend = get_pair(z80, pair) + get_flag(z80, FLAG_CARRY);
  575. uint16_t value = minuend - subtrahend;
  576. set_pair(z80, REG_HL, value);
  577. bool c = (minuend - subtrahend) != value;
  578. bool ov = (minuend - subtrahend) != ((int16_t) value); // TODO: verify these
  579. bool hc = !!(((minuend & 0x0FFF) - (subtrahend & 0x0FFF)) & 0x1000);
  580. update_flags(z80, c, 1, ov, !!(value & 0x0800), hc,
  581. !!(value & 0x2000), value == 0, !!(value & 0x8000), 0xFF);
  582. z80->regfile.pc++;
  583. return 15;
  584. }
  585. // ADD IXY, pp
  586. /*
  587. INC ss (0x03, 0x13, 0x23, 0x33):
  588. Increment ss (16-bit register).
  589. */
  590. static uint8_t z80_inst_inc_ss(Z80 *z80, uint8_t opcode)
  591. {
  592. uint8_t pair = extract_pair(opcode);
  593. set_pair(z80, pair, get_pair(z80, pair) + 1);
  594. z80->regfile.pc++;
  595. return 6;
  596. }
  597. // INC IXY
  598. /*
  599. DEC ss (0x0B, 0x1B, 0x2B, 0x3B):
  600. Decrement ss (16-bit register).
  601. */
  602. static uint8_t z80_inst_dec_ss(Z80 *z80, uint8_t opcode)
  603. {
  604. uint8_t pair = extract_pair(opcode);
  605. set_pair(z80, pair, get_pair(z80, pair) - 1);
  606. z80->regfile.pc++;
  607. return 6;
  608. }
  609. // DEC IXY
  610. // RLCA
  611. // RLA
  612. // RRCA
  613. // RRA
  614. // RLC r
  615. // RLC (HL)
  616. // RLC (IXY+d)
  617. // RL m
  618. // RRC m
  619. // RR m
  620. // SLA m
  621. // SRA m
  622. // SRL m
  623. // RLD
  624. // RRD
  625. // BIT b, r
  626. // BIT b, (HL)
  627. // BIT b, (IXY+d)
  628. // SET b, r
  629. // SET b, (HL)
  630. // SET b, (IXY+d)
  631. // RES b, m
  632. /*
  633. JP nn (0xC3):
  634. Jump to nn (16-bit immediate).
  635. */
  636. static uint8_t z80_inst_jp_nn(Z80 *z80, uint8_t opcode)
  637. {
  638. (void) opcode;
  639. z80->regfile.pc = mmu_read_double(z80->mmu, ++z80->regfile.pc);
  640. return 10;
  641. }
  642. /*
  643. JP cc, nn (0xC2, 0xCA, 0xD2, 0xDA, 0xE2, 0xEA, 0xF2, 0xFA):
  644. Jump to nn (16-bit immediate) if cc (condition) is true.
  645. */
  646. static uint8_t z80_inst_jp_cc_nn(Z80 *z80, uint8_t opcode)
  647. {
  648. if (extract_cond(z80, opcode))
  649. z80->regfile.pc = mmu_read_double(z80->mmu, ++z80->regfile.pc);
  650. else
  651. z80->regfile.pc += 3;
  652. return 10;
  653. }
  654. /*
  655. JR e (0x18):
  656. Relative jump e (signed 8-bit immediate) bytes.
  657. */
  658. static uint8_t z80_inst_jr_e(Z80 *z80, uint8_t opcode)
  659. {
  660. (void) opcode;
  661. int8_t jump = mmu_read_byte(z80->mmu, z80->regfile.pc + 1);
  662. z80->regfile.pc += jump + 2;
  663. return 12;
  664. }
  665. /*
  666. JR cc, e (0x20, 0x28, 0x30, 0x38):
  667. Relative jump e (signed 8-bit immediate) bytes if cc (condition) is true.
  668. */
  669. static uint8_t z80_inst_jr_cc_e(Z80 *z80, uint8_t opcode)
  670. {
  671. if (extract_cond(z80, opcode - 0x20)) {
  672. int8_t jump = mmu_read_byte(z80->mmu, z80->regfile.pc + 1);
  673. z80->regfile.pc += jump + 2;
  674. return 12;
  675. } else {
  676. z80->regfile.pc += 2;
  677. return 7;
  678. }
  679. }
  680. // JP (HL)
  681. // JP (IXY)
  682. /*
  683. DJNZ, e (0x10):
  684. Decrement b and relative jump e (signed 8-bit immediate) if non-zero.
  685. */
  686. static uint8_t z80_inst_djnz_e(Z80 *z80, uint8_t opcode)
  687. {
  688. (void) opcode;
  689. z80->regfile.b--;
  690. if (z80->regfile.b != 0) {
  691. int8_t jump = mmu_read_byte(z80->mmu, z80->regfile.pc + 1);
  692. z80->regfile.pc += jump + 2;
  693. return 13;
  694. } else {
  695. z80->regfile.pc += 2;
  696. return 8;
  697. }
  698. }
  699. /*
  700. CALL nn (0xCD):
  701. Push PC+3 onto the stack and jump to nn (16-bit immediate).
  702. */
  703. static uint8_t z80_inst_call_nn(Z80 *z80, uint8_t opcode)
  704. {
  705. (void) opcode;
  706. stack_push(z80, z80->regfile.pc + 3);
  707. z80->regfile.pc = mmu_read_double(z80->mmu, ++z80->regfile.pc);
  708. return 17;
  709. }
  710. /*
  711. CALL cc, nn (0xC4, 0xCC, 0xD4, 0xDC, 0xE4, 0xEC, 0xF4, 0xFC):
  712. Push PC+3 onto the stack and jump to nn (16-bit immediate) if cc is true.
  713. */
  714. static uint8_t z80_inst_call_cc_nn(Z80 *z80, uint8_t opcode)
  715. {
  716. if (extract_cond(z80, opcode)) {
  717. stack_push(z80, z80->regfile.pc + 3);
  718. z80->regfile.pc = mmu_read_double(z80->mmu, ++z80->regfile.pc);
  719. return 17;
  720. } else {
  721. z80->regfile.pc += 3;
  722. return 10;
  723. }
  724. }
  725. /*
  726. RET (0xC9):
  727. Pop PC from the stack.
  728. */
  729. static uint8_t z80_inst_ret(Z80 *z80, uint8_t opcode)
  730. {
  731. (void) opcode;
  732. z80->regfile.pc = stack_pop(z80);
  733. return 10;
  734. }
  735. /*
  736. RET cc (0xC0, 0xC8, 0xD0, 0xD8, 0xE0, 0xE8, 0xF0, 0xF8):
  737. Pop PC from the stack if cc is true.
  738. */
  739. static uint8_t z80_inst_ret_cc(Z80 *z80, uint8_t opcode)
  740. {
  741. if (extract_cond(z80, opcode)) {
  742. z80->regfile.pc = stack_pop(z80);
  743. return 11;
  744. } else {
  745. z80->regfile.pc++;
  746. return 5;
  747. }
  748. }
  749. // RETI
  750. /*
  751. RETN (0xED45, 0xED55, 0xED5D, 0xED65, 0xED6D, 0xED75, 0xED7D):
  752. Pop PC from the stack, and copy to IFF2 to IFF1.
  753. */
  754. static uint8_t z80_inst_retn(Z80 *z80, uint8_t opcode)
  755. {
  756. (void) opcode;
  757. z80->regfile.pc = stack_pop(z80);
  758. z80->regfile.iff1 = z80->regfile.iff2;
  759. return 14;
  760. }
  761. /*
  762. RST p (0xC7, 0xCF, 0xD7, 0xDF, 0xE7, 0xEF, 0xF7, 0xFF):
  763. Push PC+1 onto the stack and jump to p (opcode & 0x38).
  764. */
  765. static uint8_t z80_inst_rst_p(Z80 *z80, uint8_t opcode)
  766. {
  767. stack_push(z80, z80->regfile.pc + 1);
  768. z80->regfile.pc = opcode & 0x38;
  769. return 11;
  770. }
  771. /*
  772. IN A, (n) (0xDB):
  773. Read a byte from port n into a.
  774. */
  775. static uint8_t z80_inst_in_a_n(Z80 *z80, uint8_t opcode)
  776. {
  777. (void) opcode;
  778. uint8_t port = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  779. z80->regfile.a = read_port(z80, port);
  780. z80->regfile.pc++;
  781. return 11;
  782. }
  783. /*
  784. IN r, (C) (0xED40, 0xED48, 0xED50, 0xED58, 0xED60, 0xED68, 0xED70, 0xED78):
  785. Read a byte from port C into r, or affect flags only if 0xED70.
  786. */
  787. static uint8_t z80_inst_in_r_c(Z80 *z80, uint8_t opcode)
  788. {
  789. uint8_t data = read_port(z80, z80->regfile.c);
  790. bool parity = !(__builtin_popcount(data) % 2);
  791. if (opcode != 0x70)
  792. *extract_reg(z80, opcode) = data;
  793. update_flags(z80, 0, 0, parity, !!(data & 0x08), 0, !!(data & 0x20),
  794. data == 0, !!(data & 0x80), 0xFE);
  795. z80->regfile.pc++;
  796. return 12;
  797. }
  798. /*
  799. INI (0xEDA2):
  800. IN (HL), (C); INC HL; DEC B
  801. */
  802. static uint8_t z80_inst_ini(Z80 *z80, uint8_t opcode)
  803. {
  804. (void) opcode;
  805. uint8_t data = read_port(z80, z80->regfile.c), *b = &z80->regfile.b;
  806. uint16_t hl = get_pair(z80, REG_HL);
  807. bool h = !!(((*b & 0x0F) - 1) & 0x10);
  808. mmu_write_byte(z80->mmu, hl, data);
  809. set_pair(z80, REG_HL, hl + 1);
  810. (*b)--;
  811. update_flags(z80, 0, 1, *b == 0x7F, !!(*b & 0x08), h, !!(*b & 0x20),
  812. *b == 0, !!(*b & 0x80), 0xFE);
  813. z80->regfile.pc++;
  814. return 16;
  815. }
  816. /*
  817. INIR (0xEDB2):
  818. INI; JR NZ, -2
  819. */
  820. static uint8_t z80_inst_inir(Z80 *z80, uint8_t opcode)
  821. {
  822. z80_inst_ini(z80, opcode);
  823. if (z80->regfile.b == 0)
  824. return 16;
  825. z80->regfile.pc -= 2;
  826. return 21;
  827. }
  828. /*
  829. IND (0xEDAA):
  830. IN (HL), (C); DEC HL; DEC B
  831. */
  832. static uint8_t z80_inst_ind(Z80 *z80, uint8_t opcode)
  833. {
  834. (void) opcode;
  835. uint8_t data = read_port(z80, z80->regfile.c), *b = &z80->regfile.b;
  836. uint16_t hl = get_pair(z80, REG_HL);
  837. bool h = !!(((*b & 0x0F) - 1) & 0x10);
  838. mmu_write_byte(z80->mmu, hl, data);
  839. set_pair(z80, REG_HL, hl - 1);
  840. (*b)--;
  841. update_flags(z80, 0, 1, *b == 0x7F, !!(*b & 0x08), h, !!(*b & 0x20),
  842. *b == 0, !!(*b & 0x80), 0xFE);
  843. z80->regfile.pc++;
  844. return 16;
  845. }
  846. /*
  847. INDR (0xEDBA):
  848. IND; JR NZ, -2
  849. */
  850. static uint8_t z80_inst_indr(Z80 *z80, uint8_t opcode)
  851. {
  852. z80_inst_ind(z80, opcode);
  853. if (z80->regfile.b == 0)
  854. return 16;
  855. z80->regfile.pc -= 2;
  856. return 21;
  857. }
  858. /*
  859. OUT (n), A (0xD3):
  860. Write a byte from a into port n.
  861. */
  862. static uint8_t z80_inst_out_n_a(Z80 *z80, uint8_t opcode)
  863. {
  864. (void) opcode;
  865. uint8_t port = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  866. write_port(z80, port, z80->regfile.a);
  867. z80->regfile.pc++;
  868. return 11;
  869. }
  870. /*
  871. OUT (C), r (0xED41, 0xED49, 0xED51, 0xED59, 0xED61, 0xED69, 0xED71,
  872. 0xED79):
  873. Write a byte from r (8-bit reg, or 0 if 0xED71) into port C.
  874. */
  875. static uint8_t z80_inst_out_c_r(Z80 *z80, uint8_t opcode)
  876. {
  877. uint8_t value = opcode != 0x71 ? *extract_reg(z80, opcode) : 0;
  878. write_port(z80, z80->regfile.c, value);
  879. z80->regfile.pc++;
  880. return 12;
  881. }
  882. /*
  883. OUTI (0xEDA3):
  884. OUT (C), (HL); INC HL; DEC B
  885. */
  886. static uint8_t z80_inst_outi(Z80 *z80, uint8_t opcode)
  887. {
  888. (void) opcode;
  889. uint16_t hl = get_pair(z80, REG_HL);
  890. uint8_t *b = &z80->regfile.b;
  891. bool h = !!(((*b & 0x0F) - 1) & 0x10);
  892. write_port(z80, z80->regfile.c, mmu_read_byte(z80->mmu, hl));
  893. set_pair(z80, REG_HL, hl + 1);
  894. (*b)--;
  895. update_flags(z80, 0, 1, *b == 0x7F, !!(*b & 0x08), h, !!(*b & 0x20),
  896. *b == 0, !!(*b & 0x80), 0xFE);
  897. z80->regfile.pc++;
  898. return 16;
  899. }
  900. /*
  901. OTIR (0xEDB3):
  902. OUTI; JR NZ, -2
  903. */
  904. static uint8_t z80_inst_otir(Z80 *z80, uint8_t opcode)
  905. {
  906. z80_inst_outi(z80, opcode);
  907. if (z80->regfile.b == 0)
  908. return 16;
  909. z80->regfile.pc -= 2;
  910. return 21;
  911. }
  912. /*
  913. OUTD (0xEDAB):
  914. OUT (C), (HL); DEC HL; DEC B
  915. */
  916. static uint8_t z80_inst_outd(Z80 *z80, uint8_t opcode)
  917. {
  918. (void) opcode;
  919. uint16_t hl = get_pair(z80, REG_HL);
  920. uint8_t *b = &z80->regfile.b;
  921. bool h = !!(((*b & 0x0F) - 1) & 0x10);
  922. write_port(z80, z80->regfile.c, mmu_read_byte(z80->mmu, hl));
  923. set_pair(z80, REG_HL, hl - 1);
  924. (*b)--;
  925. update_flags(z80, 0, 1, *b == 0x7F, !!(*b & 0x08), h, !!(*b & 0x20),
  926. *b == 0, !!(*b & 0x80), 0xFE);
  927. z80->regfile.pc++;
  928. return 16;
  929. }
  930. /*
  931. OTDR (0xEDBB):
  932. OUTD; JR NZ, -2
  933. */
  934. static uint8_t z80_inst_otdr(Z80 *z80, uint8_t opcode)
  935. {
  936. z80_inst_outd(z80, opcode);
  937. if (z80->regfile.b == 0)
  938. return 16;
  939. z80->regfile.pc -= 2;
  940. return 21;
  941. }
  942. /*
  943. NOP2:
  944. No operation is performed twice; i.e., 2 NOPs-worth of cycles are spent.
  945. Used for unimplemented extended and index instructions.
  946. */
  947. static uint8_t z80_inst_nop2(Z80 *z80, uint8_t opcode)
  948. {
  949. (void) opcode;
  950. z80->regfile.pc++;
  951. return 8;
  952. }
  953. /*
  954. 0xED:
  955. Handle an extended instruction.
  956. */
  957. static uint8_t z80_prefix_extended(Z80 *z80, uint8_t opcode)
  958. {
  959. opcode = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  960. return (*instruction_table_extended[opcode])(z80, opcode);
  961. }
  962. /*
  963. 0xED:
  964. Handle a bit instruction.
  965. */
  966. static uint8_t z80_prefix_bits(Z80 *z80, uint8_t opcode)
  967. {
  968. opcode = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  969. return (*instruction_table_bits[opcode])(z80, opcode);
  970. }
  971. /*
  972. 0xDD, 0xFD:
  973. Handle an index instruction.
  974. */
  975. static uint8_t z80_prefix_index(Z80 *z80, uint8_t opcode)
  976. {
  977. opcode = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  978. return (*instruction_table_index[opcode])(z80, opcode);
  979. }
  980. /*
  981. 0xDDCB, 0xFDCB:
  982. Handle an index-bit instruction.
  983. */
  984. static uint8_t z80_prefix_index_bits(Z80 *z80, uint8_t opcode)
  985. {
  986. opcode = mmu_read_byte(z80->mmu, ++z80->regfile.pc);
  987. return (*instruction_table_index_bits[opcode])(z80, opcode);
  988. }
  989. #include "z80_tables.inc.c"