verilog换行太长代码_Verilog 之 File I/O task and function
不點藍字,何來故事?
許久之前關于Verilog I/O操作的一篇筆記梳理,再此與諸君共分享。0I/O操作類型????verilog中關于文件操作的任務和函數主要分為四類:
????(1)打開和關閉文件的任務和函數
????(2) 向文件中輸入信息的任務
????(3) 向變量中輸入信息的任務
????(4) 從文件中讀取數據并寫入變量或存儲器中
1打開和關閉文件的任務和函數fd=$fopen("file_name",type)
????該函數主要用于按照type定義的方式打開"file_name"指定的文件,并返回32比特的文件描述符。type的類型有:
????默認方式為以“w”或“wb”方式打開。注意"w","wb","w+","w+b","wb+"打開文件將會清空文件原有數據。其中“b”用于區別文本文件和二進制文件。如果一個文件因為某種無法打開,將會返回0,通過$ferror可用于檢測失敗原因。
$fclose(fd)
????該函數主要用于關閉文件。文件一旦關閉,$fmonitor 、$fstrobe等任務將會自動終止。
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 進程,但同時可以運行任意數量的$fmonitor 進程.
????$fwrite需要觸發條件才記錄,不自動換行
????$fwrite(file_id,"%format_char", parameter);
????$fdisplay需要觸發條件才記錄
????$fdisplay(file_id,"%format_char", parameter);
????$fstrobe();當該時刻的所有事件處理完后,在這個時間步的結尾寫入,可以確保所寫的數據時無誤的。推薦使用。
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從文件中讀取數據c=$fgetc(fd)——從文件中一次讀取一個字符
????如果從文件中讀取發生錯誤時,c將被設為EOF(-1).通過$ferror可獲取錯誤原因,c的位寬應大于8位。采取這種格式讀取數據文本文件中的空格、標點符號等也會被讀出來。
code=$ungetc(c,fd);
????向fd文件的緩沖區內插入字符c,c將會在下次調用$fgetc時返回,調用該函數并不會改變文件本身內容。當發生錯誤時code為EOF,否則為0。
從文件中讀取一行
integer code;
code=$fgets(str,fd);
????從fd指定的文件中讀取數據到str直至str填滿或出現換行符或出現EOF。若str長度不是整數個bytes,則高位部分數據將被舍去。若讀取失敗,則code返回0,否則code將返回讀取的字符數。
按照固定格式從文件中讀取數據
integer code;
code=$fscanf(fd,format,args);//從fd文件中按照format制定的格式讀取數據到args中
code=$sscanf(fd,format,args);//從寄存器中按照format制定的格式讀取數據到args中
????空白字符(空格、制表符、換行符)可用于跳過空白符讀取非空白字符(對于標點符號等無效)。
????%m能夠返回當前仿真路徑,而不返回文件中的字符
按照二進制格式讀取數據
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);
????讀取二進制流,將數據從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填滿或讀完截止。
????文件中的數據逐字節讀取。使用一個8位寬的存儲器加載一個字節,而使用每個存儲器字2個字節寬加載9位寬的存儲器。數據是以大尾數方式從文件中讀取;第一個字節讀取用于填充最重要的位置存儲元件。如果內存寬度不能被8(8,16,24,32)整除,則由于截斷并不是文件中的所有數據都加載到內存中。
5文件定位integer pos ;
pos = $ftell ( fd );
????返回fd文件當前位置距文件首部偏移量(按字節計算),可以用于后續的$fseek使用,以將文件重新定位到此點。重定位將會取消$ungetc操作。
code=$fseek(fd,offest,operation);
code = $rewind ( fd );
????$fseek()設置文件fd的下一輸入或輸出位置。
????????0:設置位置到偏移地址
??????? 1:設置位置到當前位置加偏移量
??????? 2:設置位置到文件結束位置加偏移量(向前)
????$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緩存區沖刷$fflush ( mcd );
$fflush ( fd );
$fflush ( );
????將所有緩沖區的數據寫入制定的mcd或fd,若參數缺省,沖刷寫入所有打開的文件。
7I/O狀態查詢I/O error status
integer errno;
errno=$ferror(fd,str);
????描述的錯誤信息字符串將被存放在字符串str中。如果沒有錯誤,errno將為0,str將被清空。
8檢測文件結尾integer code;
code=$feof(fd);
????當檢測到文件結尾時,返回1,否則返回零。
8加載文件到存儲區$readmemb ( " file_name ", memory_name [ , start_addr [ , finish_addr ] ] ) ;
$readmemh ( " file_name " , memory_name [ , start_addr [ , finish_addr] ] ) ;
????文件應該只包括空白符、注釋、二進制或十六進制數據。
精彩推薦SpinalHDL資料匯總SpinalHDL—優雅地實現總線寄存器讀寫SpinalHDL代碼組織結構之Component換個名字混江湖SpinalHDL—覆蓋率收集SpinalHDL—測試平臺搭建兄弟,要幾段?打個拍,握個手可以么SpinalHDL—柳暗花明又一村FPGA圖像處理——老戲新說山水有相逢—和大牛成為同事SpinalHDL——環境搭建亂花漸欲迷人眼—Vivado之SynthesisPCIe之MSI-x中斷(一)總結
以上是生活随笔為你收集整理的verilog换行太长代码_Verilog 之 File I/O task and function的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle 解锁 账户_oracle用
- 下一篇: transferto的流这么关闭_Jav