• AVR Freaks

Helpful ReplyHot!Is PIC16F assembly code compatible with PIC18F?

Author
mamm1504
New Member
  • Total Posts : 1
  • Reward points : 0
  • Joined: 2020/01/17 04:31:57
  • Location: 0
  • Status: offline
2020/01/19 12:08:56 (permalink)
0

Is PIC16F assembly code compatible with PIC18F?

I have a fully working PIC16F project, where I control a robot through USART and lookup-tables in assembly. However, when I try to simulate the code in MPLAP with PIC18F, the 'retlw' instruction in the lookup table, return to a completely random place in the code, and not where it was called from.
 
I have changed the configuration list and the bank selection from the PIC16F code, and I have got a successful build with the PIC18. But what causes the different behaviour in the lookup tables, and are PIC16 and PIC18 code compatible?
#1
ric
Super Member
  • Total Posts : 28336
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Is PIC16F assembly code compatible with PIC18F? 2020/01/20 12:48:26 (permalink) ☄ Helpfulby mamm1504 2020/01/21 03:12:12
+1 (1)
mamm1504
... are PIC16 and PIC18 code compatible?

No.
Very different architecture.
PIC16F has 14 bit code words, and stores one instruction per address.
PIC18F has 16 bit code words, but each occupies two addresses.
Old fashioned "modify PCL to jump into a table of RETLW instructions" from PIC16F cannot work the same on PIC18F.
PIC18F have table read instructions to do this much easier.
(Newer PIC16F chips have enhanced FSR/INDF registers that can also do it much easier.)
 

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!
#2
kl27x
Super Member
  • Total Posts : 254
  • Reward points : 0
  • Joined: 2006/09/20 13:51:48
  • Location: 0
  • Status: offline
Re: Is PIC16F assembly code compatible with PIC18F? 2020/01/29 17:06:31 (permalink)
0
Another gotcha in assembly between these two beasts:
Almost every instruction in 16F takes 1 word of program space. Except for lcall and lgoto and maybe a couple other things. 
 
There are a lot of instructions in 18F which actually take up 2 or more program words. So you have to check any of your skip if's and any of your relative jumps/branches while you port your code over.
 
Take this with some salt. It's been a long time since I did this. 18F architecture is not nearly as friendly to assembly programming simply because of this issue. If in doubt on how many words a given instruction takes up in program memory, get familiar with referring to the .lst file. I find that to be helpful.
post edited by kl27x - 2020/01/29 17:14:01
#3
1and0
Access is Denied
  • Total Posts : 11127
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Is PIC16F assembly code compatible with PIC18F? 2020/01/29 22:53:09 (permalink)
0
mamm1504
I have a fully working PIC16F project, where I control a robot through USART and lookup-tables in assembly. However, when I try to simulate the code in MPLAP with PIC18F, the 'retlw' instruction in the lookup table, return to a completely random place in the code, and not where it was called from.
 
I have changed the configuration list and the bank selection from the PIC16F code, and I have got a successful build with the PIC18. But what causes the different behaviour in the lookup tables, and are PIC16 and PIC18 code compatible?

PIC16 devices store one instruction word per address, whereas one instruction word in PIC18 devices takes two addresses. So, for a lookup table of RETLW instructions in a PIC18, the index has to be doubled; i.e.
ReadLookupTbl
        addwf   WREG    ; 2x index
        addwf   PCL
        retlw   0
        retlw   1
        retlw   2
        retlw   3
        ; ...

#4
ric
Super Member
  • Total Posts : 28336
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Is PIC16F assembly code compatible with PIC18F? 2020/01/29 23:01:17 (permalink)
0
kl27x
...
Almost every instruction in 16F takes 1 word of program space. Except for lcall and lgoto and maybe a couple other things. 

EVERY instruction in PIC16F takes one word.
"lcall" and "lgoto" are not instructions, they are assembler macros that generate multiple instructions.
You will never see "lcall" or "lgoto" in the disassembly listing.
 

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 : 11127
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Is PIC16F assembly code compatible with PIC18F? 2020/01/29 23:06:51 (permalink)
+2 (2)
kl27x
Another gotcha in assembly between these two beasts:
Almost every instruction in 16F takes 1 word of program space. Except for lcall and lgoto and maybe a couple other things. 

EVERY instructions in a PIC16 take one program memory word. LCALL and LGOTO are pseudo-instructions (macros) that consist of few instructions.
 
 

There are a lot of instructions in 18F which actually take up 2 or more program words.

Most PIC18 devices have only four double-word instructions. The newer PIC18F K42 devices have a triple-word instructions.
 

So you have to check any of your skip if's and any of your relative jumps/branches while you port your code over.

Skip instructions are non-issue because the second (and third) instruction word of the multiple-word instructions will execute as a NOP by itself.  Relative branches are non-issue if you had used labels, instead of the $ operator.
 

Take this with some salt. It's been a long time since I did this. 18F architecture is not nearly as friendly to assembly programming simply because of this issue.

IMO, PIC18 is easier than PIC16 when programming in assembly, because there is no program memory paging and less data memory banking to worry about.
 

If in doubt on how many words a given instruction takes up in program memory, get familiar with referring to the .lst file. I find that to be helpful.

Read the "Instruction Set Summary" chapter in your PIC datasheet.
 
#6
teenix
Junior Member
  • Total Posts : 69
  • Reward points : 0
  • Joined: 2017/12/21 13:47:21
  • Location: Australia, Melbourne
  • Status: offline
Re: Is PIC16F assembly code compatible with PIC18F? 2020/01/30 20:59:32 (permalink)
0
Here is a simple solution for retlw and goto tables but does not adjust for page boundaries.
You have to account for byte offsets in memory with PIC 18.
Eg RETLW is a 2 byte instruction, GOTO's are 4 byte, BRA is 2 byte
 
Table offset is in Wreg...
 
Table_1 mullw 2h
 movlw HIGH Table_1
 movwf PCLATH
 movf PRODL,W
 addwf PCL
 retlw 0xFD
 retlw 0x44
 ....
Table_2 mullw 4h
 movlw HIGH Table_2
 movwf PCLATH
 movf PRODL,W
 addwf PCL
 goto Code_20
 goto Code_21
 ....
Table_3 mullw 2h
 movlw HIGH Table_3
 movwf PCLATH
 movf PRODL,W
 addwf PCL
 bra Code_30
 bra Code_31
 ....
cheers
 
Tony
#7
ric
Super Member
  • Total Posts : 28336
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Is PIC16F assembly code compatible with PIC18F? 2020/01/30 21:01:57 (permalink)
0
MUCH simpler/safer if you just use the TBLRD instruction.
Also allows you to save two bytes per instruction word.

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!
#8
teenix
Junior Member
  • Total Posts : 69
  • Reward points : 0
  • Joined: 2017/12/21 13:47:21
  • Location: Australia, Melbourne
  • Status: offline
Re: Is PIC16F assembly code compatible with PIC18F? 2020/01/30 21:06:36 (permalink)
0
Totally agree, but just hoping it would show why his 16F code is failing in the 18F :-)
 
cheers
 
Tony
#9
Jump to:
© 2020 APG vNext Commercial Version 4.5