1、状态机(state machine):有限状态机(FSM)是指在有限个状态之间按照以一定规律转换的时序电路。
2、常用的状态机有 Mealy 状态机和 Moore状态机:他们之间的区别,mealy状态机的输出与输入有关,Moore状态机与输入无关
3、状态机的设计,四段论
状态空间设计
状态跳转模式
Next state判断
Next action
状态空间定义:
parameter SLEEP = 2'b00;
parameter STUDY = 2'b01;
parameter EAT = 2'b10;
paramter AMUSE = 2'b11;
reg [1:0] current_state;
reg [1:0] next_state;
parameter SLEEP = 2'b1000;
parameter STUDY = 2'b0100;
parameter EAT = 2'b0010;
paramter AMUSE = 2'b0001;
reg [1:0] current_state;
reg [1:0] next_state;
状态跳转(时序逻辑,非阻塞non blocking)
always @(posedge clk or negedge rst_n) begin
if(! rst_n)
current_state <=SLEEP;
else
current_state <= next_state;
end
下一个状态判断(组合逻辑block)
always @(current_state or input_signals) begin
case (current_state)
SLEEP: begin
if (clock_alarm)
next_state = STUDY;
else
next_state = SLEEP;
end
STUDY:begin
if (lunch_time)
next_state = EAT;
else
next_state = STUDY;//if else 要配对,避免latch锁存
end
EAT: begin
if (clock_alarm)
next_state = STUDY;
else
next_state = AMUSE;
end
AMUSE:begin
if (sleep_time)
next_state = SLEEP;
else
next_state = STUDY;//if else 要配对,避免latch锁存
end
end
各个状态的动作
wire read_book;
assign read_book =(current_state == STUDY)? 1'b1 :1'b0;
always @(current_state )begin
if(current_state == STUDY)
read_book = 1;
else
read_book = 0; //blocking 赋值
end