;*********************************************************************************************************
;*                                               uC/OS-II
;*                                         The Real-Time Kernel
;*
;*                        (c) Copyright 1992-1998, Jean J. Labrosse, Plantation, FL
;*                                          All Rights Reserved
;*
;*
;*                                          KeilC51 Specific code
;*                                          SMALL MEMORY MODEL
;*
;* File : OS_CPU_C.C
;* By   : Jean J. Labrosse
;* Refer to Code Written By 		: Yang Yi (http://www.zlgmcu.com/philips/philips-embedsys.asp)
;* Port to KeilC51 Small Mode By	: Li Zhanglin (wzzlin@nankai.edu.cn)
;*********************************************************************************************************

;		==========================================
;		------------------ NOTE  -----------------
;		==========================================

;		------------ abbreviation ----------------

;	Rst:	restore
;	Stk:	stack
;	Smlt:	simulated

;		----- OSTCBStk stored as following format ---

;	CONTENT					START POSITION IN OSTCBStk	
;	----------				----------------------  	
;	AR7									
;	AR6
;	AR5
;	AR4
;	AR3
;	AR2
;	AR1
;	AR0
;	PSW
;	DPL
;	DPH
;	B
;	ACC								
;	HARDWARE STACK CONTENT			2+SmltStkSize 						
;	HARDWARE STACK SIZE(INCLUDE REGISTERS)	1+SmltStkSize  		
;	SIMULATED STACK CONTENT			1				
;	?C_IBP					0

;		==========================================
;		------------------ MODULE ----------------
;		==========================================
	NAME	OS_CPU_A



;		==========================================
;		------------------ SEGMENT  --------------
;		==========================================

	?PR?OSStartHighRdy?OS_CPU_A	SEGMENT CODE
	?PR?OSCtxSw?OS_CPU_A		SEGMENT CODE
	?PR?OSIntCtxSw?OS_CPU_A		SEGMENT	CODE

;		===========================================
;		------------------ EXTRN ------------------
;		===========================================

	EXTRN XDATA	(OSTCBHighRdy)
	EXTRN XDATA	(OSTCBCur)
	EXTRN XDATA	(OSPrioCur)
        EXTRN XDATA	(OSPrioHighRdy)
	EXTRN XDATA	(OSRunning)

	EXTRN DATA	(?C_IBP)

	EXTRN CODE	(_?OSTaskSwHook)

;		===========================================
;		------------------ PUBLIC -----------------
;		===========================================
	PUBLIC	OSStartHighRdy
	PUBLIC	OSCtxSw
	PUBLIC	OSIntCtxSw

;		===========================================
;		------------------ STACK ------------------
;		===========================================
;The SEGMENT ?STACK will combined will stack defined in startup.asm
	?STACK SEGMENT IDATA
        RSEG ?STACK
StkBottom:
        
;		===========================================
;		------------------ FUNCTIONS --------------
;		===========================================

;		-------------------------------------------
;		-------------- OSStartHighRdy() -----------
;		-------------------------------------------

	RSEG ?PR?OSStartHighRdy?OS_CPU_A
OSStartHighRdy:

;-----------------
; OSRunning = TURE
;-----------------

;because EA is not enabled. where in OSStartHighRdy set OSRunning to true is not important
	MOV	DPH,#HIGH (OSRunning)
	MOV	DPL,#LOW  (OSRunning)
	MOV	A,#1
	MOVX	@DPTR,A

;-----------------
; TR0 = 1
;-----------------
	SETB	TR0

OSCPUARstStk:
;-----------------
;Get OSTCBHighRdy->OSTCBStkPtr to DPTR
;-----------------

;high byte is at low address

;Move OSTCBHighRdy to dptr
	MOV	DPH,#HIGH(OSTCBHighRdy)
	MOV	DPL,#LOW (OSTCBHighRdy)
	MOVX	A,@DPTR
	MOV	R0,A
	INC	DPTR
	MOVX	A,@DPTR
	MOV	DPH,R0
	MOV	DPL,A

;Move OSTCBStkPtr to dptr
	MOVX	A,@DPTR
	MOV	R0,A
	INC	DPTR
	MOVX	A,@DPTR
	MOV	DPH,R0
	MOV	DPL,A

;-----------------
;restore stack
;-----------------

;restore ?C_IBP
	MOVX	A,@DPTR
	MOV	?C_IBP,A

;restore simulated stack
	JZ	OSCPUASmltRstOver	;simulated stack size == 0, that is ?C_IBP == 0x100
	DEC	A			;Let R0 point to ?C_IBP - 1
	MOV	R0,A
	CPL	A			;caculate copy rotation times, store in R1
	MOV	R1,A
OSCPUASmltRstStk:
	INC	R0
	INC	DPTR
	MOVX	A,@DPTR
	MOV	@R0,A
	DJNZ	R1,OSCPUASmltRstStk
OSCPUASmltRstOver:

;restore sp, sp = #stkBottom - 1 + "Hardware stack size".
	INC	DPTR
	MOVX	A,@DPTR			;now hardware stack size is stored in A
	MOV	R0,#StkBottom - 1	;start of hardware stack defined by keilc system
	MOV	R1,A			;store rotation times in R1
	ADD	A,R0			;sp
	MOV	SP,A

;restore hardware stack 
;stack top is at high address 
;HardStk will never be empty
OSCPUARstHardStk:
	INC	R0			
	INC	DPTR
	MOVX	A,@DPTR
	MOV	@R0,A
	DJNZ	R1,OSCPUARstHardStk

;restore registers
	POP	07
	POP	06
	POP	05
	POP	04
	POP	03
	POP	02
	POP	01
	POP	00
	POP	PSW
	POP	DPL
	POP	DPH
	POP	B
	POP	ACC	;A is register but ACC is direct ram

	SETB	EA	;after start, enable EA
	RETI



;			--------------------------------------------------
;			--------------------- OSCtxSw() ------------------
;			--------------------------------------------------

	RSEG ?PR?OSCtxSw?OS_CPU_A
OSCtxSw:

;-----------------
; push registers
;-----------------
	PUSH	ACC
	PUSH	B
	PUSH	DPH
	PUSH	DPL
	PUSH	PSW
	PUSH	00
	PUSH	01
	PUSH	02
	PUSH	03
	PUSH	04
	PUSH	05
	PUSH	06
	PUSH	07

OSCPUAUniformCtxSw:
;assume register is pushed to hardware stack, in the format as discripted at the beginning of this file
;Before jump OSCPUAUniformCtxSw, OSCtxSw or OSIntCtxSw must first push registers to hardware stack as this format.

;----------------------
;Get OSTCBCurRdy->OSTCBStkPtr to DPTR
;-----------------------
;high byte is at low address

;OSTCBCurRdy to dptr
	MOV	DPH,#HIGH(OSTCBCur)
	MOV	DPL,#LOW (OSTCBCur)
	MOVX	A,@DPTR
	MOV	R0,A
	INC	DPTR
	MOVX	A,@DPTR
	MOV	DPH,R0
	MOV	DPL,A

;OSTCBStkPtr to dptr
	MOVX	A,@DPTR
	MOV	R0,A
	INC	DPTR
	MOVX	A,@DPTR
	MOV	DPH,R0
	MOV	DPL,A


;--------------------
;save stack
;--------------------

;store ?C_IBP
	MOV	A,?C_IBP
	MOVX	@DPTR,A

;store simulated stack
	JZ	OSCPUASmltSaveOver	;simulated stack size == 0, that is A == 0x100
	MOV	R0,A
OSCPUASmltSave:
	INC	DPTR
	MOV	A,@R0
	MOVX	@DPTR,A
	INC	R0
	CJNE	R0,#0,OSCPUASmltSave
OSCPUASmltSaveOver:

;store sp size
	INC	DPTR
	MOV	A,SP
	CLR	C
	SUBB	A,#StkBottom - 1	
	MOVX	@DPTR,A

;store hardware stack and registers
;stack top is at high address 
	MOV	R1,A		;caluate copy rotation times
	MOV	R0,#StkBottom - 1	;start of stack defined by keilc system
OSCPUASaveStk:
	INC	R0
	INC	DPTR
	MOV	A,@R0
	MOVX	@DPTR,A
	DJNZ	R1,OSCPUASaveStk

;-------------------
;call OSTaskSwHook
;-------------------

	LCALL _?OSTaskSwHook

;-------------------
; OSTCBCur = OSTCBHighRdy
;-------------------
	
	MOV	DPH,#HIGH (OSTCBHighRdy)
	MOV	DPL,#LOW  (OSTCBHighRdy)
	MOVX	A,@DPTR
	MOV	R7,A		;store to R7/R6
	INC	DPTR
	MOVX	A,@DPTR
	MOV	R6,A

	MOV	DPH,#HIGH (OSTCBCur)
	MOV	DPL,#LOW  (OSTCBCur)
	MOV	A,R7
	MOVX	@DPTR,A
	MOV	A,R6
	INC	DPTR
	MOVX	@DPTR,A

;-----------------
; OSPrioCur = OSPrioHighRdy
;-----------------
	
	MOV	DPH,#HIGH (OSPrioHighRdy)
	MOV	DPL,#LOW  (OSPrioHighRdy)
	MOVX	A,@DPTR

	MOV	DPH,#HIGH (OSPrioCur)
	MOV	DPL,#LOW  (OSPrioCur)
	MOVX	@DPTR,A

;-----------------
; Restore Ctx
;-----------------
	LJMP	OSCPUARstStk

;			-------------------------------------------
;			---------------- OSIntCtxSw ---------------
;			-------------------------------------------
	RSEG ?PR?OSIntCtxSw?OS_CPU_A
OSIntCtxSw:
;------------------
; adjust sp: delete stack content of OSIntExit(),OSIntCtxSw(). SP -= 4
;------------------

	MOV	A,SP
	CLR	C
	SUBB	A,#4
	MOV	SP,A

	LJMP	OSCPUAUniformCtxSw

END
