Simple FSM 1 (a)
module top_module(
input clk,
input areset, // Asynchronous reset to state B
input in,
output out);//
parameter A=0, B=1;
reg state, next_state;
always @(*) begin // This is a combinational always block
state=next_state;
// State transition logic
end
always @(posedge clk, posedge areset) begin
if(areset)
begin
out<=1;
next_state<=B;
end
else
begin
case({state,in})
2'b10:begin
out<=0;
next_state<=0;
end
2'b11:begin
out<=1;
next_state<=1;
end
2'b01:begin
out<=0;
next_state<=0;
end
2'b00:begin
out<=1;
next_state<=1;
end
endcase
end
// This is a sequential always block
// State flip-flops with asynchronous reset
end
// Output logic
// assign out = (state == ...);
endmodule
Simple FSM 1
// Note the Verilog-1995 module declaration syntax here:
module top_module(clk, reset, in, out);
input clk;
input reset; // Synchronous reset to state B
input in;
output out;//
reg out;
// Fill in state name declarations
reg A=0,B=1;
reg present_state, next_state;
always @(posedge clk) begin
if (reset) begin
present_state<=1;
out<=1;
// Fill in reset logic
end else begin
case (present_state)
B:next_state=in?B:A;
A:next_state=in?A:B;// Fill in state transition logic
endcase
// State flip-flops
present_state = next_state;
case (present_state)
1:out=1;
0:out=0;// Fill in output logic
endcase
end
end
endmodule
注意各逻辑块执行顺序,先改变next_state的值,后改变present_state的值,最后改变out的值。
// Note the Verilog-1995 module declaration syntax here:
module top_module(clk, reset, in, out);
input clk;
input reset; // Synchronous reset to state B
input in;
output out;//
reg out;
parameter A=0, B=1;
reg state, next_state;
always @(*) begin // This is a combinational always block
case(state)
A:next_state = in? A:B;
B:next_state = in? B:A;
endcase// State transition logic
end
always @(posedge clk) begin // This is a sequential always block
if(reset)
state<=B;
else
state<=next_state;// State flip-flops with asynchronous reset
end
// Output logic
// assign out = (state == ...);
assign out = (state == B);
endmodule
Simple FSM 1 (a)
module top_module(
input clk,
input areset, // Asynchronous reset to OFF
input j,
input k,
output out); //
parameter OFF=0, ON=1;
reg state, next_state;
always @(*) begin
case(state)
ON: next_state = k?OFF:ON;
OFF: next_state = j?ON:OFF;
endcase// State transition logic
end
always @(posedge clk, posedge areset) begin
if(areset)
state<=OFF;
else
state<=next_state;
// State flip-flops with asynchronous reset
end
// Output logic
// assign out = (state == ...);
assign out = (state==ON);
endmodule
Simple FSM 2
module top_module(
input clk,
input reset, // Synchronous reset to OFF
input j,
input k,
output out); //
parameter OFF=0, ON=1;
reg state, next_state;
always @(*) begin
case(state)
ON: next_state = k?OFF:ON;
OFF: next_state = j?ON:OFF;
endcase
// State transition logic
end
always @(posedge clk) begin
if(reset)
state<=OFF;
else
state<=next_state;
// State flip-flops with synchronous reset
end
// Output logic
// assign out = (state == ...);
assign out = (state==ON);
endmodule
Simple state transitions 3
module top_module(
input in,
input [1:0] state,
output [1:0] next_state,
output out); //
parameter A=0, B=1, C=2, D=3;
// State transition logic: next_state = f(state, in)
always@(*)
begin
case(state)
A:next_state = in? B:A;
B:next_state = in? B:C;
C:next_state = in? D:A;
D:next_state = in? B:C;
endcase
end
// Output logic: out = f(state) for a Moore state machine
assign out = (state==D)?1:0;
endmodule
Simple one-hot state transitions 3
module top_module(
input in,
input [3:0] state,
output [3:0] next_state,
output out); //
parameter A=0, B=1, C=2, D=3;
// State transition logic: Derive an equation for each state flip-flop.
assign next_state[A] = state[A]&(~in)|state[C]&(~in);
assign next_state[B] = state[A]&in|state[B]&in|state[D]∈
assign next_state[C] = state[B]&(~in)|state[D]&(~in);
assign next_state[D] = state[C]∈
// Output logic:
assign out = state[D];
endmodule
Simple FSM 3(a)
module top_module(
input clk,
input in,
input areset,
output out); //
reg[1:0] A=2'd0,B=2'd1,C=2'd2,D=2'd3;
reg[1:0] state,next_state;
// State transition logic
always@(*)
begin
case(state)
A:next_state = in?B:A;
B:next_state = in?B:C;
C:next_state = in?D:A;
D:next_state = in?B:C;
endcase
end
// State flip-flops with asynchronous reset
always@(posedge clk,posedge areset)
begin
if(areset)
state<=A;
else
state<=next_state;
end
// Output logic
assign out = (state==D);
endmodule
Simple FSM 3
module top_module(
input clk,
input in,
input reset,
output out); //
reg[1:0] A=2'd0,B=2'd1,C=2'd2,D=2'd3;
reg[1:0] state,next_state;
// State transition logic
always@(*)
begin
case(state)
A:next_state = in?B:A;
B:next_state = in?B:C;
C:next_state = in?D:A;
D:next_state = in?B:C;
endcase
end
// State flip-flops with asynchronous reset
always@(posedge clk)
begin
if(reset)
state<=A;
else
state<=next_state;
end
// Output logic
assign out = (state==D);
endmodule
Lemmings 1
module top_module(
input clk,
input areset, // Freshly brainwashed Lemmings walk left.
input bump_left,
input bump_right,
output walk_left,
output walk_right); //
// parameter LEFT=0, RIGHT=1, ...
reg state, next_state;
reg left=0, right=1;
always @(*) begin
// State transition logic
case(state)
left: next_state = bump_left?right:left;
right: next_state = bump_right?left:right;
endcase
end
always @(posedge clk, posedge areset) begin
// State flip-flops with asynchronous reset
if(areset)
state <= left;
else
state <= next_state;
end
// Output logic
// assign walk_left = (state == ...);
// assign walk_right = (state == ...);
assign walk_left = (state == left);
assign walk_right = (state == right);
endmodule
Lemmings 2
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 );
// parameter LEFT=0, RIGHT=1, ...
reg state, next_state;
reg left=0, right=1;
reg f_ground;
always @(*) begin
// State transition logic
case(state)
left: next_state = (bump_left&&ground&&f_ground)?right:left;
right: next_state = (bump_right&&ground&&f_ground)?left:right;
endcase
end
always @(posedge clk, posedge areset) begin
// State flip-flops with asynchronous reset
if(areset)
begin
state <= left;
aaah <= 0;
walk_left <= 1;
walk_right <= 0;
end
else
begin
if(!ground)
begin
aaah <= 1;
walk_left <= 0;
walk_right <= 0;
f_ground <= 0;
end
else
begin
aaah <= 0;
state = next_state;
walk_left <= (state == left);
walk_right <= (state == right);
f_ground<=1;
end
end
end
endmodule
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 );
// parameter LEFT=0, RIGHT=1, ...
reg[1:0] state, next_state;
parameter left=2'd0;
parameter right=2'd1;
parameter ah_left=2'd2;
parameter ah_right=2'd3;
always @(*) begin
// State transition logic
case(state)
left: begin
if(~ground) next_state = ah_left;
else if(bump_left) next_state = right;
else next_state = left;
end
right: begin
if(~ground) next_state = ah_right;
else if(bump_right) next_state = left;
else next_state = right;
end
ah_left: begin
if(~ground) next_state = ah_left;
else next_state = left;
end
ah_right: begin
if(~ground) next_state = ah_right;
else next_state = right;
end
endcase
end
always @(posedge clk, posedge areset) begin
// State flip-flops with asynchronous reset
if(areset)
begin
state <= left;
end
else
state <= next_state;
end
assign walk_left=(state==left);
assign walk_right=(state==right);
assign aaah=(state==ah_right)||(state==ah_left);
endmodule