alex

Memory initialization

Recommended Posts

I was reading through Jim Wu's FPGA blog and came across something that in HidsightTM is one of those "D'oh I should have done that!" moments but because I've seen it done a certain way before, I kept doing it the same way over and over.

 

Suppose you need a ROM that contains a repeatable waveform such as for example a sine or cosine wave. How do you do it? I used to generate the data in Excel then copy the numbers into a ROM initialization section. I know, right? Shameful!

 

Here's a better way according to Jim, generate that stuff on the fly in an initialization function:

constant MEM_DEPTH : integer := 2**ADDR_WIDTH;type mem_type is array (0 to MEM_DEPTH-1) of signed(DATA_WIDTH-1 downto 0);function init_mem return mem_type is    constant SCALE : real := 2**(real(DATA_WIDTH-2));    constant STEP  : real := 1.0/real(MEM_DEPTH);    variable temp_mem : mem_type;begin    for i in 0 to MEM_DEPTH-1 loop        temp_mem(i) := to_signed(integer(cos(2.0*MATH_PI*real(i)*STEP)*SCALE), DATA_WIDTH);    end loop;    return temp_mem;end;constant mem : mem_type := init_mem;

The advantage of this is that if you need to shrink/expand the number of values in the table or adjust the number of bits in each value, you don't have to re-generate all the table data, just tweak the initialization function accordingly. Full example source code on his site.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now