3.2.5.11 Lemmings 2

除了左右行走之外,如果地面消失在旅鼠脚下,旅鼠还会摔倒(并且可能会“啊啊!”)。

除了左右走动和碰撞时改变方向外,当ground=0时,旅鼠会摔倒并说“啊啊!”。当地面重新出现 ( ground=1 ) 时,旅鼠将继续沿与坠落前相同的方向行走。跌倒时被撞不影响行走方向,与地面消失(但尚未跌倒)同一个周期被撞,或仍在跌倒时再次出现地面时,也不影响行走方向。

构建一个模拟这种行为的有限状态机。

module Lemmings2(
    input clk,
    input areset,    // Freshly brainwashed Lemmings walk left.
    input bump_left,
    input bump_right,
    input ground,
    output walk_left,
    output walk_right,
    output aaah ); 
    wire [2:0]in;
    reg [1:0] state,next_state,temp_state;
    parameter A=2'b00,B=2'b01,D=2'b10;//A向左,B向右,D跌倒
    assign in={ground,bump_left,bump_right};
    always@(posedge clk or posedge areset)begin
        if(areset)
            state<=A;
        else
            state<=next_state;
    end
    always @(*) begin
        case(state)
            A:begin
                if(in[2]==0)begin
                    next_state=D;
                    temp_state=A;//temp_state作为锁存器,在跌倒时记录当时状态
                end
                else if(in==3'b100)
                    next_state=A;
                else if(in==3'b101)
                    next_state=A;
                else if(in==3'b110)
                    next_state=B;
                else if(in==3'b111)
                    next_state=B;
                else 
                    next_state=next_state;
            end
            B:begin
                if(in[2]==0)begin
                    next_state=D;
                    temp_state=B;
                end
                else if(in==3'b100)
                    next_state=B;
                else if(in==3'b101)
                    next_state=A;
                else if(in==3'b110)
                    next_state=B;
                else if(in==3'b111)
                    next_state=A;
                else 
                    next_state=next_state;
            end
            D:begin
                if(in[2]==0)begin
                    next_state=D;
                    temp_state=temp_state;
                end
                else 
                    next_state=temp_state;
            end
        endcase
    end
    assign walk_left=(state==A)&~(state==D);
    assign walk_right=(state==B)&~(state==D);
    assign aaah=state==D;
endmodule

HDLbits仿真错误

Vivado仿真却是对的

Vivado仿真应该没问题吧,有点懵。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值