Bird
Raised Fist0
VHDLprogramming~20 mins

Generic parameters for configurable modules in VHDL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
VHDL Generic Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output 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)
A"1000"
B"0000"
C"1111"
D"0110"
Attempts:
2 left
💡 Hint
Remember that the generic WIDTH defines the bit width of the inputs and output. The addition is unsigned.
🧠 Conceptual
intermediate
1:30remaining
Purpose of generic parameters in VHDL modules
What is the main purpose of using generic parameters in VHDL modules?
ATo enable the module to change its behavior dynamically during simulation
BTo allow the module to be configured with different constant values at compile time
CTo define input and output ports of the module
DTo specify the clock frequency of the module
Attempts:
2 left
💡 Hint
Think about how generics help reuse the same module with different settings.
🔧 Debug
advanced
2:30remaining
Identify 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;
AError: Generic parameter 'MAX_COUNT' cannot be used in a comparison
BError: Cannot assign to signal 'internal_count' inside process without reset
CError: Output port 'count' cannot be assigned from a signal of type integer
DNo error; code compiles and simulates correctly
Attempts:
2 left
💡 Hint
Check if generics can be used in comparisons and if signals can be assigned inside processes.
📝 Syntax
advanced
2:00remaining
Correct 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;
AU1: entity work.MyModule generic map (WIDTH => 8) port map (data_in => input_signal, data_out => output_signal);
BU1: entity work.MyModule port map (WIDTH => 8, data_in => input_signal, data_out => output_signal);
CU1: entity work.MyModule generic map (8 => WIDTH) port map (data_in => input_signal, data_out => output_signal);
DU1: entity work.MyModule generic map (WIDTH = 8) port map (data_in => input_signal, data_out => output_signal);
Attempts:
2 left
💡 Hint
Generic map uses => to assign values to generic names.
🚀 Application
expert
3:00remaining
Determine 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;
A256 bits
B7 bits
C8 bits
D16 bits
Attempts:
2 left
💡 Hint
Calculate how many bits are needed to address 256 locations.

Practice

(1/5)
1. What is the main purpose of generic parameters in a VHDL module?
easy
A. To declare variables local to a process
B. To define signals inside the architecture
C. To specify the clock frequency of the design
D. To customize module behavior without changing its internal code

Solution

  1. Step 1: Understand the role of generics in VHDL

    Generics allow you to set parameters that can change module behavior without rewriting the module code.
  2. Step 2: Compare with other options

    Signals and variables are different constructs; generics are not for clock frequency specifically.
  3. Final Answer:

    To customize module behavior without changing its internal code -> Option D
  4. Quick Check:

    Generics = customizable parameters [OK]
Hint: Generics customize modules without code changes [OK]
Common Mistakes:
  • Confusing generics with signals or variables
  • Thinking generics set clock frequency only
  • Assuming generics are declared inside architecture
2. Which of the following is the correct syntax to declare a generic parameter named WIDTH of type integer with default value 8 in a VHDL entity?
easy
A. constant WIDTH : integer := 8;
B. generic WIDTH : integer = 8;
C. generic (WIDTH : integer := 8);
D. parameter WIDTH : integer := 8;

Solution

  1. Step 1: Recall VHDL generic declaration syntax

    Generics are declared inside parentheses after the keyword generic with colon and default value using :=.
  2. Step 2: Check each option

    generic (WIDTH : integer := 8); matches correct syntax: generic (WIDTH : integer := 8);. Others use incorrect keywords or syntax.
  3. Final Answer:

    generic (WIDTH : integer := 8); -> Option C
  4. Quick Check:

    Generic syntax uses parentheses and := [OK]
Hint: Generics use parentheses and := for defaults [OK]
Common Mistakes:
  • Using '=' instead of ':=' for default values
  • Confusing generics with constants or parameters
  • Omitting parentheses around generic list
3. Given the following VHDL entity and architecture:
entity MyModule is
  generic (WIDTH : integer := 4);
  port (
    input : in std_logic_vector(WIDTH-1 downto 0);
    output : out std_logic_vector(WIDTH-1 downto 0)
  );
end MyModule;

architecture Behavioral of MyModule is
begin
  output <= input;
end Behavioral;

What will be the width of output if this module is instantiated with generic map (WIDTH => 8)?
medium
A. 8 bits
B. 4 bits
C. WIDTH bits (unknown)
D. 1 bit

Solution

  1. Step 1: Understand generic override on instantiation

    The generic WIDTH default is 4, but overridden to 8 during instantiation.
  2. Step 2: Calculate output width

    Output is std_logic_vector(WIDTH-1 downto 0), so with WIDTH=8, output is 8 bits wide.
  3. Final Answer:

    8 bits -> Option A
  4. Quick Check:

    Generic override sets WIDTH=8 [OK]
Hint: Generic override changes WIDTH to 8 bits [OK]
Common Mistakes:
  • Ignoring generic override and using default 4
  • Confusing WIDTH with signal length
  • Assuming output is always 1 bit
4. Identify the error in this VHDL entity declaration:
entity ConfigurableCounter is
  generic WIDTH : integer := 8;
  port (
    clk : in std_logic;
    count : out std_logic_vector(WIDTH-1 downto 0)
  );
end ConfigurableCounter;
medium
A. Missing parentheses around generic declaration
B. WIDTH should be declared as a signal, not generic
C. Port declaration syntax is incorrect
D. No error, declaration is correct

Solution

  1. Step 1: Check generic declaration syntax

    Generics must be declared inside parentheses after the keyword generic.
  2. Step 2: Analyze given code

    The code declares generic WIDTH : integer := 8; without parentheses, which is invalid syntax.
  3. Final Answer:

    Missing parentheses around generic declaration -> Option A
  4. Quick Check:

    Generics need parentheses [OK]
Hint: Always use parentheses for generics [OK]
Common Mistakes:
  • Omitting parentheses in generic declaration
  • Confusing generics with signals
  • Assuming port syntax is wrong when it's correct
5. You want to create a VHDL module for a FIFO buffer where the depth and data width can be set using generics. Which of the following is the best way to declare these generics in the entity?
hard
A. generic DEPTH : integer := 16; WIDTH : integer := 8;
B. generic (DEPTH : integer := 16; WIDTH : integer := 8);
C. constant DEPTH : integer := 16; constant WIDTH : integer := 8;
D. signal DEPTH : integer := 16; signal WIDTH : integer := 8;

Solution

  1. Step 1: Recall correct generic declaration for multiple parameters

    Multiple generics must be declared inside parentheses separated by semicolons.
  2. Step 2: Evaluate each option

    generic (DEPTH : integer := 16; WIDTH : integer := 8); correctly declares both generics inside parentheses with default values. Options B, C, and D use incorrect keywords or syntax.
  3. Final Answer:

    generic (DEPTH : integer := 16; WIDTH : integer := 8); -> Option B
  4. Quick Check:

    Multiple generics use parentheses and semicolons [OK]
Hint: Use parentheses and semicolons for multiple generics [OK]
Common Mistakes:
  • Declaring generics without parentheses
  • Using constant or signal instead of generic
  • Separating generics with commas instead of semicolons
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard