Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

VHDL. rewrite FOR LOOP using FOR GENERATE

4pi

Newbie
Joined
Oct 26, 2023
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
15
Hi there!

i have FOR LOOP (here WIDTH=8, B=3)
Code:
for i in 0 to (WIDTH - 1) loop
    if r(i)='1' then
        bcode <= std_logic_vector(to_unsigned(i, B));
    end if;
end loop;

and i have to rewrite it using FOR GENERATE.
This is not work:
Code:
for i in 0 to (WIDTH - 1) generate
    bcode <= std_logic_vector(to_unsigned(i, B)) when r(i) = '1';
end generate;

Errors:
Error (10028): Can't resolve multiple constant drivers for net "bcode[2]" at ***.vhd(33)
Error (10029): Constant driver at ***.vhd(33)
Error (10028): Can't resolve multiple constant drivers for net "bcode[1]" at ***.vhd(33)
Error (10028): Can't resolve multiple constant drivers for net "bcode[0]" at ***.vhd(33)

should i write ... <= ... when ... else... ?
But i dont have to do nothing in case r(i)=0.
 
Both codes make no sense. You are writing bcode multiple times without reading it. Insequential code (first variant) multiple writes are legal, but only the last (i = WIDTH - 1) is actually performed. In concurrent code, a signal can't be written more than once.
 
this is priority encoder, i need only one value (last, in sequential code).

in second - so it have to be array?
 
this is priority encoder, i need only one value (last, in sequential code).
O.k. then use sequential code. It doesn't work in concurrent code (generate). If it's only exercise - I'm not interested.
--- Updated ---

You understand that the priority is resolved in reversed order. Therefore you can write something like
if r(highest index) = '1' then
elsif r(highest index - 1) = '1' then
etc.
 
Hi there!

i have FOR LOOP (here WIDTH=8, B=3)
Code:
for i in 0 to (WIDTH - 1) loop
    if r(i)='1' then
        bcode <= std_logic_vector(to_unsigned(i, B));
    end if;
end loop;

and i have to rewrite it using FOR GENERATE.
This is not work:
Code:
for i in 0 to (WIDTH - 1) generate
    bcode <= std_logic_vector(to_unsigned(i, B)) when r(i) = '1';
end generate;



should i write ... <= ... when ... else... ?
But i dont have to do nothing in case r(i)=0.
if i is run-time integer (one value in the range 0-7 at any clock) then you only need to code as follows:

Code:
if r(i) = '1' then
   bcode <= std_logic_vector(to_unsigned(i, B));
end if;

no need for loop and certainly no need for generate. But what is r(i) ?
 
Last edited:
A loop is meant for the compiler to unroll. It is just for convenience instead of repeating code. It is not mapped to any loop inside hardware. If you want to know what the compiler sees then unroll it to see each statement. For your case, the compiler sees:

if i = 0 then
bcode <= std_logic_vector(to_unsigned(0, B));
elsif i = 1 then
bcode <= std_logic_vector(to_unsigned(1, B)); ...etc
end if;

Thus bcode is assigned in every line. This is like multiple drivers, and is bad coding. I guess the compiler may be kind and just take the last statement or flag error.

if bcode is indexed such as bcode(i) and so is 2D array then you get bcode(i) assigned per each i value. Then you decide a further selection from the 2D array.

If bcode must be 1D array then you need i to be run time indexing(counter), not a loop index.

On the other hand, generate is also a compile time statement but for telling the tool what to include in synthesis or not, right at start. It is unrelated to loop.
 
Last edited:

LaTeX Commands Quick-Menu:

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top