Writing The Testbench
Parte de la sección Fundamentos del Journey de Verilog de Coddy. Lección 82 de 90.
Desafío
En esta lección, crearás un testbench para verificar que el controlador del semáforo funciona correctamente.
Qué hacer:
Crea un testbench que:
- Declare señales (
regpara clk y reset,wirepara red, yellow, green) - Instancie el módulo
traffic_lightcon el nombreuut - Genere un clock que cambie de estado cada 1 unidad de time
- Aplique reset durante 2 unidades de time y después lo libere
- Ejecute la simulation durante 100 unidades de time
La simulation se comprueba mediante lo que imprime, por lo que el bloque initial también debe producir exactamente estas líneas:
- Imprima
Traffic Light Testcon$displayantes de configurar el clock y el reset - Después de las 100 unidades de time, imprima
Test completecon$display - Finalice la simulation con
$finish, que añade su propia línea al output
Pruébalo tú mismo
module traffic_light (
input clk,
input reset,
output reg red,
output reg yellow,
output reg green
);
reg [1:0] state;
reg [5:0] counter;
// Asignaciones de salida
always @(*) begin
case (state)
0: begin green = 1; yellow = 0; red = 0; end
1: begin green = 0; yellow = 1; red = 0; end
2: begin green = 0; yellow = 0; red = 1; end
default: begin green = 0; yellow = 0; red = 1; end
endcase
end
// Máquina de estados con temporización
always @(posedge clk or posedge reset) begin
if (reset) begin
state <= 2;
counter <= 0;
end else begin
if (counter == 0) begin
case (state)
0: begin
state <= 1;
counter <= 10;
end
1: begin
state <= 2;
counter <= 40;
end
2: begin
state <= 0;
counter <= 30;
end
endcase
end else begin
counter <= counter - 1;
end
end
end
endmodule
module testbench;
// TODO: Declarar reg para clk y reset
// TODO: Declarar wire para red, yellow, green
// TODO: Instanciar el módulo traffic_light con el nombre uut
// Conectar clk, reset, red, yellow, green
// TODO: Generar el reloj (alternar cada 1 unidad de tiempo)
initial begin
$display("Traffic Light Test");
// TODO: Inicializar clk a 0
// TODO: Aplicar reset (reset=1 durante 2 unidades de tiempo, luego reset=0)
// TODO: Ejecutar la simulación durante 100 unidades de tiempo
$display("Test complete");
$finish;
end
endmoduleTodas las lecciones de Fundamentos
1Introducción
Qué es VerilogHardware frente a softwareTu primer móduloComentariosNiveles de abstracción del diseño4Operators 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 LogicPractica por tu cuenta: Compilador de Verilog online