4

我想知道是否有更好更直接的方法来为从参数化类生成的一组类使用多态性。以下系统verilog代码有效。有没有更优雅的方式?编辑:我可以在 C++ 中实现类似的东西吗?

`define OVERRIDE_PARAMETER_CPU parameter WIDTH=32 ;
`define OVERRIDE_PARAMETER_GFX parameter WIDTH=16 ;     

class Req #(parameter WIDTH=8);
    bit[WIDTH-1:0]  Address;
    virtual function Print;
        $display("Generic: %x[%0d]",Address,WIDTH);
    endfunction
endclass

typedef Req#(8) generic_req ;

class CPU_Req extends generic_req;  
    `OVERRIDE_PARAMETER_CPU
    function Print;
        $display("CPU: %x[%0d]",Address,WIDTH);
    endfunction
endclass

class GFX_Req extends generic_req;          
    `OVERRIDE_PARAMETER_GFX
    function Print;
        $display("GFX: %x[%0d]",Address,WIDTH);
    endfunction
endclass


program Test ;

    generic_req Req_Q[$],Temp,Generic_Req;    
    CPU_Req     C_Req;
    GFX_Req     G_Req;

    initial
    begin
        C_Req=new();
        G_Req=new();
        Generic_Req=new();
        C_Req.Address=32'h1234;
        G_Req.Address=32'hFF;
        Generic_Req.Address=32'h0;

        Req_Q.push_back(Generic_Req);
        Req_Q.push_back(C_Req);
        Req_Q.push_back(G_Req);

        while(Req_Q.size()!=0)
        begin
            Temp=Req_Q.pop_front();
            Temp.Print();
        end    
    end    
endprogram
4

2 回答 2

1

我对您的代码做了一些更改。我不确定这是否是您要寻找的。

class Req #(parameter WIDTH=8);
   bit[WIDTH-1:0]  Address;
   virtual function void Print;
      $display("Generic: %x[%0d]",Address,WIDTH);
   endfunction
endclass

//class CPU_Req extends generic_req;  
class CPU_Req #(parameter WIDTH=16) extends Req#(WIDTH);
     function void Print;
        $display("CPU: %x[%0d]",Address,WIDTH);
     endfunction
endclass

class GFX_Req #(parameter WIDTH=24) extends Req#(WIDTH);          
     function void Print;
        $display("GFX: %x[%0d]",Address,WIDTH);
     endfunction
endclass


program Test ;

   Req            Generic_Req;
   CPU_Req #(32)  C_Req32;
   CPU_Req        C_Req16;
   GFX_Req #(128) G_Req128;

   initial
     begin
        C_Req32     = new();
        C_Req16     = new();
        G_Req128    = new();
        Generic_Req = new();

        C_Req32.Address  = 32'h12345678;
    C_Req16.Address  = 32'h12345678;
        G_Req128.Address = {2{64'h1234_5678_9ABC_DEF0}};
        Generic_Req.Address=32'h12345678;

    C_Req32.Print();
    C_Req16.Print();
    G_Req128.Print();
    Generic_Req.Print();

     end    
endprogram

结果:

# CPU: 12345678[32]
# CPU: 5678[16]
# GFX: 123456789abcdef0123456789abcdef0[128]
# Generic: 78[8]
于 2013-03-08T03:56:42.447 回答
0

以下代码按预期工作。

class BaseReq;
   virtual function void Print;
      $display("Base:");
   endfunction
endclass

class Req #(parameter WIDTH=6) extends BaseReq;
   bit[WIDTH-1:0]  Address;
   function void Print;
      $display("Generic: %x[%0d][%0d]",Address,WIDTH,$bits(Address));
   endfunction
endclass

class CPU_Req #(parameter WIDTH=16) extends Req #(WIDTH);
     function void Print;
        $display("CPU: %x[%0d][%0d]",Address,WIDTH,$bits(Address));
     endfunction
endclass

class GFX_Req #(parameter WIDTH=24) extends Req #(WIDTH);          
     function void Print;
        $display("GFX: %x[%0d][%0d]",Address,WIDTH,$bits(Address));
     endfunction
endclass


program Test ;

   BaseReq        Req_Q[$],Temp;
   Req            Generic_Req;
   CPU_Req #(32)  C_Req32;
   CPU_Req        C_Req16;
   GFX_Req #(128) G_Req128;

   initial
     begin
        Generic_Req = new();
        C_Req32     = new();
        C_Req16     = new();
        G_Req128    = new();
        Generic_Req = new();

        Generic_Req.Address = 32'h12345678;
        C_Req32.Address     = 32'h12345678;
        C_Req16.Address     = 32'h12345678;
        G_Req128.Address    = {2{64'h1234_5678_9ABC_DEF0}};

        Req_Q.push_back(Generic_Req);
        Req_Q.push_back(C_Req32);
        Req_Q.push_back(C_Req16);
        Req_Q.push_back(G_Req128);

        while(Req_Q.size!=0)
        begin
            Temp=Req_Q.pop_front();
            Temp.Print();
        end
     end    
endprogram
于 2013-04-18T20:58:42.163 回答