After course on SystemVerilog OOP, I made 2 examples to figure the upcast out. What is the difference b1 = new(); and b1 = e1;?
There’s nothing different on the result. Why do we need upcast instead of using new()?
1.Exampple of “b1=new()”.
ackage test_classes;
class base;
bit [7:0] a;
bit [31:0] addr;
function display();
$display("Addr = %0d",addr);
endfunction
function void showit();
$display("BASE(%m) : a = %2d", a);
endfunction
function void seta( bit [7:0] din);
a=din;
endfunction
function bit [7:0] geta();
return a;
endfunction
endclass
class ext extends base;
bit [7:0] data;
bit [7:0] b;
function display();
//super.display();
$display("Data = %0d",data);
endfunction
function void showit();
$display("EXT(%m) : data= %2d a=%2d ", data, b );
endfunction
function void setdata(bit [7:0] din);
data = din;
endfunction
function bit [7:0] getdata();
return data;
endfunction
endclass
endpackage
module test;
import test_classes::*;
base b1, b2;
ext e1, e2;
bit e1good, e2good;
bit [7:0] m_data;
initial begin
e1 =new();
e1.data = 3;
e1.b = 1;
e1.setdata(7);
e1.getdata();
e1.showit();
b1 = new();
// b1= e1;
b1.a = 8;
b1.showit();
end
endmodule
2.Exampple of “b1=e1”.
ackage test_classes;
class base;
bit [7:0] a;
bit [31:0] addr;
function display();
$display("Addr = %0d",addr);
endfunction
function void showit();
$display("BASE(%m) : a = %2d", a);
endfunction
function void seta( bit [7:0] din);
a=din;
endfunction
function bit [7:0] geta();
return a;
endfunction
endclass
class ext extends base;
bit [7:0] data;
bit [7:0] b;
function display();
//super.display();
$display("Data = %0d",data);
endfunction
function void showit();
$display("EXT(%m) : data= %2d a=%2d ", data, b );
endfunction
function void setdata(bit [7:0] din);
data = din;
endfunction
function bit [7:0] getdata();
return data;
endfunction
endclass
endpackage
module test;
import test_classes::*;
base b1, b2;
ext e1, e2;
bit e1good, e2good;
bit [7:0] m_data;
initial begin
e1 =new();
e1.data = 3;
e1.b = 1;
e1.setdata(7);
e1.getdata();
e1.showit();
// b1 = new();
b1= e1;
b1.a = 8;
b1.showit();
end
endmodule