HDLBits网址:https://hdlbits.01xz.net/wiki/Main_Page
前言
HDLBits前面几题简单状态机的训练,同样的状态转移图分为异步复位和同步复位,除了复位不一样,基本上其他是一样的,所以这里就只介绍异步复位的情况。
1.Simple FSM 1
This is a Moore state machine with two states, one input, and one output. Implement this state machine. Notice that the reset state is B.
Module Declaration
module top_module( input clk, input areset, // Asynchronous reset to state B input in, output out);
我的分析
状态机分两种类型:Moore状态机和Mealy状态机,两种状态机的区别如下:
Moore状态机:下一状态只由当前状态决定,即次态=f(现状,输入),输出=f(现状);
Mealy状态机:下一状态不但与当前状态有关,还与当前输入值有关,即次态=f(现状,输入),输出=f(现状,输入)。
这里是最简单的Moore状态机(输出只与状态有关),只有两个状态,一个输入和一个输出,还有一个异步复位信号。我的答案用采用三段式编写:
module top_module(
input clk,
input areset, // Asynchronous reset to state B
input in,
output out);//
parameter A=0, B=1;
reg state, next_state;
always @(*) begin // This is a combinational always block
case(state)
A:begin
if(in) next_state <= A;
else next_state <= B;
end
B:begin
if(in) next_state <= B;
else next_state <= A;
end
endcase
end
always @(posedge clk, posedge areset) begin // This is a sequential always block
// State flip-flops with asynchronous reset
if(areset)
state <= B;
else
stat