;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; WARNING: MAP THE STACK SOMEWHERE BELOW C000 !
;ifdef buildZXS_DSK
;FileName_Settings
;	db "SETUPV02.C",&ff
;endif
;ifdef buildZXS_TRD
;	FileName_Settings
;		db "SETUPV02C"
;endif 
;ifdef buildZXS_TAP
;FileName_Settings
;endif

DiskDriver_LoadDirect:
	push hl
		ld hl,null
		ld (DiscDestRelocateCall_Plus2-2),hl
	pop hl
jp DiskDriver_Load

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; TRD Betadisk Driver  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ifdef buildZXS_TRD



DiskDriver_Save:
ld (DiskSafeSP_Plus2-2),sp


push bc
	push de

	;	ld hl,FileDescriptor
		ld c,#13		;copy the file descriptor to TR-DOS system variables
		call #3D13
		ld a,9			;find a file with matching 9 first bytes in the descriptor, i.e. just the filename and type
		ld (#5D06),a
		ld c,#0A
		call #3D13
		inc c			;C will contain #FF if the file with this name wasn't found
		ld c,#12		;if it was found, mark the file descriptor as deleted
		call nz,#3D13
	pop hl
;	ld hl,Address		;save the file
;	ld de,Length
pop de



	ld c,#0B
	call #3D13



	ret


DiskDriver_Setup:
		push hl

			ld a,&C3
			ld (&5CC2),a
			ld hl,DiskError
			ld (&5CC3),hl

			ld iy,&5C3A	;Do this - or suffer the concequences! - should always be set when firmware interrupts may run!
;			ld iy,&0000
;			ld a,7
;			call Bankswapper_set

			di
			LD C,0		;DOS_RESET
			CALL &3D13 

			LD A,0		;Disk drive initialisation
			LD C,1  ;Init
			CALL &3D13 ;

			LD C,&18 ;read system track
			CALL &3D13 

;			ld a,65
;			rst 16          ; print it.
		pop hl
ret



DiskError:
	ex (sp),hl		;will eventually return if entered upon NO DISC or ABORT, with the error code in C
	push af
	ld a,h
	cp #0D			;the RIA situation is indicated by #0D6B on the stack (both 5.03 and 5.01)
	jr z,DiskError_Abort
	pop af			;otherwise, we continue
	ex (sp),hl		;LIST, CAT, FORMAT and the like display additional messages, not handled here
	ret
DiskError_Abort:
ld sp,&0000 :DiskSafeSP_Plus2
jp DiskDriverLoad_ErrorB


DiskDriver_Load_FromHL:
	ld (DiskLoadBank_Plus1-1),a
	push hl
		ld hl,null
		ld (DiscDestRelocateCall_Plus2-2),hl
	pop hl

DiskDriver_Load:
ld (DiskSafeSP_Plus2-2),sp

ifdef buildZXS_SPUNSAFE
	ld (SPRestore_Plus2-2),sp
	ld sp,StackPointerAlt
endif
	push de
			call DiskDriver_Setup
 

;			LD HL,filename ; to create a descriptor  
  			LD C,&13  
  			CALL &3D13  
    
  			LD C,&0A; find file  
  			CALL &3D13  

 			LD A,C  
  			INC C  
  			JR Z,  DiskDriverLoad_Error  


;			push af
;				ld a,66
;				rst 16          ; print it.
;			pop af

  			LD C,&08  ;Read file descriptor to DOS  variable  area
  			CALL &3D13  
;
;			push af
;				ld a,67
;				rst 16          ; print it.
;  			pop af
  
  			XOR A  
  			LD (&5CF9),A  
  			LD (&5D10),A  
	pop hl


	push bc

		ld bc,(23784) ;Length
;		ld a,(23786);length in sectors

;		ld b,a
;		ld c,0
		call null :DiscDestRelocateCall_Plus2


		ld a,0:DiskLoadBank_Plus1	;dummy
		call Bankswapper_set

	pop bc

	
	;  			LD HL,&9000 ;Destination  
;			ld (&5CE6),hl
;			ld (&5CED),hl
			LD A,3;&FF 

  			LD C,&0E ; read from disk into memory area for an address  
  			CALL &3D13  

;			ld a,68
;			rst 16          ; print it.

			call Bankswapper_Reset

			scf		; carry true if sucess 
DiskDriverLoad_Done:
ifdef buildZXS_SPUNSAFE
			ld sp,&0000 :SPRestore_Plus2
endif
			ret
;  			JR Done
DiskDriverLoad_Error:
		pop hl			; ditch this
DiskDriverLoad_ErrorB:
  			LD BC,&0FFE  
  			LD A,2  
  			OUT (C),A  
			
			or a;ccf 		; carry true if sucess
			;ei
jr DiskDriverLoad_Done

;filename defb  "spectestC"
endif


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Tape driver ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ifdef buildZXS_TAP
;if BuildLang =''
;	Tapefailwanted:	db  "Seeking:"," "+&80
;	Tapefailfound:	db  "Found:"," "+&80
;else
	Tapefailwanted:	db  "SEEKING:",255
	Tapefailfound:	db  "FOUND:",255
;endif
TapeFailMessage:
push bc
push de
		ld hl,&0A15
		ld bc,Tapefailwanted
		call DrawText_LocateAndPrintStringUnlimited

		ld hl,&1615
		call DrawText_LocateSprite
		ld hl,LoadFileName
		ld b,8
		call TapeShowName



		ld hl,&0A16
		ld bc,Tapefailfound
		call DrawText_LocateAndPrintStringUnlimited


		ld hl,&1616
		call DrawText_LocateSprite

		ld hl,TapeHeader+1 
		ld b,8
		call TapeShowName
pop de
pop bc
ret

TapeShowName:

	push hl
	push bc
		ld a,(hl)
		call DrawText_CharSprite
	pop bc
	pop hl
	inc hl
	djnz TapeShowName
ret

DiskDriverLoad_LoadhdrFail:
	call TapeFailMessage

	pop af
	pop hl
	jr DiskDriverLoad_Loadhdr
  ;	jr c,DiskDriverLoad_Loadhdr
TapeEnd:	;NC
	pop de   ;use up de
	or a;ccf 		; carry true if sucess
	di
jr DiskDriverLoad_Done



DiskDriver_Load:

ifdef buildZXS_SPUNSAFE
	ld (SPRestore_Plus2-2),sp
	ld sp,StackPointerAlt
endif


push de
DiskDriverLoad_Loadhdr:
push hl
  	ld ix, TapeHeader
  	ld de, 17  
  	xor a  
  	scf  
  	call 1366  
;	call TapeFailMessage
pop hl
push hl
push af
	  	
	  	; check header  
	  	ld b,9  
	  	ld de,TapeHeader+1  
	;  	ld hl,FileName 

	 	 chckname:  
	  	ld a,(de)  
		  cp (hl)  
	  	jr nz,DiskDriverLoad_LoadhdrFail
	  	inc HL  
	  	inc DE  
	  	djnz chckname
pop af
pop hl 
  	; load block  
	pop ix	;get back de

push hl
	push bc
		push ix
		pop hl

;		ld a,(23786)
		ld bc,(TapeHeader+11)
		call null :DiscDestRelocateCall_Plus2
		push hl
			ld a,0:DiskLoadBank_Plus1	;dummy
			call Bankswapper_set
		pop ix
	pop bc
pop hl


;  	ld ix, &9000 ;destination  
  	ld de,(TapeHeader+11)  		;File Length
  	ld a,&FF  
  	scf  
 	call 1366  

	di
	call Bankswapper_Reset
	scf 		; carry true if sucess
DiskDriverLoad_Done:
ifdef buildZXS_SPUNSAFE
			ld sp,&0000 :SPRestore_Plus2
endif
;Cant save to tape!
DiskDriver_Save:	
ret

TapeHeader:
defs 20

endif 




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +3 disk driver ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ifdef buildZXS_DSK




Plus3_RestoreBuffer:
ret
		di
push ix
push de
push hl
push bc
	;HL=Source Pos
	;DE=Dest Post
	;IXL= Source Bank
	;IXH= Dest Bank
	;BC= ByteCount
		ld ixh,7
		ld ixl,6
		ld de,&DB00		
		ld hl,&C900
		ld bc,&1100
		call Bankswapper_Copy
pop bc
pop hl
pop de
pop ix
ld a,&C9
ld (Plus3_RestoreBuffer),a
ret



DiskDriver_Save:
;bc=bytecount
;hl=filename
;de=address

ifdef buildZXS_SPUNSAFE
	ld (SPRestoreB_Plus2-2),sp
	ld sp,StackPointerAlt
endif
push bc
	push de	
		push hl

			call Plus3_RestoreBuffer
			ld iy,&5C3A	;Do this - or suffer the concequences! - should always be set when firmware interrupts may run!

		        ld   a,7
		        call Bankswapper_set

			;Stop the Disk driver using our ram!
			ld DE,&0000
			ld HL,&0000
			call &013F; (319) DOS SET 1346



		;	CALL &0100    ;DOS_INITIALISE

			ld b,1 ;	B = File number 0...15
			ld c,2;		C = Access mode required
			ld d,1;	D = Create action
			ld e,4;	E = Open action

		pop hl

		call &0106    ;DOS_OPEN
		jr   nc,DiskDriverSave_Error

;	ld b,1;B = File number
;	call &010F ;DOS REF HEAD
;	ld e,(IX+1)
;	ld d,(IX+2) ;get file size




	ld b,1;B = File number
;	ld c,7;C = Page for C000h (49152)...FFFFh (65535)
	;ld DE,16 ; Number of bytes to read (0 means 64K)
	pop hl ;	ld HL, &9000	LoadDiskFileFromHL_Destination_Plus2; Address for bytes to be read
	
	ld c,&0

pop de
	call &0115 ;DOS_WRITE
	ld b,1
	call &0109 ;DOS CLOSE
	call &019C;h ; (412) DD L OFF MOTOR


		scf	; carry true if sucess
DiskDriverSave_Finish:

	push af
;		call Bankswapper_RestoreFirmware
		call Bankswapper_Reset
	pop af
ifdef buildZXS_SPUNSAFE
	ld sp,&0000 :SPRestoreB_Plus2
endif
	ret
DiskDriverSave_Error:

	ld b,1
	call &010C ; (268) DOS ABANDON
	pop hl
	pop de
	or a;ccf 		; carry true if sucess
jr DiskDriverSave_Finish

DiskDriver_Load_FromHL:
	ld (DiskLoadBank_Plus1-1),a
	push hl
		ld hl,null
		ld (DiscDestRelocateCall_Plus2-2),hl
	pop hl


DiskDriver_Load:
ifdef buildZXS_SPUNSAFE
	ld (SPRestore_Plus2-2),sp
	ld sp,StackPointerAlt
endif
	push de	
		push hl

			call Plus3_RestoreBuffer
			ld iy,&5C3A	;Do this - or suffer the concequences! - should always be set when firmware interrupts may run!
		        ld   a,7
		        call Bankswapper_set

			;Stop the Disk driver using our ram!
			ld DE,&0000
			ld HL,&0000
			call &013F; (319) DOS SET 1346



		;	CALL &0100    ;DOS_INITIALISE

			ld b,1 ;	B = File number 0...15
			ld c,5;		C = Access mode required
			ld d,0;	D = Create action
			ld e,1;	E = Open action

		pop hl

		call &0106    ;DOS_OPEN
		jr   nc,DiskDriverLoad_Error

	ld b,1;B = File number
	call &010F ;DOS REF HEAD
	ld e,(IX+1)
	ld d,(IX+2) ;get file size




	ld b,1;B = File number
;	ld c,7;C = Page for C000h (49152)...FFFFh (65535)
	;ld DE,16 ; Number of bytes to read (0 means 64K)
pop hl ;	ld HL, &9000	LoadDiskFileFromHL_Destination_Plus2; Address for bytes to be read
	
	push bc
		push de
		pop bc
		call null :DiscDestRelocateCall_Plus2
	pop bc

;push bc
	ld c,&C0:DiskLoadBank_Plus1
;	call Bankswapper_ZXBank_Plus3Disk
;pop bc


	call &0112 ;DOS_READ
	ld b,1
	call &0109 ;DOS CLOSE
	;call &0142;h (322) DOS FLUSH

	call &019C;h ; (412) DD L OFF MOTOR




        ld   iy,&5C3A   ;restore ERR_NR

		scf	; carry true if sucess
DiskDriverLoad_Finish:

	push af
;		call Bankswapper_RestoreFirmware
		call Bankswapper_Reset
	pop af
ifdef buildZXS_SPUNSAFE
	ld sp,&0000 :SPRestore_Plus2
endif
	ret

DiskDriverLoad_Error:
	ld b,1
	call &010C ; (268) DOS ABANDON





	pop hl 	; get rid of destination
	or a;ccf 		; carry true if sucess
	jr DiskDriverLoad_Finish

endif