*--------------------------------------------------------------------------*
*                                                                          *
*   Following code is Copyright 1989 by Atari Corporation                  *
*                                                                          *
*   Taken from Rainbow TOS Release Notes - August 7, 1989,                 *
*   pp. 52-55.                                                             *
*                                                                          *
*--------------------------------------------------------------------------*
*                                                                          *
*   mediach: cause media-change on a logical device.                       *
*                                                                          *
*   USAGE:                                                                 *
*       errcode = mediach(devno);   /* returns 1 for error */              *
*                                                                          *
*                                                                          *
*   This procedure causes a media change by installing a new               *
*   handler for the mediach, rwabs, and getbpb vectors; for device         *
*   devno, the mediach handler returns "definitely changed." and           *
*   the rwabs handler returns E_CHNG, until the new getbpb handler         *
*   is called.  The new getbpb handler un-installs the new                 *
*   handlers.                                                              *
*                                                                          *
*   After installing the new handlers, this procedure performs a           *
*   disk operation (e.g. open a file) which makes GEMDOS check             *
*   the media-change status of the drive: this will trigger the            *
*   new rwabs, mediach and getbpb handlers to do their things.             *
*                                                                          *
*   RETURNS:  0 for no error, 1 for error (GEMDOS never did a              *
*           getbpb call; should never happen.)                             *
*                                                                          *
*--------------------------------------------------------------------------*

            .globl      _mediach
            
_mediach:
            move.l      d7,saved7       ; be good and save register for C
            move.w      4(sp),d0
            move.w      d0,mydev
            add.b       #'A',d0
            move.b      d0,fspec        ; set drive spec for search first
            
loop:
            clr.l       -(sp)           ; get super mode, leave old ssp
            move.w      #$20,-(sp)      ; and "super" function code on stack
            trap        #1
            addq        #6,sp
            move.l      d0,-(sp)
            move.w      #$20,-(sp)
            
            move.l      $472,oldgetbpb
            move.l      $47e,oldmediach
            move.l      $476,oldrwabs
            
            move.l      #newgetbpb,$472
            move.l      #newmediach,$47e
            move.l      #newrwabs,$476
            
            ; Fopen a file on that drive
            
            move.w      #0,-(sp)
            move.l      #fspec,-(sp)
            move.w      #$3d,-(sp)
            trap        #1
            addq        #8,sp
            
            ; Fclose the handle we just got
            
            tst.l       d0
            bmi.s       noclose    

            move.w      d0,-(sp)
            move.w      #$3e,-(sp)
            trap        #1
            addq        #4,sp
            
noclose:
            moveq       #0,d7
            cmp.l       #newgetbpb,$472 ; still installed
            bne.s       done            ; nope
            
            moveq       #1,d7           ; yup! remove & return TRUE
            move.l      oldgetbpb,$472
            move.l      oldmediach,$47e
            move.l      oldrwabs,$476
            
done:       trap        #1              ; go back to user mode (use stuff
            addq        #$6,sp          ; left on stack above)
            
            move.l      d7,d0
            move.l      saved7,d7       ; restore d7
            rts


*--------------------------------------------------------------------------*
*                                                                          *
* new getbpb: if it's our device, uninstall vectors;                       *
*           in any case, call the old getbpb vector (to really             *
*           get it)                                                        *
*                                                                          *
*--------------------------------------------------------------------------*

newgetbpb:
            move.w      mydev,d0
            cmp.w       4(sp),d0
            bne.s       dooldg
            move.l      oldgetbpb,$472  ; it's mine: un-install new vectors
            move.l      oldmediach,$47e
            move.l      oldrwabs,$476
dooldg:     move.l      oldgetbpb,a0    ; continue here whether mine or not:
                                        ; call old
            jmp         (a0)
  
*--------------------------------------------------------------------------*
*                                                                          *
* new mediach:  if it's our device, return 2; else call old.               *
*                                                                          *
*--------------------------------------------------------------------------*
                      
newmediach:
            move.w      mydev,d0
            cmp.w       4(sp),d0
            bne.s       dooldm
            moveq.l     #2,d0   ; it's mine: return 2 (definitely changed)
            rts
dooldm:
            move.l      oldmediach,a0   ; not mine: call old vector.
            jmp         (a0)
            
*--------------------------------------------------------------------------*
*                                                                          *
* new rwabs:  return E_CHG (-14) if it's my device                         *
*                                                                          *
*--------------------------------------------------------------------------*
             
newrwabs:
            move.w      mydev,d0
            cmp.w       $e(sp),d0
            bne.s       dooldr
            moveq.l     #-14,d0
            rts
            
dooldr:     move.l      oldmediach,a0
            jmp         (a0)
            
.data
fspec:      dc.b        "X:\\X",0   ; file to look for (doesn't matter)

.bss
saved7:     ds.l        1
mydev:      ds.w        1
oldgetbpb:  ds.l        1
oldmediach: ds.l        1
oldrwabs:   ds.l        1

*--------------------------------------------------------------------------*
*                                                                          *
*           end of mediach                                                 *
*                                                                          *
*--------------------------------------------------------------------------*

