This is the mail archive of the crossgcc@sources.redhat.com mailing list for the crossgcc project.

See the CrossGCC FAQ for lots more information.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Ye Ole Dereferenced Pointer Problem


Hi Robert !

What is the size of your integer ?
When I refer to the technical documentation of the Hitachi SH7032, I read that the internal data bus is 16 bits (and peripheral one too).
See http://semiconductor.hitachi.com/products/pdf/h11to003d2.pdf

Your program try to add a 32 bits address and a 16 bits integer, but the C compiler does not associate a type with a litteral constant, whatever size it is. As you want to add it with an integer, the compiler consider that the whole operation uses integers. The compiler takes the 16 lower significant bits of your 0x09000000 constant (that is to say 0x0000). The final result is that your program try to write your 85 decimal constant into address 0x00000001, 0x00000002 and 0x00000003. I think it's not a good location to write that !

Now, the compiler know exactly what to do when it must add an address and a 16 bits integer (it's what it does to implement an array). Your program should be :

for( int counter = 1; counter <= 3; counter++ )
{
        * (((volatile unsigned char *) 0x09000000) + counter) = 85;
}

As convertion is a prioritary operation, the expression in the loop block may be:

* ((volatile unsigned char *) 0x09000000 + counter) = 85;

But I prefer the first expression that is the most readable.

Pierre.

Robert Floyd wrote:

Hi everyone!

I am writing code and compiling with a Hitachi SH7032 cross-compiler.  I
am attempting to write to external DRAM (as a memory test) and using the
following syntax:

(* (volatile unsigned char *)(0x09000001)) = 85;        /*dram address at
0x09000001 */
(* (volatile unsigned char *)(0x09000002)) = 85;        /*dram address at
0x09000002 */
(* (volatile unsigned char *)(0x09000003)) = 85;        /*dram address at
0x09000003 */

This compiles fine and runs fine.  It writes 85 into each address
location.  But I really want to run this inside a 'for loop' such as
this...

for( int counter = 1; counter <= 3; counter++ )
{
        (* (volatile unsigned char *)(0x09000000 + counter) ) = 85;
}

The above compiles fine also but each time it gets to this point in the
code the SH7032 goes off into never never land.

Can anyone explain to me why the first section of code above works and
runs but the latter section of code causes some type of runtime error?
Thanks so much.

BTW, a co-worker says I need parentheses around the whole thing except
the asterick such as...

*( (volatile unsigned char *)(0x09000000 + counter) ) = 85;

But that is odd since the it works without it fine as in the examples
above.

Robert F.

------
Want more information?  See the CrossGCC FAQ, http://www.objsw.com/CrossGCC/
Want to unsubscribe? Send a note to crossgcc-unsubscribe@sourceware.cygnus.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]