Skip to content

Commit 471279b

Browse files
committed
Polished
1 parent 80efbc1 commit 471279b

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

combinational/alu_tb.sv

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ module alu_tb;
1111
localparam NUM_TESTS = 10000;
1212
localparam WIDTH = 8;
1313
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;
1616
logic [WIDTH-1:0] correct_out;
1717

1818
alu #(.WIDTH(WIDTH)) DUT (.*);
@@ -21,37 +21,37 @@ module alu_tb;
2121
function void check_flags();
2222
// Check negative flag.
2323
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]);
2525

2626
// Check zero flag.
2727
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);
2929

3030
// 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]);
3333
endfunction
3434

3535
initial begin
3636
$timeformat(-9, 0, " ns");
3737

3838
// Test NUM_TESTS random inputs and select values.
3939
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();
5353
end
5454

5555
$display("Tests completed.");
56-
end
56+
end
5757
endmodule

0 commit comments

Comments
 (0)