测试平台
本课属于 Coddy Verilog 学习旅程的“基础知识”单元。第 90 课,共 90 课。
挑战
testbench 会为你的设计提供输入,并创建一个波形文件。它本身没有端口。
你的任务
创建一个 testbench,要求:
- 为
clk、start和data_in(8 位)声明reg - 为
tx声明wire,并为cnt声明wire [3:0] - 实例化
uart_tx模块,并连接所有端口:.clk、.start、.data_in、.tx、.cnt - 生成时钟(每 5 个时间单位切换一次)
- 在
initial块中:- 使用
$dumpfile和$dumpvars创建名为uart.vcd的波形文件 - 在时间 0 设置
clk = 0、start = 1、data_in = 8'b01000001 - 经过 10 个时间单位后释放
start(start = 0) - 然后再等待 200 个时间单位,并调用
$finish(这样仿真会在时间 210 结束)
- 使用
运行 testbench 后,打开波形并检查 tx 信号。
亲手试试
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
endmodule基础知识中的所有课
自己练习: Verilog 在线编译器