Q2b:Another FSM

该题的状态机共2输入2输出;当复位信号撤销时,在下一个周期内将f输出为1,需留意f为1持续一个周期;然后状态机取决于x的值,当x在连续的三个周期中产生值为1、0、1时,下一周期将g输出为1,在保持g为1时判断y的输入,如果y在两个周期中有任意一个周期为1了,那么g永久保持1;如果两个周期都没有1,那么g将永久保持0。

module top_module (
    input clk,
    input resetn,    // active-low synchronous reset
    input x,
    input y,
    output f,
    output g
); 
	parameter a=4'd0,b=4'd1,c=4'd2,d=4'd3,e=4'd4,F=4'd5,G=4'd6,h=4'd7;
    reg [3:0]state,next_state;
    reg rstn1,rstn2;
    always@(posedge clk)begin
        if(!resetn)
            state<=a;
        else
            state<=next_state;
    end
    always@(*)begin
        if(!resetn)begin
            f=0;
            g=0;
        end
        else begin
            case(state)
                a:next_state=resetn?b:a;
                b:begin
                    f=1;
                    g=0;
                    next_state=x?c:b;
                end
                c:begin
                    f=0;
                    g=0;
                    next_state=x?c:d;
                end
                d:begin
                    f=0;
                    g=0;
                    next_state=x?e:b;
                end
                e:begin
                    f=0;
                    g=1;
                    next_state=y?F:G;
                end
                F:begin
                    f=0;
                    g=1;
                    next_state=F;
                end
                G:begin
                    f=0;
                    g=1;
                    next_state=y?F:h;
                end
                h:begin
                    f=0;
                    g=0;
                    next_state=h;
                end
            endcase
        end
    end
    
endmodule

上面是最开始我的想法,但这样不太好设计 f 的输出(上述代码中f的输出逻辑是错误的),解决方法是多加一个状态专门用来进行 f 的输出,称为fout

module top_module (
    input clk,
    input resetn,    // active-low synchronous reset
    input x,
    input y,
    output f,
    output g
); 
	parameter a=4'd0,b=4'd1,c=4'd2,d=4'd3,e=4'd4,F=4'd5,G=4'd6,h=4'd7,fout=4'd8;
    reg [3:0]state,next_state;
    reg rstn1,rstn2;
    always@(posedge clk)begin
        if(!resetn)
            state<=a;
        else
            state<=next_state;
    end
    always@(*)begin
        case(state)
            a:next_state=resetn?fout:a;
            fout:next_state=b;
            b:next_state=x?c:b;
            c:next_state=x?c:d;
            d:next_state=x?e:b;
            e:next_state=y?F:G;
            F:next_state=F;
            G:next_state=y?F:h;
            h:next_state=h;
        endcase
    end
    assign f=state==fout;
    assign g=state==e|state==F|state==G;
endmodule

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值