G7.3.1 The Vending Machine That Gave Free Candy

EasyFixIcarus Verilog + Yosys · serverReview the topic

A vending machine sells a second item to the next customer for any coin. Find the state bug and fix it.

Hardware context: A snack machine that sells a 20-cent item with change, and a washing-machine controller built from two JK flip-flops. Fix a vending machine whose credit survives a sale.

A snack machine sells a 20-cent item and gives change. After the first sale of the day, every customer gets an item for any single coin. The owner wants it fixed before the next delivery.

This is a Fix problem. The design below is the shipped one. Its credit register is the state of a small machine: 0, 5, 10 or 15 cents. Find what goes wrong after a sale and fix it.

The check feeds coins over ten cycles and compares vend, change and credit in every cycle with a model. The design must synthesize with no latches.

Signals in the Timeline

SignalWidthRole
coins20input of each check
Full specification
The module (keep the name and the ports)
module vend20 (
  input  logic       clk,
  input  logic       rst,      // synchronous reset, active high: credit 0, outputs 0
  input  logic [1:0] coin,     // this cycle's coin: 0 none, 1 = 5 cents, 2 = 10 cents, 3 = 25 cents
  output logic       vend,     // 1 for one cycle: the item drops
  output logic [2:0] change,   // change in nickels, shown with vend
  output logic [3:0] credit    // credit in nickels (the state): 0 to 3 between sales
);
  ...
endmodule
At a rising edge, with total = credit + coinIn the next cycle
total of 4 nickels (20 cents) or morevend = 1, change = total − 4, credit = 0
total below 4 nickelsvend = 0, change = 0, credit = total
rst = 1credit = 0, vend = 0, change = 0

Money is counted in nickels: a dime is 2, a quarter 5. All three outputs come from registers, so they change only at the clock edge.

Examples

Example 1

Two Dimes, Then Nothing
Input
coins in cents, one per cycle: 10 10 - - - - - - - -
Output
vend_cycles = {2}, credit_cycle0 = 0, credit_cycle1 = 2, credit_cycle2 = 0, credit_cycle3 = 0, credit_cycle4 = 0, credit_cycle5 = 0, credit_cycle6 = 0, credit_cycle7 = 0, credit_cycle8 = 0, credit_cycle9 = 0

The first dime makes the credit 2 nickels. The second makes the total 4. So the next cycle shows vend = 1 with no change, and the credit is back to 0.

Example 2

A Quarter: Change of 5 Cents
Input
coins in cents, one per cycle: 25 - - - - - - - - -
Output
vend_cycles = {1}, change_cycle1 = 1, credit_cycle0 = 0, credit_cycle1 = 0, credit_cycle2 = 0, credit_cycle3 = 0, credit_cycle4 = 0, credit_cycle5 = 0, credit_cycle6 = 0, credit_cycle7 = 0, credit_cycle8 = 0, credit_cycle9 = 0

A quarter is 5 nickels. The machine sells, returns 1 nickel of change, and the credit is 0 again.

Example 3

Coins After a Sale Start from Zero
Input
coins in cents, one per cycle: 10 10 5 5 5 - - - - -
Output
vend_cycles = {2}, credit_cycle0 = 0, credit_cycle1 = 2, credit_cycle2 = 0, credit_cycle3 = 1, credit_cycle4 = 2, credit_cycle5 = 3, credit_cycle6 = 3, credit_cycle7 = 3, credit_cycle8 = 3, credit_cycle9 = 3

After the two dimes sell, three nickels bring the credit to 3. No sale yet: 15 cents is not enough.

Constraints and Contract

  • All inputs are known values and change only between edges.
  • Keep the module name and the ports.

Hints

Hint 1

Run the tests and find the first cycle after a sale where the credit is wrong. What should the credit be right after the item drops?

Hint 2

The credit is the machine's state. After a sale the machine must return to the state "no money": the overpayment leaves as change.

Hint 3

One assignment in the sale branch is wrong. The change already holds the overpayment, so nothing may stay behind.

What the grader checks: Six directed and fourteen seeded coin streams of ten cycles after a reset. In every cycle vend, change and credit are compared with a JavaScript model. Synthesis must report no latches.

The submitted design must synthesize with 0 latches and no combinational loop. Counts come from the pinned open-source synthesis flow. They are proxies for logic size and depth, not physical area or timing.

20 checks on Icarus Verilog + Yosys · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 3 steps and the final capstone

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Two Dimes, Then Nothing

Input
coins in cents, one per cycle: 10 10 - - - - - - - -
Expected output
vend_cycles = {2}, credit_cycle0 = 0, credit_cycle1 = 2, credit_cycle2 = 0, credit_cycle3 = 0, credit_cycle4 = 0, credit_cycle5 = 0, credit_cycle6 = 0, credit_cycle7 = 0, credit_cycle8 = 0, credit_cycle9 = 0

The first dime makes the credit 2 nickels. The second makes the total 4. So the next cycle shows vend = 1 with no change, and the credit is back to 0.

Run samples executes all 3 public checks. Check all tests runs 20 checks on the server.

Icarus Verilog + Yosys · server