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.

update_asm_instructions.py 14 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. self._has_optional_args = False
  50. def _get_arg_parse_mask(self, num):
  51. """
  52. Return the appropriate mask to parse_args() for the num-th argument.
  53. """
  54. types = set()
  55. optional = False
  56. for case in self._data["cases"]:
  57. if num < len(case["type"]):
  58. atype = case["type"][num]
  59. if atype in self.ARG_TYPES:
  60. types.add(self.ARG_TYPES[atype])
  61. else:
  62. types.update(self.PSEUDO_TYPES[atype])
  63. else:
  64. optional = True
  65. if not types:
  66. return "AT_NONE"
  67. if optional:
  68. types.add("AT_OPTIONAL")
  69. self._has_optional_args = True
  70. return "|".join(sorted(types))
  71. def _handle_return(self, ret, indent=1):
  72. """
  73. Return code to handle an instruction return statement.
  74. """
  75. data = ", ".join("0x%02X" % byte if isinstance(byte, int) else byte
  76. for byte in ret)
  77. return TAB * indent + "INST_RETURN({0}, {1})".format(len(ret), data)
  78. def _build_case_type_check(self, args):
  79. """
  80. Return the test part of an if statement for an instruction case.
  81. """
  82. conds = ["INST_TYPE({0}) == {1}".format(i, self.ARG_TYPES[cond])
  83. for i, cond in enumerate(args)]
  84. check = " && ".join(conds)
  85. if self._has_optional_args:
  86. return "INST_NARGS == {0} && ".format(len(args)) + check
  87. return check
  88. def _build_register_check(self, num, cond):
  89. """
  90. Return an expression to check for a particular register value.
  91. """
  92. return "INST_REG({0}) == REG_{1}".format(num, cond.upper())
  93. def _build_immediate_check(self, num, cond):
  94. """
  95. Return an expression to check for a particular immediate value.
  96. """
  97. if "." in cond:
  98. itype, value = cond.split(".", 1)
  99. try:
  100. value = int(value)
  101. except ValueError:
  102. value = int(value, 16)
  103. vtype = "sval" if itype.upper() in ["S8", "REL"] else "uval"
  104. test1 = "INST_IMM({0}).mask & IMM_{1}".format(num, itype.upper())
  105. if (itype.upper() == "U16"):
  106. test1 += " && !INST_IMM({0}).is_label".format(num)
  107. test2 = "INST_IMM({0}).{1} == {2}".format(num, vtype, value)
  108. return "({0} && {1})".format(test1, test2)
  109. return "INST_IMM({0}).mask & IMM_{1}".format(num, cond.upper())
  110. def _build_indirect_check(self, num, cond):
  111. """
  112. Return an expression to check for a particular indirect value.
  113. """
  114. if cond.startswith("reg."):
  115. test1 = "INST_INDIRECT({0}).type == AT_REGISTER".format(num)
  116. test2 = "INST_INDIRECT({0}).addr.reg == REG_{1}".format(
  117. num, cond[len("reg."):].upper())
  118. return "({0} && {1})".format(test1, test2)
  119. if cond == "imm" or cond == "immediate":
  120. return "INST_INDIRECT({0}).type == AT_IMMEDIATE".format(num)
  121. err = "Unknown condition for indirect argument: {0}"
  122. return RuntimeError(err.format(cond))
  123. def _build_indexed_check(self, num, cond):
  124. """
  125. Return an expression to check for a particular indexed value.
  126. """
  127. raise RuntimeError("The indexed arg type does not support conditions")
  128. def _build_condition_check(self, num, cond):
  129. """
  130. Return an expression to check for a particular condition value.
  131. """
  132. return "INST_COND({0}) == COND_{1}".format(num, cond.upper())
  133. def _build_port_check(self, num, cond):
  134. """
  135. Return an expression to check for a particular port value.
  136. """
  137. if cond.startswith("reg."):
  138. test1 = "INST_PORT({0}).type == AT_REGISTER".format(num)
  139. test2 = "INST_PORT({0}).port.reg == REG_{1}".format(
  140. num, cond[len("reg."):].upper())
  141. return "({0} && {1})".format(test1, test2)
  142. if cond == "imm" or cond == "immediate":
  143. return "INST_PORT({0}).type == AT_IMMEDIATE".format(num)
  144. err = "Unknown condition for port argument: {0}"
  145. return RuntimeError(err.format(cond))
  146. _SUBCASE_LOOKUP_TABLE = {
  147. "register": _build_register_check,
  148. "immediate": _build_immediate_check,
  149. "indirect": _build_indirect_check,
  150. "indexed": _build_indexed_check,
  151. "condition": _build_condition_check,
  152. "port": _build_port_check
  153. }
  154. def _build_subcase_check(self, types, conds):
  155. """
  156. Return the test part of an if statement for an instruction subcase.
  157. """
  158. conds = [self._SUBCASE_LOOKUP_TABLE[types[i]](self, i, cond)
  159. for i, cond in enumerate(conds) if cond != "_"]
  160. return " && ".join(conds)
  161. def _iter_permutations(self, types, conds):
  162. """
  163. Iterate over all permutations of the given subcase conditions.
  164. """
  165. def split(typ, cond):
  166. if "|" in cond:
  167. splits = [split(typ, c) for c in cond.split("|")]
  168. merged = [choice for s in splits for choice in s]
  169. if len(merged) != len(set(merged)):
  170. msg = "Repeated conditions for {0}: {1}"
  171. raise RuntimeError(msg.format(typ, cond))
  172. return merged
  173. if typ == "register":
  174. if cond == "i":
  175. return ["ix", "iy"]
  176. if cond == "ih":
  177. return ["ixh", "iyh"]
  178. if cond == "il":
  179. return ["ixl", "iyl"]
  180. return [cond]
  181. splits = [split(typ, cond) for typ, cond in zip(types, conds)]
  182. num = max(len(cond) for cond in splits)
  183. if any(1 < len(cond) < num for cond in splits):
  184. msg = "Invalid condition permutations: {0}"
  185. raise RuntimeError(msg.format(conds))
  186. choices = [cond * num if len(cond) == 1 else cond for cond in splits]
  187. return zip(*choices)
  188. def _adapt_return(self, types, conds, ret):
  189. """
  190. Return a modified byte list to accomodate for prefixes and immediates.
  191. """
  192. ret = ret[:]
  193. for i, byte in enumerate(ret):
  194. if not isinstance(byte, int):
  195. if byte == "u8":
  196. index = types.index("immediate")
  197. ret[i] = "INST_IMM({0}).uval".format(index)
  198. elif byte == "u16":
  199. if i < len(ret) - 1:
  200. raise RuntimeError("U16 return byte must be last")
  201. try:
  202. index = types.index("immediate")
  203. imm = "INST_IMM({0})".format(index)
  204. except ValueError:
  205. indir = types.index("indirect")
  206. if not conds[indir].startswith("imm"):
  207. msg = "Passing non-immediate indirect as immediate"
  208. raise RuntimeError(msg)
  209. imm = "INST_INDIRECT({0}).addr.imm".format(indir)
  210. ret[i] = "INST_IMM_U16_B1({0})".format(imm)
  211. ret.append("INST_IMM_U16_B2({0})".format(imm))
  212. break
  213. else:
  214. msg = "Unsupported return byte: {0}"
  215. raise RuntimeError(msg.format(byte))
  216. for i, cond in enumerate(conds):
  217. if types[i] == "register" and cond[0] == "i":
  218. prefix = "INST_I{0}_PREFIX".format(cond[1].upper())
  219. if ret[0] != prefix:
  220. ret.insert(0, prefix)
  221. elif types[i] == "indexed":
  222. ret.insert(0, "INST_INDEX_PREFIX({0})".format(i))
  223. ret.insert(2, "INST_INDEX({0}).offset".format(i))
  224. return ret
  225. def _handle_pseudo_case(self, pseudo, case):
  226. """
  227. Return code to handle an instruction pseudo-case.
  228. Pseudo-cases are cases that have pseudo-types as arguments. This means
  229. they are expanded to cover multiple "real" argument types.
  230. """
  231. index = case["type"].index(pseudo)
  232. if pseudo == "indirect_hl_or_indexed":
  233. case["type"][index] = "indexed"
  234. indexed = self._handle_case(case)
  235. case["type"][index] = "indirect"
  236. indirect = self._handle_case(case)
  237. base_cond = self._build_case_type_check(case["type"])
  238. hl_reg = TAB * 3 + self._build_indirect_check(index, "reg.hl")
  239. indirect[0] = TAB + "if ({0} &&\n{1}) {{".format(base_cond, hl_reg)
  240. return indirect + indexed
  241. raise RuntimeError("Unknown pseudo-type: {0}".format(pseudo))
  242. def _handle_case(self, case):
  243. """
  244. Return code to handle an instruction case.
  245. """
  246. ctype = case["type"]
  247. for pseudo in self.PSEUDO_TYPES:
  248. if pseudo in ctype:
  249. return self._handle_pseudo_case(pseudo, case)
  250. lines = []
  251. cond = self._build_case_type_check(ctype)
  252. lines.append(TAB + "if ({0}) {{".format(cond))
  253. subcases = [(perm, sub["return"]) for sub in case["cases"]
  254. for perm in self._iter_permutations(ctype, sub["cond"])]
  255. for cond, ret in subcases:
  256. check = self._build_subcase_check(ctype, cond)
  257. ret = self._adapt_return(ctype, cond, ret)
  258. if check:
  259. lines.append(TAB * 2 + "if ({0})".format(check))
  260. lines.append(self._handle_return(ret, 3))
  261. else:
  262. lines.append(self._handle_return(ret, 2))
  263. break # Unconditional subcase
  264. else:
  265. lines.append(TAB * 2 + "INST_ERROR(ARG_VALUE)")
  266. lines.append(TAB + "}")
  267. return lines
  268. def render(self):
  269. """
  270. Convert data for an individual instruction into a C parse function.
  271. """
  272. lines = []
  273. if self._data["args"]:
  274. lines.append("{tab}INST_TAKES_ARGS(\n{tab2}{0},\n{tab2}{1},"
  275. "\n{tab2}{2}\n{tab})".format(
  276. self._get_arg_parse_mask(0), self._get_arg_parse_mask(1),
  277. self._get_arg_parse_mask(2), tab=TAB, tab2=TAB * 2))
  278. else:
  279. lines.append(TAB + "INST_TAKES_NO_ARGS")
  280. if "return" in self._data:
  281. lines.append(self._handle_return(self._data["return"]))
  282. elif "cases" in self._data:
  283. for case in self._data["cases"]:
  284. lines.extend(self._handle_case(case))
  285. lines.append(TAB + "INST_ERROR(ARG_TYPE)")
  286. else:
  287. msg = "Missing return or case block for {0} instruction"
  288. raise RuntimeError(msg.format(self._name))
  289. contents = "\n".join(lines)
  290. return "INST_FUNC({0})\n{{\n{1}\n}}".format(self._name, contents)
  291. def build_inst_block(data):
  292. """
  293. Return the instruction parser block, given instruction data.
  294. """
  295. return "\n\n".join(
  296. Instruction(k, v).render() for k, v in sorted(data.items()))
  297. def build_lookup_block(data):
  298. """
  299. Return the instruction lookup block, given instruction data.
  300. """
  301. macro = TAB + "HANDLE({0})"
  302. return "\n".join(macro.format(inst) for inst in sorted(data.keys()))
  303. def process(template, data):
  304. """
  305. Return C code generated from a source template and instruction data.
  306. """
  307. inst_block = build_inst_block(data)
  308. lookup_block = build_lookup_block(data)
  309. date = time.asctime(time.gmtime())
  310. result = re_date.sub(r"\1{0} UTC".format(date), template)
  311. result = re_inst.sub(r"\1{0}\3".format(inst_block), result)
  312. result = re_lookup.sub(r"\1{0}\3".format(lookup_block), result)
  313. return result
  314. def main():
  315. """
  316. Main script entry point.
  317. """
  318. with open(SOURCE, "r") as fp:
  319. text = fp.read().decode(ENCODING)
  320. with open(DEST, "r") as fp:
  321. template = fp.read().decode(ENCODING)
  322. data = yaml.load(text)
  323. result = process(template, data)
  324. with open(DEST, "w") as fp:
  325. fp.write(result.encode(ENCODING))
  326. if __name__ == "__main__":
  327. main()