赞
踩
状态机(State Machine)
有限状态机(Finite State Machine,简称 FSM)
在有限个状态之间按一定规律转换的时序电路。
状态机是包含有限个状态的概念化机器,由状态、转换和动作
这3个元素组成的有向图,三者关系如下:
所有的状态机体系中,普遍的假设一个状态机在它能进行下一个事件处理完成对每个事件的处理,这个执行模型被称为"运行—到—完成模型"(Run-to-Completion Execution Model,RTC)。
状态寄存器
由一组触发器组成,用来记忆状态机当前所处的状态,状态的改变只发生在时钟的跳变沿。
状态是否改变,如何改变,取决于组合逻辑F的输出,F是当前状态和输入信号的函数。
状态机的输出是由组合逻辑G提供的,G也是当前状态和输入信号的函数。
//define state space
parameter SLEEP = 4'b1000;
parameter STUDY = 4'b0100;
parameter EAT = 4'b0010;
parameter AMUSE = 4'b0001;
// internal variable
reg [3:0] current_state;
reg [3:0] next_state;
独热码:每个状态只有一个寄存器位置位,译码逻辑简单。
//transition
always @(posedge clk or negedge rst_n)beigin //敏感列表:时钟信号以及复位信号边沿的触发方式
if(!rst_n)
current_state <= SLEEP;
else
current_state <= next_state; //非阻塞赋值
end
//next state decision always @(current_state or input_signal)beigin //敏感列表:所有的右边表达式中的变量以及if、case条件中的变量 case (current_state) SLEEP: begin if(clock_alarm) next_state = STUDY; else next_state = SLEEP; //阻塞赋值 end SYUDY:begin if(lunch_time) next_state = EAT; else next_state = SYUDY; end EAT: .....; AMUSE: .....; default: ...; endcase end
//action
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;
end
module divider7_fsm( //inout ports input sys_clk , input sys_rst_n , //output ports output reg clk_divide_7 ); //reg define reg [6:0] curr_st ; ref [6:0] next_st; //wire define //parameter define parameter WIDTH =1 ; //one hot code design parameter S0 = 7'b0000000; parameter S1 = 7'b0000001; parameter S2 = 7'b0000010; parameter S3 = 7'b0000100; parameter S4 = 7'b0001000; parameter S5 = 7'b0010000; parameter S6 = 7'b0100000; always @(posedge sys_clk or negedge sys_rst_n)beigin if(sys_rst_n == 1'b0) curr_st <= 7'b0; else curr_st <= next_st; end //FSM state logic always @(*)begin case (curr_st) S0:beigin next_st = S1; end S1:beigin next_st = S2; end S2:beigin next_st = S3; end S3:beigin next_st = S4; end S4:beigin next_st = S5; end S5:beigin next_st = S6; end S6:beigin next_st = S0; end default: next_st = S0; endcase end //control divede clock offset always @(posedge sys_clk or negedge sys_rst_n)begin if(sys_rst_n == 1'b0)beigin clk_divide_7 <= 1'b0; end else if((curr_st == S0) | (curr_st == S1) | (curr_st == S2) |(curr_st == S3)) clk_divide_7 <= 1'b0; else if(curr_st == S4) | (curr_st == S5) | (curr_st == S6) |(curr_st == S7)) clk_divide_7 <=1;b1; else; end
三段式可以在组合逻辑后再增加一级寄存器来实现时序逻辑输出:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。