状态机
昨天飞哥要求在画完电梯状态转移图之后,写一个.v文件。奈何笔者知识浅薄,看过的状态机写法早已忘记,今天一大早带了本书,在工位上看。
首先状态机主要要处理的是,下一个状态以及输出信号,他们都是输入信号和当前状态的表达式。
状态机主要分为__Mealy__和__Moore__两种,区别在于,Mealy状态机的输出由输入以及当前状态决定,而Moore状态机的输出只由当前状态决定。
写状态机一本遵循的经典的三段式,就拿我今天写的为例:
module lift_status(
input clk,
input reset,
input emergency,
input up,
input down,
input open,
input close,
input arrive,
output elevator_up,
output elevator_down,
output elevator_stop,
output door_open,
output door_close,
output alarm
);
parameter [2:0] POWEROFF = 3'b000;
STATIC = 3'b001;
UP = 3'b010;
DOWN = 3'b011;
ARRIVE = 3'b100;
OPEN = 3'b101;
CLOSE = 3'b110;
EMERGENCY = 3'b111;
reg [2:0] curr_state;
reg [2:0] next_state;
// first segment: state transfer
always@(posedge clk or negedge reset)
begin
if(!reset)
curr_state <= POWEROFF;
else
curr_state <= next_state;
end