[A89] Re: C Problems


[Prev][Next][Index][Thread]

[A89] Re: C Problems




Hi!

> Can you give examples?
> 
> I assume that you mean it's not possible to read the value 
> from that location... ie:
> num=*(char*)0x600015

Not exactly... Suppose that you read a value from port 0x600015
and wait until it is say non-zero:

do {
  num = *(char*)0x600015;
} while (!num);

The compiler will think that you constantly read the same value
in the loop (because it can not see how the content of 0x600015
can be changed if nothing in the loop modify this address - it
can not conclude that this is not a real memory location), so 
it can optimize it into

num = *(char*)0x600015;
do {
} while (!num);

which is, of course, either an endless loop, or no loop at all.
To prevent such behaviour, you can declare address 0x600015 as
'volatile', as in

do {
  num = *(volatile char*)0x600015;
} while (!num);

This is what macro peek_IO does, so you can do

do {
  num = peek_IO(0x600015);
} while (!num);

I hope that it is clear now.

Zeljko