Jump to content
Sign in to follow this  
Logger

In-line random variable control

Recommended Posts

This section of the LRM is vague.
 
18.11 In-line random variable control

What is the expected behavior for the following code?

class child;
    rand int a;
    rand int b;

    constraint cb {
        a inside {[0:100]};
        b inside {[0:(2*a)]};
    }

endclass

class parent;
    //Uncomment to force desired behavior: rand int a;
    rand child c;
    constraint cb {
        //Uncomment to force desired behavior: a == c.a;
        c.b >= c.a;
    }

//Uncomment to force desired behavior:    function void pre_randomize();
//Uncomment to force desired behavior:        a = c.a;
//Uncomment to force desired behavior:    endfunction

endclass

module top;
    initial begin
        parent p = new;

        child c = new;
        c.a = 10;
        p.c = c;
        void'( p.randomize( c.b ) );
        $write( "c.a == %0d ( expecting 10 )\nc.b == %0d\n\n", c.a, c.b );

        c = new;
        c.a = 50;
        p.c = c;
        void'( p.randomize( c.b ) );
        $write( "c.a == %0d ( expecting 50 )\nc.b == %0d\n\n", c.a, c.b );

        $finish;
    end
endmodule

In my simulator I get:

 

c.a == 71 ( expecting 10 )

c.b == 111
 
c.a == 14 ( expecting 50 )
c.b == 15
 
$finish called from file "nested.sv", line 42.
$finish at simulation time                    0
 

Share this post


Link to post
Share on other sites

Hi Dave,

 

If you just had "c.a = 10; p.c = c;", you would certainly expect c.a (and p.c.a) to be 10 afterwards.  I think the question is why p.randomize(c.B) does not mean that c.a is a state variable, so it is still 10 after the randomize.

 

Mark

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×