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