Verilog_RTL 설계/Basys_3

[Sequential Circuit] Down_Counter

juniha 2025. 7. 7. 19:18

목적

  • 클럭의 상승엣지에 맞춰 동작하는 카운터로, 카운트 값이 매번 1씩 중러드는 것을 확인

 

사용 코드_Down_counter_asyc

module down_counter_asyc (
    input clk,          // 기본 클럭 입력 (LSB 비트의 클럭으로 사용)
    input reset_p,      // 비동기 리셋 입력 (posedge일 때 FF 초기화)
    output [3:0] count  // 4비트 카운터 출력
);
    
    // LSB 비트 (count[0])를 위한 T 플립플롭
    T_flip_flop_p T0(
        .clk(clk),          // 외부 클럭 입력
        .reset_p(reset_p),  // 비동기 리셋
        .t(1),              // 항상 토글되도록 t = 1 고정
        .q(count[0])        // 출력 연결
    );

    // count[1]은 count[0]의 출력을 클럭으로 사용 (비동기 방식)
    T_flip_flop_p T1(
        .clk(count[0]),     // 이전 비트의 출력이 클럭
        .reset_p(reset_p),  
        .t(1),              
        .q(count[1])
    );
        
    // count[2]는 count[1]의 출력을 클럭으로 사용
    T_flip_flop_p T2(
        .clk(count[1]),     
        .reset_p(reset_p),
        .t(1),
        .q(count[2])
    );           

    // count[3]은 count[2]의 출력을 클럭으로 사용
    T_flip_flop_p T3(
        .clk(count[2]),     
        .reset_p(reset_p),
        .t(1),
        .q(count[3])
    );
       
endmodule

 

사용 코드_

module down_counter_p(
    input clk,            // 클럭 입력 (양의 에지에서 동작)
    input reset_p,        // 비동기 리셋 입력 (양의 에지에서 즉시 count 초기화)
    input enable,         // 카운트 활성화 신호 (1일 때만 카운트 다운 수행)
    output reg [3:0] count // 4비트 다운 카운터 출력
);
    
    // 항상 블록: clk 또는 reset_p의 상승 에지에 반응
    always @(posedge clk, posedge reset_p) begin
        if (reset_p)
            count = 0;         // 리셋 신호가 들어오면 카운터를 0으로 초기화
        else if (enable)
            count = count - 1; // enable이 1일 때 카운터 값을 1 감소
        // enable이 0일 때는 이전 count 값을 유지 (아무 변화 없음)
    end

endmodule

 

Schematic

Simulation

 

 

결론

  • enable 신호가 1일 때마다 클럭에 맞춰 1씩 줄어드는 4비트 카운터이다.
  • reset_p 를 통해 언제든지 카운트를 0으로 초기화 할 수 있다