Optimization problem

H

hamze

I have a special device in my system, I must work with it in this
manner that : write 4 bytes and read 3 bytes,
this device has no reset, so if one of reads or writes miss, from that
time device will not work correctly because order of writes and reads
are missed.

this device works right when optimization is off, but when I turn it
on it stop working properly in my code.
some time i did not use one of its read value, so I think optimization
remove it and order of read and writes are missed.
how can I solve this problem ?
I also test this solution that I defined an asm valotile ( ) and put
all reads and write inside it but still turning optimization on
corrupt my program result.
 
J

Joshua Maurice

I have a special device in my system, I must work with it in this
manner that : write 4 bytes and read 3 bytes,
this device has no reset, so if one of reads or writes miss, from that
time device will not work correctly because order of writes and reads
are missed.

this device works right when optimization is off, but when I turn it
on it stop working properly in my code.
some time i did not use one of its read value, so I think optimization
remove it and order of read and writes are missed.
how can I solve this problem ?
I also test this solution that I defined an asm valotile ( ) and put
all reads and write inside it but still turning optimization on
corrupt my program result.

From what I've heard in this group, volatile alone is insufficient for
correct MMIO (memory mapped IO) on most systems. I would suggest
looking at the docs for your implementation to see what it says about
correct MMIO. You could also look at the output assembly to see if it
is indeed not doing as you want.
 
H

hamze

From what I've heard in this group, volatile alone is insufficient for
correct MMIO (memory mapped IO) on most systems. I would suggest
looking at the docs for your implementation to see what it says about
correct MMIO. You could also look at the output assembly to see if it
is indeed not doing as you want.

thanks alot, you gave me a valuable clue,
I know what is my hardware correct order of MMIO, but I dont know how
to make compiler know about it and force compile not to REORDER my
code.
 
J

Jorgen Grahn

thanks alot, you gave me a valuable clue,
I know what is my hardware correct order of MMIO, but I dont know how
to make compiler know about it and force compile not to REORDER my
code.

It's possible that the right thing to do is to write the critical
parts in assembly. You can start with compiler-generated assembly and
modify that, to make it a bit easier.

/Jorgen
 
N

Nobody

I know what is my hardware correct order of MMIO, but I dont know how
to make compiler know about it and force compile not to REORDER my
code.

The compiler will not re-order reads/writes of volatile variables with
respect to each other. However, the CPU might perform re-ordering; to
prevent this, you need to use a memory barrier (aka "fence").

http://en.wikipedia.org/wiki/Memory_barrier
http://en.wikipedia.org/wiki/Memory_ordering

For gcc, see e.g.:

http://gcc.gnu.org/onlinedocs/gcc-4.6.0/gcc/Atomic-Builtins.html

Also, you might try comp.arch.embedded.
 
G

Geoff

thanks alot, you gave me a valuable clue,
I know what is my hardware correct order of MMIO, but I dont know how
to make compiler know about it and force compile not to REORDER my
code.

Get to know your compiler and target CPU. Microsoft provides the
#pragma optimize directive for turning optimizations off/on for
individual functions.

Write your low-level MM I/O functions in assembler, call them from C.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top