Problems

24. Vending machine with change

HardWrite the moduleAbout 35 min
fsmdatapatharithmetic

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

Module vending

PortDirBitsWhat it is
clkin1Clock.
rstin1Synchronous reset, active high.
nickelin15 cents inserted.
dimein110 cents inserted.
quarterin125 cents inserted.
cancelin1Return the credit.
vendout1Item released (one cycle).
refundout1Credit returned (one cycle).
changeout5Cents returned with vend or refund.
creditout5Cents 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.

Stuck? . Sign in to save your progress. You can solve the free problems without an account.

Verilogvending.v
14px
Loading the editor…

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.

Vending machine with change: Verilog interview question | breadboardhub