Microchip Technology SW006023-2N Data Sheet

Page of 238
MPLAB
®
 XC32 C/C++ COMPILER
USER’S GUIDE
 2012 Microchip Technology Inc.
DS51686E-page 143
Chapter 12.  Main, Runtime Start-up and Reset
12.1
INTRODUCTION
When creating C/C++ code, there are elements that are required to ensure proper pro-
gram operation: a main function must be present; start-up code will be needed to ini-
tialize and clear variables and setup registers and the processor; and reset conditions 
will need to be handled.
12.2
THE MAIN FUNCTION
The identifier main is special. It must be used as the name of a function that will be the 
first function to execute in a program. You must always have one and only one function 
called main in your programs. Code associated with main, however, is not the first 
code to execute after reset. Additional code provided by the compiler and known as the 
runtime start-up code is executed first and is responsible for transferring control to the 
main()
 function.
12.3
RUNTIME START-UP CODE
A C/C++ program requires certain objects to be initialized and the processor to be in a 
particular state before it can begin execution of its function main(). It is the job of 
the runtime start-up code to perform these tasks. The runtime start-up code is exe-
cuted before main(), but if you require any special initialization to be performed imme-
diately after reset, you should use on reset feature described in Section 12.4 “The On 
Reset Routine”
The PIC32MX start-up code must perform the following:
1.
Jump to NMI Handler if an NMI Occurred
2.
Initialize Stack Pointer and Heap
3.
Initialize Global Pointer
4.
Initialize or Clear Variables and RAM Functions Using the Data-Initialization 
Template
5.
Initialize Bus Matrix Registers
6.
Call “On Bootstrap” Procedure
7.
Change Location of Exception Vectors
8.
For C++, call the C++ initialization code to invoke all constructors for file-scope 
static storage objects
9.
Call Main
The following provisions are made regarding the run-time model:
• Kernel mode only
• KSEG1 only
• RAM functions are attributed with __ramfunc__ or __longramfunc__, 
(defined in sys/attribs.h) meaning that all RAM functions end up in 
the .ramfunc section and the function is ramfunc attributed.