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 [1:0]digg=2'b11,walk=2'b10,fall=2'b00;
parameter left=1'b0,right=1'b1,dead=0,live=1;
reg [3:0]state,state_next;
reg [16:0]t; // 用于计算时间
always@(*)begin
// state[1:0] 表示有3种状态: 正常走[1,0] 下落[0,0] 挖坑[1,1].state[2]表示方向,state[3]表示摔死没有
if (state[1:0]==walk) //正在正常走
case ({ground,dig})
2'b11: state_next <= {state[3:2],digg}; //正常走>挖坑,结果是挖坑
2'b10: state_next <= (state[2]&bump_right | !state[2]&bump_left)? {state[3],!state[2],walk}:{state[3:2],walk};
//正常走>正常走,结果是正常走 但要判断一下方向,只有这里要判断方向
2'b01: state_next <= {state[3:2],fall}; //正常走>下落,结果是下落
2'b00: state_next <= {state[3:2],fall}; //正常走>下落,结果是下落
endcase
if (state[1:0]==fall) //正在下落
case ({ground,dig})
2'b11: state_next <= (t<=16'd20)?{state[3:2],walk}:{dead,state[2],walk}; //下落>挖坑,结果是正常走,不判断方向,但要判断是否摔死
2'b10: state_next <= (t<=16'd20)?{state[3:2],walk}:{dead,state[2],walk}; //下落>正常走, 结果是正常走 不判断方向,但要判断是否摔死
2'b01: state_next <= {state[3:2],fall}; //下落>下落,结果是下落,还没摔
2'b00: state_next <= {state[3:2],fall}; //下落>下落,结果是下落,还没摔
endcase
if (state[1:0]==digg) //正在挖坑
case ({ground,dig})
2'b11: state_next <= {state[3:2],digg}; //挖坑>挖坑,结果是挖坑
2'b10: state_next <= {state[3:2],digg}; //挖坑>正常走,结果是挖坑
2'b01: state_next <= {state[3:2],fall}; //挖坑>下落,结果是下落
2'b00: state_next <= {state[3:2],fall}; //挖坑>下落,结果是下落
endcase
end
always@(posedge clk,posedge areset)begin
if(areset)begin
state <= {live,left,walk};
end
else begin
if (state_next[1:0]==fall)
t<=t+1;
else
t<=16'd0;
state <= state_next;
end
end
assign walk_left = (state== {live,left,walk});
assign walk_right = (state== {live,right,walk});
assign aaah = (state[3]==live & state[1:0]==fall);
assign digging = (state[3]==live & state[1:0]==digg);
endmodule
// 被同学嫌弃写的不标准 哈哈 求赞!
hdlbits Lemmings4
于 2024-09-27 13:20:13 首次发布