;*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

;might be called internally, so R12 might not be correct

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

 MOV     R9, R0				;R0=5 if called from MiscOp
 LDRB    R1, [R12, #Type_Mounted - VARS]	;is an image mounted?
 TEQ     R1, #Type_None
 LDMEQFD R13!, {R1-R9, R12, PC}		;NO, exit immediately

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

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

 MOV     R1, #0				;just in case Flush failed
 STRB    R1, [R12, #floppy_content_changed - VARS]

 ._dont_flush
 ADD     R2, R12, #ADFFS_Current_DiscName - VARS	;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

 ADD     R0, R12, #JFD_tmp - VARS
 ADD     R1, R0, #JFD_Header
 BL      ZeroBlock			;clear the JFD header buffer

 MOV     R3, #0
 STR     R3, [R12, #obey_file_offset - VARS]	;clear obey file offset as well
 
 MVN     R4, #0				;release disc buffer
 BL      MemAlloc

 LDRB    R2, [R12, #disk_swap_pending - VARS]
 TEQ     R2, #0				;are we swapping discs?
 BNE     _exit				;YES, leave settings alone

 BL      release_filename_current

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

 LDRB    R3, [R12, #prevent_CMOSchanges - VARS]
 CMP     R3, #&FF				;has it been manually set?
 STRNEB  R2, [R12, #prevent_CMOSchanges - VARS]	;NO, clear it

 LDRB    R3, [R12, #prevent_RMKills - VARS]
 CMP     R3, #&FF				;has it been manually set?
 STRNEB  R2, [R12, #prevent_RMKills - VARS]	;NO, clear it

 LDRB    R3, [R12, #prevent_Hourglass - VARS]
 CMP     R3, #&FF				;has it been manually set?
 STRNEB  R2, [R12, #prevent_Hourglass - VARS]	;NO, clear it

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