OSDN Git Service

Logger/Logger.h: __linux__ changed to __unix__ to support build on cygwin
[stock/stock.osdn.git] / v2h / load_v.py
1 # load data from file in .v format
2
3 class Section:
4         start = 0 # start address
5         data = [] # data (array of %02x str)
6         def __init__(self, start):
7                 self.start = start
8                 self.data = []
9         def len(self):
10                 return len(self.data)
11
12 def v_file_load(fin):
13         sections = []
14         sec = None
15         for line in fin:
16            line = line.strip()
17            if len(line) == 0:
18               continue
19            if line[0] == '@': # address marker - start of new data chunk
20               start = int(line[1:], 0x10)
21               if sec == None:
22                  sec = Section(start)
23               else:
24                  # check for adjacent sections
25                  if start != sec.start + sec.len():
26                     sections.append(sec)
27                     sec = Section(start)
28                  # else: continue with the same section
29               continue
30            # data record
31            data = line.split()
32            if len(data) < 1:
33               print 'error:', line
34               sys.exit(1)
35            # data sanity check
36            for b in data:
37               v = int(b, 0x10)
38               if v > 0xff:
39                  print 'error:', line
40                  sys.exit(1)
41            sec.data += data
42 #           print data
43 #           print sections[-1].data
44         if sec != None:
45            sections.append(sec)
46         return sections