ems.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* $Id$
  2. * MegaZeux
  3. *
  4. * Copyright (C) 1996 Greg Janson
  5. * Copyright (C) 1998 Matthew D. Williams - dbwilli@scsn.net
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. // EMS.CPP- Simple functions to initialize EMS and allocate and deallocate
  22. // EMS memory.
  23. #include "ems.h"
  24. #include <io.h>
  25. #include <dos.h>
  26. #include <fcntl.h>
  27. #include <_null.h>
  28. int avail_pages_EMS=0;//EMS pages available to our application
  29. char far *page_frame_EMS=NULL;//Location in memory of our page frame
  30. char setup=0;
  31. //Returns -1 for error (no EMS software/hardware/etc)
  32. //Otherwise, sets up total_pages_EMS, page_frame_EMS, and
  33. //avail_pages_EMS.
  34. char setup_EMS() {
  35. int fh;
  36. union REGS rg;
  37. if((fh=open("EMMXXXX0",O_RDONLY,&fh))==-1) return -1;
  38. rg.h.ah=0x44;
  39. rg.h.al=0x00;
  40. rg.x.bx=fh;
  41. int86(0x21,&rg,&rg);
  42. close(fh);
  43. if(rg.x.cflag) return -1;
  44. if(!(rg.x.dx & 0x80)) return -1;
  45. rg.h.ah=0x40;
  46. int86(0x67,&rg,&rg);
  47. if(rg.h.ah!=0) return -1;
  48. //EMS available! Get # pages
  49. rg.h.ah=0x42;
  50. int86(0x67,&rg,&rg);
  51. if(rg.x.cflag) return -1;
  52. avail_pages_EMS=rg.x.bx;
  53. //Get page frame
  54. rg.h.ah=0x41;
  55. int86(0x67,&rg,&rg);
  56. if(rg.h.ah!=0) return -1;
  57. page_frame_EMS=(char far *)MK_FP(rg.x.bx,0);
  58. setup=1;
  59. return 0;
  60. }
  61. //Returns the number of free pages of EMS available. -1 for error.
  62. int free_mem_EMS() {
  63. union REGS rg;
  64. if(!setup) return 0;
  65. rg.h.ah=0x42;
  66. int86(0x67,&rg,&rg);
  67. if(rg.x.cflag) return -1;
  68. return rg.x.bx;
  69. }
  70. //Allocates n pages of EMS. Returns a handle or 0 for out of memory
  71. //or EMS not installed. (So this can be used even if EMS is not found)
  72. unsigned int alloc_EMS(int n) {
  73. union REGS rg;
  74. if(!setup) return 0;
  75. if(n>avail_pages_EMS) return 0;
  76. rg.h.ah=0x43;
  77. rg.x.bx=n;
  78. int86(0x67,&rg,&rg);
  79. if(rg.h.ah) return 0;
  80. return rg.x.dx;
  81. }
  82. //Frees a previously allocated EMS handle. -1 for error.
  83. void free_EMS(unsigned int h) {
  84. union REGS rg;
  85. int i;
  86. if(!setup) return;
  87. if(h==0) return;//It's OK to deallocate a "NULL" handle!
  88. for(i=0;i<5;i++) {
  89. rg.h.ah=0x45;
  90. rg.x.dx=h;
  91. int86(0x67,&rg,&rg);
  92. if(rg.h.ah==0) break;
  93. }
  94. }
  95. //Maps logical page #log_page of EMS handle #h to
  96. //physical page #phys_page. phys_page must range from 0 to 3. Used to not
  97. //map if already mapped, to save time, but this had to be changed since the
  98. //music code now uses EMS and we can't be sure of the current mapped state.
  99. void map_page_EMS(unsigned int h,int phys_page,int log_page) {
  100. union REGS rg;
  101. if(!setup) return;
  102. rg.h.ah=0x44;
  103. rg.h.al=phys_page;
  104. rg.x.bx=log_page;
  105. rg.x.dx=h;
  106. int86(0x67,&rg,&rg);
  107. }
  108. //Saves EMS map state- h is a handle to save under (must be alloc'd)
  109. void save_map_state_EMS(unsigned int h) {
  110. union REGS rg;
  111. if(!setup) return;
  112. rg.h.ah=0x47;
  113. rg.x.dx=h;
  114. int86(0x67,&rg,&rg);
  115. }
  116. //Restores EMS map state- h is a handle to save under (must be alloc'd)
  117. void restore_map_state_EMS(unsigned int h) {
  118. union REGS rg;
  119. if(!setup) return;
  120. rg.h.ah=0x48;
  121. rg.x.dx=h;
  122. int86(0x67,&rg,&rg);
  123. }