1. Simple FSM 1(asynchronous reset) 异步复位的简单FSM
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 transition logic 状态转换逻辑
case(state)
A: begin
if(in==1'b1)
next_state<= A;
else
next_state<= B;
end
B: begin
if(in == 1'b1)
next_state<= B;
else
next_state<=A;
end
endcase
end
always @(posedge clk, posedge areset) begin // This is a sequential always block
// State flip-flops with asynchronous reset 复位逻辑/
if(areset)
state<=B ;
else
state<=next_state;
end
// Output logic 输出逻辑
// assign out = (state == ...);
assign out = ( state == B);
endmodule
module top_module (
input clk,
input in,
input areset,
output out
);
// Give state names and assignments. I'm lazy, so I like to use decimal numbers.
// It doesn't really matter what assignment is used, as long as they're unique.
parameter A=0, B=1;
reg state; // Ensure state and next are big enough to hold the state encoding.
reg next;
// A finite state machine is usually coded in three parts:
// State transition logic
// State flip-flops
// Output logic
// It is sometimes possible to combine one or more of these blobs of code
// together, but be careful: Some blobs are combinational circuits, while some
// are clocked (DFFs).
// Combinational always block for state transition logic. Given the current state and inputs,
// what should be next state be?
// Combinational always block: Use blocking assignments.
always@(*) begin
case (state)
A: next = in ? A : B;
B: next = in ? B : A;
endcase
end
// Edge-triggered always block (DFFs) for state flip-flops. Asynchronous reset.
always @(posedge clk, posedge areset) begin
if (areset) state <= B; // Reset to state B
else state <= next; // Otherwise, cause the state to transition
end
// Combinational output logic. In this problem, an assign statement is the simplest.
// In more complex circuits, a combinational always block may be more suitable.
assign out = (state==B);
endmodule
2. Simple FSM 1(synchronous reset) 同步复位
// 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 present_state, next_state;
always @(posedge clk) begin
if (reset) begin
// Fill in reset logic
present_state <= 1'b1;
out<=1'b1;
end else begin
case (present_state)
// Fill in state transition logic
1'b1:next_state = in ? 1'b1 : 1'b0 ;
1'b0:next_state = in ? 1'b0 : 1'b1 ;
endcase
// State flip-flops
present_state = next_state;
case (present_state)
// Fill in output logic
1'b1:out<=1'b1;
1'b0:out<=1'b0;
endcase
end
end
endmodule
3. Simple FSM 2 (asynchronous reset)
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
// State transition logic
case(state)
OFF: next_state= j ? ON : OFF;
ON : next_state= k ? OFF : ON;
endcase
end
always @(posedge clk, posedge areset) begin
// State flip-flops with asynchronous reset
if(areset)
state <= OFF;
else
state <= next_state;
end
// Output logic
// assign out = (state == ...);
assign out = (state == ON);
endmodule
4.Simple FSM 2 (synchronous reset)
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
// State transition logic
case(state)
OFF: next_state= j ? ON : OFF;
ON : next_state= k ? OFF : ON;
endcase
end
always @(posedge clk, posedge areset) begin
// State flip-flops with asynchronous reset
if(reset)
state <= OFF;
else
state <= next_state;
end
// Output logic
// assign out = (state == ...);
assign out = (state == ON);
endmodule
5. 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);
endmodule
6. Simple one-hot state transition 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]|state[D])&(~in);
assign next_state[D] = state[C]∈
// Output logic:
assign out = ( state[D]==1 );
endmodule
7.Simple FSM 3(asynchronous reset)
module top_module(
input clk,
input in,
input areset,
output out); //
// State transition logic
parameter A=0,B=1,C=2,D=3;
reg [1:0]state,next;
always@(*)begin
case(state)
A: next = in ? B : A ;
B: next = in ? B : C ;
C: next = in ? D : A ;
D: next = in ? B : C ;
endcase
end
// State flip-flops with asynchronous reset
always@(posedge clk, posedge areset)begin
if(areset)
state <= A;
else state<=next;
end
// Output logic
assign out=(state==D);
endmodule
8. Simple FSM 3(synchronous reset)
module top_module(
input clk,
input in,
input reset,
output out); //
// State transition logic
parameter A=0,B=1,C=2,D=3;
reg [1:0]state,next;
always@(*)begin
case(state)
A: next = in ? B : A ;
B: next = in ? B : C ;
C: next = in ? D : A ;
D: next = in ? B : C ;
endcase
end
// State flip-flops with synchronous reset
always@(posedge clk)begin
if(reset)
state <= A;
else state<=next;
end
// Output logic
assign out=(state==D);
endmodule
9. Design a Moore FSM
module top_module (
input clk,
input reset,
input [3:1] s,
output fr3,
output fr2,
output fr1,
output dfr
);
//A表示Below S1,B1表示上升到Between S1 and S2,C1表示S1-S2上升到S2-S3,
//D表示Above S3,B2表示下降到Between S1 and S2,C2表示D下降到S2-S3。
parameter A=0,B1=1,C1=2,D=3,C2=4,B2=5;
reg [2:0] state,next_state;
always @(posedge clk) begin
if(reset) state<=A;
else state<=next_state;
end
always @(*) begin
case(state)
A: next_state=s[1]?(s[2]?(s[3]?D:C1):B1):A;
B1: next_state=s[1]?(s[2]?(s[3]?D:C1):B1):A;
C1: next_state=s[1]?(s[2]?(s[3]?D:C1):B2):A;
D: next_state=s[1]?(s[2]?(s[3]?D:C2):B2):A;
C2: next_state=s[1]?(s[2]?(s[3]?D:C2):B2):A;
B2: next_state=s[1]?(s[2]?(s[3]?D:C1):B2):A;
endcase
/*
本题也可以不用判断所有s信号
case(state)
A: next_state=s[1]?B1:A;
B1: next_state=s[1]?(s[2]?C1:B1):A;
C1: next_state=s[2]?(s[3]?D:C1):B2;
D: next_state=s[3]?D:C2;
C2: next_state=s[2]?(s[3]?D:C2):B2;
B2: next_state=s[1]?(s[2]?C1:B2):A;
endcase
*/
end
//根据表格可以写出fr3,fr2,fr1
assign fr3=(state==A);
assign fr2=(state==A|state==B1|state==B2);
assign fr1=(state==A|state==B1|state==B2)|(state==C2|state==C1);
//B2,C2状态分别由下降得到,所以当处于这两个状态时dfr为1,
//A状态为水位最低的状态,dfr为1
assign dfr=(state==B2)|(state==C2)|(state==A);
endmodule
10.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, ...
parameter LEFT=0, RIGHT=1;
reg state, next_state;
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 == LEFT);
assign walk_right = (state == RIGHT);
endmodule
module top_module (
input clk,
input areset,
input bump_left,
input bump_right,
output walk_left,
output walk_right
);
// Give state names and assignments. I'm lazy, so I like to use decimal numbers.
// It doesn't really matter what assignment is used, as long as they're unique.
parameter WL=0, WR=1;
reg state;
reg next;
// Combinational always block for state transition logic. Given the current state and inputs,
// what should be next state be?
// Combinational always block: Use blocking assignments.
always@(*) begin
case (state)
WL: next = bump_left ? WR : WL;
WR: next = bump_right ? WL : WR;
endcase
end
// Combinational always block for state transition logic. Given the current state and inputs,
// what should be next state be?
// Combinational always block: Use blocking assignments.
always @(posedge clk, posedge areset) begin
if (areset) state <= WL;
else state <= next;
end
// Combinational output logic. In this problem, an assign statement are the simplest.
// In more complex circuits, a combinational always block may be more suitable.
assign walk_left = (state==WL);
assign walk_right = (state==WR);
endmodule
11.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, FALL_L=2, FALL_R=3
reg [2:0]state,next;
parameter LEFT=0, RIGHT=1, FALL_L=2, FALL_R=3;
//state transition logic
always@(*)begin
case(state)
LEFT:next=ground?(bump_left?RIGHT:LEFT):FALL_L;
RIGHT:next=ground?(bump_right?LEFT:RIGHT):FALL_R;
FALL_L:next=ground?(LEFT):FALL_L;
FALL_R:next=ground?(RIGHT):FALL_R;
endcase
end
//state flip_flops with asynchronous areset
always@(posedge clk, posedge areset)begin
if(areset) state<=LEFT;
else state<=next;
end
//output logic
assign walk_left=(state==LEFT);
assign walk_right=(state==RIGHT);
assign aaah=(state==FALL_L || state==FALL_R );
endmodule
12.Lemmings 3
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 );
//parameter LEFT=0, RIGHT=1, FALL_L=2, FALL_R=3
reg [2:0]state,next;
parameter LEFT=0, RIGHT=1, FALL_L=2, FALL_R=3,dig_L=4, dig_R=5;
//state transition logic
always@(*)begin
case(state)
LEFT:next=ground?(dig?dig_L:(bump_left?RIGHT:LEFT)):FALL_L;
RIGHT:next=ground?(dig?dig_R:(bump_right?LEFT:RIGHT)):FALL_R;
FALL_L:next=ground?LEFT:FALL_L;
FALL_R:next=ground?RIGHT:FALL_R;
dig_L: next=ground?dig_L:FALL_L;
dig_R: next=ground?dig_R:FALL_R;
endcase
end
//state flip_flops with asynchronous areset
always@(posedge clk, posedge areset)begin
if(areset) state<=LEFT;
else state<=next;
end
//output logic
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
13. Limmings 4
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 );
//parameter LEFT=0, RIGHT=1, FALL_L=2, FALL_R=3,dig_L=4, dig_R=5, splat=6
reg [2:0]state,next;
parameter LEFT=0, RIGHT=1, FALL_L=2, FALL_R=3,dig_L=4, dig_R=5, splat=6;
reg [4:0]T;
reg signal;
//state transition logic
always@(*)begin
case(state)
LEFT:next=ground?(dig?dig_L:(bump_left?RIGHT:LEFT)):FALL_L;
RIGHT:next=ground?(dig?dig_R:(bump_right?LEFT:RIGHT)):FALL_R;
dig_L: next=ground?dig_L:FALL_L;
dig_R: next=ground?dig_R:FALL_R;
splat: next=splat;
FALL_L:begin
if(signal)
next=ground?splat:FALL_L;
else
next=ground?LEFT:FALL_L;
end
FALL_R:begin
if(signal)
next=ground?splat:FALL_R;
else
next=ground?RIGHT:FALL_R;
end
endcase
end
//splat
always@(posedge clk, posedge areset)begin
if(areset) T<=0;
else if(state==FALL_L||state==FALL_R)
begin
if(T==19)
signal<=1;
else
T<=T+1;
end
else begin
signal<=0;
T<=0;
end
end
//state flip_flops with asynchronous areset
always@(posedge clk, posedge areset)begin
if(areset) state<=LEFT;
else state<=next;
end
//output logic
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
14.One-hot FSM
module top_module(
input in,
input [9:0] state,
output [9:0] next_state,
output out1,
output out2);
//state trasitision logic
assign next_state[0]= ~in&(state[0]|state[1]|state[2]|state[3]|state[4]|state[7]|state[8]|state[9]);
assign next_state[1]= in&(state[0]|state[8]|state[9]);
assign next_state[6:2]={in&state[5],in&state[4],in&state[3],in&state[2],in&state[1]};
assign next_state[7]= in&(state[6]|state[7]);
assign next_state[8]= ~in&state[5];
assign next_state[9]=~in&state[6];
//output logic
assign out1=((state[8]==1)|| (state[9]==1));
assign out2=((state[7]==1)|| (state[9]==1));
endmodule
15.PS/2 packet parser
module top_module(
input clk,
input [7:0] in,
input reset, // Synchronous reset
output done); //
parameter s0=0,s1=1,s2=2,s3=3;
reg[1:0] state,next;
// State transition logic (combinational)
always@(*)begin
case(state)
s0: next=in[3]?s1:s0;
s1: next=s2;
s2: next=s3;
s3: next=in[3]?s1:s0;
endcase
end
// State flip-flops (sequential)
always@(posedge clk)begin
if(reset) state<=s0;
else state<=next;
end
// Output logic
assign done=(state==3);
endmodule
16. PS/2 packet parser and datapath
module top_module(
input clk,
input [7:0] in,
input reset, // Synchronous reset
output [23:0] out_bytes,
output done); //
parameter s0=0,s1=1,s2=2,s3=3;
reg[1:0]state,next;
// FSM from fsm_ps2
always@(*)begin
case(state)
s0: next=in[3]?s1:s0;
s1: next=s2;
s2: next=s3;
s3: next=in[3]?s1:s0;
endcase
end
always@(posedge clk)begin
if(reset) state<=s0;
else state<=next;
end
assign done=(state==s3);
// New: Datapath to store incoming bytes.
always@(posedge clk)begin
case(state)
s0: out_bytes[23:16]<=in;
s1: out_bytes[15:8]<=in;
s2: out_bytes[7:0]<=in;
s3: out_bytes[23:16]<=in;
endcase
end
endmodule