0

I have a doubt in the generate, my code is:

parameter m=1;

generate 
 for(i=0; i<m; i=i+1) :loopstart
 begin
  statements;
 end
endgenerate

Inside this loop, m should be 2^0, 2^1, 2^2, and so on. Since exponentiation is not supported, I thought of initializing m and then multiplying it by 2 on each iteration.

I have a few questions:

Is it possible to use m << 1 inside the generate in some way (since this is the same as multiplying by 2)? If I do that, it results in an error.

I referred to Samir Palnitkar's book, which says that always statement works inside a generate, so I tried:

always @(m)
 m <= m*2; // (or m << 1)

This doesn't work. I realize it can't be done because m is a parameter and not a variable.

If what I think is right, it can't be done with genvar either, since a genvar can't be initialized.

Is there an alternative?

2 Answers 2

1

The question seem to have been created because exponentials are not supported. They are :

2**0 => 1
2**1 => 2
2**2 => 4

NB:

always @(m)
 m <= m*2; // (or m << 1)

This is wrong for a few reasons

  1. Do not use <= in combinatorial blocks
  2. In the above expression m defines itself and re-triggers the loop.

I would also avoid named sensitivity lists and use always @* to avoid hardware simulation mismatches from incomplete sensitivity lists.

parameters and localparams are for defining constants, if they are not constant use something else like a logic or integer type.

logic [31:0] m = 0;
logic [31:0] power_two_m;
always @* begin
  power_two_m = 2**m;
  power_two_m = 1 << m;
end
Sign up to request clarification or add additional context in comments.

Comments

0

Instead i++ use i=i+1

And.. forget C.

4 Comments

on no :( i typed i++ here by mistake. i have entered it correctly in the program..
I have solved it.. i took m in another loop above i loop and did m=m*2, thanks all :)
@Nandhini please post an answer describing how you resolved the problem
@ Andy : I had to do m=2^j for j=0,1,2 and so on inside a for loop inside 'generate' such that m=1,2,4,.... Instead what i did was that i removed j from the loop and just took the variable m inside the for loop and wrote for(m=1;condition;m=m*2). this way it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.