An emulator, assembler, and disassembler for the Sega Game Gear
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 

272 řádky
9.7 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_REG_PREFIX(n) INST_PREFIX_(INST_ARG(n).data.reg)
  73. #define INST_INDEX_PREFIX(n) INST_PREFIX_(INST_ARG(n).data.index.reg)
  74. #define INST_IND_PREFIX(n) INST_PREFIX_(INST_ARG(n).data.indirect.addr.reg)
  75. #define INST_RETURN(len, ...) { \
  76. (void) symbol; \
  77. INST_ALLOC_(len) \
  78. INST_FILL_BYTES_(len, __VA_ARGS__) \
  79. return ED_NONE; \
  80. }
  81. #define INST_RETURN_WITH_SYMBOL(len, label, ...) { \
  82. *symbol = strdup(label); \
  83. if (!(*symbol)) \
  84. OUT_OF_MEMORY() \
  85. INST_ALLOC_(len) \
  86. INST_FILL_BYTES_(len - 2, __VA_ARGS__) \
  87. return ED_NONE; \
  88. }
  89. /*
  90. Fill an instruction's byte array with the given data.
  91. This internal function is only called for instructions longer than four
  92. bytes (of which there is only one: the fake emulator debugging/testing
  93. opcode with mnemonic "emu"), so it does not get used in normal situations.
  94. Return the value of the last byte inserted, for compatibility with the
  95. INST_SETn_ family of macros.
  96. */
  97. static uint8_t fill_bytes_variadic(uint8_t *bytes, size_t len, ...)
  98. {
  99. va_list vargs;
  100. va_start(vargs, len);
  101. for (size_t i = 0; i < len; i++)
  102. bytes[i] = va_arg(vargs, unsigned);
  103. va_end(vargs);
  104. return bytes[len - 1];
  105. }
  106. /*
  107. Parse a single instruction argument into an ASMInstArg object.
  108. Return ED_NONE (0) on success or an error code on failure.
  109. */
  110. static ASMErrorDesc parse_arg(
  111. ASMInstArg *arg, const char *str, size_t size, char **symbol)
  112. {
  113. // TODO
  114. DEBUG("parse_arg(): -->%.*s<--", (int) size, str)
  115. return ED_PS_ARG_SYNTAX;
  116. }
  117. /*
  118. Parse an argument string int ASMInstArg objects.
  119. Return ED_NONE (0) on success or an error code on failure.
  120. */
  121. static ASMErrorDesc parse_args(
  122. ASMInstArg args[3], size_t *nargs, const char *str, size_t size)
  123. {
  124. ASMErrorDesc err;
  125. static char *symbol = NULL;
  126. size_t start = 0, i = 0;
  127. while (i < size) {
  128. char c = str[i];
  129. if (c == ',') {
  130. if ((err = parse_arg(&args[*nargs], str + start, i - start, &symbol)))
  131. return err;
  132. (*nargs)++;
  133. i++;
  134. if (i < size && str[i] == ' ')
  135. i++;
  136. start = i;
  137. if (*nargs >= 3 && i < size)
  138. return ED_PS_TOO_MANY_ARGS;
  139. } else {
  140. if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
  141. c == ' ' || c == '+' || c == '-' || c == '(' || c == ')' ||
  142. c == '_' || c == '.')
  143. i++;
  144. else
  145. return ED_PS_ARG_SYNTAX;
  146. }
  147. }
  148. if (i > start) {
  149. if ((err = parse_arg(&args[*nargs], str + start, i - start, &symbol)))
  150. return err;
  151. }
  152. return ED_NONE;
  153. }
  154. /* Instruction parser functions */
  155. INST_FUNC(nop)
  156. {
  157. INST_TAKES_NO_ARGS
  158. INST_RETURN(1, 0x00)
  159. }
  160. INST_FUNC(inc)
  161. {
  162. INST_TAKES_ARGS(1, 1)
  163. switch (INST_ARG(0).type) {
  164. case AT_REGISTER:
  165. switch (INST_ARG(0).data.reg) {
  166. case REG_A: INST_RETURN(1, 0x3C)
  167. case REG_B: INST_RETURN(1, 0x04)
  168. case REG_C: INST_RETURN(1, 0x0C)
  169. case REG_D: INST_RETURN(1, 0x14)
  170. case REG_E: INST_RETURN(1, 0x1C)
  171. case REG_H: INST_RETURN(1, 0x24)
  172. case REG_L: INST_RETURN(1, 0x2C)
  173. case REG_BC: INST_RETURN(1, 0x03)
  174. case REG_DE: INST_RETURN(1, 0x13)
  175. case REG_HL: INST_RETURN(1, 0x23)
  176. case REG_SP: INST_RETURN(1, 0x33)
  177. case REG_IX: INST_RETURN(2, 0xDD, 0x23)
  178. case REG_IY: INST_RETURN(2, 0xFD, 0x23)
  179. case REG_IXH: INST_RETURN(2, 0xDD, 0x2C)
  180. case REG_IXL: INST_RETURN(2, 0xFD, 0x2C)
  181. case REG_IYH: INST_RETURN(2, 0xDD, 0x2C)
  182. case REG_IYL: INST_RETURN(2, 0xFD, 0x2C)
  183. default: INST_ERROR(ARG0_BAD_REG)
  184. }
  185. case AT_INDIRECT:
  186. if (INST_ARG(0).data.indirect.type != AT_REGISTER)
  187. INST_ERROR(ARG0_TYPE)
  188. if (INST_ARG(0).data.indirect.addr.reg != REG_HL)
  189. INST_ERROR(ARG0_BAD_REG)
  190. INST_RETURN(2, 0x34)
  191. case AT_INDEXED:
  192. INST_RETURN(3, INST_INDEX_PREFIX(0), 0x34, INST_ARG(0).data.index.offset)
  193. default:
  194. INST_ERROR(ARG0_TYPE)
  195. }
  196. }
  197. /*
  198. INST_FUNC(add)
  199. {
  200. DEBUG("dispatched to -> ADD")
  201. return ED_PS_TOO_FEW_ARGS;
  202. }
  203. INST_FUNC(adc)
  204. {
  205. DEBUG("dispatched to -> ADC")
  206. return ED_PS_TOO_FEW_ARGS;
  207. }
  208. */
  209. /*
  210. Return the relevant ASMInstParser function for a given mnemonic.
  211. NULL is returned if the mnemonic is not known.
  212. */
  213. ASMInstParser get_inst_parser(char mstr[MAX_MNEMONIC_SIZE])
  214. {
  215. // Exploit the fact that we can store the entire mnemonic string as a
  216. // single 32-bit value to do fast lookups:
  217. uint32_t key = (mstr[0] << 24) + (mstr[1] << 16) + (mstr[2] << 8) + mstr[3];
  218. DEBUG("get_inst_parser(): -->%.*s<-- 0x%08X", MAX_MNEMONIC_SIZE, mstr, key)
  219. HANDLE(nop)
  220. HANDLE(inc)
  221. // HANDLE(add)
  222. // HANDLE(adc)
  223. return NULL;
  224. }