📖 Scenario: You are designing a simple traffic light controller using VHDL. To optimize the hardware, you want to specify how the states are encoded using the state_encoding attribute.
🎯 Goal: Build a VHDL process with a state machine that uses the state_encoding attribute to specify one-hot encoding for the states.
📋 What You'll Learn
Create an enumerated type called state_type with states RED, GREEN, and YELLOW
Declare a signal current_state of type state_type
Add a state_encoding attribute with value one_hot for state_type
Write a process that cycles through the states in order: RED -> GREEN -> YELLOW -> RED
Print the current state name using a report statement
💡 Why This Matters
🌍 Real World
State encoding attributes help FPGA and ASIC designers optimize hardware resources by controlling how states are represented internally.
💼 Career
Understanding state encoding is important for hardware engineers working on digital design, embedded systems, and FPGA development.
Progress0 / 4 steps
1
Create the state type and signal
Create an enumerated type called state_type with states RED, GREEN, and YELLOW. Then declare a signal called current_state of type state_type.
VHDL
Hint
Use type state_type is (RED, GREEN, YELLOW); to define the states.
Declare signal current_state : state_type := RED; to start at RED.
2
Add the state_encoding attribute
Add a state_encoding attribute with value one_hot for the type state_type. Use the syntax: attribute state_encoding : string; and attribute state_encoding of state_type : type is "one_hot";
VHDL
Hint
Declare the attribute with attribute state_encoding : string; first.
Then assign it to state_type with attribute state_encoding of state_type : type is "one_hot";.
3
Write the state transition process
Write a process called state_process that cycles current_state through the states in order: RED → GREEN → YELLOW → RED. Use a case statement on current_state and assign the next state accordingly.
VHDL
Hint
Use a process(current_state) with a case current_state is statement.
Assign current_state <= GREEN; when RED, and so on.
4
Print the current state
Add a report statement inside the state_process to print the current state name. Use report "Current state: RED" (or GREEN, YELLOW) depending on current_state.
VHDL
Hint
Use report "Current state: RED"; inside the when RED => branch, and similarly for other states.
Practice
(1/5)
1. What does the state_encoding attribute in VHDL specify?
easy
A. How states are represented in hardware
B. The clock frequency of the design
C. The input signal types
D. The output pin assignments
Solution
Step 1: Understand the role of state_encoding
The state_encoding attribute defines how the states of a finite state machine are encoded in hardware.
Step 2: Differentiate from unrelated attributes
Clock frequency, input types, and output pins are unrelated to state encoding.
Final Answer:
How states are represented in hardware -> Option A
Quick Check:
state_encoding = hardware state representation [OK]
Hint: Remember: state_encoding controls hardware state representation [OK]
Common Mistakes:
Confusing state_encoding with clock settings
Thinking it controls input/output pins
Assuming it defines signal types
2. Which of the following is the correct syntax to specify one-hot encoding for a state machine in VHDL?
easy
A. attribute state_encoding : string; attribute state_encoding of state_reg : signal is "one_hot";
B. attribute state_encoding : integer; attribute state_encoding of state_reg : signal is 1;
C. attribute encoding_state : string; attribute encoding_state of state_reg : signal is "binary";
D. attribute state_encoding : boolean; attribute state_encoding of state_reg : signal is true;
Solution
Step 1: Identify correct attribute declaration
The attribute state_encoding is declared as a string and assigned to the signal representing states.
Step 2: Match correct encoding value and syntax
"one_hot" is the correct string value for one-hot encoding; other options use wrong types or names.
Final Answer:
attribute state_encoding : string; attribute state_encoding of state_reg : signal is "one_hot"; -> Option A
Quick Check:
Correct attribute syntax uses string and "one_hot" [OK]
Hint: Use string type and exact "one_hot" value for encoding [OK]
Common Mistakes:
Using wrong attribute name like encoding_state
Assigning non-string types to state_encoding
Missing the 'of' keyword in attribute assignment
3. Given the following VHDL snippet:
attribute state_encoding : string;
attribute state_encoding of state_reg : signal is "binary";
signal state_reg : std_logic_vector(2 downto 0);
-- States: IDLE=000, RUN=001, STOP=010
What is the encoding type used for state_reg?
medium
A. One-hot encoding
B. Binary encoding
C. Gray encoding
D. No encoding specified
Solution
Step 1: Read the attribute assignment
The attribute state_encoding is assigned the string "binary" for the signal state_reg.
Step 2: Identify encoding type from attribute value
"binary" means the states are encoded in binary format.
Final Answer:
Binary encoding -> Option B
Quick Check:
Attribute value "binary" = Binary encoding [OK]
Hint: Check the string assigned to state_encoding attribute [OK]
Common Mistakes:
Confusing binary with one-hot
Ignoring the attribute assignment
Assuming default encoding without attribute
4. Identify the error in this VHDL code snippet related to state encoding:
attribute state_encoding : string;
attribute state_encoding of state_reg : signal is one_hot;
signal state_reg : std_logic_vector(3 downto 0);
medium
A. state_encoding attribute cannot be applied to signals
B. state_encoding attribute must be integer
C. Missing quotes around one_hot string
D. state_reg signal must be integer type
Solution
Step 1: Check attribute value syntax
The value assigned to state_encoding must be a string literal, so it requires quotes around "one_hot".
Step 2: Verify other parts are correct
The attribute is correctly declared as string, and signal type is valid.
Final Answer:
Missing quotes around one_hot string -> Option C
Quick Check:
Attribute values need quotes for strings [OK]
Hint: Always put encoding names in quotes [OK]
Common Mistakes:
Omitting quotes around encoding names
Changing attribute type incorrectly
Applying attribute to wrong object types
5. You want to optimize a state machine for speed and use one-hot encoding. Which VHDL attribute setting is best to apply to your state register signal fsm_state?
hard
A. attribute state_encoding : string; attribute state_encoding of fsm_state : signal is "none";
B. attribute state_encoding : string; attribute state_encoding of fsm_state : signal is "binary";
C. attribute state_encoding : string; attribute state_encoding of fsm_state : signal is "gray";
D. attribute state_encoding : string; attribute state_encoding of fsm_state : signal is "one_hot";
Solution
Step 1: Understand encoding impact on speed
One-hot encoding uses one bit per state, allowing faster decoding and simpler logic.