verilog刷题笔记004

Lemmings4的一种解法

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 ); 
    
    reg [7:0] state;
    reg [7:0] next_state;
    parameter LEFT = 0, DIG_L = 1, FALL_L = 2,
    		 RIGHT = 3, DIG_R = 4, FALL_R = 5,
             SPLAT = 6;
    
    always@(*)begin
        case(state[2:0])
            LEFT:begin
                if(ground == 0) next_state = FALL_L;
                else begin
                    if(dig) next_state = DIG_L;
                    else begin
                        if(bump_left) next_state = RIGHT;
                        else next_state = LEFT;
                    end
                end
            end
            DIG_L:begin
                if(ground == 0) next_state = FALL_L;
                else next_state = DIG_L;
            end
            FALL_L:begin
                if(ground == 1&&state[7:3]<5'd20) next_state = LEFT;
                else if(ground == 1&&state[7:3]>=5'd20) next_state = SPLAT; 
                else begin
                    if(state[7:3]<5'd20) next_state = state + 8;
                    else begin
                        next_state = state;
                    end
                end
            end
            RIGHT:begin
                if(ground == 0) next_state = FALL_R;
                else begin
                    if(dig) next_state = DIG_R;
                    else begin
                        if(bump_right) next_state = LEFT;
                        else next_state = RIGHT;
                    end
                end
            end
            DIG_R:begin
                if(ground == 0) next_state = FALL_R;
                else next_state = DIG_R;
            end
            FALL_R:begin
                if(ground == 1&&state[7:3]<5'd20) next_state = RIGHT;
                else if(ground == 1&&state[7:3]>=5'd20) next_state = SPLAT; 
                else begin
                    if(state[7:3]<5'd20) next_state = state + 8;
                    else begin
                        next_state = state;
                    end
                end
            end 
            SPLAT: next_state = SPLAT;
        endcase
    end
    
    always@(posedge clk or posedge areset)begin
        if(areset) begin
            state <= 0;
        end
        else state <= next_state;
    end
    
    assign walk_left = ~(state == SPLAT)&(state[2:0] == LEFT);
    assign walk_right= ~(state == SPLAT)&(state[2:0] == RIGHT);
    assign aaah =      ~(state == SPLAT)&((state[2:0] == FALL_L)|(state[2:0] == FALL_R));
    assign digging =   ~(state == SPLAT)&((state[2:0] == DIG_L)|(state[2:0] == DIG_R));
    
    

endmodule
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值