local protected public 变量 system verilog 全局/局部变量

本文探讨了SystemVerilog中不同类型的变量可见性,包括公共(public)、受保护(protected)和局部(local)变量。通过具体示例展示了这些变量如何在继承类及外部环境中被访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

在这里插入图片描述


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值