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