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.
 
 
 
 
 

209 lines
7.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 "../util.h"
  8. /* Helper macros for get_inst_parser() and lookup_parser() */
  9. #define JOIN(a, b, c, d) ((uint32_t) ((a << 24) + (b << 16) + (c << 8) + d))
  10. #define DISPATCH_(s, z) ( \
  11. (z) == 2 ? JOIN(s[0], s[1], 0x00, 0x00) : \
  12. (z) == 3 ? JOIN(s[0], s[1], s[2], 0x00) : \
  13. JOIN(s[0], s[1], s[2], s[3])) \
  14. #define MAKE_CMP_(s) DISPATCH_(s, sizeof(s) / sizeof(char) - 1)
  15. #define HANDLE(m) if (key == MAKE_CMP_(#m)) return parse_inst_##m;
  16. /* Helper macro for parse_arg() */
  17. #define TRY_PARSER(func, argtype, field) \
  18. if (argparse_##func(&arg->data.field, info)) { \
  19. arg->type = argtype; \
  20. return ED_NONE; \
  21. }
  22. /* Internal helper macros */
  23. #define INST_ALLOC_(len) \
  24. *length = len; \
  25. *bytes = cr_malloc(sizeof(uint8_t) * (len));
  26. #define INST_SET_(b, val) ((*bytes)[b] = val)
  27. #define INST_SET1_(b1) INST_SET_(0, b1)
  28. #define INST_SET2_(b1, b2) INST_SET1_(b1), INST_SET_(1, b2)
  29. #define INST_SET3_(b1, b2, b3) INST_SET2_(b1, b2), INST_SET_(2, b3)
  30. #define INST_SET4_(b1, b2, b3, b4) INST_SET3_(b1, b2, b3), INST_SET_(3, b4)
  31. #define INST_DISPATCH_(a, b, c, d, target, ...) target
  32. #define INST_FILL_BYTES_(len, ...) \
  33. ((len > 4) ? fill_bytes_variadic(*bytes, len, __VA_ARGS__) : \
  34. INST_DISPATCH_(__VA_ARGS__, INST_SET4_, INST_SET3_, INST_SET2_, \
  35. INST_SET1_, __VA_ARGS__)(__VA_ARGS__));
  36. #define INST_IX_PREFIX 0xDD
  37. #define INST_IY_PREFIX 0xFD
  38. #define INST_PREFIX_(reg) \
  39. (((reg) == REG_IX || (reg) == REG_IXH || (reg) == REG_IXL) ? \
  40. INST_IX_PREFIX : INST_IY_PREFIX)
  41. #define INST_RETURN_WITH_SYMBOL_(len, label, ...) { \ // TODO
  42. *symbol = cr_strdup(label.text); \
  43. INST_ALLOC_(len) \
  44. INST_FILL_BYTES_(len - 2, __VA_ARGS__) \
  45. return ED_NONE; \
  46. }
  47. /* Helper macros for instruction parsers */
  48. #define INST_FUNC(mnemonic) \
  49. static ASMErrorDesc parse_inst_##mnemonic( \
  50. uint8_t **bytes, size_t *length, char **symbol, ASMArgParseInfo ap_info) \
  51. #define INST_ERROR(desc) return ED_PS_##desc;
  52. #define INST_TAKES_NO_ARGS \
  53. if (ap_info.arg) \
  54. INST_ERROR(TOO_MANY_ARGS)
  55. #define INST_TAKES_ARGS(a0, a1, a2) \
  56. if (!ap_info.arg) \
  57. INST_ERROR(TOO_FEW_ARGS) \
  58. ASMInstArg args[3]; \
  59. size_t nargs = 0; \
  60. ASMArgType masks = {a0, a1, a2}; \
  61. ASMErrorDesc err = parse_args(args, &nargs, ap_info, masks); \
  62. if (err) \
  63. return err; \
  64. #define INST_TYPE(n) args[n].type
  65. #define INST_REG(n) args[n].data.reg
  66. #define INST_IMM(n) args[n].data.imm
  67. #define INST_INDIRECT(n) args[n].data.indirect
  68. #define INST_INDEX(n) args[n].data.index
  69. #define INST_COND(n) args[n].data.cond
  70. #define INST_PORT(n) args[n].data.port
  71. #define INST_RETURN(len, ...) { \
  72. (void) symbol; \
  73. INST_ALLOC_(len) \
  74. INST_FILL_BYTES_(len, __VA_ARGS__) \
  75. return ED_NONE; \
  76. }
  77. #define INST_INDEX_PREFIX(n) INST_PREFIX_(INST_INDEX(n).reg)
  78. #define INST_INDIRECT_IMM(n) \ // TODO
  79. INST_INDIRECT(n).addr.imm.uval >> 8, \
  80. INST_INDIRECT(n).addr.imm.uval & 0xFF
  81. /* ----------------------------- END WORK BLOCK ---------------------------- */
  82. /*
  83. Fill an instruction's byte array with the given data.
  84. This internal function is only called for instructions longer than four
  85. bytes (of which there is only one: the fake emulator debugging/testing
  86. opcode with mnemonic "emu"), so it does not get used in normal situations.
  87. Return the value of the last byte inserted, for compatibility with the
  88. INST_SETn_ family of macros.
  89. */
  90. static uint8_t fill_bytes_variadic(uint8_t *bytes, size_t len, ...)
  91. {
  92. va_list vargs;
  93. va_start(vargs, len);
  94. for (size_t i = 0; i < len; i++)
  95. bytes[i] = va_arg(vargs, unsigned);
  96. va_end(vargs);
  97. return bytes[len - 1];
  98. }
  99. /*
  100. Parse a single instruction argument into an ASMInstArg object.
  101. Return ED_NONE (0) on success or an error code on failure.
  102. */
  103. static ASMErrorDesc parse_arg(
  104. ASMInstArg *arg, const char *str, size_t size, ASMDefineTable *deftable)
  105. {
  106. ASMArgParseInfo info = {.arg = str, .size = size, .deftable = deftable};
  107. TRY_PARSER(register, AT_REGISTER, reg)
  108. TRY_PARSER(condition, AT_CONDITION, cond)
  109. TRY_PARSER(indirect, AT_INDIRECT, indirect)
  110. TRY_PARSER(indexed, AT_INDEXED, index)
  111. TRY_PARSER(immediate, AT_IMMEDIATE, imm)
  112. return ED_PS_ARG_SYNTAX;
  113. }
  114. /*
  115. Parse an argument string into ASMInstArg objects.
  116. Return ED_NONE (0) on success or an error code on failure.
  117. */
  118. static ASMErrorDesc parse_args(
  119. ASMInstArg args[3], size_t *nargs, ASMArgParseInfo ap_info, ASMArgType masks[3]) // TODO
  120. {
  121. ASMErrorDesc err;
  122. ASMDefineTable *dt = ap_info.deftable;
  123. const char *str = ap_info.arg;
  124. size_t size = ap_info.size, start = 0, i = 0;
  125. while (i < size) {
  126. char c = str[i];
  127. if (c == ',') {
  128. if (i == start)
  129. return ED_PS_ARG_SYNTAX;
  130. if ((err = parse_arg(&args[*nargs], str + start, i - start, dt)))
  131. return err;
  132. (*nargs)++;
  133. i++;
  134. if (i < size && str[i] == ' ')
  135. i++;
  136. start = i;
  137. if (i == size)
  138. return ED_PS_ARG_SYNTAX;
  139. if (*nargs >= 3)
  140. return ED_PS_TOO_MANY_ARGS;
  141. } else {
  142. if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
  143. c == ' ' || c == '+' || c == '-' || c == '(' || c == ')' ||
  144. c == '$' || c == '_' || c == '.')
  145. i++;
  146. else
  147. return ED_PS_ARG_SYNTAX;
  148. }
  149. }
  150. if (i > start) {
  151. if ((err = parse_arg(&args[*nargs], str + start, i - start, dt)))
  152. return err;
  153. (*nargs)++;
  154. }
  155. return ED_NONE;
  156. }
  157. #include "instructions.inc.c"
  158. /*
  159. Return the relevant ASMInstParser function for a given mnemonic.
  160. NULL is returned if the mnemonic is not known.
  161. */
  162. ASMInstParser get_inst_parser(char mstr[MAX_MNEMONIC_SIZE])
  163. {
  164. // Exploit the fact that we can store the entire mnemonic string as a
  165. // single 32-bit value to do fast lookups:
  166. uint32_t key = JOIN(mstr[0], mstr[1], mstr[2], mstr[3]);
  167. return lookup_parser(key);
  168. }