Verifying The Output
جزء من قسم الأساسيات في رحلة Verilog على Coddy. الدرس 83 من 90.
التحدي
في هذا الدرس، ستضيف أوامر تفريغ الموجات وتتحقق من أن وحدة التحكم في إشارة المرور تعمل بشكل صحيح.
ما يجب فعله:
حدّث testbench من أجل:
- أضف
$dumpfileلإنشاء ملف موجات باسمtraffic.vcd - أضف
$dumpvarsلتفريغ جميع الإشارات فيtestbench - شغّل المحاكاة وتحقق من الموجة
جرّب بنفسك
module traffic_light (
input clk,
input reset,
output reg red,
output reg yellow,
output reg green
);
// الحالات: 0=أخضر، 1=أصفر، 2=أحمر
reg [1:0] state;
reg [5:0] counter;
always @(posedge clk or posedge reset) begin
if (reset) begin
state <= 2; // ابدأ عند الأحمر
counter <= 0;
end else begin
if (counter == 0) begin
// غيّر الحالة
if (state == 0) begin // أخضر -> أصفر
state <= 1;
counter <= 10; // الأصفر يستمر 10 ثوانٍ
end else if (state == 1) begin // أصفر -> أحمر
state <= 2;
counter <= 40; // الأحمر يستمر 40 ثانية
end else begin // أحمر -> أخضر
state <= 0;
counter <= 30; // الأخضر يستمر 30 ثانية
end
end else begin
counter <= counter - 1;
end
end
end
// منطق الإخراج
always @(*) begin
red = (state == 2);
yellow = (state == 1);
green = (state == 0);
end
endmodule
module testbench;
reg clk, reset;
wire red, yellow, green;
traffic_light uut (
.clk(clk),
.reset(reset),
.red(red),
.yellow(yellow),
.green(green)
);
always #1 clk = ~clk;
initial begin
// TODO: أضف $dumpfile لإنشاء "traffic.vcd"
// TODO: أضف $dumpvars لتفريغ جميع الإشارات (0, testbench)
$display("Traffic Light Test");
$monitor("Time %0t: red=%b, yellow=%b, green=%b", $time, red, yellow, green);
clk = 0;
reset = 1;
#2 reset = 0;
#90;
$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 عبر الإنترنت