;[x] 18/12/2023. ¤®¡ ¢«¥­¨¥/¤®¯¨«¨¢ ­¨¥ API CD-ROM (ATAPI)
;ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ
;CD ROM DRIVE DRIVER
;---------------------------------------------------------------
;Rev	Date	   Name	Description
;---------------------------------------------------------------
;	02-08-2001 DNS	Initial this module
;---------------------------------------------------------------
;========================================================
SIZESEC		EQU	#0800
PKTSIZE		EQU	12
RAM_ATAPI_PK 	EQU	SYS_PAGE.SHARED_BUFFER_32b
RAM_ATAPI_READ	EQU	SYS_PAGE.SHARED_BUFFER_32b+16

	ASSERT ((PKTSIZE % 2) = 0), "PKTSIZE must be an even number"

;[]================================================================[#51]
CD_5x_RESET:
	LD	C,IDE.Device.CDROM
	CALL	SELECT_DRIVE
	RET	C
	LD	B,50
.loop:
	PUSH	BC
	CALL	CD_TEST
	POP	BC
	RET	NC
	EI 
	HALT 
	DJNZ  .loop
	RET 
;[]================================================================[#51]


;[]================================================================[#55]
;Function: Read	Sectors
;	A - Disk
;   HL:IX - Sector
;      DE - Address
;	B - Sector counter
;Return:
;   HL:IX - Sector + Sector counter
;      DE - Address + (Sector counter *	Size sector)
;READ SECTOR(S)
CD_5x_READ:
	EX	AF,AF'
	IN	A,(SLOT3)
	EX	AF,AF'
;[]================================================================[#52]
;Function: Long	Read Sectors
;	A - Disk
;   HL:IX - Sector
;      DE - Address
;	B - Sector counter
;	A'- Memory Page Number
;Return:
;   HL:IX - Sector + Sector counter
;      DE - Address + (Sector counter *	Size sector)
;LONG READ SECTOR(S)
CD_5x_LONG_READ:
	LD	C,IDE.Device.CDROM
	CALL	SELECT_DRIVE
	RET	C
	;
	EXX
	LD	C,SLOT3
	IN	A,(C)
	PUSH	AF
	LD	A,SYS_PAGE
	OUT	(C),A
	LD	HL,ATAPI_CMD_PACKET.READ
	LD	DE,RAM_ATAPI_READ
	LD	BC,PKTSIZE
	LDIR
	EXX

	LD	A,H
	LD	H,L
	LD	L,A
	LD	(RAM_ATAPI_READ + ATAPI_PACKET.SECTOR+0),HL
	LD	A,XH
	LD	(RAM_ATAPI_READ + ATAPI_PACKET.SECTOR+2),A	;R01
	LD	A,XL
	LD	(RAM_ATAPI_READ + ATAPI_PACKET.SECTOR+3),A	;R01
	LD	A,B
	LD	(RAM_ATAPI_READ + ATAPI_PACKET.COUNTER+1),A	;R01
	;
	EX	AF,AF'
	OUT	(SLOT3),A
	;
	; POP	AF
	; OUT	(SLOT3),A
	LD	HL,RAM_ATAPI_READ
	CALL	AP_COM
	;
	POP	BC
	LD	C,SLOT3
	OUT	(C),B
	;
	RET 
;[]===========================================================[#52, #55]


;[]================================================================[#57]
;Function: Detect Disk
;	   A - Disk
;Return:   CF=0 - A=Drive type 
;	   CF=1 - drive not present, A=#02
CD_5x_DETECT:
	LD	C,IDE.Device.CDROM
	AND	%1011'1111
	JP	DRV_DETECT
;[]================================================================[#57]


;[]================================================================[#5E]
;Function: Extended
;	   A - Disk
;	   B - SubFunction
;Return:   
;	   
CD_5x_Extended:
	LD	C,IDE.Device.CDROM
	CALL	SELECT_DRIVE
	RET	C
	;
	LD	A,B
	CP	2
	JR	C,TRAY_FN
	; ...
	; ...
	LD	A,#AA               ;!HARDCODE error code
	SCF
	RET
;[]================================================================[#5E]


;----------------------------------------------------------------------;
TRAY_FN:
	LD	DE,0	;!FIXIT ­ã¦­® «¨?
	LD	HL,ATAPI_CMD_PACKET.CLOSE
	DEC	A
	JR	Z,AP_COM
	LD	HL,ATAPI_CMD_PACKET.OPEN
	JR	AP_COM
;----------------------------------------------------------------------;


;----------------------------------------------------------------------;
; INPUT:  HL - AP paket	(12bytes)
; RETURN: CF - ERROR
;	!TODO CD ERRORS to INCLUDES
;	 #01 - RECOVERED ERROR
;	 #02 - NOT READY
;	 #03 - MEDIUM ERROR
;	 #04 - HARDWARE	ERROR
;	 #05 - ILLEGAL REQUEST
;	 #06 - UNIT ATTETION
;	 #07 - DATA PROTECT
;	 #0B - ABORTED COMMAND
;	 #80 - TIME OUT
AP_COM:	EXX 
	 LD	DE,#8000
	 CALL	CD_WAITPRT
	EXX 
	JR	NC,.READY
	LD	BC,IDE.Write.Command
	LD	A,IDE.ATAPI.Reset
	OUT	(C),A

	LD	B,#80
.pause:	DJNZ	.pause

	EXX 
	 LD	DE,#8000
	 CALL	CD_WAITPRT
	EXX 
	RET	C
.READY:
	LD	C,SLOT3
	IN	B,(C)

	PUSH	DE
	PUSH	BC

	LD	A,SYS_PAGE
	OUT	(C),A
	LD	A,B
	LD	DE,RAM_ATAPI_PK	;!FIXIT ¬®¦¥â ­  áâ¥ª¥ ¢ë¤¥«ïâì ¬¥áâ®?
	LD	BC,PKTSIZE
	LDIR

	POP	BC
	POP	DE
	OUT	(C),B

	XOR	A
	EXX
	 ;OUT	(C),A
	 ;XOR	A
	 LD	BC,IDE.Write.Features
	 OUT	(C),A
	 LD	DE,SIZESEC	;SIZE BLOCK	;!HARDCODE ¤®áâ ¢ âì ¨§ ¯¥à¥¬¥­­®© ª ª®©-­¨¡ã¤ì
	 LD	BC,IDE.Write.CylinderLow
	 OUT	(C),E
	 LD	BC,IDE.Write.CylinderHigh
	 OUT	(C),D
	 LD	BC,IDE.Write.Command
	 LD	A,IDE.ATAPI.Packet
	 OUT	(C),A
	 LD	DE,#8000
	 CALL	CD_WAITPRT
	EXX 
	RET	C
	EXX 
	 LD	DE,#0908
	 CALL	CD_WAITPRT
	EXX 
	BIT	IDE.ControlBit.Error,A
	JR	NZ,.CDERROR
	JR	NC,.YEP_DRQ
	LD	A,#80			; TIME OUT ;!HARDCODE
	RET 
.YEP_DRQ:
	LD	C,SLOT3
	IN	B,(C)
	PUSH	BC
	LD	A,SYS_PAGE
	OUT	(SLOT3),A
	LD	HL,RAM_ATAPI_PK
	LD	BC,IDE.Write.Data
	LD	A,PKTSIZE/2
.OUTPKT:
        OUTI 
	OUTI 
	DEC	A
	JR	NZ,.OUTPKT

	POP	BC
	OUT	(C),B

	LD	B,#80
.pause2: DJNZ	.pause2

.AP_LOOP:
	EXX 
	 LD	DE,#8000
	 CALL	CD_WAITPRT
	EXX 
	RET	C
	LD	BC,IDE.Read.Status
	IN	A,(C)
	BIT	IDE.ControlBit.Error,A
	JR	Z,.NO_ERR
.CDERROR:
	LD	BC,IDE.Read.Error	;ERROR
	IN	A,(C)
	RRCA 
	RRCA 
	RRCA 
	RRCA 
	AND	#0F
	SCF 
	RET 
.NO_ERR:
	BIT	IDE.ControlBit.DataRequest,A
	LD	A,BIOS.Error.NoErrors
	RET	Z	;NO DATA REQUEST
	EX	DE,HL
	LD	BC,IDE.Read.CylinderLow
	IN	E,(C)
	LD	BC,IDE.Read.CylinderHigh
	IN	D,(C)	;TRANSFER BLOCK	SIZE
	LD	A,D
	OR	E
	RET	Z	;BLOCK = 0
	LD	BC,IDE.Read.Counter
	IN	A,(C)
	AND	#02
	;CP	#02
	JR	NZ,.FROM_CD
;.TO_CD:
	LD	BC,IDE.Read.Data
.WR_T_CD:
	OUTI 
	OUTI 
	DEC	DE
	DEC	DE
	LD	A,D
	OR	E
	JR	NZ,.WR_T_CD
	EX	DE,HL
	JR	.AP_LOOP
.FROM_CD:
	LD	A,H
	OR	L
	JR	Z,.NULL
	LD	BC,IDE.Read.Data
.RD_F_CD:
	INI 
	INI 
	DEC	DE
	DEC	DE
	LD	A,D
	OR	E
	JR	NZ,.RD_F_CD
	EX	DE,HL
	JR	.AP_LOOP
.NULL:	LD	BC,IDE.Read.Data
.RD_N_CD:
	IN	A,(C)
	DEC	B
	IN	A,(C)
	DEC	B
	DEC	DE
	DEC	DE
	LD	A,D
	OR	E
	JR	NZ,.RD_N_CD
	; DE = 0
	JR	.AP_LOOP
;----------------------------------------------------------------------;


;----------------------------------------------------------------------;
; D - MASK
; E - PATTERN
CD_WAITPRT:
	LD	BC,IDE.Read.Status
	LD	A,100
	LD	HL,#0000
.CWAITPX:
	EX	AF,AF'
.CWAITP0:
	IN	A,(C)
	CP	#FF
	JR	Z,.CWAITP1
	AND	D
	CP	E
	JR	NZ,.CWAITP2
	AND	A
	RET 
.CWAITP2:
	DEC	L
	JR	NZ,.CWAITP0
	DEC	H
	JR	NZ,.CWAITP0
	EX	AF,AF'
	DEC	A
	JR	NZ,.CWAITPX
	EX	AF,AF'
.CWAITP1:
	SCF 
	RET 
;----------------------------------------------------------------------;


;----------------------------------------------------------------------;
CD_TEST	LD	HL,ATAPI_CMD_PACKET.NOP
	LD	DE,0	;!FIXIT ­ã¦­® «¨?
	JP	AP_COM
;----------------------------------------------------------------------;


////////////////////////////////////////////////////////////////////////
ATAPI_CMD_PACKET:
.NOP:	DUP	12
	 DB	#00
	EDUP
;
.OPEN:	DB	#1B
	DB	#00,#00,#00
	DB	#02
	DB	#00,#00,#00,#00,#00,#00,#00
;
.CLOSE:	DB	#1B
	DB	#00,#00,#00
	DB	#03
	DB	#00,#00,#00,#00,#00,#00,#00
;
.READ:	DB	#28,#00
	DB	#00,#00,#00,#00	; sector dword
	DB	#00
	DB	#00,#01,#00,#00	; counter dword
	DB	#00
;
ATAPI_PACKET:
.SECTOR		EQU	2
.COUNTER	EQU	7
////////////////////////////////////////////////////////////////////////
;
; E - Second * 10
; PAUSE	LD	HL,#0000
; PAUSE1	DEC	L
; 	JR	NZ,PAUSE1
; 	DEC	H
; 	JR	NZ,PAUSE1
; 	DEC	E
; 	JR	NZ,PAUSE1
; 	RET 
;