• UVM Primer


    静态变量

    virtual class animal;
       protected int age=-1;
    
    
       function new(int age);
          set_age(age);
       endfunction : new
    
       function void set_age(int a);
          age = a;
       endfunction : set_age
    
       function int get_age();
          if (age == -1)
            $fatal(1, "You didn't set the age.");
          else
            return age;
       endfunction : get_age
    
       pure virtual function void make_sound();
    
    endclass : animal
    
    
    class lion extends animal;
    
       protected string        name;
    
       function new(int age, string n);
          super.new(age);
          name = n;
       endfunction : new
    
       function void make_sound();
          $display ("%s says Roar", get_name());
       endfunction : make_sound
    
       function string get_name();
          return name;
       endfunction : get_name
       
    endclass : lion
    //我们只能有一个放狮子的笼子,所以我们不能在 lion 类里创建笼子变量。我们需要一个带静态变量的新类lion_cage 来装所有的狮子。
    class lion_cage;
    
        static lion cage[$]; //lion_cage包含了一个 SystemVerilog 队列并定义为静态变量。这个队列用于存放lion对象
        //定义前的static关键字意味着我们不用例化这个类就可以访问这个变量。这个变量只有一份拷贝,程序一运行, 这个变量就得到了分配的空间。这样我们就可以任意访问这个狮笼了。
    endclass : lion_cage
    
    module top;
    
       initial begin
          lion   lion_h;
          lion_h  = new(2,  "Kimba");
          lion_cage::cage.push_back(lion_h);
    //
    这里我们创建了新的狮子,然后用静态变量把它们放进了笼子队列 。注意到“::”操作符。我们通过把::置于类名之后来访问lion_cage的变量。这告知了编译器我们在类的命名空间下访问静态变量

            //我们用lion_cage::cage 访问变量,用push_back()方法在队列里存放狮子

          lion_h  = new(3,  "Simba");
          lion_cage::cage.push_back(lion_h);
          lion_h  = new(15, "Mustafa");
          lion_cage::cage.push_back(lion_h);
          $display("Lions in cage"); 
          foreach (lion_cage::cage[i])
            $display(lion_cage::cage[i].get_name());  //用get_name()方法来打印出狮子的名字
       end
    
    endmodule : top

    静态方法

    注意line_cage类内的静态方法

    virtual class animal;
       protected int age=-1;
    
    
       function new(int age);
          set_age(age);
       endfunction : new
    
       function void set_age(int a);
          age = a;
       endfunction : set_age
    
       function int get_age();
          if (age == -1)
            $fatal(1, "You didn't set the age.");
          else
            return age;
       endfunction : get_age
    
       pure virtual function void make_sound();
    
    endclass : animal
    
    
    class lion extends animal;
    
       protected string        name;
    
       function new(int age, string n);  //重载基类的new(),获取age和name
          super.new(age);
          name = n;
       endfunction : new
    
       function void make_sound();
          $display ("%s says Roar", get_name());
       endfunction : make_sound
    
       function string get_name();
          return name;
       endfunction : get_name
       
    endclass : lion
    
    class lion_cage;
    
       protected static lion cage[$];  //我们用了protected关键字来保护静态变量。这个关键字阻止了用户对我们的SystemVerilog队列的直接访问 ,保证了良好的重用。有任何直接访问笼子变量的行为,SystemVerilog编译器都会报错。
       //我们提供了两个静态可访问方法
       static function void cage_lion(lion l);  //cage_lion()使用一个lion对象参数并将其放入SystemVerilog队列
          cage.push_back(l);
       endfunction : cage_lion
    
       static function void list_lions();   //lion_list()使用SystemVerilog的foreach循环来列出存放的狮子
          $display("Lions in cage"); 
          foreach (cage[i])
            $display(cage[i].get_name());
       endfunction : list_lions
    
    endclass : lion_cage
    
       
    
    module top;
    
    
       initial begin
          lion   lion_h;
          lion_h  = new(2,  "Kimba");
          lion_cage::cage_lion(lion_h);
          lion_h  = new(3,  "Simba");
          lion_cage::cage_lion(lion_h);
          lion_h  = new(15, "Mustafa");
          lion_cage::cage_lion(lion_h);
          lion_cage::list_lions();
       end
    //
    这个代码里我们新建了狮子对象并用lion_cage::cage_lion()方法将其放进笼子。然后我们用lion_cage::list_lions()列出了所有的狮子

    //所有这些方法的调用都访问了一样的地址空间。我们可以在程序的任何地方用全局引用lion_cage类来访问狮笼
    
    endmodule : top
  • 相关阅读:
    P3507 [POI2010]GRA-The Minima Game
    P2038 无线网络发射器选址
    2017.9.23清北第二场
    P3183 [HAOI2016]食物链
    2017.9.17校内noip模拟赛解题报告
    Day2代码
    P1328 生活大爆炸版石头剪刀布
    Fibinary Numbers
    Mac os 进行Android开发笔记(1)
    python中文注释及输出出错
  • 原文地址:https://www.cnblogs.com/yiyedada/p/12366790.html
Copyright © 2020-2023  润新知