状态机 三段式中第三段用组合逻辑还是时序逻辑
这个问题我一直都是混着用的有时候用组合逻辑有时候使用时序逻辑。
个人感觉没啥区别,自己用的时候别用混
用组合逻辑,一般要用当前状态判断(current_state)
用时序逻辑,一般要用下个状态来判断(next_state)
下面看一段三段式的Verilog代码
要求无重叠检测序列01010,用状态机实现。
module seq_detect(
input clk,
input rst_n,
input seq_in,
output reg out
);
//detect sequence 01010
//first step: define parameter and state
parameter s0 = 6'b0000_01, s1 = 6'b0000_10, s2 = 6'b0001_00, s3 = 6'b0010_00,
s4 = 6'b0100_00, s5 = 6'b1000_00;
reg [5:0] cur_state, nxt_state;
//三段式状态机
//第一段:时序逻辑
always@(posedge clk or negedge rst_n) begin
if(~rst_n) cur_state <= s0;
else cur_state <= nxt_state;
end
//第二段:组合逻辑
always@(*) begin
nxt_state = s0;
case(cur_state)
s0: if(seq_in == 0) nxt_state = s1;
else nxt_state = s0;