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.
 
 
 
 
 

361 lines
13 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 "parse_util.h"
  8. #include "../logging.h"
  9. /* Helper macros for get_inst_parser() */
  10. #define JOIN_(a, b, c, d) ((uint32_t) ((a << 24) + (b << 16) + (c << 8) + d))
  11. #define DISPATCH_(s, z) ( \
  12. (z) == 2 ? JOIN_(s[0], s[1], 0x00, 0x00) : \
  13. (z) == 3 ? JOIN_(s[0], s[1], s[2], 0x00) : \
  14. JOIN_(s[0], s[1], s[2], s[3])) \
  15. #define MAKE_CMP_(s) DISPATCH_(s, sizeof(s) / sizeof(char) - 1)
  16. #define HANDLE(m) if (key == MAKE_CMP_(#m)) return parse_inst_##m;
  17. /* Internal helper macros for instruction parsers */
  18. #define INST_ALLOC_(len) \
  19. *length = len; \
  20. if (!(*bytes = malloc(sizeof(uint8_t) * (len)))) \
  21. OUT_OF_MEMORY()
  22. #define INST_SET_(b, val) ((*bytes)[b] = val)
  23. #define INST_SET1_(b1) INST_SET_(0, b1)
  24. #define INST_SET2_(b1, b2) INST_SET1_(b1), INST_SET_(1, b2)
  25. #define INST_SET3_(b1, b2, b3) INST_SET2_(b1, b2), INST_SET_(2, b3)
  26. #define INST_SET4_(b1, b2, b3, b4) INST_SET3_(b1, b2, b3), INST_SET_(3, b4)
  27. #define INST_DISPATCH_(a, b, c, d, target, ...) target
  28. #define INST_FILL_BYTES_(len, ...) \
  29. ((len > 4) ? fill_bytes_variadic(*bytes, len, __VA_ARGS__) : \
  30. INST_DISPATCH_(__VA_ARGS__, INST_SET4_, INST_SET3_, INST_SET2_, \
  31. INST_SET1_, __VA_ARGS__)(__VA_ARGS__));
  32. #define INST_PREFIX_(reg) \
  33. (((reg) == REG_IX || (reg) == REG_IXH || (reg) == REG_IXL) ? 0xDD : 0xFD)
  34. /* Helper macros for instruction parsers */
  35. #define INST_FUNC(mnemonic) \
  36. static ASMErrorDesc parse_inst_##mnemonic( \
  37. uint8_t **bytes, size_t *length, char **symbol, const char *arg, size_t size)
  38. #define INST_ERROR(desc) return ED_PS_##desc;
  39. #define INST_TAKES_NO_ARGS \
  40. if (arg) \
  41. INST_ERROR(TOO_MANY_ARGS) \
  42. (void) size;
  43. #define INST_TAKES_ARGS(lo, hi) \
  44. if (!arg) \
  45. INST_ERROR(TOO_FEW_ARGS) \
  46. ASMInstArg args[3]; \
  47. size_t nargs = 0; \
  48. ASMErrorDesc err = parse_args(args, &nargs, arg, size); \
  49. if (err) \
  50. return err; \
  51. if (nargs < lo) \
  52. INST_ERROR(TOO_FEW_ARGS) \
  53. if (nargs > hi) \
  54. INST_ERROR(TOO_MANY_ARGS)
  55. #define INST_NARGS nargs
  56. #define INST_TYPE(n) args[n].type
  57. #define INST_REG(n) args[n].data.reg
  58. #define INST_IMM(n) args[n].data.imm
  59. #define INST_INDIRECT(n) args[n].data.indirect
  60. #define INST_INDEX(n) args[n].data.index
  61. #define INST_LABEL(n) args[n].data.label
  62. #define INST_COND(n) args[n].data.cond
  63. #define INST_FORCE_TYPE(n, t) { \
  64. if (INST_TYPE(n) != t) \
  65. INST_ERROR(ARG##n##_TYPE) \
  66. }
  67. #define INST_CHECK_IMM(n, m) { \
  68. if (!(INST_IMM(n).mask & (m))) \
  69. INST_ERROR(ARG##n##_RANGE) \
  70. }
  71. #define INST_INDIRECT_HL_ONLY(n) { \
  72. if (INST_INDIRECT(n).type != AT_REGISTER) \
  73. INST_ERROR(ARG##n##_TYPE) \
  74. if (INST_INDIRECT(n).addr.reg != REG_HL) \
  75. INST_ERROR(ARG##n##_BAD_REG) \
  76. }
  77. #define INST_RETURN(len, ...) { \
  78. (void) symbol; \
  79. INST_ALLOC_(len) \
  80. INST_FILL_BYTES_(len, __VA_ARGS__) \
  81. return ED_NONE; \
  82. }
  83. #define INST_RETURN_WITH_SYMBOL(len, label, ...) { \
  84. *symbol = strdup(label); \
  85. if (!(*symbol)) \
  86. OUT_OF_MEMORY() \
  87. INST_ALLOC_(len) \
  88. INST_FILL_BYTES_(len - 2, __VA_ARGS__) \
  89. return ED_NONE; \
  90. }
  91. #define INST_INDEX_PREFIX(n) INST_PREFIX_(INST_INDEX(n).reg)
  92. #define INST_INDEX_BYTES(n, b) \
  93. INST_INDEX_PREFIX(n), b, INST_INDEX(n).offset
  94. /*
  95. Fill an instruction's byte array with the given data.
  96. This internal function is only called for instructions longer than four
  97. bytes (of which there is only one: the fake emulator debugging/testing
  98. opcode with mnemonic "emu"), so it does not get used in normal situations.
  99. Return the value of the last byte inserted, for compatibility with the
  100. INST_SETn_ family of macros.
  101. */
  102. static uint8_t fill_bytes_variadic(uint8_t *bytes, size_t len, ...)
  103. {
  104. va_list vargs;
  105. va_start(vargs, len);
  106. for (size_t i = 0; i < len; i++)
  107. bytes[i] = va_arg(vargs, unsigned);
  108. va_end(vargs);
  109. return bytes[len - 1];
  110. }
  111. /*
  112. Parse a single instruction argument into an ASMInstArg object.
  113. Return ED_NONE (0) on success or an error code on failure.
  114. */
  115. static ASMErrorDesc parse_arg(
  116. ASMInstArg *arg, const char *str, size_t size, char **symbol)
  117. {
  118. #define USE_PARSER(func, argtype, field) \
  119. if (argparse_##func(&arg->data.field, str, size)) { \
  120. arg->type = argtype; \
  121. return ED_NONE; \
  122. }
  123. DEBUG("parse_arg(): -->%.*s<-- %zu", (int) size, str, size)
  124. USE_PARSER(register, AT_REGISTER, reg)
  125. USE_PARSER(immediate, AT_IMMEDIATE, imm)
  126. USE_PARSER(indirect, AT_INDIRECT, indirect)
  127. USE_PARSER(indexed, AT_INDEXED, index)
  128. USE_PARSER(condition, AT_CONDITION, cond)
  129. // AT_LABEL
  130. // ASMArgLabel label;
  131. return ED_PS_ARG_SYNTAX;
  132. #undef USE_PARSER
  133. }
  134. /*
  135. Parse an argument string int ASMInstArg objects.
  136. Return ED_NONE (0) on success or an error code on failure.
  137. */
  138. static ASMErrorDesc parse_args(
  139. ASMInstArg args[3], size_t *nargs, const char *str, size_t size)
  140. {
  141. ASMErrorDesc err;
  142. static char *symbol = NULL;
  143. size_t start = 0, i = 0;
  144. while (i < size) {
  145. char c = str[i];
  146. if (c == ',') {
  147. if (i == start)
  148. return ED_PS_ARG_SYNTAX;
  149. if ((err = parse_arg(&args[*nargs], str + start, i - start, &symbol)))
  150. return err;
  151. (*nargs)++;
  152. i++;
  153. if (i < size && str[i] == ' ')
  154. i++;
  155. start = i;
  156. if (i == size)
  157. return ED_PS_ARG_SYNTAX;
  158. if (*nargs >= 3)
  159. return ED_PS_TOO_MANY_ARGS;
  160. } else {
  161. if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
  162. c == ' ' || c == '+' || c == '-' || c == '(' || c == ')' ||
  163. c == '$' || c == '_' || c == '.')
  164. i++;
  165. else
  166. return ED_PS_ARG_SYNTAX;
  167. }
  168. }
  169. if (i > start) {
  170. if ((err = parse_arg(&args[*nargs], str + start, i - start, &symbol)))
  171. return err;
  172. (*nargs)++;
  173. }
  174. return ED_NONE;
  175. }
  176. /* Instruction parser functions */
  177. INST_FUNC(nop)
  178. {
  179. INST_TAKES_NO_ARGS
  180. INST_RETURN(1, 0x00)
  181. }
  182. INST_FUNC(inc)
  183. {
  184. INST_TAKES_ARGS(1, 1)
  185. switch (INST_TYPE(0)) {
  186. case AT_REGISTER:
  187. switch (INST_REG(0)) {
  188. case REG_A: INST_RETURN(1, 0x3C)
  189. case REG_B: INST_RETURN(1, 0x04)
  190. case REG_C: INST_RETURN(1, 0x0C)
  191. case REG_D: INST_RETURN(1, 0x14)
  192. case REG_E: INST_RETURN(1, 0x1C)
  193. case REG_H: INST_RETURN(1, 0x24)
  194. case REG_L: INST_RETURN(1, 0x2C)
  195. case REG_BC: INST_RETURN(1, 0x03)
  196. case REG_DE: INST_RETURN(1, 0x13)
  197. case REG_HL: INST_RETURN(1, 0x23)
  198. case REG_SP: INST_RETURN(1, 0x33)
  199. case REG_IX: INST_RETURN(2, 0xDD, 0x23)
  200. case REG_IY: INST_RETURN(2, 0xFD, 0x23)
  201. case REG_IXH: INST_RETURN(2, 0xDD, 0x2C)
  202. case REG_IXL: INST_RETURN(2, 0xFD, 0x2C)
  203. case REG_IYH: INST_RETURN(2, 0xDD, 0x2C)
  204. case REG_IYL: INST_RETURN(2, 0xFD, 0x2C)
  205. default: INST_ERROR(ARG0_BAD_REG)
  206. }
  207. case AT_INDIRECT:
  208. INST_INDIRECT_HL_ONLY(0)
  209. INST_RETURN(1, 0x34)
  210. case AT_INDEXED:
  211. INST_RETURN(3, INST_INDEX_BYTES(0, 0x34))
  212. default:
  213. INST_ERROR(ARG0_TYPE)
  214. }
  215. }
  216. INST_FUNC(add)
  217. {
  218. INST_TAKES_ARGS(2, 2)
  219. INST_FORCE_TYPE(0, AT_REGISTER)
  220. switch (INST_REG(0)) {
  221. case REG_A:
  222. switch (INST_TYPE(1)) {
  223. case AT_REGISTER:
  224. switch (INST_REG(1)) {
  225. case REG_A: INST_RETURN(1, 0x87)
  226. case REG_B: INST_RETURN(1, 0x80)
  227. case REG_C: INST_RETURN(1, 0x81)
  228. case REG_D: INST_RETURN(1, 0x82)
  229. case REG_E: INST_RETURN(1, 0x83)
  230. case REG_H: INST_RETURN(1, 0x84)
  231. case REG_L: INST_RETURN(1, 0x85)
  232. case REG_IXH: INST_RETURN(2, 0xDD, 0x84)
  233. case REG_IXL: INST_RETURN(2, 0xDD, 0x85)
  234. case REG_IYH: INST_RETURN(2, 0xFD, 0x84)
  235. case REG_IYL: INST_RETURN(2, 0xFD, 0x85)
  236. default: INST_ERROR(ARG1_BAD_REG)
  237. }
  238. case AT_IMMEDIATE:
  239. INST_CHECK_IMM(1, IMM_U8)
  240. INST_RETURN(2, 0xC6, INST_IMM(1).uval)
  241. case AT_INDIRECT:
  242. INST_INDIRECT_HL_ONLY(1)
  243. INST_RETURN(1, 0x86)
  244. case AT_INDEXED:
  245. INST_RETURN(3, INST_INDEX_BYTES(1, 0x86))
  246. default:
  247. INST_ERROR(ARG1_TYPE)
  248. }
  249. case REG_HL:
  250. INST_FORCE_TYPE(1, AT_REGISTER)
  251. switch (INST_REG(1)) {
  252. case REG_BC: INST_RETURN(1, 0x09)
  253. case REG_DE: INST_RETURN(1, 0x19)
  254. case REG_HL: INST_RETURN(1, 0x29)
  255. case REG_SP: INST_RETURN(1, 0x39)
  256. default: INST_ERROR(ARG1_BAD_REG)
  257. }
  258. case REG_IX:
  259. case REG_IY:
  260. INST_FORCE_TYPE(1, AT_REGISTER)
  261. switch (INST_REG(1)) {
  262. case REG_BC: INST_RETURN(2, INST_INDEX_PREFIX(1), 0x09)
  263. case REG_DE: INST_RETURN(2, INST_INDEX_PREFIX(1), 0x19)
  264. case REG_IX:
  265. case REG_IY:
  266. if (INST_REG(0) != INST_REG(1))
  267. INST_ERROR(ARG1_BAD_REG)
  268. INST_RETURN(2, INST_INDEX_PREFIX(1), 0x29)
  269. case REG_SP: INST_RETURN(2, INST_INDEX_PREFIX(1), 0x39)
  270. default: INST_ERROR(ARG1_BAD_REG)
  271. }
  272. default:
  273. INST_ERROR(ARG0_TYPE)
  274. }
  275. }
  276. /*
  277. INST_FUNC(adc)
  278. {
  279. DEBUG("dispatched to -> ADC")
  280. return ED_PS_TOO_FEW_ARGS;
  281. }
  282. */
  283. INST_FUNC(reti)
  284. {
  285. INST_TAKES_NO_ARGS
  286. INST_RETURN(2, 0xED, 0x4D)
  287. }
  288. INST_FUNC(retn)
  289. {
  290. INST_TAKES_NO_ARGS
  291. INST_RETURN(2, 0xED, 0x45)
  292. }
  293. /*
  294. Return the relevant ASMInstParser function for a given mnemonic.
  295. NULL is returned if the mnemonic is not known.
  296. */
  297. ASMInstParser get_inst_parser(char mstr[MAX_MNEMONIC_SIZE])
  298. {
  299. // Exploit the fact that we can store the entire mnemonic string as a
  300. // single 32-bit value to do fast lookups:
  301. uint32_t key = (mstr[0] << 24) + (mstr[1] << 16) + (mstr[2] << 8) + mstr[3];
  302. HANDLE(nop)
  303. HANDLE(inc)
  304. HANDLE(add)
  305. // HANDLE(adc)
  306. HANDLE(reti)
  307. HANDLE(retn)
  308. return NULL;
  309. }