Timing The Transitions
Coddy Verilog 여정의 기초 섹션에 포함된 레슨. 90개 중 81번째.
챌린지
이 레슨에서는 각 불이 켜져 있는 시간을 제어하는 counter 로직을 추가합니다. counter는 미리 설정된 값에서 0까지 카운트다운한 다음, 다음 상태 변경을 트리거합니다.
timing 요구 사항
| State | Duration | Counter Value |
|---|---|---|
| Green | 30 seconds | 30 |
| Yellow | 10 seconds | 10 |
| Red | 40 seconds | 40 |
counter 작동 방식
- 상태가 시작되면 counter에 duration 값이 로드됩니다
- 각 클록 틱마다 counter가 1씩 감소합니다
- counter가 0에 도달하면 다음 상태로 변경할 시간입니다
여러분의 과제는 상태 머신에 누락된 counter 로직을 추가하는 것입니다.
할 일:
reg [5:0] counter선언을 추가합니다- 리셋 시
counter를 0으로 설정합니다 counter == 0일 때:- 다음 상태의 duration을 counter에 로드합니다
- 다음 상태로 변경합니다
- 그렇지 않으면 각 클록마다 counter를 1씩 감소시킵니다
직접 해보기
module traffic_light (
input clk,
input reset,
output reg red,
output reg yellow,
output reg green
);
reg [1:0] state;
// TODO: 카운터 레지스터 선언 (6비트)
// 출력 할당
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; // Red에서 시작
// TODO: 카운터를 0으로 설정
end else begin
if (counter == 0) begin
case (state)
0: begin
state <= 1;
// TODO: Yellow용 카운터 로드 (10초)
end
1: begin
state <= 2;
// TODO: Red용 카운터 로드 (40초)
end
2: begin
state <= 0;
// TODO: Green용 카운터 로드 (30초)
end
endcase
end else begin
// TODO: 카운터를 1 감소
end
end
end
endmodule기초의 모든 레슨
4Operators 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 Logic직접 연습해 보세요: 온라인 Verilog 컴파일러