Testbench
Part of the Fundamentals section of Coddy's Verilog journey — lesson 90 of 90.
Challenge
A testbench provides inputs to your design and creates a waveform file. It has no ports of its own.
Your Task
Create a testbench that:
- Declares
regforclk,start, anddata_in(8 bits) - Declares
wirefortxandwire [3:0]forcnt - Instantiates the
uart_txmodule, connecting all ports:.clk,.start,.data_in,.tx,.cnt - Generates a clock (toggle every 5 time units)
- Inside an
initialblock:- Creates a waveform file named
uart.vcdusing$dumpfileand$dumpvars - Sets
clk = 0,start = 1,data_in = 8'b01000001at time 0 - Releases
startafter 10 time units (start = 0) - Runs for 200 time units
- Creates a waveform file named
After running the testbench, open the waveform to verify the tx signal.
Try it yourself
module uart_tx (
input clk,
input start,
input [7:0] data_in,
output reg tx,
output reg [3:0] cnt
);
reg [9:0] shift_reg;
initial begin
cnt = 0;
tx = 1;
shift_reg = 0;
end
always @(posedge clk) begin
if (cnt == 0 && start) begin
shift_reg <= {1'b1, data_in, 1'b0};
cnt <= 1;
end
else if (cnt > 0 && cnt < 9) begin
tx <= shift_reg[0];
shift_reg <= shift_reg >> 1;
cnt <= cnt + 1;
end
else if (cnt == 9) begin
tx <= shift_reg[0];
shift_reg <= shift_reg >> 1;
cnt <= 0;
end
end
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 Logic