remove write_ptp, add bitwidths.txt file
[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 ADDR_WIDTH = 64 # TODO parameterise
17 PADDR_WIDTH = 32 # TODO parameterise
18 DATA_WIDTH = 64 # TODO parameterise
19 # ================ #
20
21 def __init__(self, pth=None, verify=True):
22
23 max_io = 0
24 self.muxed_cells = []
25 self.dedicated_cells = []
26 self.pinnumbers = []
27 self.bankwidths = {}
28
29 fname = 'bankwidths.txt'
30 if pth:
31 fname = os.path.join(pth, fname)
32 with open(fname) as bankwidths:
33 for lineno, line in enumerate(bankwidths):
34 line1 = line[:-1].split('\t')
35 self.bankwidths[line1[0]] = int(line1[1])
36
37 # == capture the number of IO cells required == #
38 fname = 'pinmap.txt'
39 if pth:
40 fname = os.path.join(pth, fname)
41 with open(fname) as pinmapfile:
42 for lineno, line in enumerate(pinmapfile):
43 line1 = line[:-1].split('\t')
44 if len(line1) <= 2:
45 continue
46 self.pinnumbers.append(int(line1[0]))
47 # XXX TODO: dedicated pins in separate file
48 #if len(line1) == 2: # dedicated
49 # self.dedicated_cells.append(line1)
50 #else:
51 for i in range(1, len(line1)):
52 # XXX HORRIBLE HACK!!
53 if line1[i].startswith('pwm'):
54 line1[i] = 'pwm%s_out' % line1[i][4:]
55 self.muxed_cells.append(line1)
56
57 self.pinnumbers = sorted(self.pinnumbers)
58
59 if verify:
60 self.do_checks()
61
62 #self.cell_bitwidth = self.get_cell_bit_widths()
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("Muxer bit width: " + str(self.cell_bitwidth))
68 print("Muxed IOs: " + str(len(self.muxed_cells)))
69 print("Dedicated IOs: " + str(len(self.dedicated_cells)))
70 #sys.exit(0)
71
72 def do_checks(self):
73 """ Multiple checks to see if the user has not screwed up
74 """
75 missing_pins = missing_numbers(self.pinnumbers)
76
77 # Check-1: ensure no pin is present in both muxed and dedicated pins
78 for muxcell in self.muxed_cells:
79 for dedcel in self.dedicated_cells:
80 if dedcel[1] in muxcell:
81 print("ERROR: " + str(dedcel[1]) + " present \
82 in dedicated & muxed lists")
83 exit(1)
84
85 # Check-2: if pin numbering is consistent:
86 if missing_pins:
87 print("ERROR: Following pins have no assignment: " +
88 str(missing_numbers(self.pinnumbers)))
89 exit(1)
90 unique = set(self.pinnumbers)
91 duplicate = False
92 for each in unique:
93 count = self.pinnumbers.count(each)
94 if count > 1:
95 print("ERROR: Multiple assignment for pin: " + str(each))
96 duplicate = True
97 if duplicate:
98 exit(1)
99
100 # Check-3: confirm if N_* matches the instances in the pinmap
101 # ============================================================== #
102
103 # TODO
104
105 def get_cell_bit_widths(self, banks):
106 max_num_cells = 0
107 for cell in self.muxed_cells:
108 print cell
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)