verilog


Signal strengths

Table from here:

Strength Value
supply 7 Su
strong 6 St
pull 5 Pu
large 4 La
weak 3 We
medium 2 Me
small 1 Sm
highz 0 HiZ

See:

Signal states ??

~Z~ 'High impedence' / 'tristate'
~X~ Unknown state
~?~ Don't care

In simulation, all values start with x by default.

AND 0 1 x z
0 0 0 0 0
1 0 1 x x
x 0 x x x
z 0 x x x

initial block

https://www.pace.edu.in/img/course/Verilog_HDL_Module4.pdf

Assignments

General

Literals

Format: n'bVAL:

Default is base is decimal.

https://users.ece.utexas.edu/~patt/04s.382N/tutorial/verilog_manual.html

Compiler directives

See:

Macros

// word size
`define wsize 32
`define word [`wsize-1:0]
...
...
input `word a, b;

Operators

Bitwise operators

~ Unary negation
& AND
OR
^ XOR
~^a or ^~a XNOR

Reduction operators

&a Reduction AND
│a Reduction OR
~&a Reduction NAND
~│a Reduction NOR
^ Reduction XOR
~^a or ^~a Reduction XNOR

Indexing into vectors

Possible for sized values like vectors ??

Bit-select

'Indexing' into a vector/similar

Example:

wire [7:0] a;

// 0th bit of a
a[0]

// 4th bit of a
a[4]

Part-select

'Slicing a vector/similar

Example:

wire [7:0] a;

// 2nd, 1st and 0th bits of a
a[2:0]

// 5th, 4th and 3rd bits of a
a[5:3]

System tasks

See:

specify blocks

See:

Misc

iverilog

Tips

DBT

temp[0] <= a^b;
temp[1] <= a&b;

SystemVerilog

Stuff not in verilog:

enum type

typedef enum {red, green, blue} Colour;

typedef enum {red=10, green=20, blue=30} Colours;

always stuff

Verilog's always block was less expressive. Specialized blocks convey the meaning more clearly.

Associative array

// Assoc array with string index and int values
int fruitprice[string];

fruitprice = '{"Apple": 190,
               "Orange": 120};

See: https://www.chipverify.com/systemverilog/systemverilog-associative-array

Classes

https://www.chipverify.com/systemverilog/systemverilog-class

Command line arguments

Constraints

bit [7:0] addr;
constraint addr_limit { addr <= 8'hB; }

https://www.chipverify.com/systemverilog/systemverilog-constraints

Coverage

https://www.chipverify.com/systemverilog/systemverilog-functional-coverage

iverilog

Apparently, iverilog doesn't consider input as systemverilog by default. Got to use -g2005-sv.

$ iverilog -g mem.sv 
Unknown/Unsupported Language generation mem.sv

Supported generations are:
    1995    -- IEEE1364-1995
    2001    -- IEEE1364-2001
    2005    -- IEEE1364-2005
    2005-sv -- IEEE1800-2005
    2009    -- IEEE1800-2009
    2012    -- IEEE1800-2012
Other generation flags:
    assertions | supported-assertions | no-assertions
    specify | no-specify
    verilog-ams | no-verilog-ams
    std-include | no-std-include
    relative-include | no-relative-include
    xtypes | no-xtypes
    icarus-misc | no-icarus-misc
    io-range-error | no-io-range-error
    strict-ca-eval | no-strict-ca-eval
    strict-expr-width | no-strict-expr-width
    shared-loop-index | no-shared-loop-index

$ iverilog -g2005-sv mem.sv 

Parenthesis in if condition

Looks like boolean expressions serving as conditions for if always need to be enclosed within parenthesis.

This works:

if (en_wr)

but this doesn't

if en_wr

Macros

Execute only if a macro is defined:

`ifdef MACRO_NAME

`endif