OpenCores
URL https://opencores.org/ocsvn/turbocodes/turbocodes/trunk

Subversion Repositories turbocodes

[/] [turbocodes/] [trunk/] [src/] [myhdl/] [testbench.py] - Blame information for rev 7

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 7 dbrochart
######################################################################
2
####                                                              ####
3
####  testbench.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 random import randrange
44
from myhdl import Signal, posedge, negedge, always, instance
45
 
46
def randGen(clk, rst, a, b):
47
    """ Random signal generator.
48
 
49
    clk, rst    -- in  : clock and negative reset
50
    a, b        -- out : generated random signals
51
 
52
    """
53
#    x = Signal(int(0))
54
    @always(clk.posedge, rst.negedge)
55
    def randGenLogic():
56
        if rst.val == 0:
57
            a.next = 0
58
            b.next = 0
59
#            x.next = 0
60
        else:
61
            a.next = randrange(2)
62
            b.next = randrange(2)
63
 
64
#            a.next = 1
65
#            b.next = 1
66
 
67
#            if x.val == 0:
68
#                a.next = 1
69
#                b.next = 0
70
#                x.next = 1
71
#            if x.val == 1:
72
#                a.next = 0
73
#                b.next = 1
74
#                x.next = 2
75
#            if x.val == 2:
76
#                a.next = 0
77
#                b.next = 0
78
#                x.next = 0
79
 
80
#            if x.val == 0:
81
#                a.next = 0
82
#                b.next = 1
83
#                x.next = 1
84
#            else:
85
#                a.next = 0
86
#                b.next = 1
87
    return randGenLogic
88
 
89
def ber(clk, rst, aOrg, bOrg, aDec, bDec, wait, resFile):
90
    """ Bit Error Rate monitor.
91
 
92
    it          -- iteration number (0 is before decoding)
93
    clk, rst    -- in  : clock and negative reset
94
    aOrg, bOrg  -- in  : original data
95
    aDec, bDec  -- in  : decoded data
96
    wait        -- in  : number of clock cycles to wait before computing the BER
97
    resFile     -- out : file where the BER is saved
98
 
99
    """
100
    ber = Signal(0.0)
101
    cnt = Signal(0)
102
    diffCnt = Signal(0)
103
    waitCnt = Signal(0)
104
    @always(clk.posedge, rst.negedge)
105
    def berLogic():
106
        if rst.val == 0:
107
            orgCnt = 0
108
            j = 0
109
            waitCnt.next = 0
110
        else:
111
            if waitCnt == wait:
112
                cnt.next = cnt + 1
113
                if aOrg.val != aDec.val:
114
                    diffCnt.next = diffCnt + 1
115
                if bOrg.val != bDec.val:
116
                    diffCnt.next = diffCnt + 1
117
                ber.next = float(diffCnt.next) / float(2 * cnt.next)
118
                resFile.write('%1.10f\n' % ber.next)
119
                if (cnt.next % 100) == 0:
120
                    resFile.flush()
121
            else:
122
                waitCnt.next = waitCnt + 1
123
    return berLogic
124
 
125
def siho(aSoft, bSoft, aHard, bHard):
126
    """ Soft Input Hard Output decision.
127
 
128
    aSoft, bSoft    -- in  : soft inputs
129
    aHard, bHard    -- out : hard outputs
130
 
131
    """
132
    @instance
133
    def sihoLogic():
134
        while 1:
135
            if aSoft.val < 0:
136
                aHard.next = 0
137
            else:
138
                aHard.next = 1
139
            if bSoft.val < 0:
140
                bHard.next = 0
141
            else:
142
                bHard.next = 1
143
            yield aSoft, bSoft
144
    return sihoLogic
145
 
146
def monitor(clk, rst, *args):
147
    """ Signal monitor.
148
 
149
    clk, rst    -- in  : clock and negative reset
150
    args        -- in  : list of signals to monitor
151
 
152
    """
153
    @always(clk.posedge, rst.negedge)
154
    def monitorLogic():
155
        if rst.val != 0:
156
            for arg in args:
157
                print "%3d" % int(arg),
158
            print
159
    return monitorLogic
160
 
161
def sorter(clk, rst, *args):
162
    """ Sorter.
163
 
164
    clk, rst    -- in  : clock and negative reset
165
    args        -- in/out : arguments to be sorted / sorted arguments (first half is input / second half is output)
166
 
167
    """
168
    argNb = len(args) / 2
169
    @always(clk.posedge, rst.negedge)
170
    def sorterLogic():
171
        if rst.val == 0:
172
            for i in range(argNb):
173
                args[i + argNb].next = 0
174
        else:
175
            arr = []
176
            for i in range(argNb):
177
                arr.append(int(args[i].val))
178
            arr.sort()
179
            for i in range(argNb):
180
                args[i + argNb].next = arr[i]
181
    return sorterLogic
182
 
183
def sorter(*args):
184
    """ Sorter.
185
 
186
    args    -- in/out : data to be sorted / sorted data (first half is input / second half is output)
187
 
188
    """
189
    argNb = len(args) / 2
190
    @instance
191
    def sorterLogic():
192
        while 1:
193
            arr = []
194
            for i in range(argNb):
195
                arr.append(int(args[i].val))
196
            arr.sort()
197
            for i in range(argNb):
198
                args[i + argNb].next = arr[i]
199
            yield args
200
    return sorterLogic
201
 
202
def delta(*args):
203
    """ Removes the minimum value from all values.
204
 
205
    args    -- in/out : original data / delta data (first half is input / second half is output)
206
 
207
    """
208
    argNb = len(args) / 2
209
    @instance
210
    def deltaLogic():
211
        while 1:
212
            arr = []
213
            for i in range(argNb):
214
                arr.append(int(args[i].val))
215
            minimum = min(arr)
216
            for i in range(argNb):
217
                args[i + argNb].next = arr[i] - minimum
218
            yield args
219
    return deltaLogic

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.