Menu
Coddy logo textTech

Writing The Testbench

Coddy Verilog 여정의 기초 섹션에 포함된 레슨. 90개 중 82번째.

challenge icon

챌린지

이 레슨에서는 traffic light 컨트롤러가 올바르게 작동하는지 확인하기 위한 testbench를 작성합니다.

할 일:

다음 작업을 수행하는 testbench를 작성하세요:

  1. 신호를 선언합니다 (clk와 reset에는 reg, red, yellow, green에는 wire 사용)
  2. traffic_light module을 uut이라는 이름으로 인스턴스화합니다
  3. 1 time unit마다 토글되는 clock을 생성합니다
  4. 2 time units 동안 reset을 적용한 다음 해제합니다
  5. simulation을 100 time units 동안 실행합니다

simulation은 출력되는 내용을 기준으로 확인되므로, initial block에서도 정확히 다음 줄을 출력해야 합니다:

  1. $display를 사용하여 clock과 reset이 설정되기 전에 Traffic Light Test를 출력합니다
  2. 100 time units 후에 $display를 사용하여 Test complete를 출력합니다
  3. output에 자체 줄을 추가하는 $finish로 simulation을 end합니다

직접 해보기

module traffic_light (
  input clk,
  input reset,
  output reg red,
  output reg yellow,
  output reg green
);

  reg [1:0] state;
  reg [5:0] counter;

  // 출력 할당
  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

  // 타이밍이 있는 상태 머신
  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: clk와 reset을 위한 reg 선언
  
  // TODO: red, yellow, green을 위한 wire 선언
  

  // TODO: uut라는 이름으로 traffic_light 모듈 인스턴스화
  // clk, reset, red, yellow, green 연결
  

  // TODO: 클록 생성 (1 시간 단위마다 토글)
  

  initial begin
    $display("Traffic Light Test");
    
    // TODO: clk를 0으로 초기화
    
    // TODO: 리셋 적용 (2 시간 단위 동안 reset=1, 이후 reset=0)
    
    
    // TODO: 100 시간 단위 동안 시뮬레이션 실행
    
    
    $display("Test complete");
    $finish;
  end
endmodule

기초의 모든 레슨

직접 연습해 보세요: 온라인 Verilog 컴파일러