home *** CD-ROM | disk | FTP | other *** search
/ Audio 4.94 - Over 11,000 Files / audio-11000.iso / msdos / sndbords / proaudio / pcmtlsrc / pcmtlsrc.arj / LOADPCM.ARJ / XMS.C < prev    next >
C/C++ Source or Header  |  1992-09-10  |  9KB  |  430 lines

  1. ;    /*\
  2. ;    |*| $Author:   BCRANE  $
  3. ;    |*|
  4. ;    |*| $Date:   29 Jul 1992 16:58:20  $
  5. ;    |*|
  6. ;    |*| $Header:   W:/sccs/sdkapp/xms.c_v   1.0   29 Jul 1992 16:58:20   BCRANE  $
  7. ;    |*|
  8. ;    |*| $Log:   W:/sccs/sdkapp/xms.c_v  $
  9.  * 
  10.  *    Rev 1.0   29 Jul 1992 16:58:20   BCRANE
  11.  * Initial revision.
  12. ;    |*|
  13. ;    |*| $Logfile:   W:/sccs/sdkapp/xms.c_v  $
  14. ;    |*|
  15. ;    |*| $Modtimes$
  16. ;    |*|
  17. ;    |*| $Revision:   1.0  $
  18. ;    |*|
  19. ;    |*| $Workfile:   xms.c  $
  20. ;    |*|
  21. ;    \*/
  22.  
  23.  
  24. /* Copyright (c) 1992.  Media Vision, Inc.  All Rights Reserved. */
  25.  
  26. #include <stdio.h>
  27. #include <dos.h>
  28.  
  29. #define OKAY 0
  30.  
  31. static char *copyright= "Copyright (c) 1992.  Media Vision, Inc.  All Rights Reserved.";
  32. static char *programmer= "Bart Crane";
  33.  
  34. /* XMS Internal Variables */
  35.  
  36. static int (far * xmsdriver)();        /* holds address of XMS driver function */
  37. static unsigned int xmsversion;        /* set during initxms() */
  38. static unsigned int xmsdriverversion;    /* set during initxms() */
  39. static unsigned int hmaexists;        /* set during initxms */
  40. static unsigned char lastxmserror;    /* cleared/set during each function call */
  41.  
  42. /* XMS Functions */
  43.  
  44. /* initxms() - test for existence of XMS driver, and initialize internals */
  45. /* return 1 if XMS driver exists */
  46. /* return 0 if no XMS driver */
  47. /* if driver exists, retrieves address of XMS driver function and */
  48. /* obtains version information. */
  49.  
  50. int initxms()
  51. {
  52.     int status;
  53.  
  54.     lastxmserror= 0;
  55.  
  56.     _asm
  57.         {
  58.         mov ax, 4300h
  59.         int 2Fh
  60.         cmp al, 80h
  61.         mov ax, 0
  62.         jnz nodriver
  63.  
  64.         inc ax
  65.  
  66.         nodriver:
  67.         mov status, ax
  68.         }
  69.  
  70.     if (status)
  71.         {
  72.         unsigned int segxmsdriver;
  73.         unsigned int offxmsdriver;
  74.  
  75.         _asm
  76.             {
  77.             mov ax, 4310h
  78.             int 2Fh
  79.             mov offxmsdriver, bx
  80.             mov bx, es
  81.             mov segxmsdriver, bx
  82.             }
  83.         FP_SEG(xmsdriver)= segxmsdriver;
  84.         FP_OFF(xmsdriver)= offxmsdriver;
  85.  
  86.         _asm
  87.             {
  88.             mov ah, 0
  89.             mov bx, seg xmsdriver
  90.             mov es, bx
  91.             mov bx, offset xmsdriver
  92.             call dword ptr es:[bx]
  93.             mov xmsversion, ax
  94.             mov xmsdriverversion, bx
  95.             mov hmaexists, dx
  96.             }
  97.         }
  98.  
  99.     return(status);
  100. }
  101.  
  102. /* getxmsversion() - get version of XMS and driver */
  103. /* returns BCD values in DX:AX */
  104. /* AX= XMS version */
  105. /* DX= driver internal revision */
  106.  
  107. unsigned long getxmsversion()
  108. {
  109.     return(xmsversion| ((unsigned long) xmsdriverversion<< 16));
  110. }
  111.  
  112. /* havehma() - determine if HMA exists */
  113. /* return 1 if HMA exists */
  114. /* return 0 if no HMA */
  115.  
  116. int havehma()
  117. {
  118.     return(hmaexists);
  119. }
  120.  
  121.  
  122. /* getxmserror() - format a string explaining last error */
  123. /* references static variable which is set during each xms function */
  124. /* message guaranteed to be no longer than 80 characters including null */
  125. /* lastxmserror is a byte value from 0x80 on up, so the 0x80 is masked off */
  126. /* making the byte value range from 0x00 on up, which is then used to */
  127. /* index into the xmserror[] array. */
  128. /* return nonzero (the index value) if an error happened. */
  129. /* return 0 if no error occurred. */
  130.  
  131. static char *unknownerror= "Unknown error";
  132. #define MAXXMSERRORS 0x34+ 1
  133. static char *xmserror[]=
  134. {
  135.     "Function completed successfully",    /* 0 */
  136.     "XMS function not implemented",        /* 0x80 */
  137.     "VDISK device detected",                 /* 0x81 */
  138.     "A20 error occurred",                     /* 0x82 */
  139.     NULL,                             /* 0x83 */
  140.     NULL,                             /* 0x84 */
  141.     NULL,                             /* 0x85 */
  142.     NULL,                             /* 0x86 */
  143.     NULL,                             /* 0x87 */
  144.     NULL,                             /* 0x88 */
  145.     NULL,                             /* 0x89 */
  146.     NULL,                             /* 0x8A */
  147.     NULL,                             /* 0x8B */
  148.     NULL,                             /* 0x8C */
  149.     NULL,                             /* 0x8D */
  150.     NULL,                             /* 0x8E */
  151.     NULL,                             /* 0x8F */
  152.     "HMA does not exist",                    /* 0x90 */
  153.     "HMA already in use",                /* 0x91 */
  154.     "Requested HMA size less than /HMAMIN",    /* 0x92 */
  155.     "HMA not allocated",                    /* 0x93 */
  156.     "A20 line still enabled",            /* 0x94 */
  157.     NULL,                            /* 0x95 */
  158.     NULL,                            /* 0x96 */
  159.     NULL,                            /* 0x97 */
  160.     NULL,                            /* 0x98 */
  161.     NULL,                            /* 0x99 */
  162.     NULL,                            /* 0x9A */
  163.     NULL,                            /* 0x9B */
  164.     NULL,                            /* 0x9C */
  165.     NULL,                            /* 0x9D */
  166.     NULL,                            /* 0x9E */
  167.     NULL,                            /* 0x9F */
  168.     "All XMS memory already allocated",                            /* 0xA0 */
  169.     "All XMS handles already in use",        /* 0xA1 */
  170.     "Invalid XMS handle",                 /* 0xA2 */
  171.     "Invalid source XMS handle",        /* 0xA3 */
  172.     "Invalid source offset",             /* 0xA4 */
  173.     "Invalid target XMS handle",         /* 0xA5 */
  174.     "Invalid target offset",             /* 0xA6 */
  175.     "Invalid length",                     /* 0xA7 */
  176.     "Invalid overlap",                     /* 0xA8 */
  177.     "Parity error occurred",            /* 0xA9 */
  178.     "Block not locked",                    /* 0xAA */
  179.     "Block locked",                        /* 0xAB */
  180.     "Block lock count overflowed",    /* 0xAC */
  181.     "Lock failed",                            /* 0xAD */
  182.     NULL,                            /* 0xAE */
  183.     NULL,                            /* 0xAF */
  184.     "Smaller UMB is available",        /* 0xB0 */
  185.     "No UMB available",                    /* 0xB1 */
  186.     "UMB segment number invalid",        /* 0xB2 */
  187.     NULL                            /* 0xB3 MAXXMSERRORS */
  188. };
  189.  
  190. int getxmserror(char *buf)
  191. {
  192.     unsigned int msg;
  193.  
  194.     if (lastxmserror& 0x80)
  195.         {
  196.         msg= (unsigned int) (lastxmserror& 0x7F)+ 1;
  197.  
  198.         if (msg > MAXXMSERRORS)
  199.             msg= MAXXMSERRORS;
  200.  
  201.         if (buf)
  202.             if (xmserror[msg])
  203.                 sprintf(buf, "%s", xmserror[msg]);
  204.             else
  205.                 sprintf(buf, "%s", unknownerror);
  206.         return(msg);
  207.         }
  208.  
  209.     return(OKAY);
  210. }
  211.  
  212. unsigned long getxmsmemstat()
  213. {
  214.     unsigned int largestxmsblock;
  215.     unsigned int totalxmsavailable;
  216.  
  217.     lastxmserror= 0;
  218.  
  219.     _asm
  220.         {
  221.         mov ah, 8
  222.         mov bx, seg xmsdriver
  223.         mov es, bx
  224.         mov bx, offset xmsdriver
  225.         call dword ptr es:[bx]
  226.         test bl, 80h
  227.         jz gotsome
  228.         xor bh, bh
  229.         mov lastxmserror, bx
  230.         xor ax, ax
  231.         mov dx, ax
  232.         jmp short memcheckdone
  233.  
  234.         gotsome:
  235.         mov largestxmsblock, ax
  236.         mov totalxmsavailable, dx
  237.  
  238.         memcheckdone:
  239.         }
  240.  
  241.     return(largestxmsblock| ((unsigned long) totalxmsavailable<< 16));
  242. }
  243.  
  244. unsigned int allocxms(unsigned int kbsize)
  245. {
  246.     unsigned int xmshandle= 0;
  247.  
  248.     lastxmserror= 0;
  249.  
  250.     _asm
  251.         {
  252.         mov ah, 9
  253.         mov dx, kbsize
  254.         or dx, dx
  255.         jz whybother
  256.  
  257.         mov bx, seg xmsdriver
  258.         mov es, bx
  259.         mov bx, offset xmsdriver
  260.         call dword ptr es:[bx]
  261.         or ax, ax
  262.         jnz gotmem
  263.         xor bh, bh
  264.         mov lastxmserror, bx
  265.         jmp short whybother
  266.  
  267.         gotmem:
  268.         mov xmshandle, dx
  269.  
  270.         whybother:
  271.         }
  272.  
  273.     return(xmshandle);
  274. }
  275.  
  276. unsigned int reallocxms(unsigned int xmshandle, unsigned int kbsize)
  277. {
  278.     unsigned int retval= 0;
  279.  
  280.     lastxmserror= 0;
  281.  
  282.     _asm
  283.         {
  284.         mov ah, 0Fh
  285.         mov bx, kbsize
  286.         or bx, bx
  287.         jz whybother
  288.         mov dx, xmshandle
  289.         or dx, dx
  290.         jz whybother
  291.  
  292.         mov bx, seg xmsdriver
  293.         mov es, bx
  294.         mov bx, offset xmsdriver
  295.         call dword ptr es:[bx]
  296.         or ax, ax
  297.         jnz regotmem
  298.         xor bh, bh
  299.         mov lastxmserror, bx
  300.         jmp short whybother
  301.  
  302.         regotmem:
  303.         mov retval, 1
  304.  
  305.         whybother:
  306.         }
  307.  
  308.     return(retval);
  309. }
  310.  
  311. unsigned long getembstatus(unsigned int xmshandle)
  312. {
  313.     unsigned int emblockcount= 0;
  314.     unsigned int embkbsize= 0;
  315.  
  316.     lastxmserror= 0;
  317.  
  318.     _asm
  319.         {
  320.         mov ah, 0Eh
  321.         mov dx, xmshandle
  322.         mov bx, seg xmsdriver
  323.         mov es, bx
  324.         mov bx, offset xmsdriver
  325.         call dword ptr es:[bx]
  326.         or ax, ax
  327.         jnz gotembstatus
  328.         xor bh, bh
  329.         mov lastxmserror, bx
  330.         jmp short embstatusdone
  331.  
  332.         gotembstatus:
  333.         mov al, bh
  334.         cbw
  335.         mov emblockcount, ax
  336.         mov embkbsize, dx
  337.  
  338.         embstatusdone:
  339.         }
  340.  
  341.     return(emblockcount| ((unsigned long) embkbsize<< 16));
  342.  
  343. }
  344.  
  345. unsigned long getxmsfreehandles(unsigned int xmshandle)
  346. {
  347.     unsigned int embfreehandles= 0;
  348.  
  349.     lastxmserror= 0;
  350.  
  351.     _asm
  352.         {
  353.         mov ah, 0Eh
  354.         mov dx, xmshandle
  355.         mov bx, seg xmsdriver
  356.         mov es, bx
  357.         mov bx, offset xmsdriver
  358.         call dword ptr es:[bx]
  359.         or ax, ax
  360.         jnz gotembhandles
  361.         xor bh, bh
  362.         mov lastxmserror, bx
  363.         jmp short embhandlesdone
  364.  
  365.         gotembhandles:
  366.         mov al, bl
  367.         cbw
  368.         mov embfreehandles, ax
  369.  
  370.         embhandlesdone:
  371.         }
  372.  
  373.     return(embfreehandles);
  374. }
  375.  
  376. xmsmovemem(struct xmsmoves far *emm)
  377. {
  378.     unsigned int segemm= FP_SEG(emm);
  379.     unsigned int offemm= FP_OFF(emm);
  380.  
  381.     lastxmserror= 0;
  382.  
  383.     _asm
  384.         {
  385.         push si
  386.         push ds
  387.         mov si, segemm
  388.         mov ds, si
  389.         mov si, offemm
  390.         mov ah, 0Bh
  391.         mov bx, seg xmsdriver
  392.         mov es, bx
  393.         mov bx, offset xmsdriver
  394.         call dword ptr es:[bx]
  395.         or ax, ax
  396.         jnz mademove
  397.         xor bh, bh
  398.         mov lastxmserror, bx
  399.  
  400.         mademove:
  401.         pop ds
  402.         pop si
  403.         }
  404.  
  405.     return(!lastxmserror);
  406. }
  407.  
  408. freexms(unsigned int xmshandle)
  409. {
  410.     lastxmserror= 0;
  411.  
  412.     _asm
  413.         {
  414.         mov ah, 0Ah
  415.         mov dx, xmshandle
  416.         mov bx, seg xmsdriver
  417.         mov es, bx
  418.         mov bx, offset xmsdriver
  419.         call dword ptr es:[bx]
  420.         or ax, ax
  421.         jnz freedmem
  422.         xor bh, bx
  423.         mov lastxmserror, bx
  424.         freedmem:
  425.         }
  426.  
  427.     return(!lastxmserror);
  428. }
  429.  
  430.