move cell_bit_width function
[pinmux.git] / src / bsv / actual_pinmux.py
1 import math
2 from string import digits
3 try:
4 from string import maketrans
5 except ImportError:
6 maketrans = str.maketrans
7
8
9 # ============== common bsv templates ============ #
10 # first argument is the io-cell number being assigned.
11 # second argument is the mux value.
12 # Third argument is the signal from the pinmap file
13 mux_wire = '''
14 rule assign_{2}_on_cell{0}(wrcell{0}_mux=={1});
15 {2}<=cell{0}_mux_in;
16 endrule
17 '''
18 dedicated_wire = '''
19 rule assign_{1}_on_cell{0};
20 {1}<=cell{0}_mux_in;
21 endrule
22 '''
23 # ============================================================
24 digits = maketrans('0123456789', ' ' * 10) # delete space later
25
26
27 def get_cell_bit_width(p):
28 max_num_cells = 0
29 for cell in p.muxed_cells:
30 max_num_cells = max(len(cell) - 1, max_num_cells)
31 return int(math.log(max_num_cells+1, 2))
32
33
34 def cn(idx): # idx is an integer
35 return "cell%s_mux" % str(idx)
36
37
38 def transfn(temp):
39 """ removes the number from the string of signal name.
40 """
41 temp = temp.split('_')
42 if len(temp) == 2:
43 temp[0] = temp[0].translate(digits)
44 temp[0] = temp[0] .replace(' ', '')
45 return '_'.join(temp)
46
47 def fmt(cell):
48 """ blank entries need to output a 0 to the pin (it could just as
49 well be a 1 but we choose 0). reason: blank entries in
50 the pinmap.txt file indicate that there's nothing to choose
51 from. however the user may still set the muxer to that value,
52 and rather than throw an exception we choose to output... zero.
53 """
54 return "%s_io" % cell if cell else '0'
55
56
57 def mkcomment(p, cell):
58 """ returns a comment string for the cell when muxed
59 """
60 return ""
61
62 def init(p, ifaces):
63 """ generates the actual output pinmux for each io-cell. blank lines
64 need to output "0" to the iopad, if there is no entry in
65 that column.
66
67 text is outputted in the format:
68 x_out =
69 muxer_sel==0 ? a :
70 muxer_sel==1 ? b :
71 muxer_sel==2 ? 0 :
72 d
73
74 last line doesn't need selector-logic, obviously.
75 """
76 p.cell_bitwidth = get_cell_bit_width(p)
77 p.pinmux = ' '
78 global dedicated_wire
79 fmtstr = "\t\t\twr%s == %d ? %s :%s\n" # mux-selector format
80 for cell in p.muxed_cells:
81 p.pinmux += " // output muxer for cell idx %s\n" % cell[0]
82 p.pinmux += " %s_out=\n" % cn(cell[0])
83 for i in range(0, len(cell) - 2):
84 comment = mkcomment(p, cell[i + 1])
85 p.pinmux += fmtstr % (cn(cell[0]), i, fmt(cell[i + 1]), comment)
86 comment = mkcomment(p, cell[i + 2])
87 p.pinmux += "\t\t\t" + fmt(cell[i + 2]) + comment # last line
88 p.pinmux += ";\n"
89 # ======================================================== #
90
91 # check each cell if "peripheral input/inout" then assign its wire
92 # Here we check the direction of each signal in the dictionary.
93 # We choose to keep the dictionary within the code and not user-input
94 # since the interfaces are always standard and cannot change from
95 # user-to-user. Plus this also reduces human-error as well :)
96 for i in range(0, len(cell) - 1):
97 cname = cell[i + 1]
98 if not cname: # skip blank entries, no need to test
99 continue
100 temp = transfn(cname)
101 x = ifaces.getifacetype(temp)
102 #print (cname, temp, x)
103 assert x is not None, "ERROR: The signal : " + \
104 str(cname) + \
105 " of pinmap.txt isn't present \nin the current" + \
106 " dictionary. Update dictionary or fix-typo."
107 if x == "input":
108 p.pinmux += \
109 mux_wire.format(cell[0], i, "wr" + cname) + "\n"
110 elif x == "inout":
111 p.pinmux += \
112 mux_wire.format(cell[0], i, "wr" + cname +
113 "_in") + "\n"
114 # ============================================================ #
115
116 # ================== Logic for dedicated pins ========= #
117 for cell in p.dedicated_cells:
118 p.pinmux += " %s_out=%s_io;\n" % (cn(cell[0]), cell[1])
119 temp = cell[1].translate(digits)
120 x = ifaces.getifacetype(temp)
121 if x == "input":
122 pinmux = pinmux + \
123 dedicated_wire.format(cell[0], "wr" + cell[1]) + "\n"
124 elif x == "inout":
125 pinmux = pinmux + \
126 dedicated_wire.format(cell[0], "wr" + cell[1] + "_in") + "\n"
127 # =======================================================#