Forum Discussion
Writing nul character to file
Hi!
I'm currently developing a small test bench where a bitmap file is read (using std.textio.all). I have managed to read the bmp file and I can write all characters to a new bmp file except 0 (nul character). By opening the input bmp file in notepad++ I can see the nul characters and by using the attribute image on the read variable in the test bench I can see that the nul character has been read properly. The nul characters can, however, not be found in the new bmp file I write to. Here is my simple test bench:
test_proc: process (clk)
variable line_read : line;
variable line_write : line;
variable char_buf : character;
file input_file : text OPEN read_mode IS "input_image.bmp"; -- Image to read from
file output_file : text OPEN write_mode IS "output_image.bmp"; -- Image to write to
begin
if rising_edge(clk) then
readline(input_file, line_read); -- Bitmap only consists of one line.
while line_read'length > 0 loop
read(line_read, char_buf);
write(line_write, char_buf);
end loop;
writeline(output_file, line_write);
assert false report "NONE! Simulation Finished" severity failure;
end if;
end process;
Is it possible to write the nul character to a file using VHDL?11 Replies
- Altera_Forum
Honored Contributor
Yes it is, but I recommend you dont read/write data files via textio.
read() and write() procedures exist implicitly for all types. The easiest way to read/write binary data is via write(f, char). You can read/write individual chars to a files (which are basically the bytes in the files). I have an entire package for reading/writing bitmap files (currently 5000 lines of VHDL) - it reads the header for the image size/other data and puts the data into a dynamic array. It can cope with 8/24/32 bit per pixel. I cant post the package here because it's not mine to give, but I can provide support for any of your efforts.type data_file_t is file of character; ..... file bmp : data_file_t open read_mode is "input_image.bmp"; variable c_buf : character; ... read(bmp, c_buf); --read a char (byte) from the file -- or something more elaborate: -- reads a number of bytes from the file depending on the length of s procedure read_bytes( file f : data_file_t; varaible s : out std_logic_vector ) is variable c_buf : character; begin for i in 0 to (s'length/8)-1 loop read(f, c_buf); s( (8*i)+7 downto i*8 ) := char_to_slv(c_buf); end loop; end procedure read_bytes; - Altera_Forum
Honored Contributor
ps. to write a NUL char to char, just write
char_buf = NUL; - Altera_Forum
Honored Contributor
I have managed to write all characters except nul using write, e.g.
will write STX in my bmp file. I have also examined the file in an HEX editor (plug-in to notepad++) and I can see 02 instead of STX. However when I usewrite(line_write, STX);
neither the bmp file in notepad++ nor the HEX editor have any nul/00.write(line_write, NUL); - Altera_Forum
Honored Contributor
you'll also have a problem with the writeline() procedure adding an extra \n to the end of every line. Hence why textio isnt very useful in this situation.
- Altera_Forum
Honored Contributor
What simulator are you using? writing NUL to a text file works just fine for me
use std.textio.all; entity test_chars is end entity test_chars; architecture test of test_chars is begin process file f : text; variable l : line; begin FILE_OPEN(f, "test.txt", write_mode); write(l, NUL & "This string is started and ended with a NUL" & NUL); -- confirm all chars for i in l'range loop write(OUTPUT, l.all(i) & " (" & integer'image(character'pos(l.all(i))) & ")" & LF); end loop; writeline(f, l); FILE_CLOSE(F); FILE_OPEN(f, "test.txt"); readline(f,l); --confirm all chars again for i in l'range loop write(OUTPUT, l.all(i) & " (" & integer'image(character'pos(l.all(i))) & ")" & LF); end loop; FILE_CLOSE(f); wait; end process; end architecture test; - Altera_Forum
Honored Contributor
I still cannot see any NUL in test.txt. I'm using ISim and it seems as that the reason why. Thanks Tricky for you tips and code!
- Altera_Forum
Honored Contributor
--- Quote Start --- I still cannot see any NUL in test.txt. I'm using ISim and it seems as that the reason why. Thanks Tricky for you tips and code! --- Quote End --- Ive never used ISIM, but have read reports of it doing odd things for lesser used bits of the VHDL language. I would switch to something that does work - like modelsim. - Altera_Forum
Honored Contributor
I changed to ppm files with ascii code instead. Not the optimal solution but my test bench is now working. I will probably switch to Modelsim for my next project. Thanks
- Altera_Forum
Honored Contributor
Ive seen people use PPM because it is text based - the issue is most standard graphics prgrams (windows paint, picture viewer) dont support it. I switched to bitmap to allow me to double clock testbench outputs to instantly see what happened, and is then instantly available to anyone as the format is standard.
Good luck with the work ahead - bitmap has a few quirks and different headers you'll need to understand, but it's all out on the internet - Altera_Forum
Honored Contributor
irfanview (http://www.irfanview.com/) supports PPM, both for reading and writing. In supports batch conversions and can be used from the command line so (in theory) it could be integrated in an automatic testbench script.