SystemVerilog -- 2.3 Data Types ~ SystemVerilog Strings
SystemVerilog Strings
What is a SystemVerilog string ?
数据类型是有序的字符集合。变量的长度是集合中的字符数,这些字符数可以具有动态长度,并且在模拟过程中会发生变化。字符串变量表示字符串的方式与字符串文本不同。使用变量时不会发生截断。string
Syntax
string variable_name [=initial_value];
variable_name是有效的标识符,initial_value可以是字符串文本、空字符串的值“”或字符串数据类型表达式。如果在声明时未指定初始值,则变量默认为“”,即空字符串文本。
SystemVerilog String Example
module tb;
// Declare a string variable called "dialog" to store string literals
// Initialize the variable to "Hello!"
string dialog = "Hello!";
initial begin
// Display the string using %s string format
$display ("%s", dialog);
// Iterate through the string variable to identify individual characters and print
foreach (dialog[i]) begin
$display ("%s", dialog[i]);
end
end
endmodule
模拟日志
ncsim> run
Hello!
H
e
l
l
o
!
ncsim: *W,RNQUIE: Simulation is complete
How are strings represented in Verilog ?
单个 ASCII 字符需要 8 位(1字节),要存储字符串,我们需要与字符串中的字符数一样多的字节。
reg [16*8-1:0] my_string; // Can store 16 characters
my_string = "How are you"; // 5 zeros are padded from MSB, and 11 char are stored
my_string = "How are you doing?" // 19 characters; my_string will get "w are you doing?"
String Operators
Operator | Semantics | |
---|---|---|
Eqquality | Str1 == Str2 | Returns 1 if the two strings are equal and 0 if they are not |
Inequality | Str != Str2 | Returns 1 if the two strings are not equal and 0 if they are |
Comparison | Str1 < Str2 Str1 <= Str2 Str1 > Str2 Str1 >= Str2 |
Returns 1 if the correspondig condition is true and 0 if false |
Concatenation | All strings will be concatenated into one resultant string | |
Replication | {multiplier{Str}} | Replicates the string N number of times, where N is specified by the multiplier |
Indexing | Str[index] | Returns a byte, the ASCII code at the given index. If given index is out of range, it returns 0 |
Methods | Str,.method([args]) | The dot(.) operator is used to call string functions |
Example
module tb;
string firstname = "Joey";
string lastname = "Tribbiani";
initial begin
// String Equality : Check if firstname equals or not equals 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 : Check if length of fistname < length of lastname
if (firstname < lastname)
$display ("firstname=%s is LESS THAN to lastname=%s", firstname, lastname);
// String comparison : Check if length of fistname > length of lastname
if (firstname > lastname)
$display ("firstname=%s is GREATER THAN to lastname=%s", firstname, lastname);
// String concatenation : Join first and last names into a single string
$display ("Full Name = %s", {firstname, "", lastname});
// String Replication
$display ("%s", {3{firstname}});
// String Indexing : Get the ASCII character at index number 2 of both first and last names
$display ("firstnamep[2]=%s lastname[2]=%s", firstnamep[2], lastname[2]);
end
endmodule
模拟日志
ncsim> run
firstname=Joey is NOT EQUAL to lastname=Tribbiani
firstname=Joey is NOT LESS THAN to lastname=Tribbiani
Full Name = Joey Tribbiani
JoeyJoeyJoey
firstname[2]=e lastname[2]=i
ncsim: *W,RNQUIE: Simulation is complete
Basic String Methods
Usage | Defination | Comments |
---|---|---|
str.len() | function int len(); | Returns the number of characters in the string |
str.putc() | function void putc (int i, byte c); | Returns the i-th character in the string with the given character |
str.getc() | function byte getc (int i); | Returns the ASCII code of the i-th character in str |
str.tolower() | function string tolower(); | Returns a string with characters in str converted to lowercase |
str.compare(s) | function int compare(string s); | Compares str and s, as in the ANSI C strcmp function |
str.icompare(s) | function int icompare (string s); | Compares str and s, like the ANSI C strcmp function |
str.substr(i, j) | function string substr (int i, int j); | Returns a new string that is a substring formed by characters in position i through j of str |
Example
module tb;
string str = "Hello World!";
initial begin
string tmp;
// Print length of string "str"
$display ("str.en() = %0d", str.len());
// Assign to tmp variable and put char "s" at index 3
tmp = str;
tmp.pubc (3, "d");
$display ("str.getc(2) = %s (%0d)", str.getc(2), str.getc(2));
// Get the character at index 2
$display ("str.tolower() = %s", str.tolower());
// Convert all characters to lower case
$display ("str.tolower() = %s", str.tolower());
// Comparison
tmp = "Hello World!";
$display ("[tmp, str are same] str.compare(tmp) = %d", str.compare(tmp));
tmp = "Hello are you ?";
$display ([tmp, str are diff] str.compare(tmp) = %d", str.compare(tmp));
// Ignore case comparison
tmp = "hello world!";
$display ("[tmp is in lowercase] str.compare(tmp) = %d", str.compare(tmp));
tmp = "Hello World!";
$display (("[tmp, str are same] str.compare(tmp) = %d", str.compare(tmp));
// Extract new string from i to j
$display ("str.substr(4,8)=%s", str.substr (4,8));
end
endmodule
模拟日志
str.len() = 12
str.putc(3, d) = Hello World!
str.getc(2) = 1 (108)
str.tolower() = hello world!
[tmp, str are same] str.compare(tmp) = 0
[tmp, str are diff] str.compare(tmp) = -1
[tmp is in lowercase] str.compare(tmp) = -1
[tmp, str are same] str.compare(tmp) = 0
str.sbstr(4,8) = o Wor
String Conversion Methods
/ | / | / |
---|---|---|
str.atoi() | function integer atoi(); | Returns the integer corresponding to the ASCII decimal representation in str |
str.atohex() | function integer atohex(); | Interprets the string as hexadecimal |
str.atooct() | function integer atooct(); | Interprets the string as octal |
str.atobin() | function integer atobin(); | Interprets the string as binary |
str.atoreal() | function real atoreal(); | Returns the real number corresponding to the ASCII decimal representation in str |
str.itoa(i) | function void itoa(integer i); | Stores the ASCII decimal representation of i into str |
str.hextoa(i) | function void hextoa(integer i); | Stores the ASCII hexadecimal representation of i into str |
str.octtoa(i) | function void octtoa(integer i); | Stores the ASCII octal representation of i into str |
str.bintoa(i) | function void bintoa(integer i); | Stores the ASCII binary representation of i into str |
str.realtoa(r) | function void realtoa(real r); | Stores the ASCII real representation of i into str |