An emulator, assembler, and disassembler for the Sega Game Gear
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

z80.c 12 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. #include "z80.h"
  4. #include "disassembler.h"
  5. #include "logging.h"
  6. #define REG_AF 0
  7. #define REG_BC 1
  8. #define REG_DE 2
  9. #define REG_HL 3
  10. #define REG_SP 4
  11. #define REG_AF_ 5
  12. #define REG_BC_ 6
  13. #define REG_DE_ 7
  14. #define REG_HL_ 8
  15. #define FLAG_CARRY 0
  16. #define FLAG_SUBTRACT 1
  17. #define FLAG_PARITY 2
  18. #define FLAG_OVERFLOW 2
  19. #define FLAG_UNDOC_3 3
  20. #define FLAG_HALFCARRY 4
  21. #define FLAG_UNDOC_5 5
  22. #define FLAG_ZERO 6
  23. #define FLAG_SIGN 7
  24. #define BINARY_FMT "0b%u%u%u%u%u%u%u%u" // Used by z80_dump_registers()
  25. #define BINARY_VAL(data) \
  26. (data & (1 << 7) ? 1 : 0), \
  27. (data & (1 << 6) ? 1 : 0), \
  28. (data & (1 << 5) ? 1 : 0), \
  29. (data & (1 << 4) ? 1 : 0), \
  30. (data & (1 << 3) ? 1 : 0), \
  31. (data & (1 << 2) ? 1 : 0), \
  32. (data & (1 << 1) ? 1 : 0), \
  33. (data & (1 << 0) ? 1 : 0)
  34. /*
  35. Initialize a Z80 object.
  36. Register values are invalid until z80_power() is called. No other Z80
  37. functions should be called before it.
  38. */
  39. void z80_init(Z80 *z80, MMU *mmu, IO *io)
  40. {
  41. z80->mmu = mmu;
  42. z80->io = io;
  43. z80->except = true;
  44. z80->exc_code = Z80_EXC_NOT_POWERED;
  45. z80->exc_data = 0;
  46. }
  47. /*
  48. Power on the Z80, setting registers to their default values.
  49. This also clears the exception flag, which is necessary before the Z80 can
  50. begin emulation.
  51. */
  52. void z80_power(Z80 *z80)
  53. {
  54. Z80RegFile *regfile = &z80->regfile;
  55. regfile->a = regfile->f = 0xFF;
  56. regfile->b = regfile->c = 0xFF;
  57. regfile->d = regfile->e = 0xFF;
  58. regfile->h = regfile->l = 0xFF;
  59. regfile->a_ = regfile->f_ = 0xFF;
  60. regfile->b_ = regfile->c_ = 0xFF;
  61. regfile->d_ = regfile->e_ = 0xFF;
  62. regfile->h_ = regfile->l_ = 0xFF;
  63. regfile->ix = 0xFFFF;
  64. regfile->iy = 0xFFFF;
  65. regfile->sp = 0xFFF0;
  66. regfile->pc = 0x0000;
  67. regfile->i = 0xFF;
  68. regfile->r = 0xFF;
  69. regfile->im_a = regfile->im_b = 0;
  70. regfile->iff1 = regfile->iff2 = 0;
  71. z80->except = false;
  72. z80->pending_cycles = 0;
  73. z80->trace.fresh = true;
  74. z80->trace.last_addr = 0;
  75. z80->trace.counter = 0;
  76. }
  77. /*
  78. Get the value of a register pair.
  79. */
  80. static inline uint16_t get_pair(Z80 *z80, uint8_t pair)
  81. {
  82. switch (pair) {
  83. case REG_AF: return (z80->regfile.a << 8) + z80->regfile.f;
  84. case REG_BC: return (z80->regfile.b << 8) + z80->regfile.c;
  85. case REG_DE: return (z80->regfile.d << 8) + z80->regfile.e;
  86. case REG_HL: return (z80->regfile.h << 8) + z80->regfile.l;
  87. case REG_AF_: return (z80->regfile.a_ << 8) + z80->regfile.f_;
  88. case REG_BC_: return (z80->regfile.b_ << 8) + z80->regfile.c_;
  89. case REG_DE_: return (z80->regfile.d_ << 8) + z80->regfile.e_;
  90. case REG_HL_: return (z80->regfile.h_ << 8) + z80->regfile.l_;
  91. case REG_SP: return z80->regfile.sp;
  92. }
  93. FATAL("invalid call: get_pair(z80, %u)", pair)
  94. }
  95. /*
  96. Set the value of a register pair.
  97. */
  98. static inline void set_pair(Z80 *z80, uint8_t pair, uint16_t value)
  99. {
  100. switch (pair) {
  101. case REG_AF: z80->regfile.a = value >> 8; z80->regfile.f = value; break;
  102. case REG_BC: z80->regfile.b = value >> 8; z80->regfile.c = value; break;
  103. case REG_DE: z80->regfile.d = value >> 8; z80->regfile.e = value; break;
  104. case REG_HL: z80->regfile.h = value >> 8; z80->regfile.l = value; break;
  105. case REG_AF_: z80->regfile.a_ = value >> 8; z80->regfile.f_ = value; break;
  106. case REG_BC_: z80->regfile.b_ = value >> 8; z80->regfile.c_ = value; break;
  107. case REG_DE_: z80->regfile.d_ = value >> 8; z80->regfile.e_ = value; break;
  108. case REG_HL_: z80->regfile.h_ = value >> 8; z80->regfile.l_ = value; break;
  109. case REG_SP: z80->regfile.sp = value; break;
  110. default:
  111. FATAL("invalid call: set_pair(z80, %u, 0x%04X)", pair, value)
  112. }
  113. }
  114. /*
  115. Return whether a particular flag is set in the F register.
  116. */
  117. static inline bool get_flag(const Z80 *z80, uint8_t flag)
  118. {
  119. return z80->regfile.f & (1 << flag);
  120. }
  121. /*
  122. Return whether a particular flag is set in the F' register.
  123. */
  124. static inline bool get_shadow_flag(const Z80 *z80, uint8_t flag)
  125. {
  126. return z80->regfile.f_ & (1 << flag);
  127. }
  128. /*
  129. Update the F register flags according to the set bits in the mask.
  130. */
  131. static inline void update_flags(Z80 *z80, bool c, bool n, bool pv, bool f3,
  132. bool h, bool f5, bool z, bool s, uint8_t mask)
  133. {
  134. z80->regfile.f = (~mask & z80->regfile.f) | (mask & (
  135. c << FLAG_CARRY |
  136. n << FLAG_SUBTRACT |
  137. pv << FLAG_PARITY |
  138. f3 << FLAG_UNDOC_3 |
  139. h << FLAG_HALFCARRY |
  140. f5 << FLAG_UNDOC_5 |
  141. z << FLAG_ZERO |
  142. s << FLAG_SIGN));
  143. }
  144. /*
  145. Push a two-byte value onto the stack.
  146. */
  147. static inline void stack_push(Z80 *z80, uint16_t value)
  148. {
  149. z80->regfile.sp -= 2;
  150. mmu_write_double(z80->mmu, z80->regfile.sp, value);
  151. }
  152. /*
  153. Pop a two-byte value from the stack.
  154. */
  155. static inline uint16_t stack_pop(Z80 *z80)
  156. {
  157. uint16_t value = mmu_read_double(z80->mmu, z80->regfile.sp);
  158. z80->regfile.sp += 2;
  159. return value;
  160. }
  161. /*
  162. Check for errors after an I/O operation.
  163. */
  164. static void handle_io_errors(Z80 *z80)
  165. {
  166. if (z80->io->except) {
  167. z80->except = true;
  168. z80->exc_code = Z80_EXC_IO_ERROR;
  169. z80->exc_data = z80->io->exc_port;
  170. }
  171. }
  172. /*
  173. Read and return a byte from the given port, and check for errors.
  174. */
  175. static uint8_t read_port(Z80 *z80, uint8_t port)
  176. {
  177. uint8_t value = io_port_read(z80->io, port);
  178. handle_io_errors(z80);
  179. return value;
  180. }
  181. /*
  182. Write a byte to the given port, and check for errors.
  183. */
  184. static void write_port(Z80 *z80, uint8_t port, uint8_t value)
  185. {
  186. io_port_write(z80->io, port, value);
  187. handle_io_errors(z80);
  188. }
  189. /*
  190. Extract an 8-bit register from the given opcode and return a pointer to it.
  191. */
  192. static inline uint8_t* extract_reg(Z80 *z80, uint8_t opcode)
  193. {
  194. switch (opcode & 0x38) {
  195. case 0x00: return &z80->regfile.b;
  196. case 0x08: return &z80->regfile.c;
  197. case 0x10: return &z80->regfile.d;
  198. case 0x18: return &z80->regfile.e;
  199. case 0x20: return &z80->regfile.h;
  200. case 0x28: return &z80->regfile.l;
  201. case 0x38: return &z80->regfile.a;
  202. }
  203. FATAL("invalid call: extract_reg(z80, 0x%02X)", opcode)
  204. }
  205. /*
  206. Extract a register pair from the given opcode and return its identifer.
  207. */
  208. static inline uint8_t extract_pair(uint8_t opcode)
  209. {
  210. switch (opcode & 0x30) {
  211. case 0x00: return REG_BC;
  212. case 0x10: return REG_DE;
  213. case 0x20: return REG_HL;
  214. case 0x30: return REG_SP;
  215. }
  216. FATAL("invalid call: extract_pair(0x%02X)", opcode)
  217. }
  218. /*
  219. Extract a condition from the given opcode.
  220. */
  221. static inline bool extract_cond(const Z80 *z80, uint8_t opcode)
  222. {
  223. switch (opcode & 0x38) {
  224. case 0x00: return !get_flag(z80, FLAG_ZERO);
  225. case 0x08: return get_flag(z80, FLAG_ZERO);
  226. case 0x10: return !get_flag(z80, FLAG_CARRY);
  227. case 0x18: return get_flag(z80, FLAG_CARRY);
  228. case 0x20: return !get_flag(z80, FLAG_PARITY);
  229. case 0x28: return get_flag(z80, FLAG_PARITY);
  230. case 0x30: return !get_flag(z80, FLAG_SIGN);
  231. case 0x38: return get_flag(z80, FLAG_SIGN);
  232. }
  233. FATAL("invalid call: extract_cond(z80, 0x%02X)", opcode)
  234. }
  235. /*
  236. Extract the current index register.
  237. */
  238. static inline uint16_t* extract_index(Z80 *z80)
  239. {
  240. uint8_t prefix = mmu_read_byte(z80->mmu, z80->regfile.pc - 1);
  241. if (prefix != 0xDD && prefix != 0xFD)
  242. FATAL("invalid call: extract_index(z80, 0x%02X)", prefix)
  243. return prefix == 0xDD ? &z80->regfile.ix : &z80->regfile.iy;
  244. }
  245. /*
  246. Return the CPU's current interrupt mode.
  247. */
  248. static inline uint8_t get_interrupt_mode(const Z80 *z80)
  249. {
  250. if (!z80->regfile.im_a)
  251. return 0;
  252. if (!z80->regfile.im_b)
  253. return 1;
  254. return 2;
  255. }
  256. /*
  257. Increment the refresh counter register, R.
  258. */
  259. static inline void increment_refresh_counter(Z80 *z80)
  260. {
  261. z80->regfile.r = (z80->regfile.r & 0x80) | ((z80->regfile.r + 1) & 0x7F);
  262. }
  263. #include "z80_ops.inc.c"
  264. /*
  265. @TRACE_LEVEL
  266. Trace the instruction about to be executed by the CPU.
  267. */
  268. static inline void trace_instruction(Z80 *z80)
  269. {
  270. if (z80->regfile.pc == z80->trace.last_addr && !z80->trace.fresh) {
  271. z80->trace.counter++;
  272. if (!(z80->trace.counter % (1 << 14)))
  273. TRACE_NOEOL("repeat last: %llu times\r", z80->trace.counter);
  274. return;
  275. }
  276. if (z80->trace.fresh) {
  277. TRACE("PC ADDR P1 P2 OP A1 A2 INSTR\tARGS")
  278. TRACE("------- -------------- -----\t----")
  279. z80->trace.fresh = false;
  280. }
  281. z80->trace.last_addr = z80->regfile.pc;
  282. z80->trace.counter = 0;
  283. uint32_t quad = mmu_read_quad(z80->mmu, z80->regfile.pc);
  284. uint8_t bytes[4] = {quad, quad >> 8, quad >> 16, quad >> 24};
  285. DisasInstr *instr = disassemble_instruction(bytes);
  286. TRACE("0x%04X: %-14s %s", z80->regfile.pc, instr->bytestr, instr->line)
  287. disas_instr_free(instr);
  288. }
  289. /*
  290. Emulate the given number of cycles of the Z80, or until an exception.
  291. The return value indicates whether the exception flag is set. If it is,
  292. then emulation must be stopped because further calls to z80_do_cycles()
  293. will have no effect. The exception flag can be reset with z80_power().
  294. */
  295. bool z80_do_cycles(Z80 *z80, double cycles)
  296. {
  297. cycles += z80->pending_cycles;
  298. while (cycles > 0 && !z80->except) {
  299. uint8_t opcode = mmu_read_byte(z80->mmu, z80->regfile.pc);
  300. increment_refresh_counter(z80);
  301. if (TRACE_LEVEL)
  302. trace_instruction(z80);
  303. cycles -= (*instruction_table[opcode])(z80, opcode);
  304. }
  305. z80->pending_cycles = cycles;
  306. return z80->except;
  307. }
  308. /*
  309. @DEBUG_LEVEL
  310. Print out all register values to stdout.
  311. */
  312. void z80_dump_registers(const Z80 *z80)
  313. {
  314. const Z80RegFile *rf = &z80->regfile;
  315. DEBUG("Dumping Z80 register values:")
  316. DEBUG("- AF: 0x%02X%02X (%03d, %03d)", rf->a, rf->f, rf->a, rf->f)
  317. DEBUG("- BC: 0x%02X%02X (%03d, %03d)", rf->b, rf->c, rf->b, rf->c)
  318. DEBUG("- DE: 0x%02X%02X (%03d, %03d)", rf->d, rf->e, rf->d, rf->e)
  319. DEBUG("- HL: 0x%02X%02X (%03d, %03d)", rf->h, rf->l, rf->h, rf->l)
  320. DEBUG("- AF': 0x%02X%02X (%03d, %03d)", rf->a_, rf->f_, rf->a_, rf->f_)
  321. DEBUG("- BC': 0x%02X%02X (%03d, %03d)", rf->b_, rf->c_, rf->b_, rf->c_)
  322. DEBUG("- DE': 0x%02X%02X (%03d, %03d)", rf->d_, rf->e_, rf->d_, rf->e_)
  323. DEBUG("- HL': 0x%02X%02X (%03d, %03d)", rf->h_, rf->l_, rf->h_, rf->l_)
  324. DEBUG("- IX: 0x%04X (%05d)", rf->ix, rf->ix)
  325. DEBUG("- IY: 0x%04X (%05d)", rf->iy, rf->iy)
  326. DEBUG("- SP: 0x%04X (%05d)", rf->sp, rf->sp)
  327. DEBUG("- PC: 0x%04X (%05d)", rf->pc, rf->pc)
  328. DEBUG("- I: 0x%2X (%03d)", rf->i, rf->i)
  329. DEBUG("- R: 0x%2X (%03d)", rf->r, rf->r)
  330. DEBUG("- F: "BINARY_FMT" (C: %u, N: %u, P/V: %u, H: %u, Z: %u, S: %u)",
  331. BINARY_VAL(rf->f),
  332. get_flag(z80, FLAG_CARRY),
  333. get_flag(z80, FLAG_SUBTRACT),
  334. get_flag(z80, FLAG_PARITY),
  335. get_flag(z80, FLAG_HALFCARRY),
  336. get_flag(z80, FLAG_ZERO),
  337. get_flag(z80, FLAG_SIGN))
  338. DEBUG("- F': "BINARY_FMT" (C: %u, N: %u, P/V: %u, H: %u, Z: %u, S: %u)",
  339. BINARY_VAL(rf->f_),
  340. get_shadow_flag(z80, FLAG_CARRY),
  341. get_shadow_flag(z80, FLAG_SUBTRACT),
  342. get_shadow_flag(z80, FLAG_PARITY),
  343. get_shadow_flag(z80, FLAG_HALFCARRY),
  344. get_shadow_flag(z80, FLAG_ZERO),
  345. get_shadow_flag(z80, FLAG_SIGN))
  346. DEBUG("- IM: 0b%u%u (mode: %u)", rf->im_a, rf->im_b,
  347. get_interrupt_mode(z80))
  348. DEBUG("- IFF: 1: %u, 2: %u", rf->iff1, rf->iff2)
  349. }