0
0
Verilogprogramming~30 mins

State encoding (binary, one-hot, gray) in Verilog - Mini Project: Build & Apply

Choose your learning style9 modes available
State Encoding in Verilog: Binary, One-Hot, and Gray
📖 Scenario: You are designing a simple traffic light controller using Verilog. The controller cycles through three states: Green, Yellow, and Red. To implement this, you will encode these states using three different methods: binary encoding, one-hot encoding, and Gray code encoding.
🎯 Goal: Build a Verilog module that defines the traffic light states using three different state encoding schemes: binary, one-hot, and Gray code. You will declare the states, set a current state variable, and display the encoded values.
📋 What You'll Learn
Create state definitions for Green, Yellow, and Red using binary encoding.
Create state definitions for Green, Yellow, and Red using one-hot encoding.
Create state definitions for Green, Yellow, and Red using Gray code encoding.
Declare a variable current_state and assign it the binary encoded value for Yellow.
Print the encoded values of the states.
💡 Why This Matters
🌍 Real World
State encoding is used in digital circuits like traffic lights, vending machines, and communication protocols to represent different modes or steps clearly and efficiently.
💼 Career
Understanding state encoding is essential for hardware engineers and FPGA/ASIC designers to optimize circuit speed, area, and power consumption.
Progress0 / 4 steps
1
Define states using binary encoding
Create three localparam constants named GREEN_BIN, YELLOW_BIN, and RED_BIN with binary values 2'b00, 2'b01, and 2'b10 respectively.
Verilog
Need a hint?

Use localparam to define constants. Binary values use the format 2'bXX.

2
Define states using one-hot encoding
Add three localparam constants named GREEN_ONEHOT, YELLOW_ONEHOT, and RED_ONEHOT with one-hot values 3'b001, 3'b010, and 3'b100 respectively.
Verilog
Need a hint?

One-hot encoding uses one bit set to 1 and others 0. Use 3 bits here.

3
Define states using Gray code encoding
Add three localparam constants named GREEN_GRAY, YELLOW_GRAY, and RED_GRAY with Gray code values 2'b00, 2'b01, and 2'b11 respectively.
Verilog
Need a hint?

Gray code changes only one bit between states. Use 2 bits here.

4
Assign current state and display encoded values
Declare a reg [1:0] current_state and assign it the value YELLOW_BIN. Then write $display statements to print the values of GREEN_BIN, YELLOW_BIN, RED_BIN, GREEN_ONEHOT, YELLOW_ONEHOT, RED_ONEHOT, GREEN_GRAY, YELLOW_GRAY, and RED_GRAY.
Verilog
Need a hint?

Use reg [1:0] current_state = YELLOW_BIN; and $display to print values.