An emulator, assembler, and disassembler for the Sega Game Gear
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

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