move more modules to src/bsv directory
[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 # dictionary of properties of signals that are supported.
9 dictionary = {
10 "uart_rx" : "input",
11 "uart_tx" : "output",
12 "spi_sclk" : "output",
13 "spi_mosi" : "output",
14 "spi_ss" : "output",
15 "spi_miso" : "input",
16 "twi_sda" : "inout",
17 "twi_scl" : "inout",
18 "sd_clk": "output",
19 "sd_cmd": "output",
20 "sd_d": "inout",
21 "pwm_pwm": "output"
22 }
23
24
25 # ============== common bsv templates ============ #
26 # first argument is the io-cell number being assigned.
27 # second argument is the mux value.
28 # Third argument is the signal from the pinmap file
29 mux_wire = '''
30 rule assign_{2}_on_cell{0}(wrcell{0}_mux=={1});
31 {2}<=cell{0}_mux_in;
32 endrule
33 '''
34 dedicated_wire = '''
35 rule assign_{1}_on_cell{0};
36 {1}<=cell{0}_mux_in;
37 endrule
38 '''
39 # ============================================================
40 digits = maketrans('0123456789', ' ' * 10) # delete space later
41
42
43 def cn(idx):
44 return "cell%s_mux" % str(idx)
45
46
47 def init(p):
48 p.pinmux = ' '
49 global dedicated_wire
50 for cell in p.muxed_cells:
51 p.pinmux += " %s_out=" % cn(cell[0])
52 for i in range(0, len(cell) - 2):
53 p.pinmux += "wr%s" % cn(cell[0]) + \
54 "==" + str(i) + "?" + cell[i + 1] + "_io:\n\t\t\t"
55 p.pinmux += cell[i + 2] + "_io"
56 p.pinmux += ";\n"
57 # ======================================================== #
58
59 # check each cell if "peripheral input/inout" then assign its wire
60 # Here we check the direction of each signal in the dictionary.
61 # We choose to keep the dictionary within the code and not user-input
62 # since the interfaces are always standard and cannot change from
63 # user-to-user. Plus this also reduces human-error as well :)
64 for i in range(0, len(cell) - 1):
65 temp = cell[i + 1].translate(digits)
66 temp = temp.replace(' ', '')
67 x = dictionary.get(temp)
68 assert x is not None, "ERROR: The signal : " + \
69 str(cell[i + 1]) + \
70 " of pinmap.txt isn't present \nin the current" + \
71 " dictionary. Update dictionary or fix-typo."
72 if x == "input":
73 p.pinmux += \
74 mux_wire.format(cell[0], i, "wr" + cell[i + 1]) + "\n"
75 elif x == "inout":
76 p.pinmux += \
77 mux_wire.format(cell[0], i, "wr" + cell[i + 1] +
78 "_in") + "\n"
79 # ============================================================ #
80
81 # ================== Logic for dedicated pins ========= #
82 for cell in p.dedicated_cells:
83 p.pinmux += " %s" % cn(cell[0]) + \
84 "_out=" + cell[1] + "_io;\n"
85 temp = cell[1].translate(digits)
86 x = dictionary.get(temp)
87 if x == "input":
88 pinmux = pinmux + \
89 dedicated_wire.format(cell[0], "wr" + cell[1]) + "\n"
90 elif x == "inout":
91 pinmux = pinmux + \
92 dedicated_wire.format(cell[0], "wr" + cell[1] + "_in") + "\n"
93 # =======================================================#