aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2020-05-26 15:43:10 +0000
committers-ol <s-ol@users.noreply.github.com>2020-05-26 15:43:10 +0000
commite3f229cd0471d23707848016c77ea1f849361bde (patch)
tree1646637d658dc91bc1cf7092442b55ae8ad56e0c
parentadd notes (diff)
downloadsubv-e3f229cd0471d23707848016c77ea1f849361bde.tar.gz
subv-e3f229cd0471d23707848016c77ea1f849361bde.zip
add test.py
-rw-r--r--test.py59
1 files changed, 59 insertions, 0 deletions
diff --git a/test.py b/test.py
new file mode 100644
index 0000000..525e9ca
--- /dev/null
+++ b/test.py
@@ -0,0 +1,59 @@
+#!/usr/bin/env python3
+
+import re
+white = re.compile('[ \t\.\n]+')
+hex = re.compile('^(0x)?[0-9a-f]+$')
+
+def format_r(op, rd, r1, r2, funct3, funct7):
+ # opcode[7] rd[5] funct3[3] rs1[5] rs2[5] funct7[7]
+ b0 = (rd & 0x1) << 7 | op
+ b1 = (r1 & 0x1) << 7 | funct3 << 4 | rd >> 1
+ b2 = (r2 & 0xf) << 4 | r1 >> 1
+ b3 = funct7 << 1 | r2 >> 4
+ return [(b3,), (b2,), (b1,), (b0,)]
+
+def parse_part(part):
+ part = part.split('/')
+ if hex.match(part[0]):
+ part[0] = int(part[0], 16)
+ return tuple(part)
+
+def parse_instr(line):
+ parts = white.split(line)
+ parts = [parse_part(part) for part in parts if part != '']
+ return parts
+
+def translate_instr(orig):
+ return orig
+
+def format_part(part):
+ if not isinstance(part[0], str):
+ part = ('0x{:02x}'.format(part[0]),) + part[1:]
+ return '/'.join(part)
+
+def format_instr(inst, comment=None):
+ packed = ' '.join(format_part(part) for part in inst)
+ if comment:
+ packed = packed + ' # ' + comment
+ return packed
+
+if __name__ == "__main__":
+ import sys
+
+ seg = 'code'
+ for line in sys.stdin:
+ line = line.rstrip()
+ if line.startswith('=='): # segment
+ print(line)
+ seg = line.split(' ')[1]
+ elif line.endswith(':'): # label
+ print(line)
+ elif seg != 'code':
+ print(line)
+ else:
+ parts = parse_instr(line)
+ risc = translate_instr(parts)
+ print(format_instr(risc, line))
+
+ # emit_instr(format_r(0x33, 0x5, 0x6, 0x7, 0, 0))
+ # emit_instr(format_r(0x33, 0x5, 0x6, 0x7, 0, 0), "add eax, 2")