Lemmings 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 module top_module( input clk, input areset, input bump_left, input bump_right, output walk_left, output walk_right); reg state, next_state; localparam LEFT = 1'b0 ; localparam RIGHT = 1'b1 ; always @(*) begin if (state == LEFT) if (bump_left) next_state = RIGHT; else next_state = state; else if (bump_right) next_state = LEFT; else next_state = state; end always @(posedge clk, posedge areset) begin if (areset) state <= LEFT; else state <= next_state; end assign walk_left = (state == LEFT); assign walk_right = (state == RIGHT); endmodule
Lemmings2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 module top_module( input clk, input areset, input bump_left, input bump_right, input ground, output walk_left, output walk_right, output aaah ); reg [1 :0 ] state, next_state; localparam LEFT = 2'b00 ; localparam RIGHT = 2'b01 ; localparam FALL_L = 2'b10 ; localparam FALL_R = 2'b11 ; always @(*) begin next_state = state; if (state == LEFT) begin next_state = LEFT; if (!ground) next_state = FALL_L; else if (bump_left) next_state = RIGHT; end else if (state == RIGHT) begin next_state = RIGHT; if (!ground) next_state = FALL_R; else if (bump_right) next_state = LEFT; end else if (state == FALL_L) begin next_state = FALL_L; if (ground) next_state = LEFT; end else if (state == FALL_R) begin next_state = FALL_R; if (ground) next_state = RIGHT; end end always @(posedge clk or posedge areset) begin if (areset) state <= LEFT; else state <= next_state; end assign walk_left = (state == LEFT); assign walk_right = (state == RIGHT); assign aaah = (state == FALL_L || state == FALL_R); endmodule
Lemmings 3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 module top_module( input clk, input areset, input bump_left, input bump_right, input ground, input dig, output walk_left, output walk_right, output aaah, output digging ); reg [3 :0 ] state, next_state; localparam LEFT = 3'b000 ; localparam RIGHT = 3'b001 ; localparam FALL_L = 3'b010 ; localparam FALL_R = 3'b011 ; localparam DIG_L = 3'b100 ; localparam DIG_R = 3'b101 ; always @(*) begin next_state = state; case (state) LEFT: begin if (!ground) next_state = FALL_L; else if (dig) next_state = DIG_L; else if (bump_left) next_state = RIGHT; end RIGHT: begin if (!ground) next_state = FALL_R; else if (dig) next_state = DIG_R; else if (bump_right) next_state = LEFT; end FALL_L: begin if (ground) next_state = LEFT; end FALL_R: begin if (ground) next_state = RIGHT; end DIG_L: begin if (!ground) next_state = FALL_L; end DIG_R: begin if (!ground) next_state = FALL_R; end endcase end always @(posedge clk or posedge areset) begin if (areset) state <= LEFT; else state <= next_state; end 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); endmodule
Lemmings 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 module top_module( input clk, input areset, input bump_left, input bump_right, input ground, input dig, output walk_left, output walk_right, output aaah, output digging ); reg [2 :0 ] state, next_state; reg [4 :0 ] counter = 5'b00000 ; localparam LEFT = 3'b000 ; localparam RIGHT = 3'b001 ; localparam FALL_L = 3'b010 ; localparam FALL_R = 3'b011 ; localparam DIG_L = 3'b100 ; localparam DIG_R = 3'b101 ; localparam SPLAT = 3'b111 ; always @(*) begin next_state = state; case (state) LEFT: begin if (!ground) next_state = FALL_L; else if (dig) next_state = DIG_L; else if (bump_left) next_state = RIGHT; end RIGHT: begin if (!ground) next_state = FALL_R; else if (dig) next_state = DIG_R; else if (bump_right) next_state = LEFT; end FALL_L: begin if (ground) begin if (counter == 5'd20 ) next_state = SPLAT; else next_state = LEFT; end end FALL_R: begin if (ground) begin if (counter == 5'd20 ) next_state = SPLAT; else next_state = RIGHT; end end DIG_L: begin if (!ground) next_state = FALL_L; end DIG_R: begin if (!ground) next_state = FALL_R; end endcase end always @(posedge clk or posedge areset) begin if (areset) state <= LEFT; else state <= next_state; end always @(posedge clk or posedge areset) begin if (areset) counter <= 5'b00000 ; else if ((state == FALL_L || state == FALL_R) && !ground) begin if (counter < 5'd20 ) counter <= counter + 1'b1 ; else counter <= counter; end else counter <= 5'd0 ; end 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); endmodule
落地了就要重置计数器;计数器要放在时序里面
PS/2 packet parser
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 module top_module( input clk, input [7 :0 ] in, input reset, output done); reg [1 :0 ] state, next_state; localparam BYTE1 = 2'b00 ; localparam BYTE2 = 2'b01 ; localparam BYTE3 = 2'b10 ; localparam DONE = 2'b11 ; always @(*) begin case (state) BYTE1: begin if (in[3 ]) next_state = BYTE2; else next_state = BYTE1; end BYTE2: begin next_state = BYTE3; end BYTE3: begin next_state = DONE; end DONE: begin if (in[3 ]) next_state = BYTE2; else next_state = BYTE1; end endcase end always @(posedge clk or posedge reset) begin if (reset) state = BYTE1; else state = next_state; end assign done = (state == DONE); endmodule
PS/2 packet parser and datapath 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 module top_module( input clk, input [7 :0 ] in, input reset, output [23 :0 ] out_bytes, output done); reg [1 :0 ] state, next_state; reg [23 :0 ] out; localparam BYTE1 = 2'b00 ; localparam BYTE2 = 2'b01 ; localparam BYTE3 = 2'b10 ; localparam DONE = 2'b11 ; always @(*) begin case (state) BYTE1: begin if (in[3 ]) next_state = BYTE2; else next_state = BYTE1; end BYTE2: begin next_state = BYTE3; end BYTE3: begin next_state = DONE; end DONE: begin if (in[3 ]) next_state = BYTE2; else next_state = BYTE1; end endcase end always @(posedge clk) begin if (reset) begin state <= BYTE1; out <= 24'b0 ; end else begin state <= next_state; case (state) BYTE1: begin if (in[3 ]) out <= {out[15 :0 ], in}; end BYTE2: begin out <= {out[15 :0 ], in}; end BYTE3: begin out <= {out[15 :0 ], in}; end DONE: begin if (in[3 ]) out <= {out[15 :0 ], in}; end endcase end end assign done = (state == DONE); assign out_bytes = out; endmodule
移位可以直接 out <= {out[15:0], in};
移位是时序操作,所以不能放在组合逻辑里面。
Synchronous 是同步的意思。
Serial receiver