uvm_primer ch8 参数化类

20 篇文章 10 订阅

ch8 parameterized class

从module位宽参数化引入

awidth, dwidth 是实现module位宽的参数化,是其可以适用于不同的位宽;

module RAM #(awidth, dwidth) (
			       input wire [awidth-1:0] address, 
			       inout wire [dwidth-1:0] data,
			       input we);

   initial $display("awidth: %0d  dwidth %0d",awidth, dwidth);
   // code to implement RAM
endmodule // RAM

参数化类

将ch7中的lion_cage抽象化, 使其可以装不同的动物,这样我们就不用为了每一种动物创造一个新的class,实现了代码reuse;

  • protected static T cage[$]; 定义成static,则可以在任何地方访问,否则不行;只能在构造之后进行访问;
class animal_cage #(type T);

   protected static T cage[$];

   static function void cage_animal(T l);
      cage.push_back(l);
   endfunction : cage_animal

   static function void list_animals();
      $display("Animals in cage:"); 
      foreach (cage[i])
        $display(cage[i].get_name());
   endfunction : list_animals

endclass : animal_cag
module top;


   initial begin
      lion   lion_h;
      chicken  chicken_h;
      lion_h = new(15, "Mustafa");
      animal_cage #(lion)::cage_animal(lion_h);  //使用参数化的类

      chicken_h = new(1, "Clucker");
      animal_cage #(chicken)::cage_animal(chicken_h);

      $display("-- Lions --");
      animal_cage #(lion)::list_animals();
      $display("-- Chickens --");
      animal_cage #(chicken)::list_animals();
   end

endmodule : top


举一反三:另一种实现方法

把cage定义成animal类型的队列

  • protected static animal cage[$]; 可以存储该类和该类的子类的对象;
  • 但是不能存储和该类无关的对象;lion1 l1; l1这个对象放进去会报错;chicken ,lion放进去没事,是因为这两个是animal的子类;

报错信息:
#** Error: (vlog-13216) static_methods.sv(161): Arg. ‘l’ of ‘cage_lion’: Illegal assignment to type ‘class static_methods_sv_unit.animal’ from type ‘class static_methods_sv_unit.lion1’: Types are not assignment compatible.*

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();
	 pure virtual function string get_name();

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

class chicken 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 cici", get_name());
   endfunction : make_sound

   function string get_name();
      return name;
   endfunction : get_name
endclass



virtual class animal1;
   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();
	 pure virtual function string get_name();

endclass : animal1


class lion1 extends animal1;

   protected string        name;

   function new(int age, string n);
      super.new(age);
      name = n;
   endfunction : new

   function void make_sound();
      $display ("%s says Roar1", get_name());
   endfunction : make_sound

   function string get_name();
      return name;
   endfunction : get_name
   
endclass : lion1



class lion_cage;

 //  protected static lion cage[$];
	 protected static animal cage[$];

   static function void cage_lion(animal l);
      cage.push_back(l);
   endfunction : cage_lion

   static function void list_lions();
      $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;
		  chicken c1;
		  lion1 l1;
      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);
		  c1  =new(1,"c1");
		  lion_cage::cage_lion(c1);
		  l1 =new(2,"l1");
		  lion_cage::cage_lion(l1);
      lion_cage::list_lions();
   end
endmodule : top
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值