File size: 3,891 Bytes
035592e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
`timescale 1ns/1ns
`define clk_period 20
module tb_multi_pipe();
 
     reg [7:0] mul_a;
     reg [7:0] mul_b;
     reg mul_en_in;
 
     reg clk;
     reg rst_n;
 
     wire mul_en_out;
     wire [15:0] mul_out;

     reg [15:0] expected_product;
 
 multi_pipe_8bit u1(
          .clk(clk),      
          .rst_n(rst_n),       
          .mul_a(mul_a),       
          .mul_b(mul_b),
          .mul_en_in(mul_en_in),       
           
          .mul_en_out(mul_en_out),
          .mul_out(mul_out)    
);
 
 
 
initial clk = 1;
always # 10 clk = ~clk; 
integer error = 0; 
 
// ---------------- Additional checks ----------------
integer herr = 0, hpush = 0, hpop = 0, hk;
reg hon = 0, hmon = 0;
reg [15:0] hq [0:255];
always @(posedge rst_n) hon = 1;
always @(negedge clk) if (hon && mul_en_out === 1'b0 && mul_out !== 16'd0) begin
     herr = herr + 1;
     if (herr < 10) $display("HCHK idle: mul_en_out=0 but mul_out=%0d at %0t", mul_out, $time);
end
always @(posedge clk) if (hmon && mul_en_in === 1'b1) begin hq[hpush] = mul_a * mul_b; hpush = hpush + 1; end
always @(negedge clk) if (hmon && mul_en_out === 1'b1) begin
     if (hpop >= hpush || mul_out !== hq[hpop]) begin
          herr = herr + 1;
          $display("HCHK stream: result #%0d got %0d expected %0d", hpop, mul_out, hq[hpop]);
     end
     hpop = hpop + 1;
end
task h_stream;
     begin
          @(negedge clk);
          mul_en_in = 0;
          repeat (20) @(negedge clk);
          hmon = 1;
          for (hk = 0; hk < 160; hk = hk + 1) begin
               mul_a = $random; mul_b = $random;
               mul_en_in = (hk < 40) ? 1'b1 : $random;
               @(negedge clk);
          end
          mul_en_in = 0;
          repeat (20) @(negedge clk);
          hmon = 0;
          if (hpop != hpush) begin
               herr = herr + 1;
               $display("HCHK stream: %0d operand pairs accepted but %0d mul_en_out=1 cycles", hpush, hpop);
          end
     end
endtask

initial begin
     rst_n = 0;
     mul_a = 0;
     mul_b = 0;
     mul_en_in = 0;
 
     #(`clk_period*200+1);
 
     rst_n = 1;
     
     #(`clk_period*10);
 
     mul_a = 'd35;
     mul_b = 'd20;
     mul_en_in = 'd1;
     repeat (100) begin
          mul_en_in = 0;
          #(`clk_period*20);
          rst_n = 1;
          #(`clk_period*10);
          mul_a = $random;
          mul_b = $random;

          mul_en_in = 'd1;
          #(`clk_period);
          expected_product = mul_a * mul_b;
          #(`clk_period);
          error = ((mul_en_out==1) && (mul_out == expected_product)) ? error+1 : error;
          while (mul_en_out == 0) begin
                @(posedge clk); // Wait for one clock cycle
          end
          // //========it can be changed depending on your pipelining processing latency
          // #(`clk_period*4);
          // //========it can be changed depending on your pipelining processing latency
          error = (mul_out != expected_product) ? error+1 : error;
          // $display("mul_a = %d, mul_b = %d, mul_out = %d, expected = %d", mul_a, mul_b, mul_out, expected_product);
     end

     // Additional checks, after the original 100 checks:
     //  H1 "Otherwise, mul_out is set to 0": whenever mul_en_out is 0, mul_out is 0 (monitored at every falling
     //     clock edge from the first reset release to the end of the run, original stimulus included).
     //  H2 pipelining: 160 cycles of new random operands every cycle with a random mul_en_in pattern; every
     //     mul_en_out=1 cycle must show the product of the next accepted (mul_en_in=1) operand pair, in order.
     h_stream;
     error = error + herr;

     if (error == 0) begin
            $display("===========Your Design Passed===========");
     end
     else begin
     $display("===========Test completed with %d /100 failures===========", error);
     end
    
     $finish; 
   
end
 
endmodule