Forum Discussion
Altera_Forum
Honored Contributor
21 years agoHi,
Two problems : 1. Probably in your example, the variable will be putted in a register. So there is actually no memory reference. 2. In your example, the variable is a local variable to the main function, so it will be on the stack (if it is not case 1) Try to do 'volatile int i_cnt;" as a global variable instead. This forces the compiler to use a memory location and not optimise it away.#include <stdio.h>
volatile int i_cnt;
int main()
{
char hello_string={"Hello from Nios II!\n"};
for (i_cnt=1;;i_cnt++) {
printf("%d. %s",i_cnt , hello_string);
}
return 0;
} I think because it is a globale now, the volatile is not ncry anymore. But if you want to be sure that every time in the loop where i_cnt is used or changed the value is read or written to the memory (or cache), use the volatile. Otherwise, the compiler will read only once, and stores it once after the loop (if it is furhter needed). For the f core : If you need to be absolutely sure it is written to the memory every time, you have to bypass the datacache as follows : 1. change the ptf file and set always bypass dcache to 1. Then the dcache will never be used (for no data at all) 2. do a trick with the address of i_cnt to put a 1 on the highest bit. Then only for this variabele the data cache is bypassed #include <stdio.h>
volatile int i_cnt;
int main()
{
char hello_string={"Hello from Nios II!\n"};
volatile int* pi_cnt = &i_cnt;
pi_cnt = (volatile int*) ((int) pi_cnt | 0x80000000);
for (*pi_cnt=1;; *pi_cnt++) {
printf("%d. %s",*pi_cnt , hello_string);
}
return 0;
} Stefaan