Microchip Technology SW006022-2N Data Sheet
MPLAB
®
XC16 C COMPILER
USER’S GUIDE
2012 Microchip Technology Inc.
DS52071B-page 187
Chapter 12. Main, Runtime Startup and Reset
12.1
INTRODUCTION
When creating C code, there are elements that are required to ensure proper program
operation: a main function must be present; startup code to initialize and clear vari-
ables, to set up registers and the processor; and Reset conditions need to be handled.
The following topics are discussed in this section:
operation: a main function must be present; startup code to initialize and clear vari-
ables, to set up registers and the processor; and Reset conditions need to be handled.
The following topics are discussed in this section:
12.2
THE
main
FUNCTION
The identifier main is special. It is 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 func-
tion 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 startup code is executed first and is responsible for transferring
control to the main() function.
the first function to execute in a program. You must always have one and only one func-
tion 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 startup code is executed first and is responsible for transferring
control to the main() function.
The prototype that should be used for main() is as follows.
int main(void);
12.3
RUNTIME STARTUP AND INITIALIZATION
A 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 startup code to perform these tasks, specifically (and in no particular order):
particular state before it can begin execution of its function main(). It is the job of the
runtime startup code to perform these tasks, specifically (and in no particular order):
• Initialization of global variables assigned a value when defined
• Initialization of the stack
• Clearing of non-initialized global variables
• General setup of registers or processor state
Two C run-time startup modules are included in the libpic30-omf.a archive/library.
The entry point for both startup modules is __reset. The linker scripts construct a
GOTO _ _reset
The entry point for both startup modules is __reset. The linker scripts construct a
GOTO _ _reset
instruction at location 0 in program memory, which transfers control
upon device Reset.
The primary startup module (crt0.o) is linked by default and performs the following:
1.
The Stack Pointer (W15) and Stack Pointer Limit register (SPLIM) are initialized,
using values provided by the linker or a custom linker script. For more
information, see Section 4.4 “Stack”.
using values provided by the linker or a custom linker script. For more
information, see Section 4.4 “Stack”.
2.
If a .const section is defined, it is mapped into the program space visibility
window by initializing the PSV page and CORCON registers, as appropriate, if
const-in-code
window by initializing the PSV page and CORCON registers, as appropriate, if
const-in-code
memory mode is used or variables have been explicitly
allocated to space(auto_psv).