Menu
Coddy logo textTech

Testbench

Parte da seção Fundamentos do Journey de Verilog da Coddy — lição 90 de 90.

challenge icon

Desafio

Um testbench fornece entradas para o seu projeto e cria um arquivo de forma de onda. Ele não possui portas próprias.

Sua Tarefa

Crie um testbench que:

  1. Declare reg para clk, start e data_in (8 bits)
  2. Declare wire para tx e wire [3:0] para cnt
  3. Instancie o módulo uart_tx, conectando todas as portas: .clk, .start, .data_in, .tx, .cnt
  4. Gere um clock (alternando a cada 5 unidades de tempo)
  5. Dentro de um bloco initial:
    • Crie um arquivo de forma de onda chamado "uart.vcd" usando $dumpfile e $dumpvars
    • Defina clk = 0, start = 1, data_in = 8'b01000001 no tempo 0
    • Libere start após 10 unidades de tempo (start = 0)
    • Execute por 200 unidades de tempo

Após executar o testbench, abra a forma de onda para verificar o sinal tx.

Experimente você mesmo

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

Todas as lições de Fundamentos