Writing The Testbench
Part of the Fundamentals section of Coddy's Verilog journey. Lesson 45 of 90.
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, btoreg a, b(use the semicolon) - Change
output sum, carrytowire sum, carry(use the semicolon) - Remove the module ports entirely (the module should have no
( ))
Step 2: Add the test code
- Add an
initial beginblock - 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);
- Add
$finish;to end the test - Add
endto 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;
endmoduleAll lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorComparison OperatorsRecap - Simple MathBitwise Operators7Assign And Gates
Continuous AssignmentAssign With OperatorsBuilt In Gate PrimitivesAND OR NOT GatesXOR XNOR GatesRecap - Logic Gate Circuit10Decision Making
If StatementIf - ElseRecap - Simple ComparatorCase StatementCasex And CasezRecap - ALU Design5Operators Part 2
Logical OperatorsReduction OperatorsShift OperatorsConcatenation OperatorConditional OperatorRecap - Operator Challenge3Number System
Binary RepresentationSized NumbersUnsized NumbersNegative NumbersSpecial Values X And ZRecap - Number Formats6Modules
Module StructureInput And Output PortsInout PortsModule InstantiationPort Mapping By NamePort Mapping By OrderRecap - Build A Module9Procedural Blocks
Always BlockInitial BlockSensitivity ListBlocking AssignmentNon Blocking AssignmentRecap - Always vs Initial15Traffic Light Controller
Defining The StatesState Machine LogicPractice on your own: Online Verilog compiler