Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a Mealy FSM that implements a candy vending machine where each candy cost

ID: 3820315 • Letter: D

Question

Design a Mealy FSM that implements a candy vending machine where each candy costs 25 cents. The vending machine only accepts dimes (input D) and quarters (input Q), one at a time. The input format is DQ. If both a D and a Q are entered, the state machine does not advance, that is, it stays in the same state. Once 25 cents or more is entered, the vending machine will output a product (output P) and go to the Reset state. If the user enters more than 25 cents total, the vending machine will return some change (output C). Output C will be raised High regardless of how much change is required (assume some other FSM deals with the return of the change). Use state names and assignments following the usual sequences (i.e. S0, S1, etc, and 00...00, 00..01, etc, respectively). Use the minimum number of bits required for state assignments. Use don’t cares if necessary. Obtain the state transition diagram, state transition table, state assignment table, output table, next-state equations, and output equations for this FSM. Simplify the equations as much as possible, use Boolean algebra theorems and axioms to perform the simplifications

Explanation / Answer

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;
6

reg vend;
reg [2:0]change;
wire [2:0]coin;


parameter [2:0]TWENTYFIVE=3’b101;
\AS ALWAYS THE STATES ARE DEFINED AS REG
reg [2:0]state,next_state;
\MY MACHINE WORKS ON STATE AND COIN
always @(state or coin)
begin
next_state=0; \VERYFIRST 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) \THIS IS THE SECOND STATE
7
NICKEL: next_state=TEN;
DIME: next_state=FIFTEEN;
QUARTER: next_state=TWENTYFIVE; //change=NICKEL
default: next_state=FIVE;
endcase
TEN: case(coin) \THIS IS 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) \THIS IS 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) \THIS IS 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 HERE IS THE RESET
default : next_state=IDLE;
endcase
end
always @(clock)
begin \WHENEVER I GIVE A RESET I HAVE TO MAKE THE STATE TO IDLE AND VEND TO 1
if(reset) begin
state <= IDLE;
vend <= 1’b0;
// change <= 3’b000;
end \THE CHANGE ALSO HAS TO BECOME NONE
else state <= next_state;
8
case (state) \HERE WE DECIDE THE NEXT STATE
\ALL THE STATES ARE DEFINED HERE AND THE OUTPUT IS ALSO 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