EDA Playground'da Dene

String İşlemleri

Gün 1: SystemVerilog'a Giriş ve Veri Tipleri | String manipülasyonları ve dönüşüm metodları

Kaynak Kod

// =============================================================================
// GUN 1 - Konu 7: String Manipulasyonlari
// =============================================================================

module string_manipulasyonlari;

  string str1, str2, str3;
  string str_arr [$];

  initial begin
    $display("=== SystemVerilog String Islemleri ===\n");

    // --- Temel Atama ve Birlestirme ---
    $display("--- Temel Islemler ---");
    str1 = "Merhaba";
    str2 = "SystemVerilog";
    str3 = {str1, " ", str2, "!"};  // Birlestirme (concatenation)
    $display("  str1 = %s", str1);
    $display("  str2 = %s", str2);
    $display("  Birlesik = %s", str3);

    // --- Uzunluk ---
    $display("\n--- String Uzunlugu ---");
    $display("  \"%s\" uzunluk = %0d", str3, str3.len());

    // --- Karsilastirma ---
    $display("\n--- Karsilastirma ---");
    str1 = "ABC";
    str2 = "abc";
    $display("  \"%s\" == \"%s\" -> %0b", str1, str2, (str1 == str2));
    $display("  icompare (buyuk/kucuk harf duyarsiz) -> %0d", str1.icompare(str2));
    // icompare: 0 ise esit, negatifse str1 < str2

    // --- Alt String ve Karakter Erisimi ---
    $display("\n--- Karakter Erisimi ---");
    str1 = "SystemVerilog";
    $display("  String = %s", str1);
    $display("  str[0] = %c (ASCII: %0d)", str1.getc(0), str1.getc(0));
    $display("  str[6] = %c", str1.getc(6));
    
    // Karakter degistirme
    str1.putc(0, "s");  // S -> s
    $display("  putc(0,'s') -> %s", str1);

    // --- Substring ---
    $display("\n--- Alt String (substr) ---");
    str1 = "Dogrulama_Muhendisi";
    str2 = str1.substr(0, 8);   // 0'dan 8'e kadar (dahil)
    $display("  \"%s\".substr(0,8) = \"%s\"", str1, str2);

    // --- Buyuk/Kucuk Harf ---
    $display("\n--- Harf Donusumu ---");
    str1 = "Hello World";
    $display("  toupper = %s", str1.toupper());
    $display("  tolower = %s", str1.tolower());

    // --- Sayi Donusumleri ---
    $display("\n--- String <-> Sayi Donusumu ---");
    str1 = "255";
    $display("  \"%s\".atoi()  = %0d (decimal)", str1, str1.atoi());
    
    str1 = "FF";
    $display("  \"%s\".atohex() = %0d (hex -> decimal)", str1, str1.atohex());

    str1 = "11111111";
    $display("  \"%s\".atobin() = %0d (bin -> decimal)", str1, str1.atobin());

    str1 = "377";
    $display("  \"%s\".atooct() = %0d (oct -> decimal)", str1, str1.atooct());

    // Sayidan stringe
    begin
      int val = 42;
      str1.itoa(val);
      $display("  itoa(%0d)  = \"%s\"", val, str1);
      str2.hextoa(val);
      $display("  hextoa(%0d) = \"%s\"", val, str2);
    end

    // --- Formatli String ---
    $display("\n--- $sformatf (Formatli String) ---");
    begin
      int addr = 'hABCD;
      int data = 'hDEAD;
      string msg;
      msg = $sformatf("Adres: 0x%04h, Veri: 0x%04h, Zaman: %0t", addr, data, $time);
      $display("  %s", msg);
    end

    // --- String Kuyruk ---
    $display("\n--- String Kuyruk ---");
    str_arr = '{"Generator", "Driver", "Monitor", "Scoreboard"};
    $display("  Testbench Bilesenleri:");
    foreach (str_arr[i])
      $display("    [%0d] %s", i, str_arr[i]);

    $display("\n=== String Islemleri Sonu ===");
    $finish;
  end

endmodule