Menu
CoddyTech

测试平台

本课属于 Coddy Verilog 学习旅程的“基础知识”单元。第 90 课,共 90 课。

challenge icon

挑战

testbench 会为你的设计提供输入,并创建一个波形文件。它本身没有端口。

你的任务

创建一个 testbench,要求:

  1. 为 clk、start 和 data_in(8 位)声明 reg
  2. 为 tx 声明 wire,并为 cnt 声明 wire [3:0]
  3. 实例化 uart_tx 模块,并连接所有端口:.clk、.start、.data_in、.tx、.cnt
  4. 生成时钟(每 5 个时间单位切换一次)
  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 在线编译器