6.状态机

1.Moore状态机

1.1 定义

输出仅由电路状态决定的时许电路称为Moore型。

2.Melay状态机

2.1定义

输出不仅由当前电路状态决定还与当前输入有关。

3.状态机写法

以计数器位例子

3.1一段式

module  count(

clk,rst,count,result

);

input clk,rst;//时钟信号,复位信号

input count;//计数信号

output   reg result;

parameter  state0=2'd0;

parameter state1=2'd1;

parameter state2=2'd10;

paramerter state3=2'd11;

reg [3:0]  state;//

always@(posedge clk)

begin

    if(rst)

        begin

            state=state0;

            result=2'd0;

        end

else

    begin

        case(state):

        state0: begin if(count)  state=state1;  result=2'd0; end

        state1:begin  if(count)  state=state2;  result=2'd1; end

        state2:begin if(count)    state=state3; result=2'd10;end

        state3:begin if(count)    state=state0: result=2'd11;end  //计数信号 改变状态值

        default:begin state=s0;result=2'd00;end

        endcase

end

end

endmodule

3.2二段式

将状态变化和结果输出分开。分成状态转移和结果输出。

module  count(

clk,rst,count,result

);

input clk,rst;//时钟信号,复位信号

input count;//计数信号

output   reg result;

parameter  state0=2'd0;

parameter state1=2'd1;

parameter state2=2'd10;

paramerter state3=2'd11;

reg [3:0]  current_state,next_state;

alwyas@(posede clk)

begin

    if(rst)

        being

            current_state=state;

        end

else

       current_state=next_state;

end


always@(*)

begin

case(current_state):

state0:begin if(count)   next_state=state1; result=2'd00;end

state1:begin  if(count)  next_state=state2;  result=2'd1; end

state2:begin if(count)   next_state=state3; result=2'd10;end

state3:begin if(count)   next_state=state0: result=2'd11;end  //计数信号 改变状态值

default:begin next_state=s0;result=2'd00;end

endcase

end

end

endmodule

3.3三段式  

分成三个模块  状态转移时序模块 状态转移逻辑模块 状态输出

module  count(

clk,rst,count,result

);

input clk,rst;//时钟信号,复位信号

input count;//计数信号

output   reg result;

parameter  state0=2'd0;

parameter state1=2'd1;

parameter state2=2'd10;

paramerter state3=2'd11;

reg [3:0]  current_state,next_state;


alwyas@(posede clk)

begin

if(rst)

being

            current_state=state;

end

    else

        current_state=next_state;

end

 //时序模块,当前状态预存为次级状态

always@(*)

begin

    if(rst)

     begin 

        result=2'd0;

      end

    else

        begin

        case(current_stae):

        state0:begin if(count)   next_state=state1; ;end

        state1:begin  if(count)  next_state=state2;  end

        state2:begin if(count)   next_state=state3;end

        state3:begin if(count)   next_state=state0: end  //计数信号 改变状态值

        default:begin next_state=s0;end

        endcase

        end

 end    //    状态判断


always@(posedge clk)

begin

    if(rst)

     result=2'd00;

    else

        begin

            case(next_current):

            state0:begin   result=2'd00;end

            state1:begin  result=2'd1; end

            state2:begin  result=2'd10;end

            state3:begin  result=2'd11;end 

            default:begin result=2'd00;end

            endcase

       end

end//输出

endmodule

                     

    

        

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。