Browse Source

Add a step() function to reduce repetition in instructions.yml.

master
Ben Kurtovic 9 years ago
parent
commit
c3ab17f18e
3 changed files with 36 additions and 48 deletions
  1. +25
    -7
      scripts/update_asm_instructions.py
  2. +1
    -1
      src/assembler/instructions.inc.c
  3. +10
    -40
      src/assembler/instructions.yml

+ 25
- 7
scripts/update_asm_instructions.py View File

@@ -37,6 +37,15 @@ re_lookup = re.compile(
r"(/\* @AUTOGEN_LOOKUP_BLOCK_START \*/\n*)(.*?)" r"(/\* @AUTOGEN_LOOKUP_BLOCK_START \*/\n*)(.*?)"
r"(\n*/\* @AUTOGEN_LOOKUP_BLOCK_END \*/)", re.S) r"(\n*/\* @AUTOGEN_LOOKUP_BLOCK_END \*/)", re.S)


def _atoi(value):
"""
Try to convert a string to an integer, supporting decimal and hexadecimal.
"""
try:
return int(value)
except ValueError:
return int(value, 16)

class Instruction(object): class Instruction(object):
""" """
Represent a single ASM instruction mnemonic. Represent a single ASM instruction mnemonic.
@@ -56,7 +65,9 @@ class Instruction(object):
def __init__(self, name, data): def __init__(self, name, data):
self._name = name self._name = name
self._data = data self._data = data

self._has_optional_args = False self._has_optional_args = False
self._step_state = {}


def _get_arg_parse_mask(self, num): def _get_arg_parse_mask(self, num):
""" """
@@ -113,16 +124,12 @@ class Instruction(object):
""" """
if "." in cond: if "." in cond:
itype, value = cond.split(".", 1) itype, value = cond.split(".", 1)
try:
value = int(value)
except ValueError:
value = int(value, 16)
vtype = "sval" if itype.upper() in ["S8", "REL"] else "uval" vtype = "sval" if itype.upper() in ["S8", "REL"] else "uval"


test1 = "INST_IMM({0}).mask & IMM_{1}".format(num, itype.upper()) test1 = "INST_IMM({0}).mask & IMM_{1}".format(num, itype.upper())
if (itype.upper() == "U16"): if (itype.upper() == "U16"):
test1 += " && !INST_IMM({0}).is_label".format(num) test1 += " && !INST_IMM({0}).is_label".format(num)
test2 = "INST_IMM({0}).{1} == {2}".format(num, vtype, value)
test2 = "INST_IMM({0}).{1} == {2}".format(num, vtype, _atoi(value))
return "({0} && {1})".format(test1, test2) return "({0} && {1})".format(test1, test2)


return "INST_IMM({0}).mask & IMM_{1}".format(num, cond.upper()) return "INST_IMM({0}).mask & IMM_{1}".format(num, cond.upper())
@@ -253,8 +260,19 @@ class Instruction(object):


elif byte.startswith("bit(") and byte.endswith(")"): elif byte.startswith("bit(") and byte.endswith(")"):
index = types.index("immediate") index = types.index("immediate")
off = byte[4:-1]
ret[i] = "{0} + 8 * INST_IMM({1}).uval".format(off, index)
base = byte[4:-1]
ret[i] = "{0} + 8 * INST_IMM({1}).uval".format(base, index)

elif byte.startswith("step(") and byte.endswith(")"):
arg = byte[5:-1]
if " " in arg:
base, stride = map(_atoi, arg.split(" "))
else:
base, stride = _atoi(arg), 1
if base not in self._step_state:
self._step_state[base] = 0
ret[i] = base + self._step_state[base] * stride
self._step_state[base] += 1


else: else:
msg = "Unsupported return byte: {0}" msg = "Unsupported return byte: {0}"


+ 1
- 1
src/assembler/instructions.inc.c View File

@@ -7,7 +7,7 @@
`make` should trigger a rebuild when it is modified; if not, use: `make` should trigger a rebuild when it is modified; if not, use:
`python scripts/update_asm_instructions.py`. `python scripts/update_asm_instructions.py`.


@AUTOGEN_DATE Thu May 21 20:33:21 2015 UTC
@AUTOGEN_DATE Thu May 21 21:05:17 2015 UTC
*/ */


/* @AUTOGEN_INST_BLOCK_START */ /* @AUTOGEN_INST_BLOCK_START */


+ 10
- 40
src/assembler/instructions.yml View File

@@ -17,26 +17,14 @@ adc:
cases: cases:
- cond: [a, a] - cond: [a, a]
return: [0x8F] return: [0x8F]
- cond: [a, b]
return: [0x88]
- cond: [a, c]
return: [0x89]
- cond: [a, d]
return: [0x8A]
- cond: [a, e]
return: [0x8B]
- cond: [a, b|c|d|e]
return: [step(0x88)]
- cond: [a, h|ih] - cond: [a, h|ih]
return: [0x8C] return: [0x8C]
- cond: [a, l|il] - cond: [a, l|il]
return: [0x8D] return: [0x8D]
- cond: [hl, bc]
return: [0xED, 0x4A]
- cond: [hl, de]
return: [0xED, 0x5A]
- cond: [hl, hl]
return: [0xED, 0x6A]
- cond: [hl, sp]
return: [0xED, 0x7A]
- cond: [hl, bc|de|hl|sp]
return: [0xED, step(0x4A 0x10)]
- type: [register, immediate] - type: [register, immediate]
cases: cases:
- cond: [a, u8] - cond: [a, u8]
@@ -53,14 +41,8 @@ add:
cases: cases:
- cond: [a, a] - cond: [a, a]
return: [0x87] return: [0x87]
- cond: [a, b]
return: [0x80]
- cond: [a, c]
return: [0x81]
- cond: [a, d]
return: [0x82]
- cond: [a, e]
return: [0x83]
- cond: [a, b|c|d|e]
return: [step(0x80)]
- cond: [a, h|ih] - cond: [a, h|ih]
return: [0x84] return: [0x84]
- cond: [a, l|il] - cond: [a, l|il]
@@ -89,14 +71,8 @@ and:
cases: cases:
- cond: [a] - cond: [a]
return: [0xA7] return: [0xA7]
- cond: [b]
return: [0xA0]
- cond: [c]
return: [0xA1]
- cond: [d]
return: [0xA2]
- cond: [e]
return: [0xA3]
- cond: [b|c|d|e]
return: [step(0xA0)]
- cond: [h|ih] - cond: [h|ih]
return: [0xA4] return: [0xA4]
- cond: [l|il] - cond: [l|il]
@@ -411,14 +387,8 @@ jr:
return: [0x18, rel] return: [0x18, rel]
- type: [condition, immediate] - type: [condition, immediate]
cases: cases:
- cond: [nz, rel]
return: [0x20, rel]
- cond: [z, rel]
return: [0x28, rel]
- cond: [nc, rel]
return: [0x30, rel]
- cond: [c, rel]
return: [0x38, rel]
- cond: [nz|z|nc|c, rel]
return: [step(0x20 0x08), rel]


ld: ld:
args: yes args: yes


Loading…
Cancel
Save