-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvending.v
245 lines (235 loc) · 5.01 KB
/
vending.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
`timescale 1ns / 1ns
//////////////////////////////////////////////////////////////////////////////////
// Company: NONE
// Engineer: Adarsh Pal
//
// Create Date: 12:17:25 06/10/2020
// Design Name: Vending Machine Controller
// Module Name: vending_Controller
// Project Name: FSM for Vending Machine
// Target Devices: FPGA
// Tool versions: Xilinx ISE 14.7
//
// Description: NONE
//
// Dependencies: NONE
//
// Revision:
// Revision 0.01 - File Created
//
// //////////////////////////////////////////////////////////////////////////////////
module vending_Controller(
//00-nickel
//01-dime
//10-quarter
//11-dollar
input wire [1:0] coin, // Value of Coin
input wire coin_insert, // Push Button
input wire coin_return, // Push Button
input wire [1:0] product,// Input Product
input wire enable, // Enable Pin
input clk, // Input Clock
input reset, // Synchronous Reset
output reg [1:0] pro, // Output Product
output reg [2:0] change // Change Return
);
// State Assignment for Vending Machine Controller
// Using Gray Code for State Assignment
parameter Idle = 3'b000;
parameter Choose = 3'b001;
parameter Insert = 3'b011;
parameter Vend = 3'b010;
parameter Return = 3'b110;
reg [2:0] prev_state=0;
reg [2:0] state = 0;
reg [1:0] local_pro = 0;
integer total = 0;
always@(*)begin
case(prev_state)
Idle : begin
total = 0;
if(enable == 1)
begin
state = Choose;
end
end
Choose : begin
if(product == 2'b00)
begin
total = 0;
end
else if(product == 2'b01)
begin
local_pro = 2'b01;
total = 65;
state = Insert;
end
else if (product == 2'b10)
begin
local_pro = 2'b10;
total = 100;
state = Insert;
end
else if(product == 2'b11)
begin
local_pro = 2'b11;
total = 150;
state = Insert;
end
end
Insert : begin
if(coin_insert == 1)
begin
if(coin == 2'b11)
begin
total = total - 100;
end
else if(coin == 2'b10)
begin
total = total - 25;
end
else if(coin == 2'b01)
begin
total = total - 10;
end
else if(coin == 2'b00)
begin
total = total - 5;
end
end
//coin_insert = 0;
if(total <= 0)
begin
state = Vend;
end
if(coin_return == 1)
begin
state = Return;
end
end
Vend : begin
pro = local_pro;
if(total == 0)
begin
change = 0;
end
else
begin
if(total <= -100)
begin
total = total + 100;
change = 3'b100;
end
else if(total <= -25)
begin
total = total + 25;
change = 3'b011;
end
else if(total <= -10)
begin
total = total + 10;
change = 3'b010;
end
else if(total <= -5)
begin
total = total + 5;
change = 3'b001;
end
end
state = Idle;
end
Return : begin
if(local_pro == 2'b01)
begin
if(total >= 25)
begin
total = total - 25;
change = 3'b011;
end
else if(total >= 10)
begin
total = total - 10;
change = 3'b010;
end
else if(total >= 5)
begin
total = total - 5;
change = 3'b001;
end
else if(total == 0)
begin
change = 0;
state = Idle;
end
end
else if(local_pro == 2'b10)
begin
if(total >= 100)
begin
total = total - 100;
change = 3'b100;
end
else if(total >= 25)
begin
total = total - 25;
change = 3'b011;
end
else if(total >= 10)
begin
total = total - 10;
change = 3'b010;
end
else if(total >= 5)
begin
total = total - 5;
change = 3'b001;
end
else if(total == 0)
begin
change = 0;
state = Idle;
end
end
else if(local_pro == 2'b11)
begin
if(total >= 100)
begin
total = total - 100;
change = 3'b100;
end
else if(total >= 25)
begin
total = total - 25;
change = 3'b011;
end
else if(total >= 10)
begin
total = total - 10;
change = 3'b010;
end
else if(total >= 5)
begin
total = total - 5;
change = 3'b001;
end
else if(total == 0)
begin
change = 0;
state = Idle;
end
end
end
endcase
end
always@(posedge clk)
begin : FSM
if(reset == 1)
begin
prev_state <= Idle;
end
else
begin
prev_state <= state;
end
end
endmodule