Merge branch 'new_eth' of http://gnuradio.org/git/eb into new_eth
[debian/gnuradio] / usrp2 / fpga / sdr_lib / halfband_tb.v
1 module halfband_tb( ) ;
2     
3     // Parameters for instantiation
4     parameter               clocks  = 2 ; // Number of clocks per input
5     parameter               decim   = 0 ; // Sets the filter to decimate
6     parameter               rate    = 2 ; // Sets the decimation rate
7
8     reg                     clock ;
9     reg                     reset ;
10     reg                     enable ;
11     reg                     strobe_in ;
12     reg     signed  [17:0]  data_in ;
13     wire                    strobe_out ;
14     wire    signed  [17:0]  data_out ;
15
16     // Setup the clock
17     initial clock = 1'b0 ;
18     always #5 clock <= ~clock ;
19
20     // Come out of reset after a while
21     initial reset = 1'b1 ;
22     initial #100 reset = 1'b0 ;
23
24     // Enable the entire system
25     initial enable = 1'b1 ;
26
27     // Instantiate UUT
28     halfband_ideal 
29       #(
30         .decim      ( decim         ),
31         .rate       ( rate          )
32       ) uut(
33         .clock      ( clock         ),
34         .reset      ( reset         ),
35         .enable     ( enable        ),
36         .strobe_in  ( strobe_in     ),
37         .data_in    ( data_in       ),
38         .strobe_out ( strobe_out    ),
39         .data_out   ( data_out      )
40       ) ;
41     
42     integer i, ri, ro, infile, outfile ;
43
44     // Setup file IO
45     initial begin
46         infile = $fopen("input.dat","r") ;
47         outfile = $fopen("output.dat","r") ;
48         $timeformat(-9, 2, " ns", 10) ;
49     end
50
51     reg                 endofsim ;
52     reg signed  [17:0]  compare ;
53     integer             noe ;
54     initial             noe = 0 ;
55
56     initial begin
57         // Initialize inputs
58         strobe_in <= 1'd0 ;
59         data_in <= 18'd0 ;
60
61         // Wait for reset to go away
62         @(negedge reset) #0 ;
63         
64         // While we're still simulating ...
65         while( !endofsim ) begin
66
67             // Write the input from the file or 0 if EOF...
68             @( posedge clock ) begin
69                 #1 ;
70                 strobe_in <= 1'b1 ;
71                 if( !$feof(infile) )
72                     ri = $fscanf( infile, "%d", data_in ) ;
73                 else
74                     data_in <= 18'd0 ;
75             end
76
77             // Clocked in - set the strobe to 0 if the number of
78             // clocks per sample is greater than 1
79             if( clocks > 1 ) begin
80                 @(posedge clock) begin
81                     strobe_in <= 1'b0  ;
82                 end
83
84                 // Wait for the specified number of cycles
85                 for( i = 0 ; i < (clocks-2) ; i = i + 1 ) begin
86                     @(posedge clock) #1 ;
87                 end
88             end
89         end
90
91         // Print out the number of errors that occured
92         if( noe )
93             $display( "FAILED: %d errors during simulation", noe ) ;
94         else
95             $display( "PASSED: Simulation successful" ) ;
96
97         $stop ;
98     end
99
100     // Output comparison of simulated values versus known good values
101     always @ (posedge clock) begin
102         if( reset )
103             endofsim <= 1'b0 ;
104         else begin
105             if( !$feof(outfile) ) begin
106                 if( strobe_out ) begin
107                     ro = $fscanf( outfile, "%d\n", compare ) ;
108                     if( compare != data_out ) begin
109                         $display( "%t: %d != %d", $realtime, data_out, compare ) ;
110                         noe = noe + 1 ;
111                     end
112                 end
113             end else begin
114                 // Signal end of simulation when no more outputs
115                 endofsim <= 1'b1 ;
116             end
117         end
118     end     
119
120 endmodule