Hardware Modelling with SystemVerilog HDL


The SystemVerilog Hardware Description Language can be used to model a digital circuit during the design stage. Such a design may be simulated using the NC-Verilog digital simulator in order to make early descisions on the viability of a design. The design may then be further refined at this stage before any circuit is implemented. The final SystemVerilog HDL model may then be used as a base for circuit design or directly synthesized to produce a circuit implementation.

This page describes creation and refinement of SystemVerilog HDL model files. It also touches on their simulataion and synthesis. The details of simulation and synthesis are discussed elsewhere.

The SystemVerilog Hardware Description Language offers many more features than I could hope to cover in these pages. This page attempts to choose the most useful parts of the SystemVerilog HDL and illustrate their use with simple examples.

A definitive description of the SystemVerilog constructs used may be found in the Cadence Help on-line manual.


Structural Models

SystemVerilog HDL supports a number of built in logic gates from which larger systems can be constructed. The basic gates are:

Multi-input gates
(two or more inputs)
and or xor
nand nor xnor
e.g. and #delay inst (output, input1, input2, input3 .....);
Single-input gates
(one or more outputs)
buf
not
e.g. buf #delay inst (output1, output2 ....., input);
Tri-state gates bufif1 bufif0
notif1 notif0
e.g. bufif1 #delay inst (output, input, enable);

A simple strucural model is a netlist describing the interconnection of gates to create a module definition.


Structural Model of a D type flip-flop

The following file, dtype.sv, is a structural model for the D type flip-flop:

Major Features

Module Definition

The module...endmodule construct delimits the module definition.

The module statement defines the name of the module and is followed be an ordered list of ports in brackets.

Port List

Each port or group of ports has a direction and data type associated with it. Q and nQ are declared with direction output while D, Clk and nRst are declared with direction input (note that by convention the outputs are listed first).

The data type for all ports in this example is wire. Since this is the default data type, the keyword wire is optional and will usually be omitted from port lists in subsequent examples.

Timescale Declarations

These should exist at the top of each Verilog module. The timeunit defines the default units for time delays within the module while the timeprecision defines the maximum resolution for such delays.

Wire Declarations

Here all signals which will be used are declared as single bit wires. This section is optional since an undeclared signal is assumed to be a single bit wire.

Gate Instances

Each gate instance line consists of a gate type, a gate delay parameter (in this case #1 indicating 1ns), a unique instance name by which we can distiguish the different gates and an ordered list of wires to be connected to the gate instance. The ordering of these wires should correspond exactly with the ordering of the ports of the gate; for all these gates the only requirement is that the output is listed before the inputs.

Simulation

The stimulus file for this HDL model is dtype_stim.sv. The corresponding SimVision command script is dtype.tcl.

To simulate this design using the NC-Verilog simulator, copy the relevant files (dtype.sv, dtype_stim.sv & dtype.tcl.) to a suitable directory (e.g. ~/design/verilog/lab/model) and type the following at the unix command prompt:

If all goes well a waveform window should open:

Waveform Window

The simulation can then be run by typing "run" at the ncsim> prompt in the simulator console window or by pressing the play button: PLAY in any of the SimVision windows. Finally select the full view icon, Zoom Full X, to see the complete waveforms.


Structural Model of a 4-bit Counter

The following file, count.sv, is a structural model for the counter:

Major Features

Hierarchy

More complex designs employ a hierarchy of structural models. This design defines a counter module in terms of basic gates and the d-type flipflop, which is a sub-module defined in the file dtype.sv.

Thus we can redefine the stuctural model as a netlist describing the interconnection of gates and sub-modules to create a module definition.

Simulation

The stimulus file for this HDL model is count_stim.sv and the corresponding SimVision command script is count.tcl.

Type the following at the unix command prompt:

    ncverilog  +gui +access+r  count_stim.sv  count.sv  dtype.sv  +tcl+count.tcl  &

Then run the simulation ().


Behavioural Models

SystemVerilog HDL supports a number of operators from which expressions can be constructed. The basic operators are:

binary unary example
Logical single bit inputs
single bit output
&& || ! Logical AND
True && False = False
Relational multi-bit inputs
single bit output
== != >= <= Greater or Equal
9 >= 7 = True
Bit-wise Boolean multi-bit inputs
multi-bit output
& | ^ ~^ ~ Bit-wise XOR
1012 ^ 1102 = 0112
Arithmetic multi-bit inputs
multi-bit output
+ - * / % - Modulus
9 % 4 = 1
Shift multi-bit inputs
multi-bit output
>> << Right Shift
101102 >> 2 = 1012

Expressions can be used along with continuous assignment statements to provide behavioural models of combinational logic circuits.

More complex behavioural models, in particular those describing sequential circuits, will employ procedural assignments and more complex procedural statements. Such statements include descision statements such as if...else and case and even looping statements such as while and for.


Behavioural Model of a Window Comparator

The following file, window.sv, is a behavioural model of a digital window comparator:

Major Features

Continuous Assignment

A single or multi-bit wire may have its value determined as the output of an expression using a continuous assignment statement. The assignment is described as continuous since the expression on the right hand side is continuously monitored allowing any changes to be propagated to the wire on the left hand side of the assignment.

Simulation

The stimulus file for this HDL model is window_stim.sv and the corresponding SimVision command script is window.tcl.

Type the following at the unix command prompt:

Then run the simulation ().


Behavioural Model of a D type flip-flop

The following file, dff.sv, is a behavioural model of a D type flip-flop:

Major Features

Variables and Procedural Assignments

Q is defined as a logic variable. A variable may be assigned values at regular or irregular intervals. The variable will remember the value assigned until the next time an assignment takes place.

While continuous assignment may be used for wires or for variables, procedural assignments (within always or initial procedural blocks) may only be used for variables.

In SystemVerilog there are four variants to always. These are:

always
which is used in testbenches and other modules that will not be synthesized.
always_ff
which is used in sythesizable modules and hints to synthesis program that flip flop logic will be synthesized.
always_comb
which is used in sythesizable modules and hints to synthesis program that combinational logic will be synthesized.
always_latch
which is used in sythesizable modules and hints to synthesis program that transparent latch logic will be synthesized.

Event Control

@(posedge Clk) is an event control. In this example we see that every time we have a rising edge on the Clk signal we will assign the value of D to the variable Q. Between rising clock edges, Q remains unchanged regardless of any changes in the value of D.

This is the normal method for the description of a synchronous sequential system.

Simulation

The stimulus file for this HDL model is dff_stim.sv and the corresponding SimVision command script is dff.tcl.

Type the following at the unix command prompt:

    ncverilog  +gui +access+r  dff_stim.sv  dff.sv  +tcl+dff.tcl  &

Then run the simulation ().


Behavioural Model of an Up/Down Counter

The following file, up_down_count.sv, is a behavioural model of a 4-bit up/down counter:

Major Features

Reset

Here we see that the event control, @(posedge clock, negedge not_reset), will wait for either a rising edge on clock or a falling edge on not_reset.

The subsequent code gives reset priority such that when reset is active, count is held at zero regardless of the clock or other inputs.

This is the normal strategy for the description of an asynchronous reset within a synchronous sequential system.

Non-Blocking Procedural Assignments

Note that this module uses a non-blocking type of procedural assignment, X <= Y, rather than a blocking procedural assignment, X = Y.

The non-blocking version evaluates all right-hand-side expressions in a sequence before assigning values to any of the left-hand-side variables.

The difference is indicated by the following piece of code which swaps the values of A and B:

Rather than the alternative which leaves A and B both equal to the old value of B:

Note that the non-blocking type of procedural assignment was also used in the previous example for modelling a D-Type.

By convention non-blocking assignments (<=) are always used for modelling sequential logic within always_ff procedural blocks.

Failure to follow this convention may result in an individual block that appears to work but which fails to work when built onto a system because the operation of the simulated system depends on the order of evaluation of two statements which are effectively executed in parallel in the final (synthesized) system.

Simulation

The stimulus file for this HDL model is up_down_count_stim.sv and the corresponding SimVision command script is up_down_count.tcl.

Type the following at the unix command prompt:

    ncverilog  +gui +access+r  up_down_count_stim.sv  up_down_count.sv  +tcl+up_down_count.tcl  &

Then run the simulation ( ).


Alternative Behavioural Model of a Window Comparator

The following file, alt_window.sv, is a behavioural model of a digital window comparator:

Major Features

Procedural Blocks as Combinational Logic

An always_comb procedural block can be used to describe a combinational logic block provided that:

the values of all ouputs are defined for all possible combinations of inputs.

Event Control

The always_comb block is executed every time that there is any change in any of the inputs to the block.

In early versions of Verilog this would be written as always @(*) indicating that any change on any input can trigger the block. With always_comb, the @(*) event control is implied and should not be explicitly written.

Simulation

Type the following at the unix command prompt:

Then run the simulation ().

(note that in this example the file alt_window.sv contains a module name window rather than one named alt_window - this practice is generally to be avoided but it has meant that we can re-use the same stimulus file)


Behavioural Model of a Microprocessor

The microprocessor is described in the following SystemVerilog files:

Major Features

Hierarchy

Most complex behavioural models are constructed as a hierarchy of simpler behavioural models.

The Processor module instances the Control, Datapath and Memory modules. It is in fact merely a structural descrition of their interconnection:

The Datapath module instances the ALU module but also contains the behavioural description of the Accumulator:

Multiplexors using Continuous Assignment

The following construct describes a multiplexor which selects input1 if select is `1' and input0 if select is `0':

The Control module includes an address multiplexor based on this construct:

Procedural Blocks as Combinational Logic

An always_comb procedural block can be used to describe a combinational logic block provided that: the values of all ouputs are defined for all possible combinations of inputs.

The ALU module uses such a construct to evaluate result:

Note that including a default case covers the requirement to specify the value of the output (result) for all values of the input (func).

Tristate Bus

The processor includes a tristate bus, Data_bus, which may be driven from one of several (in this case only two) sources. The following statement describes a tristate buffer which drives an n-bit bus from an n-bit input drive when the enable signal is `1' (otherwise the bus is driven high impedence, `z', allowing it to be driven by another source):

The Datapath module may drive the Data_bus, locally known as data, from the Accumulator:

Note the inout declaration for data indicating that it is both an input and an output for the module.

The Memory module may also drive the Data_bus:

Constants & File Inclusion

The opcodes.sv file declares constants to be used in other modules:

The alu.sv file includes the opcodes.sv file and then uses the constants within the alu module:

Simulation

The stimulus file for this HDL model is system.sv and the corresponding SimVision command script is system.tcl.

Before simulating this design, first copy the stimulus file and the SimVision command script to a suitable directory and then create a sub directory called library into which you should copy the model files for the microprocessor. The appropiate simulation command is:

    ncverilog  +gui +access+r  -y library  +libext+.sv  +incdir+library  system.sv  +tcl+system.tcl  &
Notes:

Execute the simulation command and run the simulation ( ).


Additional Documentation

All SystemVerilog and Verilog documentation is available on-line via Cadence Help. Type the following at the unix command prompt in order to invoke Cadence Help:

On-line manuals of particular interest to this tutorial are:


Iain McNally

24-10-2024