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 );
localparam left = 0;
localparam right = 1;
localparam fall_l = 2;
localparam fall_r = 3;
reg[1:0] state, next_state;
reg[2:0] out;
//state
always@(posedge clk or posedge areset) begin
if(areset)
state <= left;
else
state <= next_state;
end
//transition
always@(*)begin
case(state)
left:next_state=ground?(bump_left?right:left):fall_l;
right:next_state=ground?(bump_right?left:right):fall_r;
fall_l:next_state=ground?left:fall_l;
fall_r:next_state=ground?right:fall_r;
endcase
end
//out
always@(posedge clk or posedge areset) begin
if(areset)
out <= 3'b100;
else
case(next_state)
left: out <= 3'b100;
fall_l: out <= 3'b001;
right: out <= 3'b010;
fall_r: out <= 3'b001;
endcase
end
assign {walk_left, walk_right, aaah} = out;
endmodule
反思:应该把状态想完整 只想到了三个状态 left right fall,用三个寄存器next state pre ,参考答案有四个状态 left right fall-l fall-r,用四个状态可以描述全部状态,并且代码整洁。 我的代码错误,里可能是逻辑不通,只有一开始的状态是正确的,到了后面状态就不会改变了。