2

I am currently designing Verilog logic that is generic so that I can modify the width of registers based on parameters that is being passed as inputs to the module. The parameters are RWIDTH and BWIDTH. Currently I am testing it for two cases

RWIDTH=16, BWIDTH=16 and

RWIDTH=20, BWIDTH=16

I am having some issues in making the code adapt to different width. Below is a small portion of the code.

localparam DIFF = RWIDTH-BWIDTH;
localparam BYTE_LENGTH = 8
localparam ROUND_2_BYTE = BYTE_LENGTH-DIFF;

When RWIDTH=16, BWIDTH=16 , I want ROUND_2_BYTE to be equal to 0

but when RWIDTH=20, BWIDTH=16, I want ROUND_2_BYTE to be equal to 4

Please note that ROUND_2_BYTE will be used to calculate further parameters that I have not shown for the sake of simplicity.

How can I achieve this using localparams or any other way in Verilog? I am also interested to hear about other approaches that the above can be achieved.

toolic
  • 57,801
  • 17
  • 75
  • 117
user2532296
  • 828
  • 1
  • 10
  • 27

2 Answers2

3

Use a conditional operator. Change:

localparam ROUND_2_BYTE = BYTE_LENGTH-DIFF;

to:

localparam ROUND_2_BYTE = (DIFF == 0) ? 0 : BYTE_LENGTH-DIFF;
toolic
  • 57,801
  • 17
  • 75
  • 117
3

You can use a function call to put whatever code you need to set a parameter if you can't fit it into a single equation. The function input arguments must all be constants or other parameters

function integer R2B(integer R,B);
  case (1)
  R==16 && B==16: R2B = 0;
  R==20 && B==16: R2B = 4;
  endcase
endfunction

localparam ROUND_2_BYTE = R2B(RWIDTH, BWIDTH);
dave_59
  • 39,096
  • 3
  • 24
  • 63