Designing a Rp4,000 vending machine with 13 Moore states

Advanced Digital Systems coursework, 2026

Article cover: the number “4,000” above the words “VENDING MACHINE”, a person feeding a note into the machine, and icons for a bottle, coins, and a clock.

Introduction

A sequential circuit is a digital circuit whose outputs depend both on the current input and on the history of previous inputs. That history is stored as a state condition inside flip-flops. Because of that storage, the vending machine remains the example that is hard to replace when explaining sequential circuits: the machine has to remember how much money has gone in before deciding when to release a product and how much change to pay out.

This piece summarises a group assignment for an Advanced Digital Systems course: designing a vending machine that sells one bottle of mineral water for Rp4,000 and accepts Rp1,000, Rp2,000, and Rp5,000 notes. One requirement is what makes it interesting: the bottle must come out first, and only then the change. That means the machine cannot forget the amount of change while the bottle is being released. A simple ordering rule like that immediately adds states, and most of the design work in this assignment went there.

Specification and assumptions

  • The machine is synchronous to a clock, with at most one note inserted per clock cycle.
  • Money already inserted cannot be returned.
  • Inserted money is accumulated in the state.
  • Once the total reaches Rp4,000 or more, the machine moves to a release state and stops responding to input until the transaction finishes.
  • The most money that can be in the machine before the final payment is Rp3,000.
  • With a final payment of Rp5,000, the highest total is Rp8,000, so the largest change is Rp4,000.
  • Bottle release states always execute before change states.

Why Moore forces doubled states

In a Moore machine, the output depends only on the state, not on the input. The consequence is simple but far-reaching: the bottle and the change cannot share a state, because they are different outputs that must appear in different cycles. The bottle gets its own run of states (B0 to B4), the change gets another (C1 to C4). An output only appears one clock cycle after its state is entered.

Precisely because of that, the “bottle first, change later” order comes out of the Moore structure on its own: every B state points to the C state with the same number, and every C state points back to S0. No extra logic is needed to remember the order, because the order is already the shape of the diagram.

The price is extra states. Every possible change amount needs one bottle state and one change state: B0 to B4 for change from Rp0 to Rp4,000, plus C1 to C4. Together with four money-accumulation states, that makes 13 in total.

Input and output encoding

The input is encoded in 2 bits, w2w1. The output is encoded in 4 bits, Z4Z3Z2Z1: Z4 marks the bottle release, while Z3Z2Z1 gives the change amount in multiples of Rp1,000.

Table 1 — Input encoding

w2 w1 Meaning
00 No money inserted
01 Rp1,000 note
10 Rp2,000 note
11 Rp5,000 note

Table 2 — Output encoding

Z4 Z3 Z2 Z1 Meaning
0000 No output
1000 Bottle released
0001 Rp1,000 change
0010 Rp2,000 change
0011 Rp3,000 change
0100 Rp4,000 change

Thirteen states

The first four states, S0 to S3, hold the amount of money inserted. States B0 to B4 release the bottle, and B1 to B4 also remember the change still owed. States C1 to C4 release the change. B0 is used when the money is exactly Rp4,000, so there is no change to remember.

Table 3 — States and their outputs

State Meaning Output
S0 Machine empty None
S1 Rp1,000 in the machine None
S2 Rp2,000 in the machine None
S3 Rp3,000 in the machine None
B0 Bottle out, no change Bottle
B1 Bottle out, Rp1,000 change pending Bottle
B2 Bottle out, Rp2,000 change pending Bottle
B3 Bottle out, Rp3,000 change pending Bottle
B4 Bottle out, Rp4,000 change pending Bottle
C1 Rp1,000 change released Rp1,000 change
C2 Rp2,000 change released Rp2,000 change
C3 Rp3,000 change released Rp3,000 change
C4 Rp4,000 change released Rp4,000 change

State diagram

State diagram of the Moore machine: S0 to S3 in grey, B0 to B4 in blue, C1 to C4 in orange, with w2w1 input labels on every arrow.
State diagram of the Moore machine. The number inside each circle is the Z4Z3Z2Z1 output; the arrow labels are the w2w1 input. Input 00 is not drawn because it only holds the machine in the same state.

The money-accumulation path runs in short chains. From S0, a Rp1,000 note leads to S1, Rp2,000 to S2, and Rp5,000 straight to B1 because the total is already Rp5,000 with Rp1,000 change. The pattern continues: S1 takes Rp1,000 to S2, Rp2,000 to S3, Rp5,000 to B2. S2 takes Rp1,000 to S3, Rp2,000 to B0 because the money is exact, and Rp5,000 to B3. From S3, Rp1,000 gives B0, Rp2,000 gives B1, and Rp5,000 gives B4.

The rest runs without conditions. From B1 to B4, the machine always moves to the change state with the same number, whatever the input. From B0 and from C1 to C4, it always returns to S0. The “no input while a transaction is running” rule is what keeps the lower half of the diagram free of branches.

State table

Table 4 — Moore state table

Present state w = 00 w = 01 w = 10 w = 11 Output
S0 S0 S1 S2 B1 0000
S1 S1 S2 S3 B2 0000
S2 S2 S3 B0 B3 0000
S3 S3 B0 B1 B4 0000
B0 S0 S0 S0 S0 1000
B1 C1 C1 C1 C1 1000
B2 C2 C2 C2 C2 1000
B3 C3 C3 C3 C3 1000
B4 C4 C4 C4 C4 1000
C1 S0 S0 S0 S0 0001
C2 S0 S0 S0 S0 0010
C3 S0 S0 S0 S0 0011
C4 S0 S0 S0 S0 0100

Assigning state codes

Thirteen states need 4 D flip-flops, since 2⁴ = 16 codes are available. The codes are assigned in order to stay readable: S0 to S3 use 0000 to 0011, B0 to B3 use 0100 to 0111, B4 uses 1000, and C1 to C4 use 1001 to 1100. The three leftover codes, 1101, 1110, and 1111, are never used, so they are marked don’t care and put to work during simplification.

Table 5 — State-assigned table (next state Y4Y3Y2Y1)

y4 y3 y2 y1 w = 00 w = 01 w = 10 w = 11 Z4 Z3 Z2 Z1
0000 0000 0001 0010 0101 0000
0001 0001 0010 0011 0110 0000
0010 0010 0011 0100 0111 0000
0011 0011 0100 0101 1000 0000
0100 0000 0000 0000 0000 1000
0101 1001 1001 1001 1001 1000
0110 1010 1010 1010 1010 1000
0111 1011 1011 1011 1011 1000
1000 1100 1100 1100 1100 1000
1001 0000 0000 0000 0000 0001
1010 0000 0000 0000 0000 0010
1011 0000 0000 0000 0000 0011
1100 0000 0000 0000 0000 0100
1101 d d d d d
1110 d d d d d
1111 d d d d d

Six-variable Karnaugh maps

Each next-state function Y4, Y3, Y2, and Y1 depends on six variables: y4, y3, y2, y1, w2, and w1. The output functions Z4 to Z1 depend only on the four state variables, as a Moore machine requires. That difference in variable count shows up in the map size: the Y functions need an 8 × 8 map, while the Z functions fit in 4 × 4.

Simplification was done with an online Karnaugh Map Solver in Sum of Products mode. Minterm numbers are computed as (state code in decimal) × 4 + the value of input w2w1, with the first variable as the most significant bit.

Table 6 — Solver input

Function Variables Minterms Don’t cares
Y4 y4, y3, y2, y1, w2, w1 15, 20–35 52–63
Y3 y4, y3, y2, y1, w2, w1 3, 7, 10, 11, 13, 14, 32–35 52–63
Y2 y4, y3, y2, y1, w2, w1 2, 5–9, 11, 12, 24–31 52–63
Y1 y4, y3, y2, y1, w2, w1 1, 3, 4, 6, 9, 11, 12, 14, 20–23, 28–31 52–63
Z4 y4, y3, y2, y1 4–8 13–15
Z3 y4, y3, y2, y1 12 13–15
Z2 y4, y3, y2, y1 10, 11 13–15
Z1 y4, y3, y2, y1 9, 11 13–15

The simplified results are in Table 7. A prime (’) marks the complement.

Table 7 — Equations after simplification

Function Equation (sum of products)
Y4 (y3·y1) + (y3·y2) + (y4’·y2·y1·w2·w1) + (y4·y3’·y2’·y1’)
Y3 (y4·y3’·y2’·y1’) + (y4’·y3’·y1’·w2·w1) + (y4’·y3’·y2’·w2·w1) + (y4’·y3’·y2·w2·w1’) + (y4’·y3’·y2·y1·w2’·w1)
Y2 (y3·y2) + (y4’·y2·y1’·w1) + (y4’·y2·w2’·w1’) + (y4’·y3’·y2’·y1·w1) + (y4’·y3’·y2’·w2·w1’)
Y1 (y3·y1) + (y4’·y1·w1’) + (y4’·y3’·y1’·w1)
Z4 (y4’·y3) + (y4·y3’·y2’·y1’)
Z3 y4·y3
Z2 y4·y2
Z1 y4·y1

Every equation above was checked back against every row of Table 5, for all 13 used states and all four input values.

The logic circuit

The circuit splits in two. The combinational part on the left computes next state Y4 to Y1 from the current state (y4 to y1) and the input (w2, w1). Four D flip-flops sharing a clock store the result, so each flip-flop’s D input is the matching Y. The combinational part on the right computes Z4 to Z1 from y4 to y1 alone. Complemented variables come from NOT gates, or straight from the Q’ output of the state flip-flops.

Table 8 — Logic gate requirements

Function AND gates OR gates
Y4 4 AND (2, 2, 5, 4 inputs) 4-input OR
Y3 5 AND (4, 5, 5, 5, 6 inputs) 5-input OR
Y2 5 AND (2, 4, 4, 5, 5 inputs) 5-input OR
Y1 3 AND (2, 3, 4 inputs) 3-input OR
Z4 2 AND (2, 4 inputs) 2-input OR
Z3 1 AND (2 inputs) –
Z2 1 AND (2 inputs) –
Z1 1 AND (2 inputs) –

The equations in Table 7 were then wired into a circuit. Figure 10 shows the result in Logicly: stacked AND gates and an OR gate on the left compute the next state, four D flip-flops in the middle store it, and the output logic on the right drives the Z4 to Z1 lamps.

Logicly circuit diagram: stacked AND and OR gates on the left, four D flip-flops in the middle, and the output logic with four Z4 to Z1 lamps on the right.
Figure 10 — The Moore vending machine circuit with four D flip-flops in Logicly.

Verilog implementation

The same design was then rewritten in Verilog on EDA Playground. The vending_moore module holds the eight sum-of-products equations from Table 7 plus one always block that behaves as the four D flip-flops. One thing was added that the paper design did not have: an asynchronous rst signal, so the machine always starts from S0 whenever a simulation runs. That reset is purely an implementation detail, not a third input alongside w2 and w1. The state port exists only to make y4 down to y1 easy to read on the waveform.

// Vending machine Moore: botol Rp4.000, uang 1000/2000/5000
// Botol keluar lebih dulu, kemudian kembalian. Memakai 4 D-FF.
module vending_moore (
    input  wire       clk,
    input  wire       rst,      // reset asinkron (aktif tinggi) -> state S0
    input  wire [1:0] w,        // w2w1: 00 kosong, 01 Rp1.000, 10 Rp2.000, 11 Rp5.000
    output wire [3:0] state,    // y4y3y2y1 (untuk pengamatan waveform)
    output wire [3:0] z         // Z4 = botol, Z3Z2Z1 = kembalian (x Rp1.000)
);

    reg y4, y3, y2, y1;         // keluaran 4 D-FF (state sekarang)
    wire w2 = w[1];
    wire w1 = w[0];

    // Rangkaian kombinasional kiri (next state = masukan D tiap FF)
    wire Y4 = (y1 & y3) | (y2 & y3) | (~y4 & y2 & y1 & w2 & w1) | (y4 & ~y3 & ~y2 & ~y1);
    wire Y3 = (y4 & ~y3 & ~y2 & ~y1) | (~y4 & ~y3 & ~y1 & w2 & w1) | (~y4 & ~y3 & ~y2 & w2 & w1) | (~y4 & ~y3 & y2 & ~w1 & w2) | (~y4 & ~y3 & y2 & y1 & ~w2 & w1);
    wire Y2 = (y3 & y2) | (~y4 & y2 & ~y1 & w1) | (~y4 & y2 & ~w2 & ~w1) | (~y4 & ~y3 & ~y2 & y1 & w1) | (~y4 & ~y3 & ~y2 & w2 & ~w1);
    wire Y1 = (y3 & y1) | (~y4 & y1 & ~w1) | (~y4 & ~y3 & ~y1 & w1);

    // Rangkaian kombinasional kanan (output Moore: hanya bergantung state)
    wire Z4 = (~y4 & y3) | (y4 & ~y3 & ~y2 & ~y1);
    wire Z3 = y4 & y3;
    wire Z2 = y4 & y2;
    wire Z1 = y4 & y1;

    // 4 buah D flip-flop
    always @(posedge clk or posedge rst) begin
        if (rst) {y4, y3, y2, y1} <= 4'b0000;
        else     {y4, y3, y2, y1} <= {Y4, Y3, Y2, Y1};
    end

    assign state = {y4, y3, y2, y1};
    assign z     = {Z4, Z3, Z2, Z1};

endmodule

Testbench and test scenarios

The testbench generates a 10 ns clock, releases reset at 20 ns, then runs the seven scenarios in Table 9. They were chosen so that every bottle and change state (B0 to B4, C1 to C4) gets visited, including exact payment, idle cycles with no money, and every denomination. For each scenario the testbench checks four things itself: the bottle leaves exactly once, it leaves before any change, the change amount is right, and the machine returns to S0. It prints PASS or FAIL, so nothing has to be read off the waveform by hand.

Table 9 — Test scenarios

Scenario Money inserted Total (Rp) Expected result
A Rp2,000 + Rp2,000 4,000 Bottle, no change
B Rp5,000 5,000 Bottle, Rp1,000 change
C Rp1,000, 2 idle clocks, Rp1,000, Rp5,000 7,000 Bottle, Rp3,000 change
D Rp1,000 + Rp2,000 + Rp5,000 8,000 Bottle, Rp4,000 change
E Rp1,000 four times 4,000 Bottle, no change
F Rp1,000 + Rp2,000 + Rp2,000 5,000 Bottle, Rp1,000 change
G Rp1,000 + Rp5,000 6,000 Bottle, Rp2,000 change
`timescale 1ns/1ps

module tb;
    reg  clk = 0, rst = 1;
    reg  [1:0] w = 2'b00;
    wire [3:0] state, z;

    vending_moore dut (.clk(clk), .rst(rst), .w(w), .state(state), .z(z));

    always #5 clk = ~clk;  // periode 10 ns

    initial begin
        $dumpfile("dump.vcd");
        $dumpvars(0, tb);
    end

    function [15:0] nama;  // nama state untuk log
        input [3:0] s;
        case (s)
            4'b0000: nama = "S0";
            4'b0001: nama = "S1";
            4'b0010: nama = "S2";
            4'b0011: nama = "S3";
            4'b0100: nama = "B0";
            4'b0101: nama = "B1";
            4'b0110: nama = "B2";
            4'b0111: nama = "B3";
            4'b1000: nama = "B4";
            4'b1001: nama = "C1";
            4'b1010: nama = "C2";
            4'b1011: nama = "C3";
            4'b1100: nama = "C4";
            default: nama = "??";
        endcase
    endfunction

    // log tiap tepi naik clock: input dan state SEBELUM berpindah
    always @(posedge clk)
        if (!rst) $display("%4d ns | w=%b | state=%b (%s) | Z4Z3Z2Z1=%b", $time, w, state, nama(state), z);

    // penghitung untuk pemeriksaan otomatis
    integer botol_cnt, kemb;
    reg urutan_salah;

    always @(posedge clk)
        if (!rst) begin
            if (z[3]) botol_cnt = botol_cnt + 1;
            if (z[2:0] != 0) begin
                kemb = kemb + z[2:0];
                if (botol_cnt == 0) urutan_salah = 1;  // kembalian sebelum botol
            end
        end

    task mulai(input [8*64-1:0] judul);
        begin
            botol_cnt = 0;
            kemb = 0;
            urutan_salah = 0;
            $display("---- %0s ----", judul);
        end
    endtask

    task coin(input [1:0] v);  // 1 koin/uang selama 1 clock
        begin
            @(negedge clk) w = v;
        end
    endtask

    task idle(input integer n);
        begin
            repeat (n) @(negedge clk) w = 2'b00;
        end
    endtask

    task cek(input integer kemb_harap);
        begin
            if (botol_cnt == 1 && kemb == kemb_harap && !urutan_salah && state == 4'b0000)
                $display(">> PASS: botol 1x, kembalian Rp%0d, urutan benar, kembali ke S0", kemb*1000);
            else
                $display(">> FAIL: botol=%0d kembalian=%0d urutan_salah=%0b state=%b", botol_cnt, kemb, urutan_salah, state);
        end
    endtask

    initial begin
        repeat (2) @(negedge clk);
        rst = 0;  // lepas reset
        idle(2);

        mulai("A: 2000 + 2000 (pas, tanpa kembalian)");
        coin(2'b10); coin(2'b10); idle(5); cek(0);

        mulai("B: 5000 (kembalian 1000)");
        coin(2'b11); idle(5); cek(1);

        mulai("C: 1000, tunggu, 1000, 5000 (kembalian 3000)");
        coin(2'b01); idle(2); coin(2'b01); coin(2'b11); idle(5); cek(3);

        mulai("D: 1000 + 2000 + 5000 (kembalian 4000)");
        coin(2'b01); coin(2'b10); coin(2'b11); idle(5); cek(4);

        mulai("E: 1000 x4 (pas, tanpa kembalian)");
        coin(2'b01); coin(2'b01); coin(2'b01); coin(2'b01); idle(5); cek(0);

        mulai("F: 1000 + 2000 + 2000 (kembalian 1000)");
        coin(2'b01); coin(2'b10); coin(2'b10); idle(5); cek(1);

        mulai("G: 1000 + 5000 (kembalian 2000)");
        coin(2'b01); coin(2'b11); idle(5); cek(2);

        $display("==== simulasi selesai ====");
        $finish;
    end
endmodule

What was actually hard

The hard part was not the Karnaugh map; it was deciding how many states were really needed. If the bottle and the change could come out together, this machine might get by with nine states. The “bottle first” rule forces the change to be parked in the bottle state, and the Moore property forces each change value to have its own output state. Once the diagram is right, the rest is mechanical: order the codes, count minterms, read the solver’s answer.

The second thing that stood out is how much don’t care buys. Three of the sixteen codes go unused. Treating them as d shrinks the Y equations considerably, because groups on the Karnaugh map can stretch across those empty rows. But that choice leaves a question we did not settle: if the circuit ever did land in 1101, 1110, or 1111, its output would be undefined. For coursework that is fine. For a circuit people actually use, those three codes should get a recovery path back to S0.

The design was then implemented in Verilog on EDA Playground and checked with a testbench that runs seven scenarios automatically. The module and testbench code sit in the two sections above, along with the table of scenarios they run.

Closing

This Rp4,000 vending machine looks small on the surface: one button, one bottle, three denominations. Inside there are 13 states, 16 codes, four flip-flops, and eight Boolean functions that have to stay consistent with each other. What made it worth doing is exactly the ordering rule. Change one sentence of the specification — “bottle first, then change” — and the number of states, the contents of the table, and the shape of every equation change with it.

This assignment was done with Rakyan Bhumi Nagari and Radhitya Kurnia Asmara for the Advanced Digital Systems course in Computer Engineering at Universitas Diponegoro.

Find me on LinkedIn and Instagram!

← all notesAlso available in Indonesian