remove newline from line before splitting with tabs
[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 # == user info after parsing ================= #
65 self.N_IO = len(self.dedicated_cells) + len(self.muxed_cells)
66 print("Max number of IO: " + str(self.N_IO))
67 print("Muxed IOs: " + str(len(self.muxed_cells)))
68 print("Dedicated IOs: " + str(len(self.dedicated_cells)))
69
70 def do_checks(self):
71 """ Multiple checks to see if the user has not screwed up
72 """
73 missing_pins = missing_numbers(self.pinnumbers)
74
75 # Check-1: ensure no pin is present in both muxed and dedicated pins
76 for muxcell in self.muxed_cells:
77 for dedcel in self.dedicated_cells:
78 if dedcel[1] in muxcell:
79 print("ERROR: " + str(dedcel[1]) + " present \
80 in dedicated & muxed lists")
81 exit(1)
82
83 # Check-2: if pin numbering is consistent:
84 if missing_pins:
85 print("ERROR: Following pins have no assignment: " +
86 str(missing_numbers(self.pinnumbers)))
87 exit(1)
88 unique = set(self.pinnumbers)
89 duplicate = False
90 for each in unique:
91 count = self.pinnumbers.count(each)
92 if count > 1:
93 print("ERROR: Multiple assignment for pin: " + str(each))
94 duplicate = True
95 if duplicate:
96 exit(1)
97
98 # Check-3: confirm if N_* matches the instances in the pinmap
99 # ============================================================== #
100
101 # TODO
102
103
104 if __name__ == '__main__':
105 p = Parse()
106 print (p.N_IO)