cormac or anyone else:
If you're going to insert debug code, here's some stuff to know. It took me a bit to realize all the issues with this stuff and ARM assembly. Here's the LED blink function I used:
#define LED_PR 0xC0220060
#define LED_SPIN_DELAY 5000000
int led_blink(int save_R0) {
int i;
volatile long *p=(void*)LED_PR;
p[0] = 0x46;
for (i=0; i< LED_SPIN_DELAY; i++) // Wait a while
{
asm volatile ( "nop\n" );
}
p[0] = 0x44;
for (i=0; i<LED_SPIN_DELAY; i++) // Wait a while
{
asm volatile ( "nop\n" );
}
return save_R0;
}
(That was thrown together mostly by copying similar stuff from the wiki and hardware addresses from the sx10 platform code.)
Unlike IA-32 (Intel), the ARM calling convention does not require that any called function preserve all the registers. Functions are allowed to stomp all over r0-r3, and gcc behaves accordingly as does whatever compiler canon uses. So when you see something like:
MOV R0, SP
MOV R1, #0x74
BL sub_FFB003BC
MOV R0, #0x53000
Don't put "BL led_blink" right before "BL sub_FFB003BC" and after the arguments are placed in r0-r3. Otherwise led_blink() will conceivably stomp all over r0 and r1 (in the example above) unless it does something special to preserve them. (In the code above, a trick is used to preserve r0 by specifying an argument, the first of which will always be passed via r0, then returning it which will also always be returned via r0.)
It looks like the easiest thing to do is use the r0 preserving trick above, then put the call to led_blink or whatever just after a call to something else. That way we know "BL sub_FFB003BC" has stomped all over r0-r3 and placed it's return value in r0. So we only have to preserve r0 for the code immediately after that to get what it expects. If gcc decides to use r4+ then it knows it has to preserve those and we don't have to worry about it as it should generate the right code.
(I was stumped for a bit as to why something like led blinking code would crash the camera when I was messing with this.)
See
http://chdk.wikia.com/wiki/CHDK_Coding_Guidelines