8.3.2 Initial Setting

The initial setting routine (PowerOn_Reset_PC) is a function that contains the procedures required before and after executing the main function. Processings required in the initial setting routine are described below in order.

(1)

Initialization of PSW for Initial Setting Processing

The PSW register necessary for performing the initial setting processing is initialized. For example, disabling interrupts is set in PSW during the initial setting processing to prevent from accepting interrupts.

All bits in PSW are initialized to 0 at a reset, and the interrupt enable bit (I bit) is also initialized to 0 (interrupt disabled state).

(2)

Initialization of Stack Pointer

The stack pointer (USP register and ISP register) is initialized. The #pragma entry declaration for the PowerOn_Reset_PC function makes the compiler automatically create the ISP/USP initialization code at the beginning of the function.

This procedure does not have to be written because the PowerOn_Reset_PC function is declared by #pragma entry.

(3)

Initialization of General Registers Used as Base Registers

When the base option is used in the compiler, general registers used as base addresses in the entire program need to be initialized. The #pragma entry declaration for the PowerOn_Reset_PC function makes the compiler automatically create the initialization code for each register at the beginning of the function.

This procedure does not have to be written because the PowerOn_Reset_PC function is declared by #pragma entry.

(4)

Initialization of Control Registers

The address of the variable vector table is written to INTB. FINTV, FPSW, BPC, and BPSW are also initialized as required. These registers can be initialized using the intrinsic functions of the compiler.

Note however that only PSW is not initialized because it holds the interrupt mask setting.

(5)

Initialization Processing of Sections

The initialization routine for RAM area sections (_INITSCT) is called. Uninitialized data sections are initialized to zero. For initialized data sections, the initial values of the ROM area are copied to the RAM area. _INITSCT is provided as a standard library.

The user needs to write the sections to be initialized to the tables for section initialization (DTBL and BTBL). The section address operator is used to set the start and end addresses of the sections used by the _INITSCT function.

Section names in the section initialization tables are declared, using C$BSEC for uninitialized data areas, and C$DSEC for initialized data areas.

A coding example is shown below.

Example:

#pragma section C C$DSEC     //Section name must be C$DSEC
extern const struct {
    void *rom_s;             //Start address member of the initialized data
                             //section in ROM
    void *rom_e;             //End address member of the initialized data
                             //section in ROM
    void *ram_s;             //Start address member of the initialized data
                             //section in RAM
} DTBL[] = {__sectop("D"), __secend("D"), __sectop("R")};
 
#pragma section C C$BSEC     //Section name must be C$BSEC
extern const struct {
    void *b_s;            //Start address member of the uninitialized data section
    void *b_e;              //End address member of the uninitialized data section
} BTBL[] = {__sectop("B"), __secend("B")};

(6)

Initialization Processing of Libraries

The routine for performing necessary initialization processing (_INITLIB) is called when the C/C++ library functions are used.

In order to set only those values which are necessary for the functions that are actually to be used, please refer to the following guidelines.

-

When an initial setting is required in the prepared low-level interface routines, the initial setting (_INIT_LOWLEVEL) in accordance with the specifications of the low-level interface routines is necessary.

-

When using the rand function or strtok function, initial settings other than those for standard I/O (_INIT_OTHERLIB) are necessary.

An example of a program to perform initial library settings is shown below.

#include <stdio.h>
#include <stdlib.h>
#define IOSTREAM 3
const size_t _sbrk_size = 520;     // Specifies the minimum unit of the size to
                                   // define for the heap area (default: 1024)
extern char *_s1ptr;
 
#ifdef __cplusplus
extern "C" {
#endif
void _INITLIB (void)
{ 
    _INIT_LOWLEVEL();              // Set initial setting for low-level 
                                   // interface routines
    _INIT_OTHERLIB();              // Set initial setting for rand function and 
                                   // strtok function
}
 
void _INIT_LOWLEVEL (void)
{                                  // Set necessary initial setting for low-level 
                                   // library
}
 
void _INIT_OTHERLIB(void)
{
    srand(1);                      // Set initial setting if using rand function
    _s1ptr=NULL;                   // Set initial setting if using strtok function
}
#ifdef __cplusplus
}
#endif

Notes 1.

Specify the filename for the standard I/O file. This name is used in the low-level interface routine "open".

Notes 2.

In the case of a console or other interactive device, a flag is set to prevent the use of buffering.

(7)

Initialization of Global Class Objects

When developing a C++ program, the routine (_CALL_INIT) for calling the constructor of a class object that is declared as global is called. _CALL_INIT is provided as a standard library.

(8)

Initialization of PSW for main Function Execution

The PSW register is initialized. The interrupt mask setting is canceled here.

(9)

Changing of PM Bit in PSW

After a reset, operation is in privileged mode (PM bit in PSW is 0). To switch to user mode, intrinsic function chg_pmusr is executed.

When using the chg_pmusr function, some care should be taken. Refer to the description of chg_pmusr in 4.2.6 Intrinsic Functions.

(10)

User Program Execution

The main function is executed.

(11)

Global Class Object Postprocessing

When developing a C++ program, the routine (_CALL_END) for calling the destructor of a class object that is declared as global is called. _CALL_END is provided as a standard library.