• AVR Freaks

AnsweredHot!XC8 and mixed assembly/C code for a PIC16F876A and banksel translation into the lst output

Author
TechDpt
Starting Member
  • Total Posts : 45
  • Reward points : 0
  • Joined: 2011/06/13 09:29:39
  • Location: 0
  • Status: offline
2019/08/14 10:20:15 (permalink)
0

XC8 and mixed assembly/C code for a PIC16F876A and banksel translation into the lst output

Hi,
I've ported an old program from a Hi-Tech PICC v8.05 PL2 to the XC8 v1.45 inside the MPLABX IDE v5.15.
All compile without trouble but the final application work not properly regarding the aseembly coded section.
To perform the port I've used the BANKSEL and BANKMASK directive, but into the final listing file I can not correctly understand in what way these ones are translated, for example, look at this piece of code (a simple delay subroutine):
void Delay_tbuf(void)
{
#asm
  movlw DELAY_4700NS ; 
  BANKSEL(_Delayctr)
        movwf BANKMASK(_Delayctr) ;
Loop_tbuf
  decfsz BANKMASK(_Delayctr),f ;
  goto Loop_tbuf ;

#endasm
    
    return;
}

 
is compiled as (from lst file):
 
  9086 ;psect for function _Delay_tbuf
  9087 17F0 _Delay_tbuf:
  9088
  9089 ;incstack = 0
  9090 ; Regs used in _Delay_tbuf: []
  9091 17F0 3004 movlw 4 ; #
  9092 17F1 1303 1683 banksel _Delayctr ;#
  9093 17F3 00DC movwf _Delayctr& (0+127) ; #
  9094 17F4 Loop_tbuf:
  9095
  9096 ;#
  9097 17F4 0BDC decfsz _Delayctr& (0+127),f ; #
  9098 17F5 2FF4 goto Loop_tbuf ;#
  9099
  9100 ;i2c.c: 139: return;
  9101 17F6 0008 return
  9102 17F7 __end_of_Delay_tbuf:

 
I can not see the expansion into the code of the "banksel" directive, this should be like a bsf/bcf instruction, there is a way to set the linker to produce a full listing code with all the asm istruction inside?
 
Also, into the old compiler all the variable used inside the assembly was set into the bank0 "psect    rbss_0", inside the XC8 all was moved inside the "psect    bssBANK1" so there is a need to perform the switch into the correct bank, what should be the best way to force allocation of these variable inside the bank0?
 
I've also another suspect concerning the use of the sequence btfsc or btfss and the banksel directive, as example look at this code (working, on bank0):
 
btfsc 7,4
bsf _EE_Error_Flag,0
fcall _Delay_Tclk
bcf 7,5
btfsc _EE_Error_Flag,0
goto StopBit

 
for the code inside the XC8 (not workin) I've instead:
 
btfsc 7,4
banksel _EE_Error_FlaG
bsf _EE_Error_Flag& (0+127),0
fcall _Delay_Tclk
bcf 7,5
banksel _EE_Error_Flag
btfsc _EE_Error_Flag& (0+127),0
goto StopBit

 
the last (not working) code will have more instruction after btfsc 7,4 due to the BANKSEL macro so I think that the flow will be messed respect the original version where no bank switch was needed, I'm right? If this is the real reason why the code is not properly working I've to change the code structure around any btfsc/banksel isnstructions sequence, I'm right?
 
Thank!
 
Best regards.
post edited by TechDpt - 2019/08/14 11:04:13
#1
1and0
Access is Denied
  • Total Posts : 9734
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: online
Re: XC8 and mixed assembly/C code for a PIC16F876A and banksel translation into the lst ou 2019/08/14 12:43:26 (permalink)
+3 (3)
For your PIC device, the BANKSEL pseudo instruction generates these two instructions:

    BCF/BSF  STATUS,RP1
    BCF/BSF  STATUS,RP0

 
#2
ric
Super Member
  • Total Posts : 23839
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: XC8 and mixed assembly/C code for a PIC16F876A and banksel translation into the lst ou 2019/08/14 13:25:40 (permalink) ☼ Best Answerby TechDpt 2019/08/14 16:39:33
+2 (2)
TechDpt
 
Also, into the old compiler all the variable used inside the assembly was set into the bank0 "psect    rbss_0", inside the XC8 all was moved inside the "psect    bssBANK1" so there is a need to perform the switch into the correct bank, what should be the best way to force allocation of these variable inside the bank0?

You did not show where you defined the variables.
XC8 has documented ways to force some variables to be placed in bank 0, it's in the user guide.
Note you do need to change a project property to tell XC8 to observe these settings.
 

I've also another suspect concerning the use of the sequence btfsc or btfss and the banksel directive, as example look at this code (working, on bank0):

You are right, you can NOT use that code.
BANKSEL in your device generates two actual instructions. BTFSC only skips a single instruction, so you can't put BANKSEL straight after BTFSC.
 
All this is much easier to work around if you move to a PIC16F1xxx or PIC16F1xxxx device.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#3
TechDpt
Starting Member
  • Total Posts : 45
  • Reward points : 0
  • Joined: 2011/06/13 09:29:39
  • Location: 0
  • Status: offline
Re: XC8 and mixed assembly/C code for a PIC16F876A and banksel translation into the lst ou 2019/08/14 16:38:13 (permalink)
0
1and0 and ric thank for your answer and confirmed the doubt so I've only to way:
 
- force the variable to be allocated inside the bank0 to avoid the bank switch selection
 
or
 
- change the code to take in account the banksel macro expansion
 
Thanks!
Best regards!
#4
ric
Super Member
  • Total Posts : 23839
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: XC8 and mixed assembly/C code for a PIC16F876A and banksel translation into the lst ou 2019/08/14 16:49:10 (permalink)
0
TechDpt
- force the variable to be allocated inside the bank0 to avoid the bank switch selection

That only works if you know for sure that bank 0 WILL be selected already at that point in your code.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#5
1and0
Access is Denied
  • Total Posts : 9734
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: online
Re: XC8 and mixed assembly/C code for a PIC16F876A and banksel translation into the lst ou 2019/08/14 17:05:45 (permalink)
0
... which better be to access register 7 (from Post #1).  Why not use PORTC instead hard-coded number 7?
#6
ric
Super Member
  • Total Posts : 23839
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: XC8 and mixed assembly/C code for a PIC16F876A and banksel translation into the lst ou 2019/08/14 17:11:00 (permalink)
+1 (1)
It generates exactly the same code (using "7" or "PORTC"), but I would strongly advise using "PORTC" for your own sanity!
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#7
TechDpt
Starting Member
  • Total Posts : 45
  • Reward points : 0
  • Joined: 2011/06/13 09:29:39
  • Location: 0
  • Status: offline
Re: XC8 and mixed assembly/C code for a PIC16F876A and banksel translation into the lst ou 2019/08/16 16:16:46 (permalink)
0
Hi,
what I've reported are the listing file (.lst), in my original source code I'm using the PORTC of course.
 
Thank!
#8
TechDpt
Starting Member
  • Total Posts : 45
  • Reward points : 0
  • Joined: 2011/06/13 09:29:39
  • Location: 0
  • Status: offline
Re: XC8 and mixed assembly/C code for a PIC16F876A and banksel translation into the lst ou 2019/08/29 00:24:02 (permalink)
+1 (1)
Hi all,
just an update on this matter.
At the end I've succesfully ported the whole code C and assembly for a PIC16F876A from the old Hi-Tech PICC v8.05 PL2 working in a MPLAB IDE v7.30 and WinXP OS to the XC8 v1.45 inside the MPLABX IDE v4.15 in a Win10 OS, here below I've written some notes as summary from this thread:
 
- do not use the BANKSEL macro just after the btfss /btfsc instruction, you have to change the code because the BANKSEL generate more instruction to select the right bank, this is also written inside the XC8 C Compiler User's Guide inside the "BANK AND PAGE SELECTION" chapter:
 
"As this pseudo instruction can expand to more than one instruction on mid-range or baseline parts, it should not immediately follow a BTFSX instruction on those devices."
 
- You should use the BANKSEL / BANKMASK macro not only to access variable defined in C and used inside the assembly code but also to access the General Purpose Registers and the Special Function Register, so, as example, if you need to set the bit 0 on the PORTC you will write:
 
 
        BANKSEL(PORTC) 
        bcf BANKMASK(PORTC),0

 
as first attempt you can write the BANKSEL / BANKMASK for every register in order to see if the code can work as expected, so you can strip out redundant declaration (e.g. if a same port is used sequentially clearly is not necessary to specify every time the BANKSEL macro, just use the BANKMASK).
 
- may be also useful take a look at the .lst generated file located inside the ../MPLABXProjects/<your_project>/dist/default/production and also take a look at the program memory windows from the MPLABX IDE (Window->PIC Memory Views->Program Memory) to see if all is right with your supposed logic and flow of the code.
 
Hope this can be useful to someone.
 
Best regards!
post edited by TechDpt - 2019/08/29 00:25:42
#9
Hen
Starting Member
  • Total Posts : 31
  • Reward points : 0
  • Joined: 2018/10/24 04:01:44
  • Location: 0
  • Status: offline
Re: XC8 and mixed assembly/C code for a PIC16F876A and banksel translation into the lst ou 2019/08/29 21:14:24 (permalink)
+1 (1)
Good feedback.
 
I would dare to add one off-topic issue:
 
If you can avoid mixing assemler and C code, avoiid mixing assembler and C code.
If you can avoid assembler coding all together, avoid assembler coding all together.
 
#10
dan1138
Super Member
  • Total Posts : 3225
  • Reward points : 0
  • Joined: 2007/02/21 23:04:16
  • Location: 0
  • Status: offline
Re: XC8 and mixed assembly/C code for a PIC16F876A and banksel translation into the lst ou 2019/08/29 23:32:58 (permalink)
+1 (1)
Hen
If you can avoid assembler coding all together, avoid assembler coding all together.



In all humor: Embedded developers that cannot code and code well in assembler are lightweights and snivelers. :)
#11
Hen
Starting Member
  • Total Posts : 31
  • Reward points : 0
  • Joined: 2018/10/24 04:01:44
  • Location: 0
  • Status: offline
Re: XC8 and mixed assembly/C code for a PIC16F876A and banksel translation into the lst ou 2019/08/29 23:50:31 (permalink)
0
dan1138
 
In all humor: Embedded developers that cannot code and code well in assembler are lightweights and snivelers. :)

Just to clarify, knowledge does not necessarily match the path forward.
 
#12
Jump to:
© 2019 APG vNext Commercial Version 4.5