我想知道是否有更好的更直接的方法来利用从参数化类生成的一组类的多态性。 以下系统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
答案 0 :(得分: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]
答案 1 :(得分: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