;
; dpoll
;
; Wimp_Poll support for DLLs and applications that use them
;
;  1994 Straylight
;

;----- Standard stuff -------------------------------------------------------

		GET	libs:header
		GET	libs:swis

;----- Main code ------------------------------------------------------------

		AREA	|DLL$$Code|,CODE,READONLY

; --- _dll_wimpPoll ---
;
; On entry:	a1 == event mask
;		a2 == pointer to wimp poll block
;		a4 == pointer to poll word if bit 22 of a1 is set
; On exit:	a1 == reason code returned by Wimp_Poll
;		Other registers preserved
;
; Floating point status is preserved if a1 on entry has bit 24 set and the
; floating point emulator or hardware is present.  The floating point
; registers preserved are f4-f7 and the FPSR.

		EXPORT	|_dll_wimpPoll|
|_dll_wimpPoll|	ROUT

		STMFD	sp!,{v1-v3,lr}

		MOV	v2,#0			;Don't restore FP state
		TST	a1,#1<<24		;Does caller want FP saved?
		BIC	v3,a1,#1<<24		;We're doing it, not the WIMP
		BEQ	%00			;No -- skip this bit
		SWI	XFPEmulator_Version	;Is the FPE/FPA there?
		MOVVC	v2,#1			;Yes -- set flag to restore
		BLVC	save_fp			;Save the FP state

00		SWI	XDLL_SaveHandle		;Get my application handle
		MOV	v1,a1			;Look after it
		MOV	a1,v3			;Restore event mask
		SWI	XWimp_Poll		;Do the SWI call
		ORRVS	v2,v2,#2		;Set bit 1 of v2 if error
		MOV	v3,a1			;Save the reason code/error
		MOV	a1,v1			;Restore application handle
		SWI	XDLL_RestoreHandle	;Put the handle back again
		MOV	a1,v3			;Restore event code/error

		TST	v2,#1			;Is bit 0 set?
		BLNE	restore_fp		;Yes -- restore FP state
		TST	v2,#2			;Was there an error?
		LDMFD	sp!,{v1-v3,lr}		;Restore all registers anyhow
		BICEQS	pc,lr,#V_flag		;No -- clear V flag
		ORRS	pc,lr,#V_flag		;Yes -- set V flag

		LTORG

; --- _dll_wimpPollIdle ---
;
; On entry:	a1 == event mask
;		a2 == pointer to wimp poll block
;		a3 == earliest time to return with an idle event
;		a4 == pointer to poll word if bit 22 of a1 is set
; On exit:	a1 == reason code returned by Wimp_Poll
;		Other registers preserved
;
; Floating point status is preserved if a1 on entry has bit 24 set and the
; floating point emulator or hardware is present.  The floating point
; registers preserved are f4-f7 and the FPSR.

		EXPORT	|_dll_wimpPollIdle|
|_dll_wimpPollIdle| ROUT

		STMFD	sp!,{v1-v3,lr}

		MOV	v2,#0			;Don't restore FP state
		TST	a1,#1<<24		;Does caller want FP saved?
		BIC	v3,a1,#1<<24		;We're doing it, not the WIMP
		BEQ	%00			;No -- skip this bit
		SWI	XFPEmulator_Version	;Is the FPE/FPA there?
		MOVVC	v2,#1			;Yes -- set flag to restore
		BLVC	save_fp			;Save the FP state

00		SWI	XDLL_SaveHandle		;Get my application handle
		MOV	v1,a1			;Look after it
		MOV	a1,v3			;Restore event mask
		SWI	XWimp_PollIdle		;Do the SWI call
		ORRVS	v2,v2,#2		;Set bit 1 of v2 if error
		MOV	v3,a1			;Save the reason code/error
		MOV	a1,v1			;Restore application handle
		SWI	XDLL_RestoreHandle	;Put the handle back again
		MOV	a1,v3			;Restore event code/error

		TST	v2,#1			;Is bit 0 set?
		BLNE	restore_fp		;Yes -- restore FP state
		TST	v2,#2			;Was there an error?
		LDMFD	sp!,{v1-v3,lr}		;Restore all registers anyhow
		BICEQS	pc,lr,#V_flag		;No -- clear V flag
		ORRS	pc,lr,#V_flag		;Yes -- set V flag

		LTORG

; --- save_fp ---
;
; On entry:	--
; On exit:	v1 corrupted

save_fp		ROUT

		RFS	v1			;Read the FP status word
		STMFD	sp!,{v1}		;Stack it
		MOV	v1,#0			;We will zero the FPSR
		WFS	v1			;Zero it
		SUB	sp,sp,#4*12		;Leave space for 4 FP regs
		STFE	f4,[sp,#0*12]		;Stack F4
		STFE	f5,[sp,#1*12]		;Stack F5
		STFE	f6,[sp,#2*12]		;Stack F6
		STFE	f7,[sp,#3*12]		;Stack F7
		MOVS	pc,lr			;Return to caller

; --- restore_fp ---
;
; On entry:	--
; On exit:	v1 corrupted

restore_fp	ROUT

		MOV	v1,#0			;We will zero the FPSR
		WFS	v1			;Zero it
		LDFE	f4,[sp,#0*12]		;Unstack F4
		LDFE	f5,[sp,#1*12]		;Unstack F5
		LDFE	f6,[sp,#2*12]		;Unstack F6
		LDFE	f7,[sp,#3*12]		;Unstack F7
		ADD	sp,sp,#4*12		;Move stack ptr up past FP
		LDMFD	sp!,{v1}		;Restore FPSR
		WFS	v1			;Write it to the FPSR
		MOVS	pc,lr			;Return to caller

;----- That's all, folks ----------------------------------------------------

		END
