State Machine Logic
CoddyのVerilogジャーニー「基礎」セクションの一部。レッスン 80/90。
チャレンジ
このレッスンでは、信号機コントローラーにstate transition logicを追加します。state machineは、ある状態から次の状態へいつ移行するかを決定します。
state machineのロジックは、信号機がwhen、how状態を変更するかを制御します。
状態のシーケンス
Green → Yellow → Red → Green → …
あなたの課題は、state machineのロジックをmoduleに追加することです。
実行すること:
always @(posedge clk or posedge reset)ブロックをAddする- reset時に、
stateをRed (2)に設定する nextが1のとき、次のstateへ移動する:- stateがGreen (0)の場合: Yellowにchangeする
- stateがYellow (1)の場合: Redにchangeする
- stateがRed (2)の場合: Greenに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オンラインコンパイラ