;[ ] 18/12/2023. добавление/допиливание API CD-ROM (ATAPI)

;=======================================================================
; Вход: A - номер устройства
;	C - Тип
SELECT_DRIVE:
	AND	#0F
	LD	IY,IDE.INIT_TBL_IDE0
	JR	Z,.channel
	DEC	A
	LD	IY,IDE.INIT_TBL_IDE1
	;R02
	 JR	Z,.channel
	 DEC	A
	 LD	IY,IDE.INIT_TBL_IDE2
	 JR	Z,.channel
	 DEC	A
	 LD	IY,IDE.INIT_TBL_IDE3
	;
	JR	Z,.channel
	LD	A,BIOS.Error.BadDrvNumber
	SCF 
	RET 	
	;
.channel:
	EXX 
	 IN	A,(SLOT3)
	 PUSH	AF
	 LD	A,SYS_PAGE
	 OUT	(SLOT3),A
	 ;
	 LD	A,(IY+IDE.HDD_INIT_TABLE.DRV_Flags)
	 AND	1
	 LD	A,IDE.Chanel.Secondary
	 JR	NZ,.device
	 LD	A,IDE.Chanel.Primary
.device:
	 OUT	(IDE.Chanel.Set),A	;R02
	 LD	C,(IY+IDE.HDD_INIT_TABLE.DriveType)
	 LD	B,(IY+IDE.HDD_INIT_TABLE.DRV_Flags)
	 POP	AF
	 OUT	(SLOT3),A				;возврат страницы
	 ;
	 LD	A,C
	EXX
	CP	C
	EXX
	 SCF
	 LD	A,BIOS.Error.BadDrvNumber
	 RET	NZ
	 ;
	 LD	A,B
	 AND	#F0
	 LD	BC,IDE.Write.DeviceHead
	 OUT	(C),A
	EXX 
	RET
	;
; NODRIVE:
; 	LD	A,BIOS.Error.BadDrvNumber
; 	SCF 
; 	RET 
;=======================================================================


;=======================================================================
;Function: Detect Disk
;	   A - Disk
;	   С - Type
;Return:   CF=0 - A=Drive type 
;	   CF=1 - drive not present, A=#02
DRV_DETECT:
	CP	#84	;!HARDCODE max IDE drives (#80,#81,#82,#83)
	CCF
	JR	C,.error

	LD	HL,IDE.INIT_TBL_IDE0.DriveType
	AND	3
	JR	Z,.get_param
	LD	HL,IDE.INIT_TBL_IDE1.DriveType
	DEC	A
	JR	Z,.get_param
	LD	HL,IDE.INIT_TBL_IDE2.DriveType
	DEC	A
	JR	Z,.get_param
	LD	HL,IDE.INIT_TBL_IDE3.DriveType
.get_param:
	IN	A,(SLOT3)
	LD	B,A
	LD	A,SYS_PAGE
	OUT	(SLOT3),A
	
	LD	A,(HL)
	CP	C	; compare Type
	LD	C,SLOT3
	OUT	(C),B
	RET	Z
	SCF
.error:	LD	A,BIOS.Error.BadDrvNumber
	RET