----- Original Message ----- From: "Jens M. Guessregen / Mailinglists" To: "Microcontroller discussion list - Public." Sent: Tuesday, May 01, 2007 2:05 PM Subject: RE: [PIC] Using PCL for relative jumps >> > I did some sim-debugging and had to find out, that RETLW >> is not restoring PCLATH correctly after returning from the >> computed goto. >> > A normal RETURN does :-( >> > >> > >> You are wrong. The datalist is not so clear but the PCLATH is >> NEVER restored by a return. >> If you want to call a subroutine in another page: >> PAGESEL MySub ;PAGESEL is an internal MPASM >> macro that load the correct value >> call MySub >> PAGESEL CurrentPage ;the choice of CurrentPage is >> important! > > I was not sure, if a RETURN restores the PCLATH, but all the doc's I > found in the web said, it does. > I made a small sample code for a 16F877A, jumping between the pages all > over the 8Kbyte ROM. > I used the ICD2 on real hardware with MPLAB7.50 and also the Software > Simulator in 7.50. > > And ... after the RETURN, the PCLATH was restored to the value, the CALL > was made from ... > But only with a RETURN. RETLW was not restoring, but RETLW is the one to > use for tables ... > > And, the datasheet is absolute clear about the PC stack: > > This is from the 16F877A datasheet: > > 2.3.2 STACK > The PIC16F87XA family has an 8-level deep x 13-bit > wide hardware stack. The stack space is not part of > either program or data space and the stack pointer is not > readable or writable. The PC is PUSHed onto the stack > when a CALL instruction is executed, or an interrupt > causes a branch. The stack is POP'ed in the event of a > RETURN, RETLW or a RETFIE instruction execution. > PCLATH is not affected by a PUSH or POP operation. > > That means, that a RETURN, RETLW and RETFIE will cause a POP of PC (= > PCLATH and PCL) back to where the CALL was made from, or? > > Best Jens > > -- > http://www.piclist.com PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist > Caveat: I have only a few months exposure to Microchip PICs and, then, only the mid-range (primarily 16F84 and 16F88). However, that doesn't stop this old fool from posting. According to the PICMicro Mid-Range MCU family Reference Manual (DS33023A) , page 6-5, Table 6-2, and text: 1. The high byte of the PC can be modified ONLY through PCLATH (6.2.4). 2. The 13-bit address in the PC (PCH:PCL) is PUSHed onto the stack. 3. Any return results in the 13-bit address at the top of the stack being put back into the PC (PCH:PCL). 4. Explicitly stated at bottom of table that "PCLATH is never updated with the contents of PCH", which brings us back to #1 (also supported by Figure 6-1 showing PCLATH -> PC, but not the reverse Have a wonderful Wednesday. Richard Seriani, Sr. -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist