Comparison Operators
Coddy Verilog 여정의 기초 섹션에 포함된 레슨. 90개 중 21번째.
비교 연산자는 두 값을 비교하고 1(참) 또는 0(거짓)을 반환합니다.
사용 가능한 비교 연산자
| 연산자 | 의미 |
|---|---|
== | Equal과 같음 |
!= | Equal과 같지 않음 |
> | greater than |
< | less than |
>= | greater than 이상 |
<= | less than 이하 |
代码示例
module comparison_demo;
reg [3:0] a, b;
reg result;
initial begin
a = 5;
b = 3;
result = (a == b);
$display("5 == 3 : %d", result); // 0 (거짓)
result = (a != b);
$display("5 != 3 : %d", result); // 1 (참)
result = (a > b);
$display("5 > 3 : %d", result); // 1 (참)
result = (a < b);
$display("5 < 3 : %d", result); // 0 (거짓)
result = (a >= 5);
$display("5 >= 5 : %d", result); // 1 (참)
result = (a <= 3);
$display("5 <= 3 : %d", result); // 0 (거짓)
$finish;
end
endmodule출력:
5 == 3 : 0
5 != 3 : 1
5 > 3 : 1
5 < 3 : 0
5 >= 5 : 1
5 <= 3 : 0조건에서 비교 사용하기
비교는 if 문에서 자주 사용됩니다:
if (count == 10)
$display("Reached maximum");
if (value > threshold)
$display("Value is too high");중요 참고 사항
- 비교 결과는 1비트 값입니다(0 또는 1).
- 비교는 모든 비트 폭에서 작동합니다.
==및!=은 신호에 X 또는 Z가 포함된 경우 주의해서 사용하세요(이들은 X를 반환합니다).
챌린지
각 작업에 맞는 올바른 비교 표현을 작성하세요.
할 일:
a가b와 같은지 확인하고eq에 저장하세요a가b보다 큰지 확인하고gt에 저장하세요a가b보다 작거나 같은지 확인하고le에 저장하세요
직접 해보기
module comparison_challenge;
reg [3:0] a, b;
reg eq, gt, le;
initial begin
a = 4'd7;
b = 4'd7;
eq = ______; // a는 b와 같다
gt = ______; // a는 b보다 크다
le = ______; // a는 b보다 작거나 같다
$display("a = %d, b = %d", a, b);
$display("a == b : %d", eq);
$display("a > b : %d", gt);
$display("a <= b : %d", le);
$finish;
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 컴파일러