Skip to content

Commit

Permalink
Merge pull request #7 from danarcana/master
Browse files Browse the repository at this point in the history
Fix_for_padding_when_word_size_bytes_is_2
  • Loading branch information
eerimoq committed May 27, 2017
2 parents c8ec5ee + 19d2be8 commit b99c765
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
13 changes: 9 additions & 4 deletions bincopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,13 +585,15 @@ def as_ihex(self, number_of_data_bytes=32, address_length_bits=32):

return '\n'.join(data_address + footer) + '\n'

def as_binary(self, minimum_address=None, padding=b'\xff'):
def as_binary(self, minimum_address=None, padding=None):
"""Return a byte string of all data.
:param minimum_address: Start address of the resulting binary data. Must
be less than or equal to the start address of
the binary data.
:param padding: Value of the padding between not adjecent segments.
:param padding: Word value of the padding between non-adjecent segments.
Give as a bytes object of length 1 when the word size is 8 bits, length 2 when
the word size is 16 bits, and so on.
:returns: A byte string of the binary data.
"""
Expand All @@ -602,6 +604,9 @@ def as_binary(self, minimum_address=None, padding=b'\xff'):
res = b''
current_maximum_address = self.get_minimum_address()

if padding is None:
padding = b'\xff' * self.word_size_bytes

if minimum_address is not None:
if minimum_address > self.get_minimum_address():
raise Error('the selected start address must be lower or '
Expand All @@ -613,11 +618,11 @@ def as_binary(self, minimum_address=None, padding=b'\xff'):
address //= self.word_size_bytes
res += padding * (address - current_maximum_address)
res += data
current_maximum_address = address + len(data)
current_maximum_address = address + (len(data) // self.word_size_bytes)

return res

def as_array(self, minimum_address=None, padding=b'\xff', separator=', '):
def as_array(self, minimum_address=None, padding=None, separator=', '):
"""Format the binary file as a string values separated by given
separator. This function can be used to generate array
initialization code for c and other languages.
Expand Down
9 changes: 9 additions & 0 deletions tests/files/in_16bits_word_padding.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
:02000004003DBD
:06C000008F3DC0000006A8
:20C00400152473C08D1074C01518744033807780881474C0151A74408D1274C015147440F1
:20C014008D1474C0151C73C08D1674C01500744033807780887274C01506744033807780E2
:20C02400887674C0151E744033807780886074C01522744033807780888074C0150874407B
:20C0340033807780886474C01504744033807780885874C01520744033807780881674C032
:20C07400883474C000007840152473D000047F2000007FA000007FA000007FA00036798158
:20C0840000007FA000007FA000007FA03E937840887273D0CD3A7880883401103F00778572
:00000001FF
Binary file added tests/files/out_16bits_word_padding.bin
Binary file not shown.
Binary file added tests/files/out_16bits_word_padding_0xff00.bin
Binary file not shown.
14 changes: 14 additions & 0 deletions tests/test_bincopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,20 @@ def test_word_size(self):
with open('tests/files/out_16bits_word.s19') as fin:
self.assertEqual(binfile.as_srec(30, 24), fin.read())

def test_word_size_default_padding(self):
binfile = bincopy.BinFile(word_size_bits=16)
with open('tests/files/in_16bits_word_padding.hex', 'r') as fin:
binfile.add_ihex(fin.read())
with open('tests/files/out_16bits_word_padding.bin', 'rb') as fin:
self.assertEqual(binfile.as_binary(), fin.read())

def test_word_size_custom_padding(self):
binfile = bincopy.BinFile(word_size_bits=16)
with open('tests/files/in_16bits_word_padding.hex', 'r') as fin:
binfile.add_ihex(fin.read())
with open('tests/files/out_16bits_word_padding_0xff00.bin', 'rb') as fin:
self.assertEqual(binfile.as_binary(padding=b'\xff\x00'), fin.read())

def test_print(self):
binfile = bincopy.BinFile()
with open('tests/files/in.s19', 'r') as fin:
Expand Down

0 comments on commit b99c765

Please sign in to comment.