本题波形似乎与题干有所不符
波形显示,下落同时碰到相同方向上的障碍物会对后续的移动有影响,即先按照下落前移动一拍,然后反向移动。
符合波形的解题思路
module top_module(
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 );
reg [3:0] state, next_state;
parameter A = 4'b0001; //left
parameter B = 4'b0010; //right
parameter UG_A = 4'b0100; //unground left
parameter UG_B = 4'b1000; //unground right
always @(posedge clk, posedge areset) begin
if(areset)
state <= A;
else
state <= next_state;
end
always @* begin
case (state)
A : next_state = ground ? (bump_left ? B : A) : (bump_left ? UG_B : UG_A);
B : next_state = ground ? (bump_right ? A : B) : (bump_right ? UG_A : UG_B);
UG_B: next_state = ground ? B : UG_B;
UG_A: next_state = ground ? A : UG_A;
default: next_state = 'x;
endcase
end
always @(posedge clk, posedge areset) begin
if(areset)
{walk_left, walk_right, aaah} <= 3'b100;
else begin
walk_left <= next_state[0];
walk_right <= next_state[1];
aaah <= |next_state[3:2];
end
end
endmodule
符合题干的解题思路
module top_module(
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 );
reg [3:0] state, next_state;
parameter A = 4'b0001; //left
parameter B = 4'b0010; //right
parameter UG_A = 4'b0100; //unground left
parameter UG_B = 4'b1000; //unground right
always @(posedge clk, posedge areset) begin
if(areset)
state <= A;
else
state <= next_state;
end
always @* begin
case (state)
A : next_state = ground ? (bump_left ? B : A) : UG_A;
B : next_state = ground ? (bump_right ? A : B) : UG_B;
UG_B: next_state = ground ? B : UG_B;
UG_A: next_state = ground ? A : UG_A;
default: next_state = 'x;
endcase
end
always @(posedge clk, posedge areset) begin
if(areset)
{walk_left, walk_right, aaah} <= 3'b100;
else begin
walk_left <= next_state[0];
walk_right <= next_state[1];
aaah <= |next_state[3:2];
end
end
endmodule