我想知道是否有人可以阐明如何在 verilog 中编写一个 LED 模式 fsm,它在 8 个 LED 上产生 4 种不同的模式,并且 LED 会改变每个滴答脉冲,有 4 个按钮可以触发 4 种不同的模式,每种模式将触发 8 个 LED 以模式移动,即从左到右,从右到左。
我写了一个顺序逻辑,但我不知道如何将 LED 的模式插入到每个状态中。这是我的代码:
`timescale 1ns / 1ps
module pattern_fsm(
input [3:0] mode,
input tick,
input clk,
input reset,
output reg [7:0] Led
);
reg [3:0] state, nextstate;
parameter s0 = 4'b0001;
parameter s1 = 4'b0010;
parameter s2 = 4'b0100;
parameter s3 = 4'b1000;
always @(posedge clk, posedge reset)
if(reset)
state <= s0;
else
state <= nextstate;
always @(*)
begin
case(state)
s0: if(mode == 4'b0001) nextstate = s0;
else nextstate = s3;
s1: if(mode == 4'b0010) nextstate = s1;
else nextstate = s0;
s2: if(mode == 4'b0100) nextstate = s2;
else nextstate = s1;
s3: if(mode == 4'b1000) nextstate = s3;
else nextstate = s2;
default: nextstate = s0;
endcase
end
always @(state)
begin
case(state)
s0: Led = 8'b00000001;
s1: Led = 8'b00000010;
s2: Led = 8'b00000011;
s3: Led = 8'b00000100;
endcase
end
endmodule