Professional Documents
Culture Documents
HDL Lecture4
HDL Lecture4
Languages
Verilog
Modeling concept
Basic concept
Behavioral modeling
Data flow modeling
Gate level modeling
Switch level modeling
Testbench creation
Tasks and functions
Verilog
Modeling concept
Basic concept
Behavioral modeling
Data flow modeling
Gate level modeling
Switch level modeling
Testbench creation
Tasks and functions
Behavioral
The highest level of abstraction provided by Verilog HDL. A module is
implemented in terms of the desired design algorithm
Data Flow
At this level the module is designed by specifying the data flow
Gate Level
The module is implemented in terms of logic gates and interconnections
between these gates
Switch Level
This is the lowest level of abstraction provided by Verilog. A module can
be implemented in terms of switches, storage nodes and the
interconnections between them
Top-Down Design
Define the top-level block
Identify the sub-blocks
necessary to build the top-level
block
Sub-blocks are formed by leaf
cells which cannot be further
divided
Bottom-up Design
Build macro cells using leaf
cells
Move up the hierarchy until the
top-level is reached
Verilog
Modeling concept
Basic concept
Behavioral modeling
Data flow modeling
Gate level modeling
Switch level modeling
Testbench creation
Tasks and functions
Module
Basic building block
Only user-declared object type
Element or a collection of lower level design blocks
Comments
Numbers
Strings
Identifier
Operators
Comments
Syntax Example
One line comment- //comment sum=a+b // This is a one line
Multiple line comment- /*comment*/ comment
d[1]=1’b0; /* d[0]=1’b1;
This is a multiple-line
comment block */
Identifiers
Names given to objects
Cannot start with a number or dollar sign ($)
Can start with alphabetic character or underscore
x or z values
x – unknown value
z – high impedance value
16`h536x // This is a 16-bit hexadecimal value with 4
least significant bits unknown
Negative numbers
Number with a minus sign before the size of a
constant number
-10`d9
Synopsys University Courseware
Copyright © 2014 Synopsys, Inc. All rights reserved.
Hardware Description Languages
Lecture - 4
Developed By: Vazgen Melikyan
15
Lexical Conventions: Numbers (3)
Strings
Any characters enclosed by double quotes
“Verilog HDL Concepts”
Nets
Vectors
Registers
Arrays
Example
wire net1;
wire net2, net4;
supply0 vss;
Example
reg register1;
reg register2,
register3;
Array
Definition syntax
<data_type_spec> {size} <variable_name> {array_size}
Reference syntax
<variable_name> {array_reference} {bit_reference}
Example
Parameters
Parameters are unsigned integer constants by default
Must be assigned when declared. Width defaults are
enough for the value assigned
May be declared signed (but many tools reject it)
May be declared real (but not synthesizable)
May be typed by vector index range
Declaration allowed anywhere in module, but local
parameter proffered in body
Operators
Unary operators
stand before the operand
Binary operators
stand between two operands
Ternary operators
have two separate operators that separate three operands
Unary +-!~
Multiply, Divide, Modulus */%
Add, Subtract +-
Shift << >>
Relational < <= > >=
Equality == != === !==
Reduction &, ~&
^, ^~
|, ~|
Logical &&
||
Lowest Precedence
Conditional ?:
Synopsys University Courseware
Copyright © 2014 Synopsys, Inc. All rights reserved.
Hardware Description Languages
Lecture - 4
Developed By: Vazgen Melikyan
26
Sequential Blocks
Sequential Blocks
Statements in a sequential block are processed in the
order they are specified
A statement is executed only after its preceding
statement completes execution (except for non-
blocking assignments)
If delay or event control is specified it is relative to the
simulation time, i.e. execution of the previous
statement
Example
reg x, y;
reg [1:0] z, w;
initial
fork
x = 1'b0;
#5 y = 1'b1; x completes at simulation time 0
y completes at simulation time 5
#10 z = {x, y}; z completes at simulation time 10
#20 w = {y, x}; w completes at simulation time 20
join
Verilog
Modeling concept
Basic concept
Behavioral modeling
Data flow modeling
Gate level modeling
Switch level modeling
Testbench creation
Tasks and functions
Blocks
Always
Event-based timing control
Branch
Case, casex, casez
Syntax Example
always operation_name module pulse;
reg clock;
initial clock = 1'b0; // start the clock at 0
always #10 clock = ~clock; /* toggle every 10 time
units*/
endmodule
Syntax Example
@(event) op_name @ (clock) a = b;
when the clock changes value, execute a = b
@ (negedge clock) a = b;
when the clock change to a 0, execute a=b
a = @(posedge clock) b;
evaluate b immediately and assign to a on a
positive clock edge.
Syntax Example
if (conditional_expression) if (x!=1) a=2; else a=5
statement{ else statement}
Syntax Example
case(op) case(S3)
op1:operation1 00:A=00
op2:operation2 01:A=01
op3:operation3 10:A=10
op4:operation4 11:A=11
default:operation5 default:operation5
endcase endcase
Executed at time 25
#10 reg_b[15:13] = {x, y, z}
count = count + 1;
//Assignment to an integer (increment)
end
x = 0; y = 1; z = 1;
count = 0;
reg_a = 16'b0;
reg_b = reg_a;
Executed at time 15
Verilog
Modeling concept
Basic concept
Behavioral modeling
Data flow modeling
Gate level modeling
Switch level modeling
Testbench creation
Tasks and functions
1. When signals in1 and in2 go high at time 20, out goes to a high 10 time units later
(time = 30).
2. When in1 goes low at 60, out changes to low at 70.
3. However, in1 changes to high at 80 but it goes down to low before 10 time units have
elapsed.
4. Hence at the time of recomputation, 10 units after time 80, in1 is 0. Thus, out gets the
value 0. A pulse of width less than the specified assignment delay is not propagated
to the output.
in1
in2
xxxxx
out
time 10 20 30 60 70 80 85
//same as
wire out;
assign #10 out = in1 & in2
//Net Delays
wire # 10 out;
assign out = in1 & in2;
Syntax Example
conditional_expression ? module multiplexor4_1 (out, in1, in2,
true_expression : in3, in4, cntrl1, cntrl2);
false_expression output out;
input in1, in2, in3, in4, cntrl1, cntrl2;
assign out = cntrl1 ? (cntrl2 ? in4 : in3)
:(cntrl2 ? in2 : in1);
endmodule
Verilog
Modeling concept
Basic concept
Behavioral modeling
Data flow modeling
Gate level modeling
Switch level modeling
Testbench creation
Tasks and functions
And/Or
Buf/not
Bufif/notif
UDP
0 0 0 0 0 0 0 1 x x 0 0 1 x x
1 0 1 x x 1 1 1 1 1 1 1 0 x x
x 0 x x x x x 1 X x x x x x x
z 0 x x x z x 1 X X z x x x x
0 1 1 1 1 0 1 0 x x 0 1 0 x x
1 1 0 x x 1 0 0 0 0 1 0 1 x x
x 1 x x x x x 0 x x x x x x x
z 1 x x x z x 0 x x z x x x x
in out in out
control control
bufif1 notif1
in out out
control control
BUFIF1 0 1 x z BUFIF0 0 1 x z
0 z 0 L L 0 0 z L L
in 1 z 1 H H in 1 1 z H H
x z x x x x x z x x
z z x x x z x z x x
control control
NOTIF1 0 1 x z NOTIF0 0 1 x z
0 z 1 H H 0 1 z H H
in 1 z 0 L L in 1 0 z L L
x z x x x x x z x x
z z x x x z x z x x
Delay specifications
One delay is specified – its value is used for all
transitions
Two delays are specified – they refer to rise and fall
delay values respectively
Three delays are specified – they refer to rise, fall and
turn-off delay values respectively
Default value is zero
..........(functionality omitted)...................
specify
specparam tALL=10, tR=20, tF=21;
(A,B,C *>Qbus) = tALL;
(CLOCK *> Qbus) = (tR, tF);
endspecify
endmodule
Descriptions
ports provide the interface by which a module can communicate with its environment
Types
input, output or inout (implicitly all are declared as wire in Verilog)
Port Data Types
input or inout ports cannot be of type reg, because reg variables store values
input ports should only reflect the changes of the external signals they are connected to
Ports
Inputs
Internally, input ports must always be of the type net
Externally, the inputs can be connected to a variable which is reg or
net
Outputs
Internally, output ports can be of the type reg or net
Externally, the outputs must always be connected to a net
Inouts
Internally, inout ports must always be of the type net
Externally, inout ports must always be connected to a net
Connections
By order
By name
Example
module TOP;
reg data_in,clock,resetn;
wire data_out;
endmodule
in2
x
out
in3
y
in4
z
cntrl1
cntrl2
Verilog
Modeling concept
Basic concept
Behavioral modeling
Data flow modeling
Gate level modeling
Switch level modeling
Testbench creation
Tasks and functions
MOS switch
nmos, rnmos, pmos, rpmos, cmos, rcmos
Bidirectional pass switch
tran, rtran, tranif1, rtranif1, tranif0, rtranif0
Switch-level net
trireg
Power sources
pullup, pulldown
Synopsys University Courseware
Copyright © 2014 Synopsys, Inc. All rights reserved.
Hardware Description Languages
Lecture - 4
Developed By: Vazgen Melikyan
83
Use of Trireg: Example
pullup(vdd);
trireg (small) #(3,3,10) TriS;
trireg (medium) #(6,7,30) TriM;
trireg (large) #(15,16,50) TriL;
// NMOS network:
rnmos #1 (TriM, TriS, vdd); // input has no effect
rnmos #1 (TriS, TriL, vdd); // input controls output
rnmos #1 (TriM, TriL, vdd); // connection on output
Verilog
Modeling concept
Basic concept
Behavioral modeling
Data flow modeling
Gate level modeling
Switch level modeling
Testbench creation
Tasks and functions
initial begin
a = 0; // executed at simulation time 0
#10 b = 2; // executed at simulation time
10
#15 c = a; // ... at time 25
#b c = 4; // ... at time 27
b=5; // ... at time 27
end
Looping constructs
While
For
Repeat
Forever
cup = cup + 1;
end
end
endmodule
address = address + 1;
$display("%d", mem16_256[index]);
end
end
endmodule
Synopsys University Courseware
Copyright © 2014 Synopsys, Inc. All rights reserved.
Hardware Description Languages
Lecture - 4
Developed By: Vazgen Melikyan
92
Looping Constructs: Repeat
Syntax Example
repeat (conditional) statement module comply;
int count;
initial begin
count = 128;
repeat (count) begin
$display("%d seconds to comply", count);
count = count - 1;
end
end
endmodule
Syntax Example
forever statement reg clock;
initial begin
clock = 1'b0;
forever #5 clock = ~clock;
end
module TEST;
// variables
reg [3:0] A, B;
reg CIN;
wire [3:0] SUM;
wire cout;
// instantiate the 4-bit ripple carry full adder and
call it ADDER_TEST
fulladder_4bit ADDER_TEST (SUM, COUT, A, B, CIN);
// signal monitoring
Initial
begin
$monitor ($time, “ A= %b, B= %b, CIN= %b, SUM=%b,
COUT=%b\n”, A, B, CIN, SUM, COUT);
end
// input signals
initial
0 A= 0000, B= 0000, CIN= 0, SUM=0000, COUT=0
begin
A=4’d0; B=4’d0; CIN=1’b0; 5 A= 0011, B= 0100, CIN= 0, SUM=0111, COUT=0
VCS
#5 A=4’d3; B=4’d4; 10 A= 0010, B= 0101, CIN= 0,SUM=0111,COUT=0
Simulation
#5 A=4’d2; B=4’d5; 15 A= 1001, B= 1001, CIN= 0,SUM=0010,COUT=1
#5 A=4’d9; B=4’d9; 20 A= 1010, B= 1111, CIN= 0,SUM=1001,COUT=1
#5 A=4’d10; B=4’d15;
#5 A=4’d10; B=4’d5; CIN=1’b1; 25 A= 1010, B= 0101, CIN= 1,SUM=0000,COUT=1
#5 A=4’d3; B=4’d4; 30 A= 0011, B= 0100, CIN= 1,SUM=1000,COUT=0
end
endmodule
Verilog
Modeling concept
Basic concept
Behavioral modeling
Data flow modeling
Gate level modeling
Switch level modeling
Testbench creation
Tasks and functions
calc_parity = ^ address
end
Tasks and functions are used to define common Verilog functionality that is
used in many places in the design.
Tasks can take any number of input, inout or output arguments. Delay,
event or timing control constructs are permitted in tasks. Tasks can enable
other tasks and functions.
Functions are used when exactly one return value is required and at least
one input argument is specified. Delay, event or timing control constructs
are not permitted in functions. Functions can invoke other functions but
cannot invoke other tasks.
Tasks and functions are included in a design hierarchy and can be
addressed by hierarchical name referencing.
A register with name as the function name is declared implicitly when a
function is declared. The return value of the function is passed back in this
register.
Synopsys University Courseware
Copyright © 2014 Synopsys, Inc. All rights reserved.
Hardware Description Languages
Lecture - 4
Developed By: Vazgen Melikyan
107
Synopsys University Courseware
Copyright © 2014 Synopsys, Inc. All rights reserved.
Hardware Description Languages
Lecture - 4
Developed By: Vazgen Melikyan
108