verilog换行太长代码_Verilog 之 File I/O task and function
不點藍字,何來故事?
許久之前關(guān)于Verilog I/O操作的一篇筆記梳理,再此與諸君共分享。0I/O操作類型????verilog中關(guān)于文件操作的任務(wù)和函數(shù)主要分為四類:
????(1)打開和關(guān)閉文件的任務(wù)和函數(shù)
????(2) 向文件中輸入信息的任務(wù)
????(3) 向變量中輸入信息的任務(wù)
????(4) 從文件中讀取數(shù)據(jù)并寫入變量或存儲器中
1打開和關(guān)閉文件的任務(wù)和函數(shù)fd=$fopen("file_name",type)
????該函數(shù)主要用于按照type定義的方式打開"file_name"指定的文件,并返回32比特的文件描述符。type的類型有:
????默認方式為以“w”或“wb”方式打開。注意"w","wb","w+","w+b","wb+"打開文件將會清空文件原有數(shù)據(jù)。其中“b”用于區(qū)別文本文件和二進制文件。如果一個文件因為某種無法打開,將會返回0,通過$ferror可用于檢測失敗原因。
$fclose(fd)
????該函數(shù)主要用于關(guān)閉文件。文件一旦關(guān)閉,$fmonitor 、$fstrobe等任務(wù)將會自動終止。
2向文件中輸入信息file_output_task_name(multi_channel_descriptior|fd,[,list_of_arguments])
file_output_task_name ::=$fdisplay | $fdisplayb | $fdisplayh | $fdisplayo| $fwrite | $fwriteb | $fwriteh | $fwriteo| $fstrobe | $fstrobeb | $fstrobeh | $fstrobeo| $fmonitor | $fmonitorb | $fmonitorh | $fmonitoro
????$fmonitor只要有變化就一直記錄
????$fmonitor(file_id,"%format_char", parameter);
????$fmonitor(file_id, "%m:%t in1=%d o1=%h", $time, in1, o1);
????只允許有一個$monitor 進程,但同時可以運行任意數(shù)量的$fmonitor 進程.
????$fwrite需要觸發(fā)條件才記錄,不自動換行
????$fwrite(file_id,"%format_char", parameter);
????$fdisplay需要觸發(fā)條件才記錄
????$fdisplay(file_id,"%format_char", parameter);
????$fstrobe();當該時刻的所有事件處理完后,在這個時間步的結(jié)尾寫入,可以確保所寫的數(shù)據(jù)時無誤的。推薦使用。
3向變量中輸入信息string_output_tasks ::=string_output_task_name ( output_reg , list_of_arguments ) ;
string_output_task_name ::=$swrite | $swriteb | $swriteh | $swriteo
variable_format_string_output_task ::=
$sformat ( output_reg , format_string , list_of_arguments ) ;
????$swrite、$sformat用于以制定格式向寄存器中寫入字符串
string s1,s2;$swriteb(s1,"a","b");$sformat(s2,"%s%s","a","b");$display("$sformat: %s ",s2);$display("$swrite: %s ",s1);4從文件中讀取數(shù)據(jù)c=$fgetc(fd)——從文件中一次讀取一個字符
????如果從文件中讀取發(fā)生錯誤時,c將被設(shè)為EOF(-1).通過$ferror可獲取錯誤原因,c的位寬應(yīng)大于8位。采取這種格式讀取數(shù)據(jù)文本文件中的空格、標點符號等也會被讀出來。
code=$ungetc(c,fd);
????向fd文件的緩沖區(qū)內(nèi)插入字符c,c將會在下次調(diào)用$fgetc時返回,調(diào)用該函數(shù)并不會改變文件本身內(nèi)容。當發(fā)生錯誤時code為EOF,否則為0。
從文件中讀取一行
integer code;
code=$fgets(str,fd);
????從fd指定的文件中讀取數(shù)據(jù)到str直至str填滿或出現(xiàn)換行符或出現(xiàn)EOF。若str長度不是整數(shù)個bytes,則高位部分數(shù)據(jù)將被舍去。若讀取失敗,則code返回0,否則code將返回讀取的字符數(shù)。
按照固定格式從文件中讀取數(shù)據(jù)
integer code;
code=$fscanf(fd,format,args);//從fd文件中按照format制定的格式讀取數(shù)據(jù)到args中
code=$sscanf(fd,format,args);//從寄存器中按照format制定的格式讀取數(shù)據(jù)到args中
????空白字符(空格、制表符、換行符)可用于跳過空白符讀取非空白字符(對于標點符號等無效)。
????%m能夠返回當前仿真路徑,而不返回文件中的字符
按照二進制格式讀取數(shù)據(jù)
integer code = $fread( myreg,fd);
integer code = $fread( mem, fd);
integer code = $fread( mem, fd, start);
integer code = $fread( mem, fd, start, count);
integer code = $fread( mem, fd, , count);
????讀取二進制流,將數(shù)據(jù)從fd讀向myreg或mem,不可讀x,z.start是指mem的位置,count指待讀取的長度。
??? For start = 12 and the memory up[10:20],the first data would be loaded at up[12]. For the memory down[20:10],the first location loaded would be down[12], then down[13]。
????缺省情況下fread將mem填滿或讀完截止。
????文件中的數(shù)據(jù)逐字節(jié)讀取。使用一個8位寬的存儲器加載一個字節(jié),而使用每個存儲器字2個字節(jié)寬加載9位寬的存儲器。數(shù)據(jù)是以大尾數(shù)方式從文件中讀取;第一個字節(jié)讀取用于填充最重要的位置存儲元件。如果內(nèi)存寬度不能被8(8,16,24,32)整除,則由于截斷并不是文件中的所有數(shù)據(jù)都加載到內(nèi)存中。
5文件定位integer pos ;
pos = $ftell ( fd );
????返回fd文件當前位置距文件首部偏移量(按字節(jié)計算),可以用于后續(xù)的$fseek使用,以將文件重新定位到此點。重定位將會取消$ungetc操作。
code=$fseek(fd,offest,operation);
code = $rewind ( fd );
????$fseek()設(shè)置文件fd的下一輸入或輸出位置。
????????0:設(shè)置位置到偏移地址
??????? 1:設(shè)置位置到當前位置加偏移量
??????? 2:設(shè)置位置到文件結(jié)束位置加偏移量(向前)
????$rewind()等價于$fseek(fd,0,0);
????When a file is opened forappend (that is, when type is "a", or "a+"), it isimpossible to overwrite information already in the file.when output is written to the file, the current file pointer is disregarded. All output is written at the end of the file and causes the file pointer to be repositioned at the end of the output.
6緩存區(qū)沖刷$fflush ( mcd );
$fflush ( fd );
$fflush ( );
????將所有緩沖區(qū)的數(shù)據(jù)寫入制定的mcd或fd,若參數(shù)缺省,沖刷寫入所有打開的文件。
7I/O狀態(tài)查詢I/O error status
integer errno;
errno=$ferror(fd,str);
????描述的錯誤信息字符串將被存放在字符串str中。如果沒有錯誤,errno將為0,str將被清空。
8檢測文件結(jié)尾integer code;
code=$feof(fd);
????當檢測到文件結(jié)尾時,返回1,否則返回零。
8加載文件到存儲區(qū)$readmemb ( " file_name ", memory_name [ , start_addr [ , finish_addr ] ] ) ;
$readmemh ( " file_name " , memory_name [ , start_addr [ , finish_addr] ] ) ;
????文件應(yīng)該只包括空白符、注釋、二進制或十六進制數(shù)據(jù)。
精彩推薦SpinalHDL資料匯總SpinalHDL—優(yōu)雅地實現(xiàn)總線寄存器讀寫SpinalHDL代碼組織結(jié)構(gòu)之Component換個名字混江湖SpinalHDL—覆蓋率收集SpinalHDL—測試平臺搭建兄弟,要幾段?打個拍,握個手可以么SpinalHDL—柳暗花明又一村FPGA圖像處理——老戲新說山水有相逢—和大牛成為同事SpinalHDL——環(huán)境搭建亂花漸欲迷人眼—Vivado之SynthesisPCIe之MSI-x中斷(一)總結(jié)
以上是生活随笔為你收集整理的verilog换行太长代码_Verilog 之 File I/O task and function的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle 解锁 账户_oracle用
- 下一篇: 广义线性模型_广义线性模型(第六章补充)