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.
 
 
 
 
 

90 lignes
2.6 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 "instructions.h"
  4. #include "../logging.h"
  5. /*
  6. TEMP SYNTAX NOTES:
  7. - http://clrhome.org/table/
  8. - http://www.z80.info/z80undoc.htm
  9. - http://www.z80.info/z80code.txt
  10. - http://www.z80.info/z80href.txt
  11. instruction := mnemonic [arg[, arg[, arg]]]
  12. mnemonic := [a-z0-9]{2-4}
  13. arg := register | immediate | label | indirect | indexed | condition | page0
  14. register := A | B | C | D | E | AF | BC | DE | HL | F | I | IX | IY | PC | R | SP
  15. immediate := 8-bit integer | 16-bit integer
  16. label := string
  17. indirect := \( (register | immediate) \)
  18. indexed := \( (IX | IY) + immediate \)
  19. condition := NZ | N | NC | C | PO | PE | P | M
  20. page0 := $0 | $8 | $10 | $18 | $20 | $28 | $30 | $38
  21. */
  22. /* Helper macros for get_inst_parser() */
  23. #define JOIN_(a, b, c, d) ((uint32_t) ((a << 24) + (b << 16) + (c << 8) + d))
  24. #define DISPATCH_(s, z) ( \
  25. (z) == 2 ? JOIN_(s[0], s[1], 0x00, 0x00) : \
  26. (z) == 3 ? JOIN_(s[0], s[1], s[2], 0x00) : \
  27. JOIN_(s[0], s[1], s[2], s[3])) \
  28. #define MAKE_CMP_(s) DISPATCH_(s, sizeof(s) / sizeof(char) - 1)
  29. #define HANDLE(m) if (key == MAKE_CMP_(#m)) return parse_inst_##m;
  30. /* Instruction parser functions */
  31. #define INST_FUNC(mnemonic) \
  32. static ASMErrorDesc parse_inst_##mnemonic( \
  33. uint8_t **bytes, size_t *length, char **symbol, const char *arg, size_t size)
  34. INST_FUNC(nop)
  35. {
  36. DEBUG("dispatched to -> NOP")
  37. return ED_PS_TOO_FEW_ARGS;
  38. }
  39. INST_FUNC(inc)
  40. {
  41. DEBUG("dispatched to -> INC")
  42. return ED_PS_TOO_FEW_ARGS;
  43. }
  44. INST_FUNC(add)
  45. {
  46. DEBUG("dispatched to -> ADD")
  47. return ED_PS_TOO_FEW_ARGS;
  48. }
  49. INST_FUNC(adc)
  50. {
  51. DEBUG("dispatched to -> ADC")
  52. return ED_PS_TOO_FEW_ARGS;
  53. }
  54. /*
  55. Return the relevant ASMInstParser function for a given mnemonic.
  56. NULL is returned if the mnemonic is not known.
  57. */
  58. ASMInstParser get_inst_parser(char mstr[MAX_MNEMONIC_SIZE])
  59. {
  60. // Exploit the fact that we can store the entire mnemonic string as a
  61. // single 32-bit value to do fast lookups:
  62. uint32_t key = (mstr[0] << 24) + (mstr[1] << 16) + (mstr[2] << 8) + mstr[3];
  63. DEBUG("get_inst_parser(): -->%.*s<-- 0x%08X", MAX_MNEMONIC_SIZE, mstr, key)
  64. HANDLE(nop)
  65. HANDLE(inc)
  66. HANDLE(add)
  67. HANDLE(adc)
  68. return NULL;
  69. }