Always Block
Coddy Verilog 여정의 기초 섹션에 포함된 레슨. 90개 중 46번째.
procedural block은 C나 Python과 같은 소프트웨어 프로그래밍 언어에서처럼 문이 순서대로 하나씩 차례로 실행되는 코드 블록입니다. Verilog에는 두 가지 procedural block이 있습니다. initial(한 번 실행됨)과 always(계속 실행됨)입니다. 먼저 always 블록부터 살펴보겠습니다.
always block은 연속적으로 실행됩니다. 시뮬레이션이 시작되면 영원히 반복됩니다. 플립플롭, 카운터, 조합 논리처럼 계속 실행되어야 하는 하드웨어를 설명하는 데 사용됩니다.
기본 구문:
always @(sensitivity_list) begin
// 반복적으로 실행되는 코드
end@(sensitivity_list)은 block이 언제 실행될지를 알려 줍니다. 이것이 없고 내부에 #5와 같은 delay도 없다면, block은 무한히 loop를 반복하여 simulation을 멈추게 됩니다.
Always 블록 예제: Counter
always 블록을 사용하여 카운터를 만드는 방법의 예시는 다음과 같습니다.
module counter (
input clk,
output reg [3:0] count
);
always @(posedge clk) count <= count + 1;
endmodule이 코드가 작동하는 방식
| 부분 | 의미 |
|---|---|
always | 이 코드를 계속해서, 영원히 실행 |
@(posedge clk) | clock이 0에서 1로 바뀌는 것을 기다림 (rising edge) |
count <= count + 1 | count의 현재 값을 가져와 1을 더한 다음 다시 저장 |
block은 clock의 every rising edge마다 실행됩니다. 매번, count가 1 증가합니다.
민감도 목록 @(posedge clk)은 지속적으로 실행하는 것이 아니라 clock edge에서만 실행하도록 지시합니다. 이것이 없으면 loop는 지연 없이 영원히 실행됩니다.
여러 신호가 있는 always 블록
특정 신호를 나열할 수 있습니다:
always @(a or b) begin
out = a & b;
enda 또는 b가 changes될 때 실행됩니다.
챌린지
Add 누락된 always block을 추가하여 이 module이 작동하도록 하세요.
작동 방식:
- 각 rising clock edge에서
out1이 0에서 1 또는 1에서 0으로 toggles(반전됩니다) out2는out1을 follows(out1과 동일한 값을 가집니다)
할 일:
always @(posedge clk)block을 Add하세요- 그 안에서
out1이 toggles하도록 하세요(out1 = ~out1사용) out2를out1과 같게 만드세요
직접 해보기
module toggler (
input clk,
output reg out1,
output reg out2
);
initial begin
out1 = 0;
out2 = 0;
end
// TODO: posedge clk와 함께 always 블록 추가
// out1은 매 클럭마다 토글됨
// out2는 out1을 따름
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 컴파일러