Menu
Coddy logo textTech

Testbench

Fa parte della sezione Fondamenti del percorso Verilog di Coddy. Lezione 90 di 90.

challenge icon

Sfida

Un testbench fornisce input al tuo progetto e crea un file della forma d’onda. Non ha porte proprie.

Il tuo compito

Crea un testbench che:

  1. Dichiara reg per clk, start e data_in (8 bit)
  2. Dichiara wire per tx e wire [3:0] per cnt
  3. Istanzia il modulo uart_tx, collegando tutte le porte: .clk, .start, .data_in, .tx, .cnt
  4. Genera un clock (commuta ogni 5 unità di tempo)
  5. All’interno di un blocco initial:
    • Crea un file della forma d’onda denominato uart.vcd usando $dumpfile e $dumpvars
    • Imposta clk = 0, start = 1, data_in = 8'b01000001 al tempo 0
    • Disattiva start dopo 10 unità di tempo (start = 0)
    • Poi attende altre 200 unità di tempo e chiama $finish (così la simulazione termina al tempo 210)

Dopo aver eseguito il testbench, apri la forma d’onda per verificare il segnale tx.

Provalo tu

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

Tutte le lezioni di Fondamenti

Esercitati da solo: Compilatore Verilog online