|
常用的状态机分为一段式、二段式、三段式。
大家对于一段式的写法并不陌生,初学fpga的人都经常用一段式去写程序,
三段式状态机的原理和源码,大家都可以在网上找到。网上的程序都是完整的程序,
看起来比较啰嗦和麻烦,我这里只是简单的把三段式用代码的形式概括一下。
1.把下一状态给当前状态
always@(posedge clk, posedge rst)
begin
if(rst)
state <= S_IDLE;
else
state <= next_state;
end
2.根据条件给下一状态赋值
always @(*)
begin
case(state)
S_IDLE: begin
if(a)
next_state = S_A;
else if(b&valid_r)
next_state = S_B;
else
next_state = S_IDLE;
end
S_A: begin
if(c)
next_state = S_IDLE;
else
next_state = S_A;
end
default: begin
next_state = 'bx;
end
endcase
end
3.根据下一状态做处理
always@(posedge clk, posedge rst)
begin
if(rst) begin
b <= 'bx;
c <= 0;
end
else begin
case(next_state)
S_IDLE: begin
c <= 0;
end
S_A,S_B: begin
b <= 1;
c <= 1;
valid_r <= 1;
end
S_C: begin
if(nnn)
else
end
endcase
end
end
如果有不合理或者有想法的朋友请留言,大家一起去学习fpga,共同成长。 |
|