Você está na página 1de 4

Advanced Digital Design

ELEC444/644
Session 3

K. Joseph Hass

Electrical Engineering
Bucknell University

Basic Verilog File IO

$fopen(filename,[accesstype]);

$fscanf(filehandle,format spec,[value],[value],. . . );

$fdisplay(filehandle,[format spec],[value],[value],. . . );

$fwrite(filehandle,[format spec],[value],[value],. . . );

$fmonitor(filehandle,[format spec],[value],[value],. . . );

$fclose([filehandle]);
Basic Verilog File IO
Opening and Closing Files

integer MyFileHandle;
MyFileHandle = $fopen(”MyFileName”,”MyAccessType”);

$fclose(MyFileHandle);

Access Type Meaning


r or rb Open text or binary file for reading
w or wb Open text or binary file for write
a or ab Open text or binary file for appending

Basic Verilog File IO


Reading From Text Files

r e a l MyNumber ;
integer i ;
i = 1;
w h i l e ( i >0) begin
@( posedge C h a n g e I n p u t s ) ;
i=$ f s c a n f ( M y I n p u t F i l e , ”%f ” , MyNumber ) ;
end

%b binary %d decimal
%h hexadecimal %s string
%t time %c character
%m module name %f real
%v strength %e exponential
The $fscanf should return −1 at end-of-file. . . this is broken in Xilinx ISE Version 11. . . use $feof instead.
Basic Verilog File IO
Writing To A Text File

integer MyFileDescriptor ;
r e a l MyNumber ;

M y F i l e D e s c r i p t o r = $fopen ( ” MyFileName ” , ”w” ) ;

always @( posedge O u t p u t S t r o b e )
$ f d i s p l a y ( M y F i l e D e s c r i p t o r , ”%f ” , MyNumber ) ;

$fwrite is similar to $fdisplay but does not append an


end-of-line.

Basic Verilog File IO


Writing To Multiple Text Streams

parameter STDOUT = 1 ;
i n t e g e r MyMCD; // A m u l t i c h a n n e l d e s c r i p t o r
r e a l MyNumber ;

MyMCD = $fopen ( ” MyFileName ” ) ; // no a c c e s s t y p e

always @( posedge O u t p u t S t r o b e ) ;
$ f d i s p l a y ( (MyMCD | STDOUT) , ”%f ” , MyNumber ) ;
end

Note that $fdisplay(STDOUT, ”%f”, MyNumber);


is equivalent to $display(”%f”, MyNumber);
and similar equivalents exist for $fwrite and $fmonitor.
Basic Verilog File IO
Monitors

initial
$monitor ( ”A= %h t i m e= %t ” , MySig , $ r e a l t i m e ) ;

I A monitor prints whenever one of the specified variables


changes.
I Only one monitor can be active at any time.
I Using $fmonitor with a file handle writes to a file

Other Verilog File IO

Other verilog commands for file IO include


I Read and write binary (rather than text files)
I Command syntax is OS dependent
I File format (big endian or little endian) is OS dependent
I Write when signal becomes stable ($fstrobe)
I Read, write, and display without format string
I Default format is decimal
I Final character of command is default format (e.g.
$writeb for binary)
I Access by characters rather than lines
I Arbitrary positioning of read/write pointers

Você também pode gostar