目录
一、状态机分类
1、摩尔(Moore)型状态机
输出仅仅依赖于当前状态,而与输入条件无关。
2、米勒(Mealy)型状态机
输出不仅取决于当前状态,还取决于该状态的输入条件。
3、Verilog写法
(1)一段式
(2)二段式
(3)三段式(推荐)
二、三段式状态机的基本格式(Verilog)
1、第一个always语句块
第一个always语句块使用同步时序逻辑描述状态转移
always @ (posedge clk or negedge rst_n) begin
if (!rst_n)
CS <= IDLE;
else
CS <= NS;
end
2、第二个always语句
第二个always语句采用组合逻辑判断
always @ (*) begin
case (CS)
IDLE: begin
if ()
NS = ;
else
NS = ;
end
S1: begin
if ()
NS = ;
else
NS = ;
end
S2: begin
if ()
NS = ;
else
NS = ;
end
defaule:
NS = IDLE;
endcase
end
3、第三个always语句
第三个always语句采用同步时序逻辑描述每个状态的输出,针对Moore和Mealy型代码不同
//Moore型
always @ (posedge clk or negedge rst_n) begin
if (!rst_n) begin
xx <= 'b0;
end
else begin
case (CS) //or NS
IDLE: xx <= 'b0;
S1: xx <= 'b0;
S2: xx <= 'b0;
S3: xx <= 'b0;
default: xx <= 'b0;
endcase
end
end
//Mealy型
always @ (posedge clk or negedge rst_n) begin
if (!rst_n)
xx <= 'd0;
else begin
case (CS)
IDLE:
if () xx <= 'd0;
else if () xx <= 'd0;
else xx <= 'd0;
S0: begin
if () xx <= 'd0;
else if () xx <= 'd0;
else xx <= 'd0;
S1: begin
if () xx <= 'd0;
else if () xx <= 'd0;
else xx <= 'd0;
S2: begin
if () xx <= 'd0;
else if () xx <= 'd0;
else xx <= 'd0;
S3: begin
if () xx <= 'd0;
else if () xx <= 'd0;
else xx <= 'd0;
default: begin
xx <= 'd0;
end
endcase
end
(1)Moore型状态机case语句中的NS or CS
对于Moore状态机,case语句的条件中使用NS和CS的区别在于:当状态跳转时,使用NS的情况下,输出是立刻变化的;使用CS的情况下,输出会延迟一个周期。可以根据自己的时序要求进行选择。
(2)Mealy型状态机的输出
对于Mealy状态机,输出由当前状态CS和输入datain控制。