Finite state machine (FSM)

Draw waiting, collection and reporting as states, each transition labeled with the event it samples. Compare outputs based on current state alone with outputs that also depend on inputs, and test reset, holds and every transition.

One measurement passes through waiting, collection and report delivery. The same ready input does nothing while waiting but can finish the job in the report state. The circuit needs a memory of its progress.

A transition takes effect on an edge that samples its condition. Otherwise the state holds.
A transition takes effect on an edge that samples its condition. Otherwise the state holds.

The RTL

The state register holds progress. Combinational logic starts with next_state equal to state and changes the destination only when the condition for this stage holds. A rising edge stores that destination. Start and done belong to different transitions.

module collector_control(
    input logic clk,
    input logic rst_n,
    input logic start,
    input logic done,
    input logic ready,
    output logic [1:0] state_code,
    output logic report,
    output logic take
);
typedef enum logic [1:0] {WAITING, COLLECT, REPORT} state_t;
    state_t state, next_state;
    assign state_code = state;
    assign report = (state == REPORT);
    assign take = report && ready;
    always_comb begin
        next_state = state;
        case (state)
            WAITING: if (start) next_state = COLLECT;
            COLLECT: if (done) next_state = REPORT;
            REPORT: if (ready) next_state = WAITING;
            default: next_state = WAITING;
        endcase
    end
    always_ff @(posedge clk) begin
        if (!rst_n) state <= WAITING;
        else state <= next_state;
    end
endmodule

Trace the outputs

Sampling done in COLLECT moves the machine to REPORT. Report goes high with that state and stays high while ready is low. Raising ready then makes take high before the edge where the receiver accepts the report.

Recorded simulation. Dashed lines mark rising edges; bus values are hexadecimal. Open the figure to enlarge it.
Recorded simulation. Dashed lines mark rising edges; bus values are hexadecimal. Open the figure to enlarge it.
Rising edgeInputs (hex)Observed after edge (hex)
E0rst_n=0, start=0, done=0, ready=0state_code=0, report=0, take=0
E1rst_n=1, start=1, done=0, ready=0state_code=1, report=0, take=0
E2rst_n=1, start=0, done=0, ready=0state_code=1, report=0, take=0
E3rst_n=1, start=0, done=1, ready=0state_code=2, report=1, take=0
E4rst_n=1, start=0, done=0, ready=0state_code=2, report=1, take=0
E5rst_n=1, start=0, done=0, ready=1state_code=0, report=0, take=0

Design choices

Report depends only on the state, a Moore-style output. Take also depends on the current ready input, a Mealy-style output. Inspect transfer conditions before the edge and state after it to recognize the completed handoff.

Yosys synth_ice40 mapped this module to the following cell counts: SB_DFFESR: 2, SB_LUT4: 7. The download records the tool versions and the commands to reproduce this mapping.

Interactive lab

Reset, then sample start and done. In REPORT, toggle ready and compare when take and state change.

state
report
take
Show the last eight observations

    Teaching film

    Read the transcript

    Run the RTL yourself

    Extract the lab and follow the commands in its README. Compare the simulated outputs and waveform with your predictions from the interactive lab.

    Download Finite state machine (FSM) RTL (.zip)

    Check your understanding

    1. Where does WAITING go when it samples start high?
    2. In COLLECT, ready is high but done is low. What state follows?
    3. In which state is report high?
    4. Raise ready while in REPORT. Which output becomes high before the edge?
    5. Should a stalled REPORT return to WAITING?