0

Suppose I have the following module definition,

module foo(b)
input b;
parameter length = 8;

reg [length-1:0] dummy ;

Now, I want to assign values to this dummy array. For instance I want to make it all 1s. If the length was not parameterized, I could do this,

always @(posedge b)
 dummy <= 8'hFF;

But when the length is parameterized, I would hope to do this,

always @(posedge b)
 dummy <= length'hFFFF //won't even compile. even if it did, how many F's should there be?

How can I assign ones (or zeroes) to an entire array whose length is parameterized? Or more generally, how can I assign values while specifing the length of a parameterized array?

2
  • Do you really mean packed arrays? reg [length-1:0] dummy; Your declaration was unpacked, and you cannot have those kind of assignments to unpacked array, parameterized or not. Commented Jul 30, 2020 at 18:45
  • @dave_59 My bad, I just fixed the declaration. Commented Jul 30, 2020 at 18:51

2 Answers 2

5

You can do bit extension:

always @ (posedge b)
  dummy <= {length{1'b1}};

What is inside the {} is extended by "parameter-1", would be the same as having:

always @ (posedge b)
  dummy <= {1'b1,1'b1,1'b1,1'b1....};
Sign up to request clarification or add additional context in comments.

Comments

1

You can write

always @(posedge b)
 dummy <= ~1'b0;

This takes advantage of the fact that Verilog extends operands before applying operators when they are in context-determined expressions.

In SystemVerilog, you can write

always @(posedge b)
 dummy <= '1;

2 Comments

What difference would writing dummy <= ~0; make, if any?
No difference in this expression. But do realize that 0 is the same as writing 32'sd0

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.