randomize() Metodu
Gün 3: Rastgele Üretim ve Kısıtlamalar | Rastgele üretim, inline constraint, hata kontrolü
Kaynak Kod
// =============================================================================
// GUN 3 - Konu 2: randomize() Metodu ve Hata Kontrolu
// =============================================================================
class Transaction;
rand logic [15:0] address;
rand logic [31:0] data;
rand bit [2:0] size;
rand bit read_write; // 0=read, 1=write
constraint addr_range {
address inside {[16'h0000 : 16'h0FFF]};
}
function void display(string prefix = "");
$display(" %sAddr=0x%04h, Data=0x%08h, Size=%0d, R/W=%s",
prefix, address, data, size,
read_write ? "WRITE" : "READ");
endfunction
endclass
module randomize_metodu;
initial begin
Transaction txn;
int success;
$display("=== randomize() Metodu ===\n");
txn = new();
// --- Temel randomize ---
$display("--- Temel randomize() ---");
repeat (5) begin
success = txn.randomize();
if (success)
txn.display();
else
$display(" HATA: Randomize basarisiz!");
end
// --- randomize() with (inline constraint) ---
$display("\n--- randomize() with (Satir ici kisitlama) ---");
repeat (5) begin
success = txn.randomize() with {
address >= 16'h0100 && address <= 16'h01FF;
data[31:16] == 16'h0000;
read_write == 1;
};
if (success)
txn.display("inline: ");
else
$display(" HATA: Inline randomize basarisiz!");
end
// --- Belirli degiskenleri sabit tutma ---
$display("\n--- Secici Randomize ---");
txn.address = 16'h0ABC;
repeat (3) begin
// address sabit, sadece digerleri rastgele
success = txn.randomize(data, size, read_write);
txn.display("addr sabit: ");
end
// --- randomize() donus degeri kontrolu ---
$display("\n--- Hata Kontrolu ---");
success = txn.randomize() with { address == 16'hFFFF; }; // Constraint ile cakisabilir
if (!success)
$display(" Constraint cakismasi: 0xFFFF aralik disi (0x0000-0x0FFF)");
// Dogru kullanim: assert ile
$display("\n--- assert ile Kontrol ---");
// assert(txn.randomize()) else $fatal(1, "Randomize HATA!");
if (txn.randomize())
txn.display("assert OK: ");
else
$fatal(1, "Randomize basarisiz!");
$display("\n=== randomize() Sonu ===");
$finish;
end
endmodule