diff options
Diffstat (limited to 'pack.py')
| -rw-r--r-- | pack.py | 19 |
1 files changed, 11 insertions, 8 deletions
@@ -4,27 +4,30 @@ import bits def byteify(word): (val, size) = word - if size != 32: - raise ValueError("Expected 32-bit word") + if size % 8 != 0: + raise ValueError("Not byte aligned: {} bits".format(size)) - 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]] + out = [] + for i in range(0, size, 8): + byte = bits.slice(word, i+7, i) + out.append(byte[:1]) + return out def pack(iter): + segment = None 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: + if segment == 'code' and total[1] != 32: raise ValueError("instruction parts do not add up to 32 bit!") yield subv.format_instr(byteify(total)) else: + if line['type'] == 'segment': + segment = line['segment'][0] yield line['raw'] if __name__ == '__main__': |
