Generator Modülü

Gün 6: Bitirme Projesi - Bölüm 1 | Rastgele ve yönlendirilmiş test üretici

Kaynak Kod

// =============================================================================
// GUN 6 - Konu 3: Generator - Rastgele Uyarici Uretici
// =============================================================================

class ALU_Generator;
  mailbox #(ALU_Transaction) gen2drv;
  int num_transactions;
  event done;

  function new(mailbox #(ALU_Transaction) mbx, int num = 100);
    this.gen2drv = mbx;
    this.num_transactions = num;
  endfunction

  task run();
    ALU_Transaction txn;
    $display("[Generator] Baslatildi: %0d islem uretilecek", num_transactions);

    for (int i = 0; i < num_transactions; i++) begin
      txn = new();
      assert(txn.randomize()) else $fatal(1, "[Generator] Randomize HATA!");
      gen2drv.put(txn);
    end

    $display("[Generator] Tamamlandi: %0d islem uretildi", num_transactions);
    -> done;
  endtask

  // Yonlendirilmis test: Sadece toplama
  task run_add_only(int num = 20);
    ALU_Transaction txn;
    $display("[Generator] Yonlendirilmis test: Sadece ADD (%0d islem)", num);
    repeat (num) begin
      txn = new();
      assert(txn.randomize() with { opcode == ALU_Transaction::OP_ADD; })
        else $fatal(1, "Randomize HATA!");
      gen2drv.put(txn);
    end
  endtask

  // Yonlendirilmis test: Kose durumlari
  task run_corner_cases();
    ALU_Transaction txn;
    logic [7:0] corners [] = '{0, 1, 8'h7F, 8'h80, 8'hFE, 8'hFF};
    $display("[Generator] Kose durumu testi");

    foreach (corners[i]) begin
      foreach (corners[j]) begin
        for (int op = 0; op < 8; op++) begin
          txn = new();
          assert(txn.randomize() with {
            operand_a == corners[i];
            operand_b == corners[j];
            opcode    == op;
          }) else $fatal(1, "Randomize HATA!");
          gen2drv.put(txn);
        end
      end
    end
    $display("[Generator] Kose durumu testi tamamlandi (%0d islem)", 
             corners.size() * corners.size() * 8);
  endtask
endclass