FSM Coding Styles: One, Two and Three Procedures
Write one state machine in each common style. Predict the cycle its outputs change in, and whether flops or gates drive them.
You Will Be Able To
- Predict the cycle in which an output changes in a one-, two- and three-procedure machine.
- Write registered outputs that are decoded from the next state and arrive on time.
- Write a one-hot machine with a reverse
case, and explain whatuniqueadds in each tool. - Find the latch in an output decoder that leaves a state out.
- Lesson
- R11.4
- Time
- 30 min
- Runs on
- Your browser
- Check
- 5 questions
A flash controller charges a capacitor until a sensor says charged, then fires. A rewrite put the whole machine in one clocked block "to register the outputs". On the bench the pump ran one cycle too long into a full capacitor. The lamp fired one cycle after the shutter.
state is the state the machine is in. The brief rows are what the brief asks for. The shipped rows are the one-procedure design.- Cycle 0
Edge 2: the machine enters CHARGE. The brief wants the pump on now. The shipped design turns it on one edge later.
- Cycle 2
Edge 4:
chargedwas seen and the machine is in AIM. The shipped pump is still on. - Cycle 3
Edge 5: the machine is in FIRE. The brief fires here.
- Cycle 4
Edge 6: the shipped design fires now, in COOL.
The state sequence is right. Only the outputs are late, each by exactly one cycle.
Three Blocks, and Three Ways to Write Them
Every state machine has three parts. The state register holds the state. The next-state logic picks the state to enter at the next edge. The output logic turns the state into outputs.
- I/O
- Control / FSM
- Storage
- Control
- Three procedures
One
always_fffor the register, onealways_combfor the next state, onealways_comborassignfor the outputs. - Two procedures
One
always_fffor the register. Onealways_combholds the next state and the outputs. This is the house style of lesson R3.2. - One procedure
Everything sits in one
always_ff. The outputs become flops, loaded from the state before the edge.
Read the diagram as text
- Inputs: trig, charged, cooled
- Next-state logic: gates: picks state_d
- State register: flops: state_q
- Output logic: gates: decodes the state
- Outputs: charge, fire, busy
- Inputs → Next-state logic: inputs
- Next-state logic → State register: state_d
- State register → Next-state logic: state_q
- State register → Output logic: state_q
- Output logic → Outputs: outputs
The same three parts are in every style. What changes is where the outputs are decoded and whether a flop holds them.
| Style | charge is 1 at edges | fire is 1 at edge | Outputs driven by | Cells | Flops | Levels |
|---|---|---|---|---|---|---|
| Three procedures | 2, 3 | 5 | gates after the state flops | 16 | 3 | 4 |
| Two procedures | 2, 3 | 5 | charge, fire: state flops. busy: a gate | 17 | 5 | 2 |
One procedure, outputs from state_q (shipped) | 3, 4 | 6 | flops | 20 | 8 | 2 |
Outputs registered from state_d | 2, 3 | 5 | flops | 25 | 8 | 4 |
The lab bench on Icarus 12, and the pinned Yosys. The brief asks for edges 2, 3 and 5. This flow re-encoded three of the four machines one-hot (lesson R3.2), which is why the flop counts differ.
The three-procedure version
module shot_ctrl (
input logic clk,
input logic rst_n, // asynchronous, active-low
input logic trig, // start a shot
input logic charged, // the capacitor is full
input logic cooled, // the lamp may fire again
output logic charge, // run the charge pump
output logic fire, // fire the lamp, one cycle
output logic busy
);
typedef enum logic [2:0] {IDLE, CHARGE, AIM, FIRE, COOL} state_e;
state_e state_q, state_d;
always_ff @(posedge clk or negedge rst_n) // 1: the state register
if (!rst_n) state_q <= IDLE;
else state_q <= state_d;
always_comb begin // 2: the next state
state_d = state_q;
case (state_q)
IDLE: if (trig) state_d = CHARGE;
CHARGE: if (charged) state_d = AIM;
AIM: state_d = FIRE;
FIRE: state_d = COOL;
COOL: if (cooled) state_d = IDLE;
default: state_d = IDLE;
endcase
end
always_comb begin // 3: the outputs, from the state
charge = (state_q == CHARGE);
fire = (state_q == FIRE);
busy = (state_q != IDLE);
end
endmoduleRegistered Outputs with No Extra Cycle
A registered output is steady for the whole cycle and has the shortest delay after the clock. The one-procedure style gets those flops and loses a cycle. One change gets both: decode the output from state_d, the state the machine is about to enter.
From the present state: a cycle late
always_ff @(posedge clk or negedge rst_n)
if (!rst_n) fire <= 1'b0;
else fire <= (state_q == FIRE);
The flop loads 1 at the edge where the machine leaves FIRE. The output is 1 during COOL.
From the next state: on time
always_ff @(posedge clk or negedge rst_n)
if (!rst_n) fire <= 1'b0;
else fire <= (state_d == FIRE);
The flop loads 1 at the edge where the machine enters FIRE. The output is 1 during FIRE.
The whole controller with outputs registered from the next state
$_DFF_PN0_ flop drives each output directly.module shot_ctrl (
input logic clk,
input logic rst_n, // asynchronous, active-low
input logic trig, // start a shot
input logic charged, // the capacitor is full
input logic cooled, // the lamp may fire again
output logic charge, // run the charge pump
output logic fire, // fire the lamp, one cycle
output logic busy
);
typedef enum logic [2:0] {IDLE, CHARGE, AIM, FIRE, COOL} state_e;
state_e state_q, state_d;
always_comb begin
state_d = state_q;
case (state_q)
IDLE: if (trig) state_d = CHARGE;
CHARGE: if (charged) state_d = AIM;
AIM: state_d = FIRE;
FIRE: state_d = COOL;
COOL: if (cooled) state_d = IDLE;
default: state_d = IDLE;
endcase
end
always_ff @(posedge clk or negedge rst_n)
if (!rst_n) begin
state_q <= IDLE;
charge <= 1'b0;
fire <= 1'b0;
busy <= 1'b0;
end else begin
state_q <= state_d;
charge <= (state_d == CHARGE); // decoded from the state being entered
fire <= (state_d == FIRE);
busy <= (state_d != IDLE);
end
endmoduleThe price is logic in front of the output flops: the next-state logic and then the decode. Give each registered output a reset value, or it is unknown until the first edge.
One-Hot by Reverse case
A one-hot machine has one flop per state, and exactly one of them holds 1. Testing "am I in FIRE?" is then a look at one bit. The reverse case writes that directly. Its case expression is the constant 1'b1, and each item is one state bit.
charge and fire are state flops. busy is one inverter.module shot_ctrl (
input logic clk,
input logic rst_n, // asynchronous, active-low
input logic trig, // start a shot
input logic charged, // the capacitor is full
input logic cooled, // the lamp may fire again
output logic charge, // run the charge pump
output logic fire, // fire the lamp, one cycle
output logic busy
);
localparam int IDLE = 0, CHARGE = 1, AIM = 2, FIRE = 3, COOL = 4; // bit numbers: one flop per state
logic [4:0] state_q, state_d;
always_ff @(posedge clk or negedge rst_n)
if (!rst_n) state_q <= 5'b00001;
else state_q <= state_d;
always_comb begin
state_d = '0;
unique case (1'b1) // which state bit is 1?
state_q[IDLE]: if (trig) state_d[CHARGE] = 1'b1; else state_d[IDLE] = 1'b1;
state_q[CHARGE]: if (charged) state_d[AIM] = 1'b1; else state_d[CHARGE] = 1'b1;
state_q[AIM]: state_d[FIRE] = 1'b1;
state_q[FIRE]: state_d[COOL] = 1'b1;
state_q[COOL]: if (cooled) state_d[IDLE] = 1'b1; else state_d[COOL] = 1'b1;
default: state_d[IDLE] = 1'b1; // no bit set: go home
endcase
end
assign charge = state_q[CHARGE];
assign fire = state_q[FIRE];
assign busy = !state_q[IDLE];
endmodule| Spelling | Cells | Levels | No bit set (zero-hot) | Two bits set |
|---|---|---|---|---|
unique case (1'b1) with a default | 18 | 4 | returns to IDLE | the first matching item wins |
case (1'b1) with a default | 21 | 4 | returns to IDLE | the first matching item wins |
priority case (1'b1), no default | 21 | 4 | stays at 00000 for ever | the first matching item wins |
unique case (1'b1), no default | 15 | 2 | stays at 00000 for ever | the first matching item wins |
Cells and levels on the pinned Yosys. Behaviour on Icarus 12, with the state forced by the bench. unique let the tool drop the priority between items.
unique is also a check in simulation, and the tools differ on how much of it they run.
| Tool | Two bits set, unique with a default | No bit set, unique with no default |
|---|---|---|
| Icarus 12 | no message | WARNING: value is unhandled for priority or unique case statement |
| Verilator 5.052 | stops: unique case, but multiple matches found | stops: unique case, but none matched |
| Lint | no message | no message |
With no default, both simulators report at time 0, before reset, because the state is still unknown there.
Latches in the Decoders
A combinational decoder must give every output a value in every state. Leave one out and the output has to remember, which is a latch. FSM decoders are where this happens most, because each state lists only the outputs it cares about.
Each state lists only what it sets
always_comb
case (state_q)
IDLE: begin charge = 1'b0; fire = 1'b0; busy = 1'b0; end
CHARGE: begin charge = 1'b1; busy = 1'b1; end
AIM: begin busy = 1'b1; end
FIRE: begin fire = 1'b1; busy = 1'b1; end
COOL: begin busy = 1'b1; end
endcase
Lint: LATCH for charge, fire and busy, and CASEINCOMPLETE. Synthesize refuses the block: found logic loop. Written with always @* it builds 3 $_DLATCH_P_ cells.
Defaults first, then the exceptions
always_comb begin
charge = 1'b0;
fire = 1'b0;
busy = 1'b1;
case (state_q)
IDLE: busy = 1'b0;
CHARGE: charge = 1'b1;
FIRE: fire = 1'b1;
default: ;
endcase
end
Every path assigns all three outputs. No latch, and each state shows only what makes it different.
The same rule covers state_d. Start the next-state block with state_d = state_q;, as lesson R3.2 does. Lesson R11.5 compares five ways to close such gaps.
The Lab: Outputs a Cycle Late
| Port | Direction | Width | Meaning |
|---|---|---|---|
| clk | input | 1 | clock |
| rst_n | input | 1 | reset, asynchronous, active-low |
| trig | input | 1 | start a shot |
| charged | input | 1 | the capacitor is full |
| cooled | input | 1 | the lamp may fire again |
| charge | output | 1 | 1 while the machine is in CHARGE |
| fire | output | 1 | 1 while the machine is in FIRE, one cycle |
| busy | output | 1 | 1 in every state but IDLE |
- Clock
- rising edge
- Reset
rst_nasynchronous, active-low; IDLE, all outputs 0- Corner cases
- the cycle a state is entered. Done when: Run shows
charge=1at edges 2 and 3 only andfire=1at edge 5. Flops still drive the outputs
Run as shipped
chargeis 1 at edges 3 and 4, andfireat edge 6. The brief says 2, 3 and 5. Synthesize reports 20 cells and 8 flops.Try the three-procedure form
Replace the body with the three-procedure version from the first section. Run: the outputs are on time. Synthesize: 16 cells, 3 flops. The outputs now come from gates.
Register them again, on time
Use the version registered from
state_d. Run: the same lines as the three-procedure form. Synthesize: 25 cells, 8 flops.Check what drives the outputs
Open the Gates tab. In the registered version each output is the Q pin of a flop. In the three-procedure version each one is a gate.
Asked in interviews
What are the one-, two- and three-procedure FSM styles?
Three procedures separate the state register, the next-state logic and the output logic. Two procedures merge the two combinational parts. One procedure puts everything in a clocked block, which registers the outputs.
How do you register FSM outputs without adding a cycle?
Decode them from the next state, not the present one. The output flop then loads at the same edge as the state register. The output is valid in the cycle the state is entered.
What is a reverse case statement?
A
case (1'b1)whose items are single state bits of a one-hot machine. Each test is one bit, not a full compare. Keep adefaultso a state with no bit set can recover.
Try It
The bench changes its inputs 1 ns after an edge and prints 1 ns after the next one. The machine enters CHARGE at edge 2, AIM at edge 4 and FIRE at edge 5. It enters COOL at edge 6 and IDLE at edge 7.
Each printed output belongs to the state of one edge earlier. charge is still 1 at edge 4, after charged was seen. fire is 1 at edge 6, when the machine is already in COOL.
Synthesize reports 20 cells: 8 flops and 12 gates, with 2 levels of logic. Five flops hold the state, which this flow re-encoded one-hot. Three hold the outputs.
Synthesize the design to measure its cells and inspect the mapped gates.
e2 trig=1 charged=0 cooled=0 | charge=0 fire=0 busy=0 e3 trig=0 charged=0 cooled=0 | charge=1 fire=0 busy=1 e4 trig=0 charged=1 cooled=0 | charge=1 fire=0 busy=1 e5 trig=0 charged=0 cooled=0 | charge=0 fire=0 busy=1 e6 trig=0 charged=0 cooled=0 | charge=0 fire=1 busy=1 e7 trig=0 charged=0 cooled=1 | charge=0 fire=0 busy=1 e8 trig=0 charged=0 cooled=0 | charge=0 fire=0 busy=0Original design · 20 cells · 8 flip-flops · 0 latches · 2 logic levels. Synthesis uses Yosys; counts depend on the toolchain.
Read included file: tb.sv
`timescale 1ns/1ps
module tb;
logic clk = 0;
always #5 clk = ~clk; // rising edges at 5, 15, 25, ...
logic rst_n = 1; // falls at 1 ns, rises 1 ns after edge 1
int edge_n = -1;
always @(posedge clk) edge_n = edge_n + 1;
logic trig = 0, charged = 0, cooled = 0;
logic charge, fire, busy;
shot_ctrl dut (.*);
task automatic tick();
@(posedge clk); #1;
if (rst_n) $display("e%0d trig=%b charged=%b cooled=%b | charge=%b fire=%b busy=%b", edge_n, trig, charged, cooled, charge, fire, busy);
endtask
initial begin
#1 rst_n = 0; tick(); tick(); rst_n = 1;
trig = 1; tick(); // e2: the shot starts
trig = 0; tick();
charged = 1; tick(); // e4: the capacitor is full
charged = 0; tick();
tick();
cooled = 1; tick(); // e7: the lamp has cooled
cooled = 0; tick();
$finish(0);
end
endmodule
Check Yourself
Common Mistakes
Wrong Move the output assignments into the clocked case to "register the outputs", and keep the old timing in mind.
An output set from the present state arrives one cycle after the machine enters that state.
Fix Decode registered outputs from state_d, the state being entered, or keep them combinational.
always_ff @(posedge clk)
case (state_q)
FIRE: begin fire <= 1'b1; state_q <= COOL; end
// ...
endcasealways_ff @(posedge clk) begin
state_q <= state_d;
fire <= (state_d == FIRE);
endWrong Drop the default item from a one-hot reverse case because unique says exactly one bit is set.
Synthesis then builds no way out of a state with no bit set, and the machine stays there.
Fix Keep a default that returns to the reset state, and let unique report two bits set in simulation.
Wrong Treat a combinational output as safe to use as a clock, a reset or an asynchronous strobe.
An output decoded by gates can glitch between edges while the state bits settle.
Fix Take such signals from a flop: a registered output, or a state bit of a one-hot machine.
Wrong Judge a style by its flop count on this flow.
The synthesizer may re-encode the state, so the count depends on the tool as much as on the style.
Fix Compare what the brief cares about: the cycle each output changes in, and what drives it.
Remember
- A state machine is a state register, next-state logic and output logic. The styles differ in which procedure holds each.
- Outputs assigned from
state_qin a clocked block arrive one cycle late. - Register outputs from
state_dto get flop outputs with no extra cycle. - A reverse
case (1'b1)tests one bit per state. Keep itsdefault, and know whatuniquedoes in your tools. - Give every output a default at the top of a decoder, or the missing state builds a latch.