diff options
Diffstat (limited to 'pack.py')
| -rwxr-xr-x | pack.py | 31 |
1 files changed, 15 insertions, 16 deletions
@@ -44,22 +44,21 @@ import bits def byteify(word): """ split longer bitfield into bytes. - >>> byteify((0x12345678, 32)) + >>> byteify(bits.Bitfield(0x12345678, 32)) [(120,), (86,), (52,), (18,)] - >>> byteify((0x4801813, 32)) + >>> byteify(bits.Bitfield(0x4801813, 32)) [(19,), (24,), (128,), (4,)] - >>> byteify((0x13, 9)), + >>> byteify(bits.Bitfield(0x13, 9)), Traceback (most recent call last): ... - AssertionError: not byte aligned: 9 bits + AssertionError: not byte aligned: 0x13'9 """ - (val, size) = word - assert size % 8 == 0, "not byte aligned: {} bits".format(size) + assert word.size % 8 == 0, "not byte aligned: {}".format(word) out = [] - for i in range(0, size, 8): - byte = bits.slice(word, i+7, i) - out.append(byte[:1]) + for i in range(0, word.size, 8): + byte = word[i+7:i] + out.append((byte.val,)) return out @subv.with_parsed_lines @@ -79,27 +78,27 @@ def pack(iter): last_segment = None for segment, line in iter: if last_segment != segment: - assert buf[1] == 0, "segment '{}' end isn't byte-aligned".format(segment) + assert buf.size == 0, "segment '{}' end isn't byte-aligned".format(segment) if out: yield subv.format(flush_bytes()) if line['type'] == 'instr': for part in line['instr']: - buf = bits.concat(buf, bits.from_part(part)) + buf = bits.from_part(part) & buf else: yield line['raw'] - while buf[1] >= 8: - byte = bits.slice(buf, 7, 0) - buf = bits.slice(buf, buf[1] - 1, 8, allow_empty=True) - out.append(byte[:1]) + while buf.size >= 8: + byte = buf[7:0] + buf = buf.slice_allowempty(slice(buf.size-1, 8)) + out.append((byte.val,)) if len(out) == 4: yield subv.format(flush_bytes()) last_segment = segment - assert buf[1] == 0, "segment '{}' end isn't byte-aligned".format(segment) + assert buf.size == 0, "segment '{}' end isn't byte-aligned".format(segment) if __name__ == '__main__': import sys |
