Maybe it's bad C on my part, maybe it's a bug.
The following happens with 4.8.1 from ATMEL. 4.8.2 from MHV. It does not happen with 4.3.x and does not happen with 4.9.2 from MobileChess.
The C code is
uint8_t newObject(uint8_t obType, uint8_t lane, uint8_t z){ uint8_t NewObjNum; ObjectDescStruct *Current; drawFunctionPointer_t firstActionFunction; NewObjNum = getFreeObject(); // Get the next free object from the store. returns MAX+1 if no objects left if(NewObjNum != (MAX_OBJS+1)) { ObjectStore[NewObjNum].obType = obType; // Copy values into object structure. ObjectStore[NewObjNum].laneHi = lane; ObjectStore[NewObjNum].z = z; ObjectStore[NewObjNum].r = pgm_read_byte(&laneAngles[web].lane[lane]); ObjectStore[NewObjNum].animation = 0; Current = (ObjectDescStruct*)&ObjectStore[NewObjNum]; // create a pointer to the object structure firstActionFunction = (drawFunctionPointer_t)pgm_read_word(&firstActionFunctionPointers[obType]); if(firstActionFunction != NULL) { firstActionFunction(Current); } moveXYCommon(Current); // call moveXYCommon to make sure object is on screen } // before first call to renderer return(NewObjNum); }
The non working ASM that 4.8.x makes does this
ObjectStore[NewObjNum].animation = 0; adiw r26, 0x07 ; 7 st X, r1 Current = (ObjectDescStruct*)&ObjectStore[NewObjNum]; moveXYCommon(Current); movw r24, r26 call 0x9944 ; 0x9944 <moveXYCommon>
The compiler has previously gotten the address of ObjectStore[NewObjNum] into R26
It then does some ADIW to it for loading the structure.
It then takes this trashed version of R26 and passes it to moveXYCommon().
In 4.9.2 it makes a copy of the address into R16 before trashing it then does
ObjectStore[NewObjNum].animation = 0; adiw r26, 0x07 ; 7 st X, r1 Current = (ObjectDescStruct*)&ObjectStore[NewObjNum]; moveXYCommon(Current); movw r24, r16 call 0x9984 ; 0x9984 <moveXYCommon>
I can also force 4.8.x to work by moving the pointer assigment above all the assigments loading the structure with data.
thankfully looks like it is fixed in 4.9.x but just a heads that might save someone else a few days hunting phantom bugs.