SlideShare a Scribd company logo
1 of 78
Verilog HDL
Verilog tutorial
E2MATRIX RESEARCH LAB
Opp Phagwara Bus Stand,
Backside Axis Bank, Parmar
Complex
Phagwara, Punjab ( India ).
Contact : +91 9041262727
Web : www.e2matrix.com
Email : support@e2matrix.com
1
Overview
ī‚´ Simulation and Synthesis
ī‚´ Modules and Primitives
ī‚´ Styles
ī‚´ Structural Descriptions
ī‚´ Language Conventions
ī‚´ Data Types
ī‚´ Delay
ī‚´ Behavioral Constructs
ī‚´ Compiler Directives
ī‚´ Simulation and Testbenches
2
Simulation and Synthesis
ī‚´Simulation tools typically accept full set of Verilog
language constructs
ī‚´Some language constructs and their use in a Verilog
description make simulation efficient and are ignored
by synthesis tools
ī‚´Synthesis tools typically accept only a subset of the
full Verilog language constructs
ī‚´In this presentation, Verilog language constructs not
supported in Synopsys FPGA Express are in red italics
ī‚´There are other restrictions not detailed here, see [2].
3
Modules
ī‚´The Module Concept
ī‚´Basic design unit
ī‚´Modules are:
ī‚´Declared
ī‚´Instantiated
ī‚´Modules declarations cannot be nested
4
Module Declaration
5
ī‚§ Annotated Example
/* module_keyword module_identifier (list of ports) */
module C_2_4_decoder_with_enable (A, E_n, D) ;
input [1:0] A ; // input_declaration
input E_n ; // input_declaration
output [3:0] D ; // output_declaration
assign D = {4{~E_n}} & ((A == 2'b00) ? 4'b0001 :
(A == 2'b01) ? 4'b0010 :
(A == 2'b10) ? 4'b0100 :
(A == 2'b11) ? 4'b1000 :
4'bxxxx) ; // continuous_assign
endmodule
Module Declaration
6
ī‚§ Identifiers - must not be keywords!
ī‚§ Ports
â€ĸ First example of signals
â€ĸ Scalar: e. g., E_n
â€ĸ Vector: e. g., A[1:0], A[0:1], D[3:0], and D[0:3]
ī‚§ Range is MSB to LSB
ī‚§ Can refer to partial ranges - D[2:1]
â€ĸ Type: defined by keywords
ī‚§ input
ī‚§ output
ī‚§ inout (bi-directional)
Module Instantiation
7
module C_4_16_decoder_with_enable (A, E_n, D) ;
input [3:0] A ;
input E_n ;
output [15:0] D ;
wire [3:0] S;
wire [3:0] S_n;
C_2_4_decoder_with_enable DE (A[3:2], E_n, S);
not N0 (S_n, S);
C_2_4_decoder_with_enable D0 (A[1:0], S_n[0], D[3:0]);
C_2_4_decoder_with_enable D1 (A[1:0], S_n[1], D[7:4]);
C_2_4_decoder_with_enable D2 (A[1:0], S_n[2], D[11:8]);
C_2_4_decoder_with_enable D3 (A[1:0], S_n[3], D[15:12]);
endmodule
ī‚§ Example
Module Instantiation
8
â€ĸ Single module instantiation for five module instances
C_2_4_decoder_with_enable DE (A[3:2], E_n, S),
D0 (A[1:0], S_n[0], D[3:0]),
D1 (A[1:0], S_n[1], D[7:4]),
D2 (A[1:0], S_n[2], D[11:8]),
D3 (A[1:0], S_n[3], D[15:12]);
â€ĸ Named_port connection
C_2_4_decoder_with_enable DE (.E_n (E_n), .A (A[3:2]) .D (S));
// Note order in list no longer important (E_n and A interchanged).
ī‚§ More Examples
Primitives
ī‚´Gate Level
ī‚´and, nand
ī‚´or, nor
ī‚´xor, xnor
ī‚´buf , not
ī‚´bufif0, bufif1, notif0, notif1 (three-state)
ī‚´Switch Level
ī‚´*mos where * is n, p, c, rn, rp, rc; pullup, pulldown; *tran+ where *
is (null), r and + (null), if0, if1 with both * and + not (null)
9
Primitives
ī‚´ No declaration; can only be instantiated
ī‚´ All output ports appear in list before any input ports
ī‚´ Optional drive strength, delay, name of instance
ī‚´ Example: and N25 (Z, A, B, C); //instance name
ī‚´ Example: and #10 (Z, A, B, X); // delay
(X, C, D, E); //delay
/*Usually better to provide instance name for debugging.*/
ī‚´ Example: or N30 (SET, Q1, AB, N5),
ī‚´ N41(N25, ABC, R1);
ī‚´ Example: and #10 N33(Z, A, B, X); // name + delay
10
Styles
ī‚´ Structural - instantiation of primitives and modules
ī‚´ RTL/Dataflow - continuous assignments
ī‚´ Behavioral - procedural assignments
11
Style Example - Structural
module half_add (X, Y, S, C);
input X, Y ;
output S, C ;
xor (S, X, Y) ;
and (C, X, Y) ;
endmodule
12
module full_add (A, B, CI, S, CO) ;
input A, B, CI ;
output S, CO ;
wire N1, N2, N3;
half_add HA1 (A, B, N1, N2),
HA2 (N1, CI, S, N3);
or P1 (CO, N3, N2);
endmodule
Style Example - RTL/Dataflow
13
module fa_rtl (A, B, CI, S, CO) ;
input A, B, CI ;
output S, CO ;
assign S = A ^ B ^ CI; //continuous assignment
assign CO = A & B | A & CI | B & CI; //continuous assignment
endmodule
Style Example - Behavioral
14
module fa_bhv (A, B, CI, S, CO) ;
input A, B, CI ;
output S, CO ;
reg S, CO; // required to “hold” values between events.
always@(A or B or CI) //;
begin
S <= A ^ B ^ CI; // procedural assignment
CO <= A & B | A & CI | B & CI;// procedural assignment
end
endmodule
Connections
ī‚´ By position association
ī‚´module C_2_4_decoder_with_enable (A, E_n, D);
ī‚´C_4_16_decoder_with_enable DX (X[3:2], W_n, word);
ī‚´A = X[3:2], E_n = W_n, D = word
ī‚´ By name association
ī‚´module C_2_4_decoder_with_enable (A, E_n, D);
ī‚´C_2_4_decoder_with_enable DX (.E_n(W_n), .A(X[3:2]),
.D(word));
ī‚´A = X[3:2], E_n = W_n, D = word
16
Connections
ī‚´ Empty Port Connections
ī‚´module C_2_4_decoder_with_enable (A, E_n, D);
ī‚´C_2_4_decoder_with_enable DX (X[3:2], , word);
ī‚´ Input E_n is at high-impedance state (z)
ī‚´C_2_4_decoder_with_enable DX (X[3:2], W_n ,);
ī‚´ Output D[3:0] unused.
17
Arrays of Instances
ī‚´{ , } is concatenate
ī‚´Example
module add_array (A, B, CIN, S, COUT) ;
input [7:0] A, B ;
input CIN ;
output [7:0] S ;
output COUT ;
wire [7:1] carry;
full_add FA[7:0] (A,B,{carry, CIN},S,{COUT, carry});
// instantiates eight full_add modules
endmodule
18
Language Conventions
ī‚´ Case-sensitivity
ī‚´ Verilog is case-sensitive.
ī‚´ Some simulators are case-insensitive
ī‚´ Advice: - Don’t use case-sensitive feature!
ī‚´ Keywords are lower case
ī‚´ Different names must be used for different items within the
same scope
ī‚´ Identifier alphabet:
ī‚´ Upper and lower case alphabeticals
ī‚´ decimal digits
ī‚´ underscore
19
Language Conventions
ī‚´ Maximum of 1024 characters in identifier
ī‚´ First character not a digit
ī‚´ Statement terminated by ;
ī‚´ Free format within statement except for within quotes
ī‚´ Strings enclosed in double quotes and must be on a single line
ī‚´ Comments:
ī‚´ All characters after // in a line are treated as a comment
ī‚´ Multi-line comments begin with /* and end with */
ī‚´ Compiler directives begin with // synopsys
ī‚´ Built-in system tasks or functions begin with $
20
Logic Values21
ī‚´ Verilog signal values
ī‚´ 0 - Logical 0 or FALSE
ī‚´ 1 - Logical 1 or TRUE
ī‚´ x, X - Unknown logic value
ī‚´ z, Z - High impedance condition
ī‚´ Also may have associated signal and charge strengths for switch level modeling
of MOS devices
ī‚´ 7 signal strengths plus 3 charge strengths
Number Representation
ī‚´Format: <size><base_format><number>
ī‚´<size> - decimal specification of number of bits
ī‚´default is unsized and machine-dependent, but at least 32 bits
ī‚´<base format> - ' followed by arithmetic base of number
ī‚´<d> <D> - decimal - default base if no <base_format> given
ī‚´<h> <H> - hexadecimal
ī‚´<o> <O> - octal
ī‚´<b> <B> - binary
ī‚´<number> - value given in base of <base_format>
ī‚´_ can be used for reading clarity
ī‚´If first character of sized, binary number is 0, 1, the value is 0-filled up
to size. If x or z,value is extended using x or z, respectively.
22
Number Representation
ī‚´Examples:
ī‚´6’b010_111 gives 010111
ī‚´8'b0110 gives 00000110
ī‚´8’b1110 gives 00001110
ī‚´4'bx01 gives xx01
ī‚´16'H3ABgives 0000001110101011
ī‚´24 gives 0â€Ļ0011000
ī‚´5'O36 gives 11100
ī‚´16'Hx gives xxxxxxxxxxxxxxxx
ī‚´8'hz gives zzzzzzzz
23
Variables
ī‚´ Nets
ī‚´ Used for structural connectivity
ī‚´ Registers
ī‚´ Abstraction of storage (May or may not be real physical storage)
ī‚´ Properties of Both
ī‚´ Informally called signals
ī‚´ May be either scalar (one bit) or vector (multiple bits)
24
Data Types - Nets - Semantics25
ī‚§ wire - connectivity only; no logical
ī‚§ tri - same as wire, but indicates will be 3-
stated in hardware
ī‚§ wand - multiple drivers - wired and
ī‚§ wor - multiple drivers - wired or
ī‚§ triand - same as wand, but 3-state
ī‚§ trior - same as wor but 3-state
ī‚§ supply0 - Global net GND
ī‚§ supply1 - Global Net VCC (VDD)
ī‚§ tri0, tri1, trireg
Net Examples
ī‚´wire x;
ī‚´wire x, y;
ī‚´wire [15:0] data, address;
ī‚´wire vectored [1:7] control;
ī‚´wire address = offset + index;
ī‚´wor interrupt_1, interrupt_2;
ī‚´tri [31:0] data_bus, operand_bus;
ī‚´Value implicitly assigned by connection to primitive or
module output
26
Initial Value & Undeclared Nets
ī‚´ Initial value of a net
ī‚´ At tsim = 0, initial value is x.
ī‚´ Undeclared Nets - Default type
ī‚´ Not explicitly declared default to wire
ī‚´ default_nettype compiler directive can specify others except for supply0 and supply1
27
Data Types - Register Semantics
ī‚´ reg - stores a logic value
ī‚´ integer – stores values which are not to be stored in hardware
ī‚´ Defaults to simulation computer register length or 32 bits whichever is
larger
ī‚´ No ranges or arrays supported
ī‚´ May yield excess hardware if value needs to be stored in hardware; in
such a case, use sized reg.
ī‚´ time - stores time 64-bit unsigned
ī‚´ real - stores values as real num
ī‚´ realtime - stores time values as real numbers
28
Register Assignment
ī‚´A register may be assigned value only within:
ī‚´ a procedural statement
ī‚´ a user-defined sequential primitive
ī‚´ a task, or
ī‚´ a function.
ī‚´A reg object may never by assigned value by:
ī‚´ a primitive gate output or
ī‚´ a continuous assignment
29
Register Examples
ī‚´ reg a, b, c;
ī‚´ reg [15:0] counter, shift_reg;
ī‚´ reg [8:4] flops;
ī‚´ integer sum, difference;
30
Strings
ī‚´ No explicit data type
ī‚´ Must be stored in reg whose size is 8*(num. of characters)
ī‚´ reg [255:0] buffer; //stores 32 characters
31
Constants (Paramters)
ī‚´ Declaration of parameters
ī‚´ parameter A = 2’b00, B = 2’b01, C = 2’b10;
ī‚´ parameter regsize = 8;
ī‚´ reg [regsize - 1:0]; /* illustrates use of parameter regsize */
32
Operators
ī‚´Arithmetic (binary: +, -,*,/,%*); (unary: +, -)
ī‚´Bitwise (~, &,|,^,~^,^~)
ī‚´Reduction (&,~&,|,~|,^,~^,^~)
ī‚´Logical (!,&&,||,==,!=,===,!==)
ī‚´Relational (<,<=,>,>=)
ī‚´Shift (>>,<<)
ī‚´Conditional ? :
ī‚´Concatenation and Replications {A,B} {4{B}}
* Not supported for variables
33
Expression Bit Widths
ī‚´Depends on:
ī‚´widths of operands and
ī‚´types of operators
ī‚´Verilog fills in smaller-width operands by using zero
extension.
ī‚´Final or intermediate result width may increase
expression width
34
Expression Bit Widths
ī‚´ Unsized constant number- same as integer (usually 32 bits)
ī‚´ Sized constant number - as specified
ī‚´ x op y where op is +, -, *, /, %, &, |, ^, ^~:
ī‚´ Arithmetic binary and bitwise
ī‚´ Bit width = max (width(x), width(y))
35
Expression Bit Widths (continued)
ī‚´ op x where op is +, -
ī‚´ Arithmetic unary
ī‚´ Bit width = width(x)
ī‚´ op x where op is ~
ī‚´ Bitwise negation
ī‚´ Bit width = width(x)
36
Expression Bit Widths (continued)
ī‚´ x op y where op is ==, !==, ===, !===, &&, ||, >, >=, <, <= or op y where op is !,
&, |, ^, ~&, ~|, ~^
ī‚´ Logical, relational and reduction
ī‚´ Bit width = 1
ī‚´ x op y where op is <<, >>
ī‚´ Shift
ī‚´ Bit width = width(x)
37
Expression Bit Widths (continued)
ī‚´ x ? y : z
ī‚´Conditional
ī‚´Bit width = max(width(y), width(z))
ī‚´{x, â€Ļ, y}
ī‚´Concatenation
ī‚´Bit width = width(x) + â€Ļ + width(y)
ī‚´{x{y, â€Ļ, z}}
ī‚´Replication
ī‚´Bit width = x * (width(y) + â€Ļ + width(z))
38
Expressions with Operands Containing x or
z
ī‚´Arithmetic
ī‚´If any bit is x or z, result is all x’s.
ī‚´Divide by 0 produces all x’s.
ī‚´Relational
ī‚´If any bit is x or z, result is x.
ī‚´Logical
ī‚´== and != If any bit is x or z, result is x.
ī‚´=== and !== All bits including x and z values must match for
equality
39
Expressions with Operands Containing x or
z
ī‚´Bitwise
ī‚´Defined by tables for 0, 1, x, z operands.
ī‚´Reduction
ī‚´Defined by tables as for bitwise operators.
ī‚´Shifts
ī‚´z changed to x. Vacated positions zero filled.
ī‚´Conditional
ī‚´If conditional expression is ambiguous (e.g., x or z), both
expressions are evaluated and bitwise combined as
follows: f(1,1) = 1, f(0,0) = 0, otherwise x.
40
Simulation Time Scales
ī‚´Compiler Directive `timescale <time_unit> /
<time_precision>
ī‚´time_unit - the time multiplier for time values
ī‚´time_precision - minimum step size during simulation
- determines rounding of numerical values
ī‚´Allowed unit/precision values:
{1| 10 | 100, s | ms | us | ns | ps}
41
Simulation Time Scales (continued)
ī‚´Example:
`timescale 10ps / 1ps
nor #3.57 (z, x1, x2);
nor delay used = 3.57 x 10 ps = 35.7 ps => 36 ps
ī‚´Different timescales can be used for different
sequences of modules
ī‚´The smallest time precision determines the
precision of the simulation.
42
Behavioral Constructs
ī‚´Concurrent communicating behaviors =>
processes same as behaviors
ī‚´Two constructs
ī‚´initial - one-time sequential activity flow - not
synthesizable but good for testbenches
ī‚´Always - cyclic (repetitive) sequential activity flow
ī‚´Use procedural statements that assign only
register variables (with one exception)
43
Behavioral Constructs (continued)
ī‚´Continuous assignments and primitives assign
outputs whenever there are events on the inputs
ī‚´Behaviors assign values when an assignment
statement in the activity flow executes. Input events
on the RHS do not initiate activity - control must be
passed to the statement.
44
Behavioral Constructs (continued)
ī‚´Body may consist of a single statement or a block
statement
ī‚´A block statement begins with begin and ends with
end
ī‚´Statements within a block statement execute
sequentially
ī‚´Behaviors are an elaborate form of continuous
assignments or primitives but operate on registers
(with one exception) rather than nets
45
Behavioral Constructs - Example
ī‚´ Initial: īē Always:
initial always
begin begin
one = 1; F1 = 0, F2 = 0;
two = one + 1; # 2 F1 = 1;
three = two + 1; # 4 F2 = 0;
four = three + 1; # 2 F1 = 1;
five = four + 1; # 4;
end end
ī‚´ What are results of each of the above?
46
Procedural Assignments
ī‚´Types
ī‚´= blocking assignment
ī‚´assign = continuous assignment
ī‚´<= non-blocking assignment
ī‚´Assignments (with one exception) to:
ī‚´reg
ī‚´integer
ī‚´real
ī‚´realtime
ī‚´time
47
Procedural Assignments - Some Rules
ī‚´ Register variable can be referenced anywhere in module
ī‚´ Register variable can be assigned only with procedural statement,
task or function
ī‚´ Register variable cannot be input or inout
ī‚´ Net variable can be referenced anywhere in module
ī‚´ Net variable may not be assigned within behavior, task or function.
Exception: force â€Ļ release
ī‚´ Net variable within a module must be driven by primitive,
continuous assignment, force â€Ļ release or module port
48
Procedural Timing, Controls &
Synchronization
ī‚´ Mechanisms
ī‚´ Delay Control Operator (#)
ī‚´ Event Control Operator (@)*
ī‚´ Event or
ī‚´ Named Events – not used much
ī‚´ wait construct
49
*Ignored by FPGA express unless a synchronous trigger
that infers a register
Procedural Timing, Controls &
Synchronization
ī‚´ Delay Control Operator (#)
ī‚´ Precedes assignment statement - postpones execution of statement
ī‚´ For blocking assignment (=), delays all statements that follow it
ī‚´ Blocking assignment statement must execute before subsequent statements can
execute.
ī‚´ Example: always @(posedge clk),
#10 Q = D;
50
Procedural Timing, Controls &
Synchronization
ī‚´ Event Control Operator (@)*
ī‚´Synchronizes the activity flow of a behavior to an event
(change) in a register or net variable or expression
ī‚´Example 1: @ (start) RegA = Data;
ī‚´Example 2: @(toggle) begin
â€Ļ
@ (posedge clk) Q = D;
â€Ļ
end
ī‚´*Ignored by FPGA express unless a synchronous trigger
that infers a register
51
Procedural Timing, Controls &
Synchronization
ī‚´ Event or - allows formation of event expression
ī‚´ Example:
always @ (X1 or X2 or X3)
assign Y = X1 & X2 | ~ X3;
ī‚´ All RHS variables in sensitivity list and no unspecified conditional results =>
combinational logic
52
Procedural Timing, Controls &
Synchronization
ī‚´ Meaning of posedge: 0 -> 1, 0 -> x, x -> 1
ī‚´ Special Example:
always @ (set or reset or posedge clk)
begin
if (reset == 1) Q = 0;
else if (set == 1) Q = 1;
else if (clk == 1) Q = data;
end
// Does this work correctly? Why or why not?
53
Procedural Timing, Controls & Synchronization
(FIO)
ī‚´wait Construct
ī‚´Suspends activity in behavior until expression following
wait is TRUE
ī‚´Example:
always
begin
a = b;
c = d;
wait (advance);
end
54
Blocking Assignments
ī‚´Identified by =
ī‚´Sequence of blocking assignments executes
sequentially
ī‚´Example:
always @(posedge clk)
begin
b = 0; c = 0;
b = a + a;
c = b + a;
d = c + a;
end
55
Non-Blocking Assignments
ī‚´ Identified by <=
ī‚´ Sequence of non-blocking assignments executes concurrently
ī‚´ Example 1:
always @(posedge clk)
begin
b <= 0; c <= 0;
b <= a + a;
c <= b + a;
d <= c + a;
end
/*Calculates b = 2a, c = b + a, d <= c + a. All values used on RHS
are those at posedge clock. Note that there are two
assignments to b and c. Only the last one is effective. */
56
Blocking Assignments - Inter-Assignment
Delay
ī‚´Delays evaluation of RHS and assignment to LHS
ī‚´ Example:
always @(posedge clk)
begin
b = 0; c = 0;
b = a + a; // uses a at posedge clock
#5 c = b + a; // uses a at posedge clock + 5
d = c + a; // uses a at posedge clock + 5
end /*c = 2 a(at posedge clock)+ a(at posedge clock + 5)
d = 2 a(at posedge clock) + 2 a(at posedge clock + 5)*/
57
58
īēDelays assignment to LHS and subsequent
statements, not evaluation of RHS
īēExample:
always @(posedge clk)
begin
b = 0; c = 0;
b = a + a; // uses a at posedge clock
c = #5 b + a; // uses a at posedge clock
d = c + a; // uses a at posedge clock + 5
end /* c = 3 a(at posedge clock)
d = 3a (at posedge clock)+ a (at posedge clock + 5)*/
Blocking Assignment -
Intra-Assignment Delay
Non-Blocking Assignment - Inter-
Assignment Delay
ī‚´Delays evaluation of RHS and assignment to LHS
ī‚´Delays subsequent statements
ī‚´ Example:
always @(posedge clk)
begin
b <= 0; c <= 0;
b <= a + a; // uses a at posedge clock
#5 c <= b + a; // uses b and a at posedge clock + 5
d <= c + a; // uses a at posedge clock + 5
end
/*c = b(at posedge clock + 5) + a(at posedge clock + 5) d = c(at
posedge clock + 5) + a (at posedge clock +5) */
59
Non-Blocking Assignment - Intra-
Assignment Delay
ī‚´Delays only assignment to LHS
ī‚´ Example:
always @(posedge clk)
begin
b <= 0; c <= 0;
b <= a + a; // uses a at posedge clock
c <= #5 b + a; // uses a and b at posedge clock d <=
c + a; // uses a and c at posedge clock
end
/* Calculates *c(posedge clock + 5) = b(at posedge clock) + a(at
posedge clock); d(posedge clock) =
c(at posedge clock) + a (at posedge clock) */
60
Activity Control
61
Overview
ī‚§ Constructs for Activity Control
â€ĸ Conditional operator
â€ĸ case statement
â€ĸ if â€Ļ else statement
â€ĸ Loops : repeat, for, while, forever
â€ĸ disable statement
â€ĸ fork â€Ļ join statement
ī‚§ Tasks and Functions
Conditional Operator
ī‚´ ? â€Ļ :
ī‚´ Same as for use in continuous assignment statement for net types except applied
to register types
ī‚´ Example:
always@(posedge clock)
Q <= S ? A : B //combined DFF and 2-to-1 MUX
62
case Statement
ī‚´ Requires complete bitwise match over all four values so
expression and case item expression must have same bit
length
ī‚´ Example: always@(state, x) begin
reg[1:0] state;
case (state)
2’b00: next_state <= s1;
2’b01: next_state <= s2;
2’b10: if x next_state <= s0;
else next_state <= s1;
end
default next_state = 1’bxx;
endcase
end
63
casex Statement
ī‚´Requires bitwise match over all but positions containing
x or z; executes first match encountered if multiple
matches.
ī‚´Example:
always@(code) begin
casex (code)
2’b0x: control <= 8’b00100110; //same for 2’b0z
2’b10: control <= 8’b11000010;
2’b11: control <= 8’b00111101;
default control <= 8b’xxxxxxxx;
endcase
end
64
casez Statement
ī‚´ Requires bitwise match over all but positions containing
z or ? (? is explicit don’t care); executes first match
encountered if multiple matches.
ī‚´ Example:
reg [1:0] code;
always@(code) begin
casez (code)
2’b0z: control <= 8’b00100110;
2’b1?: control <= 8’b11000010;
default control <= 8b’xxxxxxxx;
endcase
end
65
Conditional (if â€Ļ else) Statement Example
always@(a or b or c) begin
if (a == b)
begin
q <= data;
stop <= 1’b1;
end
else if (a > b)
q <= a;
else
q <= b;
end
end
end
66
Conditional (if â€Ļ else) Statement
(continued)
ī‚´Must be careful to define outcome for all possible
conditions – failure do do so can cause
unintentional inference of latches!
ī‚´else is paired with nearest if when ambiguous - use
begin and end in nesting to clarify.
ī‚´Nested if â€Ļ else will generate a “serial” or priority
like circuit in synthesis which may have a very long
delay - better to use case statements to get
“parallel” circuit.
67
for Loop Example
ī‚´ Example:
initial
integer r, i;
begin
r = 0;
for (i = 1; i <= 7; i = i + 2)
begin
r[i] = 1;
end
end
68
while Loop Example
initial
begin
r = 0;
i = 0;
while (i <= 7)
begin
r[i] = 1;
i = i + 2;
end
end
69
forever Loop Example
initial
begin
clk = 0;
forever
begin
#50 clk = 1;
#50 clk = 0;
end
end
ī‚´ Usually used in testbenches rather than for
synthesized logic.
70
Tasks
ī‚´Declared within a module
ī‚´Referenced only by a behavior within the module
ī‚´Parameters passed to task as inputs and inouts and
from task as outputs or inouts
ī‚´Local variables can be declared
ī‚´Recursion not supported although nesting permitted
(nested copies of variables use same storage)
ī‚´See Fig. 7.43 p. 226 of [5]for rules
71
Task Example
task leading_1;
input [7:0] data_word;
output [2:0] position;
reg [7:0] temp;
reg [2:0] position;
begin
temp = data_word;
position = 3'b111;
while (!temp[7])
@(posedge clock)
begin
temp = temp << 1;
position = position - 1;
end
end
endtask // Code is not synthesizable
72
Functions
ī‚´Implement combinational behavior
ī‚´No timing controls or tasks which implies no while
ī‚´May call other functions with no recursion
ī‚´Reference in an expression, e.g. RHS
ī‚´No output or inout allowed
ī‚´Implicit register having name and range of
function
73
Function Example
function [2:0] leading_1;
input [7:0] data_word;
reg [7:0] temp;
begin
temp = data_word;
leading_1 = 3'b111;
while (!temp[7])
begin
temp = temp << 1;
leading_1 = leading_1 - 1;
end
end
endfunction
ī‚´ Is the above code synthesizable?
74
No
Compiler Directives
ī‚´Useful for controlling what is synthesized and the
resulting logic
ī‚´Warning: Not recognized by other compilers – therefore
reduce code portability
ī‚´Examples:
ī‚´// synopsys translate_off
Code here describes something that is not to be synthesized
such at a simulation testbench -
can contain non-synthesizable constructs such as delays)
// synopsys translate_on
75
Compiler Directives (Continued)
ī‚´Examples:
ī‚´// synopsys parallel_case
Forces generation of multiplexer-like structure instead of priority
structure when included after case declaration
ī‚´// synopsys full_case
Indicates that all cases have been considered when included in
case declaration; when used, no default statement needed and
latches will not be inferred can be used in combination with
parallel case:
case (state) // synopsys parallel_case full_case
76
Testbench Approach
ī‚´ Use Verilog module to produce testing environment including stimulus generation
and/or response monitoring
77
UUT
Module
Stimulus Response
Testbench Module
Stimulus Generation Example
`timescale 1ns /1ns
module com_test_bench_v;
reg[8:0] stim;
wire[3:0] S;
wire C4;
adder_4_b_v a1(stim[8:5], stim[4:1], stim[0], S, C4);
//Continued on next slide
endmodule
78
Stimulus Generation Example
(Continued)
//Generate stimulus
initial
begin
stim = 9'b000000000;
#10 stim = 9'b111100001;
#10 stim = 9'b000011111;
#10 stim = 9'b111100010;
#10 stim = 9'b000111110;
#10 stim = 9'b111100000;
#10 stim = 9'b000011110;
#10 $stop;
end
79

More Related Content

What's hot

VHDL- gate level modelling
VHDL- gate level modellingVHDL- gate level modelling
VHDL- gate level modellingVandanaPagar1
 
Verilog overview
Verilog overviewVerilog overview
Verilog overviewposdege
 
Logic synthesis,flootplan&placement
Logic synthesis,flootplan&placementLogic synthesis,flootplan&placement
Logic synthesis,flootplan&placementshaik sharief
 
Basics of Digital Design and Verilog
Basics of Digital Design and VerilogBasics of Digital Design and Verilog
Basics of Digital Design and VerilogGanesan Narayanasamy
 
Concepts of Behavioral modelling in Verilog HDL
Concepts of Behavioral modelling in Verilog HDLConcepts of Behavioral modelling in Verilog HDL
Concepts of Behavioral modelling in Verilog HDLanand hd
 
Verilog Test Bench
Verilog Test BenchVerilog Test Bench
Verilog Test BenchDr.YNM
 
System verilog important
System verilog importantSystem verilog important
System verilog importantelumalai7
 
verilog code for logic gates
verilog code for logic gatesverilog code for logic gates
verilog code for logic gatesRakesh kumar jha
 
VHDL-Behavioral-Programs-Structure of VHDL
VHDL-Behavioral-Programs-Structure of VHDLVHDL-Behavioral-Programs-Structure of VHDL
VHDL-Behavioral-Programs-Structure of VHDLRevathi Subramaniam
 
Delays in verilog
Delays in verilogDelays in verilog
Delays in verilogJITU MISTRY
 
Verilog HDL- 2
Verilog HDL- 2Verilog HDL- 2
Verilog HDL- 2Prabhavathi P
 
Lecture 2 verilog
Lecture 2   verilogLecture 2   verilog
Lecture 2 verilogvenravi10
 
Verilog coding of demux 8 x1
Verilog coding of demux  8 x1Verilog coding of demux  8 x1
Verilog coding of demux 8 x1Rakesh kumar jha
 
Verilog tutorial
Verilog tutorialVerilog tutorial
Verilog tutorialraju reddy
 
Verilog lab manual (ECAD and VLSI Lab)
Verilog lab manual (ECAD and VLSI Lab)Verilog lab manual (ECAD and VLSI Lab)
Verilog lab manual (ECAD and VLSI Lab)Dr. Swaminathan Kathirvel
 

What's hot (20)

VHDL- gate level modelling
VHDL- gate level modellingVHDL- gate level modelling
VHDL- gate level modelling
 
Verilog overview
Verilog overviewVerilog overview
Verilog overview
 
Logic synthesis,flootplan&placement
Logic synthesis,flootplan&placementLogic synthesis,flootplan&placement
Logic synthesis,flootplan&placement
 
Hdl
HdlHdl
Hdl
 
Basics of Digital Design and Verilog
Basics of Digital Design and VerilogBasics of Digital Design and Verilog
Basics of Digital Design and Verilog
 
Concepts of Behavioral modelling in Verilog HDL
Concepts of Behavioral modelling in Verilog HDLConcepts of Behavioral modelling in Verilog HDL
Concepts of Behavioral modelling in Verilog HDL
 
Verilog Test Bench
Verilog Test BenchVerilog Test Bench
Verilog Test Bench
 
System verilog important
System verilog importantSystem verilog important
System verilog important
 
verilog code for logic gates
verilog code for logic gatesverilog code for logic gates
verilog code for logic gates
 
Verilog Tasks and functions
Verilog Tasks and functionsVerilog Tasks and functions
Verilog Tasks and functions
 
Crash course in verilog
Crash course in verilogCrash course in verilog
Crash course in verilog
 
Switch level modeling
Switch level modelingSwitch level modeling
Switch level modeling
 
VHDL-Behavioral-Programs-Structure of VHDL
VHDL-Behavioral-Programs-Structure of VHDLVHDL-Behavioral-Programs-Structure of VHDL
VHDL-Behavioral-Programs-Structure of VHDL
 
Delays in verilog
Delays in verilogDelays in verilog
Delays in verilog
 
Verilog HDL- 2
Verilog HDL- 2Verilog HDL- 2
Verilog HDL- 2
 
Lecture 2 verilog
Lecture 2   verilogLecture 2   verilog
Lecture 2 verilog
 
Verilog coding of demux 8 x1
Verilog coding of demux  8 x1Verilog coding of demux  8 x1
Verilog coding of demux 8 x1
 
Verilog tutorial
Verilog tutorialVerilog tutorial
Verilog tutorial
 
Verilog lab manual (ECAD and VLSI Lab)
Verilog lab manual (ECAD and VLSI Lab)Verilog lab manual (ECAD and VLSI Lab)
Verilog lab manual (ECAD and VLSI Lab)
 
Vhdl
VhdlVhdl
Vhdl
 

Viewers also liked

Efficient Migration of Verilog Testbenches to 'UVM' Keeping the Functionality...
Efficient Migration of Verilog Testbenches to 'UVM' Keeping the Functionality...Efficient Migration of Verilog Testbenches to 'UVM' Keeping the Functionality...
Efficient Migration of Verilog Testbenches to 'UVM' Keeping the Functionality...DVClub
 
Verilog tutorial
Verilog tutorialVerilog tutorial
Verilog tutorialamnis_azeneth
 
Experiment write-vhdl-code-for-realize-all-logic-gates
Experiment write-vhdl-code-for-realize-all-logic-gatesExperiment write-vhdl-code-for-realize-all-logic-gates
Experiment write-vhdl-code-for-realize-all-logic-gatesRicardo Castro
 
Verilog code all
Verilog code allVerilog code all
Verilog code allMNIT jaipur
 
Easy Learn to Verilog HDL
Easy Learn to Verilog HDLEasy Learn to Verilog HDL
Easy Learn to Verilog HDLkpyes34
 
Verilog hdl by samir palnitkar for verilog know how
Verilog hdl   by samir palnitkar for verilog know howVerilog hdl   by samir palnitkar for verilog know how
Verilog hdl by samir palnitkar for verilog know howSyed Ghufran Hassan
 
An 8 bit_multiplier
An 8 bit_multiplierAn 8 bit_multiplier
An 8 bit_multiplierRobi Parvez
 
Verilog HDL Training Course
Verilog HDL Training CourseVerilog HDL Training Course
Verilog HDL Training CoursePaul Laskowski
 
Vhdl Project List - Verilog Projects
Vhdl Project List - Verilog Projects Vhdl Project List - Verilog Projects
Vhdl Project List - Verilog Projects E2MATRIX
 
test generation
test generationtest generation
test generationdennis gookyi
 
System Verilog Tutorial - VHDL
System Verilog Tutorial - VHDLSystem Verilog Tutorial - VHDL
System Verilog Tutorial - VHDLE2MATRIX
 
Fpga implementation of high speed 8 bit vedic multiplier using barrel shifter(1)
Fpga implementation of high speed 8 bit vedic multiplier using barrel shifter(1)Fpga implementation of high speed 8 bit vedic multiplier using barrel shifter(1)
Fpga implementation of high speed 8 bit vedic multiplier using barrel shifter(1)Karthik Sagar
 
Multipliers in VLSI
Multipliers in VLSIMultipliers in VLSI
Multipliers in VLSIKiranmai Sony
 
ieee projects list
ieee projects listieee projects list
ieee projects list8130809758
 
Verilog codes and testbench codes for basic digital electronic circuits.
Verilog codes and testbench codes for basic digital electronic circuits. Verilog codes and testbench codes for basic digital electronic circuits.
Verilog codes and testbench codes for basic digital electronic circuits. shobhan pujari
 
8 bit alu design
8 bit alu design8 bit alu design
8 bit alu designshobhan pujari
 
Digital Circuit Verification Hardware Descriptive Language Verilog
Digital Circuit Verification Hardware Descriptive Language VerilogDigital Circuit Verification Hardware Descriptive Language Verilog
Digital Circuit Verification Hardware Descriptive Language VerilogAbhiraj Bohra
 

Viewers also liked (20)

Efficient Migration of Verilog Testbenches to 'UVM' Keeping the Functionality...
Efficient Migration of Verilog Testbenches to 'UVM' Keeping the Functionality...Efficient Migration of Verilog Testbenches to 'UVM' Keeping the Functionality...
Efficient Migration of Verilog Testbenches to 'UVM' Keeping the Functionality...
 
Verilog tutorial
Verilog tutorialVerilog tutorial
Verilog tutorial
 
Experiment write-vhdl-code-for-realize-all-logic-gates
Experiment write-vhdl-code-for-realize-all-logic-gatesExperiment write-vhdl-code-for-realize-all-logic-gates
Experiment write-vhdl-code-for-realize-all-logic-gates
 
VERILOG CODE
VERILOG CODEVERILOG CODE
VERILOG CODE
 
Verilog code all
Verilog code allVerilog code all
Verilog code all
 
07 sequential verilog
07 sequential verilog07 sequential verilog
07 sequential verilog
 
Easy Learn to Verilog HDL
Easy Learn to Verilog HDLEasy Learn to Verilog HDL
Easy Learn to Verilog HDL
 
Verilog hdl by samir palnitkar for verilog know how
Verilog hdl   by samir palnitkar for verilog know howVerilog hdl   by samir palnitkar for verilog know how
Verilog hdl by samir palnitkar for verilog know how
 
An 8 bit_multiplier
An 8 bit_multiplierAn 8 bit_multiplier
An 8 bit_multiplier
 
Verilog HDL Training Course
Verilog HDL Training CourseVerilog HDL Training Course
Verilog HDL Training Course
 
Vhdl Project List - Verilog Projects
Vhdl Project List - Verilog Projects Vhdl Project List - Verilog Projects
Vhdl Project List - Verilog Projects
 
B.Tech VLSI projects list
B.Tech VLSI projects listB.Tech VLSI projects list
B.Tech VLSI projects list
 
test generation
test generationtest generation
test generation
 
System Verilog Tutorial - VHDL
System Verilog Tutorial - VHDLSystem Verilog Tutorial - VHDL
System Verilog Tutorial - VHDL
 
Fpga implementation of high speed 8 bit vedic multiplier using barrel shifter(1)
Fpga implementation of high speed 8 bit vedic multiplier using barrel shifter(1)Fpga implementation of high speed 8 bit vedic multiplier using barrel shifter(1)
Fpga implementation of high speed 8 bit vedic multiplier using barrel shifter(1)
 
Multipliers in VLSI
Multipliers in VLSIMultipliers in VLSI
Multipliers in VLSI
 
ieee projects list
ieee projects listieee projects list
ieee projects list
 
Verilog codes and testbench codes for basic digital electronic circuits.
Verilog codes and testbench codes for basic digital electronic circuits. Verilog codes and testbench codes for basic digital electronic circuits.
Verilog codes and testbench codes for basic digital electronic circuits.
 
8 bit alu design
8 bit alu design8 bit alu design
8 bit alu design
 
Digital Circuit Verification Hardware Descriptive Language Verilog
Digital Circuit Verification Hardware Descriptive Language VerilogDigital Circuit Verification Hardware Descriptive Language Verilog
Digital Circuit Verification Hardware Descriptive Language Verilog
 

Similar to Verilog Tutorial - Verilog HDL Tutorial with Examples

Verilog Final Probe'22.pptx
Verilog Final Probe'22.pptxVerilog Final Probe'22.pptx
Verilog Final Probe'22.pptxSyedAzim6
 
Hardware Description Language
Hardware Description Language Hardware Description Language
Hardware Description Language Prachi Pandey
 
Verilog
VerilogVerilog
Verilogabkvlsi
 
Verilog_Overview.pdf
Verilog_Overview.pdfVerilog_Overview.pdf
Verilog_Overview.pdfQuangHuyDo3
 
An Overview of SystemVerilog for Design and Verification
An Overview of SystemVerilog  for Design and VerificationAn Overview of SystemVerilog  for Design and Verification
An Overview of SystemVerilog for Design and VerificationKapilRaghunandanTrip
 
SKEL 4273 CAD with HDL Topic 2
SKEL 4273 CAD with HDL Topic 2SKEL 4273 CAD with HDL Topic 2
SKEL 4273 CAD with HDL Topic 2alhadi81
 
vlsi design using verilog presentaion 1
vlsi design using verilog   presentaion 1vlsi design using verilog   presentaion 1
vlsi design using verilog presentaion 1MANDHASAIGOUD1
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5PRADEEP
 
dokumen.tips_verilog-basic-ppt.pdf
dokumen.tips_verilog-basic-ppt.pdfdokumen.tips_verilog-basic-ppt.pdf
dokumen.tips_verilog-basic-ppt.pdfVelmathi Saravanan
 
Seminar on Digital Multiplier(Booth Multiplier) Using VHDL
Seminar on Digital Multiplier(Booth Multiplier) Using VHDLSeminar on Digital Multiplier(Booth Multiplier) Using VHDL
Seminar on Digital Multiplier(Booth Multiplier) Using VHDLNaseer LoneRider
 
CombVerilog.pdf
CombVerilog.pdfCombVerilog.pdf
CombVerilog.pdfashwkr07
 

Similar to Verilog Tutorial - Verilog HDL Tutorial with Examples (20)

Verilog Final Probe'22.pptx
Verilog Final Probe'22.pptxVerilog Final Probe'22.pptx
Verilog Final Probe'22.pptx
 
Verilogspk1
Verilogspk1Verilogspk1
Verilogspk1
 
Hardware Description Language
Hardware Description Language Hardware Description Language
Hardware Description Language
 
slide8.ppt
slide8.pptslide8.ppt
slide8.ppt
 
Verilog
VerilogVerilog
Verilog
 
Verilog_Overview.pdf
Verilog_Overview.pdfVerilog_Overview.pdf
Verilog_Overview.pdf
 
Verilog HDL
Verilog HDL Verilog HDL
Verilog HDL
 
An Overview of SystemVerilog for Design and Verification
An Overview of SystemVerilog  for Design and VerificationAn Overview of SystemVerilog  for Design and Verification
An Overview of SystemVerilog for Design and Verification
 
Spdas2 vlsibput
Spdas2 vlsibputSpdas2 vlsibput
Spdas2 vlsibput
 
SKEL 4273 CAD with HDL Topic 2
SKEL 4273 CAD with HDL Topic 2SKEL 4273 CAD with HDL Topic 2
SKEL 4273 CAD with HDL Topic 2
 
Analog to Digital Converter
Analog to Digital ConverterAnalog to Digital Converter
Analog to Digital Converter
 
vlsi design using verilog presentaion 1
vlsi design using verilog   presentaion 1vlsi design using verilog   presentaion 1
vlsi design using verilog presentaion 1
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5
 
DDUV.pdf
DDUV.pdfDDUV.pdf
DDUV.pdf
 
dokumen.tips_verilog-basic-ppt.pdf
dokumen.tips_verilog-basic-ppt.pdfdokumen.tips_verilog-basic-ppt.pdf
dokumen.tips_verilog-basic-ppt.pdf
 
ERTS UNIT 3.ppt
ERTS UNIT 3.pptERTS UNIT 3.ppt
ERTS UNIT 3.ppt
 
Seminar on Digital Multiplier(Booth Multiplier) Using VHDL
Seminar on Digital Multiplier(Booth Multiplier) Using VHDLSeminar on Digital Multiplier(Booth Multiplier) Using VHDL
Seminar on Digital Multiplier(Booth Multiplier) Using VHDL
 
CombVerilog.pdf
CombVerilog.pdfCombVerilog.pdf
CombVerilog.pdf
 
C programming part2
C programming part2C programming part2
C programming part2
 
C programming part2
C programming part2C programming part2
C programming part2
 

More from E2MATRIX

Electrical Training in Phagwara
Electrical Training in PhagwaraElectrical Training in Phagwara
Electrical Training in PhagwaraE2MATRIX
 
Electrical Training in Mohali
Electrical Training in MohaliElectrical Training in Mohali
Electrical Training in MohaliE2MATRIX
 
Electrical Training in Ludhiana
Electrical Training in LudhianaElectrical Training in Ludhiana
Electrical Training in LudhianaE2MATRIX
 
Electrical Training in Jalandhar
Electrical Training in JalandharElectrical Training in Jalandhar
Electrical Training in JalandharE2MATRIX
 
Electrical Training in Chandigarh
Electrical Training in ChandigarhElectrical Training in Chandigarh
Electrical Training in ChandigarhE2MATRIX
 
Electrical Training in Amritsar
Electrical Training in AmritsarElectrical Training in Amritsar
Electrical Training in AmritsarE2MATRIX
 
Big Data Training in Amritsar
Big Data Training in AmritsarBig Data Training in Amritsar
Big Data Training in AmritsarE2MATRIX
 
Big Data Training in Mohali
Big Data Training in MohaliBig Data Training in Mohali
Big Data Training in MohaliE2MATRIX
 
Big Data Training in Ludhiana
Big Data Training in LudhianaBig Data Training in Ludhiana
Big Data Training in LudhianaE2MATRIX
 
Machine Learning Training in Phagwara
Machine Learning Training in PhagwaraMachine Learning Training in Phagwara
Machine Learning Training in PhagwaraE2MATRIX
 
Machine Learning Training in Ludhiana
Machine Learning Training in LudhianaMachine Learning Training in Ludhiana
Machine Learning Training in LudhianaE2MATRIX
 
Machine Learning Training in Amritsar
Machine Learning Training in AmritsarMachine Learning Training in Amritsar
Machine Learning Training in AmritsarE2MATRIX
 
Machine Learning Training in Mohali
Machine Learning Training in MohaliMachine Learning Training in Mohali
Machine Learning Training in MohaliE2MATRIX
 
Machine Learning Training in Jalandhar
Machine Learning Training in JalandharMachine Learning Training in Jalandhar
Machine Learning Training in JalandharE2MATRIX
 
Machine Learning Training in Chandigarh
Machine Learning Training in ChandigarhMachine Learning Training in Chandigarh
Machine Learning Training in ChandigarhE2MATRIX
 
Raspberry Pi training in Ludhiana
Raspberry Pi training in LudhianaRaspberry Pi training in Ludhiana
Raspberry Pi training in LudhianaE2MATRIX
 
Raspberry Pi Training in Phagwara
Raspberry Pi Training in PhagwaraRaspberry Pi Training in Phagwara
Raspberry Pi Training in PhagwaraE2MATRIX
 
Raspberry Pi Training in Mohali
Raspberry Pi Training in MohaliRaspberry Pi Training in Mohali
Raspberry Pi Training in MohaliE2MATRIX
 
Raspberry Pi Training in Chandigarh
Raspberry Pi Training in ChandigarhRaspberry Pi Training in Chandigarh
Raspberry Pi Training in ChandigarhE2MATRIX
 
Raspberry Pi Training in Amritsar
Raspberry Pi Training in AmritsarRaspberry Pi Training in Amritsar
Raspberry Pi Training in AmritsarE2MATRIX
 

More from E2MATRIX (20)

Electrical Training in Phagwara
Electrical Training in PhagwaraElectrical Training in Phagwara
Electrical Training in Phagwara
 
Electrical Training in Mohali
Electrical Training in MohaliElectrical Training in Mohali
Electrical Training in Mohali
 
Electrical Training in Ludhiana
Electrical Training in LudhianaElectrical Training in Ludhiana
Electrical Training in Ludhiana
 
Electrical Training in Jalandhar
Electrical Training in JalandharElectrical Training in Jalandhar
Electrical Training in Jalandhar
 
Electrical Training in Chandigarh
Electrical Training in ChandigarhElectrical Training in Chandigarh
Electrical Training in Chandigarh
 
Electrical Training in Amritsar
Electrical Training in AmritsarElectrical Training in Amritsar
Electrical Training in Amritsar
 
Big Data Training in Amritsar
Big Data Training in AmritsarBig Data Training in Amritsar
Big Data Training in Amritsar
 
Big Data Training in Mohali
Big Data Training in MohaliBig Data Training in Mohali
Big Data Training in Mohali
 
Big Data Training in Ludhiana
Big Data Training in LudhianaBig Data Training in Ludhiana
Big Data Training in Ludhiana
 
Machine Learning Training in Phagwara
Machine Learning Training in PhagwaraMachine Learning Training in Phagwara
Machine Learning Training in Phagwara
 
Machine Learning Training in Ludhiana
Machine Learning Training in LudhianaMachine Learning Training in Ludhiana
Machine Learning Training in Ludhiana
 
Machine Learning Training in Amritsar
Machine Learning Training in AmritsarMachine Learning Training in Amritsar
Machine Learning Training in Amritsar
 
Machine Learning Training in Mohali
Machine Learning Training in MohaliMachine Learning Training in Mohali
Machine Learning Training in Mohali
 
Machine Learning Training in Jalandhar
Machine Learning Training in JalandharMachine Learning Training in Jalandhar
Machine Learning Training in Jalandhar
 
Machine Learning Training in Chandigarh
Machine Learning Training in ChandigarhMachine Learning Training in Chandigarh
Machine Learning Training in Chandigarh
 
Raspberry Pi training in Ludhiana
Raspberry Pi training in LudhianaRaspberry Pi training in Ludhiana
Raspberry Pi training in Ludhiana
 
Raspberry Pi Training in Phagwara
Raspberry Pi Training in PhagwaraRaspberry Pi Training in Phagwara
Raspberry Pi Training in Phagwara
 
Raspberry Pi Training in Mohali
Raspberry Pi Training in MohaliRaspberry Pi Training in Mohali
Raspberry Pi Training in Mohali
 
Raspberry Pi Training in Chandigarh
Raspberry Pi Training in ChandigarhRaspberry Pi Training in Chandigarh
Raspberry Pi Training in Chandigarh
 
Raspberry Pi Training in Amritsar
Raspberry Pi Training in AmritsarRaspberry Pi Training in Amritsar
Raspberry Pi Training in Amritsar
 

Recently uploaded

Visit to a blind student's school🧑‍đŸĻ¯đŸ§‘‍đŸĻ¯(community medicine)
Visit to a blind student's school🧑‍đŸĻ¯đŸ§‘‍đŸĻ¯(community medicine)Visit to a blind student's school🧑‍đŸĻ¯đŸ§‘‍đŸĻ¯(community medicine)
Visit to a blind student's school🧑‍đŸĻ¯đŸ§‘‍đŸĻ¯(community medicine)lakshayb543
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1GloryAnnCastre1
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptxJonalynLegaspi2
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxDhatriParmar
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsPooky Knightsmith
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 

Recently uploaded (20)

Visit to a blind student's school🧑‍đŸĻ¯đŸ§‘‍đŸĻ¯(community medicine)
Visit to a blind student's school🧑‍đŸĻ¯đŸ§‘‍đŸĻ¯(community medicine)Visit to a blind student's school🧑‍đŸĻ¯đŸ§‘‍đŸĻ¯(community medicine)
Visit to a blind student's school🧑‍đŸĻ¯đŸ§‘‍đŸĻ¯(community medicine)
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptx
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young minds
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 

Verilog Tutorial - Verilog HDL Tutorial with Examples

  • 1. Verilog HDL Verilog tutorial E2MATRIX RESEARCH LAB Opp Phagwara Bus Stand, Backside Axis Bank, Parmar Complex Phagwara, Punjab ( India ). Contact : +91 9041262727 Web : www.e2matrix.com Email : support@e2matrix.com 1
  • 2. Overview ī‚´ Simulation and Synthesis ī‚´ Modules and Primitives ī‚´ Styles ī‚´ Structural Descriptions ī‚´ Language Conventions ī‚´ Data Types ī‚´ Delay ī‚´ Behavioral Constructs ī‚´ Compiler Directives ī‚´ Simulation and Testbenches 2
  • 3. Simulation and Synthesis ī‚´Simulation tools typically accept full set of Verilog language constructs ī‚´Some language constructs and their use in a Verilog description make simulation efficient and are ignored by synthesis tools ī‚´Synthesis tools typically accept only a subset of the full Verilog language constructs ī‚´In this presentation, Verilog language constructs not supported in Synopsys FPGA Express are in red italics ī‚´There are other restrictions not detailed here, see [2]. 3
  • 4. Modules ī‚´The Module Concept ī‚´Basic design unit ī‚´Modules are: ī‚´Declared ī‚´Instantiated ī‚´Modules declarations cannot be nested 4
  • 5. Module Declaration 5 ī‚§ Annotated Example /* module_keyword module_identifier (list of ports) */ module C_2_4_decoder_with_enable (A, E_n, D) ; input [1:0] A ; // input_declaration input E_n ; // input_declaration output [3:0] D ; // output_declaration assign D = {4{~E_n}} & ((A == 2'b00) ? 4'b0001 : (A == 2'b01) ? 4'b0010 : (A == 2'b10) ? 4'b0100 : (A == 2'b11) ? 4'b1000 : 4'bxxxx) ; // continuous_assign endmodule
  • 6. Module Declaration 6 ī‚§ Identifiers - must not be keywords! ī‚§ Ports â€ĸ First example of signals â€ĸ Scalar: e. g., E_n â€ĸ Vector: e. g., A[1:0], A[0:1], D[3:0], and D[0:3] ī‚§ Range is MSB to LSB ī‚§ Can refer to partial ranges - D[2:1] â€ĸ Type: defined by keywords ī‚§ input ī‚§ output ī‚§ inout (bi-directional)
  • 7. Module Instantiation 7 module C_4_16_decoder_with_enable (A, E_n, D) ; input [3:0] A ; input E_n ; output [15:0] D ; wire [3:0] S; wire [3:0] S_n; C_2_4_decoder_with_enable DE (A[3:2], E_n, S); not N0 (S_n, S); C_2_4_decoder_with_enable D0 (A[1:0], S_n[0], D[3:0]); C_2_4_decoder_with_enable D1 (A[1:0], S_n[1], D[7:4]); C_2_4_decoder_with_enable D2 (A[1:0], S_n[2], D[11:8]); C_2_4_decoder_with_enable D3 (A[1:0], S_n[3], D[15:12]); endmodule ī‚§ Example
  • 8. Module Instantiation 8 â€ĸ Single module instantiation for five module instances C_2_4_decoder_with_enable DE (A[3:2], E_n, S), D0 (A[1:0], S_n[0], D[3:0]), D1 (A[1:0], S_n[1], D[7:4]), D2 (A[1:0], S_n[2], D[11:8]), D3 (A[1:0], S_n[3], D[15:12]); â€ĸ Named_port connection C_2_4_decoder_with_enable DE (.E_n (E_n), .A (A[3:2]) .D (S)); // Note order in list no longer important (E_n and A interchanged). ī‚§ More Examples
  • 9. Primitives ī‚´Gate Level ī‚´and, nand ī‚´or, nor ī‚´xor, xnor ī‚´buf , not ī‚´bufif0, bufif1, notif0, notif1 (three-state) ī‚´Switch Level ī‚´*mos where * is n, p, c, rn, rp, rc; pullup, pulldown; *tran+ where * is (null), r and + (null), if0, if1 with both * and + not (null) 9
  • 10. Primitives ī‚´ No declaration; can only be instantiated ī‚´ All output ports appear in list before any input ports ī‚´ Optional drive strength, delay, name of instance ī‚´ Example: and N25 (Z, A, B, C); //instance name ī‚´ Example: and #10 (Z, A, B, X); // delay (X, C, D, E); //delay /*Usually better to provide instance name for debugging.*/ ī‚´ Example: or N30 (SET, Q1, AB, N5), ī‚´ N41(N25, ABC, R1); ī‚´ Example: and #10 N33(Z, A, B, X); // name + delay 10
  • 11. Styles ī‚´ Structural - instantiation of primitives and modules ī‚´ RTL/Dataflow - continuous assignments ī‚´ Behavioral - procedural assignments 11
  • 12. Style Example - Structural module half_add (X, Y, S, C); input X, Y ; output S, C ; xor (S, X, Y) ; and (C, X, Y) ; endmodule 12 module full_add (A, B, CI, S, CO) ; input A, B, CI ; output S, CO ; wire N1, N2, N3; half_add HA1 (A, B, N1, N2), HA2 (N1, CI, S, N3); or P1 (CO, N3, N2); endmodule
  • 13. Style Example - RTL/Dataflow 13 module fa_rtl (A, B, CI, S, CO) ; input A, B, CI ; output S, CO ; assign S = A ^ B ^ CI; //continuous assignment assign CO = A & B | A & CI | B & CI; //continuous assignment endmodule
  • 14. Style Example - Behavioral 14 module fa_bhv (A, B, CI, S, CO) ; input A, B, CI ; output S, CO ; reg S, CO; // required to “hold” values between events. always@(A or B or CI) //; begin S <= A ^ B ^ CI; // procedural assignment CO <= A & B | A & CI | B & CI;// procedural assignment end endmodule
  • 15. Connections ī‚´ By position association ī‚´module C_2_4_decoder_with_enable (A, E_n, D); ī‚´C_4_16_decoder_with_enable DX (X[3:2], W_n, word); ī‚´A = X[3:2], E_n = W_n, D = word ī‚´ By name association ī‚´module C_2_4_decoder_with_enable (A, E_n, D); ī‚´C_2_4_decoder_with_enable DX (.E_n(W_n), .A(X[3:2]), .D(word)); ī‚´A = X[3:2], E_n = W_n, D = word 16
  • 16. Connections ī‚´ Empty Port Connections ī‚´module C_2_4_decoder_with_enable (A, E_n, D); ī‚´C_2_4_decoder_with_enable DX (X[3:2], , word); ī‚´ Input E_n is at high-impedance state (z) ī‚´C_2_4_decoder_with_enable DX (X[3:2], W_n ,); ī‚´ Output D[3:0] unused. 17
  • 17. Arrays of Instances ī‚´{ , } is concatenate ī‚´Example module add_array (A, B, CIN, S, COUT) ; input [7:0] A, B ; input CIN ; output [7:0] S ; output COUT ; wire [7:1] carry; full_add FA[7:0] (A,B,{carry, CIN},S,{COUT, carry}); // instantiates eight full_add modules endmodule 18
  • 18. Language Conventions ī‚´ Case-sensitivity ī‚´ Verilog is case-sensitive. ī‚´ Some simulators are case-insensitive ī‚´ Advice: - Don’t use case-sensitive feature! ī‚´ Keywords are lower case ī‚´ Different names must be used for different items within the same scope ī‚´ Identifier alphabet: ī‚´ Upper and lower case alphabeticals ī‚´ decimal digits ī‚´ underscore 19
  • 19. Language Conventions ī‚´ Maximum of 1024 characters in identifier ī‚´ First character not a digit ī‚´ Statement terminated by ; ī‚´ Free format within statement except for within quotes ī‚´ Strings enclosed in double quotes and must be on a single line ī‚´ Comments: ī‚´ All characters after // in a line are treated as a comment ī‚´ Multi-line comments begin with /* and end with */ ī‚´ Compiler directives begin with // synopsys ī‚´ Built-in system tasks or functions begin with $ 20
  • 20. Logic Values21 ī‚´ Verilog signal values ī‚´ 0 - Logical 0 or FALSE ī‚´ 1 - Logical 1 or TRUE ī‚´ x, X - Unknown logic value ī‚´ z, Z - High impedance condition ī‚´ Also may have associated signal and charge strengths for switch level modeling of MOS devices ī‚´ 7 signal strengths plus 3 charge strengths
  • 21. Number Representation ī‚´Format: <size><base_format><number> ī‚´<size> - decimal specification of number of bits ī‚´default is unsized and machine-dependent, but at least 32 bits ī‚´<base format> - ' followed by arithmetic base of number ī‚´<d> <D> - decimal - default base if no <base_format> given ī‚´<h> <H> - hexadecimal ī‚´<o> <O> - octal ī‚´<b> <B> - binary ī‚´<number> - value given in base of <base_format> ī‚´_ can be used for reading clarity ī‚´If first character of sized, binary number is 0, 1, the value is 0-filled up to size. If x or z,value is extended using x or z, respectively. 22
  • 22. Number Representation ī‚´Examples: ī‚´6’b010_111 gives 010111 ī‚´8'b0110 gives 00000110 ī‚´8’b1110 gives 00001110 ī‚´4'bx01 gives xx01 ī‚´16'H3ABgives 0000001110101011 ī‚´24 gives 0â€Ļ0011000 ī‚´5'O36 gives 11100 ī‚´16'Hx gives xxxxxxxxxxxxxxxx ī‚´8'hz gives zzzzzzzz 23
  • 23. Variables ī‚´ Nets ī‚´ Used for structural connectivity ī‚´ Registers ī‚´ Abstraction of storage (May or may not be real physical storage) ī‚´ Properties of Both ī‚´ Informally called signals ī‚´ May be either scalar (one bit) or vector (multiple bits) 24
  • 24. Data Types - Nets - Semantics25 ī‚§ wire - connectivity only; no logical ī‚§ tri - same as wire, but indicates will be 3- stated in hardware ī‚§ wand - multiple drivers - wired and ī‚§ wor - multiple drivers - wired or ī‚§ triand - same as wand, but 3-state ī‚§ trior - same as wor but 3-state ī‚§ supply0 - Global net GND ī‚§ supply1 - Global Net VCC (VDD) ī‚§ tri0, tri1, trireg
  • 25. Net Examples ī‚´wire x; ī‚´wire x, y; ī‚´wire [15:0] data, address; ī‚´wire vectored [1:7] control; ī‚´wire address = offset + index; ī‚´wor interrupt_1, interrupt_2; ī‚´tri [31:0] data_bus, operand_bus; ī‚´Value implicitly assigned by connection to primitive or module output 26
  • 26. Initial Value & Undeclared Nets ī‚´ Initial value of a net ī‚´ At tsim = 0, initial value is x. ī‚´ Undeclared Nets - Default type ī‚´ Not explicitly declared default to wire ī‚´ default_nettype compiler directive can specify others except for supply0 and supply1 27
  • 27. Data Types - Register Semantics ī‚´ reg - stores a logic value ī‚´ integer – stores values which are not to be stored in hardware ī‚´ Defaults to simulation computer register length or 32 bits whichever is larger ī‚´ No ranges or arrays supported ī‚´ May yield excess hardware if value needs to be stored in hardware; in such a case, use sized reg. ī‚´ time - stores time 64-bit unsigned ī‚´ real - stores values as real num ī‚´ realtime - stores time values as real numbers 28
  • 28. Register Assignment ī‚´A register may be assigned value only within: ī‚´ a procedural statement ī‚´ a user-defined sequential primitive ī‚´ a task, or ī‚´ a function. ī‚´A reg object may never by assigned value by: ī‚´ a primitive gate output or ī‚´ a continuous assignment 29
  • 29. Register Examples ī‚´ reg a, b, c; ī‚´ reg [15:0] counter, shift_reg; ī‚´ reg [8:4] flops; ī‚´ integer sum, difference; 30
  • 30. Strings ī‚´ No explicit data type ī‚´ Must be stored in reg whose size is 8*(num. of characters) ī‚´ reg [255:0] buffer; //stores 32 characters 31
  • 31. Constants (Paramters) ī‚´ Declaration of parameters ī‚´ parameter A = 2’b00, B = 2’b01, C = 2’b10; ī‚´ parameter regsize = 8; ī‚´ reg [regsize - 1:0]; /* illustrates use of parameter regsize */ 32
  • 32. Operators ī‚´Arithmetic (binary: +, -,*,/,%*); (unary: +, -) ī‚´Bitwise (~, &,|,^,~^,^~) ī‚´Reduction (&,~&,|,~|,^,~^,^~) ī‚´Logical (!,&&,||,==,!=,===,!==) ī‚´Relational (<,<=,>,>=) ī‚´Shift (>>,<<) ī‚´Conditional ? : ī‚´Concatenation and Replications {A,B} {4{B}} * Not supported for variables 33
  • 33. Expression Bit Widths ī‚´Depends on: ī‚´widths of operands and ī‚´types of operators ī‚´Verilog fills in smaller-width operands by using zero extension. ī‚´Final or intermediate result width may increase expression width 34
  • 34. Expression Bit Widths ī‚´ Unsized constant number- same as integer (usually 32 bits) ī‚´ Sized constant number - as specified ī‚´ x op y where op is +, -, *, /, %, &, |, ^, ^~: ī‚´ Arithmetic binary and bitwise ī‚´ Bit width = max (width(x), width(y)) 35
  • 35. Expression Bit Widths (continued) ī‚´ op x where op is +, - ī‚´ Arithmetic unary ī‚´ Bit width = width(x) ī‚´ op x where op is ~ ī‚´ Bitwise negation ī‚´ Bit width = width(x) 36
  • 36. Expression Bit Widths (continued) ī‚´ x op y where op is ==, !==, ===, !===, &&, ||, >, >=, <, <= or op y where op is !, &, |, ^, ~&, ~|, ~^ ī‚´ Logical, relational and reduction ī‚´ Bit width = 1 ī‚´ x op y where op is <<, >> ī‚´ Shift ī‚´ Bit width = width(x) 37
  • 37. Expression Bit Widths (continued) ī‚´ x ? y : z ī‚´Conditional ī‚´Bit width = max(width(y), width(z)) ī‚´{x, â€Ļ, y} ī‚´Concatenation ī‚´Bit width = width(x) + â€Ļ + width(y) ī‚´{x{y, â€Ļ, z}} ī‚´Replication ī‚´Bit width = x * (width(y) + â€Ļ + width(z)) 38
  • 38. Expressions with Operands Containing x or z ī‚´Arithmetic ī‚´If any bit is x or z, result is all x’s. ī‚´Divide by 0 produces all x’s. ī‚´Relational ī‚´If any bit is x or z, result is x. ī‚´Logical ī‚´== and != If any bit is x or z, result is x. ī‚´=== and !== All bits including x and z values must match for equality 39
  • 39. Expressions with Operands Containing x or z ī‚´Bitwise ī‚´Defined by tables for 0, 1, x, z operands. ī‚´Reduction ī‚´Defined by tables as for bitwise operators. ī‚´Shifts ī‚´z changed to x. Vacated positions zero filled. ī‚´Conditional ī‚´If conditional expression is ambiguous (e.g., x or z), both expressions are evaluated and bitwise combined as follows: f(1,1) = 1, f(0,0) = 0, otherwise x. 40
  • 40. Simulation Time Scales ī‚´Compiler Directive `timescale <time_unit> / <time_precision> ī‚´time_unit - the time multiplier for time values ī‚´time_precision - minimum step size during simulation - determines rounding of numerical values ī‚´Allowed unit/precision values: {1| 10 | 100, s | ms | us | ns | ps} 41
  • 41. Simulation Time Scales (continued) ī‚´Example: `timescale 10ps / 1ps nor #3.57 (z, x1, x2); nor delay used = 3.57 x 10 ps = 35.7 ps => 36 ps ī‚´Different timescales can be used for different sequences of modules ī‚´The smallest time precision determines the precision of the simulation. 42
  • 42. Behavioral Constructs ī‚´Concurrent communicating behaviors => processes same as behaviors ī‚´Two constructs ī‚´initial - one-time sequential activity flow - not synthesizable but good for testbenches ī‚´Always - cyclic (repetitive) sequential activity flow ī‚´Use procedural statements that assign only register variables (with one exception) 43
  • 43. Behavioral Constructs (continued) ī‚´Continuous assignments and primitives assign outputs whenever there are events on the inputs ī‚´Behaviors assign values when an assignment statement in the activity flow executes. Input events on the RHS do not initiate activity - control must be passed to the statement. 44
  • 44. Behavioral Constructs (continued) ī‚´Body may consist of a single statement or a block statement ī‚´A block statement begins with begin and ends with end ī‚´Statements within a block statement execute sequentially ī‚´Behaviors are an elaborate form of continuous assignments or primitives but operate on registers (with one exception) rather than nets 45
  • 45. Behavioral Constructs - Example ī‚´ Initial: īē Always: initial always begin begin one = 1; F1 = 0, F2 = 0; two = one + 1; # 2 F1 = 1; three = two + 1; # 4 F2 = 0; four = three + 1; # 2 F1 = 1; five = four + 1; # 4; end end ī‚´ What are results of each of the above? 46
  • 46. Procedural Assignments ī‚´Types ī‚´= blocking assignment ī‚´assign = continuous assignment ī‚´<= non-blocking assignment ī‚´Assignments (with one exception) to: ī‚´reg ī‚´integer ī‚´real ī‚´realtime ī‚´time 47
  • 47. Procedural Assignments - Some Rules ī‚´ Register variable can be referenced anywhere in module ī‚´ Register variable can be assigned only with procedural statement, task or function ī‚´ Register variable cannot be input or inout ī‚´ Net variable can be referenced anywhere in module ī‚´ Net variable may not be assigned within behavior, task or function. Exception: force â€Ļ release ī‚´ Net variable within a module must be driven by primitive, continuous assignment, force â€Ļ release or module port 48
  • 48. Procedural Timing, Controls & Synchronization ī‚´ Mechanisms ī‚´ Delay Control Operator (#) ī‚´ Event Control Operator (@)* ī‚´ Event or ī‚´ Named Events – not used much ī‚´ wait construct 49 *Ignored by FPGA express unless a synchronous trigger that infers a register
  • 49. Procedural Timing, Controls & Synchronization ī‚´ Delay Control Operator (#) ī‚´ Precedes assignment statement - postpones execution of statement ī‚´ For blocking assignment (=), delays all statements that follow it ī‚´ Blocking assignment statement must execute before subsequent statements can execute. ī‚´ Example: always @(posedge clk), #10 Q = D; 50
  • 50. Procedural Timing, Controls & Synchronization ī‚´ Event Control Operator (@)* ī‚´Synchronizes the activity flow of a behavior to an event (change) in a register or net variable or expression ī‚´Example 1: @ (start) RegA = Data; ī‚´Example 2: @(toggle) begin â€Ļ @ (posedge clk) Q = D; â€Ļ end ī‚´*Ignored by FPGA express unless a synchronous trigger that infers a register 51
  • 51. Procedural Timing, Controls & Synchronization ī‚´ Event or - allows formation of event expression ī‚´ Example: always @ (X1 or X2 or X3) assign Y = X1 & X2 | ~ X3; ī‚´ All RHS variables in sensitivity list and no unspecified conditional results => combinational logic 52
  • 52. Procedural Timing, Controls & Synchronization ī‚´ Meaning of posedge: 0 -> 1, 0 -> x, x -> 1 ī‚´ Special Example: always @ (set or reset or posedge clk) begin if (reset == 1) Q = 0; else if (set == 1) Q = 1; else if (clk == 1) Q = data; end // Does this work correctly? Why or why not? 53
  • 53. Procedural Timing, Controls & Synchronization (FIO) ī‚´wait Construct ī‚´Suspends activity in behavior until expression following wait is TRUE ī‚´Example: always begin a = b; c = d; wait (advance); end 54
  • 54. Blocking Assignments ī‚´Identified by = ī‚´Sequence of blocking assignments executes sequentially ī‚´Example: always @(posedge clk) begin b = 0; c = 0; b = a + a; c = b + a; d = c + a; end 55
  • 55. Non-Blocking Assignments ī‚´ Identified by <= ī‚´ Sequence of non-blocking assignments executes concurrently ī‚´ Example 1: always @(posedge clk) begin b <= 0; c <= 0; b <= a + a; c <= b + a; d <= c + a; end /*Calculates b = 2a, c = b + a, d <= c + a. All values used on RHS are those at posedge clock. Note that there are two assignments to b and c. Only the last one is effective. */ 56
  • 56. Blocking Assignments - Inter-Assignment Delay ī‚´Delays evaluation of RHS and assignment to LHS ī‚´ Example: always @(posedge clk) begin b = 0; c = 0; b = a + a; // uses a at posedge clock #5 c = b + a; // uses a at posedge clock + 5 d = c + a; // uses a at posedge clock + 5 end /*c = 2 a(at posedge clock)+ a(at posedge clock + 5) d = 2 a(at posedge clock) + 2 a(at posedge clock + 5)*/ 57
  • 57. 58 īēDelays assignment to LHS and subsequent statements, not evaluation of RHS īēExample: always @(posedge clk) begin b = 0; c = 0; b = a + a; // uses a at posedge clock c = #5 b + a; // uses a at posedge clock d = c + a; // uses a at posedge clock + 5 end /* c = 3 a(at posedge clock) d = 3a (at posedge clock)+ a (at posedge clock + 5)*/ Blocking Assignment - Intra-Assignment Delay
  • 58. Non-Blocking Assignment - Inter- Assignment Delay ī‚´Delays evaluation of RHS and assignment to LHS ī‚´Delays subsequent statements ī‚´ Example: always @(posedge clk) begin b <= 0; c <= 0; b <= a + a; // uses a at posedge clock #5 c <= b + a; // uses b and a at posedge clock + 5 d <= c + a; // uses a at posedge clock + 5 end /*c = b(at posedge clock + 5) + a(at posedge clock + 5) d = c(at posedge clock + 5) + a (at posedge clock +5) */ 59
  • 59. Non-Blocking Assignment - Intra- Assignment Delay ī‚´Delays only assignment to LHS ī‚´ Example: always @(posedge clk) begin b <= 0; c <= 0; b <= a + a; // uses a at posedge clock c <= #5 b + a; // uses a and b at posedge clock d <= c + a; // uses a and c at posedge clock end /* Calculates *c(posedge clock + 5) = b(at posedge clock) + a(at posedge clock); d(posedge clock) = c(at posedge clock) + a (at posedge clock) */ 60
  • 60. Activity Control 61 Overview ī‚§ Constructs for Activity Control â€ĸ Conditional operator â€ĸ case statement â€ĸ if â€Ļ else statement â€ĸ Loops : repeat, for, while, forever â€ĸ disable statement â€ĸ fork â€Ļ join statement ī‚§ Tasks and Functions
  • 61. Conditional Operator ī‚´ ? â€Ļ : ī‚´ Same as for use in continuous assignment statement for net types except applied to register types ī‚´ Example: always@(posedge clock) Q <= S ? A : B //combined DFF and 2-to-1 MUX 62
  • 62. case Statement ī‚´ Requires complete bitwise match over all four values so expression and case item expression must have same bit length ī‚´ Example: always@(state, x) begin reg[1:0] state; case (state) 2’b00: next_state <= s1; 2’b01: next_state <= s2; 2’b10: if x next_state <= s0; else next_state <= s1; end default next_state = 1’bxx; endcase end 63
  • 63. casex Statement ī‚´Requires bitwise match over all but positions containing x or z; executes first match encountered if multiple matches. ī‚´Example: always@(code) begin casex (code) 2’b0x: control <= 8’b00100110; //same for 2’b0z 2’b10: control <= 8’b11000010; 2’b11: control <= 8’b00111101; default control <= 8b’xxxxxxxx; endcase end 64
  • 64. casez Statement ī‚´ Requires bitwise match over all but positions containing z or ? (? is explicit don’t care); executes first match encountered if multiple matches. ī‚´ Example: reg [1:0] code; always@(code) begin casez (code) 2’b0z: control <= 8’b00100110; 2’b1?: control <= 8’b11000010; default control <= 8b’xxxxxxxx; endcase end 65
  • 65. Conditional (if â€Ļ else) Statement Example always@(a or b or c) begin if (a == b) begin q <= data; stop <= 1’b1; end else if (a > b) q <= a; else q <= b; end end end 66
  • 66. Conditional (if â€Ļ else) Statement (continued) ī‚´Must be careful to define outcome for all possible conditions – failure do do so can cause unintentional inference of latches! ī‚´else is paired with nearest if when ambiguous - use begin and end in nesting to clarify. ī‚´Nested if â€Ļ else will generate a “serial” or priority like circuit in synthesis which may have a very long delay - better to use case statements to get “parallel” circuit. 67
  • 67. for Loop Example ī‚´ Example: initial integer r, i; begin r = 0; for (i = 1; i <= 7; i = i + 2) begin r[i] = 1; end end 68
  • 68. while Loop Example initial begin r = 0; i = 0; while (i <= 7) begin r[i] = 1; i = i + 2; end end 69
  • 69. forever Loop Example initial begin clk = 0; forever begin #50 clk = 1; #50 clk = 0; end end ī‚´ Usually used in testbenches rather than for synthesized logic. 70
  • 70. Tasks ī‚´Declared within a module ī‚´Referenced only by a behavior within the module ī‚´Parameters passed to task as inputs and inouts and from task as outputs or inouts ī‚´Local variables can be declared ī‚´Recursion not supported although nesting permitted (nested copies of variables use same storage) ī‚´See Fig. 7.43 p. 226 of [5]for rules 71
  • 71. Task Example task leading_1; input [7:0] data_word; output [2:0] position; reg [7:0] temp; reg [2:0] position; begin temp = data_word; position = 3'b111; while (!temp[7]) @(posedge clock) begin temp = temp << 1; position = position - 1; end end endtask // Code is not synthesizable 72
  • 72. Functions ī‚´Implement combinational behavior ī‚´No timing controls or tasks which implies no while ī‚´May call other functions with no recursion ī‚´Reference in an expression, e.g. RHS ī‚´No output or inout allowed ī‚´Implicit register having name and range of function 73
  • 73. Function Example function [2:0] leading_1; input [7:0] data_word; reg [7:0] temp; begin temp = data_word; leading_1 = 3'b111; while (!temp[7]) begin temp = temp << 1; leading_1 = leading_1 - 1; end end endfunction ī‚´ Is the above code synthesizable? 74 No
  • 74. Compiler Directives ī‚´Useful for controlling what is synthesized and the resulting logic ī‚´Warning: Not recognized by other compilers – therefore reduce code portability ī‚´Examples: ī‚´// synopsys translate_off Code here describes something that is not to be synthesized such at a simulation testbench - can contain non-synthesizable constructs such as delays) // synopsys translate_on 75
  • 75. Compiler Directives (Continued) ī‚´Examples: ī‚´// synopsys parallel_case Forces generation of multiplexer-like structure instead of priority structure when included after case declaration ī‚´// synopsys full_case Indicates that all cases have been considered when included in case declaration; when used, no default statement needed and latches will not be inferred can be used in combination with parallel case: case (state) // synopsys parallel_case full_case 76
  • 76. Testbench Approach ī‚´ Use Verilog module to produce testing environment including stimulus generation and/or response monitoring 77 UUT Module Stimulus Response Testbench Module
  • 77. Stimulus Generation Example `timescale 1ns /1ns module com_test_bench_v; reg[8:0] stim; wire[3:0] S; wire C4; adder_4_b_v a1(stim[8:5], stim[4:1], stim[0], S, C4); //Continued on next slide endmodule 78
  • 78. Stimulus Generation Example (Continued) //Generate stimulus initial begin stim = 9'b000000000; #10 stim = 9'b111100001; #10 stim = 9'b000011111; #10 stim = 9'b111100010; #10 stim = 9'b000111110; #10 stim = 9'b111100000; #10 stim = 9'b000011110; #10 $stop; end 79