Lecture 1-3 Introduction To Verilog HDL
Lecture 1-3 Introduction To Verilog HDL
Lecture 1-3 Introduction To Verilog HDL
T0 t3
T1 T2
q0 q1 q2 q3
clk T_FF T_FF T_FF T_FF
tff0 tff1 tff2 tff3
reset
4-bit Ripple carry counter
• Here ripple counter is top level block can be implemented by T_FFs. Each T_FFs can be made up
from negative edge triggered D flip flops(D_FF) and inverters
T_FF
reset qn qn+1
1 1 0
d q
1 0 0
clock
0 0 1
D_FF
0 1 0
0 0 1
reset
Modules
➢ Verilog provides the concept of a module.
➢ A module is the basic building block in Verilog.
➢ A module can be an element or a collection of lower-level design blocks.
➢ Elements are grouped into modules to provide common functionality that is used at many places
in the design.
➢ A module provides the necessary functionality to the higher-level block through its port interface
(inputs and outputs), but hides the internal implementation.
➢ A module is declared by the keyword module. A corresponding keyword endmodule must appear
at the end of the module definition.
Module example
module <module-name> (<module-terminal-list>) ;
…
i <module internals ...>
...
endmodule
Example
module T_FF (q, clock, reset) ;
….
<functionality of T-flipflop>
…..
endmodule
Level of abstraction in Verilog
➢ Verilog is both a behavioral and a structural language.
➢ Internals of each module can be defined at four levels of abstraction, depending on the needs of
the design.
• Behavioral or algorithmic level
➢ This is the highest level of abstraction provided by Verilog HDL.
➢ A module can be implemented in terms of the desired design algorithm without concern for the
hardware implementation details.
• Dataflow level
➢ At this level the module is designed by specifying the data flow.
➢ The designer is aware of how data flows between hardware registers and how the data is
processed in the design.
Levels of abstraction in Verilog
• Gate level
➢The module is implemented in terms of logic gates and interconnections between these
gates.
➢Design at this level is similar to describing a design in terms of a gate-level logic diagram.
• Switch level
➢This is the lowest level of abstraction provided by Verilog.
➢A module can be implemented in terms of switches, storage nodes, and the
interconnections between them.
➢Verilog allows the designer to mix and match all four levels of abstractions in a design.
➢In the digital design community, the term register transfer level (RTL) is frequently used
for a Verilog description that uses a combination of behavioral and dataflow constructs
Instances
➢ A module provides a template from which you can create actual objects.
➢ When a module is invoked, Verilog creates a unique object from the template.
➢ Each object has its own name, variables, parameters and I/O interface.
➢ The process of creating objects from a module template is called instantiation, and the objects
are called instances.
➢ In previous example, the top-level block creates four instances from the T-flipflop (T-FF) template.
Each T-FF instantiates a D-FF and an inverter gate.
Example Instances
/ / Define the top-level module called ripple carry
module ripple_carry_counter(q, clk, reset);
output [3:0]q;
input clk, reset;
//each instance is a copy of the module T_FF.
T_FF tff0(q[0],clk,reset);
T_FF tff1(q[1] ,q[0], reset);
T_FF tff2 (q[2] ,q[1], reset) ;
T_FF tff3(q[3] ,q[2], reset) ;
endmodule
Example
// Define the module T-FF. It instantiates a D-flipflop.
module T_FF(q, clk, reset) ;
output q;
input clk, reset;
wire d;
D-FF dff0 (q, d, clk, reset) ; / / Instantiate D-FF. Call it dff0.
not n1(d, q); / / not gate is a Verilog primitive.
endmodule
Example
// module D_FF with synchronous reset
module D_FF(q, d, clk, reset) ;
output q;
input d, clk, reset;
reg q;
always @(negedge clk)
if (reset)
q <= 1'b0 //evaluate to a logical 0
else
q <= d;
endmodule
Components of Simulation
➢ The functionality of the design block can be tested by applying stimulus and checking results.
➢ The stimulus block is also commonly called a test bench.
➢ Different test benches can be used to thoroughly test the design block.
➢ Stimulus block
➢ Two styles of stimulus application are possible.
➢ In the first style, the stimulus block instantiates the design block and directly drives the signals in
the design block.
➢ The second style of applying stimulus is to instantiate both the stimulus and design blocks in a
top-level dummy module. The stimulus block interacts with the design block only through the
interface.
Stimulus for ripple carry counter
module stimulus;
reg clk;
reg reset;
wire [3:0]q;
ripple-carry-counter rl(q, clk, reset);
initial
clk = 1'b0; //set clk to 0
always
#5 clk = ~clk; //toggle clk every 5 time units
Example Contd.
initial
begin
reset = 1'b1;
#15 reset = 1'b0;
#180 reset = 1'b1;
#10 reset = 1'b0;
#20 $finish; //terminate the simulation
end
// Monitor the outputs
initial
$monitor ($time, " Output q = %d" , q) ;
endmodule
Example Contd.
Output of stimulus
0 Output q = 0
20 Output q = 1
30 Output q = 2
40 Output q = 3
50 Output q = 4
60 Output q = 5
70 Output q = 6
80 Output q = 7
90 Output q = 8
…
…..
Conventions in Verilog
• Lexical Conventions
➢ Verilog contains a stream of tokens.
➢ Tokens can be comments, numbers, strings, identifiers, and keywords.
➢ Verilog HDL is a case-sensitive language.
➢ All keywords are in lowercase.
• Whitespace
➢ Blank spaces (\b) , tabs (\t) and newlines (\n) comprise the whitespace.
➢ Whitespace is ignored by Verilog except when it separates tokens.
➢ Whitespace is not ignored in strings.
Conventions in Verilog
• Comments
➢ Comments can be inserted in the code for readability and documentation.
➢ A one-line comment starts with "//".Verilog skips from that point to the end of line.
➢ A multiple-line comment starts with "/*“ and ends with "*/".
• Operators
➢ Operators are of three types, unary, binary, and ternary. (eg: ~, ^, ?:)
➢ Unary operators precede the operand.
➢ Binary operators appear between two operands. (eg: &, |)
➢ Ternary operators have two separate operators that separate three operands.
Example
assign a = ~b; // ~is a unary operator. b is the operand
assign a = b & c; // & is a binary operator. b and c are operands
assign a = b ? c : d; // ?: is a ternary operator. b, c and d are operands
?: conditional operator
• Number Specification
➢There are two types of number specification in Verilog: sized and unsized.
• Sized numbers
➢Sized numbers are represented as <size> ‘ <base format> <number>.
Example Sized Number
➢<size> is written only in decimal and specifies the number of bits in the number.
➢Legal base formats are decimal ('d or 'D), hexadecimal ('h or 'H), binary ('b or 'B)
and octal (‘o or ‘O)
➢Uppercase letters are legal for number specification only.
Example:
4'b1111 // This is a 4-bit binary number 15
12'habc // This is a 12-bit hexadecimal number
16'd255 // This is a 16-bit decimal number.
12’h101010111100 4’d15
Example unsized numbers
• Unsized numbers
➢Numbers that are specified without a <base format> specification are decimal
numbers by default.
➢Numbers that are written without a <size> specification have a default number
of bits that is simulator- and machine-specific (must be at least 32).
• 23456 // This is a 32-bit decimal number by default
• 'hc3 // This is a 32-bit hexadecimal number
• ‘o21 // This is a 32-bit octal number
• X or Z values
• Verilog has two symbols for unknown and high impedance values
• 12'h13x // This is a 12-bit hex number; 4 least significant bits unknown
• 6'hx //This is a 6-bit hex unknown number
• 32'bz //This is a 32-bit high impedance number
Negative numbers
- 4 ’b11 // 4-bit two’s complement of 0011 = 1101 = 4’hd
- 4’d7 // stored as 1001
- 5’ha // stored as 10110
Identifier and Keywords
• Identifier and Keywords
➢Keywords are special identifiers reserved to define the language constructs.
➢Keywords are in lowercase.
➢Identifiers are names given to objects so that they can be referenced in the
design.
➢Identifiers are made up of alphanumeric characters, the underscore ( _ ) and the
dollar sign ( $ ) and are case sensitive.
reg value; // reg is a keyword; value is an identifier
input clk; // input is a keyword, clk is an identifier
Underscore characters and question marks
• Underscore characters and question marks
➢An underscore character "-" is allowed anywhere in a number except the first
Character.
➢Underscore characters are allowed only to improve readability of numbers.
➢A question mark "?" is the Verilog HDL alternative for z in the context of
• Numbers
12'b1111_0000_1010 // Use of underline characters for readability
4'b10?? // Equivalent of a 4'b10zz
Data Type
• Value Set
• Verilog supports four values and eight strengths to model the
functionality of real
• hardware.
• Net a will continuously assume the value computed at the output of gate
gl, which is b & C.
b
gl a
c
• Integer
➢An integer is a general purpose register data type used for
manipulating quantities.
➢Integers are declared by the keyword integer
integer counter; // general purpose variable used as a counter.
initial
counter = -1; // A negative one is stored in the counter
• Real
➢Real number constants and real register data types are declared with
the keyword real
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 2.13
Time
• 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.
Memories
• Memories
➢In digital simulation, one often needs to model register files, RAMS, and ROMs
➢Memories are modeled in Verilog simply as an array of registers.
➢Each word can be one or more bits.
➢It is important to differentiate between n 1-bit registers and one n-bit register.
// Include the file header.v, which contains declarations in the main verilog
file design.v
'include header.v
...
...
<Verilog code in file design.v>
Modules and Ports
A module in Verilog consists of distinct parts:
✓Module Name,
✓Port List, Port Declarations (if ports present)
✓Parameters(optional),
✓Declaration of wires, regs and other variables
✓Data flow statement(assign)
✓Instantiation of lower level modules
✓always and initial blocks. All behavioral statements go in these blocks.
✓Tasks and functions
✓endmodule statement
Query?