RTL Foundations / F10

Finite State Machines: How Do We Organize a Job into Stages?

Give the collector three stages, then trace the fourth sample and the report acceptance event through its state machine.

Lesson film

Film chapters

TranscriptCaptionsDownload film

The F09 counter identifies the edge that completes the fourth reading. The sensor panel can now report a result. But can it begin the next batch while the receiver has yet to take this result? A new start clears the count. Starting before delivery would erase the boundary between the two jobs.

First decide which events the collector can accept at each stage. A finite state machine (FSM) stores the current stage in a register. The circuit uses that state to decide whether to act on each input.

Each stage waits for a different event

IDLE waits for start_i. An accepted start clears the previous count and moves the machine into COLLECT. Before that edge the machine is still in IDLE. Even if sample_take_i is already high, this edge only starts the job. The next rising edge is the first possible sample.

COLLECT accepts sample events and holds its progress through idle cycles. When the last-sample event from F09 occurs, the same edge records the fourth sample in the count and moves the machine to REPORT. The collector ignores another start_i during collection and continues the current job.

REPORT waits for report_taken_i, a synchronous event supplied by the receiver in this lesson. The design tracks whether a report is pending and how many samples have completed. Without acceptance the state and count hold. Delivery returns the machine to IDLE. The count stays at four until reset or the next accepted start.

Start moves IDLE to COLLECT. The last sample moves COLLECT to REPORT. Report acceptance moves REPORT to IDLE. Without the relevant event, each stage holds.
The machine evaluates the arrow conditions from its current state before the edge. Synchronous reset takes priority and sends any state to IDLE.

Account for every hold and transition

The phase outputs appear below in the order can_start_o / collecting_o / report_valid_o. They describe the current state. The current phase, its relevant conditions, and reset determine what the registers save at the edge.

Reset or state before edgeConditionPhase outputs beforeState afterCount after
rst_ni=0, any stateIgnore other inputsDecode the pre-edge stateIDLEClear
IDLEstart_i=0100IDLEHold
IDLEstart_i=1100COLLECTClear
COLLECTsample_take_i=0010COLLECTHold
COLLECTSample event with sample_last=0010COLLECTAdd one
COLLECTsample_last=1010REPORTAdvance to four
REPORTreport_taken_i=0001REPORTHold
REPORTreport_taken_i=1001IDLEHold
Reserved RTL encoding 11Any inputs000IDLEHold

All rows after the first assume rst_ni=1. During normal operation the pre-edge count in COLLECT ranges from zero to three. Synchronous reset takes effect in the registers at the rising edge. Until that edge arrives, the decoded phase outputs may still reflect the previous state. Reset blocks starts and sample events. A report also transfers only on an edge where rst_ni && report_valid_o && report_taken_i is true.

How does the next state reach the register?

state_q is the stored state. Combinational logic computes state_d, the next state. The default state_d = state_q holds the current state until a transition condition selects a different value. It also gives the combinational output a value when no event occurs. On a rising edge the register selects either the reset state or state_d. A mux in the diagram shows that choice. Synthesis may use a different logic structure to implement it.

Combinational logic uses state_q and the events to compute state_d. A register saves state_d or IDLE on a rising edge. The stored state feeds back and determines the phase outputs.
Combinational logic computes state_d before the edge. The stored state_q updates on the rising edge.
always_comb begin
    state_d = state_q;
    case (state_q)
        IDLE:    if (start_job)      state_d = COLLECT;
        COLLECT: if (sample_last)   state_d = REPORT;
        REPORT:  if (report_taken_i) state_d = IDLE;
        default:                    state_d = IDLE;
    endcase
end

always_ff @(posedge clk_i) begin
    if (!rst_ni)
        state_q <= IDLE;
    else
        state_q <= state_d;
end

report_valid_o depends only on the current state. It is high in REPORT, which makes it a Moore output. The start action depends on both state and input. The combinational signal start_job follows start_i in IDLE with reset released, and clears the count when the machine starts a job. If exposed on the interface, it would be a Mealy output. The distinction tells us whether a signal depends only on entering a phase or can also change with the current cycle’s inputs.

The fourth sample makes the result available

E1 starts the job. E2 through E7 use the six sample event values from F09, 1, 0, 1, 0, 1, 1. They start an edge later because E1 accepts the start. Before E7 the count is three. With sample_last high, state_d selects REPORT. That edge updates the count to four and the state to REPORT.

Rising edgestartsample_takereport_takenState before → afterCount afterTransfer?
E1110IDLE → COLLECT0No
E2010COLLECT → COLLECT1No
E3100COLLECT → COLLECT1No
E4010COLLECT → COLLECT2No
E5000COLLECT → COLLECT2No
E6010COLLECT → COLLECT3No
E7011COLLECT → REPORT4No
E8110REPORT → REPORT4No
E9000REPORT → REPORT4No
E10101REPORT → IDLE4Yes
The simulated state enters COLLECT at E1 and REPORT at E7. Report valid rises after E7 and falls after the transfer at E10.
The waveform comes from Icarus. At E7 report_taken_i is high, but the state before the edge is still COLLECT. There is no transfer.
E7 makes the report available. E8 and E9 hold REPORT without acceptance. E10 transfers the result and returns the machine to IDLE.
The report is available at E8 and E9. E10 is the edge where the receiver takes it.

The machine ignores the early acceptance indication at E7 because report_valid_o is low before that edge. Before E10, both validity and acceptance are high, so a transfer occurs. After E10, report_valid_o falls as the machine goes back to IDLE. The high start_i at E10 also has no effect. The state before that edge still owns the old report.

Replay the transitions and inspect the synthesized encoding

Download the lab package, extract it on Linux or WSL and run bash run.sh. The directed test in Icarus Verilog 12.0 checks 23 edges. It covers every legal hold and transition, reset coinciding with other events, and a restart that clears the previous count. A deliberately faulty variant leaves REPORT unconditionally. With the same inputs, the test detects the wrong state at E8.

The test also forces the RTL state variable to 2'b11. All three phase outputs are zero. After the test releases the forced value, the next rising edge follows the default transition to IDLE. This result applies to the RTL model. Using the packaged synthesis flow, Yosys 0.33 extracts the FSM and recodes it from two bits to three one-hot bits, with one high bit for each legal state. The synthesized state register now uses different codes. Establishing how it recovers from an illegal value requires examining that netlist or testing it directly.

The run summary, raw waveform, and evidence and SHA-256 hashes are available separately.

Transfer question: what if start arrives during collection?

The current contract ignores start_i in COLLECT. Change it so a start discards the partial job, clears the count on that edge, and stays in COLLECT. Suppose start_i and sample_take_i are both high at E3. Give start priority. How would you change the expected result at E3? How would you test the new completion edge?

Answer

The collector holds one sample before E3. The changed rule clears the count, stays in COLLECT, and discards the concurrent sample. Keeping the original input events from E4 to E7 gives post-edge counts of 1, 1, 2, 3. The machine is still in COLLECT at E7 and no report transfers. Add one sample edge and check that this one enters REPORT. A contract that rejects a new start may also specify a response to the sender and test for it.

F11 defines the receiver interface. Entering REPORT tells us that a result is available. Valid and ready at the same edge tell us when it actually transfers.

Step through the collector

The preset inputs complete a batch. You can change any input before the next rising edge. Try a second start during COLLECT, or an early report_taken with the fourth sample.

  1. IDLE
  2. COLLECT
  3. REPORT
edgeTransitionCompleted

Download RTL, testbench and results

Check your understanding

1. The collector is in IDLE. At the next edge start_i and sample_take_i are both high. What are the updated state and count?
2. Before E7 the state is COLLECT and the count is three. Both sample_take_i and report_taken_i are high. What happens on E7?
3. Before E8 the state is REPORT. report_taken_i is low and start_i and sample_take_i are high. Reset is inactive. What happens after this edge?
4. Forcing the two-bit RTL state to 11 leads to IDLE at the next edge. Yosys recodes it to three one-hot bits in this run. What does this evidence establish?
5. A new contract restarts on start in COLLECT, clearing the count and dropping a concurrent sample. Both events occur at E3. Replay samples 1, 0, 1, 1 from E4 to E7. What holds after E7?