Introduction To Verilog
Introduction To Verilog
Contents :
Introduction to Verilog
Basic Naming Convention
Verilog operators.
Data Types.
Assignment statements.
Control statements.
Behavioral Modeling in Verilog HDL.
Combinational logic design using Verilog.
Introduction to Verilog
Verilog is Hardware description language(HDL).
It describes digital system like MP, FF, Registers, Memory etc.
HDL descriptions provide technology-independent documentation of
a design and its functionality.
Design style :
Bottom-up -- Each design is performed at gate level
Top Down -- Desired style of all designers- early testing is done.
Abstraction level of Verilog
1. Behavioral Level.
2. Register-Transfer Level.
3. Gate Level.
Behavioral Level
3. Simulate the design by using a Verilog HDL simulator. Verify that the
description is correct.
Always
Synthesis
inst1
inst2
inst3
g
p in
ap
m
Place and
Route
clb 1
clb 2
Logic Values
0: zero, logic low, false, ground
X: unknown
Example: A Computer
Functionality: Perform user defined computations.
I/O Ports: Keyboard, Mouse, Monitor, Printer.
Modules
The principal design entity
Operators:
Operators
{} concatenation ~ bit-wise NOT
+ - * / arithmetic & bit-wise AND
% modulus | bit-wise OR
> >= < <= relational ^ bit-wise XOR
^~ ~^ bit-wise XNOR
! logical NOT
& reduction AND
&& logical AND
| reduction OR
|| logical OR ~& reduction NAND
== logical equality ~| reduction NOR
!= logical inequality ^ reduction XOR
?: conditional ~^ ^~ reduction XNOR
<< shift left
>> shift right
Operator Precedence
[ ] bit-select or part-select >, >=, <, <= relational
( ) parentheses ==, != logical equality
!, ~ logical and bit-wise & bit-wise AND
negation ^, ^~, ~^
&, |, ~&, ~|, ^, ~^, ^~ bit-wise XOR and XNOR
reduction operators | bit-wise OR
+, - unary arithmetic && logical AND
{ } concatenation || logical OR
*, /, % arithmetic ?: conditional
+, - arithmetic
<<, >> shift
Lexical Conventions
Comments
// Single line comment
/* Another single line comment */
/* Begins multi-line (block) comment
All text within is ignored
Line below ends multi-line comment
*/
Number
decimal, hex, octal, binary
unsized decimal form.
size base form.
include underlines, +,-
String
" Enclose between quotes on a single line"
Lexical Conventions (cont.)
Identifier
A ... Z
a ... z
0 ... 9
Underscore and dollar sign $
Declaring a register
reg [<range>] <reg_name> [<reg_name>*];
Declaring memory
reg [<range>] <memory_name> [<start_addr> : <end_addr>];
Examples
reg r; // 1-bit reg variable
wire w1, w2; // 2 1-bit wire variable
reg [7:0] vreg; // 8-bit register
reg [7:0] memory [0:1023]; a 1 KB memory
Ports and Data Types
Correct data types for ports
Module
net
inout
net
Data types
Net
physical wire between devices
the default data type.
used in structural modeling and continuous assignment.
types of nets
wire, tri : default
Simpler than VHDL
wor, trior : wire-ORed
wand, triand : wire-ANDed Only Syntactical
Difference
trireg : with capacitive storage
tri1 : pull high
tri0 ; pull low
supply1 ; power
supply0 ; ground
A Full-Adder
module add (co, s, a, b, c)
input a, b ,c ;
output co, s ;
xor (n1, a, b) ;
xor (s, n1, c) ;
Simpler than VHDL
nand (n2, a, b) ;
nand (n3,n1, c) ; Only Syntactical
nand (co, n3,n2) ; Difference
endmodule
Verilog Primitives
Basic logic gates only
and
or
not
buf
xor
nand
nor
xnor
bufif1, bufif0
notif1, notif0
Primitive Pins Are Expandable
One output and variable number of inputs
nand (y, in1, in2) ;
Syntax :
assign(strength, strength) #(delay) net = expression;
Continuous Assignments
Describe combinational logic
Operands + operators
Drive values to a net
assign out = a&b ; // and gate
assign eq = (a==b) ; // comparator
wire #10 inv = ~in ; // inverter with delay
wire [7:0] c = a+b ; // 8-bit adder
Avoid logic loops
assign a = b + a ;
asynchronous design
Logical and Conditional Operators
Logical, bit-wise and unary operators
a = 1011; b = 0010
logical bit-wise unary
a || b = 1 a | b = 1011 |a = 1
a && b = 1 a &b = 0010 &a = 0
Conditional operator
assign z = ({s1,s0} == 2'b00) ? IA :
({s1,s0} == 2'b01) ? IB :
({s1,s0} == 2'b10) ? IC :
({s1,s0} == 2'b11) ? ID :
1'bx ;
Syntax :
Case (<case_expression>)
<case1> : <Procedural statement 1>
<case2> : < Procedural statement 2>
..
<casen> : < Procedural statement n>
Default : <Procedural statement>
endcase
Case Statement
Case Statement
Example 1:
case (X)
2b00: Y = A + B;
2b01: Y = A B;
2b10: Y = A / B;
endcase
casex and casez Statement
Variants of case Statements:
casez and casex
forever
repeat
while
for
Forever Loop :
Executes Continuously, loop never ends.
Normally it is used in initial blocks.
Careful in using forever loop, if no timing
construct is present then simulation could hang.
syntax : forever <statement>
Looping statements
repeat statement :
Executes statement fixed number of times
syntax : repeat (<number>) <statement>
Module initial_example();
Reg clk,reset,enable,data;
initial begin
clk = 0;
Reset= 0;
Enable = 0;
Data = 0;
end
endmodule
Procedural Assignment Statements.
Procedural assignment statement assign values to reg, integer, real or
time variables.
It can not assign values to nets(wire data types).
registers(reg data type) can be assigned the value of net(wire),
constant, another register or specific value.
Syntax:
variable_lvalue = [timing_control] expression
variable_lvalue <= [timing_control] expression
[timing_control] variable_lvalue = expression
[timing_control] variable_lvalue <= expression
Behavioral Modeling
Can use only reg data type (within initial and
always constructs)
Cannot use wire data type
Compiler Directives
`define (Similar to #define in C) used to define global
parameter
Example:
`define BUS_WIDTH 16
reg [ `BUS_WIDTH - 1 : 0 ] System_Bus;
`undef Removes the previously defined directive
Example:
`define BUS_WIDTH 16
reg [ `BUS_WIDTH - 1 : 0 ] System_Bus;
`undef BUS_WIDTH
Compiler Directives (cont.)
Time
$time: gives the simulation
Timing Control
Delay Timing Control.
Event Timing control.
reg x, y;
integer count;
#25 y <= ~x; // at time 25
#15 count <= count + 1; // at time 40
Timing Control
always
wait (count_enable) count = count 1 ;
always
wait (count_enable) #10 count = count 1 ;
Combinational Logic Design using Verilog HDL .
Half Adder
Full Adder
Combinational Logic Design using Verilog HDL .
endmodule
Tri-State buffer (uses Cont. assignment statement)
Module tri_buff();
reg data_in, enable;
wire Bout;
assign Bout =(enable) ? data_in:1bZ; Cont. assignment statement
//Test Bench
initial begin
$montor(Enable=%b, D = %b, BOUT=%B, enable,data_in,Bout)
#1 enable =0;
#1 data_in =1;
#1 enable =1;
#1 data_in =0;
#1 enable =0;
#1 $finish;
end
endmodule
Tri-State buffer (uses Cont. assignment statement with delay)
Module tri_buff();
reg data_in, enable;
wire Bout;
assign #(1:2:3) Bout =(enable) ? data_in:1bZ; Cont. assignment
statement
//Test bench
initial begin
$montor(Enable=%b, D = %b, BOUT=%B, enable,data_in,Bout)
#10 enable =0;
#10 data_in =1;
#10 enable =1; Propagation Delay: only one delay for
#10 data_in =0; all transaction may be specified.
#10 enable =0; A Min:typical:Max dalay range may be
#10 $finish; specified.
end
endmodule
4-Bit Adder (Beh. Modeling)
Module adder(a,b,sum,carry);
input [3:0] a, b;
output[3:0] sum;
output carry;
reg [3:0] sum;
reg carry;
always@(a or b)
begin
{carry,sum}=a+b;
end
endmodule;
Sequential Logic Design using Verilog HDL.
Module dff_syn();
reg clk, reset, preset,d;
reg q;
always@(posedge clk)
If (reset)
begin
q<=0;
end else if(preset) begin
q<=1;
end else begin
q<=d;
end
Procedure block concurrency & Race Condition.
input clk ;
wire clk ;
input reset ;
wire reset ;
initial dout = 0 ;
always @ (posedge (clk)) begin
if (reset)
dout <= 0;
else if (dout<9) begin
dout <= dout + 1;
end else if (dout==9) begin
dout <= 0;
end
end