Q. How many combinations can be there for the below scenario?
There are 3 flights and 8 passengers (a,b,c,d,e,f,g,h)
1.a and b should always be together
2. each flight needs to fly . minimum passengers that can go in an flights are = 1 ,
Maximum passengers that can go in an flights are = 4 .
Answer :
Instaed of manually thinking of all scenarios , we can write in in system verilog . (using constarints) .
1. create an enum of passengers
typedef enum {a,b,c,d,e,f,g,h} passenger ;
2. create a queue called flight like this :
passenger flight[];
// Code your testbench here
// or browse Examples
class FlightScheduler;
// Passenger list
typedef enum {a, b, c, d, e, f, g, h} passengers_t;
// The flights array stores the flight allocation for each passenger
rand passengers_t flights[8]; // Array of flight assignments for 8 passengers
rand int flight_count[3]; // Number of passengers on each flight (0, 1, 2)
constraint flight_constraints {
// Each passenger must be assigned to one of the three flights
foreach (flights[i]) flights[i] inside {0, 1, 2};
// Ensure passengers 'a' and 'b' always travel together
flights[0] == flights[1]; // Both passenger a and b travel on the same flight
// Count how many passengers are on each flight
foreach (flight_count[i]) {
flight_count[i] == (flights.count(i)); // Count passengers in flight i
}
// Each flight must have between 1 and 4 passengers
foreach (flight_count[i]) flight_count[i] inside {[1:4]};
}
// Display the flight assignments
function void display_flight_allocation();
$display("Flight assignments:");
foreach (flights[i]) begin
$display("Passenger %0d -> Flight %0d", i, flights[i]);
end
$display("Flights distribution: Flight 0: %0d, Flight 1: %0d, Flight 2: %0d",
flight_count[0], flight_count[1], flight_count[2]);
endfunction
endclass
// Testbench
module tb_flight_scheduler;
initial begin
FlightScheduler scheduler = new();
if (scheduler.randomize()) begin
scheduler.display_flight_allocation();
end else begin
$display("Failed to randomize!");
end
end
endmodule
Q. 3*3 Array question :
class A ;
rand int mem[3][3];
constraint c1{
unique{mem};
foreach(mem[i,j])
{
mem[i][j] inside {[1:9]};
}
};
constraint c2{
foreach(mem[i])
{
foreach(mem[k])
{
mem[i].sum() == mem[k].sum();
}
}
};
constraint c3{
foreach(mem[,j])
{
foreach(mem[,k])
{
mem.sum() with (mem[item.index][j]) == mem.sum() with (mem[item.index][k]);
}
}
};
endclass
module t();
A a_h ;
initial begin
a_h = new ;
a_h.randomize();
$display("value is %p", a_h.mem);
end
endmodule
No comments:
Post a Comment