;*ADFMount
;---------
;Mount an ADF image into ADFFS
;
;"*ADFMount ."" - will print the currently mounted filename
;Entry:
; R0 = pointer to command tail
; R1 = no. of parameters
; R12 = pointer to module private word

.Entry_ADFMount
 STMFD   R13!, {R1-R12, R14}
 LDR     R12, [R12, #0]			;get our private area

 MOV     R11, R0			;store command tail for later
 LDRB    R2, [R11]
 TEQ     R2, #ASC(".")
 BNE     _skip
 LDR     R0, [R12, #Current_File_Mounted - VARS]
 TEQ     R0, #0
 SWINE   XOS_Write0
 TEQ     R0, #0
 SWINE   XOS_NewLine
 LDMFD   R13!, {R1-R12, PC}
 ._skip

 BL      hourglass_on
 BL      Entry_ADFEject			;Dismount current disc

 LDR     R2, [R12, #Current_File_Mounted - VARS]
 TEQ     R2, R11			;is it an internal call?
 BEQ     _skip_canonicalize		;YES

 MOV     R0, #0
 STR     R0, [R12, #File_Handle - VARS]	;ensure GZ file handle is blank

 ADD     R0, R12, #Current_File_Mounted - VARS
 MOV     R1, R11
 BL      allocate_filename
 BVS     Entry_ADFMount_Exit		;exit on error
 MOV     R11, R0			;R11=filename

 ._skip_canonicalize
 MOV     R0, #23			;read catalogue information
 MOV     R1, R11			;point to filename
 SWI     XOS_File			;PRM2-38
 BVS     Entry_ADFMount_Exit		;exit on error

 TEQ     R0, #3				;is it a directory?
 BNE     _not_a_directory		;NO
 CMP     R4, #0				;YES, does it have a size?
 BHI     _possibly_MultiFS		;YES, might be a DOS disc image

 ._not_a_directory
 TEQ     R0, #1				;did we find a file?
 MOVNE   R2, R0				;NO, generate an error
 MOVNE   R0, #19			; |
 SWINE   XOS_File			; |  PRM2-45
 BVS     Entry_ADFMount_Exit		; +- exit and report an error

					;**************************************
					;read first 4 bytes to see if it's a GZip
 ._possibly_MultiFS			;**************************************
 MOV     R1, R11			;filename
 MOV     R0, #&4F			;open read only
 SWI     XOS_Find			;PRM2-78
 MOVVS   R2, #0				;generate an error
 MOVVS   R0, #19			; |
 SWIVS   XOS_File			; |  PRM2-45
 BVS     Entry_ADFMount_Exit		; +- exit and report an error
 MOV     R10, R0			;preserve the file handle

 MOV     R1, R0				;file handle
 MOV     R0, #4				;read absolute
 ADD     R2, R12, #JFD_tmp - VARS	;pointer to temp buffer
 MOV     R3, #4				;bytes to read
 SWI     XOS_GBPB			;PRM2-69
 BVS     Entry_ADFMount_Exit		;exit on error

 MOV     R0, #0				;close file
 MOV     R1, R10			;file handle
 SWI     XOS_Find			;PRM2-77
 BVS     Entry_ADFMount_Exit		;exit on error

 ADR     R14, Entry_ADFMount_Exit	;return address for errors

 ADD     R0, R12, #FileType_Loaded - VARS
 ADD     R1, R0, #12
 BL      ZeroBlock			;clear the filetype string
 ADD     R0, R12, #JFD_tmp - VARS + 4
 ADD     R1, R0, #JFD_Header - 4
 BL      ZeroBlock			;clear the JFD header

 LDR     R0, [R12, #JFD_tmp - VARS]	;load the 4 bytes we just read
 LDR     R1, _HFE_validation_v12	;HFE v1/2?
 LDR     R2, _HFE_validation_v3		;HFE v3?
 TEQ     R0, R1
 TEQNE   R0, R2
 BEQ     Entry_ADFMount_HFE		;YES

 LDR     R1, _GZ_validation		;..and the GZ header validation
 TEQ     R0, R1				;do they match?
 BNE     Entry_ADFMount_ADF		;NO, not an APD or JFD must be ADF

					;**************************************
					;GZ support (APD, JFD)
					;**************************************
 MOV     R0, R11			;file name pointer
 ADR     R1, _zlibMode			;zlib mode
 SWI     XZLib_GZOpen			;open file via zlib
 BVS     Entry_ADFMount_Exit
 TEQ     R0, #0				;did we get a handle back?
 ADREQ   R0, Error_File_Error		;NO
 BEQ     Entry_ADFMount_Exit		; |+ exit with error
 STR     R0, [R12, #File_Handle - VARS]	;file handle
					;R4 contains file length
 ADD     R1, R12, #JFD_tmp - VARS	;buffer address
 MOV     R2, #4				;bytes to read
 SWI     XZLib_GZRead
 TEQ     R0, #4				;did we read 4 bytes?
 MOVNE   R0, #1 << 31			;NO, must have errored
 ADDNES  R0, R0, R0
 ADRVS   R0, Error_ZLib
 BVS     Entry_ADFMount_Exit

 LDR     R0, [R12, #JFD_tmp - VARS]
 LDR     R1, _APD_validation
 TEQ     R0, R1
 BEQ     Entry_ADFMount_APD

 LDR     R1, _JFD_validation
 TEQ     R0, R1
 BEQ     Entry_ADFMount_JFD
B       Entry_ADFMount_Exit		;unknown GZ file, exit

 ._JFD_validation	DCB "JFDI"
 ._APD_validation	DCB "APDX"
 ._HFE_validation_v12	DCB "HXCP"
 ._HFE_validation_v3	DCB "HXCH"
 ._GZ_validation	DCD &00088B1F		;GZ validation string
 ._zlibMode		DCB "rb", 0		;zlib read-only
 ALIGN




;Entry_ADFMount_mounted
;----------------------
;Once a file type has been loaded, it returns here

.Entry_ADFMount_mounted
 MVN     R0, #0
 STR     R0, [R12, #head_track - VARS]	;reset head position

 BL      get_current_discname
 BL      Check_Ambiguous_Disc_Name	;R0=0 for no error
 TEQ     R0, #0
 BLNE    get_current_discname		;try getting the discname again
 MOVVC   R0, #0
 BVC     Entry_ADFMount_Exit		;VC if discname known

 ADR     R1, ADFFS_Filer_Name		;disc name unknown, open ADFS::0.$
 ADD     R2, R12, #ADFFS_Current_DiscName - VARS
 ._L3
   LDRB    R0, [R1], #1
   STRB    R0, [R2], #1
   TEQ     R0, #0
 BNE     _L3

.Entry_ADFMount_Exit
 MOVS    R10, R0			;preserve R0
 MOVNE   R0, #Type_None			;NE if an load error occurred
 STRNEB  R0, [R12, #FileType_Loaded - VARS]

 ADD     R0, R12, #crc_ccitt - VARS	;free 1772 crc table if it was used
 BL      release_RMA

 LDR     R0, [R12, #File_Handle - VARS]	;GZ file handle
 TEQ     R0, #0				;is there a GZ file open?
 MOVNE   R2, #0				;YES
 STRNE   R2, [R12, #File_Handle - VARS]	;|   clear handle
 SWINE   XZLib_GZClose			;|+  close GZ file if open

 BL      hourglass_off

 MOVS    R0, R10			;did an error occur?
 MOVNE   R10, #1<<31			;YES
 ADDNES  R10, R10, R10			;set V
LDMFD R13!, {R1-R12, PC}




;*ADFOpen
;--------
;Mount an ADF image and open a filer window
;Entry:
; R0 = pointer to command tail
; R1 = no. of parameters
; R12 = point to module private word

.Entry_ADFOpen
 STMFD   R13!, {R1-R12, R14}

 BL      Entry_ADFMount			;mount the disc
 LDMVSFD R13!, {R1-R12, PC}		;exit if the mount failed

 ADR     R0, Open_Filer_Window		;open the filer window
 MOV     R2, R0				;find end of discname
 ._L1
   LDRB    R1, [R2], #1
   TEQ     R1, #0
 BNE     _L1

 MOV     R1, #ASC(".")			;append .$
 STRB    R1, [R2, #-1]
 MOV     R1, #ASC("$")
 STRB    R1, [R2, #0]
 MOV     R1, #0
 STRB    R1, [R2, #1]			;and terminate
 SWI     XOS_CLI			;PRM1-961
 STRB    R1, [R2, #-1]			;terminate at actual name
 SUBS    R0, R0, R0			;clear V
LDMFD   R13!, {R1-R12, PC}




;get_current_discname
;--------------------
;Get the current ADF disc name
;
;Exit:
; R0, R1, R4, R5 corrupt
; R2 - disc name pointer
; V set on error

.get_current_discname
 STMFD    R13!, {R0-R5, R14}

 MOV     R0, #37			;Get the disc name PRM2-121 using
 ADR     R1, ADFFS_Filer_Name		;..convert path to canonicalised name
 ADR     R2, ADFFS_Current_DiscName
 MOV     R3, #0
 MOV     R4, #0
 MOV     R5, #32
 SWI     XOS_FSControl			;PRM2-80
 MOVVS   R4, #0				;error, blank the disc name
 STRVSB  R4, [R2, #0]			;probably "FileCore in use" caused
					;by ADFS looking for a disc change
 LDMVSFD R13!, {R0-R5, PC}
 CMP     R5, #0
 MOVMI   R5, #0				;if the disc name is too long, blank it
 RSBPL   R5, R5, #32 - 2		;terminate before the ".$"
 MOV     R0, #0
 STRB    R0, [R2, R5]			;terminate the discname
LDMFD   R13!, {R0-R5, PC}




.Check_Ambiguous_Disc_Name
 STMFD   R13!, {R1-R12, R14}
 ADR     R10, ADFFS_Current_DiscName

 MOV     R0, #17			;Check for Ambiguous Disc Name
 MOV     R1, R10
 SWI     XOS_File			;PRM2-38 do we get an error?
 BVC     _exit				;NO

 LDR     R14, [R0]			;is the error "Ambiguous Disc Name"?
 EORS    R14, R14, #Error_Ambiguous_Disc_Name
 BNE     _exit				;NO

 ADR     R9, Star_Dismount_DiscName	;YES, issue: *DISMOUNT ADFS::<discname>
 ADD     R10, R10, #6			;R10 now start of discname
 ._L1
   LDRB    R0, [R10], #1
   TEQ     R0, #ASC(".")		;copy to end of floppy name
   MOVEQ   R0, #0
   STRB    R0, [R9], #1
   TEQ     R0, #0
 BNE     _L1

 ADR     R0, Star_Dismount
 SWI     XOS_CLI
LDMFD   R13!, {R1-R12, PC}

 ._exit
 MOV     R0, #0
LDMFD   R13!, {R1-R12, PC}
