annotate python/hexfile.py @ 237:81752b0f85a5

Added burn led test program
author Windel Bouwman
date Wed, 17 Jul 2013 22:31:54 +0200
parents d3dccf12ca88
children 6259856841a0
rev   line source
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
1 import os
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
2
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
3 class HexFileException(Exception):
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
4 pass
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
5
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
6 def parseHexLine(line):
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
7 """ Parses a hexfile line into three parts """
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
8 line = line[1:] # Remove ':'
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
9 nums = bytes.fromhex(line)
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
10 bytecount = nums[0]
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
11 if len(nums) != bytecount + 5:
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
12 raise HexFileException('byte count field incorrect')
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
13 crc = sum(nums)
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
14 if (crc & 0xFF) != 0:
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
15 raise HexFileException('crc incorrect')
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
16 address = nums[1] * 256 + nums[2]
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
17 typ = nums[3]
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
18 data = nums[4:-1]
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
19 return (address, typ, data)
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
20
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
21
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
22 class HexFile:
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
23 """ Represents an intel hexfile """
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
24 def __init__(self, filename=None):
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
25 self.regions = []
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
26 self.startAddress = 0
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
27
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
28 def load(self, f):
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
29 endOfFile = False
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
30 offset = 0
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
31 startAddress = 0
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
32 curAddress = 0
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
33 curData = bytearray()
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
34 for line in f:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
35 line = line.strip() # Strip spaces and newlines
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
36 if not line: continue # Skip empty lines
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
37 if line[0] != ':': continue # Skip lines that do not start with a ':'
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
38 if endOfFile: raise HexFileException('hexfile line after end of file record')
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
39 address, typ, data = parseHexLine(line)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
40 if typ == 0x0: # Data record
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
41 address += offset # Fix address with offset
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
42 # Append data
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
43 if address == curAddress:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
44 curData += data
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
45 curAddress += len(data)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
46 else:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
47 if curData:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
48 self.regions.append(HexFileRegion(startAddress, bytes(curData)))
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
49 startAddress = address
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
50 curAddress = address + len(data)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
51 curData = bytearray(data)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
52 elif typ == 0x4: # Extended linear address record
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
53 offset = ((data[0] << 8) + data[1]) << 16
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
54 elif typ == 0x1: # End of file record
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
55 if len(data) != 0:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
56 raise HexFileException('end of file record must contain no data')
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
57 endOfFile = True
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
58 elif typ == 0x5: # Start address record (where IP goes after loading)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
59 self.startAddress = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
60 else:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
61 raise HexFileException('record type {0} not implemented'.format(typ))
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
62 print(hex(address), typ, data)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
63 # After all lines:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
64 if curData:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
65 self.regions.append(HexFileRegion(startAddress, bytes(curData)))
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
66
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
67 def __repr__(self):
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
68 i = []
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
69 i.append(super().__repr__())
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
70 i.append('Start address {0}'.format(hex(self.startAddress)))
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
71 for r in self.regions:
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
72 i.append(str(r))
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
73 return os.linesep.join(i)
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
74
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
75 def save(self, f):
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
76 for r in self.regions:
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
77 offset = 0
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
78 while offset < len(r.data):
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
79 f.write('a')
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
80 offset += 16
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
81
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
82
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
83 class HexFileRegion:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
84 def __init__(self, address, data = bytes()):
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
85 self.address = address
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
86 self.data = data
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
87 def __repr__(self):
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
88 return 'Region at 0x{0:X} of {1} bytes'.format(self.address, len(self.data))
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
89
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
90
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
91 if __name__ == '__main__':
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
92 h = HexFile()
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
93 print(h)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
94 """ Test hexfile implementation with some hexfile """
233
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
95 h1 = HexFile()
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
96 with open('audio.hex', 'r') as f:
d3dccf12ca88 Added hexfile tests
Windel Bouwman
parents: 104
diff changeset
97 h1.load(f)
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
98 print(h1)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
99