Author Topic: Verilog best way to replicate if else logic inside always block? Macros?  (Read 1413 times)

0 Members and 1 Guest are viewing this topic.

Offline hal9001Topic starter

  • Regular Contributor
  • *
  • Posts: 115
  • Country: 00
I want to replicate logic inside an always block. The logic contains if elses. The same logic is used a few places in the always block with only register names and parameters changing.
Whats the best way to do this in Verilog (not SystemVerilog)? Im using Xilinx ISE. Maybe multi line macros can be used but I dont find examples for it in Verilog?


Cheers!
« Last Edit: March 27, 2024, 04:38:43 am by hal9001 »
 

Offline BrianHG

  • Super Contributor
  • ***
  • Posts: 7742
  • Country: ca
Since you aren't showing us an example of what you want to replace, it is hard to make an appropriate suggestion.

Have you thought about using a 'case' statement?

You also have access to 'while' and 'for' loops.

If you want to replicate logic based on parameters, you can  even go as far as using compiler code generating directives like 'generate' and 'genvar'.

If you have grouped circumstances for your 'if' statements, you can always use 'assign' to create a shortcut net/label.

I mean, there is just soooo much which is possible.

Probably you are looking to use the 'for' loop to operate repetitious logic on different points in an array.
« Last Edit: March 27, 2024, 05:20:06 am by BrianHG »
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 11261
  • Country: us
    • Personal site
You can also use functions.
Alex
 
The following users thanked this post: BrianHG

Offline BrianHG

  • Super Contributor
  • ***
  • Posts: 7742
  • Country: ca
You can also use `define  label  (replacement function)

EG: (some old code of mine)
Code: [Select]
`define VOL_factor ( ( DAC_LSB_DB[DAC_BITS]+LSB_DB_CORRECTION[DAC_BITS] ) /31 )      // factor the decibel range over the 5 bit volume range.

// Volume attenuation to linear amplitude formula with -infinity/basement mute correction.
`define atten_dbv(x)    ( (10**(((31-x)*`VOL_factor)/20) *(2**DAC_BITS-1))   )
`define gain_fix        ( (2**DAC_BITS-1) / (`atten_dbv(31) - `atten_dbv(0)) )
`define dac_vout(z)     ( (`atten_dbv(z) - `atten_dbv(0)) * `gain_fix        )

However, it's use cases are kinda narrow and I only use it to simplify some long esoteric calculation function down to a single final function name.  I only used it for verilog coding once in my life, the example above.
(The code was used to generate a table of constants based on the module's set parameter, DAC_BITS.  The constants became a 32 entry rom LUT table with the width of DAC_BITS)


« Last Edit: March 27, 2024, 06:45:33 pm by BrianHG »
 

Offline pbernardi

  • Contributor
  • Posts: 17
  • Country: br
Consider the use of ternary operators (? : )

For example, in case of:

Code: [Select]
if (cond1)
  a <= value1;
else
  a <= value2;

you can write:

Code: [Select]
a <= cond1 ? value1 : value2;
The restriction is that the affected register must be the same on the if and else condition.

as cited above, "case" may be useful in several situations as well, or even "generate". Once you did not post your code, we do not know exactly which approach is more suitable.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf