Comments on: One-hot State Machine in SystemVerilog – Reverse Case Statement https://www.verilogpro.com/systemverilog-one-hot-state-machine/ Verilog and Systemverilog Resources for Design and Verification Mon, 27 Jun 2022 07:42:19 +0000 hourly 1 https://wordpress.org/?v=6.4.4 By: Jason Yu https://www.verilogpro.com/systemverilog-one-hot-state-machine/#comment-1640 Mon, 20 May 2019 00:14:31 +0000 http://www.verilogpro.com/?p=167#comment-1640 In reply to Darshan.

I think if coded correctly, the reverse case statement FSM coding style should automatically create a case statement that is parallel, without requiring the ‘parallel_case’ directive. Since each state is represented by a unique number, each case expression is effectively comparing 1 unique register bit against 1’b1. Therefore no overlap between the different case expressions should be possible, which matches the definition of parallel case.

]]>
By: Darshan https://www.verilogpro.com/systemverilog-one-hot-state-machine/#comment-1626 Tue, 14 May 2019 13:58:43 +0000 http://www.verilogpro.com/?p=167#comment-1626 Thanks for the article, Jason. Your articles have helped answer many questions I’ve had. My colleague and I were discussing about this recently and a question came up – Are reverse case statements useful at all in plain Verilog (not System Verilog) if one isn’t allowed to use synthesis pragmas such as ‘parallel_case’? Unlike System Verilog, Verilog doesn’t have the ‘unique’ keyword.

Put in another way, the question is what really happens when one codes a reverse case statement in Verilog with no ‘parallel_case’ directive. Does synthesis assume that the input to the case-statement is not parallel and hence not one-hot and hence infer priority logic? I’d love to hear your thoughts.

]]>
By: Jason Yu https://www.verilogpro.com/systemverilog-one-hot-state-machine/#comment-504 Wed, 12 Jul 2017 06:24:52 +0000 http://www.verilogpro.com/?p=167#comment-504 In reply to LG_FALCON.

I have not had a chance to look closely at the synthesized netlist of a one-hot encoded state machine written with enumerated type and regular case statement. I believe with today’s compiler technology it should synthesize the same as the reverse case statement method (i.e. optimized to do single state bit comparisons). I coded my most recent design this way based on this belief. I’ll let you know when I can verify exactly the gates that Design Compiler synthesized 🙂

]]>
By: LG_FALCON https://www.verilogpro.com/systemverilog-one-hot-state-machine/#comment-502 Tue, 11 Jul 2017 09:55:59 +0000 http://www.verilogpro.com/?p=167#comment-502 Hi Jason
Reffering to the last comment by shailesh, do you still suggests using “reverse case method” for FSM ? is it worth the less-readable code?
or that shailesh’s code will do the same w/o infers the full bit comparison? this code is much more initiuative, but what about performence?

]]>
By: Jason Yu https://www.verilogpro.com/systemverilog-one-hot-state-machine/#comment-417 Sat, 08 Apr 2017 18:17:28 +0000 http://www.verilogpro.com/?p=167#comment-417 In reply to shailesh K S.

Thanks for your comments! Yes I had been intending to make an update to this page for while, especially about your first point on waveform display of the state vector. After simulating my coworker’s code in the original coding style, I also realized simulators do not visualize state vectors written this way very well. I would agree that specifying the one-hot state encoding in the enum type should be equivalent and will display better. Your proposed code is indeed how I would write a one-hot state machine using SystemVerilog today!

]]>
By: shailesh K S https://www.verilogpro.com/systemverilog-one-hot-state-machine/#comment-414 Thu, 06 Apr 2017 15:42:36 +0000 http://www.verilogpro.com/?p=167#comment-414 Hello Jason,
Thanks for the post. It really helped me to understand more about OneHot FSMs.
The statement on Cliff’s paper in 2003 is not entirely true. we can handle that case by adding a unique key word. In the meantime concern raised by VK is great and worth looking into. with both of this I would like you to consider the following code which will give you the same synthesis result.
Advantages being
1) state vector is enum, hence waveform visualization is better
2) does propagate x, in case FSM assignment is buggy
3) simpler and easy to read

typedef enum logic [3:0] {
IDLE = 4’b0001,
READ = 4’b0010,
DLY = 4’b0100,
DONE = 4’b1000,
SX = 4’x
} state_t;
state_t state, next;

// Sequential state transition
always_ff @(posedge clk or negedge rst_n)
if (!rst_n)
state <= IDLE;
else
state <= next;

// Combinational next state logic
always_comb begin
next = SX;
unique case (state)
IDLE : begin
if (go)
next = READ;
else
next = IDLE;
end
READ : next = DLY;
DLY : begin
if (!ws)
next = DONE;
else
next = READ;
end
DONE : next = IDLE;
endcase
end

// Make output assignments
always_ff @(posedge clk or negedge rst_n)

]]>
By: Jason Yu https://www.verilogpro.com/systemverilog-one-hot-state-machine/#comment-106 Mon, 24 Oct 2016 04:14:13 +0000 http://www.verilogpro.com/?p=167#comment-106 In reply to VK.

Hi VK, thanks for your comment! When you say the code “kills x-prop”, I think you mean that if “ws” or “go” input has the value of X, then the “if(x)-else” coding style will take on the “else” case, rather than also corrupting the outputs (in this case the “next” vector)? Yes you’re right, and I’ll admit I’ve coded this kind of bug before! Recently our team has turned on the Synopsys VCS x-propagation feature to detect this kind of problem. It corrupts the output even with this “if-else” coding style (see my post on x-prop). If that’s not available, then yes, the code you propose will also do the job. Previously I also coded a “default: next <= 'x" in my state machines, but the RTL linting tool we use complains about this, so I've moved away from this style. VCS x-prop will also catch this kind of problem.

]]>
By: VK https://www.verilogpro.com/systemverilog-one-hot-state-machine/#comment-99 Tue, 18 Oct 2016 22:33:25 +0000 http://www.verilogpro.com/?p=167#comment-99 I just realized you are not driving every bit of “next” in the case statement, so you would need to have the default next = ‘0 at the beginning as you did originally. That would not kill x-prop anyways since the default in the case statement would override it if needed.

]]>
By: VK https://www.verilogpro.com/systemverilog-one-hot-state-machine/#comment-98 Tue, 18 Oct 2016 22:30:38 +0000 http://www.verilogpro.com/?p=167#comment-98 This is a synthetic example I would assume but want to get your feedback on preserving X-propagation. Bugs in the state machine next state logic or outside the state machine driving the input signals “go” and “ws” can be hidden by the current way of coding (using if-else and also default assigned to 0). Killing x-prop can cause “simulation vs. synthesis mismatch”, which can be pretty fatal. Consider this improvement:

// Combinational next state logic
always_comb begin
//next = ‘0; // Comment out; see added default in case statement instead
unique case (1’b1)
state[IDLE] : begin
// Ensures x-prop if go === x
next[READ] = go == 1’b1;
next[IDLE] = go == 1’b0;
end
state[READ] : next[ DLY] = 1’b1;
state[ DLY] : begin
// Ensures x-prop if ws === x
next[DONE] = ws == 1’b0;
next[READ] = ws == 1’b1;
end
state[DONE] : next[IDLE] = 1’b1;
// Add default to propagate Xs for unhandled states or if state reg went X itself
default: next <= 'x;
endcase
end

]]>
By: Amol Gole https://www.verilogpro.com/systemverilog-one-hot-state-machine/#comment-91 Tue, 04 Oct 2016 23:25:39 +0000 http://www.verilogpro.com/?p=167#comment-91 Interesting. I had a “typo” in my code recently that caused the case_expression to be one not listed in the case statement and I did observe odd behavior from what I would have expected. So I guess having the default “next[IDLE]=1’b1” is probably not that useful here. 🙂

]]>