home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / c / src_cpp / include / emm.hpp next >
Encoding:
C/C++ Source or Header  |  1993-08-14  |  5.6 KB  |  173 lines

  1. // Copyright 1993, Mark T. Pflaging
  2. // Interface for a class for handling the LIM EMS version 4.0.
  3. // Portions borrowed from:
  4. // "LOTUS(R)/INTEL(R)/MICROSOFT(R)
  5. //  EXPANDED MEMORY SPECIFICATION
  6. //   Version 4.0
  7. //   300275-005
  8. //   October, 1987"
  9.  
  10. #ifndef __EMM_HPP
  11. #define __EMM_HPP
  12.  
  13. #include "standard.hpp"
  14. #include <iostream.h>
  15. #include <stdlib.h>
  16.  
  17. #define EMM_INT                 0x67  // EMM interrupt number
  18. #define GET_PAGE_FRAME          0x41  // EMM get page frame
  19.                       // function number
  20. #define GET_UNALLOC_PAGE_COUNT  0x42  // EMM get unallocated
  21.                       // page count
  22.                       // function number
  23. #define ALLOCATE_PAGES          0x43  // EMM allocate pages
  24.                       // function number
  25. #define MAP_PAGES               0x44  // EMM map pages
  26.                       // function number
  27. #define DEALLOCATE_PAGES        0x45  // EMM deallocate pages
  28.                       // function number
  29. #define DEVICE_NAME_LENGTH      8     // length of a device
  30.                       // name string
  31. #define GET_PHYSICAL_PAGES      0x58  // EMM get mappable physical pages
  32.                       // function number
  33. #define MAP_MULTIPLE_PAGES      0x50  // EMM map multiple pages
  34.                       // function number
  35. #define REALLOCATE_PAGES    0x51  // EMM reallocate pages
  36.                       // function number
  37.  
  38. // Structure used in determining mappable physical pages:
  39. typedef struct _mappable_phys_page {
  40.     unsigned int phys_page_segment;
  41.     unsigned int phys_page_number;
  42. } mappable_phys_page;
  43.  
  44. typedef struct _log_to_phys_map {
  45.     unsigned int log_page_number;
  46.     unsigned int phys_page_number;
  47. } log_to_phys_map, *log_to_phys_map_ptr;
  48.  
  49. class LIMEMS {
  50.     static union REGS input_regs, output_regs;
  51.     static struct SREGS segment_regs;
  52.     static int emm_handle;
  53.     static char * page_frame;
  54.     static int pages_needed;
  55.  
  56.     static Boolean emm_installed();
  57.     static Boolean enough_unallocated_pages (int pages_needed);
  58.     static Boolean allocate_expanded_memory_pages (int pages_needed);
  59.     static Boolean reallocate_expanded_memory_pages (int pages_needed);
  60.     static Boolean map_expanded_memory_pages (int physical_page,
  61.                          int logical_page);
  62.     static Boolean get_page_frame_address();
  63.     static Boolean deallocate_expanded_memory_pages ();
  64.     static Boolean get_mappable_physical_pages();
  65.     static Boolean map_multiple_pages(log_to_phys_map * mapping, int num_maps);
  66.  
  67.     void Create();
  68.     void MemoryExists(int num_pages) {
  69.         //--------------------------------------------------------//
  70.         // Determine if enough expanded memory pages exist for    //
  71.         // application.                                           //
  72.         //--------------------------------------------------------//
  73.         if (!enough_unallocated_pages (num_pages)) {
  74.             cout << "Not enough EMM!" << endl;
  75.             exit(1);
  76.         }
  77.     }
  78.     void Allocate() {
  79.         //--------------------------------------------------------//
  80.         // Allocate expanded memory pages.                        //
  81.         //--------------------------------------------------------//
  82.         if (!allocate_expanded_memory_pages (pages_needed)) {
  83.             cout << "Could not allocate EMM!" << endl;
  84.             exit(1);
  85.         }
  86.     }
  87.     void ReAllocate() {
  88.         //--------------------------------------------------------//
  89.         // Allocate expanded memory pages.                        //
  90.         //--------------------------------------------------------//
  91.         if (!reallocate_expanded_memory_pages (pages_needed)) {
  92.             cout << "Could not REallocate EMM!" << endl;
  93.             exit(1);
  94.         }
  95.     }
  96.     void PageFrame() {
  97.         //--------------------------------------------------------//
  98.         // Get expanded memory page frame address.                //
  99.         //--------------------------------------------------------//
  100.         if (!get_page_frame_address ()) {
  101.             cout << "Could not get page frame address!" << endl;
  102.             exit(1);
  103.         }
  104.     }
  105.  
  106. public:
  107.     LIMEMS() { Create(); }
  108.     LIMEMS(int num_pages) {
  109.         Create();
  110.         Init(num_pages);
  111.     }
  112.     void Init(int num_pages) {
  113.         pages_needed = num_pages;
  114.         MemoryExists(pages_needed);
  115.         Allocate();
  116.         PageFrame();
  117.     }
  118.     ~LIMEMS() {
  119.         //--------------------------------------------------------//
  120.         // Return expanded memory pages before exiting.           //
  121.         //--------------------------------------------------------//
  122.         if (page_frame) {
  123.             if (!deallocate_expanded_memory_pages()) {
  124.                 cout << "Could not deallocate EMM!" << endl;
  125.                 exit(1);
  126.             }
  127.             page_frame = NULL;
  128.         }
  129.     }
  130.     void MoreMemory(int num_pages) {
  131.         pages_needed += num_pages;
  132.         MemoryExists(num_pages);
  133.         ReAllocate();
  134.     }
  135.     Boolean isValidYet() {
  136.         // returns True when Allocate has been called.
  137.         return (page_frame ? True : False);
  138.     }
  139.     void Allocate(int num_pages) {
  140.         if (isValidYet()) MoreMemory(num_pages);
  141.         else Init(num_pages);
  142.     }
  143.     void MappablePhysical() {
  144.         // Determine mappable physical pages.
  145.         if (!get_mappable_physical_pages()) {
  146.             cout << "Could not get mappable physical pages!" << endl;
  147.             exit(1);
  148.         }
  149.     }
  150.     void Map(int physical_page, int logical_page) {
  151.         //--------------------------------------------------------//
  152.         // Map in the required pages.                             //
  153.         //--------------------------------------------------------//
  154.         if (!map_expanded_memory_pages (physical_page, logical_page)) {
  155.             cout << "Could not map EMM!" << endl;
  156.             exit(1);
  157.         }
  158.     }
  159.     char * getPageFrame() { return page_frame; }
  160.     void MapMultiple(log_to_phys_map * mapping, int num_maps) {
  161.         //--------------------------------------------------------//
  162.         // Map in some pages.                                     //
  163.         //--------------------------------------------------------//
  164.         if (!map_multiple_pages(mapping, num_maps)) {
  165.             cout << "Could not multiple map EMM!" << endl;
  166.             exit(1);
  167.         }
  168.     }
  169.     int CurrentlyAllocated() { return pages_needed; }
  170. };
  171.  
  172. #endif
  173.