               UHDD Data Caching For User Drivers
             ======================================

UHDD can be called by a user driver to cache its I-O data.   For an
actual "running" example of this, see the UDVD2 driver, which calls
UHDD to cache CD/DVD data files and their directories.

A user driver must have a "callback" subroutine which does its I-O.
Every I-O request to a user driver will cause a call to UHDD, which
immediately calls-back the user routine for output and for uncached
input.    Data already in cache is simply moved to the user's input
buffer, without a call-back.

A user "callback" subroutine is entered on UHDD's stack.    All but
the SS:SP-registers are undefined.   The "callback" subroutine must
save whatever registers it uses, while handling I-O for the request
originally given to UHDD for caching.     After I-O ends, it clears
the carry flag for "no errors" or sets carry and sets an error code
in the AL-register.    Then, the "callback" subroutine restores its
registers and exits back to UHDD with a "retf" command.

Upon return from a caching call to UHDD, the user driver must check
for a device I-O error and also an "XMS error" (carry set, AH=0FFh)
declared by UHDD.   This denotes a problem moving cached data to or
from XMS memory, thus a memory DIAGNOSTIC may be necessary!

During their initialization, user drivers must examine memory, from
about 00600h to F8000h, to find UHDD and save its "segment" address
(e.g. in variable UHDDSeg, as shown in the sample logic below), for
use in caching calls.   User drivers must search for a segment with
bytes 10-17 (000Ah-0011h) equal to "UHDD$" which is the driver name
permanently assigned to the caching UHDD.    NOTE:  The stand-alone
UHDD, whose name is "UHDD-SA$", must NOT be used for caching calls!

If a user driver finds UHDD, but its UHDD_IOF flag byte has no free
cache-unit numbers (bits 2-7 of UHDD_IOF are all ones), user-driver
caching is not available, and UHDD caching calls may NOT be issued!
This is true if all cache-unit numbers have been reserved for other
drivers.   When UHDD is not found, when its caching is unavailable,
the user-driver variable  UHDDSeg  can stay "zero" and be tested on
an I-O request, as in the sample logic below.

User drive "CacheUnit" number must range from 030h to 05Fh.    UHDD
permanently reserves units 000h to 02Fh for use by disks/diskettes,
and UDVD2 permanently reserves units 028h to 02Fh for CDs and DVDs.
Cache-unit numbers are reserved/released in groups of 8 by setting/
clearing an appropriate bit in UHDD_IOF as follows:

UHDD_IOF bit 2:  Reserves cache-units 030h to 037h.
         bit 3:  Reserves cache-units 038h to 03Fh.
         bit 4:  Reserves cache-units 040h to 047h.
         bit 5:  Reserves cache-units 048h to 04Fh.
         bit 6:  Reserves cache-units 050h to 057h.
         bit 7:  Reserves cache-units 058h to 05Fh.

User drivers (except UDVD2) must set or clear these bits themselves
when a driver is being "initialized" or "unloaded".   Other drivers
may NOT use cache-unit numbers that are already reserved!   Drivers
must avoid altering UHDD_IOF bits 0 or 1, as they are UHDD's "busy"
or "flush cache" flags!

After doing the above initialization procedures, a user driver then
calls UHDD for data caching with the following logic --

UHDD_IOF equ  byte  ptr 013h    ;UHDD flags & cache-unit "bitmap".
UHDD_CBA equ  dword ptr 014h    ;UHDD user "callback" address, seg:
                                ;  offset (not 32-bit!).   Must NOT
                                ;  be set when UHDD is "busy"!
UHDD_TYP equ  byte  ptr 01Bh    ;UHDD device-type code, always 07Eh
                                ;  for any user devices.   Must NOT
                                ;  be set when UHDD is "busy"!
UHDD_ENT equ  000A0h            ;Fixed user-caching "entry" offset.

         ..
         ..
         ..
        mov   cx,UHDDSeg        ;UHDD absent or caching unavailable?
        jcxz  NoUHDD            ;If either, go do normal device I-O.
        mov   es,cx             ;Set saved UHDD driver segment.
        mov   eax,BufferAddr    ;Set EAX = 32-bit buffer address.
                                ;("VDS lock" address, NOT seg:offs!).
        mov   cl,Sectors        ;Set CL = number of 512-byte sectors.
        mov   ch,RdWrCmd        ;Set CH = 00h if read, 02h if write.
        mov   di,LBAHighBits    ;Set DI =  upper 16 LBA addr. bits.
        mov   dx,LBAMidBits     ;Set DX = middle 16 LBA addr. bits.
        mov   si,LBALowBits     ;Set SI =  lower 16 LBA addr. bits.
                                ;(Unused hi-order bits must be 0!).
        movzx bp,CacheUnit      ;Set BP = 8-bit cache unit number.
        pushf                   ;Stack current CPU flags.
        cli                     ;Disable CPU interrupts.
        bts   es:UHDD_IOF,0     ;Is UHDD currently busy?
        jc    BsyErr            ;Yes?  Handle as an error!
        push  cs                ;Set "callback" subroutine seg:offs
        push  offset OurCBRtn   ;  in UHDD "callback" address.
        pop   es:UHDD_CBA
        mov   es:UHDD_TYP,07Eh  ;Set "user device" in UHDD byte 01Fh.
        push  cs                ;Stack UHDD "Int 13h" exit address.
        push  offset Return
        pushf                   ;Stack "dummy" flags and BP-reg.,
        push  bp                ;  loaded when UHDD does its exit.
        xor   bx,bx             ;Ensure UHDD "base register" is zero.
        push  es                ;Do 32-bit "jump" (not call) to UHDD.
        push  UHDD_ENT
        retf
Return: jc    CachErr           ;If carry is set, go handle error!
        ..                      ;No UHDD errors if we arrive here.
        ..
        ..
BsyErr: popf                    ;If busy, reload interrupt state.
        ..                      ;Handle UHDD-busy error as desired.
        ..
        ..
NoUHDD: ..                      ;No UHDD caching -- do normal I-O.
        ..
        ..

If a media-change or serious error for a user drive requires a UHDD
cache "flush", the following logic can be used --

        ..
        ..
        cmp   UHDDSeg,0         ;UHDD absent or caching unavailable?
        je    NoFlsh            ;If either, no cache "flush" needed.
        mov   es,UHDDSeg        ;Set saved UHDD driver segment.
        or    es:UHDD_IOF,002h  ;Post UHDD "flush cache" flag.
                                ;("Flush" occurs before next I-O).
NoFlsh: ..
        ..


*** SPECIAL NOTE ***

The UIDE driver can still be called for data caching, though this is
NOT recommended!   UIDE's /D: switch permits multiple "device names"
to be assigned, which makes "finding" UIDE in memory more difficult!
Also, note that UHDD posts its XMS "handle" number in offset 008h of
the driver, to permit sharing its XMS I-O buffer.   But UIDE may NOT
do this; offset 008h is for CD/DVD "device interrupt" calls in UIDE!

If UIDE must be called for data caching, its variable offsets are --

UIDE_IOF equ  byte  ptr 013h    ;UIDE flags & cache-unit "bitmap".
UIDE_CBA equ  dword ptr 018h    ;UIDE user "callback" address.
UIDE_TYP equ  byte  ptr 01Fh    ;UIDE device-type code.
UIDE_ENT equ  000B0h            ;Fixed user-caching "entry" offset.

