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

Subversion Repositories ima_adpcm_enc_dec

[/] [ima_adpcm_enc_dec/] [trunk/] [verilog/] [bench/] [tb_ima_adpcm.v] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 motilito
//---------------------------------------------------------------------------------------
2
//      Project:        ADPCM Encoder / Decoder 
3
// 
4
//      Filename:       tb_ima_adpcm.v                  (April 26, 2010 )
5
// 
6
//      Author(s):      Moti Litochevski 
7
// 
8
//      Description:
9
//              This file implements the ADPCM encoder & decoder test bench. The input samples 
10
//              to be encoded are read from a binary input file. The encoder stream output and 
11
//              decoded samples are also compared with binary files generated by the Scilab 
12
//              simulation.
13
//
14
//---------------------------------------------------------------------------------------
15
//
16
//      To Do: 
17
//      - 
18
// 
19
//---------------------------------------------------------------------------------------
20
// 
21
//      Copyright (C) 2010 Moti Litochevski 
22
// 
23
//      This source file may be used and distributed without restriction provided that this 
24
//      copyright statement is not removed from the file and that any derivative work 
25
//      contains the original copyright notice and the associated disclaimer.
26
//
27
//      THIS SOURCE FILE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, 
28
//      INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND 
29
//      FITNESS FOR A PARTICULAR PURPOSE. 
30
// 
31
//---------------------------------------------------------------------------------------
32
 
33
`timescale 1ns / 1ns
34
 
35
module test;
36
//---------------------------------------------------------------------------------------
37
// internal signal  
38
reg clock;                              // global clock 
39
reg reset;                              // global reset 
40
reg [15:0] inSamp;               // encoder input sample 
41
reg inValid;                    // encoder input valid flag 
42
wire inReady;                   // encoder input ready indication  
43
wire [3:0] encPcm;               // encoder encoded output value 
44
wire encValid;                  // encoder output valid flag 
45
wire [15:0] decSamp;     // decoder output sample value 
46
wire decValid;                  // decoder output valid flag 
47
integer sampCount, encCount, decCount;
48
integer infid, encfid, decfid;
49
integer intmp, enctmp, dectmp;
50
reg [3:0] encExpVal;
51
reg [15:0] decExpVal;
52
reg [31:0] dispCount;
53
 
54
// global definitions 
55
`define EOF                             -1
56
 
57
// file names 
58
`define IN_FILE         "../../../scilab/test_in.bin"
59
`define ENC_FILE        "../../../scilab/test_enc.bin"
60
`define DEC_FILE        "../../../scilab/test_dec.bin"
61
 
62
//---------------------------------------------------------------------------------------
63
// test bench implementation 
64
// global signals generation  
65
initial
66
begin
67
        clock = 0;
68
        reset = 1;
69
        #40 reset = 0;
70
end
71
 
72
// clock generator - 50MHz clock 
73
always
74
begin
75
        #10 clock = 0;
76
        #10 clock = 1;
77
end
78
 
79
// test bench dump variables 
80
initial
81
begin
82
        $display("");
83
        $display("IMA ADPCM encoder & decoder simulation");
84
        $display("--------------------------------------");
85
        $dumpfile("test.vcd");
86
        $dumpvars(0, test);
87
end
88
 
89
//------------------------------------------------------------------
90
// encoder input samples read process 
91
initial
92
begin
93
        // clear encoder input signal 
94
        inSamp = 16'b0;
95
        inValid = 1'b0;
96
        // clear samples counter 
97
        sampCount = 0;
98
 
99
        // binary input file 
100
        infid = $fopen(`IN_FILE, "rb");
101
 
102
        // wait for reset release
103
        while (reset) @(posedge clock);
104
        repeat (50) @(posedge clock);
105
 
106
        // read input samples file 
107
        intmp = $fgetc(infid);
108
        while (intmp != `EOF)
109
        begin
110
                // read the next character to form the new input sample 
111
                // Note that first byte is used as the low byte of the sample 
112
                inSamp[7:0] <= intmp;
113
                inSamp[15:8] <= $fgetc(infid);
114
                // sign input sample is valid 
115
                inValid <= 1'b1;
116
                @(posedge clock);
117
 
118
                // update the sample counter 
119
                sampCount = sampCount + 1;
120
 
121
                // wait for encoder input ready assertion to confirm the new sample was read
122
                // by the encoder.
123
                while (!inReady)
124
                        @(posedge clock);
125
 
126
                // read next character from the input file 
127
                intmp = $fgetc(infid);
128
        end
129
        // sign input is not valid 
130
        inValid <= 1'b0;
131
        @(posedge clock);
132
 
133
        // close input file 
134
        $fclose(infid);
135
end
136
 
137
// encoder output checker - the encoder output is compared to the value read from 
138
// the ADPCM coded samples file. 
139
initial
140
begin
141
        // clear encoded sample value 
142
        encCount = 0;
143
        // open input file 
144
        encfid = $fopen(`ENC_FILE, "rb");
145
 
146
        // wait for reset release
147
        while (reset) @(posedge clock);
148
 
149
        // encoder output compare loop 
150
        enctmp = $fgetc(encfid);
151
        while (enctmp != `EOF)
152
        begin
153
                // assign the expected value to a register with the same width 
154
                encExpVal = enctmp;
155
 
156
                // wait for encoder output valid 
157
                while (!encValid)
158
                        @(posedge clock);
159
 
160
                // compare the encoded value with the value read from the input file 
161
                if (encPcm != encExpVal)
162
                begin
163
                        // announce error detection and exit simulation 
164
                        $display(" Error!");
165
                        $display("Error found in encoder output index %0d.", encCount+1);
166
                        $display("   (expected value 'h%x, got value 'h%x)", encExpVal, encPcm);
167
                        // wait for a few clock cycles before ending simulation 
168
                        repeat (20) @(posedge clock);
169
                        $finish;
170
                end
171
 
172
                // update the encoded sample counter 
173
                encCount = encCount + 1;
174
                // delay for a clock cycle after comparison 
175
                @(posedge clock);
176
 
177
                // read next char from input file 
178
                enctmp = $fgetc(encfid);
179
        end
180
 
181
        // close input file 
182
        $fclose(encfid);
183
end
184
 
185
// decoder output checker - the decoder output is compared to the value read from 
186
// the ADPCM decoded samples file. 
187
initial
188
begin
189
        // clear decoded sample value 
190
        decCount = 0;
191
        dispCount = 0;
192
        // open input file 
193
        decfid = $fopen(`DEC_FILE, "rb");
194
 
195
        // wait for reset release
196
        while (reset) @(posedge clock);
197
 
198
        // display simulation progress bar title 
199
        $write("Simulation progress: ");
200
 
201
        // decoder output compare loop 
202
        dectmp = $fgetc(decfid);
203
        while (dectmp != `EOF)
204
        begin
205
                // read the next char to form the expected 16 bit sample value 
206
                decExpVal[7:0] = dectmp;
207
                decExpVal[15:8] = $fgetc(decfid);
208
 
209
                // wait for decoder output valid 
210
                while (!decValid)
211
                        @(posedge clock);
212
 
213
                // compare the decoded value with the value read from the input file 
214
                if (decSamp != decExpVal)
215
                begin
216
                        // announce error detection and exit simulation 
217
                        $display(" Error!");
218
                        $display("Error found in decoder output index %0d.", decCount+1);
219
                        $display("   (expected value 'h%x, got value 'h%x)", decExpVal, decSamp);
220
                        // wait for a few clock cycles before ending simulation 
221
                        repeat (20) @(posedge clock);
222
                        $finish;
223
                end
224
                // delay for a clock cycle after comparison 
225
                @(posedge clock);
226
 
227
                // update the decoded sample counter 
228
                decCount = decCount + 1;
229
 
230
                // check if simulation progress should be displayed
231
                if (dispCount[31:13] != (decCount >> 13))
232
                        $write(".");
233
                // update the display counter 
234
                dispCount = decCount;
235
 
236
                // read next char from input file 
237
                dectmp = $fgetc(decfid);
238
        end
239
 
240
        // close input file 
241
        $fclose(decfid);
242
 
243
        // when decoder output is done announce simulation was successful 
244
        $display(" Done");
245
        $display("Simulation ended successfully after %0d samples", decCount);
246
        $finish;
247
end
248
 
249
//------------------------------------------------------------------
250
// device under test 
251
// Encoder instance 
252
ima_adpcm_enc enc
253
(
254
        .clock(clock),
255
        .reset(reset),
256
        .inSamp(inSamp),
257
        .inValid(inValid),
258
        .inReady(inReady),
259
        .outPCM(encPcm),
260
        .outValid(encValid),
261
        .outPredictSamp(/* not used */),
262
        .outStepIndex(/* not used */)
263
);
264
 
265
// Decoder instance 
266
ima_adpcm_dec dec
267
(
268
        .clock(clock),
269
        .reset(reset),
270
        .inPCM(encPcm),
271
        .inValid(encValid),
272
        .inReady(decReady),
273
        .inPredictSamp(16'b0),
274
        .inStepIndex(7'b0),
275
        .inStateLoad(1'b0),
276
        .outSamp(decSamp),
277
        .outValid(decValid)
278
);
279
 
280
endmodule
281
//---------------------------------------------------------------------------------------
282
//                                              Th.. Th.. Th.. Thats all folks !!!
283
//---------------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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