Created
June 12, 2023 07:46
-
-
Save xeonqq/5c3cac0d4ff26d6c92b5f51f1ab4d619 to your computer and use it in GitHub Desktop.
fsm 4 locations, move left and right, one hot encoding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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