State Machine
CoddyのVerilogジャーニー「基礎」セクションの一部。レッスン 88/90。
チャレンジ
ステートマシンは、複数の state のいずれかになれる回路です。UART では、各 bit に対して異なる state があります:idle、start、data bits 0-7、stop です。bit カウンター(cnt)によって、現在どの state にいるかがわかります。cnt に基づいて、tx line に送信する値を決定します。
前のレッスンで使用した bit カウンターがあります。これを UART transmitter として動作するように変更する必要があります。
送信する Bit の値(文字 'A' の場合)
| cnt | tx の値 |
|---|---|
| 0 | 1 |
| 1 | 0 |
| 2 | 1 |
| 3 | 0 |
| 4 | 0 |
| 5 | 0 |
| 6 | 0 |
| 7 | 0 |
| 8 | 1 |
| 9 | 0 |
| 10 | 1 |
実行すること
startという input を追加するtxという output reg を追加するinitialblock で、tx = 1(idle HIGH)を設定する- カウンターの logic を変更する:
cnt == 0かつstart == 1の場合、cnt <= 1を設定する(transmitting を開始)cntが 1 から 9 の間の場合、インクリメントする:cnt <= cnt + 1cnt == 10の場合、0に reset する
自分で試してみよう
module uart_tx (
input clk,
output reg [3:0] cnt
);
initial begin
cnt = 0;
end
always @(posedge clk) begin
cnt <= cnt + 1;
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オンラインコンパイラ