home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / ems / emsarray / ems.pas next >
Encoding:
Pascal/Delphi Source File  |  1992-10-28  |  9.9 KB  |  244 lines

  1. Unit EMS;
  2.  
  3. {-------------------------------------------------------------------}
  4. { This program allows you to define any array in the Expand Memory  }
  5. { and access the array directly. You may freely distribute it or    }
  6. { use it for any purpose, provided this notice is attached with it. }
  7. { I would be very happy if you intend to use it in your program.    }
  8. { Please let me know if you like it.                                }
  9. {                                                                   }
  10. { InterNet:                                                         }
  11. {          jialong@neuro.informatik.uni-ulm.de                      }
  12. {                                                                   }
  13. { Home address:                                                     }
  14. {          Hof Str. 22                                              }
  15. {          7906 Blaustein                                           }
  16. {          Germany                                                  }
  17. {                                                                   }
  18. {-------------------------------------------------------------------}
  19.  
  20.  
  21. Interface
  22.  
  23. Const
  24.  
  25.   PAGESIZE                  = 16384;  {bytes}
  26.  
  27. Type
  28.  
  29.   {--------------------------------------------------------------}
  30.   {   You can modify BaseType to meet your need                  }
  31.   {--------------------------------------------------------------}
  32.   ByteArrayType =  Array [1..(65535 div Sizeof(Byte))] of Byte;
  33.   IntArrayType  =  Array [1..(65535 div Sizeof(Integer))] of Integer;
  34.   RealArrayType =  Array [1..(65535 div Sizeof(Real))] of Real;
  35.   {--------------------------------------------------------------}
  36.   {   The following define a basic EMS object                    }
  37.   {--------------------------------------------------------------}
  38.   EMSArray=Object
  39.      Ems_Handle,
  40.      Page_Frame_Address,                   
  41.      Pages_Needed  : Word;
  42.      NormalPtr     :  Pointer;
  43.      ByteArry      : ^ByteArrayType;
  44.      IntArry       : ^IntArrayType;
  45.      RealArry      : ^RealArrayType;
  46.   {--------------------------------------------------------------}
  47.   {   The followings are all methods you can call                }
  48.   {--------------------------------------------------------------}
  49.      Function  Ems_Installed              : Boolean;
  50.      Function  Pages_Available            : Word;
  51.      Procedure Alloc(Page_Requested: Word);
  52.      Procedure MapIn(Logical_Page_No, Physical_Page_No: Word);
  53.      Procedure Release;
  54.   End;
  55.  
  56. Var
  57.   {--------------------------------------------------------------}
  58.   {   Global varible, You can check it after calling a function  }
  59.   {--------------------------------------------------------------}
  60.  
  61.    EmsError              : Integer;
  62.  
  63.  
  64.  
  65. Implementation
  66.  
  67.  
  68. Uses  Dos;
  69.  
  70. Const
  71.   EMM_INT                   = $67;
  72.   DOS_Int                   = $21;
  73.   GET_PAGE_FRAME            = $41;
  74.   GET_UNALLOCATED_PAGE_COUNT= $42;
  75.   ALLOCATE_PAGES            = $43;
  76.   MAP_PAGES                 = $44;
  77.   DEALLOCATE_PAGES          = $45;
  78.   GET_VERSION               = $46;
  79.   STATUS_OK                 = 0;
  80.  
  81.   {--------------------------------------------------------------}
  82.   {   The function Emm_Installed checks to see if the Expanded   }
  83.   {   Memory Manager (EMM) is loaded in memory. It does this by  }
  84.   {   looking for the string 'EMMXXXX0', which should be located }
  85.   {   at 10 bytes from the beginning of the code segment pointed }
  86.   {   to by the EMM interrupt, 67h.                              }
  87.   {--------------------------------------------------------------}
  88.   Function EMSArray.Ems_installed : Boolean;
  89.     Var
  90.       Emm_Device_Name       : string[8];
  91.       Int_67_Device_Name    : string[8];
  92.       Position              : integer;
  93.       Regs                  : registers;
  94.     Begin
  95.         Int_67_Device_Name := '';
  96.         Emm_Device_Name    := 'EMMXXXX0';
  97.         with Regs do
  98.          Begin
  99.           {------------------------------------------------------}
  100.           {   Get the code segment pointed to by interrupt 67h,  }
  101.           {   the EMM interrupt by using DOS function 35h.       }
  102.           {   (get interrupt vector)                             }
  103.           {------------------------------------------------------}
  104.           AH := $35;
  105.           AL := EMM_INT;
  106.           Intr (DOS_int,Regs);
  107.           For Position := 0 to 7 do
  108.             Int_67_Device_Name :=
  109.                     Int_67_Device_Name+Chr (mem[ES:Position+$0A]);
  110.           {------------------------------------------------------}
  111.           {   If the string is the EMM manager signature,        }
  112.           {   'EMMXXXX0', then EMM is installed and ready for    }
  113.           {   use.  If not, then EMM is not present.             }
  114.           {------------------------------------------------------}
  115.           If Int_67_Device_Name = Emm_Device_Name
  116.             then Ems_Installed := True
  117.           Else   Ems_Installed := False
  118.         end; { with Regs do }
  119.     End;
  120.   {--------------------------------------------------------------}
  121.   { This function returns the pages number that are available    }
  122.   { for use. After calling Intr, BX contains currrently          }
  123.   { unallocated pages, DS contains total pages in the system     }
  124.   {--------------------------------------------------------------}
  125.   Function  EMSArray.Pages_Available : Word;
  126.  
  127.     Var
  128.       Regs: Registers;
  129.  
  130.       Begin
  131.        With Regs Do
  132.         Begin
  133.           AH := Get_Unallocated_Page_Count;
  134.           intr (EMM_INT, Regs);
  135.           Pages_Available := BX;
  136.           EmsError := AH;
  137.         End;
  138.       End;
  139.  
  140.   {--------------------------------------------------------------}
  141.   {--------------------------------------------------------------}
  142.   Procedure EMSArray.Alloc(Page_Requested: Word);
  143.     Var
  144.       Regs: Registers;
  145.  
  146.       Begin
  147.       With Regs do
  148.         Begin
  149.           {------------------------------------------------------}
  150.           {   Get the page frame segment address from EMM.       }
  151.           {      AH = get page frame segment function            }
  152.           {------------------------------------------------------}
  153.           AH := Get_Page_Frame;
  154.           intr (EMM_INT,Regs);
  155.           {------------------------------------------------------}
  156.           {      BX = page frame segment address                 }
  157.           {      AH = status                                     }
  158.           {------------------------------------------------------}
  159.           Page_Frame_Address := BX;
  160.           EmsError := AH;
  161.  
  162.           {------------------------------------------------------}
  163.           {   Allocate the specified number of pages from EMM.   }
  164.           {      AH = allocate pages function.                   }
  165.           {      BX = number of pages to allocate.               }
  166.           {------------------------------------------------------}
  167.           Pages_Needed:= Page_Requested;
  168.           AH := Allocate_Pages;
  169.           BX := Pages_Needed;
  170.           intr (EMM_INT,Regs);
  171.           Ems_Handle  := DX;
  172.           EmsError    := AH;
  173.         End; {With Regs Do}
  174.  
  175.       End;
  176.  
  177.   {--------------------------------------------------------------}
  178.   {   This function maps a logical page allocated by the         }
  179.   {   Allocate_Expanded_Memory_Pages function into one of the    }
  180.   {   four physical pages.                                       }
  181.   {--------------------------------------------------------------}
  182.   Procedure EMSArray.MapIn(Logical_Page_No, Physical_Page_No : Word);
  183.     Var
  184.        Regs: Registers;
  185.  
  186.     Begin
  187.  
  188.       With Regs do
  189.         Begin
  190.           {------------------------------------------------------}
  191.           {   Map a logical page at physical_page_No             }
  192.           {      AH = map page function                          }
  193.           {      DX = handle                                     }
  194.           {      BX = logical page number                        }
  195.           {      AL = physical page number                       }
  196.           {------------------------------------------------------}
  197.           AH := Map_Pages;
  198.           DX := Ems_Handle;
  199.           BX := Logical_Page_No;
  200.           AL := Physical_Page_No;
  201.           Intr (EMM_INT, Regs);
  202.           EmsError := AH;
  203.         end; { with Regs do }
  204.           {------------------------------------------------------}
  205.           {   Making array pointer point to this physical pages  }
  206.           {     For convience, Different pointer points to same  }
  207.           {     address. User can add his own pointer type and   }
  208.           {     add a line here.                                 }
  209.           {------------------------------------------------------}
  210.         NormalPtr:= Ptr(Page_Frame_Address, Physical_Page_No * PAGESIZE);
  211.         ByteArry := Ptr(Page_Frame_Address, Physical_Page_No * PAGESIZE);
  212.         IntArry  := Ptr(Page_Frame_Address, Physical_Page_No * PAGESIZE);
  213.         RealArry := Ptr(Page_Frame_Address, Physical_Page_No * PAGESIZE);
  214.         { ****  add other pointer here *****}
  215.  
  216.     end; { Function Map_Expanded_Memory_Pages }
  217.  
  218.  
  219.  
  220.  
  221.   {--------------------------------------------------------------}
  222.   {   This function releases the EMM memory pages allocated to   }
  223.   {   us, back to the EMM memory pool.                           }
  224.   {--------------------------------------------------------------}
  225.   Procedure EMSArray.Release;
  226.     Var
  227.       Regs: Registers;
  228.  
  229.     Begin
  230.       with Regs do
  231.         Begin
  232.           {------------------------------------------------------}
  233.           {   Deallocate the pages allocated to an EMM handle.   }
  234.           {      AH = deallocate pages function                  }
  235.           {      DX = EMM handle                                 }
  236.           {------------------------------------------------------}
  237.           AH := DEALLOCATE_PAGES;
  238.           DX := Ems_Handle;
  239.           Intr (EMM_INT,Regs);
  240.           EmsError := AH;
  241.         end; { with Regs do }
  242.     end;  { Function Deallocate_Expanded_Memory_Pages }
  243. End.
  244.