code clean using pep8 and autopep8.
[pinmux.git] / src / actual_pinmux.py
1 from params import *
2 from string import digits
3
4
5 # dictionary of properties of signals that are supported.
6 dictionary = {
7 "uart_rx" : "input",
8 "uart_tx" : "output",
9 "spi_sclk" : "output",
10 "spi_mosi" : "output",
11 "spi_ss" : "output",
12 "spi_miso" : "input",
13 "twi_sda" : "inout",
14 "twi_scl" : "inout"
15 }
16
17
18 # ============== common bsv templates ============ #
19 assign_cell = '''cell{0}_out=wrmux{0}=={1}?'''
20 # first argument is the io-cell number being assigned.
21 # second argument is the mux value.
22 # Third argument is the signal from the pinmap file
23 input_wire = '''
24 rule assign_{2}_on_cell{0}(wrmux{0}=={1});
25 {2}<=cell{0}_in;
26 endrule
27 '''
28 # ============================================================
29 pinmux = ''' '''
30 pinmap_file = open("./pinmap.txt", "r")
31 dedicated = False
32 for lineno, line in enumerate(pinmap_file):
33 line1 = line.split()
34 if("muxed" in line):
35 dedicated = False
36 elif("dedicated" in line):
37 dedicated = True
38 # ==== Logic for muxed pins ==== #
39 if(len(line1) > 1 and not(dedicated)):
40 if(lineno > N_IO):
41 print(
42 "ERROR: Parameter N_IO(" +
43 str(N_IO) +
44 ") is less than the pin number in line: " +
45 str(lineno) +
46 " of pinmap.txt")
47 exit(1)
48 # ==== Mux each generic IO cell with the mapping ===== #
49 # provided in the pinmap file
50 pinmux = pinmux + " cell" + str(line1[0]) + "_out="
51 i = 0
52 while(i < len(line1) - 1):
53 pinmux = pinmux + "wrmux" + \
54 str(line1[0]) + "==" + str(i) + "?" + line1[i + 1] + "_io:"
55 if(i + 2 == len(line1) - 1):
56 pinmux = pinmux + line1[i + 2] + "_io"
57 i = i + 2
58 else:
59 i = i + 1
60 pinmux = pinmux + ";\n"
61 # ======================================================== #
62
63 # check each cell if "peripheral input/inout" then assign its wire
64 # Here we check the direction of each signal in the dictionary.
65 # We choose to keep the dictionary within the code and not user-input
66 # since the interfaces are always standard and cannot change from
67 # user-to-user. Plus this also reduces human-error as well :)
68 for i in range(0, len(line1) - 1):
69 digits = str.maketrans(dict.fromkeys('0123456789'))
70 temp = line1[i + 1].translate(digits)
71 x = dictionary.get(temp)
72 if(x is None):
73 print(
74 "Error: The signal : " +
75 str(line1[i + 1]) +
76 " in lineno: " +
77 str(lineno) + "of pinmap.txt isn't present in the \
78 current dictionary.\nUpdate dictionary or fix-typo.")
79 exit(1)
80 if(x == "input"):
81 pinmux = pinmux + \
82 input_wire.format(line1[0], i, "wr" + line1[i + 1]) + "\n"
83 elif(x == "inout"):
84 pinmux = pinmux + \
85 input_wire.format(line1[0], i, "wr" + line1[i + 1] +
86 "_in") + "\n"
87 # ============================================================ #
88
89 # ================== Logic for dedicated pins ========= #
90 elif(len(line1) > 1 and dedicated):
91 pinmux = pinmux + " cell" + \
92 str(line1[0]) + "_out=" + line1[1] + "_io;\n"
93 # ======================================================= #
94 # =========================================================