Menu
Coddy logo textTech

Writing The Testbench

Part of the Fundamentals section of Coddy's Verilog journey — lesson 45 of 90.

challenge icon

Challenge

Now we need to test if the half adder is working correctly. Add the test code inside the initial block.

Important: Before adding the test, you must change the module ports so the test can work properly.

What to do:

Step 1: Change the port declarations

  • Change input a, b to reg a, b (use the semicolon)
  • Change output sum, carry to wire sum, carry (use the semicolon)
  • Remove the module ports entirely (the module should have no ( ))

Step 2: Add the test code

  1. Add an initial begin block
  2. Inside the block, add:
$display("a b | sum carry"); 
a = 0; b = 0; #1 $display("%d %d |  %d    %d", a, b, sum, carry); 
a = 0; b = 1; #1 $display("%d %d |  %d    %d", a, b, sum, carry); 
a = 1; b = 0; #1 $display("%d %d |  %d    %d", a, b, sum, carry); 
a = 1; b = 1; #1 $display("%d %d |  %d    %d", a, b, sum, carry);
    
  1. Add $finish; to end the test
  2. Add end to close the initial block

Try it yourself

module half_adder (
  input a,
  input b,
  
  output sum,
  output carry
);
  assign sum = a ^ b;
  assign carry = a & b;
  
endmodule

All lessons in Fundamentals