RANDOMIZATION AND THREADS
Scope randomisation
Soft constraint
Threads and diff types of threads
Difference between fort joint , any and none
Which thread does disable dosent work
Difference btw mail box and semaphore
Different types of mail box
. Static Constraints
• Purpose: Let you create constraints that always apply to a variable, unless you
explicitly turn them off.
Extern Constraints
• Purpose: You can define a constraint for a class outside the class definition.
If static constraint is enabled then it’ll be applied for all the object in the class
If disabled then all the constraints for all the objects will be disabled
Scope randomization in SystemVerilog refers to randomizing variables that are not members of a
class, but exist in the current procedural scope (for example, local variables in a function or task).
This is achieved using the std::randomize() function.
Events: Used to synchronize threads so some operation only proceeds when a certain condition is
met.
Randomization:
Used to generate random values for variables in verification, helping test many scenarios.
It’s done using SystemVerilog methods like $random, $urandom, or class randomize().
Constraints and Types:
Constraints restrict values of random variables so only valid scenarios are generated.
Types include named, inline, soft, set membership, distribution, and conditional constraints.
Difference between rand_mode and constraint_mode:
rand_mode enables or disables randomization for a variable.
constraint_mode enables or disables a specific constraint block during randomization.
When to use inheritance in constraint:
Use inheritance to extend or modify constraints in a child class based on the parent class.
It helps create specialized test scenarios without rewriting all constraints.
Constraint overriding:
If a constraint in a child class has the same name as the parent’s, only the child’s applies.
This lets you easily change randomization behavior for derived classes.
Scope randomization:
Randomizes local (non-class) variables directly in the current scope using std::randomize() function,
not requiring class members.
You can apply inline constraints with with , just like on class objects.
Soft constraint:
A soft constraint (using soft keyword) gives a preferred condition but allows overriding by other
(hard) constraints.
It’s useful when you want defaults that can be suppressed by inline constraints in testbenches.
Threads and different types of threads:
Each initial, always block, or branch of a fork…join creates a parallel thread of execution in
SystemVerilog.
Threads can be managed and synchronized using constructs like fork…join, fork…join_any, and fork…
join_none.
Difference between fork join, join_any, and join_none:
• fork...join : Waits until all threads finish before continuing (full synchronization).
• fork...join_any : Waits until any one thread finishes, then continues; the rest run in
background.
• fork...join_none : Does not wait for any thread, next code executes immediately (no
wait).
Which thread does disable not work on:
Disable only works on named blocks or tasks at the same hierarchical scope; it cannot disable
unnamed blocks.
It can kill all threads with the same given name in the same scope; be careful when threads have
duplicate names in parallel forks.
Difference between Mailbox and Semaphore (2 lines):
A mailbox is used for passing data (messages) safely between two or more processes, acting like a
FIFO queue for inter-process communication.
A semaphore is only for synchronization and mutual exclusion—controlling access to shared
resources but it cannot transfer data between processes.
Different Types of Mailbox (2 lines):
Mailboxes can be generic (type-less), accepting any data type, or parameterized, accepting only a
specific type; both can be bounded (fixed max size) or unbounded (unlimited size).
Bounded mailboxes block on overflow, while unbounded mailboxes can grow dynamically with no
size limit.
class SeriesConstraint;
rand int value;
// Constraint to restrict value to the given series
constraint series_c {
value inside {20, 25, 27, 30, 36, 40, 45};
endclass
module test;
initial begin
SeriesConstraint obj = new();
repeat (10) begin
if (obj.randomize())
$display("Random value from series = %0d", obj.value);
else
$display("Randomization failed!");
end
end
endmodule
class packet;
rand bit [3:0] addr;
// Soft constraint syntax
constraint addr_range { soft addr > 6; }
endclass
event ev; // Declare an event
-> ev; // Trigger an event
@(ev); // Wait for the event (blocking)
wait(ev.triggered);// Wait until the event is triggered (safe)
driver = new(ev); // Pass event as an argument to methods/tasks
mailbox #(transaction) mbx; // Declare a typed mailbox
mbx = new(); // Create mailbox object
mbx.put(trans_h); // Put transaction into mailbox
mbx.get(trans_h); // Get and remove transaction from mailbox
success = mbx.try_get(trans_h); // Non-blocking get
mbx.peek(trans_h); // Peek transaction without removing (blocking)
success = mbx.try_peek(trans_h); // Non-blocking peek
count = mbx.num(); // Get number of transactions in mailbox