Comparison Operators
جزء من قسم الأساسيات في رحلة Verilog على Coddy. الدرس 21 من 90.
تقارن معاملات المقارنة بين قيمتين وتُرجع إما 1 (صحيح) أو 0 (خطأ).
معاملات المقارنة المتاحة
| المعامل | المعنى |
|---|---|
== | يساوي |
!= | لا يساوي |
> | 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");ملاحظات مهمة
- نتائج المقارنة هي قيم ذات بت واحد (0 أو 1)
- تعمل المقارنات مع أي عرض بتات
- انتبه عند استخدام
==و!=عندما تحتوي الإشارات على X أو Z (فستُرجع X)
التحدي
اكتب تعبيرات المقارنة الصحيحة لكل مهمة.
ما يجب فعله:
- تحقق مما إذا كانت
aequalsbوخزّن النتيجة فيeq - تحقق مما إذا كانت
agreater thanbوخزّن النتيجة فيgt - تحقق مما إذا كانت
aless than or equal tobوخزّن النتيجة في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 عبر الإنترنت