home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / c / src_cpp / src / emm.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-14  |  6.9 KB  |  205 lines

  1. // Using LIM EMS 4.0 in Borland 3.1.
  2. // Copyright 1993, Mark T. Pflaging
  3. // Implementation for a class for handling the LIM EMS version 4.0.
  4. // Portions borrowed from:
  5. // "LOTUS(R)/INTEL(R)/MICROSOFT(R)
  6. //  EXPANDED MEMORY SPECIFICATION
  7. //   Version 4.0
  8. //   300275-005
  9. //   October, 1987"
  10. #include <dos.h>
  11. #include <iomanip.h>
  12. #include <mem.h>
  13.  
  14. #include "emm.hpp"
  15.  
  16. union REGS LIMEMS::input_regs, LIMEMS::output_regs;
  17. struct SREGS LIMEMS::segment_regs;
  18.  
  19. int LIMEMS::emm_handle;
  20. int LIMEMS::pages_needed = 0;
  21. char * LIMEMS::page_frame = NULL;
  22.  
  23. //------------------------------------------------------------//
  24. // Function which determines whether EMM device driver        //
  25. // is installed.                                              //
  26. //------------------------------------------------------------//
  27. Boolean LIMEMS::emm_installed()
  28. {
  29.     char *EMM_device_name = "EMMXXXX0";
  30.     char *int_67_device_name_ptr;
  31.  
  32.     //--------------------------------------------------------//
  33.     // AH = DOS get interrupt vector function.                //
  34.     //--------------------------------------------------------//
  35.     input_regs.h.ah = 0x35;
  36.  
  37.     //--------------------------------------------------------//
  38.     // AL = EMM interrupt vector number.                      //
  39.     //--------------------------------------------------------//
  40.     input_regs.h.al = EMM_INT;
  41.     intdosx (&input_regs, &output_regs, &segment_regs);
  42.  
  43.     //--------------------------------------------------------//
  44.     // Upon return ES:0Ah points to location where            //
  45.     // device name should be.                                 //
  46.     //--------------------------------------------------------//
  47.     int_67_device_name_ptr = (char *)MK_FP(segment_regs.es, 0x0A);
  48.  
  49.     //--------------------------------------------------------//
  50.     // Compare memory with EMM device name.                   //
  51.     //--------------------------------------------------------//
  52.     if (memcmp (EMM_device_name, int_67_device_name_ptr,
  53.                      DEVICE_NAME_LENGTH) == 0)
  54.         return (True);
  55.     else
  56.         return (False);
  57. }
  58.  
  59. //--------------------------------------------------------//
  60. // Determine if EMM is installed.                         //
  61. //--------------------------------------------------------//
  62. void LIMEMS::Create() {
  63.     // This class may be instantiated more than once,
  64.     // but it can only be 'Init'ed once!
  65.     static Boolean checked = False;
  66.     if (!checked)
  67.         if (!emm_installed()) {
  68.             cout << "EMM not installed!" << endl;
  69.             exit(1);
  70.         }
  71.     checked = True;
  72. }
  73.  
  74. //------------------------------------------------------------//
  75. // Function which determines if there are enough unallocated  //
  76. // expanded memory pages for the application.                 //
  77. //------------------------------------------------------------//
  78. Boolean LIMEMS::enough_unallocated_pages (int pages_needed)
  79. {
  80.     input_regs.h.ah = GET_UNALLOC_PAGE_COUNT;
  81.     int86 (EMM_INT, &input_regs, &output_regs);
  82.     if (output_regs.h.ah != 0 || pages_needed > output_regs.x.bx)
  83.         return (False);
  84.     else
  85.         return (True);
  86. }
  87.  
  88. //------------------------------------------------------------//
  89. // Function which allocates expanded memory pages and passes  //
  90. // back to the main EMM handle.                               //
  91. //------------------------------------------------------------//
  92. Boolean LIMEMS::allocate_expanded_memory_pages (int pages_needed)
  93. {
  94.     input_regs.h.ah = ALLOCATE_PAGES;
  95.     input_regs.x.bx = pages_needed;
  96.     int86 (EMM_INT, &input_regs, &output_regs);
  97.     if (output_regs.h.ah == 0) {
  98.         emm_handle = output_regs.x.dx;
  99.         return (True);
  100.     }
  101.     else
  102.         return (False);
  103. }
  104.  
  105. //------------------------------------------------------------//
  106. // Routine to map a logical page to a physical page.          //
  107. //------------------------------------------------------------//
  108. Boolean LIMEMS::map_expanded_memory_pages (int physical_page, int logical_page)
  109. {
  110.     input_regs.h.ah = MAP_PAGES;
  111.     input_regs.h.al = physical_page;
  112.     input_regs.x.bx = logical_page;
  113.     input_regs.x.dx = emm_handle;
  114.     int86 (EMM_INT, &input_regs, &output_regs);
  115.     if (output_regs.h.ah == 0)
  116.         return (True);
  117.     else
  118.         return (False);
  119. }
  120.  
  121. //------------------------------------------------------------//
  122. // Routine which gets the page frame base address from EMM.   //
  123. //------------------------------------------------------------//
  124. Boolean LIMEMS::get_page_frame_address ()
  125. {
  126.     input_regs.h.ah = GET_PAGE_FRAME;
  127.     int86 (EMM_INT, &input_regs, &output_regs);
  128.     if (output_regs.h.ah != 0)       /* check EMM status */
  129.         return (False);
  130.     else
  131.         page_frame = (char *)MK_FP(output_regs.x.bx, 0);
  132.     return (True);
  133. }
  134.  
  135. //------------------------------------------------------------//
  136. // Routine to release all expanded memory pages allocated     //
  137. // by an EMM handle.                                          //
  138. //------------------------------------------------------------//
  139. Boolean LIMEMS::deallocate_expanded_memory_pages ()
  140. {
  141.     input_regs.h.ah = DEALLOCATE_PAGES;
  142.     input_regs.x.dx = emm_handle;
  143.     int86 (EMM_INT, &input_regs, &output_regs);
  144.     if (output_regs.h.ah == 0)
  145.         return (True);
  146.     else
  147.         return (False);
  148. }
  149.  
  150. Boolean LIMEMS::get_mappable_physical_pages()
  151. {
  152.     input_regs.h.ah = GET_PHYSICAL_PAGES;
  153.     input_regs.h.al = 0x01;
  154.     int86 (EMM_INT, &input_regs, &output_regs);
  155.     if (output_regs.h.ah != 0)       /* check EMM status */
  156.         return (False);
  157.     else {
  158.         int num_pages = output_regs.x.cx;
  159.         mappable_phys_page * map = new mappable_phys_page[num_pages];
  160.         input_regs.h.ah = GET_PHYSICAL_PAGES;
  161.         input_regs.h.al = 0x00;
  162.         segment_regs.es = FP_SEG(map);
  163.         input_regs.x.di = FP_OFF(map);
  164.         int86x (EMM_INT, &input_regs, &output_regs, &segment_regs);
  165.         if (output_regs.h.ah != 0)       /* check EMM status */
  166.             return (False);
  167.         for (int i = 0; i < num_pages; i ++) {
  168.             cout << "phys_page_segment=0x" << hex << map[i].phys_page_segment << dec;
  169.             cout << "\tphys_page_number=" << map[i].phys_page_number << endl;
  170.         }
  171.         return (True);
  172.     }
  173. }
  174.  
  175. Boolean LIMEMS::map_multiple_pages(log_to_phys_map * mapping, int num_maps)
  176. {
  177.     input_regs.h.ah = MAP_MULTIPLE_PAGES;
  178.     input_regs.h.al = 0x00;
  179.     input_regs.x.dx = emm_handle;
  180.     input_regs.x.cx = num_maps;
  181.     segment_regs.ds = FP_SEG(mapping);
  182.     input_regs.x.si = FP_OFF(mapping);
  183.     int86x (EMM_INT, &input_regs, &output_regs, &segment_regs);
  184.     if (output_regs.h.ah != 0)       // check EMM status
  185.         return (False);
  186.     return (True);
  187. }
  188.  
  189. //------------------------------------------------------------//
  190. // Function which allocates expanded memory pages and passes  //
  191. // back to the main EMM handle.                               //
  192. //------------------------------------------------------------//
  193. Boolean LIMEMS::reallocate_expanded_memory_pages (int pages_needed)
  194. {
  195.     input_regs.h.ah = REALLOCATE_PAGES;
  196.     input_regs.x.dx = emm_handle;
  197.     input_regs.x.bx = pages_needed;
  198.     int86 (EMM_INT, &input_regs, &output_regs);
  199.     if (output_regs.h.ah == 0)
  200.         return (True);
  201.     else
  202.         return (False);
  203. }
  204.  
  205.