24. Vending machine with change
A machine sells one item for 25 cents. It accepts 5, 10 and 25 cent coins, one coin per clock cycle at most: nickel, dime or quarter is 1 for one edge when that coin is inserted (the test never sets two at once, and never inserts a coin in the same cycle as cancel).
credit shows the money inserted so far (0 to 20 cents). On each rising clock edge:
- A coin adds its value. If the new total is 25 or more, the item is sold: vend is 1 for the next cycle, change shows the total minus 25 for that cycle, and credit goes back to 0.
- cancel = 1 returns the credit: refund is 1 for the next cycle and change shows the credit that was returned; credit goes back to 0. If the credit was already 0, refund stays 0.
- In every other cycle vend, refund and change are 0.
rst (synchronous, active high) clears the credit and all outputs.
Ports
| Port | Dir | Bits | What it is |
|---|---|---|---|
clk | in | 1 | Clock. |
rst | in | 1 | Synchronous reset, active high. |
nickel | in | 1 | 5 cents inserted. |
dime | in | 1 | 10 cents inserted. |
quarter | in | 1 | 25 cents inserted. |
cancel | in | 1 | Return the credit. |
vend | out | 1 | Item released (one cycle). |
refund | out | 1 | Credit returned (one cycle). |
change | out | 5 | Cents returned with vend or refund. |
credit | out | 5 | Cents inserted so far. |
Example
Dime, dime, quarter: credit reads 10, then 20; the quarter makes 45, so vend = 1 with change = 20 and credit returns to 0.
Written hints
AI hint
Submissions
No submissions yet. Submit your answer and it shows here. Sign in to keep them.
Submit your answer to have it graded. The hidden test runs on the server; you see whether it passes and, if not, the first case it gets wrong.
Press Build to check your code with Yosys. Errors and warnings show here, and a clean build draws the logic diagram.
Build your design to drive it by hand and see its signals here.