State Mach Onehot PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

One-Hot State Machines

I Typical state machine designs minimize the number of flip flops


I This is a result of using highly-encoded states
I To decode the states, lots of combo logic is required.
I FPGAs are programmable logic devices that are rich in flip-flops and
poor in combo logic
I One-Hot state machines use one flip-flop per state and thus need
much less decode logic
I This makes one-hot encoding much more efficient for FPGAs.

I One Hot Encoding: 000001, 000010, 000100, 001000, 010000,....


One-Hot State Machines

I case usually evaluates a case expression and then


checks the result against several case items in a list
case (case_expression)
case_item1 : case_item_statement1;
case_item2 : case_item_statement2;
case_item3 : case_item_statement3;
case_item4 : case_item_statement4;
default : case_item_statement5;
endcase
One-Hot State Machines
I One hot encoding uses the reversed case statement
I In this style, case expression and case item are swapped
I In one-hot encoding:
I case expression is the literal (1’b1) to match against
I case items are single bits in the present state vector

case (1’b1)
present_state[bit0]: next_state_assignment;
present_state[bit1]: next_state_assignment;
present_state[bit2]: next_state_assignment;
present_state[bit3]: next_state_assignment;
endcase

I The index to bits in the ps vector are declared as parameters or


enumerated types
I This style infers efficient one-bit comparison logic against the ps
and ns vectors
One-Hot State Machines

I One-hot state machine code


I Infer the present state flip-flops

//instantiate the _ps vector flip flops


always_ff @ (posedge clk, negedge rst_n)
if (!rst_n) begin
arbiter_oh_ps <= 4’b0000; //reset, clear all bits
arbiter_oh_ps[IDLE] <= 1’b1; //set IDLE state bit
end
else arbiter_oh_ps <= arbiter_oh_ns;
One-Hot State Machines
I Glitchless state machine code
I Define ns transitions and output
//describe the _ns steering logic and output decoder
always_comb begin
arbiter_oh_ns = 4’b0000; //default for _ns vector
gnt = 1’b0; //default assignment
case (1’b1)
arbiter_oh_ps[IDLE] :
if (req) arbiter_oh_ns[BBUSY] = 1’b1;
else arbiter_oh_ns[IDLE] = 1’b1;
arbiter_oh_ps[BBUSY]:
begin
gnt = 1’b1; //output is asserted here
if (!done) arbiter_oh_ns[BBUSY] = 1’b1;
else if ( dly) arbiter_oh_ns[BWAIT] = 1’b1;
else arbiter_oh_ns[BFREE] = 1’b1;
end
arbiter_oh_ps[BWAIT]:
begin
gnt = 1’b1;
if (!dly) arbiter_oh_ns[BFREE] = 1’b1;
else arbiter_oh_ns[BWAIT] = 1’b1;
end
arbiter_oh_ps[BFREE]:
if (req) arbiter_oh_ns[BBUSY] = 1’b1;
else arbiter_oh_ns[IDLE] = 1’b1;
endcase
end //always
One-Hot State Machines
I Synthesis output from one-hot state machine
I Could the output gnt glitch?

n16
arbiter_oh_ns[3]
dly dly n15
U22
done done U21
...
U23 n13
U27
U29 U28 n19
U30 n20 ...eg[2]
...eg[0]
...eg[3]
clk

n23
rst_n
n22
rst_n U26
n21
n18

req U25 n14


req
U20 gnt
n17 gnt
...eg[1]

clk
One-Hot State Machines
I The case items are unique, they may be decoded in parallel
I Good place to use unique case
always_comb begin
arbiter_oh_ns = 4’b0000; //default for _ns vector
gnt = 1’b0; //default assignment
unique case (1’b1)
...

U14

...[3]
U22 U18 U20
U21
U17 ...[0]
...[1]
U19 U15
U16
...[2]

U13

I Difference in area: 218 without unique, 196 with unique

You might also like