SlideShare a Scribd company logo
1 of 18
Download to read offline
Vending Machine using Verilog
Ajay Sharma
3rd May 05
Contents
1 Introduction 2
2 Finite State Machine 3
2.1 MOORE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 MEALY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.3 State Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3 Verilog Code of the Machine 6
4 TesT Bench 10
5 Output 14
6 Explaination of Output 17
1
Chapter 1
Introduction
Vending Machine is a soft drink dispensor machine that dispenses drink based
on the amount deposited in the machine. It accepts all the coins ie: Nickel(5
cents), Dime(10 cents), Quarter(25 cents). Till it recieves 40 cents it will not
dispense anything. After it has recieved 40 cents it will dispense a softdrink.
Any amount above that will be given back as a change.
The diagram below is the block diagram of the vending machine.The in-
puts are the coins and the output is the drink.
In the next chapter we will see the how we can encode the state machine
in a state diagram and draw it.
2
Chapter 2
Finite State Machine
Any Sequential digital circuit can be converted into a state machine using
state diagram. In a State machine the circuit’s output is defined in a different
set of states ie. each output is a state. There is a State Register to hold the
state of the machine and a nextstate logic to decode the nextstate. There is
also a output register that defines the output of the machine. The nextstate
logic is the sequential part of the machine and the Output and Currentstate
are the Register part of the logic.
There are two types of state machines:
1. MOORE
2. MEALY
Lets see each:
2.1 MOORE
In a moore machine the output state is totally dependent on the present
state. The diagram shows the information.
3
2.2 MEALY
In a mealy machine the output depends on the input as well as the present
state.
2.3 State Diagram
The State Diagram of the Vending Machine is given here. It has the following
states:
1. State 1: reset
2. State 2: Five
3. State 3: Ten
4. State 4: Fifteen
5. State 5: Twenty
6. State 6: Twenty Five
The next state is the Reset state again. The diagram is given below and is
self explanatory.
4
What happens is whenever we get a coin we jump to the next state. So
for example we get a coin from the reset state say a NICKEL, then we jump
to the next state FIVE . Otherwise we stay in the same state. When we get
Extra amount we come back to the reset state and the difference is given
back to the user.
In the next chapter we will see the Verilog Code for the Vending Ma-
chine/State Machine.
5
Chapter 3
Verilog Code of the Machine
The code below is the verilog Code of the State Diagram. There are two
parts to the code. The first being the Sequential Logic that decides where
to go nex or the change in state. The second being the one that decides the
Outputs of each state. The Code is as follows:
***********************
***AUTHOR : AJAY SHARMA
************************
please see the website:
******************************
 http://www.csun.edu/~ags55111
******************************
for the entire source code of the machine
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;
6
i need to define the registers as change,coin and vend
reg vend;
reg [2:0]change;
wire [2:0]coin;
my coins are declared as parameters to make reading better.
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;
MY STATES ARE ALSO PARAMETERS . I DONT WANT TO MAKE YOU READ
IN MACHINE LANGUAGE
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;
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’d
TEN: begin vend <= 1’b0; if (coin==QUARTER) change <=DIME; else change <= 3’d
FIFTEEN : begin vend <= 1’b0; if (coin==QUARTER) change <=NICKEL_DIME; else cha
TWENTY : begin vend <= 1’b0; if (coin==DIME) change <=NICKEL; else if (coin==QU
TWENTYFIVE : begin vend <= 1’b1; change <=3’d0; end
default: state <= IDLE;
endcase
end
endmodule
In the next chapter we see the Test Bench of the machine.
9
Chapter 4
TesT Bench
We try to test the machine for the outputs . We add coins to the machine
and see after which state does it give output or Softdrink.
The testbench code is given below.
AUTHOR: AJAY SHARMA
PLEASE VISIT: HTTP://WWW.CSUN.EDU/~AGS55111
FOR THE ENTIRE SOURCE CODE OF THE MACHINE.
OK
SO I HAVE TO INCLUDE THE STATEMACHINE VERILOG CODE
IN THIS FILE SO THAT THE I CAN RUN IT AND TEST IT HERE.
‘include "fsm2.v"
THIS IS THE HIGHEST LEVEL OF THE MODULE: THE TEST BENCH.
module test;
THE INPUT IN THE FSM MODULE ARE REG HERE
reg clock,reset;
reg [2:0]coin;
THE OUTPUT IN THE FSM MODULE ARE WIRES HERE
wire vend;
wire [2:0]state;
wire [2:0]change;
10
THE PARAMETERS AGAIN FOR THE COIN AND STATE
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;
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;
I MONITOR THE TIME,DRINK,RESET,CLOCK,STATE AND CHANGE FOR CHANGES.
initial begin
$display("Timetcointdrinktresettclocktstatetchange");
$monitor("%gt%bt%bt%bt%bt%dt%d",$time,coin,vend,reset,clock,state,change);
NEW FEATURE: MY MACHINE HAS THE FACILITY TO DUMP VARIABLES SO THAT
 I CAN VIEW THEM USING A VCD VIEWER.
$dumpvars;
$dumpfile("file.vcd"); // Dump output file.
THIS IS WHERE THE COINS ARE ADDED.
clock=0;
reset=1; FIRST LETS RESET THE MACHINE
#2 reset=0;
coin=NICKEL; CHECK FOR STATE 1
#2 reset=1; coin=2’b00;
#2 reset=0;
coin=DIME;
RESET AGAIN AND CHECK FOR STATE 2
#2 reset=1; coin=2’b00;
#2 reset=0;
RESET AGAIN AND CHECK FOR STATE 5
11
coin=QUARTER;
#2 reset=1; coin=2’b00;
#2 reset=0;
RESET AGAIN AND CHECK FOR STATE 5
coin=NICKEL;
#2 coin=NICKEL;
#2 coin=NICKEL;
#2 coin=NICKEL;
#2 coin=NICKEL;
#2 reset=1; coin=2’b00;
#2 reset=0;
RESET AGAIN AND CHECK FOR STATE 5 AND SO ON
coin=NICKEL;
#2 coin=DIME;
#2 coin=DIME;
#2 reset=1; coin=2’b00;
#2 reset=0;
coin=NICKEL;
#2 coin=DIME;
#2 coin=QUARTER;
#2 reset=1; coin=2’b00;
#2 reset=0;
coin=NICKEL;
#2 coin=NICKEL;
#2 coin=NICKEL;
#2 coin=DIME;
#2 reset=1; coin=2’b00;
#2 reset=0;
coin=NICKEL;
#2 coin=NICKEL;
#2 coin=NICKEL;
#2 coin=NICKEL;
#2 coin=DIME;
#2 reset=1; coin=2’b00;
#2 reset=0;
coin=NICKEL;
#2 coin=NICKEL;
#2 coin=QUARTER;
12
#2 reset=1; coin=2’b00;
#2 reset=0;
coin=NICKEL;
#2 coin=QUARTER;
#2 reset=1; coin=2’b00;
#2 $finish;
end
THE CLOCK NEEDS TO TICK EVERY 2 TIME UNIT
always
#1 clock=~clock;
//always @(state)
// coin=!coin;
initial begin
if (reset)
coin=2’b00;
end
THIS IS WHERE I INSTANTIATE THE MACHINE
fsm inst1(clock,reset,coin,vend,state,change);
endmodule
13
Chapter 5
Output
This chapter describes the output of the machine
GPLCVER_2.10c of 02/28/05 (Linux-elf).
Copyright (c) 1991-2005 Pragmatic C Software Corp.
All Rights reserved. Licensed under the GNU General Public License (GPL).
See the ’COPYING’ file for details. NO WARRANTY provided.
Today is Wed May 11 03:36:14 2005.
Compiling source file "fsmtest2.v"
Highest level modules:
test
Time coin drink reset clock state change
0 000 0 1 0 0 x
1 000 0 1 1 0 0
2 001 0 0 0 0 0
3 001 0 0 1 1 0
4 000 0 1 0 0 0
5 000 0 1 1 0 0
6 010 0 0 0 0 0
7 010 0 0 1 2 0
8 000 0 1 0 0 0
9 000 0 1 1 0 0
10 101 0 0 0 0 0
11 101 0 0 1 5 0
14
12 000 1 1 0 0 0
13 000 0 1 1 0 0
14 001 0 0 0 0 0
15 001 0 0 1 1 0
16 001 0 0 0 2 0
17 001 0 0 1 3 0
18 001 0 0 0 4 0
19 001 0 0 1 5 0
20 001 1 0 0 0 0
21 001 0 0 1 1 0
22 001 0 0 0 2 0
23 001 0 0 1 3 0
24 000 0 1 0 0 0
25 000 0 1 1 0 0
26 001 0 0 0 0 0
27 001 0 0 1 1 0
28 010 0 0 0 2 0
29 010 0 0 1 4 0
30 010 0 0 0 5 1
31 010 1 0 1 0 0
32 000 0 1 0 0 0
33 000 0 1 1 0 0
34 001 0 0 0 0 0
35 001 0 0 1 1 0
36 010 0 0 0 2 0
37 010 0 0 1 4 0
38 101 0 0 0 5 4
39 101 1 0 1 0 0
40 000 0 1 0 0 0
41 000 0 1 1 0 0
42 001 0 0 0 0 0
43 001 0 0 1 1 0
44 001 0 0 0 2 0
45 001 0 0 1 3 0
46 001 0 0 0 4 0
47 001 0 0 1 5 0
48 010 1 0 0 0 0
49 010 0 0 1 2 0
15
50 000 0 1 0 0 0
51 000 0 1 1 0 0
52 001 0 0 0 0 0
53 001 0 0 1 1 0
54 001 0 0 0 2 0
55 001 0 0 1 3 0
56 001 0 0 0 4 0
57 001 0 0 1 5 0
58 001 1 0 0 0 0
59 001 0 0 1 1 0
60 010 0 0 0 2 0
61 010 0 0 1 4 0
62 000 0 1 0 0 0
63 000 0 1 1 0 0
64 001 0 0 0 0 0
65 001 0 0 1 1 0
66 001 0 0 0 2 0
67 001 0 0 1 3 0
68 101 0 0 0 4 3
69 101 0 0 1 5 4
70 000 1 1 0 0 0
71 000 0 1 1 0 0
72 001 0 0 0 0 0
73 001 0 0 1 1 0
74 101 0 0 0 2 1
75 101 0 0 1 5 2
76 000 1 1 0 0 0
77 000 0 1 1 0 0
Halted at location **fsmtest2.v(82) time 78 from call to $finish.
There were 0 error(s), 0 warning(s), and 15 inform(s).
16
Chapter 6
Explaination of Output
The Output is explained here. We start by adding coins. We start first with
the least amount NICKEL. Then we reset the machine to see the output ie:
state and the SOFTDRINK. Then we add a DIME and see the result. So
on till till QUARTER is also added. Then i add a dime 5 times to see the
output and it vends. ie. a softdrink is given . When i add an extra amount
it gives a change also. so my machine works.
The next page has the state diagram of the machine.
17

More Related Content

Similar to Kk

Data Encryption Standard (DES)
Data Encryption Standard (DES)Data Encryption Standard (DES)
Data Encryption Standard (DES)Amir Masinaei
 
DEF CON 23 - Yaniv Balmas and Lior Oppenheim - key logger-video mouse
DEF CON 23 - Yaniv Balmas and Lior Oppenheim - key logger-video mouseDEF CON 23 - Yaniv Balmas and Lior Oppenheim - key logger-video mouse
DEF CON 23 - Yaniv Balmas and Lior Oppenheim - key logger-video mouseFelipe Prado
 
Binárna číselná sústava - Бинарни бројни систем
Binárna číselná sústava - Бинарни бројни системBinárna číselná sústava - Бинарни бројни систем
Binárna číselná sústava - Бинарни бројни системDarina Poljak
 
calpine 8K101705
calpine 8K101705calpine 8K101705
calpine 8K101705finance29
 
calpine 8K101705
calpine 8K101705calpine 8K101705
calpine 8K101705finance29
 
Css rete counter strike source
Css rete counter strike sourceCss rete counter strike source
Css rete counter strike sourceAhmad Efendi
 
Semester project report_Prafulla_Kumar_Shahi
Semester project report_Prafulla_Kumar_ShahiSemester project report_Prafulla_Kumar_Shahi
Semester project report_Prafulla_Kumar_ShahiPrafulla Kumar Shahi
 
Campaña Jorge diapositivas_proyecto_cinematica_en_coordenadas_tangenciales_no...
Campaña Jorge diapositivas_proyecto_cinematica_en_coordenadas_tangenciales_no...Campaña Jorge diapositivas_proyecto_cinematica_en_coordenadas_tangenciales_no...
Campaña Jorge diapositivas_proyecto_cinematica_en_coordenadas_tangenciales_no...JORGELUISCAMPAAREINO
 
001 network toi_basics_v1
001 network toi_basics_v1001 network toi_basics_v1
001 network toi_basics_v1Hisao Tsujimura
 
Reliable multimedia transmission under noisy condition
Reliable multimedia transmission under noisy conditionReliable multimedia transmission under noisy condition
Reliable multimedia transmission under noisy conditionShahrukh Ali Khan
 
2012 University of Dayton .docx
2012   University of Dayton                   .docx2012   University of Dayton                   .docx
2012 University of Dayton .docxeugeniadean34240
 
Unidrive m200 m201 sheet issue 1
Unidrive m200   m201 sheet issue 1Unidrive m200   m201 sheet issue 1
Unidrive m200 m201 sheet issue 1Toàn Huỳnh
 
Thesis-presentation: Tuenti Engineering
Thesis-presentation: Tuenti EngineeringThesis-presentation: Tuenti Engineering
Thesis-presentation: Tuenti EngineeringMarcus Ljungblad
 
Guia de configuracao_ms9520
Guia de configuracao_ms9520Guia de configuracao_ms9520
Guia de configuracao_ms9520plisleo
 
Evaluation of chem lab software.
Evaluation of chem lab software.Evaluation of chem lab software.
Evaluation of chem lab software.Abir Almaqrashi
 
Unidrive m400 sheet issue 1
Unidrive m400 sheet issue 1Unidrive m400 sheet issue 1
Unidrive m400 sheet issue 1Toàn Huỳnh
 

Similar to Kk (20)

Amos command
Amos commandAmos command
Amos command
 
Data Encryption Standard (DES)
Data Encryption Standard (DES)Data Encryption Standard (DES)
Data Encryption Standard (DES)
 
DEF CON 23 - Yaniv Balmas and Lior Oppenheim - key logger-video mouse
DEF CON 23 - Yaniv Balmas and Lior Oppenheim - key logger-video mouseDEF CON 23 - Yaniv Balmas and Lior Oppenheim - key logger-video mouse
DEF CON 23 - Yaniv Balmas and Lior Oppenheim - key logger-video mouse
 
Binárna číselná sústava - Бинарни бројни систем
Binárna číselná sústava - Бинарни бројни системBinárna číselná sústava - Бинарни бројни систем
Binárna číselná sústava - Бинарни бројни систем
 
calpine 8K101705
calpine 8K101705calpine 8K101705
calpine 8K101705
 
calpine 8K101705
calpine 8K101705calpine 8K101705
calpine 8K101705
 
Dee2034 chapter 5 counter
Dee2034 chapter 5 counterDee2034 chapter 5 counter
Dee2034 chapter 5 counter
 
04 3 보수부동소수문자형제어문자
04 3 보수부동소수문자형제어문자04 3 보수부동소수문자형제어문자
04 3 보수부동소수문자형제어문자
 
Css rete counter strike source
Css rete counter strike sourceCss rete counter strike source
Css rete counter strike source
 
Flashback ITOUG
Flashback ITOUGFlashback ITOUG
Flashback ITOUG
 
Semester project report_Prafulla_Kumar_Shahi
Semester project report_Prafulla_Kumar_ShahiSemester project report_Prafulla_Kumar_Shahi
Semester project report_Prafulla_Kumar_Shahi
 
Campaña Jorge diapositivas_proyecto_cinematica_en_coordenadas_tangenciales_no...
Campaña Jorge diapositivas_proyecto_cinematica_en_coordenadas_tangenciales_no...Campaña Jorge diapositivas_proyecto_cinematica_en_coordenadas_tangenciales_no...
Campaña Jorge diapositivas_proyecto_cinematica_en_coordenadas_tangenciales_no...
 
001 network toi_basics_v1
001 network toi_basics_v1001 network toi_basics_v1
001 network toi_basics_v1
 
Reliable multimedia transmission under noisy condition
Reliable multimedia transmission under noisy conditionReliable multimedia transmission under noisy condition
Reliable multimedia transmission under noisy condition
 
2012 University of Dayton .docx
2012   University of Dayton                   .docx2012   University of Dayton                   .docx
2012 University of Dayton .docx
 
Unidrive m200 m201 sheet issue 1
Unidrive m200   m201 sheet issue 1Unidrive m200   m201 sheet issue 1
Unidrive m200 m201 sheet issue 1
 
Thesis-presentation: Tuenti Engineering
Thesis-presentation: Tuenti EngineeringThesis-presentation: Tuenti Engineering
Thesis-presentation: Tuenti Engineering
 
Guia de configuracao_ms9520
Guia de configuracao_ms9520Guia de configuracao_ms9520
Guia de configuracao_ms9520
 
Evaluation of chem lab software.
Evaluation of chem lab software.Evaluation of chem lab software.
Evaluation of chem lab software.
 
Unidrive m400 sheet issue 1
Unidrive m400 sheet issue 1Unidrive m400 sheet issue 1
Unidrive m400 sheet issue 1
 

Recently uploaded

Russian Faridabad Call Girls(Badarpur) : ☎ 8168257667, @4999
Russian Faridabad Call Girls(Badarpur) : ☎ 8168257667, @4999Russian Faridabad Call Girls(Badarpur) : ☎ 8168257667, @4999
Russian Faridabad Call Girls(Badarpur) : ☎ 8168257667, @4999Tina Ji
 
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...anilsa9823
 
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableDipal Arora
 
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best ServicesMysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best ServicesDipal Arora
 
It will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayIt will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayNZSG
 
Grateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfGrateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfPaul Menig
 
The Coffee Bean & Tea Leaf(CBTL), Business strategy case study
The Coffee Bean & Tea Leaf(CBTL), Business strategy case studyThe Coffee Bean & Tea Leaf(CBTL), Business strategy case study
The Coffee Bean & Tea Leaf(CBTL), Business strategy case studyEthan lee
 
Progress Report - Oracle Database Analyst Summit
Progress  Report - Oracle Database Analyst SummitProgress  Report - Oracle Database Analyst Summit
Progress Report - Oracle Database Analyst SummitHolger Mueller
 
Keppel Ltd. 1Q 2024 Business Update Presentation Slides
Keppel Ltd. 1Q 2024 Business Update  Presentation SlidesKeppel Ltd. 1Q 2024 Business Update  Presentation Slides
Keppel Ltd. 1Q 2024 Business Update Presentation SlidesKeppelCorporation
 
Ensure the security of your HCL environment by applying the Zero Trust princi...
Ensure the security of your HCL environment by applying the Zero Trust princi...Ensure the security of your HCL environment by applying the Zero Trust princi...
Ensure the security of your HCL environment by applying the Zero Trust princi...Roland Driesen
 
Monthly Social Media Update April 2024 pptx.pptx
Monthly Social Media Update April 2024 pptx.pptxMonthly Social Media Update April 2024 pptx.pptx
Monthly Social Media Update April 2024 pptx.pptxAndy Lambert
 
9599632723 Top Call Girls in Delhi at your Door Step Available 24x7 Delhi
9599632723 Top Call Girls in Delhi at your Door Step Available 24x7 Delhi9599632723 Top Call Girls in Delhi at your Door Step Available 24x7 Delhi
9599632723 Top Call Girls in Delhi at your Door Step Available 24x7 DelhiCall Girls in Delhi
 
VIP Kolkata Call Girl Howrah 👉 8250192130 Available With Room
VIP Kolkata Call Girl Howrah 👉 8250192130  Available With RoomVIP Kolkata Call Girl Howrah 👉 8250192130  Available With Room
VIP Kolkata Call Girl Howrah 👉 8250192130 Available With Roomdivyansh0kumar0
 
Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023Neil Kimberley
 
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...Dave Litwiller
 
Pharma Works Profile of Karan Communications
Pharma Works Profile of Karan CommunicationsPharma Works Profile of Karan Communications
Pharma Works Profile of Karan Communicationskarancommunications
 
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdfRenandantas16
 
Cash Payment 9602870969 Escort Service in Udaipur Call Girls
Cash Payment 9602870969 Escort Service in Udaipur Call GirlsCash Payment 9602870969 Escort Service in Udaipur Call Girls
Cash Payment 9602870969 Escort Service in Udaipur Call GirlsApsara Of India
 
Best Basmati Rice Manufacturers in India
Best Basmati Rice Manufacturers in IndiaBest Basmati Rice Manufacturers in India
Best Basmati Rice Manufacturers in IndiaShree Krishna Exports
 
Monte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMMonte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMRavindra Nath Shukla
 

Recently uploaded (20)

Russian Faridabad Call Girls(Badarpur) : ☎ 8168257667, @4999
Russian Faridabad Call Girls(Badarpur) : ☎ 8168257667, @4999Russian Faridabad Call Girls(Badarpur) : ☎ 8168257667, @4999
Russian Faridabad Call Girls(Badarpur) : ☎ 8168257667, @4999
 
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
Lucknow 💋 Escorts in Lucknow - 450+ Call Girl Cash Payment 8923113531 Neha Th...
 
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Pune Just Call 9907093804 Top Class Call Girl Service Available
 
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best ServicesMysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
 
It will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayIt will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 May
 
Grateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdfGrateful 7 speech thanking everyone that has helped.pdf
Grateful 7 speech thanking everyone that has helped.pdf
 
The Coffee Bean & Tea Leaf(CBTL), Business strategy case study
The Coffee Bean & Tea Leaf(CBTL), Business strategy case studyThe Coffee Bean & Tea Leaf(CBTL), Business strategy case study
The Coffee Bean & Tea Leaf(CBTL), Business strategy case study
 
Progress Report - Oracle Database Analyst Summit
Progress  Report - Oracle Database Analyst SummitProgress  Report - Oracle Database Analyst Summit
Progress Report - Oracle Database Analyst Summit
 
Keppel Ltd. 1Q 2024 Business Update Presentation Slides
Keppel Ltd. 1Q 2024 Business Update  Presentation SlidesKeppel Ltd. 1Q 2024 Business Update  Presentation Slides
Keppel Ltd. 1Q 2024 Business Update Presentation Slides
 
Ensure the security of your HCL environment by applying the Zero Trust princi...
Ensure the security of your HCL environment by applying the Zero Trust princi...Ensure the security of your HCL environment by applying the Zero Trust princi...
Ensure the security of your HCL environment by applying the Zero Trust princi...
 
Monthly Social Media Update April 2024 pptx.pptx
Monthly Social Media Update April 2024 pptx.pptxMonthly Social Media Update April 2024 pptx.pptx
Monthly Social Media Update April 2024 pptx.pptx
 
9599632723 Top Call Girls in Delhi at your Door Step Available 24x7 Delhi
9599632723 Top Call Girls in Delhi at your Door Step Available 24x7 Delhi9599632723 Top Call Girls in Delhi at your Door Step Available 24x7 Delhi
9599632723 Top Call Girls in Delhi at your Door Step Available 24x7 Delhi
 
VIP Kolkata Call Girl Howrah 👉 8250192130 Available With Room
VIP Kolkata Call Girl Howrah 👉 8250192130  Available With RoomVIP Kolkata Call Girl Howrah 👉 8250192130  Available With Room
VIP Kolkata Call Girl Howrah 👉 8250192130 Available With Room
 
Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023
 
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
Enhancing and Restoring Safety & Quality Cultures - Dave Litwiller - May 2024...
 
Pharma Works Profile of Karan Communications
Pharma Works Profile of Karan CommunicationsPharma Works Profile of Karan Communications
Pharma Works Profile of Karan Communications
 
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
 
Cash Payment 9602870969 Escort Service in Udaipur Call Girls
Cash Payment 9602870969 Escort Service in Udaipur Call GirlsCash Payment 9602870969 Escort Service in Udaipur Call Girls
Cash Payment 9602870969 Escort Service in Udaipur Call Girls
 
Best Basmati Rice Manufacturers in India
Best Basmati Rice Manufacturers in IndiaBest Basmati Rice Manufacturers in India
Best Basmati Rice Manufacturers in India
 
Monte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSMMonte Carlo simulation : Simulation using MCSM
Monte Carlo simulation : Simulation using MCSM
 

Kk

  • 1. Vending Machine using Verilog Ajay Sharma 3rd May 05
  • 2. Contents 1 Introduction 2 2 Finite State Machine 3 2.1 MOORE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2.2 MEALY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 2.3 State Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 3 Verilog Code of the Machine 6 4 TesT Bench 10 5 Output 14 6 Explaination of Output 17 1
  • 3. Chapter 1 Introduction Vending Machine is a soft drink dispensor machine that dispenses drink based on the amount deposited in the machine. It accepts all the coins ie: Nickel(5 cents), Dime(10 cents), Quarter(25 cents). Till it recieves 40 cents it will not dispense anything. After it has recieved 40 cents it will dispense a softdrink. Any amount above that will be given back as a change. The diagram below is the block diagram of the vending machine.The in- puts are the coins and the output is the drink. In the next chapter we will see the how we can encode the state machine in a state diagram and draw it. 2
  • 4. Chapter 2 Finite State Machine Any Sequential digital circuit can be converted into a state machine using state diagram. In a State machine the circuit’s output is defined in a different set of states ie. each output is a state. There is a State Register to hold the state of the machine and a nextstate logic to decode the nextstate. There is also a output register that defines the output of the machine. The nextstate logic is the sequential part of the machine and the Output and Currentstate are the Register part of the logic. There are two types of state machines: 1. MOORE 2. MEALY Lets see each: 2.1 MOORE In a moore machine the output state is totally dependent on the present state. The diagram shows the information. 3
  • 5. 2.2 MEALY In a mealy machine the output depends on the input as well as the present state. 2.3 State Diagram The State Diagram of the Vending Machine is given here. It has the following states: 1. State 1: reset 2. State 2: Five 3. State 3: Ten 4. State 4: Fifteen 5. State 5: Twenty 6. State 6: Twenty Five The next state is the Reset state again. The diagram is given below and is self explanatory. 4
  • 6. What happens is whenever we get a coin we jump to the next state. So for example we get a coin from the reset state say a NICKEL, then we jump to the next state FIVE . Otherwise we stay in the same state. When we get Extra amount we come back to the reset state and the difference is given back to the user. In the next chapter we will see the Verilog Code for the Vending Ma- chine/State Machine. 5
  • 7. Chapter 3 Verilog Code of the Machine The code below is the verilog Code of the State Diagram. There are two parts to the code. The first being the Sequential Logic that decides where to go nex or the change in state. The second being the one that decides the Outputs of each state. The Code is as follows: *********************** ***AUTHOR : AJAY SHARMA ************************ please see the website: ****************************** http://www.csun.edu/~ags55111 ****************************** for the entire source code of the machine 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; 6
  • 8. i need to define the registers as change,coin and vend reg vend; reg [2:0]change; wire [2:0]coin; my coins are declared as parameters to make reading better. 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; MY STATES ARE ALSO PARAMETERS . I DONT WANT TO MAKE YOU READ IN MACHINE LANGUAGE 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; 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
  • 9. 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
  • 10. 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’d TEN: begin vend <= 1’b0; if (coin==QUARTER) change <=DIME; else change <= 3’d FIFTEEN : begin vend <= 1’b0; if (coin==QUARTER) change <=NICKEL_DIME; else cha TWENTY : begin vend <= 1’b0; if (coin==DIME) change <=NICKEL; else if (coin==QU TWENTYFIVE : begin vend <= 1’b1; change <=3’d0; end default: state <= IDLE; endcase end endmodule In the next chapter we see the Test Bench of the machine. 9
  • 11. Chapter 4 TesT Bench We try to test the machine for the outputs . We add coins to the machine and see after which state does it give output or Softdrink. The testbench code is given below. AUTHOR: AJAY SHARMA PLEASE VISIT: HTTP://WWW.CSUN.EDU/~AGS55111 FOR THE ENTIRE SOURCE CODE OF THE MACHINE. OK SO I HAVE TO INCLUDE THE STATEMACHINE VERILOG CODE IN THIS FILE SO THAT THE I CAN RUN IT AND TEST IT HERE. ‘include "fsm2.v" THIS IS THE HIGHEST LEVEL OF THE MODULE: THE TEST BENCH. module test; THE INPUT IN THE FSM MODULE ARE REG HERE reg clock,reset; reg [2:0]coin; THE OUTPUT IN THE FSM MODULE ARE WIRES HERE wire vend; wire [2:0]state; wire [2:0]change; 10
  • 12. THE PARAMETERS AGAIN FOR THE COIN AND STATE 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; 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; I MONITOR THE TIME,DRINK,RESET,CLOCK,STATE AND CHANGE FOR CHANGES. initial begin $display("Timetcointdrinktresettclocktstatetchange"); $monitor("%gt%bt%bt%bt%bt%dt%d",$time,coin,vend,reset,clock,state,change); NEW FEATURE: MY MACHINE HAS THE FACILITY TO DUMP VARIABLES SO THAT I CAN VIEW THEM USING A VCD VIEWER. $dumpvars; $dumpfile("file.vcd"); // Dump output file. THIS IS WHERE THE COINS ARE ADDED. clock=0; reset=1; FIRST LETS RESET THE MACHINE #2 reset=0; coin=NICKEL; CHECK FOR STATE 1 #2 reset=1; coin=2’b00; #2 reset=0; coin=DIME; RESET AGAIN AND CHECK FOR STATE 2 #2 reset=1; coin=2’b00; #2 reset=0; RESET AGAIN AND CHECK FOR STATE 5 11
  • 13. coin=QUARTER; #2 reset=1; coin=2’b00; #2 reset=0; RESET AGAIN AND CHECK FOR STATE 5 coin=NICKEL; #2 coin=NICKEL; #2 coin=NICKEL; #2 coin=NICKEL; #2 coin=NICKEL; #2 reset=1; coin=2’b00; #2 reset=0; RESET AGAIN AND CHECK FOR STATE 5 AND SO ON coin=NICKEL; #2 coin=DIME; #2 coin=DIME; #2 reset=1; coin=2’b00; #2 reset=0; coin=NICKEL; #2 coin=DIME; #2 coin=QUARTER; #2 reset=1; coin=2’b00; #2 reset=0; coin=NICKEL; #2 coin=NICKEL; #2 coin=NICKEL; #2 coin=DIME; #2 reset=1; coin=2’b00; #2 reset=0; coin=NICKEL; #2 coin=NICKEL; #2 coin=NICKEL; #2 coin=NICKEL; #2 coin=DIME; #2 reset=1; coin=2’b00; #2 reset=0; coin=NICKEL; #2 coin=NICKEL; #2 coin=QUARTER; 12
  • 14. #2 reset=1; coin=2’b00; #2 reset=0; coin=NICKEL; #2 coin=QUARTER; #2 reset=1; coin=2’b00; #2 $finish; end THE CLOCK NEEDS TO TICK EVERY 2 TIME UNIT always #1 clock=~clock; //always @(state) // coin=!coin; initial begin if (reset) coin=2’b00; end THIS IS WHERE I INSTANTIATE THE MACHINE fsm inst1(clock,reset,coin,vend,state,change); endmodule 13
  • 15. Chapter 5 Output This chapter describes the output of the machine GPLCVER_2.10c of 02/28/05 (Linux-elf). Copyright (c) 1991-2005 Pragmatic C Software Corp. All Rights reserved. Licensed under the GNU General Public License (GPL). See the ’COPYING’ file for details. NO WARRANTY provided. Today is Wed May 11 03:36:14 2005. Compiling source file "fsmtest2.v" Highest level modules: test Time coin drink reset clock state change 0 000 0 1 0 0 x 1 000 0 1 1 0 0 2 001 0 0 0 0 0 3 001 0 0 1 1 0 4 000 0 1 0 0 0 5 000 0 1 1 0 0 6 010 0 0 0 0 0 7 010 0 0 1 2 0 8 000 0 1 0 0 0 9 000 0 1 1 0 0 10 101 0 0 0 0 0 11 101 0 0 1 5 0 14
  • 16. 12 000 1 1 0 0 0 13 000 0 1 1 0 0 14 001 0 0 0 0 0 15 001 0 0 1 1 0 16 001 0 0 0 2 0 17 001 0 0 1 3 0 18 001 0 0 0 4 0 19 001 0 0 1 5 0 20 001 1 0 0 0 0 21 001 0 0 1 1 0 22 001 0 0 0 2 0 23 001 0 0 1 3 0 24 000 0 1 0 0 0 25 000 0 1 1 0 0 26 001 0 0 0 0 0 27 001 0 0 1 1 0 28 010 0 0 0 2 0 29 010 0 0 1 4 0 30 010 0 0 0 5 1 31 010 1 0 1 0 0 32 000 0 1 0 0 0 33 000 0 1 1 0 0 34 001 0 0 0 0 0 35 001 0 0 1 1 0 36 010 0 0 0 2 0 37 010 0 0 1 4 0 38 101 0 0 0 5 4 39 101 1 0 1 0 0 40 000 0 1 0 0 0 41 000 0 1 1 0 0 42 001 0 0 0 0 0 43 001 0 0 1 1 0 44 001 0 0 0 2 0 45 001 0 0 1 3 0 46 001 0 0 0 4 0 47 001 0 0 1 5 0 48 010 1 0 0 0 0 49 010 0 0 1 2 0 15
  • 17. 50 000 0 1 0 0 0 51 000 0 1 1 0 0 52 001 0 0 0 0 0 53 001 0 0 1 1 0 54 001 0 0 0 2 0 55 001 0 0 1 3 0 56 001 0 0 0 4 0 57 001 0 0 1 5 0 58 001 1 0 0 0 0 59 001 0 0 1 1 0 60 010 0 0 0 2 0 61 010 0 0 1 4 0 62 000 0 1 0 0 0 63 000 0 1 1 0 0 64 001 0 0 0 0 0 65 001 0 0 1 1 0 66 001 0 0 0 2 0 67 001 0 0 1 3 0 68 101 0 0 0 4 3 69 101 0 0 1 5 4 70 000 1 1 0 0 0 71 000 0 1 1 0 0 72 001 0 0 0 0 0 73 001 0 0 1 1 0 74 101 0 0 0 2 1 75 101 0 0 1 5 2 76 000 1 1 0 0 0 77 000 0 1 1 0 0 Halted at location **fsmtest2.v(82) time 78 from call to $finish. There were 0 error(s), 0 warning(s), and 15 inform(s). 16
  • 18. Chapter 6 Explaination of Output The Output is explained here. We start by adding coins. We start first with the least amount NICKEL. Then we reset the machine to see the output ie: state and the SOFTDRINK. Then we add a DIME and see the result. So on till till QUARTER is also added. Then i add a dime 5 times to see the output and it vends. ie. a softdrink is given . When i add an extra amount it gives a change also. so my machine works. The next page has the state diagram of the machine. 17