is_MSCDEX proc far
; cx contains 1st cd-rom drive number
  	mov ax,1500h
  	mov bx,0
  	int 2fh
	ret
is_MSCDEX endp

is_cdrom proc far
; cx = drive number 0=A:
	mov  ax, 150bh
	xor  bx, bx
	; mov  cx, cddrive
	int  2fh
	cmp  bx, 0adadh
        je   is_cdrom_yes
	xor  ax, ax
is_cdrom_yes:
	ret
is_cdrom endp

settrans proc far
   push bp
   mov bx,offset cs:cdCtlBlkCode
   mov cs:[cdtransoff], bx
   mov bp,ds
   mov cs:[cdtransseg], bp
   pop bp
   ret
settrans endp

CALL_MSCDEX proc far 
  pushf
  push ds
  pop es
  mov ax,1510h            ; send device driver request service
  mov bx,offset cs:cdhdrlen
  int 2fh
  popf
  ret
CALL_MSCDEX endp

CDROM_TRAY proc far
  call is_MSCDEX
  cmp bx,0
  je cdtNoMSCDEX

  mov cs:[cdcmdcode],03h       ; IOCTL input
  mov cs:[cdCtlBlkCode],STATUS
  call SetTrans
  Call CALL_MSCDEX
  mov cs:[cdcmdcode],0ch       ; IOCTL output
  test cs:[cdDeviceParms],1    ; check if bit 0 is set
  jz cdtOpenIt               ; Nope, means CD-ROM is closed so call open procedure..
  mov cs:[cdCtlBlkCode],CLOSE  ; else close it
  jmp cdtInitialize

cdtOpenIt:
  mov cs:[cdCtlBlkCode],OPEN

cdtInitialize:
  call SetTrans
  call CALL_MSCDEX
  jmp exit

cdtNoMSCDEX:
	_print cs:NoMSCDEXmsg, 1
cdtexit:
  ret
CDROM_TRAY endp


CDROM_STRUCT:
      cdhdrlen     db 20   ; our device header structure starts here
      cdsubunit    db 0
      cdcmdcode    db ?
      cddevicestat dw ?
      cdres        db 8 dup(?)
      cdcdmode     db ?
      cdtransoff   dw ?
      cdtransseg   dw ?

      OPEN         equ 0
      CLOSE        equ 5
      STATUS       equ 6

      cdCtlBlkCode   db ?
      cdDeviceParms  dw ?
                     dw ?

      NoMSCDEXmsg  db '[W#5CD]',0ffh,0
