1 |
7 |
dbrochart |
######################################################################
|
2 |
|
|
#### ####
|
3 |
|
|
#### misc.py ####
|
4 |
|
|
#### ####
|
5 |
|
|
#### This file is part of the turbo decoder IP core project ####
|
6 |
|
|
#### http://www.opencores.org/projects/turbocodes/ ####
|
7 |
|
|
#### ####
|
8 |
|
|
#### Author(s): ####
|
9 |
|
|
#### - David Brochart(dbrochart@opencores.org) ####
|
10 |
|
|
#### ####
|
11 |
|
|
#### All additional information is available in the README.txt ####
|
12 |
|
|
#### file. ####
|
13 |
|
|
#### ####
|
14 |
|
|
######################################################################
|
15 |
|
|
#### ####
|
16 |
|
|
#### Copyright (C) 2005 Authors ####
|
17 |
|
|
#### ####
|
18 |
|
|
#### This source file may be used and distributed without ####
|
19 |
|
|
#### restriction provided that this copyright statement is not ####
|
20 |
|
|
#### removed from the file and that any derivative work contains ####
|
21 |
|
|
#### the original copyright notice and the associated disclaimer. ####
|
22 |
|
|
#### ####
|
23 |
|
|
#### This source file is free software; you can redistribute it ####
|
24 |
|
|
#### and/or modify it under the terms of the GNU Lesser General ####
|
25 |
|
|
#### Public License as published by the Free Software Foundation; ####
|
26 |
|
|
#### either version 2.1 of the License, or (at your option) any ####
|
27 |
|
|
#### later version. ####
|
28 |
|
|
#### ####
|
29 |
|
|
#### This source is distributed in the hope that it will be ####
|
30 |
|
|
#### useful, but WITHOUT ANY WARRANTY; without even the implied ####
|
31 |
|
|
#### warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ####
|
32 |
|
|
#### PURPOSE. See the GNU Lesser General Public License for more ####
|
33 |
|
|
#### details. ####
|
34 |
|
|
#### ####
|
35 |
|
|
#### You should have received a copy of the GNU Lesser General ####
|
36 |
|
|
#### Public License along with this source; if not, download it ####
|
37 |
|
|
#### from http://www.opencores.org/lgpl.shtml ####
|
38 |
|
|
#### ####
|
39 |
|
|
######################################################################
|
40 |
|
|
|
41 |
|
|
|
42 |
|
|
|
43 |
|
|
from myhdl import Signal, intbv, always_comb, instance, always, posedge, negedge, concat
|
44 |
|
|
|
45 |
|
|
def delayer(clk, rst, d, q, delay = 1, mi = 0, ma = 1):
|
46 |
|
|
""" Delayer.
|
47 |
|
|
|
48 |
|
|
delay -- number of clock cycles to delay
|
49 |
|
|
mi -- minimum value of the signal to delay
|
50 |
|
|
ma -- maximum value of the signal to delay
|
51 |
|
|
clk, rst -- in : clock and negative reset
|
52 |
|
|
d -- in : signal to be delayed by "delay" clock cycles
|
53 |
|
|
q -- out : delayed signal
|
54 |
|
|
|
55 |
|
|
"""
|
56 |
|
|
r = [Signal(intbv(0, mi, ma)) for i in range(delay)]
|
57 |
|
|
@always(clk.posedge, rst.negedge)
|
58 |
|
|
def delayerLogic():
|
59 |
|
|
if rst.val == 0:
|
60 |
|
|
q.next = 0
|
61 |
|
|
for i in range(delay):
|
62 |
|
|
r[i].next = 0
|
63 |
|
|
else:
|
64 |
|
|
r[0].next = d
|
65 |
|
|
q.next = r[delay - 1].val
|
66 |
|
|
for i in range(delay - 1):
|
67 |
|
|
r[i + 1].next = r[i].val
|
68 |
|
|
return delayerLogic
|
69 |
|
|
|
70 |
|
|
def opposite(pos, neg):
|
71 |
|
|
""" Take the opposite of a number.
|
72 |
|
|
|
73 |
|
|
pos -- in : original number
|
74 |
|
|
neg -- out : opposite number
|
75 |
|
|
|
76 |
|
|
"""
|
77 |
|
|
@always_comb
|
78 |
|
|
def oppositeLogic():
|
79 |
|
|
neg.next = -pos.val
|
80 |
|
|
return oppositeLogic
|
81 |
|
|
|
82 |
|
|
def adder(op1, op2, res):
|
83 |
|
|
""" Adder.
|
84 |
|
|
|
85 |
|
|
op1 -- in : first operand
|
86 |
|
|
op2 -- in : second operand
|
87 |
|
|
res -- out : result of the addition
|
88 |
|
|
|
89 |
|
|
"""
|
90 |
|
|
@always_comb
|
91 |
|
|
def addLogic():
|
92 |
|
|
res.next = op1.val + op2.val
|
93 |
|
|
return addLogic
|
94 |
|
|
|
95 |
|
|
def register(clk, rst, d, q):
|
96 |
|
|
""" Register.
|
97 |
|
|
|
98 |
|
|
clk, rst -- in : clock and negative reset
|
99 |
|
|
d -- in : next value
|
100 |
|
|
q -- out : current value
|
101 |
|
|
|
102 |
|
|
"""
|
103 |
|
|
@always(clk.posedge, rst.negedge)
|
104 |
|
|
def registerLogic():
|
105 |
|
|
if rst.val == 0:
|
106 |
|
|
q.next = 0
|
107 |
|
|
else:
|
108 |
|
|
q.next = d.val
|
109 |
|
|
return registerLogic
|
110 |
|
|
|
111 |
|
|
def cmp2(op1, op2, res):
|
112 |
|
|
""" 2-input comparator.
|
113 |
|
|
|
114 |
|
|
op1 -- in : first operand
|
115 |
|
|
op2 -- in : second operand
|
116 |
|
|
res -- out : compare result (0 if op2 < op1, 1 otherwise)
|
117 |
|
|
|
118 |
|
|
"""
|
119 |
|
|
@always_comb
|
120 |
|
|
def cmp2Logic():
|
121 |
|
|
if op1.val > op2.val:
|
122 |
|
|
res.next = 0
|
123 |
|
|
else:
|
124 |
|
|
res.next = 1
|
125 |
|
|
return cmp2Logic
|
126 |
|
|
|
127 |
|
|
def mux2(in1, in2, sel, outSel):
|
128 |
|
|
""" 2-input mux.
|
129 |
|
|
|
130 |
|
|
in1 -- in : first input signal
|
131 |
|
|
in2 -- in : second input signal
|
132 |
|
|
sel -- in : 1-bit control signal
|
133 |
|
|
outSel -- out : selected output signal
|
134 |
|
|
|
135 |
|
|
"""
|
136 |
|
|
@always_comb
|
137 |
|
|
def mux2Logic():
|
138 |
|
|
if sel.val == 0:
|
139 |
|
|
outSel.next = in2.val
|
140 |
|
|
else:
|
141 |
|
|
outSel.next = in1.val
|
142 |
|
|
return mux2Logic
|
143 |
|
|
|
144 |
|
|
def orGate(op1, op2, res):
|
145 |
|
|
""" 2-input OR gate.
|
146 |
|
|
|
147 |
|
|
op1 -- in : first operand
|
148 |
|
|
op2 -- in : second operand
|
149 |
|
|
res -- out : result
|
150 |
|
|
|
151 |
|
|
"""
|
152 |
|
|
@always_comb
|
153 |
|
|
def orGateLogic():
|
154 |
|
|
res.next = op1.val or op2.val
|
155 |
|
|
return orGateLogic
|
156 |
|
|
|
157 |
|
|
def min4(op1, op2, op3, op4, res1, res2, res3, q = 8):
|
158 |
|
|
""" Selects the minimum between 4 values.
|
159 |
|
|
|
160 |
|
|
q -- width of the signals to compare
|
161 |
|
|
op1 -- in : first input signal
|
162 |
|
|
op2 -- in : second input signal
|
163 |
|
|
op3 -- in : third input signal
|
164 |
|
|
op4 -- in : fourth input signal
|
165 |
|
|
res1 -- out : partial code of the minimum value
|
166 |
|
|
res2 -- out : partial code of the minimum value
|
167 |
|
|
res3 -- out : partial code of the minimum value
|
168 |
|
|
|
169 |
|
|
"""
|
170 |
|
|
op5 = Signal(intbv(0, 0, 2**q))
|
171 |
|
|
op6 = Signal(intbv(0, 0, 2**q))
|
172 |
|
|
cmp2_i0 = cmp2(op1, op2, res1)
|
173 |
|
|
cmp2_i1 = cmp2(op3, op4, res2)
|
174 |
|
|
mux2_i0 = mux2(op1, op2, res1, op5)
|
175 |
|
|
mux2_i1 = mux2(op3, op4, res2, op6)
|
176 |
|
|
cmp2_i2 = cmp2(op5, op6, res3)
|
177 |
|
|
|
178 |
|
|
return cmp2_i0, cmp2_i1, mux2_i0, mux2_i1, cmp2_i2
|
179 |
|
|
|
180 |
|
|
def mux4(in1, in2, in3, in4, sel, outSel):
|
181 |
|
|
""" 4-input mux.
|
182 |
|
|
|
183 |
|
|
in1 -- in : first input signal
|
184 |
|
|
in2 -- in : second input signal
|
185 |
|
|
in3 -- in : third input signal
|
186 |
|
|
in4 -- in : fourth input signal
|
187 |
|
|
sel -- in : 2-bit control signal
|
188 |
|
|
outSel -- out : selected output signal
|
189 |
|
|
|
190 |
|
|
"""
|
191 |
|
|
@always_comb
|
192 |
|
|
def mux4Logic():
|
193 |
|
|
if sel.val == 0:
|
194 |
|
|
outSel.next = in1.val
|
195 |
|
|
elif sel.val == 1:
|
196 |
|
|
outSel.next = in2.val
|
197 |
|
|
elif sel.val == 2:
|
198 |
|
|
outSel.next = in3.val
|
199 |
|
|
else:
|
200 |
|
|
outSel.next = in4.val
|
201 |
|
|
return mux4Logic
|
202 |
|
|
|
203 |
|
|
def cod2(in1, in2, in3, outCod):
|
204 |
|
|
""" 2-bit coder.
|
205 |
|
|
|
206 |
|
|
in1 -- in : 1-bit first input signal
|
207 |
|
|
in2 -- in : 1-bit second input signal
|
208 |
|
|
in3 -- in : 1-bit third input signal
|
209 |
|
|
outCod -- out : 2-bit coded value
|
210 |
|
|
|
211 |
|
|
"""
|
212 |
|
|
tmp = intbv(0, 0, 8)
|
213 |
|
|
@always_comb
|
214 |
|
|
def cod2Logic():
|
215 |
|
|
tmp = concat(bool(in1.val), bool(in2.val), bool(in3.val))
|
216 |
|
|
if tmp == 5:
|
217 |
|
|
outCod.next = 0
|
218 |
|
|
elif tmp == 7:
|
219 |
|
|
outCod.next = 0
|
220 |
|
|
elif tmp == 1:
|
221 |
|
|
outCod.next = 1
|
222 |
|
|
elif tmp == 3:
|
223 |
|
|
outCod.next = 1
|
224 |
|
|
elif tmp == 2:
|
225 |
|
|
outCod.next = 2
|
226 |
|
|
elif tmp == 6:
|
227 |
|
|
outCod.next = 2
|
228 |
|
|
elif tmp == 0:
|
229 |
|
|
outCod.next = 3
|
230 |
|
|
else:
|
231 |
|
|
outCod.next = 3
|
232 |
|
|
return cod2Logic
|
233 |
|
|
|
234 |
|
|
def cod3(inSig, outCod):
|
235 |
|
|
""" 3-bit coder.
|
236 |
|
|
|
237 |
|
|
inSig -- in : 7 1-bit input signals
|
238 |
|
|
outCod -- out : 3-bit coded value
|
239 |
|
|
|
240 |
|
|
"""
|
241 |
|
|
tmp = intbv(0, 0, 8)
|
242 |
|
|
@instance
|
243 |
|
|
def cod3Logic():
|
244 |
|
|
while 1:
|
245 |
|
|
tmp[0] = ((not inSig[6].val) and (not inSig[5].val) and (not inSig[3].val)) or ((not inSig[6].val) and inSig[5].val and (not inSig[2].val)) or (inSig[6].val and (not inSig[4].val) and (not inSig[1].val)) or ((inSig[6].val) and (inSig[4].val) and (not inSig[0].val));
|
246 |
|
|
tmp[1] = ((not inSig[6].val) and (not inSig[5].val)) or (inSig[6].val and (not inSig[4].val));
|
247 |
|
|
tmp[2] = not inSig[6].val
|
248 |
|
|
outCod.next = tmp
|
249 |
|
|
yield inSig[0], inSig[1], inSig[2], inSig[3], inSig[4], inSig[5], inSig[6]
|
250 |
|
|
return cod3Logic
|
251 |
|
|
|
252 |
|
|
def min8(op, res, q = 8):
|
253 |
|
|
""" Selects the minimum between 8 values.
|
254 |
|
|
|
255 |
|
|
q -- accumulated distance width
|
256 |
|
|
op -- in : input signals
|
257 |
|
|
res -- out : code of the minimum value
|
258 |
|
|
|
259 |
|
|
"""
|
260 |
|
|
tmp = [Signal(intbv(0, 0, 2**q)) for i in range(6)]
|
261 |
|
|
cmp2_i0 = cmp2(op[0], op[1], res[0])
|
262 |
|
|
cmp2_i1 = cmp2(op[2], op[3], res[1])
|
263 |
|
|
cmp2_i2 = cmp2(op[4], op[5], res[2])
|
264 |
|
|
cmp2_i3 = cmp2(op[6], op[7], res[3])
|
265 |
|
|
mux2_i0 = mux2(op[0], op[1], res[0], tmp[0])
|
266 |
|
|
mux2_i1 = mux2(op[2], op[3], res[1], tmp[1])
|
267 |
|
|
mux2_i2 = mux2(op[4], op[5], res[2], tmp[2])
|
268 |
|
|
mux2_i3 = mux2(op[6], op[7], res[3], tmp[3])
|
269 |
|
|
cmp2_i4 = cmp2(tmp[0], tmp[1], res[4])
|
270 |
|
|
cmp2_i5 = cmp2(tmp[2], tmp[3], res[5])
|
271 |
|
|
mux2_i4 = mux2(tmp[0], tmp[1], res[4], tmp[4])
|
272 |
|
|
mux2_i5 = mux2(tmp[2], tmp[3], res[5], tmp[5])
|
273 |
|
|
cmp2_i6 = cmp2(tmp[4], tmp[5], res[6])
|
274 |
|
|
|
275 |
|
|
return cmp2_i0, cmp2_i1, cmp2_i2, cmp2_i3, mux2_i0, mux2_i1, mux2_i2, mux2_i3, cmp2_i4, cmp2_i5, mux2_i4, mux2_i5, cmp2_i6
|
276 |
|
|
|
277 |
|
|
def mux8(in1, in2, in3, in4, in5, in6, in7, in8, sel, outSel):
|
278 |
|
|
""" 8-input mux (4 bits per input).
|
279 |
|
|
|
280 |
|
|
in1 -- in : first input signals
|
281 |
|
|
in2 -- in : second input signals
|
282 |
|
|
in3 -- in : third input signals
|
283 |
|
|
in4 -- in : fourth input signals
|
284 |
|
|
in5 -- in : fifth input signals
|
285 |
|
|
in6 -- in : sixth input signals
|
286 |
|
|
in7 -- in : seventh input signals
|
287 |
|
|
in8 -- in : eighth input signals
|
288 |
|
|
sel -- in : 3-bit control signal
|
289 |
|
|
outSel -- out : selected output signals
|
290 |
|
|
|
291 |
|
|
"""
|
292 |
|
|
@instance
|
293 |
|
|
def mux8Logic():
|
294 |
|
|
while 1:
|
295 |
|
|
if sel.val == 0:
|
296 |
|
|
for i in range(4):
|
297 |
|
|
outSel[i].next = in1[i].val
|
298 |
|
|
elif sel.val == 1:
|
299 |
|
|
for i in range(4):
|
300 |
|
|
outSel[i].next = in2[i].val
|
301 |
|
|
elif sel.val == 2:
|
302 |
|
|
for i in range(4):
|
303 |
|
|
outSel[i].next = in3[i].val
|
304 |
|
|
elif sel.val == 3:
|
305 |
|
|
for i in range(4):
|
306 |
|
|
outSel[i].next = in4[i].val
|
307 |
|
|
elif sel.val == 4:
|
308 |
|
|
for i in range(4):
|
309 |
|
|
outSel[i].next = in5[i].val
|
310 |
|
|
elif sel.val == 5:
|
311 |
|
|
for i in range(4):
|
312 |
|
|
outSel[i].next = in6[i].val
|
313 |
|
|
elif sel.val == 6:
|
314 |
|
|
for i in range(4):
|
315 |
|
|
outSel[i].next = in7[i].val
|
316 |
|
|
else:
|
317 |
|
|
for i in range(4):
|
318 |
|
|
outSel[i].next = in8[i].val
|
319 |
|
|
yield in1[0], in1[1], in1[2], in1[3], in2[0], in2[1], in2[2], in2[3], in3[0], in3[1], in3[2], in3[3], in4[0], in4[1], in4[2], in4[3], in5[0], in5[1], in5[2], in5[3], in6[0], in6[1], in6[2], in6[3], in7[0], in7[1], in7[2], in7[3], in8[0], in8[1], in8[2], in8[3], sel
|
320 |
|
|
return mux8Logic
|
321 |
|
|
|
322 |
|
|
def sub(op1, op2, res):
|
323 |
|
|
""" Substracter.
|
324 |
|
|
|
325 |
|
|
op1 -- in : first operand
|
326 |
|
|
op2 -- in : second operand
|
327 |
|
|
res -- out : result of the substraction
|
328 |
|
|
|
329 |
|
|
"""
|
330 |
|
|
@instance
|
331 |
|
|
def subLogic():
|
332 |
|
|
while 1:
|
333 |
|
|
if op1.val >= op2.val: # remove that when translate into Verilog (Python expects a positive result)
|
334 |
|
|
res.next = op1.val - op2.val
|
335 |
|
|
yield op1, op2
|
336 |
|
|
return subLogic
|