system verilog 局部变量 全局变量
默认为public
继承类/外部均可以调用public
// https://www.edaplayground.com 调试
// 继承类可以调用public
// 外部可以调用public
`include "uvm_macros.svh"
`timescale 1ns/1ps
import uvm_pkg::*;
// The top module that contains the DUT and interface.
// This module starts the test.
module top;
class my_test;
bit[7:0] a = 8'h4; // 默认为public
protected bit[7:0] b = 8'b1111;
local bit[7:0] c = 8'b1010;
function void play_something();
$display("my_test display public a: %0d!!!",a);
$display("my_test display protected b: %0d!!!",b);
$display("my_test display local c: %0d!!!",c);
endfunction
endclass
class sub_test extends my_test;
function void play_parents_para();
$display("sub_test display public a: %0d!!!",a);
endfunction
endclass
my_test test; // 声明my_test类变量
sub_test s_test; // 声明sub_test类变量
initial begin
test = new(); // 初始化
s_test = new(); // 初始化
$display("hello, UVM!!!");
test.play_something();
s_test.play_parents_para();
$display("top_tb display public a: %0d!!!",test.a);
$display("top_tb display public a: %0d!!!",s_test.a);
end
endmodule
继承类可以调用protected,外部不可以调用protected
// 继承类可以调用protected
`include "uvm_macros.svh"
`timescale 1ns/1ps
import uvm_pkg::*;
// The top module that contains the DUT and interface.
// This module starts the test.
module top;
class my_test;
bit[7:0] a = 8'h4; // 默认为public
protected bit[7:0] b = 8'b1111;
local bit[7:0] c = 8'b1010;
function void play_something();
$display("my_test display public a: %0d!!!",a);
$display("my_test display protected b: %0d!!!",b);
$display("my_test display local c: %0d!!!",c);
endfunction
endclass
class sub_test extends my_test;
function void play_parents_para();
$display("sub_test display public a: %0d!!!",a);
$display("sub_test display protected b: %0d!!!",b); // 继承类调用
endfunction
endclass
my_test test; // 声明my_test类变量
sub_test s_test; // 声明sub_test类变量
initial begin
test = new(); // 初始化
s_test = new(); // 初始化
$display("hello, UVM!!!");
test.play_something();
s_test.play_parents_para();
$display("top_tb display public a: %0d!!!",test.a);
$display("top_tb display public a: %0d!!!",s_test.a);
end
endmodule
继承类可以调用protected
// 外部不可以调用protected
`include "uvm_macros.svh"
`timescale 1ns/1ps
import uvm_pkg::*;
// The top module that contains the DUT and interface.
// This module starts the test.
module top;
class my_test;
bit[7:0] a = 8'h4; // 默认为public
protected bit[7:0] b = 8'b1111;
local bit[7:0] c = 8'b1010;
function void play_something();
$display("my_test display public a: %0d!!!",a);
$display("my_test display protected b: %0d!!!",b);
$display("my_test display local c: %0d!!!",c);
endfunction
endclass
class sub_test extends my_test;
function void play_parents_para();
$display("sub_test display public a: %0d!!!",a);
$display("sub_test display protected b: %0d!!!",b);
endfunction
endclass
my_test test; // 声明my_test类变量
sub_test s_test; // 声明sub_test类变量
initial begin
test = new(); // 初始化
s_test = new(); // 初始化
$display("hello, UVM!!!");
test.play_something();
s_test.play_parents_para();
$display("top_tb display protected b: %0d!!!",test.b); // 外部调用protected
$display("top_tb display protected b: %0d!!!",s_test.b);
end
endmodule
外部调用protected出错:
local只能类内调用
// 继承类不可调用local
`include "uvm_macros.svh"
`timescale 1ns/1ps
import uvm_pkg::*;
// The top module that contains the DUT and interface.
// This module starts the test.
module top;
class my_test;
bit[7:0] a = 8'h4; // 默认为public
protected bit[7:0] b = 8'b1111;
local bit[7:0] c = 8'b1010;
function void play_something();
$display("my_test display public a: %0d!!!",a);
$display("my_test display protected b: %0d!!!",b);
$display("my_test display local c: %0d!!!",c);
endfunction
endclass
class sub_test extends my_test;
function void play_parents_para();
$display("sub_test display public a: %0d!!!",a);
$display("sub_test display local c: %0d!!!",c);
endfunction
endclass
my_test test; // 声明my_test类变量
sub_test s_test; // 声明sub_test类变量
initial begin
test = new(); // 初始化
s_test = new(); // 初始化
$display("hello, UVM!!!");
test.play_something();
s_test.play_parents_para();
$display("top_tb display public a: %0d!!!",test.a);
$display("top_tb display public a: %0d!!!",s_test.a);
end
endmodule
继承类调用local出错:
// 外部不可调用local
`include "uvm_macros.svh"
`timescale 1ns/1ps
import uvm_pkg::*;
// The top module that contains the DUT and interface.
// This module starts the test.
module top;
class my_test;
bit[7:0] a = 8'h4; // 默认为public
protected bit[7:0] b = 8'b1111;
local bit[7:0] c = 8'b1010;
function void play_something();
$display("my_test display public a: %0d!!!",a);
$display("my_test display protected b: %0d!!!",b);
endfunction
endclass
class sub_test extends my_test;
function void play_parents_para();
$display("sub_test display public a: %0d!!!",a);
$display("sub_test display local c: %0d!!!",c); // 继承类调用
endfunction
endclass
my_test test; // 声明my_test类变量
sub_test s_test; // 声明sub_test类变量
initial begin
test = new(); // 初始化
s_test = new(); // 初始化
$display("hello, UVM!!!");
test.play_something();
s_test.play_parents_para();
$display("top_tb display local c: %0d!!!",test.c); // 外部调用
end
endmodule