forked from FPGA-Research/FABulous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_generation_Verilog.py
203 lines (168 loc) · 6.89 KB
/
code_generation_Verilog.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
from typing import Literal
import math
import re
from fabric_generator.fabric import Tile, Bel, ConfigBitMode, IO
from fabric_generator.code_generator import codeGenerator
class VerilogWriter(codeGenerator):
"""
The writer class for generating Verilog code.
"""
def addComment(self, comment, onNewLine=False, end="", indentLevel=0) -> None:
if onNewLine:
self._add("")
if self._content:
self._content[-1] += f"{' ':<{indentLevel*4}}" + \
f"//{comment}"f"{end}"
else:
self._add(f"{' ':<{indentLevel*4}}" +
f"// {comment}"f"{end}")
def addHeader(self, name, package='', indentLevel=0):
self._add(f"module {name}", indentLevel)
def addHeaderEnd(self, name, indentLevel=0):
pass
def addParameterStart(self, indentLevel=0):
self._add(f"#(", indentLevel)
def addParameterEnd(self, indentLevel=0):
temp = self._content.pop()
if "//" in temp:
temp2 = self._content.pop()[:-1]
self._add(temp2)
self._add(temp)
else:
self._add(temp[:-1])
self._add(")", indentLevel)
def addParameter(self, name, type, value, indentLevel=0):
if type.startswith("["):
self._add(f"parameter {type} {name}={value},", indentLevel)
else:
self._add(f"parameter {name}={value},", indentLevel)
def addPortStart(self, indentLevel=0):
self._add(f"(", indentLevel)
def addPortEnd(self, indentLevel=0):
def deComma(x):
cpos = x.rfind(',')
assert cpos != -1, x
return x[:cpos] + x[cpos+1:]
temp = self._content.pop()
if "//" in temp and "," not in temp:
temp2 = deComma(self._content.pop())
self._add(temp2)
self._add(temp)
else:
self._add(deComma(temp))
self._add(");", indentLevel)
def addPortScalar(self, name, io: IO, indentLevel=0):
ioString = io.value.lower()
self._add(f"{ioString} {name},", indentLevel)
def addPortVector(self, name, io: IO, msbIndex, indentLevel=0):
ioString = io.value.lower()
self._add(f"{ioString} [{msbIndex}:0] {name},", indentLevel)
def addDesignDescriptionStart(self, name, indentLevel=0):
pass
def addDesignDescriptionEnd(self, indentLevel=0):
self._add("endmodule", indentLevel)
def addConstant(self, name, value, indentLevel=0):
self._add(f"parameter {name} = {value};", indentLevel)
def addConnectionScalar(self, name, indentLevel=0):
self._add(f"wire {name};", indentLevel)
def addConnectionVector(self, name, startIndex, endIndex=0, indentLevel=0):
self._add(
f"wire[{startIndex}:{endIndex}] {name};", indentLevel)
def addLogicStart(self, indentLevel=0):
pass
def addLogicEnd(self, indentLevel=0):
pass
def addInstantiation(self, compName, compInsName, portsPairs, paramPairs=[], emulateParamPairs=[], indentLevel=0):
if paramPairs:
port = [f".{i[0]}({i[1]})" for i in paramPairs]
self._add(
f"{compName}", indentLevel=indentLevel)
self._add("#(", indentLevel=indentLevel+1)
self._add(
(",\n"f"{' ':<{4*(indentLevel + 1)}}").join(port), indentLevel=indentLevel + 1)
self._add(")", indentLevel=indentLevel+1)
self._add(f"{compInsName}", indentLevel=indentLevel+1)
self._add("(", indentLevel=indentLevel+1)
elif emulateParamPairs:
port = [f".{i[0]}({i[1]})" for i in emulateParamPairs]
self._add(
f"{compName}", indentLevel=indentLevel)
self._add("`ifdef EMULATION", indentLevel=0)
self._add("#(", indentLevel=indentLevel+1)
self._add(
(",\n"f"{' ':<{4*(indentLevel + 1)}}").join(port), indentLevel=indentLevel + 1)
self._add(")", indentLevel=indentLevel+1)
self._add("`endif", indentLevel=0)
self._add(f"{compInsName}", indentLevel=indentLevel+1)
self._add("(", indentLevel=indentLevel+1)
else:
self._add(f"{compName} {compInsName} (", indentLevel=indentLevel)
connectPair = []
for i in portsPairs:
if "(" in i[1]:
tmp = i[1].replace("(", "[").replace(")", "]")
else:
tmp = i[1]
connectPair.append(f".{i[0]}({tmp})")
self._add(
(",\n"f"{' ':<{4*(indentLevel + 1)}}").join(connectPair), indentLevel=indentLevel + 1)
self._add(");", indentLevel=indentLevel)
self.addNewLine()
def addComponentDeclarationForFile(self, fileName):
configPortUsed = 0 # 1 means is used
with open(fileName, 'r') as f:
data = f.read()
if result := re.search(r"NumberOfConfigBits.*?(\d+)", data, flags=re.IGNORECASE):
configPortUsed = 1
if result.group(1) == '0':
configPortUsed = 0
return configPortUsed
def addShiftRegister(self, configBits, indentLevel=0):
template = f"""
// the configuration bits shift register
always @ (posedge CLK)
begin
if (MODE=1b'1) begin //configuration mode
ConfigBits <= {{CONFin,ConfigBits[{configBits}-1:1]}};
end
end
assign CONFout = ConfigBits[{configBits}-1];
"""
self._add(template, indentLevel)
def addFlipFlopChain(self, configBits, indentLevel=0):
cfgBit = int(math.ceil(configBits/2.0))*2
template = f"""
genvar k;
assign ConfigBitsInput = {{ConfigBits[{cfgBit}-1-1:0], CONFin;}}
// for k in 0 to Conf/2 generate
for (k=0; k < {cfgBit-1}; k = k + 1) begin: L
LHQD1 inst_LHQD1a(
.D(ConfigBitsInput[k*2]),
.E(CLK),
.Q(ConfigBits[k*2])
);
LHQD1 inst_LHQD1b(
.D(ConfigBitsInput[(k*2)+1]),
.E(MODE),
.Q(ConfigBits[(k*2)+1])
);
end
assign CONFout = ConfigBits[{cfgBit}-1];
"""
self._add(template, indentLevel)
def addAssignScalar(self, left, right, delay=0, indentLevel=0):
if type(right) == list:
self._add(f"assign {left} = {{{','.join(right)}}};", indentLevel)
else:
self._add(f"assign {left} = {right};")
def addAssignVector(self, left, right, widthL, widthR, indentLevel=0):
self._add(
f"assign {left} = {right}[{widthL}:{widthR}];", indentLevel)
def addPreprocIfDef(self, macro, indentLevel=0):
self._add(f"`ifdef {macro}", indentLevel)
def addPreprocIfNotDef(self, macro, indentLevel=0):
self._add(f"`ifndef {macro}", indentLevel)
def addPreprocElse(self, indentLevel=0):
self._add("`else", indentLevel)
def addPreprocEndif(self, indentLevel=0):
self._add("`endif", indentLevel)