Programmable Devices
CPLDs, FPGAs, SoC FPGAs, Configuration, and Transceivers
20726 Discussions

problem of handling pointers: !!!

Altera_Forum
Honored Contributor II
1,057 Views

I have a problem in reading the data that I wrote in memory.  

I tried to write data on a memory chip.  

the operation is to transfer data from one memory chip to another.  

I managed to make the first operation that is writing data on the target memory.  

the second is to read the data that I have already classified in the second memory. but I can not do this. 

the first processor written in memory of the second processor. 

the first processor written in memory of the second processor.  

the second processor accesses the data, there is the problem.  

the second processor displays nothing. 

-----------------code first processor------------------- 

#include "io.h" 

#include "system.h" 

#include <stdio.h> 

#include "data.h" 

#include <sys/alt_cache.h> 

int h1,h2,g1,g2,p; 

 

 

int taille_matrice; 

int *z1,*z2,*z3, *z4,*z5,*z6,*y,*x,B,i,j; 

int *k; 

void load_data() 

{  

 

 

y=(unsigned int*)0x000020020;// base address memory of the second processor 

 

j=0;// à ne pas changer 

for (i=0; i<2;i++) 

 

 

B=(M1);  

y=y+j; 

*y=b; 

j=1; 

alt_dcache_flush_all(); 

j=1; 

 

for (i=0; i<4;i=i+2)//i+la taille d'une ligne 

 

 

b=(m2);  

y=y+j; 

*y=B; 

//j=j+1; 

j=1; 

alt_dcache_flush_all(); 

z4=(unsigned int*)0x000020030;// i try print the result (see second processor) 

printf("u4=%d\t", *(z4)); 

------------------------second processor----------------------------- 

////////////////////////////////////////////////////////////////////// 

#include "io.h" 

#include "system.h" 

#include <stdio.h> 

 

int i; 

unsigned int h1,h2,g1,g2,p,p1; 

 

 

int taille_matrice; 

int *z1,*z2,*z3,*z4,*z5,*z6; 

int *k; 

void load_data() 

{  

taille_matrice = 4; 

z1 = (unsigned int*)0x000020020; 

h1 =*z1; 

z2 = (unsigned int*)0x000020024; 

h2=*z2; 

 

z3= (unsigned int*)0x000020028; 

g1=*z3; 

 

 

z4= (unsigned int*)0x00002002C; 

g2=*z4; 

 

z5= (unsigned int*)0x000020030;// this result will be print by the first processor 

 

*z5=(h1*g1)+(h2*g2); 

------------------------------------------------------------ 

first processor print nothing 

u4=-653432534 

 

 

 

can someone help me  

thks for all 

 

 

0 Kudos
3 Replies
Altera_Forum
Honored Contributor II
346 Views

How and when do you call the load_data functions on both processors? 

You should protect access to the shared memory using a hardware mutex to be sure that you don't have both CPUs accessing the zone at the same time. You must also have a kind of synchronization between the two processes, maybe with a message exchange. 

Instead of using alt_dcache_flush_all you can use alt_remap_uncached on the pointer. It will make all memory accesses bypass the cache, and is better performance-wise than alt_dcache_flush_all that clears all the data cache. 

You should bypass the cache on the second processor too. 

 

Just a few pointers...
0 Kudos
Altera_Forum
Honored Contributor II
346 Views

hi, 

thk for Re. 

load_data function () is called in the two processors.  

can you explain to me what is bypass cache?  

thank you very much 

0 Kudos
Altera_Forum
Honored Contributor II
346 Views

The "when" is important. For your system to work, the load_data() on the second processor would need to be called just after the load_data() function on the first processor filled the matrix, but before it would read the result. This would be impossible to guarantee without any synchronisation between the two CPUs. 

The NIOS processor can have a data cache, and it will cause some problems when you share memory. If the memory is updated by one CPU, the second CPU may not notice it, and when reading will return the old value from its cache instead of the new value in memory. 

One way to go around that is to call the alt_dcache_flush_all() function as you did, after writing and before reading. This will clear all the data cache and force the CPU to read from memory. This is not very efficient though, because all the cache is flushed, including other regions of memory that could still be in cache. 

Another way is to use the remap function. Given a pointer, it will return a new pointer value, that when used will force the CPU to access the main memory and never go through the cache. 

Finally for code readability and robustness to SOPC regenerations you should use a system.h define for the address rather than a hard coded value. Something like that: 

unsigned int *memory = alt_remap_uncached(SHARED_MEMORY_BASE); h1 = memory; h2 = memory; g1 = memory; g2 = memory; memory = (h1*g1)+(h2*g2);  

You should of course replace SHARED_MEMORY_BASE by the actual constant defined in system.h for your shared memory area. 

 

And by the way I think the NIOS forum would be more appropriate for this discussion ;)
0 Kudos
Reply