-
Notifications
You must be signed in to change notification settings - Fork 1
/
tbgenerator.py
212 lines (179 loc) · 6.09 KB
/
tbgenerator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
'''
Funktioniert zur Zeit nur für Module ohne Parameter
'''
def readFile(filename):
f = open(filename, "r")
content = f.read()
f.close()
return content
def writeFile(filename, content):
f = open(filename, "w")
f.write(content)
f.close()
return
def removeComments(text): # Removes Comments (/**/ and //) in and before the portlist and whitespace at the beginning
result = ""
insideMultilineComment = False
insideOneLineComment = False
for i in range (0, len(text)):
charPair = text[i:i+2]
lastCharPair = text[i-1:i+1]
if (insideMultilineComment):
result += '' #"\n INSIDE MULTILINE COMMENT \n"
if (insideOneLineComment):
result += '' #"\n INSIDE ONELINE COMMENT \n"
if (charPair == "/*"):
insideMultilineComment = True
if (charPair == "//"):
insideOneLineComment = True
if ((not insideMultilineComment) and (not insideOneLineComment)):
result += text[i]
if (lastCharPair == "*/"):
insideMultilineComment = False
if (text[i] == '\n'):
insideOneLineComment = False
if (text[i] == ";"): # we only need the part up to the portlist
break
# now remove whitespace at beginning
start = result.find('m') # find 'module'
result = result[start:]
return result
def removeLengthSpecifiers(input):
output = []
stop = False
temp = ""
for element in input:
for char in element:
if (char == '['):
stop = True
if (stop == False and char != ' '):
temp += char
if (char == ']'):
stop = False
output.append(temp)
temp = ""
return output
def getInputsAndOutputs (portlist):
portlist = portlist + ','
inputs = [] # array of strings
outputs = [] # array of strings
temp = ""
inputTag = True # boolean True = input False = output
for char in portlist:
if (temp == "inputwire"):
inputTag = True
temp = ""
if (temp == "outputreg"):
inputTag = False
temp = ""
if (char == ','):
if (inputTag == True):
inputs.append(temp)
temp = ""
if (inputTag == False):
outputs.append(temp)
temp = ""
if ((char != ' ') and (char != '\n') and (char != '\t') and (char != ',')):
temp = temp + char
if (char == ']'):
temp = temp + " "
return [inputs, outputs]
def createInstantiation(modulename, portlist):
inputs = getInputsAndOutputs(portlist)[0]
outputs = getInputsAndOutputs(portlist)[1]
result = ""
# Remove lenght specifiers (z.B.: [63:0])
inputs = removeLengthSpecifiers(inputs)
outputs = removeLengthSpecifiers(outputs)
# Instantiate module
result += "\n" + modulename + " " + modulename + "_I (\n"
for input in inputs:
result += "." + input + "(" + input + "),\n"
for output in outputs:
result += "." + output + "(" + output + "),\n"
result = result[:-2]
result += ");"
return result
def getModuleName(file): # call removeComments first
temp = ""
for char in file:
if (temp == "module "):
temp = ""
if (char == ' ' and temp != "module"):
return temp
temp += char
def getPortlist (file):
portlist = ""
stop = True
for char in file:
if (char == ")"):
stop = True
if (stop == False):
portlist += char
if (char == "("):
stop = False
if (char == ";"):
break
return portlist
def createInitialiseTask(inputs):
inputs = removeLengthSpecifiers(inputs)
result = ""
result += "task initialise; \n"
result += " begin\n"
for input in inputs:
if (input == "clk"):
continue
if (input[-2:] == "_n"):
result += " " + input + " <= 1;\n"
else:
result += " " + input + " <= 0;\n"
result += " #PERIOD\n"
result += " reset;\n"
result += " end\n"
result += "endtask\n \n"
return result
def createResetTask():
result = "task reset;\n"
result += " begin\n"
result += " @(negedge clk) res_n <= 0;\n"
result += " #PERIOD\n"
result += " @(negedge clk) res_n <= 1;\n"
result += " end\n"
result += "endtask\n \n"
return result
def createClockInstance():
result = "parameter PERIOD = 20;\n"
result += "clock #(.PERIOD(PERIOD)) clk_I (clk);\n"
return result
def createTb (filename):
file = readFile(filename)
file = removeComments(file)
modulename = getModuleName(file)
portlist = getPortlist(file)
inputs = getInputsAndOutputs(portlist)[0]
outputs = getInputsAndOutputs(portlist)[1]
result = ""
result += "module " + modulename + "_tb;\n \n"
# Declare inputs and outputs
for input in inputs:
result += "reg " + input + "; \n"
for output in outputs:
result += "wire " + output + "; \n"
# Instantiate clock and module
result += createClockInstance()
result += createInstantiation(modulename, portlist)
# Create tasks
result += "\n"
result += createInitialiseTask(inputs)
result += createResetTask()
result += "\n" + "endmodule"
return result
print("Hi!\nThis program can take your SystemVerilog module and create a skeleton for your testbench.\n")
print("Please note that this program currently does not support modules with parameters or any types other than wire and reg. Also note that all inputs must be wires and all outputs must be regs\n")
print("Please enter the name of the .sv sourcefile (if your file is called counter.sv, please enter counter)")
inp = input()
filename = inp + ".sv"
tbfilename = inp + "_tb.sv"
print("Reading " + filename + " and creating testbench...")
writeFile(tbfilename, (createTb(filename)))
print("Testbench skeleton saved as " + tbfilename)