2
\$\begingroup\$

I would like to create a parametric bit-width assignment in Verilog.

Something like the following code:

module COUNTER
 ( 
 CLEAR,
 CLK,
 CODE)
#(parameter BUS_WIDTH = 8)
reg [BUS_WIDTH-1:0] CODE;
always @(posedge CLK or posedge CLEAR)
begin
 if(CLEAR)
 begin
 CODE <= BUS_WIDTH'b{BUS_WIDTH{0}}; 
 CODEreg <= BUS_WIDTH'b{BUS_WIDTH{0}};
 end
 ... 

What is the right way for it to be synthesizable?

toolic
10.8k11 gold badges31 silver badges35 bronze badges
asked Feb 14, 2022 at 11:40
\$\endgroup\$

2 Answers 2

5
\$\begingroup\$

You were very close. The proper syntax is as follows:

 CODE <= {BUS_WIDTH{1'b0}}; 
 CODEreg <= {BUS_WIDTH{1'b0}};

Refer to IEEE Std 1800-2017, section 11.4.12.1 Replication operator.

The one bit value (1'b0) is replicated 8 times to form the 8-bit value (8'b0000_0000).

This is synthesizable.

This is also called a multiple concatenation.

answered Feb 14, 2022 at 11:57
\$\endgroup\$
4
\$\begingroup\$

There is no need to do this unless you have linting tools and need to suppress warnings about mismatched sizes. Verilog implicitly pads or truncates expressions to the appropriate width.

if(CLEAR)
 begin
 CODE <= 0;
 CODEreg <= 0;
 end

SystemVerilog has a fill literal '0, '1, 'z that automatically replicates the value to the target expression

if(CLEAR)
 begin
 CODE <= '0; 
 CODEreg <= '1;
 end
answered Feb 14, 2022 at 22:43
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.