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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. Extract an 8-bit register from the given opcode and return a pointer to it.
  145. */
  146. static inline uint8_t* extract_reg(Z80 *z80, uint8_t opcode)
  147. {
  148. switch (opcode & 0x38) {
  149. case 0x00: return &z80->regfile.b;
  150. case 0x08: return &z80->regfile.c;
  151. case 0x10: return &z80->regfile.d;
  152. case 0x18: return &z80->regfile.e;
  153. case 0x20: return &z80->regfile.h;
  154. case 0x28: return &z80->regfile.l;
  155. case 0x38: return &z80->regfile.a;
  156. }
  157. FATAL("invalid call: extract_reg(z80, 0x%02X)", opcode)
  158. }
  159. /*
  160. Extract a register pair from the given opcode and return its identifer.
  161. */
  162. static inline uint8_t extract_pair(uint8_t opcode)
  163. {
  164. switch (opcode & 0x30) {
  165. case 0x00: return REG_BC;
  166. case 0x10: return REG_DE;
  167. case 0x20: return REG_HL;
  168. case 0x30: return REG_SP;
  169. }
  170. FATAL("invalid call: extract_pair(0x%02X)", opcode)
  171. }
  172. /*
  173. Return the CPU's current interrupt mode.
  174. */
  175. static inline uint8_t get_interrupt_mode(const Z80 *z80)
  176. {
  177. if (!z80->regfile.im_a)
  178. return 0;
  179. if (!z80->regfile.im_b)
  180. return 1;
  181. return 2;
  182. }
  183. /*
  184. Increment the refresh counter register, R.
  185. */
  186. static inline void increment_refresh_counter(Z80 *z80)
  187. {
  188. z80->regfile.r = (z80->regfile.r & 0x80) | ((z80->regfile.r + 1) & 0x7F);
  189. }
  190. #include "z80_ops.inc.c"
  191. /*
  192. @TRACE_LEVEL
  193. Trace the instruction about to be executed by the CPU.
  194. */
  195. static inline void trace_instruction(Z80 *z80)
  196. {
  197. if (z80->regfile.pc == z80->trace.last_addr && !z80->trace.fresh) {
  198. z80->trace.counter++;
  199. if (!(z80->trace.counter % (1 << 14)))
  200. TRACE_NOEOL("repeat last: %llu times\r", z80->trace.counter);
  201. return;
  202. }
  203. if (z80->trace.fresh) {
  204. TRACE("PC ADDR RAW INSTR\tARGS")
  205. TRACE("------- --- -----\t----")
  206. z80->trace.fresh = false;
  207. }
  208. z80->trace.last_addr = z80->regfile.pc;
  209. z80->trace.counter = 0;
  210. uint32_t quad = mmu_read_quad(z80->mmu, z80->regfile.pc);
  211. uint8_t bytes[4] = {quad, quad >> 8, quad >> 16, quad >> 24};
  212. DisasInstr *instr = disassemble_instruction(bytes);
  213. TRACE("0x%04X: %11s %s", z80->regfile.pc, instr->bytestr, instr->line)
  214. disas_instr_free(instr);
  215. }
  216. /*
  217. Emulate the given number of cycles of the Z80, or until an exception.
  218. The return value indicates whether the exception flag is set. If it is,
  219. then emulation must be stopped because further calls to z80_do_cycles()
  220. will have no effect. The exception flag can be reset with z80_power().
  221. */
  222. bool z80_do_cycles(Z80 *z80, double cycles)
  223. {
  224. cycles -= z80->pending_cycles;
  225. while (cycles > 0 && !z80->except) {
  226. uint8_t opcode = mmu_read_byte(z80->mmu, z80->regfile.pc);
  227. increment_refresh_counter(z80);
  228. if (TRACE_LEVEL)
  229. trace_instruction(z80);
  230. cycles -= (*instruction_lookup_table[opcode])(z80, opcode);
  231. }
  232. z80->pending_cycles = -cycles;
  233. return z80->except;
  234. }
  235. /*
  236. @DEBUG_LEVEL
  237. Print out all register values to stdout.
  238. */
  239. void z80_dump_registers(const Z80 *z80)
  240. {
  241. const Z80RegFile *rf = &z80->regfile;
  242. DEBUG("Dumping Z80 register values:")
  243. DEBUG("- AF: 0x%02X%02X (%03d, %03d)", rf->a, rf->f, rf->a, rf->f)
  244. DEBUG("- BC: 0x%02X%02X (%03d, %03d)", rf->b, rf->c, rf->b, rf->c)
  245. DEBUG("- DE: 0x%02X%02X (%03d, %03d)", rf->d, rf->e, rf->d, rf->e)
  246. DEBUG("- HL: 0x%02X%02X (%03d, %03d)", rf->h, rf->l, rf->h, rf->l)
  247. DEBUG("- AF': 0x%02X%02X (%03d, %03d)", rf->a_, rf->f_, rf->a_, rf->f_)
  248. DEBUG("- BC': 0x%02X%02X (%03d, %03d)", rf->b_, rf->c_, rf->b_, rf->c_)
  249. DEBUG("- DE': 0x%02X%02X (%03d, %03d)", rf->d_, rf->e_, rf->d_, rf->e_)
  250. DEBUG("- HL': 0x%02X%02X (%03d, %03d)", rf->h_, rf->l_, rf->h_, rf->l_)
  251. DEBUG("- IX: 0x%04X (%05d)", rf->ix, rf->ix)
  252. DEBUG("- IY: 0x%04X (%05d)", rf->iy, rf->iy)
  253. DEBUG("- SP: 0x%04X (%05d)", rf->sp, rf->sp)
  254. DEBUG("- PC: 0x%04X (%05d)", rf->pc, rf->pc)
  255. DEBUG("- I: 0x%2X (%03d)", rf->i, rf->i)
  256. DEBUG("- R: 0x%2X (%03d)", rf->r, rf->r)
  257. DEBUG("- F: "BINARY_FMT" (C: %u, N: %u, P/V: %u, H: %u, Z: %u, S: %u)",
  258. BINARY_VAL(rf->f),
  259. get_flag(z80, FLAG_CARRY),
  260. get_flag(z80, FLAG_SUBTRACT),
  261. get_flag(z80, FLAG_PARITY),
  262. get_flag(z80, FLAG_HALFCARRY),
  263. get_flag(z80, FLAG_ZERO),
  264. get_flag(z80, FLAG_SIGN))
  265. DEBUG("- F': "BINARY_FMT" (C: %u, N: %u, P/V: %u, H: %u, Z: %u, S: %u)",
  266. BINARY_VAL(rf->f_),
  267. get_shadow_flag(z80, FLAG_CARRY),
  268. get_shadow_flag(z80, FLAG_SUBTRACT),
  269. get_shadow_flag(z80, FLAG_PARITY),
  270. get_shadow_flag(z80, FLAG_HALFCARRY),
  271. get_shadow_flag(z80, FLAG_ZERO),
  272. get_shadow_flag(z80, FLAG_SIGN))
  273. DEBUG("- IM: 0b%u%u (mode: %u)", rf->im_a, rf->im_b,
  274. get_interrupt_mode(z80))
  275. DEBUG("- IFF: 1: %u, 2: %u", rf->iff1, rf->iff2)
  276. }