Challenge - 5 Problems
VHDL Generic Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of a VHDL module with generic parameter
What is the output value of the signal
result after simulation of this VHDL code snippet?VHDL
library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.NUMERIC_STD.ALL; entity Adder is generic ( WIDTH : integer := 4 ); port ( A, B : in std_logic_vector(WIDTH-1 downto 0); result : out std_logic_vector(WIDTH-1 downto 0) ); end Adder; architecture Behavioral of Adder is begin result <= std_logic_vector(unsigned(A) + unsigned(B)); end Behavioral; -- Testbench snippet -- WIDTH = 4 -- A = "0011" (3 decimal) -- B = "0101" (5 decimal)
Attempts:
2 left
💡 Hint
Remember that the generic WIDTH defines the bit width of the inputs and output. The addition is unsigned.
✗ Incorrect
The inputs A and B are 4-bit vectors representing decimal 3 and 5. Adding 3 + 5 = 8, which in 4-bit binary is "1000".
🧠 Conceptual
intermediatePurpose of generic parameters in VHDL modules
What is the main purpose of using generic parameters in VHDL modules?
Attempts:
2 left
💡 Hint
Think about how generics help reuse the same module with different settings.
✗ Incorrect
Generic parameters let you set constant values when you instantiate the module, so the same code can be reused with different configurations.
🔧 Debug
advancedIdentify the error in generic usage
What error will occur when compiling this VHDL code snippet?
VHDL
entity Counter is generic ( MAX_COUNT : integer := 10 ); port ( clk : in std_logic; count : out integer ); end Counter; architecture Behavioral of Counter is signal internal_count : integer := 0; begin process(clk) begin if rising_edge(clk) then if internal_count = MAX_COUNT then internal_count <= 0; else internal_count <= internal_count + 1; end if; end if; end process; count <= internal_count; end Behavioral;
Attempts:
2 left
💡 Hint
Check if generics can be used in comparisons and if signals can be assigned inside processes.
✗ Incorrect
The generic MAX_COUNT is used correctly in the comparison. The signal internal_count is assigned inside a clocked process, which is valid. The output port count is assigned from the signal internal_count of the same type integer. No errors occur.
📝 Syntax
advancedCorrect syntax for generic map in instantiation
Which option shows the correct way to instantiate a VHDL module with a generic parameter
WIDTH set to 8?VHDL
entity MyModule is generic ( WIDTH : integer := 4 ); port ( data_in : in std_logic_vector(WIDTH-1 downto 0); data_out : out std_logic_vector(WIDTH-1 downto 0) ); end MyModule;
Attempts:
2 left
💡 Hint
Generic map uses => to assign values to generic names.
✗ Incorrect
Option A uses the correct syntax: generic map (WIDTH => 8). Option A incorrectly puts WIDTH in port map. Option A reverses the mapping. Option A uses = instead of =>.
🚀 Application
expertDetermine the number of bits in output for configurable generic
Given this VHDL entity with a generic parameter
DEPTH that defines the size of a memory, what is the width of the address port if DEPTH is set to 256?VHDL
entity Memory is generic ( DEPTH : integer := 16 ); port ( address : in std_logic_vector(integer(ceil(log2(real(DEPTH))))-1 downto 0); data_in : in std_logic_vector(7 downto 0); data_out : out std_logic_vector(7 downto 0) ); end Memory;
Attempts:
2 left
💡 Hint
Calculate how many bits are needed to address 256 locations.
✗ Incorrect
To address 256 locations, you need log2(256) = 8 bits. So the address port width is 8 bits.
