Menu

#3287 PIC14 port does not update (volatile) global variable in some situations

open
nobody
PIC14
5
2021-10-08
2021-10-08
No

sdcc "PIC14" port sometimes does not update global variable when the variable is in the middle of arithmetic operation of a function call. The following code generated assembly can make sure it does not write correct result to "atemp1", which is a global variable.

---- testd.c ---
int atemp1;
int atemp2;
long ADCH,ADCM,ADCL,ADCData;
long PIRAVG1_32bit;
extern volatile char PT2;
signed x=3,y=4;
int ffff,aaaab;
void pass(void)
{
while(1)
PT2++;
}
void fail(void)
{
while(1)
PT2--;
}
void chk(void)
{
if(atemp1!=0x3ff)
fail();
}
unsigned int abs2(int a, int b)
{
if(a>b)
return a-b;

return b-a;

}
main()
{
ADCH=0x03;
ADCM=0xff;
ffff=x-y;
aaaab=abs2(x,y);
atemp2=ADCH;
atemp1= (atemp2 <<8 )+ ADCM ; ///bug result
PIRAVG1_32bit=PIRAVG1_32bit+atemp1;
chk();
pass();
while(1);
}

-- testd.c --- above
compile command:
sdcc -mpic14 -S testd.c -I ~/sdcc/include/pic14 -p16f76 --use-non-free

The compiled result from source line 37 is below

; .line 37; "testd.c" atemp2=ADCH;
BANKSEL _ADCH
MOVF _ADCH,W
BANKSEL _atemp2
MOVWF _atemp2
BANKSEL _ADCH
MOVF (_ADCH + 1),W
BANKSEL _atemp2
MOVWF (_atemp2 + 1)
; .line 38; "testd.c" atemp1= (atemp2 <<8 )+ ADCM ;
MOVF _atemp2,W
BANKSEL r0x101F
MOVWF r0x101F
CLRF r0x1020
BANKSEL _ADCM
MOVF _ADCM,W
BANKSEL r0x1021
MOVWF r0x1021
BANKSEL _ADCM
MOVF (_ADCM + 1),W
BANKSEL r0x1022
MOVWF r0x1022
MOVF r0x1021,W
ADDWF r0x1020,W
MOVWF r0x1020
MOVF r0x101F,W
MOVF r0x1022,W
BTFSC STATUS,0
INCFSZ r0x1022,W
ADDWF r0x101F,F
MOVLW 0xff
BTFSS r0x101F,7
MOVLW 0x00
MOVWF r0x1021
MOVWF r0x1022
; .line 39; "testd.c" PIRAVG1_32bit=PIRAVG1_32bit+atemp1;
MOVF r0x1020,W
BANKSEL _PIRAVG1_32bit
ADDWF _PIRAVG1_32bit,F
BANKSEL r0x101F
MOVF r0x101F,W

Line 38 shows global variable "atemp1" is not set as wanted, neither set at other places.

This bug is only in PIC14 port, which I patched for other targets, and has the same bug.

From "dump-i-code" I see problem at pack-register steps.

Discussion


Log in to post a comment.