Verilog Notes
Verilog Notes
Introduction to Verilog Thursday, October 11, 2001 9:39 pm Peter M. Nyasulu Thursday, October 11, 2001 9:39 pm 1 Peter M. Nyasulu
Introduction to Verilog Introduction to Verilog
Thursday, October 11, 2001 9:39 pm 2 Peter M. Nyasulu Thursday, October 11, 2001 9:39 pm 3 Peter M. Nyasulu
Introduction to Verilog Introduction to Verilog
4.5. Integer
4. Data Types Integers are general-purpose variables. For synthesois they are used mainly loops-indicies, parameters, and con-
stants. See“Parameter” on p. 5. They are of implicitly of type reg. However they store data as signed numbers
whereas explicitly declared reg types store them as unsigned. If they hold numbers which are not defined at compile
4.1. Value Set time, their size will default to 32-bits. If they hold constants, the synthesizer adjusts them to the minimum width
Verilog consists of only four basic values. Almost all Verilog data types store all these values: needed at compilation.
0 (logic zero, or false condition)
1 (logic one, or true condition)
Syntax Example 4 .4
x (unknown logic value) x and z have limited use for synthesis.
z (high impedance state) integer a; // single 32-bit integer
integer integer_variable_list;
assign b=63; // 63 defaults to a 7-bit variable.
... integer_constant ... ;
4.2. Wire
A wire represents a physical wire in a circuit and is used to connect gates or modules. The value of a wire can be
read, but not assigned to, in a function or block. See “Functions” on p. 19, and “Procedures: Always and Initial
Blocks” on p. 18. A wire does not store its value but must be driven by a continuous assignment statement or by con- 4.6. Supply0, Supply1
necting it to the output of a gate or module. Other specific types of wires include: Supply0 and supply1 define wires tied to logic 0 (ground) and logic 1 (power), respectively.
wand (wired-AND);:the value of a wand depend on logical AND of all the drivers connected to it.
wor (wired-OR);: the value of a wor depend on logical OR of all the drivers connected to it. Syntax Example 4 .5
tri (three-state;): all drivers connected to a tri must be z, except one (which determines the value of the tri).
supply0 logic_0_wires; supply0 my_gnd; // equivalent to a wire assigned 0
Syntax Example 4 .1 supply1 logic_1_wires; supply1 a, b;
wire c // simple wire
wire [msb:lsb] wire_variable_list;
wand d;
wand [msb:lsb] wand_variable_list;
assign d = a; // value of d is the logical AND of
wor [msb:lsb] wor_variable_list; 4.7. Time
assign d = b; // a and b
tri [msb:lsb] tri_variable_list; Time is a 64-bit quantity that can be used in conjunction with the $time system task to hold simulation time. Time is
wire [9:0] A; // a cable (vector) of 10 wires.
not supported for synthesis and hence is used only for simulation purposes.
Thursday, October 11, 2001 9:39 pm 4 Peter M. Nyasulu Thursday, October 11, 2001 9:39 pm 5 Peter M. Nyasulu
Introduction to Verilog Introduction to Verilog
Thursday, October 11, 2001 9:39 pm 6 Peter M. Nyasulu Thursday, October 11, 2001 9:39 pm 7 Peter M. Nyasulu
Introduction to Verilog Introduction to Verilog
Operators
6.1. Literals
Example 5 .9
x y Literals are constant-valued operands that can be used in Verilog expressions. The two common Verilog literals are:
assign a = (g) ? x : y; g
(cond) ? (result if cond true): 1 1 (a) String: A string literal is a one-dimensional array of characters enclosed in double quotes (“ “).
assign a = (inc = = 2) ? a+1 : a-1; (b) Numeric: constant numbers specified in binary, octal, decimal or hexadecimal.
(result if cond false) /* if (inc), a = a+1, else a = a-1 */
Number Syntax Example 6 .1
Thursday, October 11, 2001 9:39 pm 8 Peter M. Nyasulu Thursday, October 11, 2001 9:39 pm 9 Peter M. Nyasulu
Introduction to Verilog Introduction to Verilog
The instantiated module’s ports must be matched to those defined in the template. This is specified:
(i) by name, using a dot(.) “ .template_port_name (name_of_wire_connected_to_port)”.
or(ii) by position, placing the ports in exactly the same positions in the port lists of both the template and the instance.
Thursday, October 11, 2001 9:39 pm 10 Peter M. Nyasulu Thursday, October 11, 2001 9:39 pm 11 Peter M. Nyasulu
Introduction to Verilog Introduction to Verilog
c
8.4. Nonblocking (RTL) Assignments (see below for synthesis)
8. Behavioral Modeling RTL (nonblocking) assignments (<=), which follow each other in the code, are started in parallel. The right hand
side of nonblocking assignments is evaluated starting from the completion of the last blocking assignment or if none,
Verilog has four levels of modelling: the start of the procedure. The transfer to the left hand side is made according to the delays. An intra-assignment
1) The switch level which includes MOS transistors modelled as switches. This is not discussed here. delay in a non-blocking statement will not delay the start of any subsequent statement blocking or non-blocking.
2) The gate level. See “Gate-Level Modelling” on p. 3 However a normal delays will are cummulative and will delay the output.
3) The Data-Flow level. See Example 7 .4 on page 11 For synthesis
4) The Behavioral or procedural level described below. • One must not mix “<=” or “=” in the same procedure.
Verilog procedural statements are used to model a design at a higher level of abstraction than the other levels. They
• “<=” best mimics what physical flip-flops do; use it for “always @ (posedge clk ..) type procedures.
provide powerful ways of doing complex designs. However small changes n coding methods can cause large changes
in the hardware generated. Procedural statements can only be used in procedures. Verilog procedures are described • “=” best corresponds to what c/c++ code would do; use it for combinational procedures.
later in “Procedures: Always and Initial Blocks” on page 18,“Functions” on page 19, and “Tasks, Not Synthesizable” :
on page 21. Syntax Example 8 .3. For simulation
Non-Blocking initial
8.1. Procedural Assignments variable <= expression; begin
variable <= #Δt expression; #3 b <= a; /* grab a at t=0 Deliver b at t=3.
Procedural assignments are assignment statements used within Verilog procedures (always and initial blocks). Only
#Δt variable <= expression; #6 x <= b + c; // grab b+c at t=0, wait and assign x at t=6.
reg variables and integers (and their bit/part-selects and concatenations) can be placed left of the “=” in procedures.
x is unaffected by b’s change. */
The right hand side of the assignment is an expression which may use any of the operator types described in Sect. 5.
Example 8 .4. For synthesis X Y Z
8.2. Delay in Assignment (not for synthesis) always @( posedge clk) 1D 1D
In a delayed assignment Δt time units pass before the statement is executed and the left-hand assignment is made. C1 C1
begin
With intra-assignment delay, the right side is evaluated immediately but there is a delay of Δt before the result is Z<=Y; Y<=X; // shift register x y z
place in the left hand assignment. If another procedure changes a right-hand side signal during Δt, it does not effect 1D 1D
y<=x; z<=y; //also a shift register.
C1 C1
the output. Delays are not supported by synthesis tools.
Syntax for Procedural Assignment Example 8 .1 Example 8 .3. Use <= to transform a variable into itself.
variable = expression reg G[7:0];
Delayed assignment reg [6:0] sum; reg h, ziltch;
sum[7] = b[7] ^ c[7]; // execute now. always @( posedge clk)
#Δt variable = expression; G <= { G[6:0], G[7]}; // End around rotate 8-bit register.
Intra-assignment delay ziltch = #15 ckz&h; /* ckz&a evaluated now; ziltch changed
variable = #Δt expression; after 15 time units. */
#10 hat = b&c; /* 10 units after ziltch changes, b&c is
evaluated and hat changes. */ The following example shows interactions between blocking and non-blocking for simulation. Do not mix the two
types in one procedure for synthesis.
Thursday, October 11, 2001 9:39 pm 12 Peter M. Nyasulu Thursday, October 11, 2001 9:39 pm 13 Peter M. Nyasulu
Introduction to Verilog Introduction to Verilog
Thursday, October 11, 2001 9:39 pm 14 Peter M. Nyasulu Thursday, October 11, 2001 9:39 pm 15 Peter M. Nyasulu
Introduction to Verilog Introduction to Verilog
8.12. case
The case statement allows a multipath branch based on comparing the expression with a list of case choices. 9. Timing Controls
Statements in the default block executes when none of the case choice comparisons are true (similar to the else block
in the if ... else if ... else). If no comparisons , including delault, are true, synthesizers will generate unwanted latches. 9.1. Delay Control, Not synthesizable
Good practice says to make a habit of puting in a default whether you need it or not.
This specifies the delay time units before a statement is executed during simulation. A delay time of zero can also be
If the defaults are dont cares, define them as ‘x’ and the logic minimizer will treat them as don’t cares.
specified to force the statement to the end of the list of statements to be evaluated at the current simulation time. See
Case choices may be a simple constant or expression, or a comma-separated list of same.
also “Intra-Assignment Delay, Not synthesizable” on p. 17
Thursday, October 11, 2001 9:39 pm 16 Peter M. Nyasulu Thursday, October 11, 2001 9:39 pm 17 Peter M. Nyasulu
Introduction to Verilog Introduction to Verilog
Syntax Example 10 .2
initial
11.4. Function Rules
inital
begin The following are some of the general rules for functions:
begin
... statements ... - Functions must contain at least one input argument.
clr = 0; // variables initialized at
end - Functions cannot contain an inout or output declaration.
clk = 1; // beginning of the simulation
- Functions cannot contain time controlled statements (#, @, wait).
end
- Functions cannot enable tasks.
- Functions must contain a statement that assigns the return value to the implicit function name register.
inital // specify simulation waveforms
begin
a = 2’b00; // at time = 0, a = 00
#50 a = 2’b01; // at time = 50, a = 01
#50 a = 2’b10; // at time = 100, a = 10
end
Thursday, October 11, 2001 9:39 pm 18 Peter M. Nyasulu Thursday, October 11, 2001 9:39 pm 19 Peter M. Nyasulu
Introduction to Verilog Introduction to Verilog
Thursday, October 11, 2001 9:39 pm 20 Peter M. Nyasulu Thursday, October 11, 2001 9:39 pm 21 Peter M. Nyasulu
Introduction to Verilog Introduction to Verilog
13.3. Multiplexers
13. Component Inference A multiplexer is inferred by assigning a variable to different variables/values in each branch of an if or case state-
ment. You can avoid specifying each and every possible branch by using the else and default branches. Note that a
latch will be inferred if a variable is not assigned to for all the possible branch conditions.
13.1. Latches To improve readability of your code, use the case statement to model large multiplexers.
A latch is inferred (put into the synthesized circuit) if a variable, or one of its bits, is not assigned in all branch of an
if statement. A latch is also inferred in a case statement if a variable is assigned to in only some of the branches.
Syntax Example 13 .5
To improve code readability, use the if statement to synthesize a latch because it is difficult to explicitly specify
sel
the latch enable signal using a case statement.
See Sections 8.9 and 8.10 for if (sel == 1)
While in theory, a proper reset should be infered from the Verilog code shown, Synopsys will not do a proper job
if ... else if ... else and case statements y = a; a
without adding the //Synopsys comments shown. y
else
b
y = b;
Syntax Example 13 .1
See Sect. 8.11. and Sect. 8.12. for always @(clk,d); begin d D
Q q case (sel) sel[1:0]
if ... else if ... else and case statements if (clk)
2’b00: y = a; a
q <=d;
clk EN 2’b01: y = b; b
//Synopsys statement end
2’b10: y = c; y
These are treated as comments by all sim- c
Example 13 .2 default: y = d;
ulators. For synthesis using Synopsys, d
//Synopsys async_set_reset “rst” d endcase
they direct the synthesizer as to what par- D
q
Q
ticular inference is wanted. always @(clk or rst or d); begin
if (rst) q<=0; clk EN
Thursday, October 11, 2001 9:39 pm 22 Peter M. Nyasulu Thursday, October 11, 2001 9:39 pm 23 Peter M. Nyasulu
Introduction to Verilog Introduction to Verilog
14.1.
Thursday, October 11, 2001 9:39 pm 24 Peter M. Nyasulu Thursday, October 11, 2001 9:39 pm 25 Peter M. Nyasulu
Introduction to Verilog Introduction to Verilog
Syntax Example 15 .3
16.4. $deposit
$deposit sets a net to a particular value.
`include file_name; module x;
'include “dclr.v”; // contents of file “dclr,v” are put here Syntax Example 16 .2
16.6. $list
$list (hierarchical_name) lists line-numbered source code of the named module, task, function or named-block.
Thursday, October 11, 2001 9:39 pm 26 Peter M. Nyasulu Thursday, October 11, 2001 9:39 pm 27 Peter M. Nyasulu
Introduction to Verilog Introduction to Verilog
Syntax Example 16 .5
Thursday, October 11, 2001 9:39 pm 28 Peter M. Nyasulu Thursday, October 11, 2001 9:39 pm 29 Peter M. Nyasulu
Introduction to Verilog Introduction to Verilog
Thursday, October 11, 2001 9:39 pm 30 Peter M. Nyasulu Thursday, October 11, 2001 9:39 pm 31 Peter M. Nyasulu
Introduction to Verilog
18. Memorys
18.1. Two-Dimensional Arrays
Two dimensional arrays can be declared and accessed by word. To get at a bit(s) one must equate the output to a
register or wire and select the bits from this new variable. See Example 18 .1
18.1.1 Initializing Memory From a File
The command $readmemb will read a file of binary numbers into the array. The data file consists of addresses and
data. An address written in hex as @hhh...and indicates the address of the first word in a block of data. It is followed
by binary data words separated by blanks. Legal binary bits are “0 1 x X z Z _”. Data not included in the file will be
given xxx... values. The data may be given in noncontiguous blocks if an address proceeds each block. If no initial
address is given, @000 is assumed for the first data word. Comments are allowed in data files.
If start_addr is given the memory array will be filled starting at that address and continue until finish_addr (or the
end of the array) is reached. One must have start address ≤ @hhh..., the initial address in the file.
The command $readmemh is similar except the data must contain hexadecimal numbers.
Syntax Example 18 .1
reg [wordsize:0] array [0:arraysize] reg [7:0] memry [0:31]; // 32 byte memory.
wire [7:0] memwrd;
readmemb(“file_name”, array_name); wire x;
readmemb(“file_name”, array_name, initial begin
start_addr); // Initialize memory contents from file.
readmemb(“file_name”, array_name, $readmemb(“init.dat”, memry, 8);
start_addr, finish_addrs); // words 8 and 9 are not in the file and will default to x.
end
readmemh(“file_name”, array_name); ---
// start_addr and finish addr are optional // Extract last word in memory.
assign memwrd= memry[31];
// Extract most sig bit in word 31
assign x= memwrd[7];