An emulator, assembler, and disassembler for the Sega Game Gear
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 

330 Zeilen
11 KiB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright (C) 2014-2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. # Released under the terms of the MIT License. See LICENSE for details.
  5. """
  6. This script generates 'src/assembler/instructions.inc.c' from
  7. 'src/assembler/instructions.yml'. It should be run automatically by make
  8. when the latter is modified, but can also be run manually.
  9. """
  10. from __future__ import print_function
  11. from itertools import product
  12. import re
  13. import time
  14. SOURCE = "src/assembler/instructions.yml"
  15. DEST = "src/assembler/instructions.inc.c"
  16. ENCODING = "utf8"
  17. TAB = " " * 4
  18. try:
  19. import yaml
  20. except ImportError:
  21. print("Error: PyYAML is required (https://pypi.python.org/pypi/PyYAML)\n"
  22. "If you don't want to rebuild {0}, do:\n`make -t {0}`".format(DEST))
  23. exit(1)
  24. re_date = re.compile(r"^(\s*@AUTOGEN_DATE\s*)(.*?)$", re.M)
  25. re_inst = re.compile(
  26. r"(/\* @AUTOGEN_INST_BLOCK_START \*/\n*)(.*?)"
  27. r"(\n*/\* @AUTOGEN_INST_BLOCK_END \*/)", re.S)
  28. re_lookup = re.compile(
  29. r"(/\* @AUTOGEN_LOOKUP_BLOCK_START \*/\n*)(.*?)"
  30. r"(\n*/\* @AUTOGEN_LOOKUP_BLOCK_END \*/)", re.S)
  31. class Instruction(object):
  32. """
  33. Represent a single ASM instruction mnemonic.
  34. """
  35. ARG_TYPES = {
  36. "register": "AT_REGISTER",
  37. "immediate": "AT_IMMEDIATE",
  38. "indirect": "AT_INDIRECT",
  39. "indexed": "AT_INDEXED",
  40. "condition": "AT_CONDITION",
  41. "port": "AT_PORT"
  42. }
  43. PSEUDO_TYPES = {
  44. "indirect_hl_or_indexed": ["AT_INDIRECT", "AT_INDEXED"]
  45. }
  46. def __init__(self, name, data):
  47. self._name = name
  48. self._data = data
  49. def _get_arg_parse_mask(self, num):
  50. """
  51. Return the appropriate mask to parse_args() for the num-th argument.
  52. """
  53. types = set()
  54. optional = False
  55. for case in self._data["cases"]:
  56. if num < len(case["type"]):
  57. atype = case["type"][num]
  58. if atype in self.ARG_TYPES:
  59. types.add(self.ARG_TYPES[atype])
  60. else:
  61. types.update(self.PSEUDO_TYPES[atype])
  62. else:
  63. optional = True
  64. if not types:
  65. return "AT_NONE"
  66. if optional:
  67. types.add("AT_OPTIONAL")
  68. return "|".join(types)
  69. def _handle_return(self, ret, indent=1):
  70. """
  71. Return code to handle an instruction return statement.
  72. """
  73. data = ", ".join("0x%02X" % byte if isinstance(byte, int) else byte
  74. for byte in ret)
  75. return TAB * indent + "INST_RETURN({0}, {1})".format(len(ret), data)
  76. def _build_case_type_check(self, args):
  77. """
  78. Return the test part of an if statement for an instruction case.
  79. """
  80. conds = ["INST_TYPE({0}) == {1}".format(i, self.ARG_TYPES[cond])
  81. for i, cond in enumerate(args)]
  82. return "INST_NARGS == {0} && {1}".format(len(args), " && ".join(conds))
  83. def _build_register_check(self, num, cond):
  84. """
  85. Return an expression to check for a particular register value.
  86. """
  87. return "INST_REG({0}) == REG_{1}".format(num, cond.upper())
  88. def _build_immediate_check(self, num, cond):
  89. """
  90. Return an expression to check for a particular immediate value.
  91. """
  92. # TODO: also allow direct value comparisons here
  93. return "INST_IMM({0}).mask & IMM_{1}".format(num, cond.upper())
  94. def _build_indirect_check(self, num, cond):
  95. """
  96. Return an expression to check for a particular indirect value.
  97. """
  98. if cond.startswith("reg."):
  99. test1 = "INST_INDIRECT({0}).type == AT_REGISTER".format(num)
  100. test2 = "INST_INDIRECT({0}).addr.reg == REG_{1}".format(
  101. num, cond[len("reg."):].upper())
  102. return "({0} && {1})".format(test1, test2)
  103. if cond == "imm" or cond == "immediate":
  104. return "INST_INDIRECT({0}).type == AT_IMMEDIATE".format(num)
  105. err = "Unknown condition for indirect argument: {0}"
  106. return RuntimeError(err.format(cond))
  107. def _build_indexed_check(self, num, cond):
  108. """
  109. Return an expression to check for a particular indexed value.
  110. """
  111. raise RuntimeError("The indexed arg type does not support conditions")
  112. def _build_condition_check(self, num, cond):
  113. """
  114. Return an expression to check for a particular condition value.
  115. """
  116. return "INST_COND({0}) == COND_{1}".format(num, cond.upper())
  117. def _build_port_check(self, num, cond):
  118. """
  119. Return an expression to check for a particular port value.
  120. """
  121. # TODO
  122. err = "Unknown condition for port argument: {0}"
  123. return RuntimeError(err.format(cond))
  124. _SUBCASE_LOOKUP_TABLE = {
  125. "register": _build_register_check,
  126. "immediate": _build_immediate_check,
  127. "indirect": _build_indirect_check,
  128. "indexed": _build_indexed_check,
  129. "condition": _build_condition_check,
  130. "port": _build_port_check
  131. }
  132. def _build_subcase_check(self, types, conds):
  133. """
  134. Return the test part of an if statement for an instruction subcase.
  135. """
  136. return " && ".join(self._SUBCASE_LOOKUP_TABLE[types[i]](self, i, cond)
  137. for i, cond in enumerate(conds) if cond != "_")
  138. def _iter_permutations(self, types, conds):
  139. """
  140. Iterate over all permutations of the given subcase conditions.
  141. """
  142. def split(typ, cond):
  143. if "|" in cond:
  144. sets = [split(typ, c) for c in cond.split("|")]
  145. return {choice for s in sets for choice in s}
  146. if typ == "register":
  147. if cond == "i":
  148. return {"ix", "iy"}
  149. if cond == "ih":
  150. return {"ixh", "iyh"}
  151. if cond == "il":
  152. return {"ixl", "iyl"}
  153. return {cond}
  154. return product(*(split(types[i], cond)
  155. for i, cond in enumerate(conds)))
  156. def _adapt_return(self, types, conds, ret):
  157. """
  158. Return a modified byte list to accomodate for prefixes and immediates.
  159. """
  160. ret = ret[:]
  161. for i, byte in enumerate(ret):
  162. if not isinstance(byte, int):
  163. if byte == "u8":
  164. imm = types.index("immediate")
  165. ret[i] = "INST_IMM({0}).uval".format(imm)
  166. else:
  167. msg = "Unsupported return byte: {0}"
  168. raise RuntimeError(msg.format(byte))
  169. for i, cond in enumerate(conds):
  170. if types[i] == "register" and cond.startswith("ix"):
  171. ret.insert(0, "INST_IX_PREFIX")
  172. elif types[i] == "register" and cond.startswith("iy"):
  173. ret.insert(0, "INST_IY_PREFIX")
  174. elif types[i] == "indexed":
  175. ret.insert(0, "INST_INDEX_PREFIX({0})".format(i))
  176. ret.append("INST_INDEX({0}).offset".format(i))
  177. return ret
  178. def _handle_pseudo_case(self, pseudo, case):
  179. """
  180. Return code to handle an instruction pseudo-case.
  181. Pseudo-cases are cases that have pseudo-types as arguments. This means
  182. they are expanded to cover multiple "real" argument types.
  183. """
  184. index = case["type"].index(pseudo)
  185. if pseudo == "indirect_hl_or_indexed":
  186. case["type"][index] = "indexed"
  187. indexed = self._handle_case(case)
  188. case["type"][index] = "indirect"
  189. for subcase in case["cases"]:
  190. if subcase["cond"][index] != "_":
  191. raise RuntimeError(
  192. "indirect_hl_or_indexed pseudo-type requires a "
  193. "wildcard (_) in all corresponding conditionals")
  194. subcase["cond"][index] = "reg.hl"
  195. return self._handle_case(case) + indexed
  196. raise RuntimeError("Unknown pseudo-type: {0}".format(pseudo))
  197. def _handle_case(self, case):
  198. """
  199. Return code to handle an instruction case.
  200. """
  201. for pseudo in self.PSEUDO_TYPES:
  202. if pseudo in case["type"]:
  203. return self._handle_pseudo_case(pseudo, case)
  204. lines = []
  205. cond = self._build_case_type_check(case["type"])
  206. lines.append(TAB + "if ({0}) {{".format(cond))
  207. for subcase in case["cases"]:
  208. for perm in self._iter_permutations(case["type"], subcase["cond"]):
  209. cond = self._build_subcase_check(case["type"], perm)
  210. ret = self._adapt_return(case["type"], perm, subcase["return"])
  211. lines.append(TAB * 2 + "if ({0})".format(cond))
  212. lines.append(self._handle_return(ret, 3))
  213. lines.append(TAB * 2 + "INST_ERROR(ARG_VALUE)")
  214. lines.append(TAB + "}")
  215. return lines
  216. def render(self):
  217. """
  218. Convert data for an individual instruction into a C parse function.
  219. """
  220. lines = []
  221. if self._data["args"]:
  222. lines.append("{tab}INST_TAKES_ARGS(\n{tab2}{0}, \n{tab2}{1}, "
  223. "\n{tab2}{2}\n{tab})".format(
  224. self._get_arg_parse_mask(0), self._get_arg_parse_mask(1),
  225. self._get_arg_parse_mask(2), tab=TAB, tab2=TAB * 2))
  226. else:
  227. lines.append(TAB + "INST_TAKES_NO_ARGS")
  228. if "return" in self._data:
  229. lines.append(self._handle_return(self._data["return"]))
  230. elif "cases" in self._data:
  231. for case in self._data["cases"]:
  232. lines.extend(self._handle_case(case))
  233. lines.append(TAB + "INST_ERROR(ARG_TYPE)")
  234. else:
  235. msg = "Missing return or case block for {0} instruction"
  236. raise RuntimeError(msg.format(self._name))
  237. contents = "\n".join(lines)
  238. return "INST_FUNC({0})\n{{\n{1}\n}}".format(self._name, contents)
  239. def build_inst_block(data):
  240. """
  241. Return the instruction parser block, given instruction data.
  242. """
  243. return "\n\n".join(
  244. Instruction(k, v).render() for k, v in sorted(data.items()))
  245. def build_lookup_block(data):
  246. """
  247. Return the instruction lookup block, given instruction data.
  248. """
  249. macro = TAB + "HANDLE({0})"
  250. return "\n".join(macro.format(inst) for inst in sorted(data.keys()))
  251. def process(template, data):
  252. """
  253. Return C code generated from a source template and instruction data.
  254. """
  255. inst_block = build_inst_block(data)
  256. lookup_block = build_lookup_block(data)
  257. date = time.asctime(time.gmtime())
  258. result = re_date.sub(r"\1{0} UTC".format(date), template)
  259. result = re_inst.sub(r"\1{0}\3".format(inst_block), result)
  260. result = re_lookup.sub(r"\1{0}\3".format(lookup_block), result)
  261. return result
  262. def main():
  263. """
  264. Main script entry point.
  265. """
  266. with open(SOURCE, "r") as fp:
  267. text = fp.read().decode(ENCODING)
  268. with open(DEST, "r") as fp:
  269. template = fp.read().decode(ENCODING)
  270. data = yaml.load(text)
  271. result = process(template, data)
  272. # with open(DEST, "w") as fp:
  273. # fp.write(result.encode(ENCODING))
  274. print(result) # TODO: remove me!
  275. if __name__ == "__main__":
  276. main()