Jump to content

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
 
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...