Everything
8.5.7 Application Startup

Specify the following in the application.

The items marked with [Optional] may be unnecessary in some cases.

(1)

Preparation of Entry Point (PIC Initiation Address)

This is the address from which the application is initiated.

(2)

Initialization of Stack Pointer [Optional]

This processing is not necessary when the application shares the stack with the master.

When necessary, add appropriate settings by referring to section 7.3.2 (2).

(3)

Initialization of General Registers Used as Base Registers [Optional]

This processing is not necessary when no base register is used.

When necessary, add appropriate settings by referring to section 7.3.2 (3).

(4)

Initialization Processing of Sections [Optional]

This processing is not necessary when the master initializes them.

When necessary, add appropriate settings by referring to the example shown later.

Note that the processing described in section 7.3.2 (5) cannot be used without change.

(5)

Initialization Processing of Libraries [Optional]

This processing is not necessary when no standard library is used.

When necessary, add appropriate settings by referring to section 7.3.2 (6).

Initialization of PSW for main Function Execution [Optional]

Specify interrupt masks or move to the user mode as necessary.

Add appropriate settings by referring to sections 7.3.2 (8) and 7.3.2 (9).

(6)

User Program Execution

Execute the main function.

Specify the processing by referring to section 7.3.2 (10).

The following shows an example of application startup.

The processing is divided into three files.

-

startup_picpid.c: Body of the startup processing.

-

initsct_pid.src: Section initialization for PID; _INITSCT_PID.

This is created by modifying the _INITSCT function described in section 7.3.2 (5) to support the PID function.

"__PID_REG" in the program will be converted into PID register when the assembling.

-

initiolib.c; Contains _INITLIB, which initializes the standard libraries.

This is created by modifying the code described in section 7.3.2 (6) to be used for the application.

[startup_picpid.c]

// Initialization Processing Described in Section 7.3.2(5)
#pragma section C C$DSEC //Section name is set to C$DSEC
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 is set to C$BSEC 
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")};
 
extern void main(void);
extern void _INITLIB(void); // Library initialization processing described 
                            //in section 7.3.2 (6)
#pragma entry application_pic_entry
void application_pic_entry(void)
{ 
    _INITSCT_PICPID();
    _INITLIB();
     main();
}

 

[initsct_pid.src]

; Section Initialization Routine for PID Support
; ** Note ** Check the PID register.
; This code assumes that R13 is used as the PID register. If another 
; register is used as the PID register, modify the description related to R13 
; in the following code to the register assigned as the PID register 
; in your system.
    .glb   __INITSCT_PICPID
    .glb   __PID_TOP
    .section  C$BSEC,ROMDATA,ALIGN=4
    .section  C$DSEC,ROMDATA,ALIGN=4
    .section  P,CODE
 
__INITSCT_PICPID:               ; function: _INITSCT
    .STACK    __INITSCT_PICPID=28
    PUSHM     R1-R6
    ADD       #-__PID_TOP,__PID_REG,R6 ;   How long distance PID moves
;;;
;;; clear BBS(B)
;;;
    ADD       #TOPOF C$BSEC,  R6, R4
    ADD       #SIZEOF C$BSEC, R4, R5
    MOV.L     #0, R2
    BRA       next_loop1
 
loop1:
    MOV.L     [R4+], R1
    MOV.L     [R4+], R3
    CMP       R1, R3
    BLEU      next_loop1
    SUB       R1, R3
    SSTR.B
next_loop1:
    CMP       R4,R5
    BGTU      loop1
 
;;;
;;; copy DATA from ROM(D) to RAM(R)
;;;
    ADD       #TOPOF C$DSEC,  R6, R4
    ADD       #SIZEOF C$DSEC, R4, R5
    BRA       next_loop3
 
loop3:
    MOV.L     [R4+], R2
    MOV.L     [R4+], R3
    MOV.L     [R4+], R1
    CMP       R2, R3
    BLEU      next_loop3
    SUB       R2, R3
    ADD       R6, R2       ; Adjust for real address of PID
    SMOVF
next_loop3:
    CMP       R4, R5
    BGTU      loop3
    POPM      R1-R6
    RTS
 
    .end
 

 

[initiolib.c]

#include <stdio.h>
#include <stdlib.h>
#define IOSTREAM 3
const size_t _sbrk_size = 520; // Specifies the minimum unit of the heap area 
                               // allocation size. (Default: 1024)
 
void _INIT_LOWLEVEL(void);
void _INIT_OTHERLIB(void);
 
void _INITLIB (void)
{
    _INIT_LOWLEVEL(); // Initial settings for low-level interface routines
    _INIT_IOLIB();    // Initial settings for I/O library
    _INIT_OTHERLIB(); // Initial settings for rand and strtok functions
}
void _INIT_LOWLEVEL(void)
{ // Make necessary settings for low-level library
}
void _INIT_OTHERLIB(void)
{
     srand(1); // Initial settings necessary when the rand function is used
}