;CD ROM DRIVE DRIVER    ;!FIXIT ALL!
;---------------------------------------------------------------
;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"
;[]===========================================================[]
CD_5x.RESET:
	LD	B,50
.loop:
	PUSH	BC
	LD	A,1
	CALL	CD_TEST
	POP	BC
	RET	NC
	EI 
	HALT 
	DJNZ  .loop
	RET 
;[]===========================================================[]


;[]===========================================================[]
CD_5x.LONG_READ:                          ; !FIXIT не надо сохранять страницу SLOT3 как в других драйвах?
;[]===========================================================[]
;[]===========================================================[]
CD_5x.READ:
        LD	A,1
	JP	CD_READ
;[]===========================================================[]


;[]===========================================================[]
CD.OPEN:
	LD	A,1
	JP	CD_OPEN
;[]===========================================================[]


;[]===========================================================[]
CD.CLOSE:
	LD	A,1
	JP	CD_CLOSE
;[]===========================================================[]

CD_TEST	LD	HL,CMDNOPP
	LD	DE,0
	CALL	AP_COM
	RET 

;
;[]===========================================================[]
CD_5x.DETECT:      ; !FIXIT
        SCF
        RET
;[]===========================================================[]
CD_OPEN:
	LD	HL,CMDOPEN
	LD	DE,0
	CALL	AP_COM
	RET 
;

CD_CLOSE:
	LD	HL,CMDCLOS
	LD	DE,0
	CALL	AP_COM
	RET 

; HL:IX	- SECTOR
;    DE	- ADDRESS
;     B	- SECTOR COUNT
;     A	- DRIVE

CD_READ:
	LD	C,A
	EXX
	LD	C,SLOT3
	IN	A,(C)
	PUSH	AF
	LD	A,SYS_PAGE
	OUT	(C),A
	LD	HL,CMDREAD
	LD	DE,RAM_ATAPI_READ
	LD	BC,PKTSIZE
	LDIR
	EXX

	;!TEST
	 ;LD	A,H
	 ;LD	(RAM_ATAPI_READ+SECREAD+0),A	;R01
	 ;LD	A,L
	 ;LD	(RAM_ATAPI_READ+SECREAD+1),A	;R01
	 LD	A,H
	 LD	H,L
	 LD	L,A
	 LD	(RAM_ATAPI_READ+SECREAD+0),HL
	;
	LD	A,XH
	LD	(RAM_ATAPI_READ+SECREAD+2),A	;R01
	LD	A,XL
	LD	(RAM_ATAPI_READ+SECREAD+3),A	;R01
	LD	A,B
	LD	(RAM_ATAPI_READ+COUNT+1),A	;R01
	POP	AF
	OUT	(SLOT3),A
;R01	LD	HL,CMDREAD
	LD	HL,RAM_ATAPI_READ
	LD	A,C
	CALL	AP_COM
	RET 

; INPUT:  HL - AP paket	(12bytes)
; RETURN: CF - ERROR
;	 01h - RECOVERED ERROR
;	 02h - NOT READY
;	 03h - MEDIUM ERROR
;	 04h - HARDWARE	ERROR
;	 05h - ILLEGAL REQUEST
;	 06h - UNIT ATTETION
;	 07h - DATA PROTECT
;	 0Bh - ABORTED COMMAND
;	 80h - TIME OUT
AP_COM:	AND	#01
	LD	A,IDE.Drive.Master
	JR	Z,.APCOM1
	LD	A,IDE.Drive.Slave
.APCOM1	LD	BC,IDE.Write.DeviceHead
	OUT	(C),A		;SELECT	DRIVE
	EXX 
	LD	DE,#8000
	;LD	BC,IDE.Read.Status
	CALL	CWAITPRT
	EXX 
	JR	NC,.CDREADY
	LD	BC,IDE.Write.Command
	LD	A,#08
	OUT	(C),A

	LD	B,#00
.pause:	DJNZ	.pause

	EXX 
	LD	DE,#8000
	;LD	BC,IDE.Read.Status
	CALL	CWAITPRT
	EXX 
	RET	C
.CDREADY:
	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
	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
	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
	;LD	BC,IDE.Read.Status
	CALL	CWAITPRT
	EXX 
	RET	C
	EXX 
	LD	DE,#0908
	;LD	BC,IDE.Read.Status
	CALL	CWAITPRT
	EXX 
	BIT	IDE.ControlBit.Error,A
	JR	NZ,.CDERROR
	JR	NC,.YEP_DRQ
	LD	A,#80
	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
	;SRL	A
.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
	;LD	BC,IDE.Read.Status
	CALL	CWAITPRT
	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,0
	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
	JP	Z,.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



; E - Second * 10
; PAUSE	LD	HL,#0000
; PAUSE1	DEC	L
; 	JR	NZ,PAUSE1
; 	DEC	H
; 	JR	NZ,PAUSE1
; 	DEC	E
; 	JR	NZ,PAUSE1
; 	RET 


; D - MASK
; E - PATTERN
	//; BC - PORT
CWAITPRT:
	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 

CMDNOPP	DEFB	#00
	DEFB	00,00,00
	DEFB	#00
	DEFB	00,00,00
	DEFB	00,00,00
	DEFB	#00

CMDOPEN	DEFB	#1B
	DEFB	#00,#00,#00
	DEFB	#02
	DEFB	#00,#00,#00,#00,#00,#00,#00

CMDCLOS	DEFB	#1B
	DEFB	#00,#00,#00
	DEFB	#03
	DEFB	#00,#00,#00,#00,#00,#00,#00

CMDREAD	DEFB	#28,#00
SECREAD	EQU	$-CMDREAD
	DEFB	#00,#00,#00,#00
	DEFB	#00
COUNT	EQU	$-CMDREAD
	DEFB	#00,#01
	DEFB	#00,#00,#00

;========================================================


