Hardware Modeling:: Switch Level Modeling Gate (Structural) Level Modeling Behavioral Modeling
Hardware Modeling:: Switch Level Modeling Gate (Structural) Level Modeling Behavioral Modeling
Hardware modeling:
Switch level modeling
Gate (structural) level
modeling
Behavioral modeling
Module instantiation
Assignments
Procedural blocks
Conditional and loop
constructs
Timing control
Compiler directives and system
tasks
Tasks and functions
Basic testbench
Tools we will use
3
Time-based
🞑 evaluate the entire circuit on a periodic
basis
🞑 SPICE
Cycle-based
simulations
🞑 Evaluate only changes in the circuit state
🞑 Modelsim, NC-Verilog (Cadence), VCS (Synopsys)
Modeling concurrency
5
execution is
statements
always @(posedge
// Potential clock) (depending
race condition a = b; on simulator implementation)
// Bad code:
always Two concurrent
@(posedge clock) always
b = blocks with blocking statements
a;
always @(posedge clock) a = b;
// Potential race condition (depending on simulator implementation)
always @(posedge clock) b = a;
// Good code: Two concurrent always blocks with non-blocking statements
//
// Eliminate
Good code: theTwo
race, values of always
concurrent registers a andwith
blocks b arenon-blocking
swapped correctly
always @(posedge clock)
statements a <= b;
always @(posedge
// Eliminate clock)
the race, b <= a; a and b are swapped correctly
values of registers
always @(posedge clock) a <= b;
always @(posedge clock) b <= a;
Switch Level Modeling
9
Unidirectional
switches: nmos, pmos,
cmos
Bidirectional
switches: tranif, tran,
pullup, pulldown
Tri-state buffers:
bufif0, bufif1
endmodule
Gate Level Modeling
1
4
Gate level or Structural modeling describes
hardware functionality of a device in terms
of gates
Verilog provides basic logical functions as
predefined primitives. You do not have
to define this basic functionality.
Most ASIC libraries are developed using
primitives. Outcome of the synthesis process
is gate-level netlist.
Built-in primitives
1
5
(A => O) = 2:2.1:2.2
*> signifies full connections. All the inputs connected to all the
outputs.
is equivalent
to (a => q) =
12:15:18;
(b => q) =
12:15:18;
(a => qb)
Parameters in specify blocks
2
7
The keyword module noror (O, A, B, C);
specparam output O;
input A, B, C;
declares parameters nor n1 (net1, A, B);
within a specify or o1 (O, C, net1);
specify
block. specparam ao = 2, bo = 3,
🞑 Must be declared co = 1;
(A => O) =
inside
ao; (B =>
specify blocks O) = bo; (C
🞑 Can only be used => O) = co;
endspecify
inside endmodul
specify blocks e
🞑 Cannot use
defparam to
Gate Level Modeling - Primitive
Instantiation
// Structural model of AND gate from two NANDS
module and_from_nand (X, Y, F);
input X, Y;
X
W F output F;
Y wire W;
endmodule
nand U1 (X,D,CLK) ;
Clk nand U2 (Y,X,CLK) ;
Y nand U3 (Q,Qb,X);
nand U4 (Qb,Q,Y);
Qb
endmodule
Strength modeling
2
9
Eight strength levels are used to resolve conflicts between
drivers of different strengths. The table below shows five
most useful ones.
If two signals with unequal strengths are driven on a
wire, the
stronger one wins
If two signals of equal strengths are driven on a wire, the
result is
unknown
Strength modeling
3
0
g1
Exampl a
e: (strong1, weak0) g1
buf y
(y,
bufa);
(pull1,
b supply0) g2 g2
(y,
a b);
b y Strength of y Comment
0 0 0 supply both gates will set y to 0 and supply strength has bigger
value than weak strength
0 1 1 pull g1 will set y to 0 with weak strength and g2 will set y to 1
with pull strength (pull strength is stronger than the weak
strength).
1 0 0 supply g1 will set y to 1 with strong strength and g2 will set y to 0
with supply strength (supply strength is stronger than the
strong strength)
1 1 1 strong g1 will set y to 1 with strong strength and g2 will set y to 1
with pull strength
Module Instantiation
3
1
Module is a basing building entity in Verilog
hardware modeling:
// Module declaration
module <name> (<port list>);
<port declarations;>
<parameters>;
<declaration of wires, regs and variables>;
<lower level instantiations>;
<assign statements>
<behavioral blocks>
<tasks and functions>
endmodule
Module ports
3
2
A module can have ports of 3 types:
Input: Internally, input ports must always be of the
type net. Externally, the inputs can be connected to
a variable which is a reg or net.
Output: Internally outputs can be of the type reg
or net. Externally, outputs must always be
connected to a net. They cannot be connected to a
reg.
Inout: Internally inout ports must always be of the
type net. Externally inout ports must always be
connected to net.
Module ports
3
3
Width matching: it is legal to connect
internal and external items of different
sizes when making inter- module port
connections. Warning will be issued when
the width differs.
declaration.
In named mapping, port order is independent of the
position.
module mod1 (out1, out2, in1, in2);
output out1, out2;
module testbench;
input in1, in2;
……
...
mod1 c1 (a,b,c,d);
endmodule // Positional mapping
mod1 c2 (.in2(d),.out1(a),.out2(b),.in1(c)); // Named mapping
mod1 c3 (a,,c,d); // One port left unconnected
…….
endmodule
Behavioural Modelling
3
5
Behavioural modelling provides means to describe the
system at a higher level of abstraction than switch- or
gate- level modelling
Behavioral model of a hardware block in Verilog is
described by specifying a set of concurrently active
procedural blocks.
for behavioral Behavioural FF description:
High-level
modeling. programming language constructs are
available inReset
Verilog - At every positive edge of Clock
If Reset is high
Set Q to the value of Data
Data Q Set Qb to the inverse of Data
Qb - Whenever reset goes low
Clock
Q is set to 0
Qb is set to 1
Register transfer level (RTL)
3
6
level
The register transfer level, RTL, is a design level
of abstraction. “RTL” refers to coding that uses a
subset of the Verilog language.
RTL is the level of abstraction below behavioral
and above
structural.
Events are defined in terms of clocks and certain
behavioral constructs are not used.
Some of Verilog constructs are not understood by
synthesizers. Each tool is different in the subset of
the language that it supports, but as time progresses
the differences become smaller.
The simplest definition of what is RTL is “any
code that is synthesizable”.
Assignments
3
7
Assignment is the basic mechanism for
getting values into nets and
registers. An assignment consists of two
parts, a left-hand side (LHS) and a
right-hand side (RHS), separated by
the equal sign (=).
The right-hand side can be any
expression that evaluates to a value.
The left-hand side indicates the variable
that the right-hand side is to be
assigned to.
Assignments can be either continuous or
procedural
Assignments
3
8
Continuous assignments drive values onto nets,
both vector and scalar. Left-hand side should be
net (vector or scalar).
Procedural assignments occur only within
procedures, such as always and initial statements.
LHS should be register or memory//Procedural
element. assignment
module la1;
//Continuous assignment
reg a;
module la (a,b,c,d);
wire b,c,d;
input b,c,d;
initial
output a;
begin
wire a;
a = b | (c
assign a
& d);
= b | (c &
end
d);
endmodule
endmodule
Continuous Assignments
3
9
Combinational logic can be modeled with
continuous assignments, instead of using gates
and interconnect nets.
In a continuous
wire out; assignment, the LHS is updated at
any change in the
assign out =a& RHS
b; expression,
// explicit after a specified
delay.wire inv = ~in; // implicit
Procedural assignments
4
1
LHS of a procedural assignment (PA) should be
register, real, integer, time variable, or memory
element. PA can not assign values to nets (wire
data types)
In the RHS has more bits than the LHS, the RHS is
truncated to mach the width of the LHS.
If the RHS has fewer bits, zeros are filled in the
MS bits of the register variable
endmodule
“always”
4
5
block
Always available for module clock_gen;
execution: reg clock;
always @(sensitivity-list)
begin // Initialize a clock at time
/ / statements zero
end initial
Can model both clock = 1’b0;
combinatorial and sequential
logic // Toggle clock every half
When at least one of the clock cycle
signals in the sensitivity list // Clock period = 50
changes, the always
always block executes #25 clock = ~clock;
through to the end
keyword. endmodule
The sensitivity list prevents the
always block from executing
again
until another change occurs
“always”
4
6
block
Combinatorial logic with always
block:
reg F; // Verilog reg, not a HW reg !!!
always @(a or b or c or d) // Verilog-95 requires complete sensitivity lists!
begin
F = ~((a & b) | (c & d));
end
Simulat Simulat
or or
Output Output
0 a= 0
x 10 a a=
=0 x
21 a = 10
1 a=
Blocking and Non-blocking
Procedural assignments
4
9
There are two types of procedural
assignment statements: blocking and
non-blocking.
……
OUT1 = IN1; // will be executed first
OUT2 = IN2;
…….
Procedural assignments: non-
5
3
blocking
A nonblocking assignment gets its name because it
evaluates the RHS expression of a statement at the
beginning of a time step and schedules the LHS
update to take place at the end of the time step.
begin :count1s
reg [7:0] tmp;
tmp = 8’b11111111; count = 0;
initial control variable
condition check assignment
for (tmp = rega; tmp; tmp = tmp >>
1)
if (tmp]) count = count + 1;
end
Looping statements –
6
5
“repeat”
“repeat” loop statement repeats a
statement (or block of statements)
specified number of times
integer cnt;
initial
cnt = 0;
repeat (256)
begin
if (a)
shifta = shifta << 1;
cnt = cnt + 1;
end
Looping statements –
6
6
“forever”
Continuously executes the statements (or block of
statements) until the $finish task is encountered
“forever” loop is equivalent to a while(1) loop
Should be used only with timing control or disable
statement ! (otherwise, the Verilog simulator would
execute this statement infinitely without advancing
simulation time)
// typical use – clock generation
reg clock;
initial
begin
clock = 1’b0;
forever #10
clock = ~clock;
end
Timing
6
7
control
Timing control constructs are used to
advance simulation time
Timing control can be:
Delay-based - specifies the time duration
between when the statement is
encountered and when it is executed
(delays are specified by the symbol “#”)
Event-based - simulation waits for occurrence
of some event, before executing a statement,
specified by “@” symbol
Level-sensitive event - the execution can be
delayed until a condition becomes true
Delay based timing control
6
8
Examples:
a) Regular:
#10 rega = regb; // assigns a delay of 10 time units. After 10 time units,
// rega will be assigned value of regb, sampled at that time.
b) Intra-assignment:
regy = #8 regx ; // regx will be sampled now, but regy will be assigned
//value after 8 time units
c) Zero-delay control:
initial x = 0;
…..
initial #0 x = 1; //Schedules this event as last event in current simulation time.
// (No sequence guaranteed, if there are several assignments)
Event based timing
6
9
control
An event is the change in the value on a register
or a net, or edge transition. Events serve a
trigger for execution of a statement or a block of
statements
A “@” symbol is used to specify an event
control.
The keyword “or ” is used to specify multiple
triggers.
A named event is declared by the keyword
event. A named event is triggered by the
symbol “-> ”
The keyword “wait” is used for level-
sensitive constructs
An event does not hold any data
Event based timing
7
0
control
@(clock) q = d; // q = d is executed each time
// clock changes value
begin
if (reset)
q = 1’b0; // if reset signal is high, set q to 0
else
if // if clock is high, latch output
end (cloc
k)
q = d;
Event based timing
7
2
control
event my_frame; // Define an event called my_frame
always @(my_frame)
data_buff = 0;
endmodule
Tasks and functions
8
1
Often it is required to implement
the same functionality at many
places in a design.
Rather than replicating the code, a routine
should be invoked each time when the
same functionality is called.
Tasks and functions allow the designer to
abstract Verilog code that is used at
many places in the design.
Tasks and functions are included in the
design hierarchy. Like named blocks,
tasks and functions can be addressed by
means of hierarchical names
Tasks and functions
8
2
Both tasks and functions must be
defined in a module and are local
to the module.
Tasks and functions contain behavioural
statements only
Tasks and functions do not contain always
or initial
statements
Tasks or functions cannot have wires
In order to be able to call a task or
function from other modules, all
variables used inside the task or
function should be in its port list
Tasks and functions
Functions Tasks
A function can call to another A task can call another task or function
function but not another task
Functions always execute in Tasks may execute in non-zero
0 simulation time simulation time
Functions must not contain any delay, Tasks may contain any delay, event, or
event, or timing control statement timing control statement
Functions must have at least one Tasks may have zero or more
input argument. They can have more arguments of type input, output,
than one input. or inout
Functions always return a single Tasks do not return with a value but
value. They cannot have output or can pass multiple values through
inout argument output and inout arguments
Tasks and functions
8
4
// Example for function
function [31:0] factorial;
input [3:0] operand;
reg [3:0] index;
begin
factorial = operand ? 1 : 0;
for (index = 2; index <= operand; index = index +
1) factorial = index * factorial;
end
endfunction
// Calling
a function
for (n = 2;
n <= 9; n =
n+1)
begin
$display ("Partial result n=%d result=%d", n,
result); result = n * factorial(n) / ((n * 2) + 1);
end
Tasks and functions
8
5
//Example of Task Definition:
task light;
output color;
input [31:0] tics;
begin
repeat (tics)
@(posedge
clock);
color = off;
//
Invokin
g the
task in
the
module
System tasks
8
6
All Verilog simulators support system tasks
used for some routine operations, like print,
stop/interrupt simulation, monitor variables,
dumping signal values etc.