State Machine Logic
Parte da seção Fundamentos do Journey de Verilog da Coddy. Lição 80 de 90.
Desafio
Nesta lição, você adicionará a lógica de transição de state ao controlador do semáforo. A máquina de estados determina quando passar de um state para o próximo.
A lógica da máquina de estados controla quando e como o semáforo muda de state.
Sequência de estados
Verde → Amarelo → Vermelho → Verde → …
Sua tarefa é adicionar a lógica da máquina de estados ao módulo.
O que fazer:
- Add um bloco
always @(posedge clk or posedge reset) - No reset, defina
statecomo Vermelho (2) - Quando
nextfor 1, avance para o próximo state:- Se o state for Verde (0): change para Amarelo
- Se o state for Amarelo (1): change para Vermelho
- Se o state for Vermelho (2): change para Verde
Experimente você mesmo
module traffic_light (
input clk,
input reset,
input next, // Gatilho para mudar o estado
output reg red,
output reg yellow,
output reg green
);
reg [1:0] state;
// Atribuições de saída
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: Adicionar lógica da máquina de estados (sem temporização)
// 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
endmoduleTodas as lições de Fundamentos
1Introdução
O que é VerilogHardware vs. SoftwareSeu primeiro móduloComentáriosNíveis de abstração de projeto4Operators 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 LogicPratique por conta própria: Compilador de Verilog online