verilog学习(三) 状态机四段论

状态机概念

实现顺序逻辑:在有限个状态中跳转。 finite state machine FSM

状态机模型

mealy状态机

输出状态受输入状态影响

moore状态机

输出状态不受输入状态影响

状态机的设计

四段论

状态空间定义

// 定义状态空间
parameter	SLEEP	=	2'B00;
parameter	STUDY	=	2'B01;
parameter	WORK	=	2'B10;
parameter	AMUSE	=	2'B11;

//内部变量
reg	[1:0] current_state;
reg [1:0] next_state;
// 定义状态空间 独热码 译码逻辑简单
parameter	SLEEP	=	4'B0001;
parameter	STUDY	=	4'B0010;
parameter	WORK	=	4'B0100;
parameter	AMUSE	=	4'B1000;

//内部变量
reg	[3:0] current_state;
reg [3:0] next_state;

状态跳转

always @(posedge clk or negedge rst_n) begin
    if(!rst_n);
		current_state <= SLEEP; //初始化
	else
        current_state <= next_state
end

唯一使用时序逻辑之处,注意要非阻塞赋值

下一个状态判断

always @(*) begin
    case (current_state)
        SLEPP:
            begin
                if (clock_alarm)
                    next_state = STUDY;
                else
                    next_state = SLEEP;
            end
        STUDY:
            begin
            end
        WORK:
            begin
            end
        AMUSE:
            begin
            end
        DEFAULT:
            begin
            end
    endcase
end

使用组合逻辑,注意要阻塞赋值

各个状态的动作

使用组合逻辑,简单麻烦两种办法:

//第一种
wire read_book;
assign read_book = (current_state == WORK) 	?	 1'b1	:	1'b0;

//第二种
always @(current_state) begin
    if (current_state == WORK)
        read_book = 1;
    else 
        read_book = 0;
end

动作这里加一个时序逻辑更好。

猜你喜欢

转载自blog.csdn.net/shenchen2010/article/details/120955599