Professional Documents
Culture Documents
Verification I
Verification I
1DT109 ASPLOC
2022 VT1-VT2
2022-09-19 1
Agenda
• Introduction to Verification
• Verification model
• Verification abstraction
• Verification task
• Introduction to SystemVerilog
• Basic building blocks
• Delta delay
2022-09-19 2
The Quebec Bridge and Pierre Laporte Bridge
Suspension
Cantilever bridge
bridge
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 3
The Quebec Bridge and Pierre Laporte Bridge
Suspension
Cantilever bridge
bridge
• Heavy
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 4
The Quebec Bridge and Pierre Laporte Bridge
Suspension
Cantilever bridge
bridge
• Heavy
• Expensive to build
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 5
The Quebec Bridge and Pierre Laporte Bridge
Suspension
Cantilever bridge
bridge
• Heavy
• Expensive to build
• Spans shorter range
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 6
The Quebec Bridge and Pierre Laporte Bridge
Suspension
Cantilever bridge
bridge
• Heavy
• Expensive to build
• Spans shorter range
• Collapse in 1907
and 1916, twice!
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 7
The Quebec Bridge and Pierre Laporte Bridge
Suspension
Cantilever bridge
bridge
• Heavy
• Expensive to build
• Spans shorter range
• Collapse in 1907
and 1916, twice!
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 8
The Quebec Bridge and Pierre Laporte Bridge
Suspension
Cantilever bridge
bridge
• Heavy
• Expensive to build
• Spans shorter range
• Collapse in 1907
and 1916, twice!
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 9
The Quebec Bridge and Pierre Laporte Bridge
Suspension
Cantilever bridge
bridge
• Heavy
• Expensive to build
• Spans shorter range
• Collapse in 1907
and 1916, twice!
• Due to flaws in
blueprint verification
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 10
The Quebec Bridge and Pierre Laporte Bridge
Suspension
Cantilever bridge
bridge
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 11
The Quebec Bridge and Pierre Laporte Bridge
Suspension
Cantilever bridge
bridge
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 12
The Quebec Bridge and Pierre Laporte Bridge
Suspension
Cantilever bridge
bridge
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 13
The Quebec Bridge and Pierre Laporte Bridge
Suspension
Cantilever bridge
bridge
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 14
The Quebec Bridge and Pierre Laporte Bridge
Suspension
Cantilever bridge
bridge
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 15
The Quebec Bridge and Pierre Laporte Bridge
Suspension
Cantilever bridge
bridge
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 16
The Quebec Bridge and Pierre Laporte Bridge
Case-based testbench
Suspension
Cantilever bridge
bridge
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 17
The Quebec Bridge and Pierre Laporte Bridge
Case-based testbench
Suspension
Cantilever bridge
bridge
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 18
The Quebec Bridge and Pierre Laporte Bridge
Case-based testbench Verification
Suspension
Cantilever bridge
bridge
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 19
The Quebec Bridge and Pierre Laporte Bridge
Case-based testbench Verification
Case-based
Verification
testbench
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 20
The Quebec Bridge and Pierre Laporte Bridge
Case-based testbench Verification
Case-based
Verification
testbench
• Verbose
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 21
The Quebec Bridge and Pierre Laporte Bridge
Case-based testbench Verification
Case-based
Verification
testbench
• Verbose
• Hard and tedious to
build
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 22
The Quebec Bridge and Pierre Laporte Bridge
Case-based testbench Verification
Case-based
Verification
testbench
• Verbose
• Hard and tedious to
build
• Provide limited test
cases
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 23
The Quebec Bridge and Pierre Laporte Bridge
Case-based testbench Verification
Case-based
Verification
testbench
• Verbose
• Hard and tedious to
build
• Provide limited test
cases
• Waveform eyeing
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 24
The Quebec Bridge and Pierre Laporte Bridge
Case-based testbench Verification
Case-based
Verification
testbench
• Verbose
• Hard and tedious to
build
• Provide limited test
cases
• Waveform eyeing
• Ad-hoc
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 25
The Quebec Bridge and Pierre Laporte Bridge
Case-based testbench Verification
Case-based
Verification
testbench
• Verbose • Concise
• Hard and tedious to
build
• Provide limited test
cases
• Waveform eyeing
• Ad-hoc
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 26
The Quebec Bridge and Pierre Laporte Bridge
Case-based testbench Verification
Case-based
Verification
testbench
• Verbose • Concise
• Hard and tedious to • Easy to build
build
• Provide limited test
cases
• Waveform eyeing
• Ad-hoc
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 27
The Quebec Bridge and Pierre Laporte Bridge
Case-based testbench Verification
Case-based
Verification
testbench
• Verbose • Concise
• Hard and tedious to • Easy to build
build • Powerful in test
• Provide limited test strength
cases
• Waveform eyeing
• Ad-hoc
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 28
The Quebec Bridge and Pierre Laporte Bridge
Case-based testbench Verification
Case-based
Verification
testbench
• Verbose • Concise
• Hard and tedious to • Easy to build
build • Powerful in test
• Provide limited test strength
cases • Coverage
• Waveform eyeing
• Ad-hoc
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 29
The Quebec Bridge and Pierre Laporte Bridge
Case-based testbench Verification
Case-based
Verification
testbench
• Verbose • Concise
• Hard and tedious to • Easy to build
build • Powerful in test
• Provide limited test strength
cases • Coverage
• Waveform eyeing • Constraints
• Ad-hoc
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 30
The Quebec Bridge and Pierre Laporte Bridge
Case-based testbench Verification
Case-based
Verification
testbench
• Verbose • Concise
• Hard and tedious to • Easy to build
build • Powerful in test
• Provide limited test strength
cases • Coverage
• Waveform eyeing • Constraints
• Ad-hoc • Randomization
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 31
The Quebec Bridge and Pierre Laporte Bridge
Case-based testbench Verification
Case-based
Verification
testbench
• Verbose • Concise
• Hard and tedious to • Easy to build
build • Powerful in test
• Provide limited test strength
cases • Coverage
• Waveform eyeing • Constraints
• Ad-hoc • Randomization
• And many more
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette
2022-09-19 32
The Quebec Bridge and Pierre Laporte Bridge
Case-based testbench Verification
Case-based
Verification
testbench
• Verbose • Concise
• Hard and tedious to • Easy to build
build • Powerful in test
• Provide limited test strength
cases • Coverage
• Waveform eyeing • Constraints
• Ad-hoc • Randomization
• And many more
• Formal methods to
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette verify the results
2022-09-19 33
The Quebec Bridge and Pierre Laporte Bridge
Case-based testbench Verification
Case-based
Verification
testbench
• Verbose • Concise
• Hard and tedious to • Easy to build
build • Powerful in test
• Provide limited test strength
cases • Coverage
• Waveform eyeing • Constraints
• Ad-hoc • Randomization
• And many more
• Formal methods to
Source: https://www.wikiwand.com/en/Quebec_Bridge#/google_vignette verify the results
2022-09-19 • True industry34
standard
Intel’s Pentium FDIV bug
• A hardware bug affecting
the floating-point unit (FPU)
• Discovered in 1994
• Caused Intel about $ 0.5
billion to recall all flaw
processors in 1995.
• Roughly speaking $ 0.5 billion
in 1995 is worth $ 0.9 billion
today
• ~7.4 billion SEK
• Ericsson full-year sales in Source: https://en.wikipedia.org/wiki/Pentium_FDIV_bug
Testing
Verification (this lecture)
Validation
Volume
Chip design and development life cycle shipment
Specifications
Prototype
Architecture RTL Design Physical Design Tapeout
(Chip back from fab)
Microarchitecture
2022-09-19 39
The Embedded SoC engineering pie
2022-09-19 40
The Embedded SoC engineering pie
System design
Implementation
Verification
2022-09-19 41
The Embedded SoC engineering pie
System design
Implementation
Verification
2022-09-19 42
The Embedded SoC engineering pie
System design
Implementation
Verification
2022-09-19 43
What to verify – the reconvergence model
Requirements
• Remember that the
purpose of the System design team’s
verification process is interpretation
to ensure that the (SystemC)
functionality of a logic
is as intended.
Specification
• The reconvergence
model is a conceptual Verification Team’s
Interpretation
Design Team’s
Interpretation
representation of what (SystemVerilog) (Verilog)
to verify.
Implementation
2022-09-19 44
What to verify – the reconvergence model
Requirements
• Remember that the
purpose of the System design team’s
Independence of interpretation by
verification process is
to ensure that the
interpretation
(SystemC)
to verify.
Implementation
2022-09-19 45
What to verify – the reconvergence model
Requirements
• Remember that the
purpose of the System design team’s
Independence of interpretation by
verification process is
to ensure that the
interpretation
(SystemC)
Do not fight.
representation of what
Interpretation
(SystemVerilog)
Interpretation
(Verilog)
to verify.
Implementation
2022-09-19 46
Verification models
• Black-box model
• The verification team has access to input and output only
• White-box model
• The verification team has access to the internal logic
• Grey-box model
• The verification team has access to the internal logic that is specifically
designed to aid debug
2022-09-19 47
Verification abstraction
• Function/system level
• Operations ordered by functionality
• Example: DSP systems modeled in Matlab
• Transaction level
• Operations ordered by transmission protocol
• Example: AXI data transfer
• RTL/cycle accurate level
• Operations ordered by clock signal
• Example: FSM state transition
• Gate/Transistor level
• Operations ordered by physical signal
• Example: Bit change in gate with manufactural delays
2022-09-19 48
Verification abstraction
• Function/system level
• Operations ordered by functionality
• Example: DSP systems modeled in Matlab
Higher
• Operationsabstraction
• Transaction level
level = greater
ordered by transmission protocol
efficiency
• Example: AXI data transfer
• RTL/cycle accurate level
but less control
• Operations ordered by clock signal
• Example: FSM state transition
• Gate/Transistor level
• Operations ordered by physical signal
• Example: Bit change in gate with manufactural delays
2022-09-19 49
Verification abstraction
• Function/system level
• Operations ordered by functionality
• Example: DSP systems modeled in Matlab
Higher
• Operationsabstraction
• Transaction level
level = greater
ordered by transmission protocol
efficiency
• Example: AXI data transfer
• RTL/cycle accurate level
but less control
• Operations ordered by clock signal
• Example: FSM state transition
• Gate/Transistor level
• Operations ordered by physical signal
• Example: Bit change in gate with manufactural delays
2022-09-19 50
Verification abstraction
• Function/system level
• Operations ordered by functionality
• Example: DSP systems modeled in Matlab
Higher
• Operationsabstraction
• Transaction level
level = greater
ordered by transmission protocol
efficiency
• Example: AXI data transfer
• RTL/cycle accurate level
but less control
• Operations ordered by clock signal We mainly
• Example: FSM state transition
focus here
• Gate/Transistor level
• Operations ordered by physical signal
• Example: Bit change in gate with manufactural delays
2022-09-19 51
Verification tasks
In each of the verification hierarchy, checking the
following
• Function and performance
o Function: A piece of HW/SW that transforms input to output
o Performance: How well does the function perform in terms of figures of merits
• Timing and interface
o The logic timing which specifies when the input and output should appear/be
valid/be active, especially in relation to each other.
• Address/Location/Amount
o The amount of data that is read/input and the location from which it is read.
o The amount of data that is written/output and the location to which it is written.
• Structure/configuration
o IPs/Sub-systems are connected and configured properly
2022-09-19 52
The verification space
Model
Abstraction
Task
2022-09-19 53
Why SystemVerilog
• Verilog lacks
• Constrained random generation
• Functional coverage
• Assertions
• Specman E/Vera
• Used with VHDL and Verilog for constrained random generation and
functional coverage
• Property specification language (PSL)
• For assertion
• Learning 1 language (SystemVerilog) is better than learning 2
(Specman E/Vera and PSL)
• Even as a verification engineer, one should have enough knowledge
on HDL and system design language such as SystemC
2022-09-19 54
More than just a verification tool
• SystemVerilog rapidly getting accepted as the next
generation HDL for
• System design
• RTL design and synthesis
• Verification
• Easy bridging up different design teams.
• It is strongly practiced by the industries such as Ericsson.
• There are many properties and features in SV (can be
enough for another full course).
• We will only cover a small set of useful ones for writing
testbench in this course.
2022-09-19 55
More than just a verification tool
• SystemVerilog rapidly getting accepted as the next
generation HDL for
• System design
In this course, we use Verilog for RTL
• RTL design and synthesis
• Verification
modeling
• Easy bridgingand SV design
up different onlyteams.
for verification.
• It is strongly practiced by the industries such as Ericsson.
• There are many properties and features in SV (can be
enough for another full course).
• We will only cover a small set of useful ones for writing
testbench in this course.
2022-09-19 56
Basic building blocks
• Basic data types
• Different always blocks
• Struct vs. Class
• Packed and unpacked array
• Associative array
• File
2022-09-19 57
bit [15:0] bus; //unsigned
the testbench.
$display("bus=%b", bus);
• logic: 1 bit, identical to reg
• integer: 32 bit, signed $display("i,q=%b", i);
$display("addr_unsigned=%b", addr_unsigned);
$display("addr_logic=%b", addr_logic);
$display("myName=%s", myName);
$display("c=%c", c);
$display("c=%b", c);
$display("s=%s", s);
2022-09-19 //demo_1 59
Different always blocks
• Comes in four flavors
• always module demo_2(
o The old Verilog legacy. input a,
• always_ff input b,
o Clocked synchronous body output c
);
o To infer DFF
reg c;
o The only case to use “<=”
• always _comb: always_ff
o To infer combinational logic if (b)
o Sensitivity list is inferred from c <= a;
signals involved in the RHS of
expressions. endmodule
• always_latch
o To infer latch
o Sensitivity list is inferred from Compilation error.
signals involved in the RHS of
2022-09-19
expressions. 60
Different always blocks
• Comes in four flavors
• always module demo_2(
o The old Verilog legacy. input a,
• always_ff input b,
o Clocked synchronous body output c
);
o To infer DFF
reg c;
o The only case to use “<=”
• always _comb: always_ff @ (a, b)
o To infer combinational logic if (b)
o Sensitivity list is inferred from c <= a;
signals involved in the RHS of
expressions. endmodule
• always_latch
o To infer latch
o Sensitivity list is inferred from Compilation error.
signals involved in the RHS of
2022-09-19
expressions. 61
Different always blocks
• Comes in four flavors
• always module demo_2(
o The old Verilog legacy. input a,
• always_ff input b,
o Clocked synchronous body output c
);
o To infer DFF
reg c;
o The only case to use “<=”
• always _comb: always_ff @ (a, b)
o To infer combinational logic if (b)
o Sensitivity list is inferred from c <= a;
signals involved in the RHS of
expressions. endmodule
• always_latch
o To infer latch Warning.
o Sensitivity list is inferred from Compilation
No DFFerror.
signals involved in the RHS of generated.
2022-09-19
expressions. 62
Different always blocks
• Comes in four flavors
• always module demo_2( module demo_2(
o The old Verilog legacy. input a, input a,
• always_ff input b, input b,
o Clocked synchronous body output c output c
); );
o To infer DFF
reg c; reg c;
o The only case to use “<=”
• always _comb: always_ff @ (a, b) always_comb
o To infer combinational logic if (b) if (b)
o Sensitivity list is inferred from c <= a; c = a;
signals involved in the RHS of
expressions. endmodule endmodule
• always_latch
o To infer latch Warning.
o Sensitivity list is inferred from Compilation
No DFFerror.
signals involved in the RHS of generated.
2022-09-19
expressions. 63
Different always blocks
• Comes in four flavors
• always module demo_2( module demo_2(
o The old Verilog legacy. input a, input a,
• always_ff input b, input b,
o Clocked synchronous body output c output c
); );
o To infer DFF
reg c; reg c;
o The only case to use “<=”
• always _comb: always_ff @ (a, b) always_comb
o To infer combinational logic if (b) if (b)
o Sensitivity list is inferred from c <= a; c = a;
signals involved in the RHS of
expressions. endmodule endmodule
• always_latch
o To infer latch Warning. Warning.
o Sensitivity list is inferred from Compilation
No DFFerror. No comb logic
signals involved in the RHS of generated. generated
2022-09-19
expressions. 64
Different always blocks
• Comes in four flavors
• always module demo_2( module demo_2( module demo_2(
o The old Verilog legacy. input a, input a, input a,
• always_ff input b, input b, input b,
o Clocked synchronous body output c output c output c
); ); );
o To infer DFF
reg c; reg c; reg c;
o The only case to use “<=”
• always _comb: always_ff @ (a, b) always_comb always_latch
o To infer combinational logic if (b) if (b) if (b)
o Sensitivity list is inferred from c <= a; c = a; c = a;
signals involved in the RHS of
expressions. endmodule endmodule endmodule
• always_latch
o To infer latch Warning. Warning.
o Sensitivity list is inferred from Compilation
No DFFerror. No comb logic
signals involved in the RHS of generated. generated
2022-09-19
expressions. 65
Different always blocks
• Comes in four flavors
• always module demo_2( module demo_2( module demo_2(
o The old Verilog legacy. input a, input a, input a,
• always_ff input b, input b, input b,
o Clocked synchronous body output c output c output c
); ); );
o To infer DFF
reg c; reg c; reg c;
o The only case to use “<=”
• always _comb: always_ff @ (a, b) always_comb always_latch
o To infer combinational logic if (b) if (b) if (b)
o Sensitivity list is inferred from c <= a; c = a; c = a;
signals involved in the RHS of
expressions. endmodule endmodule endmodule
• always_latch
o To infer latch Warning. Warning.
o Sensitivity list is inferred from Compilation
No DFFerror. No comb logic Correct.
signals involved in the RHS of generated. generated
2022-09-19
expressions. 66
Different always blocks
• Comes in four flavors
• always module demo_2( module demo_2( module demo_2(
o The old Verilog legacy. input a, input a, input a,
• always_ff input b, input b, input b,
o Clocked synchronous body output c output c output c
); ); );
o To infer DFF
Can be used for synthesis.
reg c; reg c; reg c;
o The only case to use “<=”
• always _comb: always_ff @ (a, b) always_comb always_latch
o To infer combinational logic if (b) if (b) if (b)
o Sensitivity list is inferred from c <= a; c = a; c = a;
signals involved in the RHS of
expressions. endmodule endmodule endmodule
• always_latch
o To infer latch Warning. Warning.
o Sensitivity list is inferred from Compilation
No DFFerror. No comb logic Correct.
signals involved in the RHS of generated. generated
2022-09-19
expressions. 67
Struct vs. Class
2022-09-19 68
Concept of struct
• A struct is an integral type, just like integer or reg.
• Whenever a “struct” type is declared, the necessary
number of bits is automatically allocated.
• If a struct variable is assigned to another or passed as
an argument to a function or task, all the bits are copied.
typedef struct {
bit sign;
bit [22:0] mantissa;
bit [ 7:0] exponent;
} ieee_sp_float_unpacked
2022-09-19 69
Concept of struct
2022-09-19 70
module demo_3;
end
endmodule
2022-09-19 71
module demo_3;
end Error
endmodule
2022-09-19 72
module demo_3;
end Error
endmodule
2022-09-19 73
module demo_3;
end Error
endmodule
2022-09-19 74
class atm_pkt; Variable
bit [11:0] id; By default, “public”
To use private var:
Concept of class bit [15:0] pw;
bit [ 2:0] pri; local bit [11:0] id
2022-09-19 80
More on unpacked array
• Which should be used to model a general-purpose array?
• Packed or unpacked?
• A dynamic array is an unpacked array with an unspecified
dimension size.
• The actual size of the array is specified and allocated at runtime.
• 1-D or multi-dimensional.
ieee_sp_float_packed array_1d [ ]; //demo_5 ieee_sp_float_packed array_2d [ ][ ];
bit [22:0] rand_vec;
array_1d = new [32]; // 32 elements array_2d = new [32]; Observe how the
array_1d = new [64] (array_1d); // resize foreach (array_2d [i]) array_2d is indexed.
No need to declare I,j
array_1d.delete(); // Delete array array_2d[i] = new [32];
foreach (array_2d [i, j])
Call the SV operator new to allocate rand_vec = $urandom();
memory for the array dynamically. array_2d[i][j] = {1'b1, rand_vec, 8'h0};
2022-09-19 81
Not a constructor!
Multi-dimensional class pixel;
bit [7:0] red;
16GB 0
Allocated
2022-09-19 region 88
More on associative array
class sparse_memory;
local logic [7:0] mem [bit [63:0]];
/********************************/
function logic [7:0] read(input bit [63:0] addr); module demo_7;
read = 8’hxx; sparse_memory mem = new();
if (this.mem.exists(addr)) begin bit [63:0] addr;
read = this.mem[addr]; bit [7:0] data;
end
endfunction: read initial begin
/********************************/ $display(“Method 1 Init. memory by random writing”);
function void write(input bit [63:0] addr, repeat (10) begin
input logic [ 7:0] data); addr = { $urandom(), $urandom() };
this.mem[addr] = data; data = $urandom();
endfunction: write mem.write(addr, data);
/********************************/ $display("0x%h, 0x%h", addr, mem.read(addr));
function void display(); end
foreach (mem[i]) mem.display();
$display("Addr: 0x%h, data: 0x%h", i, mem[i]); end
endfunction: display endmodule
endclass: sparse_memory
2022-09-19 89
More on associative array
class sparse_memory;
local logic [7:0] mem [bit [63:0]];
/********************************/
function logic [7:0] read(input bit [63:0] addr); module demo_7;
read = 8’hxx; sparse_memory mem = new();
bit [63:0] addr; $random()/$urandom()
if (this.mem.exists(addr)) begin
read = this.mem[addr]; bit [7:0] data; only return 32-bit
end random number
endfunction: read initial begin
/********************************/ $display(“Method 1 Init. memory by random writing”);
function void write(input bit [63:0] addr, repeat (10) begin
input logic [ 7:0] data); addr = { $urandom(), $urandom() };
this.mem[addr] = data; data = $urandom();
endfunction: write mem.write(addr, data);
/********************************/ $display("0x%h, 0x%h", addr, mem.read(addr));
function void display(); end
foreach (mem[i]) mem.display();
$display("Addr: 0x%h, data: 0x%h", i, mem[i]); end
endfunction: display endmodule
endclass: sparse_memory
2022-09-19 90
More on associative array
class sparse_memory;
local logic [7:0] mem [bit [63:0]];
/********************************/
function logic [7:0] read(input bit [63:0] addr); module demo_7;
read = 8’hxx; sparse_memory mem = new();
bit [63:0] addr; $random()/$urandom()
if (this.mem.exists(addr)) begin
read = this.mem[addr]; bit [7:0] data; only return 32-bit
end random number
endfunction: read initial begin
/********************************/ $display(“Method 1 Init. memory by random writing”);
function void write(input bit [63:0] addr, repeat (10) begin
input logic [ 7:0] data); addr = { $urandom(), $urandom() };
this.mem[addr] = data; data = $urandom();
endfunction: write mem.write(addr, data);
/********************************/ $display("0x%h, 0x%h", addr, mem.read(addr));
function void display(); end
foreach (mem[i]) mem.display();
$display("Addr: 0x%h, data: 0x%h", i, mem[i]); end
endfunction: display endmodule
endclass: sparse_memory
2022-09-19 91
File
• SystemVerilog can both open file for read and write using the
$fopen() system task.
• The task will return a 32-bit integer handle called a file
descriptor. This handle should be used to read and write to the
file until closed.
• Close a file by using the $fclose() system task.
• File is another way to initialization memory content.
2022-09-19 92
More on File
class sparse_memory; // demo_7
local logic [7:0] mem [bit [63:0]];
/********************************/
function void loadimage();
$display("Loading mem image...");
fd = $fopen("mem_image.txt", "r");
if (fd) begin
while (!$feof(fd)) begin
bit [63:0] addr;
bit [7:0] data;
string line;
$fgets(line, fd);
$sscanf(line, "%h %h", addr, data);
this.write(addr, data);
end
$fclose(fd);
end else
$display("Error in opening memory image file.");
endfunction: loadimage
endclass: sparse_memory
2022-09-19 93
More on File
class sparse_memory; // demo_7
local logic [7:0] mem [bit [63:0]];
/********************************/
function void loadimage();
$display("Loading mem image...");
fd = $fopen("mem_image.txt", "r");
if (fd) begin
while (!$feof(fd)) begin
bit [63:0] addr; Remember to import
bit [7:0] data; the file to Vivado as
string line; “Simulation Sources”
$fgets(line, fd);
$sscanf(line, "%h %h", addr, data);
this.write(addr, data);
end
$fclose(fd);
end else
$display("Error in opening memory image file.");
endfunction: loadimage
endclass: sparse_memory
2022-09-19 94
More on File
class sparse_memory; // demo_7
local logic [7:0] mem [bit [63:0]];
/********************************/
function void loadimage();
$display("Loading mem image...");
fd = $fopen("mem_image.txt", "r");
if (fd) begin
while (!$feof(fd)) begin
bit [63:0] addr; Remember to import
bit [7:0] data; the file to Vivado as
string line; “Simulation Sources”
$fgets(line, fd);
$sscanf(line, "%h %h", addr, data);
this.write(addr, data);
end
$fclose(fd);
end else
$display("Error in opening memory image file.");
endfunction: loadimage
endclass: sparse_memory
2022-09-19 95
More on File
class sparse_memory; // demo_7
local logic [7:0] mem [bit [63:0]];
/********************************/
function void loadimage(); File content
$display("Loading mem image...");
fd = $fopen("mem_image.txt", "r");
if (fd) begin
while (!$feof(fd)) begin
bit [63:0] addr; Remember to import
bit [7:0] data; the file to Vivado as
string line; “Simulation Sources”
$fgets(line, fd);
$sscanf(line, "%h %h", addr, data);
this.write(addr, data);
end
$fclose(fd);
end else
$display("Error in opening memory image file.");
endfunction: loadimage
endclass: sparse_memory
2022-09-19 96
More on File
class sparse_memory; // demo_7
local logic [7:0] mem [bit [63:0]];
/********************************/
function void loadimage(); File content
$display("Loading mem image...");
fd = $fopen("mem_image.txt", "r");
if (fd) begin
while (!$feof(fd)) begin
bit [63:0] addr; Remember to import
bit [7:0] data; the file to Vivado as module demo_7;
string line; “Simulation Sources” sparse_memory mem = new();
$fgets(line, fd); bit [63:0] addr;
$sscanf(line, "%h %h", addr, data); bit [7:0] data;
this.write(addr, data);
end initial begin
$fclose(fd); $display(“Method 2 Load memory by reading file”);
end else mem.loadimage();
$display("Error in opening memory image file."); mem.display();
endfunction: loadimage end
endclass: sparse_memory endmodule
2022-09-19 97
More on File
class sparse_memory; // demo_7
local logic [7:0] mem [bit [63:0]];
/********************************/
function void loadimage(); File content
$display("Loading mem image...");
fd = $fopen("mem_image.txt", "r");
if (fd) begin
while (!$feof(fd)) begin
bit [63:0] addr; Remember to import
bit [7:0] data; the file to Vivado as module demo_7;
string line; “Simulation Sources” sparse_memory mem = new();
$fgets(line, fd); bit [63:0] addr;
$sscanf(line, "%h %h", addr, data); bit [7:0] data;
this.write(addr, data);
end initial begin
$fclose(fd); $display(“Method 2 Load memory by reading file”);
end else mem.loadimage();
$display("Error in opening memory image file."); mem.display();
endfunction: loadimage end
endclass: sparse_memory endmodule
2022-09-19 98
Questions?
2022-09-19 99