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

  • These operators are unary.
  • Kind of like a fold
    • Eg: Reduction AND does AND of all bits in operand vector
&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;

Splitting long lines

This works:

// Verilog
module full_adder (
  input a, b, cin,
  output s, cout
);

  assign s = a ^ b ^ cin;
  assign cout =
      (a & b)
    | (b & cin)
    | (cin & a);
endmodule

SystemVerilog

Stuff not in verilog:

enum type

typedef enum {red, green, blue} Colour;

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

always stuff

  • always_comb: a combinatory block
    • sensitisation list can be inferred automatically if all used signals are there ??
  • always_ff: infer 'transparent flip flip'
  • always_latch: infer 'transparent latch'

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

Associative array

  • Like python dictionaries
  • Declaration syntax: <val_type> <name> [<index_type>];
// 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

  • Syntax is kind of like JavaScript classes.
  • this: current object (or is it class??)
  • function new (): constructor
    • Auto invoked on object creation
  • Inheritance: extends
    • Eg: class subclass extends baseclass;
    • Invoke superclass attribute: super.<attribute>
  • virtual class: abstract class
    • Can only be extended.
    • Cannot be instantiated.

Command line arguments

Constraints

  • Kind of like refined types
  • Useful during verification/testing
bit [7:0] addr;
constraint addr_limit { addr <= 8'hB; }

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

Coverage

  • coverpoint: variables
  • covergroup block: groups cover points

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

Clocking blocks

See: https://theoctetinstitute.com/content/sv/clocking-blocks/

From https://www.doulos.com/knowhow/systemverilog/systemverilog-tutorials/systemverilog-clocking-tutorial/:

A clocking block is a set of signals synchronised on a particular clock

Example:

clocking drv_cb @(posedge clk);
  output a, b, cin;
endclocking

Macros

Execute only if a macro is defined:

`ifdef MACRO_NAME

`endif

Linters

svlint

https://github.com/dalance/svlint

  • Configuration file: .svlint.toml
  • Get a template config file: svlint --example > .svlint.toml
  • Newer svlint may change names of variables in config file. Can use: svlint --update .svlint.toml > new.toml

Verible

https://github.com/chipsalliance/verible/tree/master/verible/verilog/tools/lint

For ignoring a linter error, use: // verilog_lint: waive rule-name before that line.

$ verible-verilog-lint tb/*

tb/ifc.sv:18:1-2: Remove trailing spaces. [Style: trailing-spaces] [no-trailing-spaces]
tb/ifc.sv:33:31: Remove trailing spaces. [Style: trailing-spaces] [no-trailing-spaces]
tb/scoreboard.sv:26:87: Remove trailing spaces. [Style: trailing-spaces] [no-trailing-spaces]
tb/scoreboard.sv:30:101-119: Line length exceeds max: 100; is: 119 [Style: line-length] [line-length]
tb/scoreboard.sv:30:119: Remove trailing spaces. [Style: trailing-spaces] [no-trailing-spaces]
tb/top.sv:31:15: Remove trailing spaces. [Style: trailing-spaces] [no-trailing-spaces]

Autofix: verible linter can try to auto-fix errors:

  • --autofix_output_file path for writing diff. Patch is written to stdout if not given

  • Eg: --autofix=inplace-interactive

    no No fix is is shown/applied.
    patch-interactive Interactive choice of fixes that are written as unified diff to –autofixoutputfile.
    inplace-interactive Interactive choice of fixes that are applied to the original file in place. (modifies input)
    patch All available fixes are written as unified diff.
    inplace All available fixes are applied to the original file in place. (modifies input)
    generate-waiver Generates a waiver rule for each violation, as a temporary fix.