Goto Instruction in Microchip

Thread Starter

electronicsLearner77

Joined May 26, 2012
127
I have seen the GOTO instruction in pic controllers. In 8051 and other controllers when reset or power on, the controller goes to the reset vector but why in PIC controllers we have both GOTO instruction and Reset address? What is the purpose of this instruction? On reset which location it will jump to?
 

MaxHeadRoom

Joined Jul 18, 2013
30,655
In Picmicro it is simply Reset, which resets all registers and flags that is the same as a MCLR reset.
The GOTO is simply that to any point in the program.
Max.
 

OBW0549

Joined Mar 2, 2015
3,566
I have seen the GOTO instruction in pic controllers. In 8051 and other controllers when reset or power on, the controller goes to the reset vector but why in PIC controllers we have both GOTO instruction and Reset address?
You are confusing two different things that are only loosely related.

What is the purpose of this instruction?
The GOTO instruction in the PIC, and in nearly all other processors, is a general-purpose instruction the directs the CPU to continue execution at some address other than the next instruction, effectively jumping to an arbitrary place in code memory. That's all it is.

On reset which location it will jump to?
Consult the data sheet or programmer's reference manual for the particular PIC you are using; they are not all the same.
 

OBW0549

Joined Mar 2, 2015
3,566
Sorry for not being clear. But i was referring to the memory map shown above which refers to the GOTO instruction. It is located at address 0x000000.
Ah. Thanks for clarifying that, now it's clear as mud.

Looking at the dsPIC33EVXXXGM00X/10X data sheet, on page 98, I see:

7.3 Reset Sequence A device Reset is not a true exception because the interrupt controller is not involved in the Reset process. The dsPIC33EVXXXGM00X/10X family devices clear their registers in response to a Reset, which forces the PC to zero. The device then begins program execution at location, 0x000000. A GOTO instruction at the Reset address can redirect program execution to the appropriate start-up routine.
So a device RESET clearly resets the program counter to zero, forcing program execution to begin by executing the instruction at address 0x000000.

But what about the "Reset Address" located at address 0x000002 in the memory map on page 31?

On page 34 I see:

A hardware Reset vector is provided to redirect code execution from the default value of the PC on device Reset to the actual start of code. A GOTO instruction is programmed by the user application at address, 0x000000 of Flash memory, with the actual address for the start of code at address, 0x000002 of Flash memory.
which has me completely baffled by now.

Does program execution begin after a RESET condition with the instruction at address 0x000000? Or does execution begin at an address specified by the contents of location 0x000002? Or, as the table on page 95 might (vaguely) suggest, does address 0x000000 contain a GOTO opcode and address 0x000002 contain the address portion of the GOTO instruction?

I think the answer is in the instruction set summary, particularly the table on page 330. There, it shows that the "GOTO address" form of the GOTO instruction is a 2-word instruction, and this is evidently what is at addresses 0x000000 and 0x000002.

From the dsPIC Programmer's Reference Manual, the detailed description of the "GOTO address" form of the GOTO instruction is as follows:

goto.png

It took a bit of sleuthing, but I think I have successfully un-confused myself. I think.
 

MaxHeadRoom

Joined Jul 18, 2013
30,655
What happened to John in TX answer?
The first instruction is a GOTO to an address past the Interupt vectors at 0004.
Sounds right to me.
Max.
 

Thread Starter

electronicsLearner77

Joined May 26, 2012
127
After reading several times i have come to conclusion that address 0x000000 contains GOTO opcode and 0x000002 address contains program start address. Am i correct? But again got confused with the statement GOTO to an instruction past the interrupt vector at 0004. What it actually means?
 

OBW0549

Joined Mar 2, 2015
3,566
After reading several times i have come to conclusion that address 0x000000 contains GOTO opcode and 0x000002 address contains program start address. Am i correct?
Mostly. Looking at the detailed description of the "GOTO address" form of the GOTO instruction, it appears that the first word (at 0x000000) contains the GOTO opcode plus the least significant 16 bits of the address to jump to, while the second word contains the 7 most significant bits of that address.

But again got confused with the statement GOTO to an instruction past the interrupt vector at 0004. What it actually means?
That GOTO instruction at 0x000000 and 0x000002 is there so your program can skip over the interrupt vector table, which extends from 0x000004 through 0x0001FF.

As explained on page 95 of the data sheet, the IVT consists of a block of two-word addresses (not instructions), each one pointing to an interrupt service routine for the associated condition:

The dsPIC33EVXXXGM00X/10X family IVT, shown in Figure 7-2, resides in program memory, starting at location, 000004h. The IVT contains seven nonmaskable trap vectors and up to 187 sources of interrupt. In general, each interrupt source has its own vector. Each interrupt vector contains a 24-bit wide address. The value programmed into each interrupt vector location is the starting address of the associated Interrupt Service Routine (ISR). Interrupt vectors are prioritized in terms of their natural priority. This priority is linked to their position in the vector table. Lower addresses generally have a higher natural priority. For example, the interrupt associated with Vector 0 takes priority over interrupts at any other vector address.
 

MrChips

Joined Oct 2, 2009
34,807
Microchip PICs have evolved over the years and have expanded beyond the initial 12-bit instruction.

On POR (Power-On Reset) the PC (Program Counter) is clear to all zeros. Hence the first instruction to be executed must reside at address 0000.
As pointed, the first instruction is usually

GOTO main

where main the address of the start of program proper. main can be any label you want to choose. Call it Reset if you will but please note that RESET is also a new assembly language instruction in upper PIC models.

By using

GOTO main

this avoids conflict with the usage of the memory locations immediately following. As you have observed, these are the interrupt vector locations. You want to reserve these locations if you plan on using interrupts.
 

MaxHeadRoom

Joined Jul 18, 2013
30,655
After reading several times i have come to conclusion that address 0x000000 contains GOTO opcode and 0x000002 address contains program start address. Am i correct? GOTO to an instruction past the interrupt vector at 0004. What it actually means?
As explained from post #3 on.
It is a jump to main program area to avoid interrupt vector location, as shown in just about all Picmicro .TMP (template) files.
mAX.

Code:
;.PICXX.TMP
;------------------------------------------------------------------------------
; RESET VECTOR
;------------------------------------------------------------------------------

RES_VECT  ORG  0x0000  ; processor reset vector
  GOTO  START  ; go to beginning of program

;------------------------------------------------------------------------------
; HIGH PRIORITY INTERRUPT VECTOR
;------------------------------------------------------------------------------

ISRH  ORG  0x0008

  ; Run the High Priority Interrupt Service Routine
  GOTO  HIGH_ISR 

;------------------------------------------------------------------------------
; LOW PRIORITY INTERRUPT VECTOR
;------------------------------------------------------------------------------

ISRL  ORG  0x0018
 
  ; Run the High Priority Interrupt Service Routine
  GOTO  LOW_ISR 

;------------------------------------------------------------------------------
; HIGH PRIORITY INTERRUPT SERVICE ROUTINE
;------------------------------------------------------------------------------

HIGH_ISR

  ; Insert High Priority ISR Here

  RETFIE  FAST

;------------------------------------------------------------------------------
; LOW PRIORITY INTERRUPT SERVICE ROUTINE
;------------------------------------------------------------------------------

LOW_ISR
  ; Context Saving for Low ISR
  MOVWF  W_TEMP  ; save W register
  MOVFF  STATUS, STATUS_TEMP ; save status register
  MOVFF  BSR, BSR_TEMP  ; save bankselect register

  ; Insert Low Priority ISR Here

  ; Context Saving for Low ISR
  MOVFF  BSR_TEMP, BSR  ; restore bankselect register
  MOVF  W_TEMP, W  ; restore W register
  MOVFF  STATUS_TEMP, STATUS ; restore status register
  RETFIE

;------------------------------------------------------------------------------
; MAIN PROGRAM
;------------------------------------------------------------------------------

START 

  ; Insert User Program Here
 

MrChips

Joined Oct 2, 2009
34,807
On a side note, when the first Microchip PICs came out, I programmed a lot of PIC12C508 OTP version (one-time programmable). Since there was no way of erasing the EPROM in order to fix code errors, I put all the code at the highest memory locations. So it took an extra few milliseconds for the code to start execution from POR but I was able to patch the code by just adding new code below the programmed code.

Hence I was able to reuse my used OTP PIC MCUs.
 
Top