put back to RV64
[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 # TODO parameterise
18 DATA_WIDTH = 64 # TODO parameterise
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 for i in range(1, len(line1)):
56 # XXX HORRIBLE HACK!!
57 if line1[i].startswith('pwm'):
58 line1[i] = 'pwm%s_out' % line1[i][4:]
59 self.muxed_cells.append(line1)
60
61 self.pinnumbers = sorted(self.pinnumbers)
62 self.upper_offset = self.lower_offset + \
63 int(math.log(len(self.muxed_cells), 2))
64
65 if verify:
66 self.do_checks()
67
68 self.cell_bitwidth = self.get_cell_bit_width()
69
70 # == user info after parsing ================= #
71 self.N_IO = len(self.dedicated_cells) + len(self.muxed_cells)
72 print("Max number of IO: " + str(self.N_IO))
73 print("Muxer bit width: " + str(self.cell_bitwidth))
74 print("Muxed IOs: " + str(len(self.muxed_cells)))
75 print("Dedicated IOs: " + str(len(self.dedicated_cells)))
76
77 def do_checks(self):
78 """ Multiple checks to see if the user has not screwed up
79 """
80 missing_pins = missing_numbers(self.pinnumbers)
81
82 # Check-1: ensure no pin is present in both muxed and dedicated pins
83 for muxcell in self.muxed_cells:
84 for dedcel in self.dedicated_cells:
85 if dedcel[1] in muxcell:
86 print("ERROR: " + str(dedcel[1]) + " present \
87 in dedicated & muxed lists")
88 exit(1)
89
90 # Check-2: if pin numbering is consistent:
91 if missing_pins:
92 print("ERROR: Following pins have no assignment: " +
93 str(missing_numbers(self.pinnumbers)))
94 exit(1)
95 unique = set(self.pinnumbers)
96 duplicate = False
97 for each in unique:
98 count = self.pinnumbers.count(each)
99 if count > 1:
100 print("ERROR: Multiple assignment for pin: " + str(each))
101 duplicate = True
102 if duplicate:
103 exit(1)
104
105 # Check-3: confirm if N_* matches the instances in the pinmap
106 # ============================================================== #
107
108 # TODO
109
110 def get_cell_bit_width(self):
111 max_num_cells = 0
112 for cell in self.muxed_cells:
113 max_num_cells = max(len(cell) - 1, max_num_cells)
114 return int(math.log(max_num_cells + 1, 2))
115
116
117 if __name__ == '__main__':
118 p = Parse()
119 print (p.N_IO)