;************************************************************************
;startup routine    startup.src      Copyright (C) 2001 T.KOSAKA TNCT
;  6 Jan 2003   for h8/3048 compiler ver2.0
; 18 Feb 2002
; 14 Dec 2001
;************************************************************************
        .CPU 300HA

        .IMPORT _main, _interrupt_cfunc

        .EXPORT _E_INT, _D_INT  ;for compatible with old version

        .SECTION    VEC, DATA, LOCATE=0;
        .ORG        H'000000
        .DATA.L     STARTUP
        .ORG        H'000070
        .DATA.L     IMIA1

        .SECTION    P, CODE, ALIGN = 2
STARTUP:
        MOV.L   #H'FFF10,ER7	;set stack pointer

        ;***** move Section D(ROM) to Section X(RAM) *****
        MOV.L @_D_Head,ER0      ;source address to ER0
        MOV.L @_X_Head,ER1      ;destination address to ER1
        MOV.L @_D_Size,ER2      ;size to be copied to ER2
        OR.L ER2,ER2            ;(ER2 or ER2) to ER2
        JMP @LOOP_11 
LOOP_1:
        MOV.B @ER0+,R3H         ;source byte to E3H with ER0++
        MOV.B R3H,@ER1          ;E3H to destination
        INC.L #1,ER1            ;increment destination address
        DEC.L #1,ER2            ;ER2--
LOOP_11:
        BNE LOOP_1

        ;***** clear Section B *****
        MOV.L @_B_Head,ER1      ;destination address to ER1
        MOV.L @_B_Size,ER2      ;size to be copied to ER2
        MOV.B #0,R3H            ;0 to R3H
        OR.L ER2,ER2            ;(ER2 or ER2) to ER2
        JMP @LOOP_21 
LOOP_2:
        MOV.B R3H,@ER1          ;R3H to destination
        INC.L #1,ER1            ;increment destination address
        DEC.L #1,ER2            ;ER2--
LOOP_21:
        BNE LOOP_2

        JSR @_main              ; Call main()
EternalLoop:
        BRA EternalLoop

        .SECTION    P, CODE, ALIGN = 2
_E_INT:
        ANDC.B #B'01111111,CCR  ;Clear Interrupt mask = Enable Interrupt
        RTS

        .SECTION    P, CODE, ALIGN = 2
_D_INT:
        ORC.B #B'10000000,CCR   ;Set Interrupt mask = Disable Interrupt
        RTS

	.SECTION P,CODE,ALIGN = 2; 
ITU1_TSR  .EQU  H'FFFF71
IMIA1:
	PUSH.L	ER0
	PUSH.L	ER1
	PUSH.L	ER2
	PUSH.L	ER3
	PUSH.L	ER4
	PUSH.L	ER5
	PUSH.L	ER6
	BCLR	#0,@ITU1_TSR	;Clear IMFA
	JSR @_interrupt_cfunc
	POP.L	ER6
	POP.L	ER5
	POP.L	ER4
	POP.L	ER3
	POP.L	ER2
	POP.L	ER1
	POP.L	ER0
	RTE

; "Clear IMFA" and "push/pop" are written here.
; This makes easy for beginners to write interrupt routine.

        .SECTION    D, DATA, ALIGN = 2
        .SECTION    X, DATA, ALIGN = 2
        .SECTION    B, DATA, ALIGN = 2

        .SECTION    C, DATA, ALIGN = 2
_D_Head:
        .DATA.L (STARTOF D)    ; Section D Head Address
_X_Head:
        .DATA.L (STARTOF X)    ; Section X Head Address
_D_Size:
        .DATA.L (SIZEOF D)     ; Section D Size
_B_Head:
        .DATA.L (STARTOF B)    ; Section B Head Address
_B_Size:
        .DATA.L (SIZEOF B)     ; Section B Size
        .END
