State Machine Logic
جزء من قسم الأساسيات في رحلة Verilog على Coddy. الدرس 80 من 90.
التحدي
في هذا الدرس، ستضيف منطق انتقال state إلى وحدة تحكم إشارة المرور. تحدد state machine متى تنتقل من حالة إلى الحالة التالية.
يتحكم منطق state machine في متى وكيف تغيّر إشارة المرور حالاتها.
تسلسل state
أخضر → أصفر → أحمر → أخضر → …
مهمتك هي Add منطق state machine إلى module.
ما يجب فعله:
- Add كتلة
always @(posedge clk or posedge reset) - عند reset، عيّن
stateإلى الأحمر (2) - عندما تكون قيمة
nextهي 1، انتقل إلى الحالة التالية:- إذا كانت state هي الأخضر (0): change إلى الأصفر
- إذا كانت state هي الأصفر (1): change إلى الأحمر
- إذا كانت state هي الأحمر (2): change إلى الأخضر
جرّب بنفسك
module traffic_light (
input clk,
input reset,
input next, // مشغّل لتغيير الحالة
output reg red,
output reg yellow,
output reg green
);
reg [1:0] state;
// تعيينات المخرجات
always @(*) begin
case (state)
0: begin green = 1; yellow = 0; red = 0; end
1: begin green = 0; yellow = 1; red = 0; end
2: begin green = 0; yellow = 0; red = 1; end
default: begin green = 0; yellow = 0; red = 1; end
endcase
end
// TODO: أضف منطق آلة الحالة (بدون توقيت)
// always @(posedge clk or posedge reset) begin
// if (reset) begin
// state <= 2;
// end else if (next) begin
// case (state)
// 0: state <= 1;
// 1: state <= 2;
// 2: state <= 0;
// endcase
// end
// 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 عبر الإنترنت