#if DAVECG_BLOCK_COMMUTATION
;;
;; C166/ST10 startup code generated by EDE for motor control
;;
;; 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
$DEBUG
$NOLOCALS
$CHECKCPU16
$CHECKBUS18
$NOMOD166				; disable the internal set of SFRs
#if DAVECG_XC164CS
$STDNAMES(regxc164cs.def)		; define SFRs
#endif

#if DAVECG_XC164CM
$STDNAMES(regxc164cm.def)		; define SFRs
#endif

@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	CPUCON1, #0x3
	MOV	CPUCON2, #0x8f3d
	EXTR	#1
	MOV	SYSCON0, #0x0000
	EXTR	#1
	MOV	SYSCON1, #0x0
	EXTR	#1
	MOV	SYSCON3, #0xa04c
	MOV	SPSEG, #0x0
	MOV	R0,#0x1008
	MOV	ADDRSEL1, R0
	EXTR	#1
	MOV	PLLCON, #0x7343
	MOV	R0,EBCMOD0
	AND	R0, #(~0xFFFF)|0
	OR	R0, #((0x7400&~0)&0xFFFF)
	MOV	EBCMOD0, R0
	MOV	R0,#0x6240
	MOV	TCONCS0, R0
	MOV	R0,FCONCS0
	AND	R0, #(~0x0037)|0
	OR	R0, #((0x31&~0)&0x0037)
	MOV	FCONCS0, R0
	MOV	R0,#0x0
	MOV	TCONCS1, R0
	MOV	R0,#0x1
	MOV	FCONCS1, R0

	ATOMIC	#4
	MOV	SPSEG,	#0x0			; Set stack segment
	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	DPP1, #PAG C166_XGROUP	; set DPP1 to page of user stack / xnear data
	; Set user stack pointer and make sure it is pointed to by DPP1.
	MOV	R0, #POF (?USRSTACK_TOP - 1) + 0x4001	; set user stack pointer
	BFLDH	PSW, #3, #2		; set local register bank 0 (10)

	MOV	R0, #POF (?USRSTACK0_TOP - 1) + 0x4001	; set user stack pointer
	BFLDH	PSW, #3, #3		; set local register bank 1 (11)

	MOV	R0, #POF (?USRSTACK1_TOP - 1) + 0x4001	; set user stack pointer

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

	EINIT				; End of initialization

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

        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	DATA 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 DATA WORD GLBUSRSTACK 'CUSTACK'
C166_US0 ENDS

; Define empty user stack used with localregister bank 1.
C166_US1 SECTION DATA WORD GLBUSRSTACK 'CUSTACK'
C166_US1 ENDS

; Define empty user stack used with localregister bank 2.
C166_US2 SECTION DATA WORD GLBUSRSTACK 'CUSTACK'
C166_US2 ENDS

C166_DGROUP	DGROUP	__DUMMY
__DUMMY	SECTION DATA WORD PUBLIC 'CNEAR'
__DUMMY	ENDS
C166_XGROUP	DGROUP	__XDUMMY,C166_US,C166_US0,C166_US1
__XDUMMY SECTION DATA WORD PUBLIC
__XDUMMY ENDS

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

CSTART_RBANK REGDEF R0-R15		; Register usage


	END
#endif	//BLOCK

#if DAVECG_FIELD_ORIENTED_CONTROL
;; C166/ST10 startup code generated by EDE for motor handler
;;
;; 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
$DEBUG
$NOLOCALS
$CHECKCPU16
$CHECKBUS18
$NOMOD166				; disable the internal set of SFRs
#if DAVECG_XC164CS
$STDNAMES(regxc164cs.def)		; define SFRs
#endif

#if DAVECG_XC164CM
$STDNAMES(regxc164cm.def)		; define SFRs
#endif

@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	CPUCON1, #0x3
	MOV	CPUCON2, #0x8FBB
	EXTR	#1
	MOV	SYSCON0, #0x0000
	EXTR	#1
	MOV	SYSCON1, #0x0
	EXTR	#1
	MOV	SYSCON3, #0xFCC4
	EXTR	#1
	MOV	IMBCTR, #0x0001
	EXTR	#1
	MOV	PLLCON, #0x7343
	MOV	R0,EBCMOD0
	AND	R0, #(~0xFFFF)|0
	OR	R0, #((0x7400&~0)&0xFFFF)
	MOV	EBCMOD0, R0
	MOV	R0,#0x007F
	MOV	EBCMOD1, R0

	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	DPP1, #PAG C166_XGROUP	; set DPP1 to page of user stack / xnear data
	; Set user stack pointer and make sure it is pointed to by DPP1.
	MOV	R0, #POF (?USRSTACK_TOP - 1) + 0x4001	; set user stack pointer
	BFLDH	PSW, #3, #2		; set local register bank 0 (10)

	MOV	R0, #POF (?USRSTACK0_TOP - 1) + 0x4001	; set user stack pointer
	BFLDH	PSW, #3, #3		; set local register bank 1 (11)

	MOV	R0, #POF (?USRSTACK1_TOP - 1) + 0x4001	; set user stack pointer

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

	EINIT				; End of initialization

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

        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	DATA 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 DATA WORD GLBUSRSTACK 'CUSTACK'
C166_US0 ENDS

; Define empty user stack used with localregister bank 1.
C166_US1 SECTION DATA WORD GLBUSRSTACK 'CUSTACK'
C166_US1 ENDS

; Define empty user stack used with localregister bank 2.
C166_US2 SECTION DATA WORD GLBUSRSTACK 'CUSTACK'
C166_US2 ENDS

C166_DGROUP	DGROUP	__DUMMY
__DUMMY	SECTION DATA WORD PUBLIC 'CNEAR'
__DUMMY	ENDS
C166_XGROUP	DGROUP	__XDUMMY,C166_US,C166_US0,C166_US1
__XDUMMY SECTION DATA WORD PUBLIC
__XDUMMY ENDS

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

CSTART_RBANK REGDEF R0-R15		; Register usage


	END

#endif	//FIELD_ORIENTED_CONTROL
