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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.