Skip to content

Commit

Permalink
Implement issue #2: Missing option for byte width
Browse files Browse the repository at this point in the history
1. Added word_size argument to File() constructor to select word size in bits. It is only used on the address field in records.
  • Loading branch information
eerimoq committed May 22, 2015
1 parent a9851cb commit a395102
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
19 changes: 15 additions & 4 deletions bincopy.py
Expand Up @@ -285,7 +285,11 @@ def __str__(self):

class File(object):

def __init__(self):
def __init__(self, word_size=8):
if (word_size % 8) != 0:
raise ValueError('Word size must be a multiple of 8 bits.')
self.word_size = word_size
self.word_size_bytes = (word_size // 8)
self.header = None
self.execution_start_address = None
self.segments = _Segments()
Expand All @@ -299,6 +303,7 @@ def add_srec(self, iostream):
if type == '0':
self.header = data
elif type in '123':
address *= self.word_size_bytes
self.segments.add(_Segment(address, address + size, [data]))
elif type in '789':
self.execution_start_address = address
Expand All @@ -315,6 +320,7 @@ def add_ihex(self, iostream):
address = (address
+ extended_segment_address
+ extended_linear_address)
address *= self.word_size_bytes
self.segments.add(_Segment(address, address + size, [data]))
elif type == 1:
pass
Expand Down Expand Up @@ -345,7 +351,10 @@ def as_srec(self, size=32, address_length=32):
if self.header:
header.append(pack_srec('0', 0, len(self.header), self.header))
type = str((address_length // 8) - 1)
data = [pack_srec(type, address, len(data), data)
data = [pack_srec(type,
address // self.word_size_bytes,
len(data),
data)
for address, data in self.segments.iter(size)]
footer = [pack_srec('5', len(data), 0, None)]
if ((self.execution_start_address is not None)
Expand Down Expand Up @@ -374,6 +383,7 @@ def as_ihex(self, size=32, address_length=32):
data_address = []
extended_address = -1
for address, data in self.segments.iter(size):
address //= self.word_size_bytes
if address_length == 32:
if ((address >> 16) & 0xffff) > extended_address:
extended_address = ((address >> 16) & 0xffff)
Expand Down Expand Up @@ -412,6 +422,7 @@ def as_binary(self, begin=None, padding=b'\xff'):
raise ValueError(fmt.format(begin, end))
end = begin
for address, data in self.segments.iter():
address //= self.word_size_bytes
res.extend(bytearray(padding * (address - end)))
res.extend(data)
end = address + len(data)
Expand Down Expand Up @@ -439,13 +450,13 @@ def get_minimum_address(self):
'''
Get minimum address.
'''
return self.segments.get_minimum_address()
return (self.segments.get_minimum_address() // self.word_size_bytes)

def get_maximum_address(self):
'''
Get maximum address.
'''
return self.segments.get_maximum_address()
return (self.segments.get_maximum_address() // self.word_size_bytes)

def info(self, type, filename):
'''
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -3,7 +3,7 @@
from setuptools import setup

setup(name='bincopy',
version='1.0.1',
version='1.1.0',
description=('Mangling of various file formats that conveys '
'binary information (Motorola S-Record, '
'Intel HEX and binary files).'),
Expand Down
4 changes: 4 additions & 0 deletions tests/files/in_16bits_word.s19
@@ -0,0 +1,4 @@
S00600004844521B
S2083E80000123ABCD9D
S2223E8002761B0005ABBDA8BDA0BDC2BDC3BDFE04FF6929425616761F002C0E01761FB8
S2223E80110227FF3D1E00761F002C0E02761F0227FF3D1E10761F002C0E03761F022702
5 changes: 5 additions & 0 deletions tests/files/out_16bits_word.s19
@@ -0,0 +1,5 @@
S00600004844521B
S2223E80000123ABCD761B0005ABBDA8BDA0BDC2BDC3BDFE04FF6929425616761F002CC2
S2223E800F0E01761F0227FF3D1E00761F002C0E02761F0227FF3D1E10761F002C0E031E
S2083E801E761F02275D
S5030003F9
8 changes: 8 additions & 0 deletions tests/test_bincopy.py
Expand Up @@ -176,5 +176,13 @@ def test_ihex_crc(self):
self.assertEqual(bincopy.crc_ihex('0300300002337a'), 0x1e)
self.assertEqual(bincopy.crc_ihex('00000000'), 0)

def test_word_size(self):
f = bincopy.File(word_size=16)
with open('tests/files/in_16bits_word.s19', 'r') as fin:
f.add_srec(fin)
with open('tests/files/out_16bits_word.s19') as fin:
self.assertEqual(f.as_srec(30, 24), fin.read())


if __name__ == '__main__':
unittest.main()

0 comments on commit a395102

Please sign in to comment.