An emulator, assembler, and disassembler for the Sega Game Gear
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

277 linhas
9.9 KiB

  1. /* Copyright (C) 2014-2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. Released under the terms of the MIT License. See LICENSE for details. */
  3. #include <stdarg.h>
  4. #include <stdlib.h>
  5. #include "instructions.h"
  6. #include "inst_args.h"
  7. #include "../logging.h"
  8. /*
  9. TEMP SYNTAX NOTES:
  10. - http://clrhome.org/table/
  11. - http://www.z80.info/z80undoc.htm
  12. - http://www.z80.info/z80code.txt
  13. - http://www.z80.info/z80href.txt
  14. inst := mnemonic [arg[, arg[, arg]]]
  15. mnemonic := [a-z0-9]{2-4}
  16. arg := register | immediate | indirect | indexed | label | condition
  17. register := A | B | C | D | E | AF | BC | DE | HL | H | L | F | I | IX |
  18. IY | PC | R | SP | AF' | IXH | IXL | IYH | IYL
  19. immediate := 16-bit integer
  20. indirect := \( (register | immediate) \)
  21. indexed := \( (IX | IY) + immediate \)
  22. label := string
  23. condition := NZ | N | NC | C | PO | PE | P | M
  24. */
  25. /* Helper macros for get_inst_parser() */
  26. #define JOIN_(a, b, c, d) ((uint32_t) ((a << 24) + (b << 16) + (c << 8) + d))
  27. #define DISPATCH_(s, z) ( \
  28. (z) == 2 ? JOIN_(s[0], s[1], 0x00, 0x00) : \
  29. (z) == 3 ? JOIN_(s[0], s[1], s[2], 0x00) : \
  30. JOIN_(s[0], s[1], s[2], s[3])) \
  31. #define MAKE_CMP_(s) DISPATCH_(s, sizeof(s) / sizeof(char) - 1)
  32. #define HANDLE(m) if (key == MAKE_CMP_(#m)) return parse_inst_##m;
  33. /* Internal helper macros for instruction parsers */
  34. #define INST_ALLOC_(len) \
  35. *length = len; \
  36. if (!(*bytes = malloc(sizeof(uint8_t) * (len)))) \
  37. OUT_OF_MEMORY()
  38. #define INST_SET_(b, val) ((*bytes)[b] = val)
  39. #define INST_SET1_(b1) INST_SET_(0, b1)
  40. #define INST_SET2_(b1, b2) INST_SET1_(b1), INST_SET_(1, b2)
  41. #define INST_SET3_(b1, b2, b3) INST_SET2_(b1, b2), INST_SET_(2, b3)
  42. #define INST_SET4_(b1, b2, b3, b4) INST_SET3_(b1, b2, b3), INST_SET_(3, b4)
  43. #define INST_DISPATCH_(a, b, c, d, target, ...) target
  44. #define INST_FILL_BYTES_(len, ...) \
  45. ((len > 4) ? fill_bytes_variadic(*bytes, len, __VA_ARGS__) : \
  46. INST_DISPATCH_(__VA_ARGS__, INST_SET4_, INST_SET3_, INST_SET2_, \
  47. INST_SET1_, __VA_ARGS__)(__VA_ARGS__));
  48. #define INST_PREFIX_(reg) \
  49. (((reg) == REG_IX || (reg) == REG_IXH || (reg) == REG_IXL) ? 0xDD : 0xFD)
  50. /* Helper macros for instruction parsers */
  51. #define INST_FUNC(mnemonic) \
  52. static ASMErrorDesc parse_inst_##mnemonic( \
  53. uint8_t **bytes, size_t *length, char **symbol, const char *arg, size_t size)
  54. #define INST_ERROR(desc) return ED_PS_##desc;
  55. #define INST_TAKES_NO_ARGS \
  56. if (arg) \
  57. INST_ERROR(TOO_MANY_ARGS) \
  58. (void) size;
  59. #define INST_TAKES_ARGS(lo, hi) \
  60. if (!arg) \
  61. INST_ERROR(TOO_FEW_ARGS) \
  62. ASMInstArg args[3]; \
  63. size_t nargs; \
  64. ASMErrorDesc err = parse_args(args, &nargs, arg, size); \
  65. if (err) \
  66. return err; \
  67. if (nargs < lo) \
  68. INST_ERROR(TOO_FEW_ARGS) \
  69. if (nargs > hi) \
  70. INST_ERROR(TOO_MANY_ARGS)
  71. #define INST_ARG(n) (args[n])
  72. #define INST_TYPE(n) INST_ARG(n).type
  73. #define INST_REG(n) INST_ARG(n).data.reg
  74. #define INST_IMM(n) INST_ARG(n).data.imm
  75. #define INST_INDIRECT(n) INST_ARG(n).data.indirect
  76. #define INST_INDEX(n) INST_ARG(n).data.index
  77. #define INST_LABEL(n) INST_ARG(n).data.label
  78. #define INST_COND(n) INST_ARG(n).data.cond
  79. #define INST_REG_PREFIX(n) INST_PREFIX_(INST_REG(n))
  80. #define INST_INDEX_PREFIX(n) INST_PREFIX_(INST_INDEX(n).reg)
  81. #define INST_IND_PREFIX(n) INST_PREFIX_(INST_INDIRECT(n).addr.reg)
  82. #define INST_RETURN(len, ...) { \
  83. (void) symbol; \
  84. INST_ALLOC_(len) \
  85. INST_FILL_BYTES_(len, __VA_ARGS__) \
  86. return ED_NONE; \
  87. }
  88. #define INST_RETURN_WITH_SYMBOL(len, label, ...) { \
  89. *symbol = strdup(label); \
  90. if (!(*symbol)) \
  91. OUT_OF_MEMORY() \
  92. INST_ALLOC_(len) \
  93. INST_FILL_BYTES_(len - 2, __VA_ARGS__) \
  94. return ED_NONE; \
  95. }
  96. /*
  97. Fill an instruction's byte array with the given data.
  98. This internal function is only called for instructions longer than four
  99. bytes (of which there is only one: the fake emulator debugging/testing
  100. opcode with mnemonic "emu"), so it does not get used in normal situations.
  101. Return the value of the last byte inserted, for compatibility with the
  102. INST_SETn_ family of macros.
  103. */
  104. static uint8_t fill_bytes_variadic(uint8_t *bytes, size_t len, ...)
  105. {
  106. va_list vargs;
  107. va_start(vargs, len);
  108. for (size_t i = 0; i < len; i++)
  109. bytes[i] = va_arg(vargs, unsigned);
  110. va_end(vargs);
  111. return bytes[len - 1];
  112. }
  113. /*
  114. Parse a single instruction argument into an ASMInstArg object.
  115. Return ED_NONE (0) on success or an error code on failure.
  116. */
  117. static ASMErrorDesc parse_arg(
  118. ASMInstArg *arg, const char *str, size_t size, char **symbol)
  119. {
  120. // TODO
  121. DEBUG("parse_arg(): -->%.*s<--", (int) size, str)
  122. return ED_PS_ARG_SYNTAX;
  123. }
  124. /*
  125. Parse an argument string int ASMInstArg objects.
  126. Return ED_NONE (0) on success or an error code on failure.
  127. */
  128. static ASMErrorDesc parse_args(
  129. ASMInstArg args[3], size_t *nargs, const char *str, size_t size)
  130. {
  131. ASMErrorDesc err;
  132. static char *symbol = NULL;
  133. size_t start = 0, i = 0;
  134. while (i < size) {
  135. char c = str[i];
  136. if (c == ',') {
  137. if ((err = parse_arg(&args[*nargs], str + start, i - start, &symbol)))
  138. return err;
  139. (*nargs)++;
  140. i++;
  141. if (i < size && str[i] == ' ')
  142. i++;
  143. start = i;
  144. if (*nargs >= 3 && i < size)
  145. return ED_PS_TOO_MANY_ARGS;
  146. } else {
  147. if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
  148. c == ' ' || c == '+' || c == '-' || c == '(' || c == ')' ||
  149. c == '_' || c == '.')
  150. i++;
  151. else
  152. return ED_PS_ARG_SYNTAX;
  153. }
  154. }
  155. if (i > start) {
  156. if ((err = parse_arg(&args[*nargs], str + start, i - start, &symbol)))
  157. return err;
  158. }
  159. return ED_NONE;
  160. }
  161. /* Instruction parser functions */
  162. INST_FUNC(nop)
  163. {
  164. INST_TAKES_NO_ARGS
  165. INST_RETURN(1, 0x00)
  166. }
  167. INST_FUNC(inc)
  168. {
  169. INST_TAKES_ARGS(1, 1)
  170. switch (INST_TYPE(0)) {
  171. case AT_REGISTER:
  172. switch (INST_REG(0)) {
  173. case REG_A: INST_RETURN(1, 0x3C)
  174. case REG_B: INST_RETURN(1, 0x04)
  175. case REG_C: INST_RETURN(1, 0x0C)
  176. case REG_D: INST_RETURN(1, 0x14)
  177. case REG_E: INST_RETURN(1, 0x1C)
  178. case REG_H: INST_RETURN(1, 0x24)
  179. case REG_L: INST_RETURN(1, 0x2C)
  180. case REG_BC: INST_RETURN(1, 0x03)
  181. case REG_DE: INST_RETURN(1, 0x13)
  182. case REG_HL: INST_RETURN(1, 0x23)
  183. case REG_SP: INST_RETURN(1, 0x33)
  184. case REG_IX: INST_RETURN(2, 0xDD, 0x23)
  185. case REG_IY: INST_RETURN(2, 0xFD, 0x23)
  186. case REG_IXH: INST_RETURN(2, 0xDD, 0x2C)
  187. case REG_IXL: INST_RETURN(2, 0xFD, 0x2C)
  188. case REG_IYH: INST_RETURN(2, 0xDD, 0x2C)
  189. case REG_IYL: INST_RETURN(2, 0xFD, 0x2C)
  190. default: INST_ERROR(ARG0_BAD_REG)
  191. }
  192. case AT_INDIRECT:
  193. if (INST_INDIRECT(0).type != AT_REGISTER)
  194. INST_ERROR(ARG0_TYPE)
  195. if (INST_INDIRECT(0).addr.reg != REG_HL)
  196. INST_ERROR(ARG0_BAD_REG)
  197. INST_RETURN(2, 0x34)
  198. case AT_INDEXED:
  199. INST_RETURN(3, INST_INDEX_PREFIX(0), 0x34, INST_INDEX(0).offset)
  200. default:
  201. INST_ERROR(ARG0_TYPE)
  202. }
  203. }
  204. /*
  205. INST_FUNC(add)
  206. {
  207. DEBUG("dispatched to -> ADD")
  208. return ED_PS_TOO_FEW_ARGS;
  209. }
  210. INST_FUNC(adc)
  211. {
  212. DEBUG("dispatched to -> ADC")
  213. return ED_PS_TOO_FEW_ARGS;
  214. }
  215. */
  216. /*
  217. Return the relevant ASMInstParser function for a given mnemonic.
  218. NULL is returned if the mnemonic is not known.
  219. */
  220. ASMInstParser get_inst_parser(char mstr[MAX_MNEMONIC_SIZE])
  221. {
  222. // Exploit the fact that we can store the entire mnemonic string as a
  223. // single 32-bit value to do fast lookups:
  224. uint32_t key = (mstr[0] << 24) + (mstr[1] << 16) + (mstr[2] << 8) + mstr[3];
  225. DEBUG("get_inst_parser(): -->%.*s<-- 0x%08X", MAX_MNEMONIC_SIZE, mstr, key)
  226. HANDLE(nop)
  227. HANDLE(inc)
  228. // HANDLE(add)
  229. // HANDLE(adc)
  230. return NULL;
  231. }