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.

z80.c 10 KiB

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