diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2020-05-28 16:28:07 +0000 |
|---|---|---|
| committer | s-ol <s-ol@users.noreply.github.com> | 2020-05-28 16:28:07 +0000 |
| commit | d298144e6a5d6a0c52338100b73eadf4b33c5f09 (patch) | |
| tree | 8b595d54677bfdcf58f0ec606f25c154712d6d71 /pack.py | |
| parent | Add ex.* (diff) | |
| download | subv-d298144e6a5d6a0c52338100b73eadf4b33c5f09.tar.gz subv-d298144e6a5d6a0c52338100b73eadf4b33c5f09.zip | |
cleanup
Diffstat (limited to 'pack.py')
| -rw-r--r-- | pack.py | 76 |
1 files changed, 47 insertions, 29 deletions
@@ -1,25 +1,31 @@ #!/usr/bin/env python3 -from riscv import byteify, bit_concat -from subx import clean, classify, parse_bits, format_instr +import subv +import bits -def pack(iter): - for line in iter: - line = clean(line) - if line == '': - continue +def byteify(word): + (val, size) = word + if size != 32: + raise ValueError("Expected 32-bit word") - type = classify(line) + b0 = bits.slice(word, 7, 0) + b1 = bits.slice(word, 15, 8) + b2 = bits.slice(word, 23, 16) + b3 = bits.slice(word, 31, 24) + return [b0[:1], b1[:1], b2[:1], b3[:1]] - if type == 'segment' or type == 'label': - yield line - else: - fields = parse_bits(line) - total = bit_concat(*fields) +def pack(iter): + for line in iter: + line = subv.parse(line) + if line['type'] == 'instr': + fields = [bits.from_part(p) for p in line['instr']] + total = bits.concat(*fields) if total[1] != 32: raise ValueError("instruction parts do not add up to 32 bit!") - yield format_instr(byteify(total)) + yield subv.format_instr(byteify(total)) + else: + yield line['raw'] if __name__ == '__main__': import sys @@ -27,29 +33,41 @@ if __name__ == '__main__': print(line) import unittest -class TestE2E(unittest.TestCase): +class TestPack(unittest.TestCase): + def test_byteify(self): + self.assertEqual( + byteify((0x12345678, 32)), + [(0x78,), (0x56,), (0x34,), (0x12,)] + ) + self.assertEqual( + byteify((0x4801813, 32)), + [(0x13,), (0x18,), (0x80,), (0x04,)] + ) + def test_e2e(self): from io import StringIO from textwrap import dedent inp = dedent('''\ + # do some things == code 0x80000000 - 37/7 05/5 10010/20 - 13/7 06/5 00/3 00/5 48/12 - 23/7 00/5 02/3 05/5 06/5 00/7 - 13/7 06/5 00/3 00/5 65/12 - 23/7 00/5 02/3 05/5 06/5 00/7 - 13/7 06/5 00/3 00/5 6c/12 - 23/7 00/5 02/3 05/5 06/5 00/7 - 13/7 06/5 00/3 00/5 6c/12 - 23/7 00/5 02/3 05/5 06/5 00/7 - 13/7 06/5 00/3 00/5 6f/12 - 23/7 00/5 02/3 05/5 06/5 00/7 - 13/7 06/5 00/3 00/5 0a/12 - 23/7 00/5 02/3 05/5 06/5 00/7 - 6f/7 00/5 ff/8 01/1 3e6/10 1/1 + 37/7 05/5 10010/20 + 13/7 06/5 00/3 00/5 48/12 + 23/7 00/5 02/3 05/5 06/5 00/7 + 13/7 06/5 00/3 00/5 65/12 + 23/7 00/5 02/3 05/5 06/5 00/7 + 13/7 06/5 00/3 00/5 6c/12 + 23/7 00/5 02/3 05/5 06/5 00/7 + 13/7 06/5 00/3 00/5 6c/12 + 23/7 00/5 02/3 05/5 06/5 00/7 + 13/7 06/5 00/3 00/5 6f/12 + 23/7 00/5 02/3 05/5 06/5 00/7 + 13/7 06/5 00/3 00/5 0a/12 + 23/7 00/5 02/3 05/5 06/5 00/7 + 6f/7 00/5 ff/8 01/1 3e6/10 1/1 ''') out = dedent('''\ + # do some things == code 0x80000000 b7 02 01 10 13 03 80 04 |
