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.
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.
| Rising edge | Inputs (hex) | Observed after edge (hex) |
|---|---|---|
| E0 | rst_n=0, start=0, done=0, ready=0 | state_code=0, report=0, take=0 |
| E1 | rst_n=1, start=1, done=0, ready=0 | state_code=1, report=0, take=0 |
| E2 | rst_n=1, start=0, done=0, ready=0 | state_code=1, report=0, take=0 |
| E3 | rst_n=1, start=0, done=1, ready=0 | state_code=2, report=1, take=0 |
| E4 | rst_n=1, start=0, done=0, ready=0 | state_code=2, report=1, take=0 |
| E5 | rst_n=1, start=0, done=0, ready=1 | state_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
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)