0
0
Verilogprogramming~10 mins

State encoding (binary, one-hot, gray) in Verilog - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - State encoding (binary, one-hot, gray)
Define States
Choose Encoding Type
Binary
Assign Codes to States
Use Encoded States in FSM
Simulate and Verify
This flow shows how to define states, pick an encoding type (binary, one-hot, or gray), assign codes, and use them in a finite state machine.
Execution Sample
Verilog
typedef enum logic [1:0] {
  IDLE = 2'b00,
  LOAD = 2'b01,
  EXEC = 2'b11,
  DONE = 2'b10
} state_t;

state_t state, next_state;
Defines a 2-bit binary encoded state machine with four states.
Execution Table
StepState NameBinary CodeOne-Hot CodeGray Code
1IDLE00000100
2LOAD01001001
3EXEC11010010
4DONE10100011
5End---
💡 All states assigned codes in each encoding scheme.
Variable Tracker
StateBinary CodeOne-Hot CodeGray Code
IDLE00000100
LOAD01001001
EXEC11010010
DONE10100011
Key Moments - 3 Insights
Why does one-hot encoding use more bits than binary encoding?
One-hot encoding assigns one bit per state, so for N states it uses N bits, unlike binary which uses log2(N) bits. See execution_table rows 1-4 for bit widths.
How does Gray code differ from binary in state transitions?
Gray code changes only one bit between consecutive states, reducing glitches. Look at execution_table rows 1-4 to compare Gray and Binary codes.
Can two states have the same code in these encodings?
No, each state must have a unique code in all encoding schemes to avoid confusion. The execution_table shows unique codes for each state.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the one-hot code for the EXEC state?
A0010
B1000
C0100
D0001
💡 Hint
Check the 'One-Hot Code' column at the row where State Name is EXEC.
At which step does the Gray code for the state change from '01' to '11'?
AStep 1 to 2
BStep 3 to 4
CStep 2 to 3
DStep 4 to 5
💡 Hint
Look at the 'Gray Code' column between rows 3 and 4 in execution_table.
If the FSM had 8 states, how many bits would one-hot encoding require?
A8 bits
B3 bits
C4 bits
D1 bit
💡 Hint
One-hot uses one bit per state; see key_moments about bit width differences.
Concept Snapshot
State encoding assigns binary codes to FSM states.
Binary uses few bits (log2 states).
One-hot uses one bit per state.
Gray changes one bit between states.
Choose encoding by speed, area, or glitch needs.
Full Transcript
State encoding in Verilog means giving each FSM state a unique code. Binary encoding uses the fewest bits by counting in binary. One-hot encoding uses one bit per state, so only one bit is high at a time. Gray code changes only one bit between states to reduce glitches. We define states, pick an encoding, assign codes, and use them in the FSM. The execution table shows codes for four states in each encoding. One-hot uses more bits but can be faster. Gray code helps with smooth transitions. Each state must have a unique code to avoid errors.