Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Altera_Forum
Honored Contributor I
834 Views

Code and Data in SDRAM

Hello! 

Beginner's question:  

 

When using an SDRAM in an NIOS II project, where (at what address) does the code stops and were does the data can be written?  

 

I need to know the address because I want to write some data in the SDRAM, but the memory is also used by Nios II (less then 2 %)... so I don't want to overwrite portions of code. 

 

Thank you in advance.
0 Kudos
6 Replies
Altera_Forum
Honored Contributor I
38 Views

In C you should use the malloc() function. It will allocate for you a memory region that isn't used for anything else. 

Alternatively if you know at compile time how much space you need, you can also declare a static variable (a char array for example).
Altera_Forum
Honored Contributor I
38 Views

The IDE might let you constrain the part of the SDRAM used for the nios code/data/stack/heap. 

Alternatively the space can be reserved by writing a custom linker script. 

The linker script can also assign symbolic names to the base (etc) of the reserved area. 

 

But it you don't really care what the address is (because you are only referencing by its symbolic address) a static data item would suffice. 

You might need to mark with __attribute__(aligned(32)) in order to stop cache line sharing. You could also use __attribute__(section("fubar"))) to make it easier to assign the item to a specific address using the linker script. 

 

There are a lot of ways to hang yourself :-)
Altera_Forum
Honored Contributor I
38 Views

 

--- Quote Start ---  

In C you should use the malloc() function. It will allocate for you a memory region that isn't used for anything else. 

Alternatively if you know at compile time how much space you need, you can also declare a static variable (a char array for example). 

--- Quote End ---  

 

 

Thank you for your response Daixiwen 

 

But if I want to write data periodically (T= T1 write data + T2 do nothing ) using vhdl logic and then I want to access it through Nios (when vhdl logic does not write, that is T2) , will that be also work with malloc () ?
Altera_Forum
Honored Contributor I
38 Views

 

--- Quote Start ---  

The IDE might let you constrain the part of the SDRAM used for the nios code/data/stack/heap. 

Alternatively the space can be reserved by writing a custom linker script. 

The linker script can also assign symbolic names to the base (etc) of the reserved area. 

 

But it you don't really care what the address is (because you are only referencing by its symbolic address) a static data item would suffice. 

You might need to mark with __attribute__(aligned(32)) in order to stop cache line sharing. You could also use __attribute__(section("fubar"))) to make it easier to assign the item to a specific address using the linker script. 

 

There are a lot of ways to hang yourself :-) 

--- Quote End ---  

 

 

Than you for your answer as well. 

 

I think I need to know the address because i want to write data there before with vhdl logic and then access it through Nios. Would it be possible to divide the SDRAM in two? one half for data and one for code? Do you know were can I find information on the scripts that you are referring to?
Altera_Forum
Honored Contributor I
38 Views

I don't use the IDE or any of the sopc/qsys generated files to build my nios code. So don't know where the IDE lets you configure things. 

For the linker script format itself use google.
Altera_Forum
Honored Contributor I
38 Views

You basically have those two solutions. You can as you say divide the RAM in two zones, one for the software and another one shared with hardware. It makes it simpler for the hardware as you can put a fixed address, but you need to modify the link script, as dsl explained. I think that the new environment (software build tools) can let you specify your own link script, but you will have to modify it by hand. I don't remember the syntax, but you can google the GNU cc documentation for that, I remember I found it easily when I needed it. 

Using malloc() or a static variable is easier for the software and requires no special link script, but as you guessed you need to give this address to your hardware, as it can change at each compile or run. For that you can create a register in your IP that the CPU can write to, that will contain the address to use. This is commonly done with DMA IPs for example.
Reply