Chap9_Useful Modeling Techniques
Chap9_Useful Modeling Techniques
Digital Design
Content
Procedural continuous assignments
overriding parameters,
conditional compilation and execution
useful system tasks
Logic Synthesis
Impact of logic synthesis
Verilog HDL Synthesis
Synthesis design flow, Verification of Gate-Level
netlist
1
2024-12-26
Learning Objectives
Describe procedural continuous assignment
statements assign, deassign, force, and release.
Explain their significance in modeling and
debugging.
Understand how to override parameters by using the
defparam statement at the time of module
instantiation.
Explain conditional compilation and execution of
parts of the Verilog description.
Identify system tasks for file output, displaying
hierarchy, strobing, random number generation,
memory initialization, and value change dump
2
2024-12-26
3
2024-12-26
4
2024-12-26
module stimulus;
...
//instantiate the d-flipflop
edge_dff dff(Q, Qbar, D, CLK, RESET);
...
initial
begin
//these statements force value of 1 on dff.q between time 50
and
//100, regardless of the actual output of the edge_dff.
#50 force dff.q = 1'b1; //force value of q to 1 at time 50.
#50 release dff.q; //release the value of q at time 100.
end
...
endmodule
5
2024-12-26
6
2024-12-26
Overriding Parameters
Parameters can be defined in a module definition, as
was discussed earlier in Section 3.2.8, Parameters.
However, during compilation of Verilog modules,
parameter values can be altered separately for each
module instance.
This allows us to pass a distinct set of parameter values
to each module during compilation regardless of
predefined parameter values.
There are two ways to override parameter values:
through the defparam statement or through module
instance parameter value assignment.
Defparam Statement
Example 5-2. Defparam Statement
//Define a module hello_world
module hello_world;
parameter id_num = 0; //define a module
identification number = 0
initial //display the module identification
number
$display("Displaying hello_world id number =
%d", id_num);
endmodule
7
2024-12-26
8
2024-12-26
Conditional Compilation
Conditional compilation can be accomplished by using
compiler directives
`ifdef, `ifndef, `else, `elsif, and `endif. Example 5-5
contains Verilog source code to be compiled
conditionally.
Example 5-5. Conditional Compilation
//Conditional Compilation
//Example 1
'ifdef TEST //compile module test only if text macro TEST is
defined
module test;
...
endmodule
9
2024-12-26
//Example 2
module top;
bus_master b1(); //instantiate module unconditionally
'ifdef ADD_B2
bus_master b2(); //b2 is instantiated conditionally if text macro
//ADD_B2 is defined
'elsif ADD_B3
bus_master b3(); //b3 is instantiated conditionally if text macro
//ADD_B3 is defined
'else
bus_master b4(); //b4 is instantiate by default
'endif
'ifndef IGNORE_B5
bus_master b5(); //b5 is instantiated conditionally if text macro
//IGNORE_B5 is not defined
'endif
endmodule
10
2024-12-26
11
2024-12-26
Conditional Execution
Conditional execution flags allow the
designer to control statement execution flow
at run time.
All statements are compiled but executed
conditionally.
Conditional execution flags can be used only
for behavioral statements.
The system task keyword $test$plusargs is
used for conditional execution.
12
2024-12-26
initial
begin
if($value$plusargs("testname=%s", test_string))
$readmemh(test_string, vectors); //Read test vectors
else
//otherwise display error message
$display("Test name option not specified");
if($value$plusargs("clk_t=%d", clk_period))
forever #(clk_period/2) clk = ~clk; //Set up clock
else
//otherwise display error message
$display("Clock period option name not specified");
end
//For example, to invoke the above options invoke simulator with
//+testname=test1.vec +clk_t=10
//Test name = "test1.vec" and clk_period = 10
endmodule
13
2024-12-26
Time Scales
Often, in a single simulation, delay values in
one module need to be defined by using
certain time unit, e.g., 1 μs, and delay values
in another module need to be defined by
using a different time unit, e.g. 100 ns.
Verilog HDL allows the reference time unit for
modules to be specified with the `timescale
compiler directive.
14
2024-12-26
15
2024-12-26
16
2024-12-26
5 , In dummy1 toggle = 1
10 , In dummy1 toggle = 0
15 , In dummy1 toggle = 1
20 , In dummy1 toggle = 0
25 , In dummy1 toggle = 1
30 , In dummy1 toggle = 0
35 , In dummy1 toggle = 1
40 , In dummy1 toggle = 0
45 , In dummy1 toggle = 1
--> 5 , In dummy2 toggle = 1
50 , In dummy1 toggle = 0
55 , In dummy1 toggle = 1
Notice that the $display statement in dummy2
executes once for every ten $display statements in
dummy1.
17
2024-12-26
File Descriptors
//Multichannel descriptor
integer handle1, handle2, handle3; //integers are 32-bit
values
//standard output is open; descriptor = 32'h0000_0001 (bit 0
set)
initial
begin
handle1 = $fopen("file1.out"); //handle1 = 32'h0000_0002
(bit 1 set)
handle2 = $fopen("file2.out"); //handle2 = 32'h0000_0004
(bit 2 set)
handle3 = $fopen("file3.out"); //handle3 = 32'h0000_0008
(bit 3 set)
end
18
2024-12-26
Writing to files
The system tasks $fdisplay, $fmonitor, $fwrite, and $fstrobe
are used to write to files.
Note that these tasks are similar in syntax to regular system
tasks $display, $monitor, etc., but they provide the
additional capability of writing to file We will consider only
$fdisplay and $fmonitor tasks.
Usage: $fdisplay(<file_descriptor>, p1, p2 ..., pn);
$fmonitor(<file_descriptor>, p1, p2,..., pn);
p1, p2, … , pn can be variables, signal names, or quoted
strings.
A file_descriptor is a multichannel descriptor that can be a
file handle or a bitwise combination of file handles. Verilog
will write the output to all files that have a 1 associated with
them in the file descriptor.
19
2024-12-26
Closing files
Files can be closed with the system task
$fclose.
Usage: $fclose(<file_handle>);
//Closing Files
$fclose(handle1);
A file cannot be written to once it is closed.
The corresponding bit in the multichannel
descriptor is set to 0. The next $fopen call
can reuse the bit.
Displaying Hierarchy
Hierarchy at any level can be displayed by means of
the %m option in any of the display tasks, $display,
$write task, $monitor, or $strobe task, as discussed
briefly in Section 4.3, Hierarchical Names.
This is a very useful option. For example, when
multiple instances of a module execute the same
Verilog code, the %m option will distinguish from
which module instance the output is coming.
No argument is needed for the %m option in the
display tasks. See Example 9-10.
20
2024-12-26
Displaying Hierarchy
//Displaying hierarchy information
module M;
...
initial
$display("Displaying in %m");
endmodule
//instantiate module M
module top;
...
M m1();
M m2();
//Displaying hierarchy information
M m3();
endmodule
21
2024-12-26
Strobing
Strobing is done with the system task keyword $strobe.
This task is very similar to the $display task except for a
slight difference.
If many other statements are executed in the same time
unit as the $display task, the order in which the
statements and the $display task are executed is
nondeterministic.
If $strobe is used, it is always executed after all other
assignment statements in the same time unit have
executed.
Thus, $strobe provides a synchronization mechanism to
ensure that data is displayed only after all other
assignment statements, which change the data in that
time step, have executed.
22
2024-12-26
23
2024-12-26
24
2024-12-26
Initializing Memory
module test;
reg [7:0] memory[0:7]; //declare an 8-byte memory
integer i;
initial
begin
//read memory file init.dat. address locations given in memory
$readmemb("init.dat", memory);
module test;
//display contents of initialized memory
for(i=0; i < 8; i = i + 1)
$display("Memory [%0d] = %b", i, memory[i]);
end
endmodule
25
2024-12-26
26
2024-12-26
initial
$dumpfile("myfile.dmp"); //Simulation info
dumped to myfile.dmp
//Dump signals in a module
initial
$dumpvars; //no arguments, dump all signals
in the design
initial
$dumpvars(1, top); //dump variables in
module instance top.
27
2024-12-26
// below top.m1
//Start and stop dump process
initial
begin
$dumpon; //start the dump process.
#100000 $dumpoff; //stop the dump process after 100,000
time units
end
//Create a checkpoint. Dump current value of all VCD
variables
initial
$dumpall;
The $dumpfile and $dumpvars tasks are normally specified at
the beginning of the simulation. The $dumpon, $dumpoff, and
$dumpall control the dump process during the simulation.[5]
28
2024-12-26
Questions
Questions
What will be the output of the $display statement
shown below?
module top;
A a1();
endmodule
module A;
B b1();
endmodule
module B;
initial
$display("I am inside instance %m");
endmodule
29
2024-12-26
Questions
Identify the files to which the following display
statements will write:
//File output with multi-channel descriptor
module test;
integer handle1,handle2,handle3; //file handles
//open files
initial
begin
handle1 = $fopen("f1.out");
handle2 = $fopen("f2.out");
handle3 = $fopen("f3.out");
end
Questions
//Display statements to files
initial
begin
//File output with multi-channel descriptor
#5;
$fdisplay(4, "Display Statement # 1");
$fdisplay(15, "Display Statement # 2");
$fdisplay(6, "Display Statement # 3");
$fdisplay(10, "Display Statement # 4");
$fdisplay(0, "Display Statement # 5");
end
endmodule
30
2024-12-26
Q&A
31