| 1 |
7 |
dbrochart |
######################################################################
|
| 2 |
|
|
#### ####
|
| 3 |
|
|
#### distances.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 misc import opposite, adder, register
|
| 44 |
|
|
from myhdl import Signal, intbv, always_comb, instance
|
| 45 |
|
|
|
| 46 |
|
|
def partDistance(a, b, y1, y2, res, ref = intbv(0, 0, 8)):
|
| 47 |
|
|
""" Partial distance from (a, b, y1, y2) = ref.
|
| 48 |
|
|
|
| 49 |
|
|
ref -- reference to compute the distance from
|
| 50 |
|
|
a, b, y1, y2 -- in : decoder input signals, coded with n bits
|
| 51 |
|
|
res -- out : partial distance signal, coded with (n + 2) bits
|
| 52 |
|
|
|
| 53 |
|
|
"""
|
| 54 |
|
|
@instance
|
| 55 |
|
|
def partDistanceLogic():
|
| 56 |
|
|
while 1:
|
| 57 |
|
|
if ref[2] == 0:
|
| 58 |
|
|
bSigned = b.val
|
| 59 |
|
|
else:
|
| 60 |
|
|
bSigned = -b.val
|
| 61 |
|
|
if ref[1] == 0:
|
| 62 |
|
|
y1Signed = y1.val
|
| 63 |
|
|
else:
|
| 64 |
|
|
y1Signed = -y1.val
|
| 65 |
|
|
if ref[0] == 0:
|
| 66 |
|
|
y2Signed = y2.val
|
| 67 |
|
|
else:
|
| 68 |
|
|
y2Signed = -y2.val
|
| 69 |
|
|
res.next = a.val + bSigned + y1Signed + y2Signed
|
| 70 |
|
|
yield a, b, y1, y2
|
| 71 |
|
|
return partDistanceLogic
|
| 72 |
|
|
|
| 73 |
|
|
def distance(partDist, z, dist, n = 4):
|
| 74 |
|
|
""" Distance computation.
|
| 75 |
|
|
|
| 76 |
|
|
n -- number of bits for the coding of the decoder input signals
|
| 77 |
|
|
partDist -- in : sum of the decoder input signals
|
| 78 |
|
|
z -- in : extrinsic information
|
| 79 |
|
|
dist -- out : distance
|
| 80 |
|
|
|
| 81 |
|
|
"""
|
| 82 |
|
|
@always_comb
|
| 83 |
|
|
def distanceLogic():
|
| 84 |
|
|
dist.next = (4 * (2 ** (n - 1) - 1) + partDist.val) / 2 + z.val
|
| 85 |
|
|
return distanceLogic
|
| 86 |
|
|
|
| 87 |
|
|
def distances(a, b, y1, y2, z, distance16, n = 4):
|
| 88 |
|
|
""" Computes the 16 distances from the decoder input signals.
|
| 89 |
|
|
|
| 90 |
|
|
n -- number of bits for the coding of the decoder input signals
|
| 91 |
|
|
a, b, y1, y2 -- in : decoder input signals, coded with n bits
|
| 92 |
|
|
z -- in : extrinsic information signals (x4), coded with m bits
|
| 93 |
|
|
distance16 -- out : distance signals (x16)
|
| 94 |
|
|
|
| 95 |
|
|
"""
|
| 96 |
|
|
partDist = [Signal(intbv(0, -(2**(n+1)), 2**(n+1))) for i in range(16)]
|
| 97 |
|
|
opposite_i = [None for i in range(8)]
|
| 98 |
|
|
distance_i = [None for i in range(16)]
|
| 99 |
|
|
partDistance_i = [None for i in range(8)]
|
| 100 |
|
|
for i in range(8):
|
| 101 |
|
|
partDistance_i[i] = partDistance(a, b, y1, y2, partDist[i], intbv(i, 0, 8))
|
| 102 |
|
|
for i in range(8):
|
| 103 |
|
|
opposite_i[i] = opposite(partDist[i], partDist[15 - i])
|
| 104 |
|
|
for i in range(16):
|
| 105 |
|
|
distance_i[i] = distance(partDist[i], z[i / 4], distance16[i], n)
|
| 106 |
|
|
|
| 107 |
|
|
return partDistance_i, opposite_i, distance_i
|
| 108 |
|
|
|
| 109 |
|
|
def reduction(org, chd, q = 8):
|
| 110 |
|
|
""" (Reduction: if anyone's q(th) bit is set, divide everyone by 2.)
|
| 111 |
|
|
Temporary test: when everyone's q(th) bit is set, reset everyone's q(th) bit.
|
| 112 |
|
|
|
| 113 |
|
|
q -- accumulated distance width
|
| 114 |
|
|
org -- in : original array of 8 q-bit accumulated distances
|
| 115 |
|
|
chd -- out : reduced array of 8 q-bit accumulated distances
|
| 116 |
|
|
|
| 117 |
|
|
"""
|
| 118 |
|
|
# tmp = intbv(0, 0, 2**q)
|
| 119 |
|
|
@instance
|
| 120 |
|
|
def reductionLogic():
|
| 121 |
|
|
while 1:
|
| 122 |
|
|
# msb = bool(0)
|
| 123 |
|
|
msb = bool(1)
|
| 124 |
|
|
for i in range(8):
|
| 125 |
|
|
# msb = msb or org[i].val[q-1]
|
| 126 |
|
|
msb = msb and org[i].val[q - 1]
|
| 127 |
|
|
for i in range(8):
|
| 128 |
|
|
chd[i].next[q-1:0] = org[i].val[q-1:0]
|
| 129 |
|
|
chd[i].next[q - 1] = (not msb) and org[i].val[q - 1]
|
| 130 |
|
|
# if msb == 1:
|
| 131 |
|
|
# tmp[q-1:0] = org[i].val[q:1]
|
| 132 |
|
|
# else:
|
| 133 |
|
|
# tmp = org[i].val
|
| 134 |
|
|
# chd[i].next = tmp
|
| 135 |
|
|
yield org[0], org[1], org[2], org[3], org[4], org[5], org[6], org[7]
|
| 136 |
|
|
return reductionLogic
|
| 137 |
|
|
|
| 138 |
|
|
def accDist(clk, rst, accDistReg, dist, accDistNew, q = 8):
|
| 139 |
|
|
""" Accumulated distances.
|
| 140 |
|
|
|
| 141 |
|
|
q -- in : accumulated distance width
|
| 142 |
|
|
clk, rst -- in : clock and negative reset
|
| 143 |
|
|
accDistReg -- in : original array of 8 q-bit accumulated distance registers
|
| 144 |
|
|
dist -- in : array of 16 distances
|
| 145 |
|
|
accDistNew -- out : array of 32 (q+1)-bit accumulated distances
|
| 146 |
|
|
|
| 147 |
|
|
"""
|
| 148 |
|
|
adder_i = [None for i in range(32)]
|
| 149 |
|
|
register_i = [None for i in range(8)]
|
| 150 |
|
|
accDistOld = [Signal(intbv(0, 0, 2**q)) for i in range(8)]
|
| 151 |
|
|
accDistRed = [Signal(intbv(0, 0, 2**q)) for i in range(8)]
|
| 152 |
|
|
accDistRegSorted = [Signal(intbv(0, 0, 2**q)) for i in range(8)]
|
| 153 |
|
|
accDistRegDelta = [Signal(intbv(0, 0, 2**q)) for i in range(8)]
|
| 154 |
|
|
distIndex = [0, 7, 11, 12, 0, 7, 11, 12, 2, 5, 9, 14, 2, 5, 9, 14, 3, 4, 8, 15, 3, 4, 8, 15, 1, 6, 10, 13, 1, 6, 10, 13]
|
| 155 |
|
|
for i in range(32):
|
| 156 |
|
|
adder_i[i] = adder(accDistOld[i/4], dist[distIndex[i]], accDistNew[i])
|
| 157 |
|
|
reduction_i0 = reduction(accDistReg, accDistRed, q)
|
| 158 |
|
|
for i in range(8):
|
| 159 |
|
|
register_i[i] = register(clk, rst, accDistRed[i], accDistOld[i])
|
| 160 |
|
|
|
| 161 |
|
|
return adder_i, register_i, reduction_i0
|