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