HDLbits---Lemmings3

本文解析了一段Verilog HDL代码,详细介绍旅鼠如何根据地面状态和挖掘信号在Lemmings3游戏中进行挖洞行为。它探讨了状态机设计及如何在地面变化时调整行为,如停止挖掘并可能改变行走方向。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HDLbits---Lemmings3

作者说如果旅鼠在地面上(ground = 1)并且挖掘信号为1,则它可以开始挖洞,一直挖,直到把地都挖穿了(ground = 0),此时,旅鼠就掉下去了,并且发出大叫。一开始以为dig会一直保持,结果只有一个周期的高电平,而只有当ground = 0时,才会从挖掘状态跳变成别的状态。

module top_module(
    input clk,
    input areset,    // Freshly brainwashed Lemmings walk left.
    input bump_left,
    input bump_right,
    input ground,
    input dig,
    output walk_left,
    output walk_right,
    output aaah,
    output digging ); 
	parameter LEFT = 3'd0, RIGHT = 3'd1, GROUND_LEFT = 3'd2, GROUND_RIGHT = 3'd3,DIG_LEFT = 3'd4,DIG_RIGHT = 3'd5;
    reg	[2:0]	current_state;
    reg [2:0]	next_state;
    always@(posedge clk or posedge areset )begin
        if(areset)begin
            current_state = LEFT;
        end
        else begin
            current_state = next_state;
        end
    end

always@(*)begin
    case(current_state)
        LEFT:begin
            if(!ground)begin
                next_state = GROUND_LEFT;
            end
            else begin
                case({dig,bump_left})
                    2'b00:next_state = current_state;
                    2'b01:next_state = RIGHT;
                    2'b11:next_state = DIG_LEFT;
                    2'b10:next_state = DIG_LEFT;
                    default:next_state = current_state;
                endcase
            end
        end
        RIGHT:begin
            if(!ground)begin
                next_state = GROUND_RIGHT;
            end
            else begin
                case({dig,bump_right})
                    2'b00:next_state = current_state;
                    2'b01:next_state = LEFT;
                    2'b11:next_state = DIG_RIGHT;
                    2'b10:next_state = DIG_RIGHT;
                    default:next_state = current_state;
                endcase
            end
        end
        GROUND_LEFT:begin
            if(!ground)begin
                next_state = GROUND_LEFT;
            end
            else begin    
                next_state = LEFT;
            end
        end
        GROUND_RIGHT:begin
            if(!ground)begin
                next_state = GROUND_RIGHT;
            end
            else begin    
                next_state = RIGHT;
            end
        end
        DIG_LEFT:begin
            if(!ground)begin
                next_state = GROUND_LEFT;
            end
            else begin
                next_state = current_state;
            end
        end
        DIG_RIGHT:begin
            if(!ground)begin
                next_state = GROUND_RIGHT;
            end
            else begin
                next_state = current_state;
            end
        end
        default:next_state = current_state;
    endcase
end
assign walk_left = (current_state == LEFT);
assign walk_right = (current_state == RIGHT);
assign aaah = (current_state == GROUND_LEFT || current_state == GROUND_RIGHT);
assign digging = (current_state == DIG_LEFT || current_state == DIG_RIGHT);
endmodule

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

离离离谱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值