@@ -11,8 +11,8 @@ module alu_tb;
11
11
localparam NUM_TESTS = 10000 ;
12
12
localparam WIDTH = 8 ;
13
13
logic [WIDTH - 1 : 0 ] in0, in1, out;
14
- logic [1 : 0 ] sel;
15
- logic neg, pos, zero;
14
+ logic [1 : 0 ] sel;
15
+ logic neg, pos, zero;
16
16
logic [WIDTH - 1 : 0 ] correct_out;
17
17
18
18
alu # (.WIDTH (WIDTH )) DUT (.* );
@@ -21,37 +21,37 @@ module alu_tb;
21
21
function void check_flags ();
22
22
// Check negative flag.
23
23
if (correct_out[WIDTH - 1 ] != neg)
24
- $display (" ERROR (time %0t ): neg = %b instead of %b ." , $realtime , neg, correct_out[WIDTH - 1 ]);
24
+ $display (" ERROR (time %0t ): neg = %b instead of %b ." , $realtime , neg, correct_out[WIDTH - 1 ]);
25
25
26
26
// Check zero flag.
27
27
if ((correct_out == 0 ) != zero)
28
- $display (" ERROR (time %0t ): zero = %b instead of %b ." , $realtime , zero, correct_out == 0 );
28
+ $display (" ERROR (time %0t ): zero = %b instead of %b ." , $realtime , zero, correct_out == 0 );
29
29
30
30
// Check pos flag.
31
- if (((correct_out != 0 ) && ! correct_out[ WIDTH - 1 ] ) != pos)
32
- $display (" ERROR (time %0t ): pos = %b instead of %b ." , $realtime , pos, correct_out != 0 && ! correct_out[WIDTH - 1 ]);
31
+ if ((signed ' (correct_out) > 0 ) != pos)
32
+ $display (" ERROR (time %0t ): pos = %b instead of %b ." , $realtime , pos, correct_out != 0 && ! correct_out[WIDTH - 1 ]);
33
33
endfunction
34
34
35
35
initial begin
36
36
$timeformat (- 9 , 0 , " ns" );
37
37
38
38
// Test NUM_TESTS random inputs and select values.
39
39
for (int i= 0 ; i < NUM_TESTS ; i++ ) begin
40
- in0 = $random ;
41
- in1 = $random ;
42
- sel = $random ;
43
- # 10 ;
44
- if (sel == 2'b00 ) correct_out = in0 + in1;
45
- else if (sel == 2'b01 ) correct_out = in0 - in1;
46
- else if (sel == 2'b10 ) correct_out = in0 & in1;
47
- else if (sel == 2'b11 ) correct_out = in0 | in1;
48
-
49
- if (out != correct_out)
50
- $display (" ERROR (time %0t ): out = %h instead of %h ." , $realtime , out, correct_out);
51
-
52
- if (sel == 2'b00 || sel == 2'b01 ) check_flags ();
40
+ in0 = $random ;
41
+ in1 = $random ;
42
+ sel = $random ;
43
+ # 10 ;
44
+ if (sel == 2'b00 ) correct_out = in0 + in1;
45
+ else if (sel == 2'b01 ) correct_out = in0 - in1;
46
+ else if (sel == 2'b10 ) correct_out = in0 & in1;
47
+ else if (sel == 2'b11 ) correct_out = in0 | in1;
48
+
49
+ if (out != correct_out)
50
+ $display (" ERROR (time %0t ): out = %h instead of %h ." , $realtime , out, correct_out);
51
+
52
+ if (sel == 2'b00 || sel == 2'b01 ) check_flags ();
53
53
end
54
54
55
55
$display (" Tests completed." );
56
- end
56
+ end
57
57
endmodule
0 commit comments