Hi. You have to read of on the handling of addresses in different ways. Some things (GOTO, CALL...) only codes the lower part of the effective address in the instruction and fetches the higher bits from PCLATH. Some other things (like an RETURN or RETFIE) always fetches the full return address from the stack and does not use PCLATH. > but this will break the BTFSS as there will be more than one instruction there... A well-known "problem"... :-) You simply has to work around it. > the target is nearby, so I'm be very surprised that they appear in different pages. Now, the GOTO uses PCLATH which probably is still pointing to the page from where the original CALL was made. PCLATH is not changed by the CALL. You ca= n start your subroutine by setting PCLATH using the value of the symbol/name of the new code-block. That way, PCLATH will always be correct no matter which page the actual routine happens to end up in. > From what I have read, I need to PAGESEL before each GOTO... It's enough if you so it once at the start of the CODE block (or at each entry-point to the CODE block, if there are multiple possible point to CALL). Then also after a CALL in that code itself, the routines CALL'ed can also have changed PCLATH. But once PCLATH is setup, you can GOTO as much as you like *within* the sam= e page. > Is there anything I can do to the linker script to ensure that the code i= s put in the same page? Two things pops up. - Use the same CODE segment. - Hardcode a memory section in the CODE directive. Jan-Erik. -----Ursprungligt meddelande----- Fr=E5n: piclist-bounces@mit.edu [mailto:piclist-bounces@mit.edu] F=F6r Anth= ony Toft Skickat: den 13 januari 2015 01:27 Till: 'Microcontroller discussion list - Public.' =C4mne: [PIC] Relocatable code, pagesel, call and goto Hey folks, I am midway through writing my first relocatable code project fo= r a 16f876a, and have managed to confuse myself thoroughly... The problem started when I added some new code (in a new CODE block) and my interrupt handler started misbehaving. The code that is exhibiting the problem is here NOT_TMR0 BTFSC PIR1, ADIF CALL HandleAd_R BANKSEL PIE1 BTFSS PIE1, TXIE GOTO NOT_TXING BANKSEL PIR1 BTFSC PIR1, TXIF CALL SendNext_R NOT_TXING The call "GOTO NOT_TXING" appeared to branch into an unrelated function, using the debugger (PICKit 3) I determined that the program counter in the interrupt handler is 0x18?? which doesn't seem right, I would expect it to be 0x00?? as the interrupt handler is introduced with IRQ CODE 0x004. The GOTO changes the lower byte of the program counter, which when combined wit= h PCLATH is the address it jumps to. After a bit of research, it appears that the issue is related to the program page.=20 Which is where the confusion shows up... From what I have read, I need to PAGESEL before each GOTO, but this will break the BTFSS as there will be more than one instruction there, but in this case, the target is nearby, so I'm be very surprised that they appear in different pages. As far as I can tell each CODE block is guaranteed to b= e in a single page. Do I need to PAGESEL the interrupt handler even though I am already there? If I do, will the RETFIE work properly? The current code size is around 6% of the program memory, so why is the linker using more than one page? Is there anything I can do to the linker script to ensure that the code is put in the same page? --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .