轉載:https://blog.csdn.net/Holden_Liu/article/details/100727957
傳統的Veriog僅僅支持文字表述上的字符串, 而SystemVerilog將字符串作為了內建的數據類型。類似C++的std::string類型,SystemVerilog字符串類型支持很多操作和函數。
SytemVerilog字符串類型支持的操作和方法:
一些有用的系統任務
SytemVerilog字符串類型支持的操作和方法 |
|
操作 |
描述 |
strA==strB |
相等——操作數可以是字符串類型或者字符串文字,如果兩個字符串由相同的字符序列組成,則返回1. |
strA!=strB |
不等於—— 對相等操作取反 |
strA<strB strA<=strB strA>strB strA>=strB |
比較——如果相應的按字典序的比較條件滿足時,則返回1 |
{strA,strB,..,strN} |
連接——擴展指定的字符串。操作符可以是字符串類型,也可以是字符串文字。當所有的操作符都是字符串文字時,此操作完成整體的連接,結果也是一個字符串文字。 |
str.len() |
長度——返回代表字符串的長度的整數 |
str.putc(i, c) |
字符輸入——將字符串str中的第i個字符替換成字符c。i必須是一個整數,c必須是一個字節類型的字符 |
str.getc(i) |
獲取字符——返回字符串str的第i個字符。i必須是整數,返回的字符表示為一個字節。 |
str.toupper() |
轉成大寫——返回str中所有字符變成大寫的字符串 |
str.tolower() |
轉成小寫——返回str中所有字符變成小寫的字符串
|
strA.compare(strB) |
比較——比較strA和strB.從第一個字符開始比較。如果相等,則繼續后續字符,知道兩者有不同或者到達某個字符串的結尾。如果相等,返回’0‘;如果strA在strB之后,則返回整數;如果strA在strB之前,則返回負數. |
strA.icompare(strB) |
比較——和compare方法類似,但是不關心大小寫。 |
str.substr(i, j) |
子串——i和j都是整數,返回一個新的字符串,由str的第i和和第j個中間的所有字符組成 |
str.atoi() |
字符串轉整數——返回一個32bit整數(不考慮此字符串表示的數字的長度),對於atoi, 字符串將被看作十進制;對於atoh,十六進制;對於atooct,八進制;對於atob,二進制。對於比較大的數字,使用系統任務$sscanf更加合適[1]. |
str.atoreal() |
字符串轉實數——此方法返回字符串str表示的實數 |
str.itoa(i) |
整數轉字符串——atoi,atohex,atooct,atobin的反運算。此系列方法輸入一個整數i,將其對應的表示方式存在字符串str中。 |
str.realtoa(r) |
實數轉字符串——atoreal的反運算。此方法輸入一個實數r,將其對應的表示方式存在字符串str中。 |
字符串在構建日志信息的時候非常有用。有許多很方便有用的SystemVerilog系統函數,以下列舉其中的幾個:
Useful SystemVerilog System Tasks
Useful SystemVerilog System Tasks | |||||||||
---|---|---|---|---|---|---|---|---|---|
Task Name | Description | ||||||||
$sscanf(str,format,args); | $sscanf 將字符串按照某個模板格式進行掃描,其字符串格式和C語言中的printf()函數類似 |
||||||||
$sformat(str,format,args); | $sformat是$sscanf的反函數。將字符串按照給定的格式填入相應的參數args中 |
||||||||
$display(format,args); | $display就是Verilog的printf語句,在stdout上顯示格式化的字符串 |
||||||||
$sformatf(format,args); | $sformatf任務和$sformat相似,除了其返回字符串結果。字符串作為$sformatf的返回值,而不是像$sformt一樣放在第一個參數上。 |
注意
- 一個流行的$sformatf的替代方式是$psprintf. 它實際上是由Vera遺留下來的。$sformtf 在后來成為了SystemVerilog的語言標准。然而,大部分流行的SystemVerilog編譯器都支持$psprintf,盡管它沒有成為標准。如果想符合標准,請使用$sformatf.
參考
- ↑ Dave Rich, Verification Guild, 2007, "String manipulation through atohex();", accessed on August 22 2010
- ↑ Ben Cohen, Verification Guild, 2006, "$psprintf // In VCS not in P1800", accessed on August 22 201
int fh; str_num.itas(num); fh = $fopen({"aa_",str_num,"_bbb.log"},“w”); $fdisplay(fh,"data:%h, time:%0t",data,$time);
string rm_file; $sformat(rm_file,"rm_out_data_%0d.log",cmd_cnt);//將打印字符串放入rm_file int rm_out_file; // as file handle rm_out_file = $fopen(rm_file); // $fclose(rm_out_file);
$sscanf
http://docs.roxen.com/pike/7.0/tutorial/strings/sscanf.xml
$sformat和$sformatf區別
https://baijiahao.baidu.com/s?id=1665683787389360422&wfr=spider&for=pc