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.
 
 
 
 
 

495 line
16 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. import re
  12. import time
  13. SOURCE = "src/assembler/instructions.yml"
  14. DEST = "src/assembler/instructions.inc.c"
  15. ENCODING = "utf8"
  16. TAB = " " * 4
  17. try:
  18. import yaml
  19. except ImportError:
  20. print("Error: PyYAML is required (https://pypi.python.org/pypi/PyYAML)\n"
  21. "If you don't want to rebuild {0}, do:\n`make -t {0}`".format(DEST))
  22. exit(1)
  23. re_date = re.compile(r"^(\s*@AUTOGEN_DATE\s*)(.*?)$", re.M)
  24. re_inst = re.compile(
  25. r"(/\* @AUTOGEN_INST_BLOCK_START \*/\n*)(.*?)"
  26. r"(\n*/\* @AUTOGEN_INST_BLOCK_END \*/)", re.S)
  27. re_lookup = re.compile(
  28. r"(/\* @AUTOGEN_LOOKUP_BLOCK_START \*/\n*)(.*?)"
  29. r"(\n*/\* @AUTOGEN_LOOKUP_BLOCK_END \*/)", re.S)
  30. def _rindex(L, val):
  31. """
  32. Return the index of the last occurence of val in L.
  33. """
  34. return len(L) - L[::-1].index(val) - 1
  35. def _atoi(value):
  36. """
  37. Try to convert a string to an integer, supporting decimal and hexadecimal.
  38. """
  39. try:
  40. return int(value)
  41. except ValueError:
  42. return int(value, 16)
  43. def _is_call(call, func):
  44. """
  45. Return whether the first argument is a function call of the second.
  46. """
  47. return call.startswith(func + "(") and call.endswith(")")
  48. def _call_args(call):
  49. """
  50. Given a call and a function name, return the function call arguments.
  51. """
  52. return call[call.index("(") + 1:-1].strip()
  53. def _parse_step_args(call):
  54. """
  55. Parse arguments to a step function (e.g. reg() or cond()).
  56. """
  57. args = _call_args(call)
  58. if " " in args:
  59. return map(_atoi, args.split(" "))
  60. else:
  61. return _atoi(args), 1
  62. class ASMInstError(Exception):
  63. """
  64. Base class for all errors while trying to generate the instructions file.
  65. """
  66. class Instruction(object):
  67. """
  68. Represent a single ASM instruction mnemonic.
  69. """
  70. ARG_TYPES = {
  71. "register": "AT_REGISTER",
  72. "immediate": "AT_IMMEDIATE",
  73. "indirect": "AT_INDIRECT",
  74. "indexed": "AT_INDEXED",
  75. "condition": "AT_CONDITION",
  76. "port": "AT_PORT"
  77. }
  78. PSEUDO_TYPES = {
  79. "indirect_hl_or_indexed": ["AT_INDIRECT", "AT_INDEXED"]
  80. }
  81. REGISTER_OFFSETS = {
  82. "a": 7,
  83. "b": 0,
  84. "c": 1,
  85. "d": 2,
  86. "e": 3,
  87. "h": 4,
  88. "ixh": 4,
  89. "iyh": 4,
  90. "l": 5,
  91. "ixl": 5,
  92. "iyl": 5,
  93. "bc": 0,
  94. "de": 1,
  95. "hl": 2,
  96. "ix": 2,
  97. "iy": 2,
  98. "sp": 3,
  99. "af": 3
  100. }
  101. CONDITION_ORDER = ["nz", "z", "nc", "c", "po", "pe", "p", "m"]
  102. def __init__(self, name, data):
  103. self._name = name
  104. self._data = data
  105. self._has_optional_args = False
  106. def _get_arg_parse_mask(self, num):
  107. """
  108. Return the appropriate mask to parse_args() for the num-th argument.
  109. """
  110. types = set()
  111. optional = False
  112. for case in self._data["cases"]:
  113. if num < len(case["type"]):
  114. atype = case["type"][num]
  115. if atype in self.ARG_TYPES:
  116. types.add(self.ARG_TYPES[atype])
  117. else:
  118. types.update(self.PSEUDO_TYPES[atype])
  119. else:
  120. optional = True
  121. if not types:
  122. return "AT_NONE"
  123. if optional:
  124. types.add("AT_OPTIONAL")
  125. self._has_optional_args = True
  126. return "|".join(sorted(types))
  127. def _handle_return(self, ret, indent=1):
  128. """
  129. Return code to handle an instruction return statement.
  130. """
  131. data = ", ".join("0x%02X" % byte if isinstance(byte, int) else byte
  132. for byte in ret)
  133. return TAB * indent + "INST_RETURN({0}, {1})".format(len(ret), data)
  134. def _build_case_type_check(self, args):
  135. """
  136. Return the test part of an if statement for an instruction case.
  137. """
  138. conds = ["INST_TYPE({0}) == {1}".format(i, self.ARG_TYPES[cond])
  139. for i, cond in enumerate(args)]
  140. check = " && ".join(conds)
  141. if self._has_optional_args:
  142. return "INST_NARGS == {0} && ".format(len(args)) + check
  143. return check
  144. def _build_register_check(self, num, cond):
  145. """
  146. Return an expression to check for a particular register value.
  147. """
  148. return "INST_REG({0}) == REG_{1}".format(num, cond.upper())
  149. def _build_immediate_check(self, num, cond):
  150. """
  151. Return an expression to check for a particular immediate value.
  152. """
  153. if "." in cond:
  154. itype, value = cond.split(".", 1)
  155. vtype = "sval" if itype.upper() in ["S8", "REL"] else "uval"
  156. test1 = "INST_IMM({0}).mask & IMM_{1}".format(num, itype.upper())
  157. if (itype.upper() == "U16"):
  158. test1 += " && !INST_IMM({0}).is_label".format(num)
  159. test2 = "INST_IMM({0}).{1} == {2}".format(num, vtype, _atoi(value))
  160. return "({0} && {1})".format(test1, test2)
  161. return "INST_IMM({0}).mask & IMM_{1}".format(num, cond.upper())
  162. def _build_indirect_check(self, num, cond):
  163. """
  164. Return an expression to check for a particular indirect value.
  165. """
  166. if cond.startswith("reg."):
  167. test1 = "INST_INDIRECT({0}).type == AT_REGISTER".format(num)
  168. test2 = "INST_INDIRECT({0}).addr.reg == REG_{1}".format(
  169. num, cond[len("reg."):].upper())
  170. return "({0} && {1})".format(test1, test2)
  171. if cond == "imm" or cond == "immediate":
  172. return "INST_INDIRECT({0}).type == AT_IMMEDIATE".format(num)
  173. err = "Unknown condition for indirect argument: {0}"
  174. return ASMInstError(err.format(cond))
  175. def _build_indexed_check(self, num, cond):
  176. """
  177. Return an expression to check for a particular indexed value.
  178. """
  179. raise ASMInstError("The indexed arg type does not support conditions")
  180. def _build_condition_check(self, num, cond):
  181. """
  182. Return an expression to check for a particular condition value.
  183. """
  184. return "INST_COND({0}) == COND_{1}".format(num, cond.upper())
  185. def _build_port_check(self, num, cond):
  186. """
  187. Return an expression to check for a particular port value.
  188. """
  189. if cond == "reg" or cond == "reg.c":
  190. return "INST_PORT({0}).type == AT_REGISTER".format(num)
  191. if cond == "imm" or cond == "immediate":
  192. return "INST_PORT({0}).type == AT_IMMEDIATE".format(num)
  193. err = "Unknown condition for port argument: {0}"
  194. return ASMInstError(err.format(cond))
  195. _SUBCASE_LOOKUP_TABLE = {
  196. "register": _build_register_check,
  197. "immediate": _build_immediate_check,
  198. "indirect": _build_indirect_check,
  199. "indexed": _build_indexed_check,
  200. "condition": _build_condition_check,
  201. "port": _build_port_check
  202. }
  203. def _build_subcase_check(self, types, conds):
  204. """
  205. Return the test part of an if statement for an instruction subcase.
  206. """
  207. conds = [self._SUBCASE_LOOKUP_TABLE[types[i]](self, i, cond)
  208. for i, cond in enumerate(conds) if cond != "_"]
  209. return " && ".join(conds)
  210. def _iter_permutations(self, types, conds):
  211. """
  212. Iterate over all permutations of the given subcase conditions.
  213. """
  214. def split(typ, cond):
  215. if "|" in cond:
  216. splits = [split(typ, c) for c in cond.split("|")]
  217. merged = [choice for s in splits for choice in s]
  218. if len(merged) != len(set(merged)):
  219. msg = "Repeated conditions for {0}: {1}"
  220. raise ASMInstError(msg.format(typ, cond))
  221. return merged
  222. if typ == "register":
  223. if cond == "i":
  224. return ["ix", "iy"]
  225. if cond == "ih":
  226. return ["ixh", "iyh"]
  227. if cond == "il":
  228. return ["ixl", "iyl"]
  229. return [cond]
  230. splits = [split(typ, cond) for typ, cond in zip(types, conds)]
  231. num = max(len(cond) for cond in splits)
  232. if any(1 < len(cond) < num for cond in splits):
  233. msg = "Invalid condition permutations: {0}"
  234. raise ASMInstError(msg.format(conds))
  235. choices = [cond * num if len(cond) == 1 else cond for cond in splits]
  236. return zip(*choices)
  237. def _adapt_return(self, types, conds, ret):
  238. """
  239. Return a modified byte list to accomodate for prefixes and immediates.
  240. """
  241. def handle_reg_func(call):
  242. base, stride = _parse_step_args(call)
  243. index = _rindex(types, "register")
  244. return base + self.REGISTER_OFFSETS[conds[index]] * stride
  245. ret = ret[:]
  246. for i, byte in enumerate(ret):
  247. if not isinstance(byte, int):
  248. if byte == "u8":
  249. try:
  250. index = types.index("immediate")
  251. imm = "INST_IMM({0})".format(index)
  252. except ValueError:
  253. index = types.index("port")
  254. imm = "INST_PORT({0}).port.imm".format(index)
  255. ret[i] = imm + ".uval"
  256. elif byte == "u16":
  257. if i < len(ret) - 1:
  258. raise ASMInstError("U16 return byte must be last")
  259. try:
  260. index = types.index("immediate")
  261. imm = "INST_IMM({0})".format(index)
  262. except ValueError:
  263. indir = types.index("indirect")
  264. if not conds[indir].startswith("imm"):
  265. msg = "Passing non-immediate indirect as immediate"
  266. raise ASMInstError(msg)
  267. imm = "INST_INDIRECT({0}).addr.imm".format(indir)
  268. ret[i] = "INST_IMM_U16_B1({0})".format(imm)
  269. ret.append("INST_IMM_U16_B2({0})".format(imm))
  270. break
  271. elif byte == "rel":
  272. index = types.index("immediate")
  273. ret[i] = "INST_IMM({0}).sval - 2".format(index)
  274. elif _is_call(byte, "bit"):
  275. index = types.index("immediate")
  276. base = _call_args(byte)
  277. if _is_call(base, "reg"):
  278. base = handle_reg_func(base)
  279. ret[i] = "0x{0:02X} + 8 * INST_IMM({1}).uval".format(
  280. _atoi(base), index)
  281. elif _is_call(byte, "rst"):
  282. index = types.index("immediate")
  283. base = _call_args(byte)
  284. ret[i] = "0x{0:02X} + INST_IMM({1}).uval".format(
  285. _atoi(base), index)
  286. elif _is_call(byte, "reg"):
  287. ret[i] = handle_reg_func(byte)
  288. elif _is_call(byte, "cond"):
  289. base, stride = _parse_step_args(byte)
  290. index = types.index("condition")
  291. offset = self.CONDITION_ORDER.index(conds[index])
  292. ret[i] = base + offset * stride
  293. else:
  294. msg = "Unsupported return byte: {0}"
  295. raise ASMInstError(msg.format(byte))
  296. for i, cond in enumerate(conds):
  297. if types[i] == "register" and cond[0] == "i":
  298. prefix = "INST_I{0}_PREFIX".format(cond[1].upper())
  299. if ret[0] != prefix:
  300. ret.insert(0, prefix)
  301. elif types[i] == "indexed":
  302. ret.insert(0, "INST_INDEX_PREFIX({0})".format(i))
  303. ret.insert(2, "INST_INDEX({0}).offset".format(i))
  304. return ret
  305. def _handle_null_case(self, case):
  306. """
  307. Return code to handle an instruction case that takes no arguments.
  308. """
  309. return [
  310. TAB + "if (INST_NARGS == 0) {",
  311. self._handle_return(case["return"], 2),
  312. TAB + "}"
  313. ]
  314. def _handle_pseudo_case(self, pseudo, case):
  315. """
  316. Return code to handle an instruction pseudo-case.
  317. Pseudo-cases are cases that have pseudo-types as arguments. This means
  318. they are expanded to cover multiple "real" argument types.
  319. """
  320. index = case["type"].index(pseudo)
  321. if pseudo == "indirect_hl_or_indexed":
  322. case["type"][index] = "indexed"
  323. indexed = self._handle_case(case)
  324. case["type"][index] = "indirect"
  325. indirect = self._handle_case(case)
  326. base_cond = self._build_case_type_check(case["type"])
  327. hl_reg = TAB * 3 + self._build_indirect_check(index, "reg.hl")
  328. indirect[0] = TAB + "if ({0} &&\n{1}) {{".format(base_cond, hl_reg)
  329. return indirect + indexed
  330. raise ASMInstError("Unknown pseudo-type: {0}".format(pseudo))
  331. def _handle_case(self, case):
  332. """
  333. Return code to handle an instruction case.
  334. """
  335. ctype = case["type"]
  336. if not ctype:
  337. return self._handle_null_case(case)
  338. for pseudo in self.PSEUDO_TYPES:
  339. if pseudo in ctype:
  340. return self._handle_pseudo_case(pseudo, case)
  341. lines = []
  342. cond = self._build_case_type_check(ctype)
  343. lines.append(TAB + "if ({0}) {{".format(cond))
  344. subcases = [(perm, sub["return"]) for sub in case["cases"]
  345. for perm in self._iter_permutations(ctype, sub["if"])]
  346. for cond, ret in subcases:
  347. check = self._build_subcase_check(ctype, cond)
  348. ret = self._adapt_return(ctype, cond, ret)
  349. if check:
  350. lines.append(TAB * 2 + "if ({0})".format(check))
  351. lines.append(self._handle_return(ret, 3))
  352. else:
  353. lines.append(self._handle_return(ret, 2))
  354. break # Unconditional subcase
  355. else:
  356. lines.append(TAB * 2 + "INST_ERROR(ARG_VALUE)")
  357. lines.append(TAB + "}")
  358. return lines
  359. def render(self):
  360. """
  361. Convert data for an individual instruction into a C parse function.
  362. """
  363. lines = []
  364. if self._data["args"]:
  365. lines.append("{tab}INST_TAKES_ARGS(\n{tab2}{0},\n{tab2}{1},"
  366. "\n{tab2}{2}\n{tab})".format(
  367. self._get_arg_parse_mask(0), self._get_arg_parse_mask(1),
  368. self._get_arg_parse_mask(2), tab=TAB, tab2=TAB * 2))
  369. else:
  370. lines.append(TAB + "INST_TAKES_NO_ARGS")
  371. if "return" in self._data:
  372. lines.append(self._handle_return(self._data["return"]))
  373. elif "cases" in self._data:
  374. for case in self._data["cases"]:
  375. lines.extend(self._handle_case(case))
  376. lines.append(TAB + "INST_ERROR(ARG_TYPE)")
  377. else:
  378. msg = "Missing return or case block for {0} instruction"
  379. raise ASMInstError(msg.format(self._name))
  380. contents = "\n".join(lines)
  381. return "INST_FUNC({0})\n{{\n{1}\n}}".format(self._name, contents)
  382. def _build_inst_block(data):
  383. """
  384. Return the instruction parser block, given instruction data.
  385. """
  386. return "\n\n".join(
  387. Instruction(k, v).render() for k, v in sorted(data.items()))
  388. def _build_lookup_block(data):
  389. """
  390. Return the instruction lookup block, given instruction data.
  391. """
  392. macro = TAB + "HANDLE({0})"
  393. return "\n".join(macro.format(inst) for inst in sorted(data.keys()))
  394. def _process(template, data):
  395. """
  396. Return C code generated from a source template and instruction data.
  397. """
  398. inst_block = _build_inst_block(data)
  399. lookup_block = _build_lookup_block(data)
  400. date = time.asctime(time.gmtime())
  401. result = re_date.sub(r"\1{0} UTC".format(date), template)
  402. result = re_inst.sub(r"\1{0}\3".format(inst_block), result)
  403. result = re_lookup.sub(r"\1{0}\3".format(lookup_block), result)
  404. return result
  405. def main():
  406. """
  407. Main script entry point.
  408. """
  409. with open(SOURCE, "r") as fp:
  410. text = fp.read().decode(ENCODING)
  411. with open(DEST, "r") as fp:
  412. template = fp.read().decode(ENCODING)
  413. data = yaml.load(text)
  414. result = _process(template, data)
  415. with open(DEST, "w") as fp:
  416. fp.write(result.encode(ENCODING))
  417. if __name__ == "__main__":
  418. main()