diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2020-05-26 15:43:10 +0000 |
|---|---|---|
| committer | s-ol <s-ol@users.noreply.github.com> | 2020-05-26 15:43:10 +0000 |
| commit | e3f229cd0471d23707848016c77ea1f849361bde (patch) | |
| tree | 1646637d658dc91bc1cf7092442b55ae8ad56e0c | |
| parent | add notes (diff) | |
| download | subv-e3f229cd0471d23707848016c77ea1f849361bde.tar.gz subv-e3f229cd0471d23707848016c77ea1f849361bde.zip | |
add test.py
| -rw-r--r-- | test.py | 59 |
1 files changed, 59 insertions, 0 deletions
@@ -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") |
