;*ADFFlush
;---------
;Save changes to the currently loaded ADF or JFD file
;Entry:
; R0 = pointer to command tail
; R1 = no. of parameters	1=ON/OFF	2=Internall call to silently flush
; R12 = pointer to module private word

;Exit:
; R0 - error or corrupt
; R1 - corrupt

.Entry_ADFFlush
 STMFD   R13!, {R2, R14}
 LDR     R12, [R12, #0]

 TEQ     R1, #1				;are we toggling on/off
 BEQ     _toggle

 LDRB    R14, [R12, #floppy_content_changed - VARS]
 TEQ     R14, #0			;has the file changed?
 LDRNE   R0, [R12, #Current_File_Mounted - VARS]
 TEQNE   R0, #0				;do we have a filename?
 LDMEQFD R13!, {R2, PC}			;NO, fail silently

 LDRB    R2, [R12, #Type_Mounted - VARS]
 TEQ     R2, #Type_ADF			;is it an ADF?
 BEQ     _retain
 TEQ     R14, #Type_JFD			;is it a JFD?
 ADRNE   R0, Error_Disc_Flush		;NO
 BNE     _error

 LDR     R14, [R12, #JFD_tmp - VARS + JFD_MinVersion]
 SUBS    R14, R14, #JFD_Supports_Autoflush	;does it support flushing?
 ADRMI   R0, Error_Disc_Flush2		;NO
 BMI     _error

 ._retain				;YES
 STRB    R2, [R12, #flush_in_progress - VARS]	;set to non-zero
 LDMFD   R13!, {R2, R14}
B       Entry_ADFSave			;save the file

 ._error
 TEQ     R1, #2				;internal call?
 MOVNE   R14, #1 << 31
 ADDNES  R14, R14, R14			;set V
LDMFD   R13!, {R2, PC}

._toggle
 BL      On_Off_Option			;Read the On/Off option
 LDR     R14, [R12, #JFD_tmp - VARS + JFD_Bit_Flags]
 TEQ     R0, #0
 BICEQ   R14, R14, #JFD_BF_AutoFlush	;OFF
 ORRNE   R14, R14, #JFD_BF_AutoFlush	;ON
 STR     R14, [R12, #JFD_tmp - VARS + JFD_Bit_Flags]
LDMFD   R13!, {R2, PC}




;Performs a scheduled auto flush after performing some sanity checks
;entry:
; R12 = VARS

.Entry_Auto_Flush
 STMFD   R13!, {R0-R1, R14}

 LDRB    R14, [R12, #floppy_content_changed - VARS]
 TEQ     R14, #0			;has the file changed?
 LDRNE   R0, [R12, #Current_File_Mounted - VARS]	;YES
 TEQNE   R0, #0				; | do we have a filename?
 LDMEQFD R13!, {R0-R1, PC}		; |+ NO, exit silently

 LDRB    R1, [R12, #Type_Mounted - VARS]
 TEQ     R1, #Type_ADF			;is it ADF?
 BEQ     _retain			;YES, allow it

 TEQ     R14, #Type_JFD			;is it a JFD?
 LDMNEFD R13!, {R0-R1, PC}		;NO, exit silently

 LDR     R14, [R12, #JFD_tmp - VARS + JFD_MinVersion]
 SUBS    R14, R14, #JFD_Supports_Autoflush		;does it support auto-flush?
 LDMMIFD R13!, {R0-R1, PC}		;NO, exit silently

 ._retain
 STRB    R1, [R12, #flush_in_progress - VARS]	;set to non-zero
 BL      Entry_ADFSave
 SUBS    R0, R0, R0			;ignore errors
LDMFD   R13!, {R0-R1, PC}
