Comparison Operators
CoddyのVerilogジャーニー「基礎」セクションの一部。レッスン 21/90。
比較演算子は2つの値を比較し、1(true)または0(false)を返します。
利用可能な比較演算子
| 演算子 | 意味 |
|---|---|
== | 等しい |
!= | 等しくない |
> | より大きい |
< | より小さい |
>= | 以上 |
<= | 以下 |
コード例
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と equals か確認し、eqに保存するaがbより greater than か確認し、gtに保存するaがbより less than or equal to か確認し、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オンラインコンパイラ