Вы находитесь на странице: 1из 9

Edd – Rustom R.

Lumayaga

Homework 3

Problem 1: Designing an FSM

Consider the design of a Moore-style FSM with a 1-bit input (x), and a 1-bit output (y). The FSM accepts
inputs one bit at a time and outputs a 0 until it sees the sequence 0101 (with no other bit values in
between). After seeing the second 1 in the sequence it outputs a 1 and then starts over.

(a) Draw the state transition diagram. Label all nodes and arcs.

(b) Derive the next state logic and the output logic equations.

SOLUTION:

(a):
(b): One way is to use one-hot encoding and 5 bits to express states:

You can then use Karnaugh-maps to derive simplified expressions for each bit of
the next state n from the current state c and input x.
Problem 2: Implementing an FSM

Consider the state transition diagram shown below:

(a) Write the Verilog behavior description for the corresponding circuit.

(b) Draw the circuit diagram for one-hot encoded implementation. (You may assume that flip-flops have
both "set" and "reset" inputs, but you must label which one you would use for each flip-flop.)

SOLUTION:

(a)
(b):
Problem 3: Converting from Mealy to Moore Style

Convert the Mealy-style state transition diagram to a Moore-style.

SOLUTION:

Problem 4: Verilog Code for a Vending Machine

The State Diagram of the Vending Machine is given here. It has the following states:

State 1: Reset

State 2: Five

State 3: Ten

State 4: Fifteen

State 5: Twenty

State 6: Twenty Five

The next state is the RESET state again. The diagram is given below and is self-explanatory. Whenever a
coin is dropped, the system jumps to the next state. For example, if the coin dropped from the RESET
State is a NICKEL, then the system jumps to the FIVE State. Otherwise, the system stays in the present
state. When the system gets an extra amount, it goes back to the RESET State and the difference is given
back to the user. Write the Verilog behavior description of this vending machine system.
SOLUTION:

\\module name
module fsm(clock,reset,coin,vend,state,change);

\\these are the inputs and the outputs


input clock;
input reset;
input [2:0]coin;

output vend;
output [2:0]state;
output [2:0]change;

\\defining the registers as change,coin and vend


reg vend;
reg [2:0]change;
wire [2:0]coin;
\\coins are declared as parameters
parameter [2:0]NICKEL=3’b001;
parameter [2:0]DIME=3’b010;
parameter [2:0]NICKEL_DIME=3’b011;
parameter [2:0]DIME_DIME=3’b100;
parameter [2:0]QUARTER=3’b101;

\\states are also declared as parameters

parameter [2:0]IDLE=3’b000;
parameter [2:0]FIVE=3’b001;
parameter [2:0]TEN=3’b010;
parameter [2:0]FIFTEEN=3’b011;
parameter [2:0]TWENTY=3’b100;
parameter [2:0]TWENTYFIVE=3’b101;

\\states are defined as registers


reg [2:0]state,next_state;

\\the machine works on state and coin


always @(state or coin)
begin
next_state=0; \\first next state is given zero
case(state)
IDLE: case(coin) \\this is the idle state
NICKEL: next_state=FIVE;
DIME: next_state=TEN;
QUARTER: next_state=TWENTYFIVE;
default: next_state=IDLE;
endcase

FIVE: case(coin) \\the second state


NICKEL: next_state=TEN;
DIME: next_state=FIFTEEN;
QUARTER: next_state=TWENTYFIVE; //change=NICKEL
default: next_state=FIVE;
endcase

TEN: case(coin) \\the third state


NICKEL: next_state=FIFTEEN;
DIME: next_state=TWENTY;
QUARTER: next_state=TWENTYFIVE; //change=DIME
default: next_state=TEN;
endcase
FIFTEEN: case(coin) \\the fourth state
NICKEL: next_state=TWENTY;
DIME: next_state=TWENTYFIVE;
QUARTER: next_state=TWENTYFIVE; //change==NICKEL_DIME
default: next_state=FIFTEEN;
endcase

TWENTY: case(coin) \\the fifth state


NICKEL: next_state=TWENTYFIVE;
DIME: next_state=TWENTYFIVE; //change=NICKEL
QUARTER: next_state=TWENTYFIVE; //change==DIME_DIME
default: next_state=TWENTY;
endcase

TWENTYFIVE: next_state=IDLE; \\the next state is reset

default : next_state=IDLE;
endcase
end

always @(clock)
begin \\if reset, state=idle and vend=1
if(reset) begin
state <= IDLE;
vend <= 1’b0;
// change <= 3’b000;
end \\ change must be none
else state <= next_state;
case (state) \\ case for next state
\\the states are defined and output is given

IDLE: begin vend <= 1’b0;


change <=3’d0;
end

FIVE: begin vend <= 1’b0;


if (coin==QUARTER) change <=NICKEL;
else change <=3’d0;
end

TEN: begin vend <= 1’b0;


if (coin==QUARTER) change <=DIME;
else change <= 3’d0;
end
FIFTEEN : begin vend <= 1’b0;
if (coin==QUARTER) change <=NICKEL_DIME;
else change <= 3’d0;
end
TWENTY : begin vend <= 1’b0;
if (coin==DIME) change <=NICKEL;
else if (coin==QUARTER) change <=DIME_DIME;
else change <= 3’d0;
end

TWENTYFIVE : begin vend <= 1’b1;


change <=3’d0;
end

default: state <= IDLE;


endcase

end
endmodule