;;
;; C166/ST10 startup code generated by EDE for project Block Commutation
;;
;; If modifications are needed, disable generation of the startup code in EDE:
;; In the EDE Project Options dialog select Application, and then Startup. Make
;; sure the checkbox 'Generate system startup code and add it to project' is not
;; checked. Note that changes in EDE will now no longer be reflected in the
;; startup code. Also be aware that the modifications will be overwritten when
;; the checkbox is enabled again.
;;

$EXTEND2
$CASE
$GENONLY
$NOLOCALS
$CHECKCPU16
$CHECKBUS18
$NOMOD166				; disable the internal set of SFRs
$STDNAMES(regxe164f.def)		; define SFRs

@SET( BIT_INIT, 0 )			; disable(0)/enable(1) initialization of bit
					; variables at startup
$INCLUDE(head.asm)			; Generic definitions (see product include dir)
$INCLUDE(_c_init.asm)			; Initialize C variables

	NAME	CSTART			; module name

	PUBLIC	__IDLE			; cstart end
	PUBLIC	__EXIT			; address to jump to on 'exit()'
	EXTERN	_main:FAR		; start label user program
	EXTERN __C_INIT:FAR

__CSTART_PR	SECTION CODE WORD PUBLIC 'CPROGRAM'
__CSTART 	PROC TASK __CSTART_TASK INTNO __CSTART_INUM = 0

	DISWDT						;  Disable watchdog timer


	MOV	VECSEG, #(0xC00000>>16)			; Set vector table segment

	ATOMIC	#3
	MOV	SP,	#SOF ?SYSSTACK_TOP		; Set stack pointer.
	MOV	STKOV,	#SOF ?SYSSTACK_BOTTOM + 6*2	; Set stack overflow pointer.
	MOV	STKUN,	#SOF ?SYSSTACK_TOP		; Set stack underflow pointer.

	MOV	CP,	#CSTART_RBANK	; Set context pointer.
	NOP

	MOV	DPP0, #PAG ?BASE_DPP0	; Set data page pointer.
	MOV	DPP1, #PAG ?BASE_DPP1	; Initialise these before we can make a
	MOV	DPP2, #PAG ?BASE_DPP2	; user stack call below
	; Set user stack pointer
	; To avoid problems when the userstack is located at the end of a page, the
	; initial value is set using base + size.
	MOV	R0, #?USRSTACK_BOTTOM + (?USRSTACK_TOP - ?USRSTACK_BOTTOM)

	BFLDH	PSW, #3, #2		; set local register bank 0 (10)

	MOV	R0, #?USRSTACK0_BOTTOM + (?USRSTACK0_TOP - ?USRSTACK0_BOTTOM)

	BFLDH	PSW, #3, #3		; set local register bank 1 (11)

	MOV	R0, #?USRSTACK1_BOTTOM + (?USRSTACK1_TOP - ?USRSTACK1_BOTTOM)


	BFLDH	PSW, #3, #0		; set to global user stack

	EINIT				; End of initialization

@_CALL(__C_INIT, R1)			; initialization of global/static data

	BSET	IEN			; allow monitor to break application

        MOV     R12, #0			; set argc to 0
        MOV     R13, #0			; set argv[] to 0
        MOV     R14, #0			;

@_CALL( _main, R1)

; The exit() function causes normal program termination to occur. First, all 
; functions registered by the atexit() function are called in the reverse 
; order. Next, all open streams with unwritten buffered data are flushed, all 
; open streams are closed and all files created by the tmpfile() function are 
; removed. The status value passed to exit is returned in R4.
__EXIT: LABEL FAR			; the exit() or abort() function jumps
					; to this entry.
__IDLE: IDLE				; Power down CPU until peripheral inter-
					; rupt or external interrupt occurs.
	JMPR	CC_UC, __IDLE		; set idle mode again.
	RETV				; Virtual return.
__CSTART	ENDP
__CSTART_PR	ENDS

C166_US	SECTION	LDAT WORD GLBUSRSTACK 'CUSTACK'
	DS	2			; Allocate a user stack of at least 2 bytes
C166_US	ENDS

; Define empty user stack used with localregister bank 0.
C166_US0 SECTION LDAT WORD GLBUSRSTACK 'CUSTACK'
C166_US0 ENDS
C166_US1 SECTION LDAT WORD GLBUSRSTACK 'CUSTACK'
C166_US1 ENDS
C166_US2 SECTION LDAT WORD GLBUSRSTACK 'CUSTACK'
C166_US2 ENDS

C166_SYSSTACK SECTION HDAT WORD SYSSTACK
	DS 512 * 2	; Reserve stack size in words
C166_SYSSTACK ENDS
	SSKDEF 7			; System stack size

CSTART_RBANK REGDEF R0-R15		; Register usage


	END
