verilog HDLBits刷题[Finite State Machines]“Lemmings4”---Lemmings4

verilog HDLBits刷题[Finite State Machines]“Lemmings4”---Lemmings4 1、题目See also: Lemmings1, Lemmings2, and Lemmings3.Although Lemmings can walk, fall, and dig, Lemmings arent invulnerable. If a Lemming falls for too long then hits the ground, it can splatter. In particular, if a Lemming falls for more than 20 clock cycles then hits the ground, it will splatter and cease walking, falling, or digging (all 4 outputs become 0), forever (Or until the FSM gets reset). There is no upper limit on how far a Lemming can fall before hitting the ground. Lemmings only splatter when hitting the ground; they do not splatter in mid-air.Extend your finite state machine to model this behaviour.Falling for 20 cycles is survivable:2、代码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 ); localparam LEFT 1, RIGHT 2, FALL_L 3, FALL_R 4, DIG_L 5, DIG_R 6, SPLAT 7; reg [2:0] state; reg [2:0] next_state; reg [6:0] cnt; // watch out this! always (posedge clk , posedge areset)begin if(areset) state LEFT; else state next_state; end always (posedge clk or posedge areset)begin if(areset) cnt 7d0; else if(state FALL_L || state FALL_R) cnt cnt 1b1; else cnt 7d0; end always (*) case(state) LEFT : next_state ~ground ? FALL_L : (dig ? DIG_L : (bump_left ? RIGHT : LEFT)); RIGHT : next_state ~ground ? FALL_R : (dig ? DIG_R : (bump_right ? LEFT : RIGHT)); FALL_L : next_state ground ? (cnt 5d20 ? SPLAT : LEFT) : FALL_L; FALL_R : next_state ground ? (cnt 5d20 ? SPLAT : RIGHT) : FALL_R; DIG_L : next_state ~ground ? FALL_L : DIG_L; DIG_R : next_state ~ground ? FALL_R : DIG_R; SPLAT : next_state SPLAT; endcase assign walk_left (state LEFT); assign walk_right (state RIGHT); assign aaah (state FALL_L) || (state FALL_R); assign digging (state DIG_L) || (state DIG_R); endmodule3、结果