Systemverilog(绿皮书)第三章——过程块和方法

在做设计验证机需要写很多的代码,其中大部分在任务和函数中使用。systemverilog在这方面增加了很多改进,使其更加接近C语言,从而使代码的编写变得更加容易,尤其是在处理参数的传递上。

  • 在Systemverilog中,过程块和其方法通过域进行划分,分为硬件部分和软件部分,具体的内容如下图:

 针对function参数类型设置考题如下:

typedef    struct{
    bit    [1:0] cmd;
    bit    [7:0] addr;
    bit    [31:0] data;
} trans;

function    automatic void op_copy(trans t,trans s);
    
    t = s;

endfunction


initial begin
    trans s;
    trans t;
    s.cmd = 'h1;
    s.addr = 'h10;
    s.data = 'h100;
    op_copy(t,s);
    t.cmd = 'h2;
end

问:t 中的三个成员变量{cmd,addr, data}最后的数值是多少?

{‘h2,'h0,'h0}

由于function的参数,默认的 方向都是input,这里trans t 变量被看做是输入型变量。因此, 通过调用op_copy函数来实现赋值的功能,由于方向出值的方向未被确定,结果为0。

  • 在systemverilog中,我们将生命周期分为动态(automatic)和静态(static)。
  • 针对静态变量和动态变量的作用域设计问题如下:

  • function automatic int auto_cnt(input a);
        int cnt = 0;
        cnt +=a;
        return cnt;
    endfunction
    
    
    function static int auto_cnt(input a);
        static int cnt = 0;
        cnt +=a;
        return cnt;
    endfunction
    
    function int auto_cnt(input a);
        static int cnt = 0;
        cnt +=a;
        return cnt;
    endfunction
    
    initial begin
        $display("@1 auto_cnt = %0d",auto_cnt(1));
        $display("@2 auto_cnt = %0d",auto_cnt(1));
        $display("@1 auto_cnt = %0d",static_cnt(1));
        $display("@2 auto_cnt = %0d",static_cnt(1));
        $display("@1 def_cnt = %0d",def_cnt (1));
        $display("@2 def_cnt = %0d",def_cnt (1));
    end
    
    
    

    解: 对于第一个函数,定义为automatic则其里面的内容都是动态的变量。因此,每次调用之后,都会恢复原值0;

    • 对于第二个函数,定义为static则其里面的内容都是静态的变量。因此,每次调用之后,都会递增;

    • 对于第 个三函数,在module中 所有的函数,默认 情况下都是静态函数函数中的变量都是静态 变量。但是,大多数情况下,我们希望module中的函数是动态函数,这样更加符合软件要求。

发布了14 篇原创文章 · 获赞 10 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Jay_who/article/details/105339446