SystemVerilog

2016-03-22  本文已影响1166人  constant007

数据类型


  1. 动态数组: 声明时不需要指定个数
  2. 关联数组区别于动态数组的地方: 索引方式;

关联数组:
integer as_mem [integer];

  1. queue name[$]
    Each element in a queue is identified by an ordinal number that represents its position within the queue, with 0 representing the first, and $ representing the last.
  1. queue ::

并行


  1. fork ... join
fork
  begin
  ...
  end

  begin//每个begin end内的代码串行;各个begin之间代码并行
  ...
  end

join
fork
  task  //task or function 并行
  function

join
  • join 等所有分支结束再开始执行后续代码

函数


  1. task/function
task doInit (input bit[3:0] count, delay);   
  automatic reg[7:0] a; 
  if(count > 5)begin
     $display("@%g Returning from task",$time); 
      return
   end
   #(delay)$display("@%g Value passed is %d",$time, count);  //task中插入延迟 
endtask
  1. task/funciton port

    • input : copy value in at beginning
    • output : copy value out at end
    • inout : copy in at beginning and out at end
    • ref : pass reference
  2. Argument Passing

    • Pass by value
    • Pass by reference
    • Pass by name
    • Pass by position
    • default values
    • Optional arguments.
  3. ref 相当于指针;

进程间通信


  1. mailbox 可以传递参数,像fifo
  2. semaphores 如同令牌/许可证 put(3) 可以 get(1),也可以get(3)
    这里的值并不是判断put/get的值相等才能执行;

包定义


  1. pakage相等于module,c的函数; 里边的变量在pakage内部为局部变量;作用是限定变量的scope;
    编译空间中存放不在任何一个pakage的代码 , 编译空间相当于一个特殊的pakage

class


  1. class 如同struct,可以嵌套;
int a; //int 类型,a为int类型的变量
class_name c_o; //class_name class类型,c_o 是一个object
              //c_o是一个handle 没有分配内存空间;
  1. 类成员变量static,所有对象共享一个属性,相当于全局变量,方便参数传递;
  2. 类的方法也可以是静态的,所有的类都可以直接调用其他类的静态方法,即使调用没有初始化的对象也是合法的,但是静态方法不能调用非静态成员,如果调用会产生编译错误。静态方法不能为虚方法。另外需要注意区分的是静态方法和一般方法但是变量为静态生命周期。
  3. this只能用在非静态的成员函数中

指针复制/浅拷贝/深拷贝

  1. 指针复制

    class_name a;      
    class_name b;       
    a=new();       
    b=a;  //b,a指向同一个内存空间
    
  2. 浅拷贝

  3. 深拷贝
    代码需要自己写

浅拷贝是指源对象与拷贝对象共用一份实体,仅仅是引用的变量不同(名称不同)。对其中任何一个对象的改动都会影响另外一个对象。举个例子,一个人一开始叫张三,后来改名叫李四了,可是还是同一个人,不管是张三缺胳膊少腿还是李四缺胳膊少腿,都是这个人倒霉。
深拷贝是指源对象与拷贝对象互相独立,其中任何一个对象的改动都不会对另外一个对象造成影响。举个例子,一个人名叫张三,后来用他克隆(假设法律允许)了另外一个人,叫李四,不管是张三缺胳膊少腿还是李四缺胳膊少腿都不会影响另外一个人。

class baseA ;
    integer j = 5;
endclass

class B ;
    integer i = 1;
    baseA a = new;
endclass

module test();
  //xtndA xtnd1;
  baseA base2, base3;
  int test;   
  initial begin
    B b1    = new;    // Create an object of class B
    B b2    = new b1; // Create an object that is a copy of b1
    b2.i    = 10;     // i is changed in b2, but not in b1
    $display("b1.i = %0d",b1.i);     
    b2.a.j  = 50;     // change a.j, shared by both b1 and b2
    $display("b1.a.j = %0d",b1.a.j);
  end 
endmodule

继承

  1. 把通用的代码放到父类
  2. 继承重载:当子类的方法需要和父类不同时使用;

super

  1. 当在子类中需要对父类的属性进行操作,或者调用父类的方法可以使用super指针直接指向该成员

虚函数

class BasePacket;
  int A = 1;
  int B = 2;
  function void printA;
    $display("BasePacket::A is %0d", A);
  endfunction : printA
  
  virtual function void printB;
    $display("BasePacket::B is %0d", B);
  endfunction : printB
endclass : BasePacket
  
class My_Packet extends BasePacket;
  int A = 3;
  int B = 4;
  function void printA;
    $display("My_Packet::A is %0d", A);
  endfunction: printA
  virtual function void printB;
    $display("My_Packet::B is %0d", B);
  endfunction : printB
endclass : My_Packet
  
module test();
  BasePacket P1 = new;
  My_Packet P2 = new;
  initial begin
    P1.printA; 
    P1.printB; 
    P1 = P2;   
    P1.printA; 
    P1.printB; // 此处调用子类函数 
    P2.printA; 
    P2.printB; 
  end
endmodule

数据的隐藏与封装

  1. 一个local成员只能被当前类中的方法访问,即使是其子类也无法访问local成员。
  2. protected成员和local成员什么相似,只是其对于子类是可见的。

类的常量

  1. 在SystemVerilog中也可以通过在变量前面加上const来声明一个只读属性;

多态

  1. 注意多态的实现需要四个条件:有继承,有重写,父类指针指向子类对象,还有有父类函数的virtual。

封装


为了减少代码之间的依赖关系

上一篇 下一篇

猜你喜欢

热点阅读