Randomization and race conditions

In reply to dave_59:

Hi Dave, thank you for your reply! I have tried to run your solution but it produced Fatal error:

(SIGSEGV) Bad handle or reference.

Since then, I have tried to “upgrade” my code to use interfaces…

Package is the same, and I have this interface

interface pix(input bit clk);
	import RGB_specific::*;
	
	RGB_random px_in, px_out;
	
	modport dut(input clk, input px_in, output px_out);
	modport tb(input clk, input px_out, output px_in);
	
endinterface
module DUT(input bit clock, pix.dut pif);

    import RGB_specific::*;
       
    // DUT just checks that data arrives
		always_ff @(posedge clock) begin 
		 $display("px_in: %h %h %h at time %t",
		        pif.px_in.pixel.R, pif.px_in.pixel.G, pif.px_in.pixel.B, $time);
		    //pif.px_out <= pif.px_in;
		    //now copies each of 3 components separetely
		    pif.px_out.pixel.R <= pif.px_in.pixel.R;
		    pif.px_out.pixel.G <= pif.px_in.pixel.G;
		    pif.px_out.pixel.B <= pif.px_in.pixel.B;
		end
  
endmodule : DUT
module testb(input bit clk, pix.tb pif);

    import RGB_specific::*;
         
    //always randomize values of input
    always_ff @(negedge clk) begin
    	if (!pif.px_in.randomize()) $error("randomization failed");
    	
    	$display(pif.px_in.pixel.R,,pif.px_in.pixel.G,,pif.px_in.pixel.B,,$time);
    end
	
		//when any of the output values change, print new output values
		always @(pif.px_out.pixel.R or pif.px_out.pixel.G or pif.px_out.pixel.B) begin
			$display("px_out: %h %h %h at time %t", pif.px_out.pixel.R, pif.px_out.pixel.G, pif.px_out.pixel.B, $time);
		end
   
endmodule : testb
module top;
	bit clk;
	
	always begin
		#5 clk = ~clk;
	end
	
	pix interf_pix(clk);
	
	//initialization of objects of RGB_random class within this interface
	initial begin
		interf_pix.px_in = new;
		interf_pix.px_out = new;
	end
	
	DUT U1(clk,interf_pix);
	testb TST(clk, interf_pix); 
	
	initial begin
		#50
		$display("setgreen");
		interf_pix.px_in.SetGreens;
	end

endmodule

I think this now works as it should.

As I understand creating objects of classes, you need to allocate memory for that objects and thats why you call a constructor (“new”). So, thats why I tried to allocate both objects. And then, I guess only change values in that single object.

Do you mind explaining where my logic is flawed please?

Thanks in advance.