FSM Vending Machine Design Code
FSM Vending Machine Design Code
The vending machine FSM testbench applies stimulus through controlled changes in input values — specifically coin inputs — while monitoring the 'nw_pa' output to observe expected behaviors. Coin inputs are simulated with various values to trigger transitions through states s0, s5, s10, and s15, checking if 'nw_pa' activates only at s15. Clocks and resets are pulsed at intervals to synchronize state transitions, reflecting real-world operations. Expected observations include seeing 'nw_pa' turn high only when a cumulative coin value of 15 is reached, aligning with setup parameters .
Default state definitions in FSM design serve as a fail-safe mechanism, preventing undefined behavior when unexpected inputs or errors occur. In the vending machine FSM, the default next_state is set to s0, ensuring the FSM resets to an initial state in unanticipated scenarios. This design choice is crucial for maintaining clarity and preventing state confusion or deadlock errors in sequential logic circuits, safeguarding proper operation despite anomalies .
The 'nw_pa' signal in the vending machine FSM serves as an indicator that a product is dispensed. It becomes active (1'b1) only in state s15, which signifies the vending machine has received enough value to dispense the product. In all other states (s0, s5, s10), 'nw_pa' remains inactive (1'b0).
The vending machine FSM is a Moore machine where outputs are based solely on states, while the Mealy machine's outputs depend on transitions and current state. The vending machine FSM transitions states based on coin inputs, moving between s0, s10, s5, s15, before returning to s0. In contrast, the Mealy machine transitions among S0, S1, S2, and S3 based on the data_in signal, producing output on transition from S3. The key difference is that the vending machine FSM prioritizes steady-state outputs, whereas the Mealy machine integrates inputs directly with state transitions to generate outputs .
The FSM for the vending machine transitions between states based on a two-bit coin input. From the initial state s0, if a coin valued 0 (2'b00) or 1 (2'b01) is input, the next state is s10. In state s5, a coin input of 0 keeps it in s5, a coin input of 1 takes it to s10, and a coin input of 2 takes it to s15. In state s10, a coin input of 0 retains s10, and any non-zero input moves it to s15. State s15 is reset to s0 automatically .
In the Mealy machine FSM, the detection output is set high (1) when it reaches state S3 and the subsequent transition is based on a high data input. If data_in is 1 while in state S3, the machine transitions back to state S1 but with detected output activated. The data input directly affects state transitions, determining pathways and thereby controlling when detection occurs. The output logic relies on this integration of inputs with state to enable the flexible state-based decision-making hallmark to Mealy machines .
The testbench utilizes the 'always' block to toggle the clock signal at regular intervals, effectively simulating a clock signal necessary for FSM operations. In both designs, the clock flips states ('clk=~clk') every 25 time units in the simulation, ensuring that state transitions within the FSM occur with clock synchronization. This regular toggling simulates real-world clock behavior, allowing the FSMs to correctly interpret inputs and transition through states consistently .
In the vending machine FSM, state s10 transitions occur based on the coin input. If the coin input is 0, the machine stays in s10. If the input is non-zero (either 1 or 2), it transitions to s15, preparing for a product dispense. These transitions allow for flexibility in valuing additional inputs and control the machine’s progression towards meeting sufficient value for product dispatch. Correct triggering ensures efficient operation and correct production of outputs ('nw_pa').
The 'always' block in FSM design code is pivotal as it enables continuous monitoring and updating of states and outputs based on inputs and clocks. In both implementations, separate 'always' blocks manage state transitions and output logic. For the vending machine, these manage both the current and next states based on coin inputs, while controlling 'nw_pa'. For the Mealy machine, the 'always' block similarly transitions states based on data_in, critically governing the detected outputs based on real-time data input and immediate state for dynamic feedback .
The reset condition ensures the FSMs begin operation from a known state, regardless of previous activity, an essential feature for predictable operation. In the vending machine FSM, a reset moves the FSM to state s0, ensuring it starts with no value accepted. Similarly, the Mealy machine uses reset to return to state S0, ensuring no outputs are erroneously registered from a non-initial state. Reset is implemented so that upon activation, the FSMs ignore current transitions and revert to initial states .