Skip to question
asic.fyi
DesignVerificationSystemVerilogFirmwareArchitectureInterviews
asic.fyi/Interview questions/Implement a Stock-Aware 75-Cent Vending FSM

Hardware interview practice

Implement a Stock-Aware 75-Cent Vending FSM

MediumRTL DesignSystemVerilog

A machine sells one drink for 75 cents and accepts one 25-cent or 50-cent coin per accepted cycle. It stores at most 50 cents of credit because the next valid coin either vends or reaches that bound. A one-bit stock flag is cleared by a vend and set by restock. Implement the FSM with deterministic cancel, restock, invalid-coin, and simultaneous-input priority behavior.

Try it in the question bankReason first. Then compare.

Keep this exact question selected while you check your answer and review the full solution.

Practice this question →
Starting point

Question code

module vend75(input logic clk,rst_n,coin_valid,coin50,cancel,restock,
 output logic coin_ready,vend,change25,refund_valid,
 output logic [6:0] refund, output logic sold_out);
// coin50=0 means 25 cents; coin50=1 means 50 cents
// reset state: no credit and sold_out=1
Reviewed example

Work through one case

Input
Case 1: After restock, accept 25 then 50
Case 2: After restock, accept 50 then 50
Case 3: After restock, accept 50 then assert cancel
Expected output
Case 1: vend pulses on the second coin, change25=0, credit returns to 0, and sold_out becomes 1
Case 2: vend=1 and change25=1 on the second coin; the extra 25 cents is returned
Case 3: refund_valid=1 with refund=50 for one cycle, no vend occurs, credit becomes 0, and stock remains

The shown result follows by applying this rule: The state transitions implement the exact 0/25/50 credit set and the 75/100-cent vend cases. The cases also demonstrate this requirement: When sold out, coin inputs are not accepted. `vend`, `change25`, and `refund_valid` are one-cycle pulses, refund is zero whenever `refund_valid` is zero, and restock wins if restock, cancel, and coin_valid arrive together.

What to cover

Requirements

  1. Use credit states 0, 25, and 50 cents plus one stocked bit. Active-low reset clears credit, marks sold out, and clears every pulse output.
  2. Cycle priority after reset is restock, then cancel, then an accepted coin. Restock sets stocked and leaves credit unchanged; cancel emits one `refund_valid` pulse with refund equal to current credit, then clears credit.
  3. `coin_ready` is one only when stocked, cancel and restock are low, and no vend/refund pulse from the prior cycle is being held. An accepted 25/50-cent coin adds credit; totals 75 or 100 vend, clear stock and credit, and pulse `change25` only for total 100.
  4. When sold out, coin inputs are not accepted. `vend`, `change25`, and `refund_valid` are one-cycle pulses, refund is zero whenever `refund_valid` is zero, and restock wins if restock, cancel, and coin_valid arrive together.
asic.fyi · Learn silicon end to end.info@asic.fyi