除了左右行走之外,如果地面消失在旅鼠脚下,旅鼠还会摔倒(并且可能会“啊啊!”)。
除了左右走动和碰撞时改变方向外,当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仿真应该没问题吧,有点懵。。