Electronics > FPGA

Verilog best way to replicate if else logic inside always block? Macros?

(1/1)

hal9001:
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!

BrianHG:
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.

ataradov:
You can also use functions.

BrianHG:
You can also use `define  label  (replacement function)

EG: (some old code of mine)

--- Code: ---`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        )

--- End code ---

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)


pbernardi:
Consider the use of ternary operators (? : )

For example, in case of:


--- Code: ---if (cond1)
  a <= value1;
else
  a <= value2;
--- End code ---

you can write:


--- Code: ---a <= cond1 ? value1 : value2;
--- End code ---

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.

Navigation

[0] Message Index

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod