;*ADFEject
;---------
;Eject the ADF image
;Entry if called from commandline:
; R0 = pointer to command tail  (if =5, we don't eject the floppy)
; R1 = no. of parameters
; R12 = pointer to module private word or VARS

;Exit:
; R0 - error or corrupt

.Entry_ADFEject
 STMFD   R13!, {R1-R9, R12, R14}
 ADR     R12, VARS

 LDR     R1, [R12, #ADF_Mounted - VARS]	;is an image mounted?
 TEQ     R1, #0
 LDMEQFD R13!, {R1-R9, R12, PC}		;NO, exit immediately

 LDR     R2, [R12, #floppy_content_changed - VARS]
 TEQ     R2, #0				;has the disc contents changed?
 BEQ     _dont_flush			;NO

 TEQ     R1, #3				;is it a JFD?
 LDREQ   R3, [R12, #JFD_tmp + JFD_Bit_Flags - VARS]	;YES
 TSTEQ   R3, #JFD_BF_RW			; |+ should it be flushed?
 BLNE    Entry_ADFFlush			;YES, write the floppy back

 MOV     R1, #0
 STR     R1, [R12, #floppy_content_changed - VARS]

 ._dont_flush
 MOV     R9, R0				;preserve R0
 ADR     R2, ADFFS_Current_DiscName	;notify system disc has changed
 LDRB    R1, [R2, #0]
 TEQ     R1, #0				;do we know the disc name?
 MOV     R1, #Service_DiscDismounted	;PRM2-506
 SWINE   XOS_ServiceCall		;YES, PRM1-254

 TEQ     R9, #5				;were we called from MiscOp?
 LDMEQFD R13!, {R1-R9, R12, PC}		;YES, exit

 ADR     R4, JFD_tmp			;buffer address
 MOV     R2, #JFD_Header		;clear the JFD header buffer
 MOV     R3, #0				;..as it's used to check write protection
 STR     R3, [R12, #obey_file_offset - VARS]	;clear obey file offset as well
 ._L1
   SUBS    R2, R2, #4
   STR     R3, [R4, R2]
 BNE     _L1

 MVN     R4, #0				;release disc buffer
 BL      MemAlloc

 LDR     R2, disk_swap_pending
 TEQ     R2, #0				;are we swapping discs?
 BNE     _exit				;YES, leave settings alone

 MOV     R2, #0				;now unset options set by floppy mount
 STR     R2, [R12, #force_vsync - VARS]	;reset the forced FPS

 LDR     R3, [R12, #prevent_CMOSchanges - VARS]
 CMN     R3, #1					;has it been manually set?
 STRNE   R2, [R12, #prevent_CMOSchanges - VARS]	;NO, clear it

 LDR     R3, [R12, #prevent_RMKills - VARS]
 CMN     R3, #1					;has it been manually set?
 STRNE   R2, [R12, #prevent_RMKills - VARS]	;NO, clear it

 LDR     R3, [R12, #prevent_Hourglass - VARS]
 CMN     R3, #1					;has it been manually set?
 STRNE   R2, [R12, #prevent_Hourglass - VARS]	;NO, clear it

 ._exit
 SUBS    R0, R0, R0			;clear V
LDMFD   R13!, {R1-R9, R12, PC}
