home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_11 / 2n11027a < prev    next >
Text File  |  1991-09-13  |  5KB  |  171 lines

  1. /* ----------------------------------------------------
  2.  *  LISTING 6
  3.  *
  4.  *  Filename:           emm.c
  5.  *  Summary:            Expanded memory interface
  6.  *                      functions for BCACHE manager
  7.  *  Author:             T.W. Nelson
  8.  *  Compile options:    
  9.  *  Version:            1.00
  10.  *  Date:               09-Sep-1991
  11.  *  Notes:
  12.  *
  13.  *  Source code Copyright (c) 1991 T.W. Nelson. May be
  14.  *  used only for non-commercial purposes with
  15.  *  appropriate acknowledgement of copyright.
  16.  * ------------------------------------------------- */
  17.  
  18. #include "bcache.h"
  19.  
  20. #pragma inline
  21.  
  22. static char EMM_dev[] = "EMMXXXX0";
  23. static int EMM_flag = 1;    //run-time emm usage switch
  24.                             //... default is ENABLED
  25.  
  26. #define handle  user1       //rename user data areas
  27. #define npages  user2       //in BCACHE object
  28.  
  29. void emm_setflag( int flag )
  30. {
  31.    /* Turn expmem usage on/off.  */
  32.  
  33.     EMM_flag = flag;
  34. }
  35.  
  36. void emm_map_pages( BCACHE *cp )
  37. {
  38.    /* Save current EMM page-mapping context, then
  39.     * map our cache pages into the physical page frame.
  40.     */
  41.  
  42.     size_t emm_handle, pages;
  43.     asm cmp EMM_flag,0      //expmem in use?
  44.     asm je map_x            //no, bypass
  45.     emm_handle = cp->handle;
  46.     pages = cp->npages;
  47.     asm mov ah,47h          //EMS save page map
  48.     asm mov dx,emm_handle
  49.     asm int 67h
  50.     asm mov cx,pages        //set loop counter
  51.     asm xor bx,bx           //start at logpage 0
  52. map_in:
  53.     asm mov ah,44h          //EMS map expmemory page
  54.     asm mov al,bl           //set physpage
  55.     asm int 67h
  56.     asm inc bx              //to next logpage
  57.     asm loop map_in
  58. map_x:
  59.     return;
  60. }
  61.  
  62. void emm_unmap_pages( BCACHE *cp )
  63. {
  64.    /* Restore page mapping context ... */
  65.     size_t emm_handle;
  66.  
  67.     asm cmp EMM_flag,0      //expmem in use?
  68.     asm je unmap_x          //no, bypass
  69.     emm_handle = cp->handle;
  70.     asm mov ah,48h          //EMS restore page map
  71.     asm mov dx,emm_handle
  72.     asm int 67h
  73. unmap_x:
  74.     return;
  75. }
  76.  
  77. int emm_allocate( BCACHE *cp, int flag )
  78. {
  79.    /* Allocate expanded memory for the block cache.
  80.     * Called by bc_open() if using expmemory.
  81.     */
  82.  
  83.     chdr_t *page_frame;
  84.     size_t pages_needed, bytes_mem, emm_handle;
  85.  
  86.     asm cmp flag,ALLOCATE   //allocate?
  87.     asm jne emmalloc_4      //no, deallocate
  88.     bytes_mem = (cp->bsiz+BHDR_SIZE) * cp->bmax;
  89.     asm mov ax,bytes_mem    //set dividend loword
  90.     asm mov dx,0            //clear dividend hiword
  91.     asm mov bx,16384        //16k page size divisor
  92.     asm div bx              //ax == number pages
  93.     asm or ax,ax            //0 quotient?
  94.     asm jz emmalloc_1       //yes, adjust
  95.     asm or dx,dx            //any remainder?
  96.     asm jz emmalloc_2       //no, bypass
  97. emmalloc_1:
  98.     asm inc ax              //adjust for remainder
  99. emmalloc_2:
  100.     asm cmp ax,4            //check page request
  101.     asm ja emmalloc_3       //too many pages
  102.     asm mov pages_needed,ax
  103.     asm mov ah,41h          //EMS get page frame seg
  104.     asm int 67h
  105.     asm mov word ptr page_frame[2],bx
  106.     asm mov word ptr page_frame[0],0
  107.     asm mov bx,pages_needed
  108.     asm mov ah,43h          //EMS allocate pages
  109.     asm int 67h
  110.     asm or ah,ah            //allocated?
  111.     asm jnz emmalloc_3      //no, quit
  112.     asm mov bytes_mem,dx    //save emm handle
  113.     cp->handle = bytes_mem;
  114.     cp->npages = pages_needed;
  115.     cp->head = page_frame;
  116.  
  117.     goto emmalloc_5;
  118.  
  119. emmalloc_4:                 //deallocate memory
  120.     emm_handle = cp->handle;
  121.     asm mov ah,45h          //EMS release handle
  122.     asm mov dx,emm_handle
  123.     asm int 67h
  124.  
  125. emmalloc_5:
  126.     return BC_NOERROR;
  127.  
  128. emmalloc_3:
  129.     return BC_NOMEMORY;
  130. }
  131.  
  132. int emm_check( void )
  133. {
  134.    /* Test for presence of functioning EMM.
  135.     * If EMM not available, 'EMM_flag' is reset to
  136.     * OFF (== 0). Function returns 0 if EMM is
  137.     * available and functioning, -1 if not or
  138.     * 'EMM_flag' is OFF.
  139.     */
  140.  
  141.     asm cmp EMM_flag,0      //use expmem?
  142.     asm je emm_error        //no, return error
  143.     asm push ds
  144.     asm mov dx,offset EMM_dev
  145.     asm mov ax,seg EMM_dev
  146.     asm mov ds,ax
  147.     asm mov ax,3d00h        //DOS open file, read only
  148.     asm int 21h
  149.     asm pop ds
  150.     asm jc emm_error        //EM driver not present
  151.     asm mov bx,ax           //bx <- open device handle
  152.     asm mov ax,4407h        //DOS IOCTL device status
  153.     asm int 21h
  154.     asm jc emm_error        //IOCTL failure
  155.     asm or al,al            //device ready?
  156.     asm jz emm_error        //al == 0 if not
  157.     asm mov ah,3eh          //DOS close handle
  158.     asm int 21h
  159.     asm mov ah,40h          //check EMM status
  160.     asm int 67h
  161.     asm or ah,ah            //op status OK?
  162.     asm jnz emm_error       //no, exit
  163.     return 0;               //return EMM OK
  164.  
  165. emm_error:
  166.     emm_setflag(0);         //disable EMM usage
  167.     return -1;
  168. }
  169.  
  170. /* ---- End of File -------------------------------- */
  171.