Intel® Quartus® Prime Software
Intel® Quartus® Prime Design Software, Design Entry, Synthesis, Simulation, Verification, Timing Analysis, System Design (Platform Designer, formerly Qsys)
17267 Discussions

Writing nul character to file

Altera_Forum
Honored Contributor II
5,374 Views

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?
0 Kudos
11 Replies
Altera_Forum
Honored Contributor II
3,178 Views

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;
0 Kudos
Altera_Forum
Honored Contributor II
3,178 Views

ps. to write a NUL char to char, just write 

 

char_buf = NUL;
0 Kudos
Altera_Forum
Honored Contributor II
3,178 Views

I have managed to write all characters except nul using write, e.g. 

write(line_write, STX);  

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 use 

 

write(line_write, NUL);  

 

neither the bmp file in notepad++ nor the HEX editor have any nul/00.
0 Kudos
Altera_Forum
Honored Contributor II
3,178 Views

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.

0 Kudos
Altera_Forum
Honored Contributor II
3,178 Views

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;
0 Kudos
Altera_Forum
Honored Contributor II
3,178 Views

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!

0 Kudos
Altera_Forum
Honored Contributor II
3,178 Views

 

--- 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.
0 Kudos
Altera_Forum
Honored Contributor II
3,178 Views

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

0 Kudos
Altera_Forum
Honored Contributor II
3,178 Views

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
0 Kudos
Altera_Forum
Honored Contributor II
3,178 Views

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.

0 Kudos
Altera_Forum
Honored Contributor II
3,178 Views

 

--- Quote Start ---  

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. 

--- Quote End ---  

 

 

This is great, until someone else checks out the project after you've left the company and you didnt leave any information about how to view files. 

Bitmaps are just natively supported everywhere.
0 Kudos
Reply