info:
package class_pkg;
class rectangle;
rand int lenght,width;
function new(int l = 10, int w = 20);
lenght = l;
width = w;
$display("from class rectangle:new: length = %d and width = %d",lenght,width);
endfunction
function int area();
$display("from class rectangle:area: area = %d", lenght*width);
return lenght*width;
endfunction
endclass
class square extends rectangle;
function new(int l = 10);
super.new(l,l);
$display("from class square:new: length = %d ",l);
endfunction
endclass
endpackage
module tb;
import class_pkg::*;
rectangle rct1,rct2;
square sqr1,sqr2;
logic clk;
initial begin
clk = 0;
forever clk = #5 !clk;
end
initial begin
rct1 = new(20,30);
sqr1 = new(20);
rct2 = new();
sqr2 = new();
#100;
// by passing cusom value while creating object
$display($time,"Rectangle area = %d",rct1.area());
$display($time,"Square area = %d\n",sqr1.area());
// for default values inside the class
$display($time,"Rectangle area = %d",rct2.area());
$display($time,"Square area = %d\n",sqr2.area());
// using randomization
repeat(10)
begin
@(posedge clk);
rct2.randomize();
sqr2.randomize();
$display($time,"RANDOMIZE: Rectangle area = %d",rct2.area());
$display($time,"RANDOMIZE: Square area = %d",sqr2.area());
end
end
endmodule
output:
run
from class rectangle length = 20 and width = 30
from class rectangle length = 20 and width = 20
from class square length = 20
from class rectangle length = 10 and width = 20
from class rectangle length = 10 and width = 10
from class square length = 10
from class rectangle:area: area = 600
100Rectangle area = 600
from class rectangle:area: area = 400
100Square area = 400
from class rectangle:area: area = 200
100Rectangle area = 200
from class rectangle:area: area = 100
100Square area = 100
quit
End time: 09:31:25 on Sep 10,2022, Elapsed time: 0:00:01
Errors: 0, Warnings: 0
=====================================
but in the above code, if i remove or comment the @(posedge clk) in the repeat block, here is the output and is as expected:
run
from class rectangle length = 20 and width = 30
from class rectangle length = 20 and width = 20
from class square length = 20
from class rectangle length = 10 and width = 20
from class rectangle length = 10 and width = 10
from class square length = 10
from class rectangle:area: area = 600
100Rectangle area = 600
from class rectangle:area: area = 400
100Square area = 400
from class rectangle:area: area = 200
100Rectangle area = 200
from class rectangle:area: area = 100
100Square area = 100
from class rectangle:area: area = 570685860
100RANDOMIZE: Rectangle area = 570685860
from class rectangle:area: area = -131359708
100RANDOMIZE: Square area = -131359708
from class rectangle:area: area = 1874853487
100RANDOMIZE: Rectangle area = 1874853487
from class rectangle:area: area = -1143184658
100RANDOMIZE: Square area = -1143184658
from class rectangle:area: area = 1666416198
100RANDOMIZE: Rectangle area = 1666416198
from class rectangle:area: area = -1357049832
100RANDOMIZE: Square area = -1357049832
from class rectangle:area: area = 1131627911
100RANDOMIZE: Rectangle area = 1131627911
from class rectangle:area: area = 933851776
100RANDOMIZE: Square area = 933851776
from class rectangle:area: area = 1659118711
100RANDOMIZE: Rectangle area = 1659118711
from class rectangle:area: area = -433249897
100RANDOMIZE: Square area = -433249897
from class rectangle:area: area = 725371862
100RANDOMIZE: Rectangle area = 725371862
from class rectangle:area: area = -1707424016
100RANDOMIZE: Square area = -1707424016
from class rectangle:area: area = 806064176
100RANDOMIZE: Rectangle area = 806064176
from class rectangle:area: area = -446701857
100RANDOMIZE: Square area = -446701857
from class rectangle:area: area = 1631562138
100RANDOMIZE: Rectangle area = 1631562138
from class rectangle:area: area = -603291980
100RANDOMIZE: Square area = -603291980
from class rectangle:area: area = -1141090826
100RANDOMIZE: Rectangle area = -1141090826
from class rectangle:area: area = 824529279
100RANDOMIZE: Square area = 824529279
from class rectangle:area: area = -401615008
100RANDOMIZE: Rectangle area = -401615008
from class rectangle:area: area = -1847494794
100RANDOMIZE: Square area = -1847494794
quit
End time: 09:33:51 on Sep 10,2022, Elapsed time: 0:00:01
Errors: 0, Warnings: 0
============================
why randomization not happening in case of delay (@(posedge clk)) ?
any help or suggestions are highly appreciated.
Thank you.