Merged r9433:9527 from features/gr-usrp2 into trunk. Adds usrp2 and gr-usrp2 top...
[debian/gnuradio] / usrp2 / fpga / sdr_lib / small_hb_dec_tb.v
1 module hb_dec_tb( ) ;
2    
3     // Parameters for instantiation
4     parameter               clocks  = 9'd2 ; // Number of clocks per input
5     parameter               decim   = 1 ; // 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    initial
17      begin
18         $dumpfile("hb_dec_tb.vcd");
19         $dumpvars(0,hb_dec_tb);
20      end
21    
22     // Setup the clock
23     initial clock = 1'b0 ;
24     always #5 clock <= ~clock ;
25
26     // Come out of reset after a while
27     initial reset = 1'b1 ;
28     initial #1000 reset = 1'b0 ;
29
30     // Enable the entire system
31     initial enable = 1'b1 ;
32
33     // Instantiate UUT
34    /*
35     halfband_ideal 
36       #(
37         .decim      ( decim         ),
38         .rate       ( rate          )
39       ) uut(
40         .clock      ( clock         ),
41         .reset      ( reset         ),
42         .enable     ( enable        ),
43         .strobe_in  ( strobe_in     ),
44         .data_in    ( data_in       ),
45         .strobe_out ( strobe_out    ),
46         .data_out   ( data_out      )
47       ) ;
48     */
49
50    
51    small_hb_dec #(.WIDTH(18)) uut
52      (.clk(clock),.rst(reset),.bypass(0),.stb_in(strobe_in),.data_in(data_in),
53       .stb_out(strobe_out),.data_out(data_out) );
54    
55     integer i, ri, ro, infile, outfile ;
56
57    always @(posedge clock)
58      begin
59         if(strobe_out)
60           $display(data_out);
61      end
62    
63     // Setup file IO
64     initial begin
65         infile = $fopen("input.dat","r") ;
66         outfile = $fopen("output.dat","r") ;
67         $timeformat(-9, 2, " ns", 10) ;
68     end
69
70     reg                 endofsim ;
71     reg signed  [17:0]  compare ;
72     integer             noe ;
73     initial             noe = 0 ;
74
75     initial begin
76         // Initialize inputs
77         strobe_in <= 1'd0 ;
78         data_in <= 18'd0 ;
79
80         // Wait for reset to go away
81         @(negedge reset) #0 ;
82         
83         // While we're still simulating ...
84         while( !endofsim ) begin
85
86             // Write the input from the file or 0 if EOF...
87             @( posedge clock ) begin
88                //#1 ;
89                 strobe_in <= 1'b1 ;
90                 if( !$feof(infile) )
91                     ri = $fscanf( infile, "%d", data_in ) ;
92                 else
93                     data_in <= 18'd0 ;
94             end
95
96             // Clocked in - set the strobe to 0 if the number of
97             // clocks per sample is greater than 1
98             if( clocks > 1 ) begin
99                 @(posedge clock) begin
100                     strobe_in <= 1'b0  ;
101                 end
102
103                 // Wait for the specified number of cycles
104                 for( i = 0 ; i < (clocks-2) ; i = i + 1 ) begin
105                     @(posedge clock) #1 ;
106                 end
107             end
108         end
109
110         // Print out the number of errors that occured
111         if( noe )
112             $display( "FAILED: %d errors during simulation", noe ) ;
113         else
114             $display( "PASSED: Simulation successful" ) ;
115
116         $finish ;
117     end
118
119    // Output comparison of simulated values versus known good values
120    always @ (posedge clock) begin
121       if( reset )
122         endofsim <= 1'b0 ;
123       else begin
124          if( !$feof(outfile) ) begin
125             if( strobe_out ) begin
126                ro = $fscanf( outfile, "%d\n", compare ) ;
127                if( compare != data_out ) begin
128                   //$display( "%t: %d != %d", $realtime, data_out, compare ) ;
129                   noe = noe + 1 ;
130                end
131             end
132          end else begin
133             // Signal end of simulation when no more outputs
134             endofsim <= 1'b1 ;
135          end
136       end
137    end     
138
139 endmodule // hb_dec_tb
140