【问题标题】:System Verilog parameterise class with interfaceSystemverilog 带接口的参数化类
【发布时间】:2020-01-17 08:24:28
【问题描述】:

一个协议可以在不同的物理层上实现,例如

interface async_if;
    logic tx;

    task send(int n);
        tx <= 0; // stop bit
        #10;
        // etc.
    endtask
endinterface

interface clkd_if;
    logic data;
    logic clk;

    task send(int n);
        foreach(n[ii]) begin
            data <= n[ii];
            clk <= 1;
            #10 clk <= 0;
            #10 ;
        end endtask
endinterface

有没有一种方法可以通过接口参数化 System Verilog 类?以下似乎无法编译,因为 System Verilog 不将接口视为类型。

class sender#(type I);
    virtual I if;
    function void send(int n);
        if.send(n);
    endfunction
endclass

【问题讨论】:

  • 奇怪的是,接口不是类型,而是虚拟接口。

标签: verilog system-verilog verification hdl


【解决方案1】:

您可以使用 SystemVerilog 中的接口对类进行参数化。你只是做得不太对。这有效:

  class sender #(type I);
    I vif;  // 'if' is a reserved word and you don't want the 'virtual' here
    function void send(int n);
        vif.send(n);
    endfunction     
  endclass

那么你可以:

  sender#(virtual async_if) s = new;

https://www.edaplayground.com/home

module M;

  interface async_if;
    logic tx;

    task send(int n);
        tx <= 0; // stop bit
        #10;
        // etc.
    endtask
  endinterface

  interface clkd_if;
    logic data;
    logic clk;

    task send(int n);
        foreach(n[ii]) begin
            data <= n[ii];
            clk <= 1;
            #10 clk <= 0;
            #10 ;
        end
    endtask
  endinterface

  class sender #(type I);
    I vif;
    function void send(int n);
        vif.send(n);
    endfunction     
  endclass

  sender#(virtual async_if) s = new;

endmodule

【讨论】:

  • 那不是接口。那是一个虚拟接口。
  • @user234461 如果上述解决方案无法解决,您想做什么?
  • 那无关紧要
【解决方案2】:

不,因为正如您所说,它不是数据类型(请参阅SystemVerilog LRM 部分 A.2.2.1 了解什么是数据类型)。

不确定您的用例是什么,但看起来很像 polymorphic interfaces 是您想要的。它涉及在实现基类virtual class 的方法的接口中嵌入一个类。

【讨论】: