SystemVerilog文字列



Systemverilog Strings



SystemVerilog文字列とは何ですか?

文字列データ型は、順序付けられた文字のコレクションです。文字列変数の長さは、セット内の文字数です。これらの文字は動的な長さを持つことができ、シミュレーション中に変更されます。文字列変数は、文字列リテラルと同じ方法で文字列を表すことはできません。文字列変数を使用する場合、切り捨ては発生しません。

構文(構文規則)

string variable_name [= initial_value]

variable_nameは有効な識別子です。オプションのinitial_valueは、文字列リテラル、空の文字列値 ''、または文字列データ型式にすることができます。宣言時に初期値が指定されていない場合、変数はデフォルトで ''になります。これは、空の文字列リテラルです。



SystemVerilog文字列の例

module tb // Declare a string variable named 'dialog' to store the string literal // Initialize the variable to 'Hello' string dialog = 'Hello!' initial begin // Use %s string format to display the string $display ('%s', dialog) // Traverse the string variable to identify each character and print 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で文字列を表す方法は?

1つのASCII文字には8ビット(1バイト)が必要です。 文字列を格納するには、文字列の文字数と同じ数のバイトが必要です

reg [16*8-1:0] my_string // 16 characters can be stored my_string = 'How are you' //Fill 5 zeros from MSB and store 11 characters my_string = 'How are you doing?' //19 characters my_string will be displayed as 'are you doing?'

文字列演算子

画像



module tb string firstname = 'Joey' string lastname = 'Tribbiani' initial begin // String Equality: Check if firstname is equal to 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 the length of firstname the length of 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: connect firstname and lastname into a string $display ('Full Name = %s', {firstname, ' ', lastname}) // String Replication $display ('%s', {3{firstname}}) // String Indexing: Get the ASCII characters at index 2 of firstname and lastname $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.

基本的な文字列メソッド

SystemVerilogには、組み込みのメソッド表記を使用する文字列を使用する多くの特別なメソッドも含まれています。
画像

使用法 定義 コメント
str.len() 関数intlen() 文字列の文字数を返します
str.putc() 関数voidputc(int i、byte c) 文字列のi番目の文字を指定された文字に置き換えます
- -
str.getc() 関数バイトgetc(int i) strのi番目の文字のASCIIコードを返します
str.tolower() 関数文字列tolower() str内の文字が小文字に変換される文字列を返します
- -
str.compare(s) 関数int比較(文字列s) strとsを比較する
str.icompare(s) function int icompare(string s) ANSI Cstrcmp関数などのstrとsを比較します
- -
str.substr(i、j) 関数文字列substr(int i、int j) 新しい文字列を返します。これは、str位置iからjまでの文字で構成される部分文字列です。

module tb string str = 'Hello World!' initial begin string tmp//String variable tmp // print the length of the string 'str' $display ('str.len() = %0d', str.len()) // Assign to tmp variable and put char 'd' in index 3 tmp = str tmp.putc (3,'d') $display ('str.putc(3, d) = %s', tmp) // Get the character at index 2 $display ('str.getc(2) = %s (%0d)', str.getc(2), str.getc(2)) // Convert all characters to lower case $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)) // Ignore case comparison 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)) //Extract from i to j to extract a new string $display ('str.substr(4,8) = %s', str.substr (4,8)) end endmodule

参照:
【1】https://www.chipverify.com/systemverilog/systemverilog-strings