I want to take in a parameter and assign a number of zeroes equal to the paramter to a constant, and use this constant for comparison. how do I do it ?
For example, say parameter is 3, I want to create a constant
n=3'b000;
and use this n in another statement. Only thing is, I don't know n. How do i initialize 'n' zeroes and to what verilog data type do I assign it to ?
In Verilog-1995[6], there are two ways to define constants: the parameter, a constant that is local to a module and macro definitions, created using the `define compiler directive. A parameter, after it is declared, is referenced using the parameter name.
'localparam' keyword is used to defined the constants in verilog.
Parameters are Verilog constructs that allow a module to be reused with a different specification. For example, a 4-bit adder can be parameterized to accept a value for the number of bits and new parameter values can be passed in during module instantiation. So, an N-bit adder can become a 4-bit, 8-bit or 16-bit adder.
Your looking for the replication operator. The syntax is {replication_constant{value}}
.
An example of creating a bus of size WIDTH
to all zeros.
parameter WIDTH = 3;
wire [WIDTH-1:0] n = {WIDTH{1'b0}};
For full description of the replication operator, see IEEE std 1800-2012 § 11.4.12.1 "Replication operator"
To expand Gregs answer and answer what if you wanted 1 then all 0's.
Use a mixture of concatenation {a,b}
and replication {width{c}}
:
wire [WIDTH-1:0] n = { 1'b1, {WIDTH-1{1'b0}} } ;
While the '0
or '1
syntax is in use in SystemVerilog 'b0
for width matching is valid in older Verilog. In verilog-95 it would only width match upto 32 bits but that has since been rectified.
Example defining the reset values on flip-flops :
reg [7:0] a;
always @(posedge clk or negedge rst_n) begin
if(~rst_n) begin
a <= 'b0 ;
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With