cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
25,506 Views
Registered: ‎04-20-2012

Initializing Block RAM with External Data File

Jump to solution

Hi,

 

It's my first post on this forum, so I'm a bit confused.

I would like to write an external file in order to externally initialize BRAM.

In XST USER GUIDE, it's said that

"The data file must be pure binary or hexadecimal content with no comments or
other information"

I wrote a pure binary data file and it's ok, but it's tiring to write 64 1 or 0 per line.

The problem is that I don't know how to write a hexadecimal file to initialize my BRAM.

I tried the notation (X"") like in VHDL but it didn't work. I also tried many others hexadecimal notation but I had the same results -> KO.

 

How can I do that ? 

0 Kudos
1 Solution

Accepted Solutions
Highlighted
35,575 Views
Registered: ‎04-20-2012

Re: Initializing Block RAM with External Data File

Jump to solution

Thanks for your reply !

 

I finally find the solution.

I use hread() instead of read(). It's possible by including following line in the code :

use ieee.std_logic_textio.all;

 

So I post my code for those who could have the same "problem".

 

Thanks again.

 

My code describes an asynchronous read 512x24 rom which will be used as instruction memory in a small softcore processor that I'm designing.

 

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
use std.textio.all;
use ieee.std_logic_textio.all;


entity inst_memory is
port
(
addr: IN std_logic_VECTOR(8 downto 0);
inst: OUT std_logic_VECTOR(23 downto 0)
);
end inst_memory;


architecture behavioral of inst_memory is

type rom_type is array (511 downto 0) of std_logic_vector (23 downto 0);


impure function InitRomFromFile (RomFileName : in string) return rom_type is
FILE romfile : text is in RomFileName;
variable RomFileLine : line;
variable rom : rom_type;
begin
for i in rom_type'range loop
readline(romfile, RomFileLine);
hread(RomFileLine, rom(i));
end loop;
return rom;
end function;


signal rom : rom_type := InitRomFromFile("rom.data");


begin

 

inst <= rom(conv_integer(addr));

end behavioral;

View solution in original post

0 Kudos
4 Replies
Highlighted
Professor
Professor
25,501 Views
Registered: ‎08-14-2007

Re: Initializing Block RAM with External Data File

Jump to solution

It would help if you told us what language you're using.  For Verilog, the file format

is ASCII binary or hex (note that "ASCII binary" is not a "binary file in the usual sense).

Also the support for initializing RAM from a file depends on the ISE version.  At least

for Verilog I have found that using a new line for each entry is necessary, even though

the language reference manual indicates that any "whitespace" between values is

OK.

 

For example for verilog $readmemb and a 5-bit wide memory:

 

11011

10111

01001

11001

. . .

 

For $readmemh and a 16-bit wide memory:

 

2EA5

73F6

10A4

E4F0

9B41

. . .

 

In addition, XST likes the initialization file to have exactly the number of elements

required to initialize the memory.  Otherwise it will issue a warning that it has not

initialized the memory.

 

-- Gabor

-- Gabor
0 Kudos
Highlighted
25,494 Views
Registered: ‎04-20-2012

Re: Initializing Block RAM with External Data File

Jump to solution

First , Thank you for your quick reply.

 

I'm using VHDL. I'm not used to Verilog.

 

I tried all hexadecimal notations. For example, for a 8-bit wide memory :

0xFF

0XFF

X"FF" like in VHDL

hFF

FFh

And all others possibilities. But it didn't work.

 

May be I use the wrong function.

This is a piece of my code (from XST user guide):

 

impure function InitRomFromFile (RomFileName : in string) return rom_type is
FILE romfile : text is in RomFileName;
variable RomFileLine : line;
variable rom : rom_type;
begin
for i in rom_type'range loop
readline(romfile, RomFileLine);
read(RomFileLine, rom(i));
end loop;
return rom;
end function;

 

Are functions readline() and read() suitable to read hexadecimal values instead of binary ones ?

0 Kudos
Highlighted
Historian
Historian
25,489 Views
Registered: ‎02-25-2008

Re: Initializing Block RAM with External Data File

Jump to solution

@marcalexandre wrote:

First , Thank you for your quick reply.

 

I'm using VHDL. I'm not used to Verilog.

 

I tried all hexadecimal notations. For example, for a 8-bit wide memory :

0xFF

0XFF

X"FF" like in VHDL

hFF

FFh

And all others possibilities. But it didn't work.

 

May be I use the wrong function.

This is a piece of my code (from XST user guide):

 

impure function InitRomFromFile (RomFileName : in string) return rom_type is
FILE romfile : text is in RomFileName;
variable RomFileLine : line;
variable rom : rom_type;
begin
for i in rom_type'range loop
readline(romfile, RomFileLine);
read(RomFileLine, rom(i));
end loop;
return rom;
end function;

 

Are functions readline() and read() suitable to read hexadecimal values instead of binary ones ?


Check out what's in the textio package.

----------------------------Yes, I do this for a living.
0 Kudos
Highlighted
35,576 Views
Registered: ‎04-20-2012

Re: Initializing Block RAM with External Data File

Jump to solution

Thanks for your reply !

 

I finally find the solution.

I use hread() instead of read(). It's possible by including following line in the code :

use ieee.std_logic_textio.all;

 

So I post my code for those who could have the same "problem".

 

Thanks again.

 

My code describes an asynchronous read 512x24 rom which will be used as instruction memory in a small softcore processor that I'm designing.

 

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
use std.textio.all;
use ieee.std_logic_textio.all;


entity inst_memory is
port
(
addr: IN std_logic_VECTOR(8 downto 0);
inst: OUT std_logic_VECTOR(23 downto 0)
);
end inst_memory;


architecture behavioral of inst_memory is

type rom_type is array (511 downto 0) of std_logic_vector (23 downto 0);


impure function InitRomFromFile (RomFileName : in string) return rom_type is
FILE romfile : text is in RomFileName;
variable RomFileLine : line;
variable rom : rom_type;
begin
for i in rom_type'range loop
readline(romfile, RomFileLine);
hread(RomFileLine, rom(i));
end loop;
return rom;
end function;


signal rom : rom_type := InitRomFromFile("rom.data");


begin

 

inst <= rom(conv_integer(addr));

end behavioral;

View solution in original post

0 Kudos