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.

instructions.c 13 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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(ASMInstArg *arg, const char *str, size_t size)
  116. {
  117. #define TRY_PARSER(func, argtype, field) \
  118. if (argparse_##func(&arg->data.field, str, size)) { \
  119. arg->type = argtype; \
  120. return ED_NONE; \
  121. }
  122. DEBUG("parse_arg(): -->%.*s<-- %zu", (int) size, str, size)
  123. TRY_PARSER(register, AT_REGISTER, reg)
  124. TRY_PARSER(immediate, AT_IMMEDIATE, imm)
  125. TRY_PARSER(indirect, AT_INDIRECT, indirect)
  126. TRY_PARSER(indexed, AT_INDEXED, index)
  127. TRY_PARSER(condition, AT_CONDITION, cond)
  128. TRY_PARSER(label, AT_LABEL, label)
  129. return ED_PS_ARG_SYNTAX;
  130. #undef TRY_PARSER
  131. }
  132. /*
  133. Parse an argument string int ASMInstArg objects.
  134. Return ED_NONE (0) on success or an error code on failure.
  135. */
  136. static ASMErrorDesc parse_args(
  137. ASMInstArg args[3], size_t *nargs, const char *str, size_t size)
  138. {
  139. ASMErrorDesc err;
  140. size_t start = 0, i = 0;
  141. while (i < size) {
  142. char c = str[i];
  143. if (c == ',') {
  144. if (i == start)
  145. return ED_PS_ARG_SYNTAX;
  146. if ((err = parse_arg(&args[*nargs], str + start, i - start)))
  147. return err;
  148. (*nargs)++;
  149. i++;
  150. if (i < size && str[i] == ' ')
  151. i++;
  152. start = i;
  153. if (i == size)
  154. return ED_PS_ARG_SYNTAX;
  155. if (*nargs >= 3)
  156. return ED_PS_TOO_MANY_ARGS;
  157. } else {
  158. if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
  159. c == ' ' || c == '+' || c == '-' || c == '(' || c == ')' ||
  160. c == '$' || c == '_' || c == '.')
  161. i++;
  162. else
  163. return ED_PS_ARG_SYNTAX;
  164. }
  165. }
  166. if (i > start) {
  167. if ((err = parse_arg(&args[*nargs], str + start, i - start)))
  168. return err;
  169. (*nargs)++;
  170. }
  171. return ED_NONE;
  172. }
  173. /* Instruction parser functions */
  174. INST_FUNC(nop)
  175. {
  176. INST_TAKES_NO_ARGS
  177. INST_RETURN(1, 0x00)
  178. }
  179. INST_FUNC(inc)
  180. {
  181. INST_TAKES_ARGS(1, 1)
  182. switch (INST_TYPE(0)) {
  183. case AT_REGISTER:
  184. switch (INST_REG(0)) {
  185. case REG_A: INST_RETURN(1, 0x3C)
  186. case REG_B: INST_RETURN(1, 0x04)
  187. case REG_C: INST_RETURN(1, 0x0C)
  188. case REG_D: INST_RETURN(1, 0x14)
  189. case REG_E: INST_RETURN(1, 0x1C)
  190. case REG_H: INST_RETURN(1, 0x24)
  191. case REG_L: INST_RETURN(1, 0x2C)
  192. case REG_BC: INST_RETURN(1, 0x03)
  193. case REG_DE: INST_RETURN(1, 0x13)
  194. case REG_HL: INST_RETURN(1, 0x23)
  195. case REG_SP: INST_RETURN(1, 0x33)
  196. case REG_IX: INST_RETURN(2, 0xDD, 0x23)
  197. case REG_IY: INST_RETURN(2, 0xFD, 0x23)
  198. case REG_IXH: INST_RETURN(2, 0xDD, 0x2C)
  199. case REG_IXL: INST_RETURN(2, 0xFD, 0x2C)
  200. case REG_IYH: INST_RETURN(2, 0xDD, 0x2C)
  201. case REG_IYL: INST_RETURN(2, 0xFD, 0x2C)
  202. default: INST_ERROR(ARG0_BAD_REG)
  203. }
  204. case AT_INDIRECT:
  205. INST_INDIRECT_HL_ONLY(0)
  206. INST_RETURN(1, 0x34)
  207. case AT_INDEXED:
  208. INST_RETURN(3, INST_INDEX_BYTES(0, 0x34))
  209. default:
  210. INST_ERROR(ARG0_TYPE)
  211. }
  212. }
  213. INST_FUNC(add)
  214. {
  215. INST_TAKES_ARGS(2, 2)
  216. INST_FORCE_TYPE(0, AT_REGISTER)
  217. switch (INST_REG(0)) {
  218. case REG_A:
  219. switch (INST_TYPE(1)) {
  220. case AT_REGISTER:
  221. switch (INST_REG(1)) {
  222. case REG_A: INST_RETURN(1, 0x87)
  223. case REG_B: INST_RETURN(1, 0x80)
  224. case REG_C: INST_RETURN(1, 0x81)
  225. case REG_D: INST_RETURN(1, 0x82)
  226. case REG_E: INST_RETURN(1, 0x83)
  227. case REG_H: INST_RETURN(1, 0x84)
  228. case REG_L: INST_RETURN(1, 0x85)
  229. case REG_IXH: INST_RETURN(2, 0xDD, 0x84)
  230. case REG_IXL: INST_RETURN(2, 0xDD, 0x85)
  231. case REG_IYH: INST_RETURN(2, 0xFD, 0x84)
  232. case REG_IYL: INST_RETURN(2, 0xFD, 0x85)
  233. default: INST_ERROR(ARG1_BAD_REG)
  234. }
  235. case AT_IMMEDIATE:
  236. INST_CHECK_IMM(1, IMM_U8)
  237. INST_RETURN(2, 0xC6, INST_IMM(1).uval)
  238. case AT_INDIRECT:
  239. INST_INDIRECT_HL_ONLY(1)
  240. INST_RETURN(1, 0x86)
  241. case AT_INDEXED:
  242. INST_RETURN(3, INST_INDEX_BYTES(1, 0x86))
  243. default:
  244. INST_ERROR(ARG1_TYPE)
  245. }
  246. case REG_HL:
  247. INST_FORCE_TYPE(1, AT_REGISTER)
  248. switch (INST_REG(1)) {
  249. case REG_BC: INST_RETURN(1, 0x09)
  250. case REG_DE: INST_RETURN(1, 0x19)
  251. case REG_HL: INST_RETURN(1, 0x29)
  252. case REG_SP: INST_RETURN(1, 0x39)
  253. default: INST_ERROR(ARG1_BAD_REG)
  254. }
  255. case REG_IX:
  256. case REG_IY:
  257. INST_FORCE_TYPE(1, AT_REGISTER)
  258. switch (INST_REG(1)) {
  259. case REG_BC: INST_RETURN(2, INST_INDEX_PREFIX(1), 0x09)
  260. case REG_DE: INST_RETURN(2, INST_INDEX_PREFIX(1), 0x19)
  261. case REG_IX:
  262. case REG_IY:
  263. if (INST_REG(0) != INST_REG(1))
  264. INST_ERROR(ARG1_BAD_REG)
  265. INST_RETURN(2, INST_INDEX_PREFIX(1), 0x29)
  266. case REG_SP: INST_RETURN(2, INST_INDEX_PREFIX(1), 0x39)
  267. default: INST_ERROR(ARG1_BAD_REG)
  268. }
  269. default:
  270. INST_ERROR(ARG0_TYPE)
  271. }
  272. }
  273. /*
  274. INST_FUNC(adc)
  275. {
  276. DEBUG("dispatched to -> ADC")
  277. return ED_PS_TOO_FEW_ARGS;
  278. }
  279. */
  280. INST_FUNC(reti)
  281. {
  282. INST_TAKES_NO_ARGS
  283. INST_RETURN(2, 0xED, 0x4D)
  284. }
  285. INST_FUNC(retn)
  286. {
  287. INST_TAKES_NO_ARGS
  288. INST_RETURN(2, 0xED, 0x45)
  289. }
  290. /*
  291. Return the relevant ASMInstParser function for a given mnemonic.
  292. NULL is returned if the mnemonic is not known.
  293. */
  294. ASMInstParser get_inst_parser(char mstr[MAX_MNEMONIC_SIZE])
  295. {
  296. // Exploit the fact that we can store the entire mnemonic string as a
  297. // single 32-bit value to do fast lookups:
  298. uint32_t key = (mstr[0] << 24) + (mstr[1] << 16) + (mstr[2] << 8) + mstr[3];
  299. HANDLE(nop)
  300. HANDLE(inc)
  301. HANDLE(add)
  302. // HANDLE(adc)
  303. HANDLE(reti)
  304. HANDLE(retn)
  305. return NULL;
  306. }