Modeling Styles and Testbenching in Verilog HDL: Dr. Belgacem Ben Youssef
Modeling Styles and Testbenching in Verilog HDL: Dr. Belgacem Ben Youssef
Modeling Styles and Testbenching in Verilog HDL: Dr. Belgacem Ben Youssef
1
Learning Objectives
2
Modeling Styles in Verilog
module_name instance_name(.port_name([expression])
{, .port_name([expression])});
Top down: In top-down design, the top level block is defined and then sub-blocks
necessary to build the top level block are identified.
Bottom up: Here the building blocks are first identified and then combined to build the
top level block.
NOTE:
1. The output of 3-state
gates can be connected
together to form a
common output line. To
identify such connections,
module muxtri(A,B,select,out);
Verilog HDL uses the
input A,B,select;
keyword tri (for tri-state)
output OUT;
to indicate that the output
tri OUT;
has multiple drivers.
bufif1 g1(OUT,A,select);
2. The above circuit bufif0 g2(OUT,B,select);
represents a 2-to-1 endmodule
multiplexer (see the code
on the right).
Logic Simulation
Logic simulation is a
fast, accurate
method of analyzing
a circuit to see and
evaluate its
waveforms.
Gate Delays in Verilog
`timescale 1 ns/1 ns 1
X
0
module And2(X, Y, F);
1
Y
input X, Y; 0
output F; time
reg F;
10 20 30 (ns)
Testbench
X_s
procedure
X CompToTest
Y_s F_s
Y (And2) F
endmodule
Testbench - 3
1
X_s
0
1
Y_s
0
1
F_s
0
time
10 20 30 (ns)
Component Instantiations – Combinational
Circuits
X
F X F
Circuit – A connection of modules Y
Also known as structure.
A circuit is a second way to describe a Modules to be used
module.
Module instances
Instance – An occurrence of a module
in a circuit:
K
May be multiple instances of a P And2_1 N1
W
module. N2 And2_2
S
e.g., A Car's modules: tires, Inv_1
engine, windows, etc., with 4 tire BeltWarn
instances, 1 engine instance, 6
window instances, etc.
32
Module Instantiations – Combinational Circuits
Creating a circuit:
1. Start definition of a new module `timescale 1 ns/1 ns
`timescale 1 ns/1 ns
Module instantiation module BeltWarn(K, P, S, W);
Statement: input K, P, S;
And2 And2_1(K, P, N1); output W;
Inv_1 BeltWarn
34
Module Instantiations
Example: How to complete
a 2x1 mux circuit's module `timescale 1 ns/1 ns
instantiations: Mux2 module Mux2(I1, I0, S0, D);
I0
D input I1, I0;
1. Start definition of a new I1 input S0;
S0 output D;
module (done)
wire N1, N2, N3;
(Draw desired circuit,
And2_1
if not already done) N2 Or2_1 Inv Inv_1 (S0, N1);
I0
And2 And2_1 (I0, N1, N2);
2. Declare nets N1 D And2 And2_2 (I1, S0, N3);
N3
for internal wires I1 Or2 Or2_1 (N2, N3, D);
And2_2 endmodule
3. Create module
Inv_1
instances and connect S0
ports
35
Simulating the Circuit
initial begin
K_s = 0; P_s = 0; S_s = 0;
Testbench #10 K_s = 0; P_s = 1; S_s = 0;
#10 K_s = 1; P_s = 1; S_s = 0;
K_s #10 K_s = 1; P_s = 1; S_s = 1;
K
procedure
`timescale 1 ns/1 ns
Simulate testbench file to
obtain waveforms module Testbench();
end
1
Belt
W_s
0 endmodule
10 20 30time (ns)
Simulator
37
User Defined Primitives (UDP)
table
/* A B C : x (Note that this is only a
comment) */
0 0 0 : 1;
0 0 1 : 0;
0 1 0 : 1;
0 1 1 : 0;
1 0 0 : 1;
1 0 1 : 0;
1 1 0 : 1;
1 1 1 : 1;
endtable
endprimitive
Calling a User Defined Primitive
Disadvantages of UDPs:
Only 1 output is allowed.
Difficult to write correctly when the number of inputs
becomes large (in case of large truth table).
Data Formats - User Defined
Primitives
One way to help with large truth tables.
Shorthand symbols for signal values/levels are available so that
UDP tables can be written in a concise manner.