home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / doc / ems / demos / cexample / article.doc < prev    next >
Text File  |  1986-12-13  |  11KB  |  265 lines

  1. ;(s16.6H(s0T&l8D&a16L
  2. /* */
  3.  
  4. /*-------------------------------------------------------------------------*/
  5. /*                                                                         */
  6. /*                             M A I N                                     */
  7. /*                                                                         */
  8. /*                          P R O G R A M                                  */
  9. /*                                                                         */
  10. /*                       F I G U R E   T W O                               */
  11. /*                                                                         */
  12. /*-------------------------------------------------------------------------*/
  13.  
  14. #include <dos.h>
  15. #include <stdio.h>
  16.  
  17. #define EMM_INT 0x67                /*  EMM interrupt number  */
  18. #define GET_PAGE_FRAME_BASE 0x41    /*  EMM func = get page frame base address  */
  19. #define GET_FREE_COUNT 0x42         /*  EMM Func = get unallocated pages count  */
  20. #define ALLOCATE_PAGES 0x43         /*  EMM Func = allocates pages  */
  21. #define MAP_PAGES 0x44              /*  EMM Func = map pages  */
  22. #define DEALLOCATE_PAGES 0x45       /*  EMM Func = deallocate pages  */
  23. #define GET_INT_VECTOR 0x35         /*  DOS func = get interrupt vector  */
  24. #define DEVICE_NAME_LEN 8           /*  Number of chars in device driver name field  */
  25. #define VIDEO_RAM_SIZE 4000         /*  Total bytes in video RAM (char/attr)  */
  26. #define VIDEO_RAM_BASE 0xB0000000   /*  Video RAM start address (MDA) */
  27.  
  28. union REGS input_regs, output_regs; /*  Regs used for calls to EMM and DOS  */
  29. struct SREGS segment_regs;
  30. unsigned int emm_status;            /*  Status returned by EMM  */
  31.  
  32. main ()
  33.  
  34. {
  35. unsigned int i;
  36. long target_time, current_time;
  37. char *video_ram_ptr = {VIDEO_RAM_BASE}; /*  Pointer to video RAM  */
  38. unsigned int emm_handle;                /*  EMM handle  */
  39. char *expanded_memory_ptr;              /*  Pointer to expanded memory  */
  40.  
  41.  
  42. /*  Ensure that the Expanded Memory Manager software is installed on the 
  43.     user's system.  */
  44.  
  45.    detect_emm();
  46.  
  47.  
  48. /*  Get a page of expanded memory.  */
  49.  
  50.    get_expanded_memory_page (&expanded_memory_ptr, &emm_handle);
  51.  
  52.  
  53. /*  Copy the current video RAM contents to expanded memory.  */
  54.  
  55.    memcpy (expanded_memory_ptr, video_ram_ptr, VIDEO_RAM_SIZE);
  56.  
  57.  
  58. /*  Clear the screen to nulls.  */
  59.  
  60.    memset (video_ram_ptr, '\0', VIDEO_RAM_SIZE);
  61.  
  62.  
  63. /*  Delay for 1 second so the user can see the blanked screen.  */
  64.  
  65.    time (¤t_time);
  66.    target_time = current_time + 1;
  67.    while (current_time < target_time)
  68.     {
  69.      time (¤t_time);
  70.     }
  71.  
  72.  
  73. /*  Restore the video RAM contents from expanded memory.  */
  74.  
  75.    memcpy (video_ram_ptr, expanded_memory_ptr, VIDEO_RAM_SIZE);
  76.  
  77.  
  78. /*  Deallocate the expanded memory page  */
  79.  
  80.    release_expanded_memory_page (emm_handle);
  81.  
  82.  
  83.    exit(0);
  84. }
  85.  
  86. /* */
  87.  
  88. /*-------------------------------------------------------------------------*/
  89. /*                                                                         */
  90. /*                       S U B R O U T I N E                               */
  91. /*                                                                         */
  92. /*           G E T _ E X P A N D E D _ M E M O R Y _ P A G E               */
  93. /*                                                                         */
  94. /*                     F I G U R E   T H R E E                             */
  95. /*                                                                         */
  96. /*-------------------------------------------------------------------------*/
  97.  
  98. get_expanded_memory_page (expanded_memory_ptr_ptr, emm_handle_ptr)
  99.  
  100. unsigned int *emm_handle_ptr;     /*  16 bit handle returned by EMM  */
  101. char *(*expanded_memory_ptr_ptr); /*  Pointer to expanded memory page  */
  102.  
  103. {
  104. unsigned int page_frame_base;     /*  Expanded memory page frame base  */
  105. unsigned int physical_page = {0}; /*  Physical page number  */
  106.  
  107.  
  108. /*  Get unallocated pages count.  */
  109.  
  110.    input_regs.h.ah = GET_FREE_COUNT;    /*  EMM function  */
  111.    int86x (EMM_INT, &input_regs, &output_regs, &segment_regs);
  112.    emm_status = output_regs.h.ah;
  113.    check_status(emm_status);            /*  Check for errors  */
  114.    if (output_regs.x.bx < 1)            /*  Check unallocated page count  */
  115.     {
  116.      printf ("\x07Abort: insufficient unallocated expanded memory pages\n");
  117.      exit(0);
  118.     }
  119.  
  120.  
  121. /*  Allocate the specified number of pages.  */
  122.  
  123.    input_regs.h.ah = ALLOCATE_PAGES;     /*  EMM function  */
  124.    input_regs.x.bx = 1;                  /*  Number of pages to allocate  */
  125.    int86x (EMM_INT, &input_regs, &output_regs, &segment_regs);
  126.    emm_status = output_regs.h.ah;
  127.    check_status(emm_status);             /*  Check for errors  */
  128.    *emm_handle_ptr = output_regs.x.dx;   /*  Get EMM handle  */
  129.  
  130.  
  131. /*  Map the logical page into physical page 0.  */
  132.  
  133.    input_regs.h.ah = MAP_PAGES;          /*  EMM function  */
  134.    input_regs.h.al = 0;                  /*  Logical page number  */
  135.    input_regs.x.bx = physical_page;      /*  Physical page number  */
  136.    input_regs.x.dx = *emm_handle_ptr;    /*  EMM handle  */
  137.    int86x (EMM_INT, &input_regs, &output_regs, &segment_regs);
  138.    emm_status = output_regs.h.ah;
  139.    check_status(emm_status);             /*  Check for errors  */
  140.  
  141.  
  142. /*  Determine the page frame address.   */
  143.  
  144.    input_regs.h.ah = GET_PAGE_FRAME_BASE; /*  EMM function  */
  145.    int86x (EMM_INT, &input_regs, &output_regs, &segment_regs);
  146.    emm_status = output_regs.h.ah;
  147.    check_status(emm_status);              /*  Check for errors  */
  148.    *expanded_memory_ptr_ptr = 
  149.      (output_regs.x.bx * 65536)
  150.      + (physical_page * 16 * 1024);       /*  Set the expanded memory ptr  */
  151. }
  152.  
  153. /* */
  154.  
  155. /*-------------------------------------------------------------------------*/
  156. /*                                                                         */
  157. /*                      S U B R O U T I N E                                */
  158. /*                                                                         */
  159. /*       R E L E A S E _ E X P A N D E D _ M E M O R Y _ P A G E           */
  160. /*                                                                         */
  161. /*                     F I G U R E   F O U R                               */
  162. /*                                                                         */
  163. /*-------------------------------------------------------------------------*/
  164.  
  165. release_expanded_memory_page (emm_handle)
  166.  
  167. unsigned int emm_handle;      /*  Handle identifying which page 
  168.                                   set to deallocate  */
  169. {
  170.  
  171. /*  Release the expanded memory pages by deallocating the handle 
  172.     associated with those pages.  */
  173.   
  174.    input_regs.h.ah = DEALLOCATE_PAGES;  /*  EMM function  */
  175.    input_regs.x.dx = emm_handle;        /*  EMM handle passed in DX  */
  176.    int86x (EMM_INT, &input_regs, &output_regs, &segment_regs);
  177.    emm_status = output_regs.h.ah;
  178.    check_status(emm_status);            /*  Check for errors  */
  179.  
  180. /* */
  181.  
  182. /*-------------------------------------------------------------------------*/
  183. /*                                                                         */
  184. /*                      S U B R O U T I N E                                */
  185. /*                                                                         */
  186. /*                    C H E C K _ S T A T U S                              */
  187. /*                                                                         */
  188. /*                     F I G U R E   F I V E                               */
  189. /*                                                                         */
  190. /*-------------------------------------------------------------------------*/
  191.  
  192. check_status (emm_status)
  193. unsigned int emm_status;
  194. {
  195. static char *emm_error_strings[] = {
  196.    "no error",
  197.    "EMM software malfunction",
  198.    "EMM hardware malfunction",
  199.    "RESERVED",
  200.    "Invalid EMM handle",
  201.    "Invalid EMM function code",
  202.    "All EMM handles being used",
  203.    "Save/restore page mapping context error",
  204.    "Not enough expanded memory pages",
  205.    "Not enough unallocated pages",
  206.    "Can not allocate zero pages",
  207.    "Logical page out of range",
  208.    "Physical page out of range",
  209.    "Page mapping hardware state save area full",
  210.    "Page mapping hardware state save area already has handle",
  211.    "No handle associated with the page mapping hardware state save area",
  212.    "Invalid subfunction"
  213.  };
  214.  
  215. /*  IF EMM error, THEN print error message and EXIT  */
  216.  
  217.    if (emm_status != 0)                    /*  IF EMM error...  */
  218.     {
  219.       emm_status -= 0x7F;                  /*  Make error code zero-based  */
  220.       printf ("\x07Abort: EMM error = ");  /*  Issue error prefix  */
  221.       printf ("%s\n", emm_error_strings[emm_status]);
  222.                                            /*  Issue actual error message  */
  223.       exit(0);                             /*  And then exit to DOS  */
  224.     }
  225. }
  226.  
  227. /* */
  228.  
  229. /*-------------------------------------------------------------------------*/
  230. /*                                                                         */
  231. /*                          S U B R O U T I N E                            */
  232. /*                                                                         */
  233. /*                          D E T E C T _ E M M                            */
  234. /*                                                                         */
  235. /*                          F I G U R E   S I X                            */
  236. /*                                                                         */
  237. /*-------------------------------------------------------------------------*/
  238.  
  239. detect_emm ()
  240.  
  241. {
  242. static char EMM_device_name [DEVICE_NAME_LEN] = {"EMMXXXX0"};
  243. char *int_67_device_name_ptr;
  244.  
  245.  
  246. /*  Determine the address of the routine associated with INT 67 hex.  */
  247.  
  248.    input_regs.h.ah = GET_INT_VECTOR;  /*  DOS function  */
  249.    input_regs.h.al = EMM_INT;         /*  EMM interrupt number  */
  250.    intdosx (&input_regs, &output_regs, &segment_regs);
  251.    int_67_device_name_ptr = 
  252.    (segment_regs.es * 65536) + 10;    /*  Create ptr to device name field  */
  253.  
  254.  
  255. /*  Compare the device name with the known EMM device name.  */
  256.  
  257.    if (memcmp (EMM_device_name, int_67_device_name_ptr, DEVICE_NAME_LEN) != 0)
  258.     {
  259.       printf ("\x07Abort: EMM device driver not installed\n");
  260.       exit(0);
  261.     }  
  262.  
  263.