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