学习:b站视频
更新:2022/9/2, 新增检测方法
状态机检测
简要概括三段式状态,使用三个always
块描述
- 使用同步时序逻辑,描述状态转移
- 使用组合逻辑,判断状态转移条件,描述状态转移规律
- 使用同步时序逻辑,描述没端口的输出行为
题目如下:
要求检测11010
字符串,检测到完整序列时,dout
输出1
,否则为0
状态图:
FSM
代码:
///check state 11010
module FSM(
input clk,
input rst_n,
input din,
output reg dout
);
localparam
IDLE = 6'h1,
A = 6'h2,
B = 6'h3,
C = 6'h4,
D = 6'h5,
E = 6'h6;
reg[5:0] cur_state, nex_state;
always@(posedge clk or negedge rst_n) begin
if(!rst_n)
cur_state <= IDLE;
else
cur_state <= nex_state; //状态打一拍寄存,时序逻辑,非阻塞赋值
end
always@(*) begin
case(cur_state) //组合逻辑判断条件,打一拍寄存后的状态,以及输入信号
IDLE: if(din == 1'b1) nex_state = A; //状态跳转,组合逻辑,使用阻塞赋值
else nex_state = IDLE;
A: if(din == 1'b1) nex_state = B;
else nex_state = IDLE;
B: if(din == 1'b1) nex_state = B;
else nex_state <= C;
C: if(din == 1'b1) nex_state = D;
else nex_state = IDLE;
D: if(<