Re: text to binary
raghu wrote:
> Thanks a lot for your valuable response.
> As you said that a text in a file can be read with %s format. can you
> please give me more detailed description on this. I mean ,the syntax
> and where the file should be kept i.e in which directory ,in order to
> access the file.
Assuming that your simulator supports Verilog-2001 file I/O, you have a
lot of flexibility in this. You can keep the file anywhere you like,
since the filename argument to $fopen can be a full pathname including
directories. For relative paths, I believe $fopen uses the directory
you are running in as the current working directory, in most
implementations. So the simplest thing is to put the file in the
directory you are running in.
Then something like
integer fd, nread;
reg [NBYTES*8:1] message;
initial
begin
fd = $fopen("myfile", "r"); // open for reading
if (!fd)
$display("failed to open myfile");
else
begin
nread = $fscanf(fd, "%s", message);
if (nread != 1)
$display("failed to read message");
end
end
You could also use $fgets to read a line into the message buffer,
instead of $fscanf. Either way, the buffer will end up with the bits
representing the ASCII encoding of the string read in. If the string
is shorter than the buffer, the upper bits will all be zeroes.
|