Unit-II Verilog HDL
Unit-II Verilog HDL
PROGRAMMING IN HDL(SEC1406)
MENTORS
Dr.T.RAVI
Dr.T.VINO
Department of ECE
Sathyabama Institute of Science and Technology
SYLLABUS
Course Outcome
After the completion of course student will be able to
VHDL
V H SIC Very High Speed Integrated Circuit
Hardware Description Language
Verilog HDL
Verilog HDL is a Hardware Description Language, that
can be used to model a digital system at many levels of
abstraction ranging from the algorithmic level to
the gate-level to the switch level
Facts About Verilog HDL
We use Verilog for FPGA programming
Verilog is more popular in industry
They offer similar features like VHDL
History of Verilog
In 1980s, originally developed by Gateway Design Automation
In 1990, was put in public domain
In 1995, adopted as an IEEE standard 1364-1995
In 2001, an enhanced version, Verilog 2001
Functions of Verilog
Design entry, like schematic
Simulation and verification of Design
Verilog HDL
Synthesis is the process of translating a design description to another
level of abstraction, i.e, from behaviour to structure.
abstraction:
Transistor/Switch Level Modeling. LOW LEVEL
Gate Level Modeling.
Data Flow Modeling.
Behavioral or algorithmic Modeling. HIGH LEVEL
Verilog HDL – Major Capabilities
Primitive logic gates- and, or and nand are built-in into the
language
Flexibility of creating Used Defined Primitives (UDF)
Switch level modeling primitive gates, such as pmos and
nmos are built-in into the language.
Three style of modeling
Two data types – Net data type and Register data type
Hierarchical design – up to any level, using module
instantiation
Design – switch level to algorithmic level
VHDL - Verilog HDL
Module
Describes the functionality of the design
States the input and output ports
Example: A Computer
Functionality: Perform user defined computations
I/O Ports: Keyboard, Mouse, Monitor, Printer
Basic unit --Module
module module_name (port_name);
port declaration
data type declaration
module functionality or structure
endmodule
Module Example (HalfAdder)
General definition
module HalfAdder (A, B, Sum Carry);
module module_name ( port_list );
input A, B;
port declarations;
output Sum, Carry;
…
assign Sum = A ^ B;
variable declaration; //^ denotes XOR
… assign Carry = A & B;
description of behavior // & denotes AND
endmodule endmodule
Verilog HDL
Example of Adder
A Full Adder
module name
module adder (carry, sum, a, b, cin); b
a
U0 sum
output carry, sum; I/O pins
cin
input a, b, cin; a
w0
wire w0, w1, w2; u0(su I/O pin declaration
U1
xor m, a, b, cin); ba
U2
w1 U4 carry
and u1(w0, a, b); cin
b
and u2(w1, b, cin); cin
U3 w2
<##
A Simple Verilog Example
// A simple example comment line
module gate1 (a,b,c); module name
input a,b; port list
port declarations
output c; gate1
and (c,a,b); a
c
endmodule end module
b
Modules are the basic building blocks in Verilog.
A logic circuit module, Its ports: inputs and outputs
a g f
b
F = AB + B'C
h i
//
// Data
Data Flow
Flow Modeling
Modeling
module
module ex1
ex1 (a,b,c,f);
(a,b,c,f);
input
input a,b,c;
a,b,c; output
output f;
f;
assign
assign ff == (a&b)
(a&b) || (~b&c);
(~b&c);
endmodule
endmodule
or (f,g,i);
endmodule
Behavioral Styles
a
Black Box out
b 2x1 MUX
sel
Modeling in Verilog HDL (Ex:Full Adder)
// Full adder Data flow Modeling // Full adder Structural Modeling // Full adder Behavioral Modeling
module fulladd (a, b, c, sum, carry); module fulladd (a, b, c, sum, carry); module fulladd (a, b, c, sum, carry);
input a; input a; input a;
input b; input b; input b;
input c; input c; input c;
output sum; output sum; output sum;
output carry; output carry; output carry;
assign sum = a^b^c; wire p,q,r; reg sum,carry;
assign carry = (a & b) | (b & c) | (c & a); xor reg p,q,r;
endmodule x1(sum,a,b,c); always @ (a or b or c) begin
and sum = (a^b)^c;
a1(p,a,b), p=a & b;
x1 a2(q,b,c), q=b & c;
a3(r,a,c); r=a & c;
or carry=(p | q) | r;
o1(carry,p,q,r); end
rr
a3 endmodule endmodule
a1 p o1
a2 qq
Data flow Modeling in // Full adder - VHDL
library IEEE;
Verilog HDL & VHDL use IEEE.STD_LOGIC_1164.ALL;
(Ex:Full Adder) use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
// Full adder Data flow – Verilog HDL entity fadd_dataflow is
module fulladd (a, b, c, sum, carry); Port ( a : in std_logic;
input a; b : in std_logic;
input b; c : in std_logic;
sum : out std_logic;
input c;
carry : out std_logic);
output sum;
end fadd_dataflow;
output carry; architecture dataflow of fadd_dataflow is
assign sum = a^b^c; signal p,q,r:std_logic;
assign carry = (a & b) | (b & c) | (c & a); begin
endmodule p<= a and b;
q<= b and c;
x1 r<= c and a;
sum<= ((a xor b) xor c);
carry<= p or q or r;
a3 rr end dataflow;
a1 p o1
a2 qq
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
Structural Modeling in use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
Verilog HDL & VHDL entity fadd_structural is
Port ( a : in std_logic;
(Ex:Full Adder) b : in std_logic;
c : in std_logic;
// Full adder – Verilog HDL sum : out std_logic;
carry : out std_logic);
module fulladd (a, b, c, sum, carry); end fadd_structural;
input a; architecture structural of fadd_structural is
input b; component xor2
port(a,b:in std_logic;
input c;
z:out std_logic);
output sum; end component;
output carry; component and2
wire p,q,r,s; port(a,b:in std_logic;
z:out std_logic);
xor end component;
x1(p,a,b), component or3
x2(sum,p,c); port(a,b,c:in std_logic;
z:out std_logic);
and end component;
a1(q,a,b), signal p,q,r,s:std_logic;
a2(r,b,c), begin
x1: xor2 port map (a,b,p);
a3(s,a,c);
x2: xor2 port map (p,c,sum);
or a1: and2 port map (a,b,q);
o1(carry,q,r,s); a2: and2 port map (b,c,r);
endmodule a3: and2 port map (c,a,s);
o1: or3 port map (q,r,s,carry);
end structural;
library IEEE;
Behavioral Modeling in use IEEE.STD_LOGIC_1164.ALL;
Verilog HDL & VHDL use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
(Ex:Full Adder) entity fadd_behv is
Port ( a : in std_logic;
b : in std_logic;
// Full adder Behavioral Modeling
module fulladd (a, b, c, sum, carry); c : in std_logic;
input a; sum : out std_logic;
input b; carry : out std_logic);
input c; end fadd_behv;
output sum; architecture Behavioral of fadd_behv is
output carry; begin
reg sum,carry; process(a,b,c)
reg p,q,r; variable p,q,r:std_logic;
always @ (a or b or c) begin begin
sum = (a^b)^c; p:= a and b;
p=a & b; q:= b and c;
q=b & c;
r:= c and a;
r=a & c;
sum<= a xor b xor c;
carry=(p | q) | r;
end carry<= p or q or r;
endmodule end process;
end Behavioral;
Delays
Dataflow Modeling
Uses continuous assignment statement (Concurrent Statements)
Format: assign [ delay ] net = expression;
Example: assign sum = a ^ b;
Timescale
`timescale 1ns/100ps
1 Time unit = 1 ns
Time precision is 100ps (0.1 ns)
10.512ns is interpreted as 10.5ns
Delays (Cont….)
Dataflow Modeling
Example:
`timescale 1ns/100ps
module HalfAdder (A, B, Sum, Carry);
input A, B;
output Sum, Carry;
assign #3 Sum = A ^ B;
assign #6 Carry = A & B;
endmodule
Behavioral Modeling
Example:
Sequential Block: All statements within the block are executed sequentially
When is it executed?
Occurrence of an event in the sensitivity list
Event: Change in the logical value
Intra-Assignment Delay
Example:
Sum = A ^ B;
Carry = #2 A & B;
Delayed assignment
Procedural Constructs
initial
begin
Top = 3’b001;
#2 Top = 3’b011;
end
System Tasks and Functions
$<identifier>
Sign‘$’ denotes Verilog system tasks and functions.
A task can return zero or more values. A function is like a task except that it can
return only one value.
A number of system tasks and functions
are available to perform different operations such as
• Display tasks
– $display : Displays the entire list at the time when statement is encountered
– $monitor : Whenever there is a change in any argument, displays the entire list at end of time
step
• Simulation Control Task
– $finish : makes the simulator to exit
– $stop : suspends the simulation
• Time -system function
– $time: gives the simulation
Compiler Directives
• Identifiers that starts with (`) (backquote) character are called compiler directives
Compiler directives remain active until they are overridden or deactivated.
Verilog Standard compiler directives are
`define, `undef
`ifdef, `else, `endif
`include
`resetall
`timescale
`celldefine, `endcelldefine
Compiler Directives
The `resetall compiler directive resets all the compiler directives to
their default values (only if there is a default value).
`define s0 2’b00
`include “lib.v”
`timescale 10ns/100ps
`resetall
Compiler Directives
The `define compiler directive provides a simple text- substitution
facility.
• Syntax: ‘define <macro_name> <text_string>
• <text_string> will substitute <macro_name> at compile time.
`ifdef WINDOWS
Parameter Word = 16;
`else
Parameter Word = 32;
`endif
Compiler Directives
‘timescale
`timescale time unit / time precision
Example:
module m1(…);
…
`timescale 100ns/1ns;
…
Value Set
Verilog consists of only four basic values. Almost all verilog data
types store all these values:
• Integer
• Real
• String
Integers:
Reals: String:
Simple decimal
32 is Decimal Notation Sequence of characters
decimal 32 2.0 with in double quotes.
-15 is
decimal -15 5.678 “INTERNAL ERROR”
Net type:
• It represents physical connection between structural
elements.
• Net default value is z
Register type:
• It represents a data storage element.
• Register type default value of x.
Nets and Registers
Nets: nets are continuously driven by the devices that drive them.
wire, wor, wand, ...
- example : wire p; //scalar type
• reg
• integer
• time
• real
• realtime
Reg Register
• A value in a reg register is unsigned number.
• Example:
reg sum, carry, p,q; //one bit register
reg [3:0] Q; // Q is four bit register
Memories:
Memory is an array of registers.
reg[0:3] mymem [0:63] // an array of 64, 4 bit register
Registers
Integer Register
•An integer register contains integer values. It can be used as a general purpose
register.
•An integer cannot be accessed as a bit-vector.
•The size of an integer variable is 32 bits.
Integer a,b,c; // three integer register
integer p[0:3]; //An array of four register
Time Register:
•A time register is used to store and manipulate time values.
time events [0:31]; // Array of time values
time currtime // holds one time value
PARAMETERS
a = 4’b1010;
b = 4’b1100;
c = a ^ b;
a = 4’b1010;
b = 2’b11;
shift, Conditional Operator
Concatenation is the operation of joining bits from smaller expressions to form larger
expressions.
{exp1, exp2, …. expN}