Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6823797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:49:34+00:00 2026-05-26T21:49:34+00:00

I have a variable number of modules linked to another module via a signal

  • 0

I have a variable number of modules linked to another module via a signal bus : std_logic_vector(NUM-1 downto 0), with each component using 8 bits, so that:

bus(7 downto 0) = first module
bus(15 downto 8) = second module

As for creating the instances and doing the port mapping, that is easily done with a

INST: for i in 0 to NUM-1 generate
         Inst_module port map ( bus => bus(i*8+7 downto i*8) );
       end generate INST;

My question:
I would like to be able to interface with each module via a FSM (since it needs to do some other things too), so would like to be able to ‘generate’ the following code, rather than having to write out each state manually (Where signal empty : std_logic_vector(NUM-1 downto 0) is a status flag for each module)

 type state_type is (st0_idle, st1_work0, st1_work1 --,etc.)
 signal state : state_type;
 begin
 process(empty)
   begin
     if RESET = '1' then
        --reset FSM
        state <= st0_idle;
     else
       if CLK'event and CLK='1' then
         case state is
           when st0_idle =>
             if empty(0) = '0' then
               state <= st1_work0;
             elsif empty(1) = '1' then
               state <= st1_work1;
             --etc.
             end if;             
           when st1_work0 =>
             bus(7 downto 0) <= SOMETHING;
             state <= st0_idle;
           when st1_work1 =>
              bus(15 downto 8) <= SOMETHINGELSE;
              state <= st0_idle;
            --etc..
       end if;
     end if;
end process;

As you can see, there is a lot of repetition. But I can’t simply put a for-generate inside the case, so what should I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:49:35+00:00Added an answer on May 26, 2026 at 9:49 pm

    One good way to make processes with state machines more readable is to merge common code into procedures defined within the process. For example:

    process (empty) is
    
      procedure assign_something (
        index      : natural;
        something  : std_logic_vector(7 downto 0)
        next_state : state_type
      ) is
      begin
        bus(index*8+7 downto index*8) <= something;
        state <= next_state;
      end procedure;
    
    begin
      wait until rising_edge(clk);
      case state is
        when st0_idle  => ...
        when st1_work0 => assign_something(0, something,      st0_idle);
        when st1_work1 => assign_something(1, something_else, st0_idle);
        -- ... etc ...
      end case;
      if reset = '1' then
        state <= st0_idle;
      end if;
    end procedure;
    

    Hopefully you get the idea. Depending on how regular the state machine structure is, you may also want to replace the enumerated state variables that correspond to each index with a simple count or index variable that you keep track of along with the named state.

    That’s all up to you, but however you do it, using procedures to factor out common code whenever you can will probably make your VHDL much easier to work with.

    Applying this change would make the code look something like this:

    architecture ...
    
    type state_type is (st_idle, st_work);
    signal state : state_type;
    signal index : integer range 0 to NUM-1;
    
    ...
    begin
    ...
    
    process (empty) is
    
      procedure assign_something (
        index      : natural;
        something  : std_logic_vector(7 downto 0)
        next_state : state_type
      ) is
      begin
        bus(index*8+7 downto index*8) <= something;
        state <= next_state;
      end procedure;
    
    begin
      wait until rising_edge(clk);
      case state is
        when st_idle  => 
          for i in 0 to NUM-1 loop
             if empty(i) = '1' then
               index := i;
               exit;
             end if;
          end loop;
        when st_work => assign_something(index, something, st_idle);
      end case;
      if reset = '1' then
        state <= st_idle;
      end if;
    end procedure;
    

    Obviously this has to be changed to match exactly what you want to do … =)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How I can have variable number of parameters in my function in C++. Analog
Is it possible to have a variable number of fields using django forms? The
I have a variable of type Number, and i like to obtain the sign
I have a PHP function that takes a variable number of arguments (using func_num_args()
I have an app that creates a variable number of ScatterviewItems based on which
I have a function that accepts a variable number of parameters: foo (Class... types);
I have an ASP.NET web form which I am adding a variable number User
I have a form that has 8 columns and a variable number of rows
Say I have a C function which takes a variable number of arguments: How
Greetings, I have a form with a variable number of inputs, a simplified version

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.