behavioral descriptions in verilog hdl

TODAY’S OUTLINE
Finite State Machine
 Exercise
1
 Exercise 2
 Exercise 3
 Exercise 4
Exercise 1
Solution 1
module FSM1 (reset, brake, accelerator, clock, constant_speed, decelerating, accelerating );
input reset, brake, accelerator, clock;
output constant_speed, decelerating, accelerating ;
reg constant_speed, decelerating, accelerating ;
parameter S0 = 2'b00;
parameter S1 = 2'b01;
parameter S2 = 2'b10;
parameter S3 = 2'b11;
reg [1:0] state, next_state;
always @ (posedge clk)
begin
case (state)
S0: begin
if (!brake && accelerator)
begin
next_state = S1;
{accelerating,constant_speed,decelerating} = 3’b100;
end
else if (brake)
begin
next_state = S0;
{accelerating,constant_speed,decelerating} = 3’b010;
end
end
Cont..
S1: begin
if (!brake && accelerator)
begin
next_state = S2;
{accelerating,constant_speed,decelerating} = 3’b100;
end
else if (brake)
begin
next_state = S0;
{accelerating,constant_speed,decelerating} = 3’b001;
end
end
S2: begin
if (!brake && accelerator)
begin
next_state = S3;
{accelerating,constant_speed,decelerating} = 3’b100;
end
else if (brake)
begin
next_state = S1;
{accelerating,constant_speed,decelerating} = 3’b001;
end
end
Cont..
S3: begin
if (!brake && accelerator)
begin
next_state = S3;
{accelerating,constant_speed,decelerating} = 3’b100;
end
else if (brake)
begin
next_state = S2;
{accelerating,constant_speed,decelerating} = 3’b001;
end
end
default: begin
next_state = S0;
{accelerating,constant_speed,decelerating} = 3’b000;
end
endcase
end
always @(posedge clk or posedge reset)
begin
if (reset) state <= S0;
else
state <= next_state;
end
endmodule
Exercise 2
State Transition Table
PS
(Present State)
NS
(Next State)
Output (Y)
X=0
X=1
X=0
X=1
A
A
B
0
0
B
C
D
0
0
C
A
D
0
0
D
E
F
0
1
E
A
F
1
1
F
G
F
1
1
G
A
F
0
1
Exercise 3
State Transition Table
PS
(Present State)
NS
(Next State)
Output (Y)
X=0
X=1
A
A
B
0
B
C
D
0
C
A
D
0
D
E
F
1
E
A
F
1
F
G
F
1
G
A
F
1
Exercise 4
Sequence of events when you answer the telephone.

We begin the process by waiting for the telephone to ring; during
this time, we are in a wait state, (designated W)

When the phone rings, you pick up the handset and change to the
answer state (designated A).

At this point, one of four things may happen:

the person making the call asks for you;

the person making the call asks for someone who is
currently at home;

the person making the call asks for someone who is
currently not at home;

it is a wrong number.

If the call is for you, you say hello and change to the talking state
(designated T).
Cont..

If the call is for someone who is currently at home, you
tell the person making the call to wait and move to a
state where you are getting the person they desire
(designated G).

If the call is for someone who is currently not at home,
you tell the caller and change to the state where you are
taking a message (designated M).

If it is a wrong number, you tell the caller and move to
the state where the call is finishing (designated F).

Once in the talking state, whenever the caller talks you
reply and stay in the talking state.
Cont..

If however, the caller says goodbye, you say goodbye
and move to the finish state.

If you were in the state where you were getting someone
else to come to the phone, when they
arrive they say hello and enter the talking state.

If you are taking a message and the caller says
goodbye, you say goodbye and move to the finish state.

Once in the finish state, when the caller hangs up, you
hang up the handset and change to the wait state.
Next week Lab 4
(Finite State Machine)
Do some homework…
That’s all for today.
See u on Tuesday….