SystemVerilog Strings

什么是SystemVerilog字符串?

字符串数据类型是字符的有序集合。 字符串变量的长度是集合中字符的数量,这些字符可以具有动态长度,并且在仿真过程中会有所变化。 字符串变量不能以与字符串文字相同的方式表示字符串。 使用字符串变量时,不会发生截断。

Syntax(句法规则)

string  variable_name [= initial_value];

variable_name是有效的标识符,可选的initial_value可以是字符串文字,空字符串的值“”或字符串数​​据类型表达式。 如果在声明时未指定初始值,则变量默认为“”,这是一个空字符串文字。

SystemVerilog String Example

module tb;  
  // 声明一个名为“ dialog”的字符串变量以存储字符串文字
  // 将变量初始化为“Hello”
  string   dialog = "Hello!";            
 
  initial begin
    // 使用%s字符串格式显示字符串
    $display ("%s", dialog);
 
    // 遍历字符串变量以识别各个字符并打印
    foreach (dialog[i]) begin
      $display ("%s", dialog[i]);
    end
  end
endmodule
Simulation Log
ncsim> run
Hello!
H
e
l
l
o
!
ncsim: *W,RNQUIE: Simulation is complete.

Verilog中的字符串如何表示?

单个ASCII字符需要8位(1个字节),要存储一个字符串,我们需要的字节数与该字符串中的字符数一样多

 reg  [16*8-1:0]   my_string;           // 可存储16个字符
 
  my_string = "How are you";             //从MSB填充5个零,并存储11个字符
  my_string = "How are you doing?"       //19个字符; my_string将显示为“are you doing?  "

String Operators

在这里插入图片描述

Example

module tb;
  string firstname = "Joey";
  string lastname  = "Tribbiani";
 
  initial begin
    // String Equality : 检查 firstname是否等于lastname
    if (firstname == lastname)
      $display ("firstname=%s is EQUAL to lastname=%s", firstname, lastname);
 
    if (firstname != lastname)
      $display ("firstname=%s is NOT EQUAL to lastname=%s", firstname, lastname);
 
    // String comparison :检查firstname 的长度<或>lastname的长度
    if (firstname < lastname)
      $display ("firstname=%s is LESS THAN lastname=%s", firstname, lastname);
 
    if (firstname > lastname)
      $display ("firstname=%s is GREATER THAN lastname=%s", firstname, lastname);
 
    // String concatenation : 将firstname 和lastname连接成一个字符串
    $display ("Full Name = %s", {firstname, " ", lastname});
 
    // String Replication(字符串复制)
    $display ("%s", {3{firstname}});
 
    // String Indexing : 获取firstname和lastname的索引号2处的ASCII字符
    $display ("firstname[2]=%s lastname[2]=%s", firstname[2], lastname[2]);
 
  end
endmodule
Simulation Log
ncsim> run
firstname=Joey is NOT EQUAL to lastname=Tribbiani
firstname=Joey is LESS THAN lastname=Tribbiani
Full Name = Joey Tribbiani
JoeyJoeyJoey
firstname[2]=e lastname[2]=i
ncsim: *W,RNQUIE: Simulation is complete.

Basic String Methods(基本的字符串方法)

SystemVerilog还包括许多使用字符串的特殊方法,这些方法使用内置的方法表示法。
在这里插入图片描述

Usage 用法 Definition定义 Comments注释
str.len() function int len() 返回字符串中的字符数
str.putc() function void putc (int i, byte c) 用给定字符替换字符串中的第i个字符
str.getc() function byte getc (int i) 返回str中第i个字符的ASCII码
str.tolower() function string tolower() 返回字符串,其中str中的字符转换为小写
str.compare(s) function int compare (string s) 比较str和s
str.icompare(s) function int icompare (string s) 比较str和s,例如ANSI C strcmp函数
str.substr (i, j) function string substr (int i, int j) 返回一个新字符串,它是由str的位置i到j的字符组成的子字符串

Example

module tb;
   string str = "Hello World!";
 
   initial begin
       string tmp;//字符串变量tmp
 
    // 打印字符串“ str”的长度
      $display ("str.len() = %0d", str.len());
 
      // 分配给tmp变量并将char“ d”放在索引3
      tmp = str;
      tmp.putc (3,"d");
      $display ("str.putc(3, d) = %s", tmp);
 
      // 获取索引2处的字符
      $display ("str.getc(2) = %s (%0d)", str.getc(2), str.getc(2));
 
      // 将所有字符转换为小写
      $display ("str.tolower() = %s", str.tolower());
 
      tmp = "Hello World!";
      $display ("[tmp,str are same] str.compare(tmp) = %0d", str.compare(tmp));
      tmp = "How are you ?";
      $display ("[tmp,str are diff] str.compare(tmp) = %0d", str.compare(tmp));
 
      // 忽略大小写比较
      tmp = "hello world!";
      $display ("[tmp is in lowercase] str.compare(tmp) = %0d", str.compare(tmp));
      tmp = "Hello World!";
      $display ("[tmp,str are same] str.compare(tmp) = %0d", str.compare(tmp));
 
      //从i提取到j 提取到新字符串
      $display ("str.substr(4,8) = %s", str.substr (4,8));
 
   end
endmodule

参考文献:
【1】https://www.chipverify.com/systemverilog/systemverilog-strings

发布了91 篇原创文章 · 获赞 7 · 访问量 5269

猜你喜欢

转载自blog.csdn.net/qq_43042339/article/details/104450089