class restructure parsing
[pinmux.git] / src / parse.py
1 import math
2
3
4 def missing_numbers(num_list):
5 original_list = [x for x in range(num_list[0], num_list[-1] + 1)]
6 num_list = set(num_list)
7 return (list(num_list ^ set(original_list)))
8
9
10 class Parse(object):
11 # == Parameters == #
12 N_MUX = 1 # number of selection lines for the mux per io
13 N_IO = 0
14 N_MUX_IO = 0
15 Addressing = 'WORD'
16 ADDR_WIDTH = 32
17 DATA_WIDTH = 32
18 # ================ #
19
20 # Generating the number of bits for memory map #
21 lower_offset = 0
22 if Addressing == 'BYTE':
23 lower_offset = 0
24 elif Addressing == 'HWORD':
25 lower_offset = 1
26 elif Addressing == 'WORD':
27 lower_offset = 2
28 elif Addressing == 'DWORD':
29 lower_offset = 3
30 else:
31 print('ERROR: Addressing should be one of: BYTE, HWORD, WORD, DWORD')
32 exit(1)
33
34 def __init__(self, verify=True):
35
36 # == capture the number of IO cells required == #
37 pinmapfile = open('pinmap.txt', 'r')
38 max_io = 0
39 self.muxed_cells = []
40 self.dedicated_cells = []
41 self.pinnumbers = []
42
43 for lineno, line in enumerate(pinmapfile):
44 line1 = line.split()
45 if len(line1) <= 1:
46 continue
47 self.pinnumbers.append(int(line1[0]))
48 if len(line1) == 2: # dedicated
49 self.dedicated_cells.append(line1)
50 else:
51 self.muxed_cells.append(line1)
52
53 self.pinnumbers = sorted(self.pinnumbers)
54 self.upper_offset = self.lower_offset + \
55 int(math.log(len(self.muxed_cells), 2))
56
57 if verify:
58 self.do_checks()
59
60 # == user info after parsing ================= #
61 self.N_IO = len(self.dedicated_cells) + len(self.muxed_cells)
62 print("Max number of IO: " + str(self.N_IO))
63 print("Muxed IOs: " + str(len(self.muxed_cells)))
64 print("Dedicated IOs: " + str(len(self.dedicated_cells)))
65
66 def do_checks(self):
67 """ Multiple checks to see if the user has not screwed up
68 """
69 missing_pins = missing_numbers(self.pinnumbers)
70
71 # Check-1: ensure no pin is present in both muxed and dedicated pins
72 for muxcell in self.muxed_cells:
73 for dedcel in self.dedicated_cells:
74 if dedcel[1] in muxcell:
75 print("ERROR: " + str(dedcel[1]) + " present \
76 in dedicated & muxed lists")
77 exit(1)
78
79 # Check-2: if pin numbering is consistent:
80 if missing_pins:
81 print("ERROR: Following pins have no assignment: " +
82 str(missing_numbers(self.pinnumbers)))
83 exit(1)
84 unique = set(self.pinnumbers)
85 duplicate = False
86 for each in unique:
87 count = self.pinnumbers.count(each)
88 if count > 1:
89 print("ERROR: Multiple assignment for pin: " + str(each))
90 duplicate = True
91 if duplicate:
92 exit(1)
93
94 # Check-3: confirm if N_* matches the instances in the pinmap
95 # ============================================================== #
96
97 # TODO
98
99 if __name__ == '__main__':
100 p = Parse()
101 print p.N_IO