Skip to content

Instantly share code, notes, and snippets.

@xeonqq
Created June 12, 2023 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeonqq/5c3cac0d4ff26d6c92b5f51f1ab4d619 to your computer and use it in GitHub Desktop.
Save xeonqq/5c3cac0d4ff26d6c92b5f51f1ab4d619 to your computer and use it in GitHub Desktop.
fsm 4 locations, move left and right, one hot encoding
/*
* Do not change Module name
*/
module FSM(input clk, input [1:0] w, output [3:0] z);
localparam A=4'b0001, B=4'b0010, C=4'b0100, D=4'b1000;
reg [3:0] state, next_state;
initial state = A;
always @(posedge clk)
state <= next_state;
always @(*)
case (w)
2'b10:
if (state !=A)
next_state = state >> 1;
2'b01:
if (state !=D)
next_state = state << 1;
endcase
assign z = state;
endmodule
module main;
reg clk = 0;
always #1 clk = !clk;
reg [1:0] in;
wire [3:0] out;
FSM fsm(clk, in, out);
initial
begin
in = 2'b01;
#2 $display("%b",out); in = 2'b10;
#2 $display("%b",out); in = 2'b10;
#2 $display("%b",out);in = 2'b01;
#2 $display("%b",out);in = 2'b01;
#2 $display("%b",out);in = 2'b01;
#2 $display("%b",out);in = 2'b01;
#2 $display("%b",out);in = 2'b01;
$finish ;
end
endmodule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment