2.Basic Constructs
2.Basic Constructs
Concepts
CHAPTER -II
BASIC CONSTRUCTS
I. Syntax
• Verilog HDL is a case-sensitive language. All
keywords are in lowercase.
1. Whitespace
2. Comments
3. Operators
4. Number Specification
5. Strings
6. Identifiers and Keywords
1. Whitespace
• Blank spaces (\b) , tabs (\t) and newlines (\n)
comprise the whitespace.
• Not ignored in strings
2. Comments
Two types:
1. single-line comment.
2. multi-line comment.
Ex: 1. // single line comment
2. /* this is a multiple line comment */
X or Z values
• Verilog has two symbols for unknown and high impedance values.
• These values are very important for modeling real circuits.
• An unknown value is denoted by an X. A high impedance value is denoted
by z
12'h13x // This is a 12-bit hex number; 4 least significant bits
unknown
6'hx // This is a 6-bit hex number
32'bz // This is a 32-bit high impedance number
Negative numbers:
• Negative numbers can be specified by putting a minus
sign before the size for a constant number.
• Size constants are always positive.
• It is illegal to have a minus sign between <base
format> and <number>.
-6’d3[Correct] 4’d-2[Wrong]
Underscore Character:
Ex: 12’b1111_0000_1010
Question Mark:
‘?’ is the same as ‘z’ (only regarding numbers)
EX: 4’b10?? //the same as 4’b10zz
5. Strings
• A string is a sequence of characters that are enclosed by
double quotes.
• Group of chars within double quotes.
• Do not embed newlines.
• It can’t be on multiple lines.
• You can use escaped characters (such as \t and \n) in
strings.
Examples:
“Hello Verilog World” // is a string
“a /b ” //is a string
6. Identifiers & Keywords
• Keywords are special identifiers reserved to
define the language constructs.
• Special identifiers , language constructs.
• Lowercase.
• Keywords != identifiers.
EXAMPLE:
• reg [3:0] v; // A 4-bit vector reg
• reg [1:8] m,n; // Two 8-bit reg’s
3.Vectors
• Nets or reg data types can be declared as vectors
(multiple bit widths). If bit width is not specified,
the default is scalar (l-bit).
Example:
wire a; // scalar net variable,
default
wire [7:0] bus; // 8-bit bus
wire [31:0] busA,busB,busC; // 3 buses of
32-bit width.
reg clock; // scalar register,
default
4.Integer
• Integers are declared by the keyword integer.
• The default width for an integer is the host-machine
word size, which is implementation specific but is at
least 32 bits.
• Registers declared as data type reg store values as
unsigned quantities, whereas integers store values as
signed quantities.
Example:
integer counter; // general purpose variable
used as a counter.
initial
counter = -1; // A negative one is stored in
the counter
5.Real
• Real number constants and real register data types are decIared
with the keyword real.
• They can be specified in decimal notation (e.g., 3.14) or in
scientific notation (e.g., 3e6, which is 3 X 106 ).
• Real numbers cannot have a range declaration, and their default
value is 0.
Example:
real delta; // Define a real variable called delta
initial begin
delta = 4e10; // delta is assigned in
scientific notation
delta = 2.13; // delta is assigned a value
end
integer i; // Define an integer i
initial i = delta; // i gets the value 2 (rounded value of 2.13)
6.Time
• Verilog simulation is done with respect to simulation
time
• A special time register data type is used in Verilog to
store simulation time.
• A time variable is declared with the keyword time.
• The width for time register data types is
implementation specific but is at least 64 bits
• Can use $time system task
• Simulation time is measured in terms of simulation
seconds. The unit is denoted by S, the same as real
time.
EX: time chng_hist[1:1000] // an array of 1000 time
values
7.Arrays
• Arrays are allowed in Verilog for reg, integer, time,
and vector register data types.
• Arrays are not allowed for real variables.
• Multidimensional arrays are not permitted in
Verilog.
Note: It is important not to confuse arrays with net
or register vectors. A vector is a single element that
is n-bits wide. On the other hand, arrays are
multiple elements that are l-bit or n-bits wide
Syntax:
<data_type> <var_name>[start_index : end_index];
Examples:
• integer count[0:7]; // An array of 8 count variables
• reg bool[31:0]; // Array of 32 one-bit boolean register
variables
• time chk_point[1:100]; // Array of 100 time checkpoint
variables
• reg [4:0] port_id[0:7]; // Array of 8 port_ids; each port_id is 5
bits wide
• wire [7:0] w_array2 [5:0]; // Declare an array of 8 bit vector
wire
8.Memories
• Memories are modeled in Verilog simply as an
array of registers.
one-8-bit memory 8-one bit memory
reg [7:0]mem;
reg mem[0:7];
Examples:
• $finish, $stop
1.‘define
• The 'define directive is used to define text macros in
Verilog
2. ‘include
• The ‘include directive allows you to
include entire contents of a Verilog source
file in another Verilog file during
compilation
• This directive is typically used to include
header files, which typically contain
global or commonly used definitions
3 . ‘timescale
• The specifies the unit of measurement for
times and delays. The specifies the
precision to which the delays are rounded
off during simulation
`include compiler directive.
Syntax:
`include “<filename>”
EX: `include “module_dut.v”
`timescale compiler directive.
Syntax:
`timescale time_units / precision_units
EX : `timescale 1ns/1ps
`define
Syntax:
`define <macro_name> <macro_text>
EX: `define A_WIDTH 4
`define D_WIDTH 32
Conditional Compilation
Directives:
• Verilog has following conditional compiler directives.
• `ifdef `else `elsif and `endif
• `ifndef, `else, `elsif, and `endif
`ifdef Syntax :
`ifdef <text_macro_identifier>
ifdef_group_of_lines
`elsif <text_macro_identifier>
elsif_group_of_lines
`else
else_group_of_lines
`endif
• The `ifdef compiler directive checks for the definition of a
text_macro_name.
• If the text_macro_name is defined, then the lines following the
`ifdef directive are included.
• If the text_macro_name is not defined and an `else directive
exists, then this source is
compiled.
• Ex: module switches();
initial
begin
`ifdef TYPE_1
`else`if TYPE_2
`define and_gate
output reg a;
input b,c;
initial begin
`ifdef and_gate // use `ifndef also here
assign a = b & c;
$display("and output a= %0b",a);
`else
assign a = b ^ c;
$display("xor output a= %0b",a);
`endif
Module Parameters
1. parameter
2. localparam
Synta parameter
x: list_of_assignments;
Examples:
parameter cycle = 20;
parameter prop_del = 3;
parameter setup = cycle/2 -
prop_del;
parameter width = 8;
parameter x_word = 16’bx;
Module Parameters
... ...
… ...
endmodule endmodule
Contd.
module
mod1(out,in1,in2); module top;
... ...
parameter cycle = 8; mod1 #(6,3.1,16’bx) dut
parameter real_constant (out,in1,in2);
= 2.039; ...
parameter x_word = endmodule
16’bx;
...
endmodule
VII. Synthesizable and Non-
Synthesizable Constructs
• If a piece of HDL can be converted to a
design with logic gates by a synthesis tool,
then it is synthesizable.
• If a piece of HDL is used for creating
reference events, trackers, displays, file
dumping, etc., and the synthesis tool cannot
infer a digital circuit, then it is non-
synthesizable
Specify Blocks
• The specify blocks describe module timing checks and
pin to pin timing in Verilog.
• The timing checks include predefined systems tasks like
$setup, $hold, $width, $period, $skew, $recovery,
$setuphold
specify
specparam S_VALUE = 3;
specparam H_VALUE = 10;
$setup(d,posedge clk,S_VALUE);
$hold(posedge clk,d,H_VALUE);
endspecify
endmodule
THANK YOU