HDLBis-Fsm3s

See also: State transition logic for this FSM

The following is the state transition table for a Moore state machine with one input, one output, and four states. Implement this state machine. Include a synchronous reset that resets the FSM to state A. (This is the same problem as Fsm3 but with a synchronous reset.)

StateNext stateOutput
in=0in=1
AAB0
BCB0
CAD0
DCB1

Module Declaration

module top_module(
    input clk,
    input in,
    input reset,
    output out); 

自己写的代码:

module top_module(
    input clk,
    input in,
    input reset,
    output out); 
    //采用同步置位reset
    reg[3:0] state;
    parameter A = 0, //0001
              B = 1, //0010
              C = 2, //0100
              D = 3; //1000
    // State transition logic
    always @(posedge clk) begin
        if(reset)begin 
            state <= 4'b0001;
        end
        else begin 
         state[A] <= state[A]&(~in)|state[C]&(~in);
         state[B] <= state[A]&in | state[B]&in |state[D]&in;
         state[C] <= state[B]&(~in) | state[D]&(~in);
         state[D] <= state[C]&in;
        end
    end
    // State flip-flops with synchronous reset

    // Output logic
    assign out = state[D];
endmodule

标准的写法是在always @(posedge clk)模块中写

 if(reset)begin

            state <= 4'b0001;

        end

        else begin

           state <= nextstate;

        end

通过组合逻辑对nextstate的状态进行改变

下面是标准的代码:

module top_module(
    input clk,
    input in,
    input reset,
    output out); 
    //采用同步置位reset
    reg[3:0] state,nextstate;
    parameter A = 0, //0001
              B = 1, //0010
              C = 2, //0100
              D = 3; //1000
    // State transition logic
    always @(*)begin 
    	 nextstate[A] <= state[A]&(~in)|state[C]&(~in);
         nextstate[B] <= state[A]&in | state[B]&in |state[D]&in;
         nextstate[C] <= state[B]&(~in) | state[D]&(~in);
         nextstate[D] <= state[C]&in;
    end
    // State flip-flops with synchronous reset
	  always @(posedge clk) begin
        if(reset)begin 
            state <= 4'b0001;
        end
        else begin 
        	state <= nextstate;
        end
    end
    // Output logic
    assign out = state[D];
endmodule

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值