dos_v2.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include <sys/segments.h>
  16. #include <go32.h>
  17. #include <unistd.h>
  18. #include <sys/nearptr.h>
  19. #include <dos.h>
  20. #include <string.h>
  21. #include <dpmi.h>
  22. // #include <osfcn.h>
  23. #include <bios.h>
  24. #include "dosisms.h"
  25. _go32_dpmi_registers hmm;
  26. // globals
  27. regs_t regs;
  28. void (*dos_error_func)(char *msg, ...);
  29. static unsigned conventional_memory = -1;
  30. __dpmi_regs callback_regs;
  31. void map_in_conventional_memory(void)
  32. {
  33. if (conventional_memory == -1)
  34. {
  35. if (__djgpp_nearptr_enable())
  36. {
  37. conventional_memory = __djgpp_conventional_base;
  38. }
  39. }
  40. }
  41. unsigned int ptr2real(void *ptr)
  42. {
  43. map_in_conventional_memory();
  44. return (int)ptr - conventional_memory;
  45. }
  46. void *real2ptr(unsigned int real)
  47. {
  48. map_in_conventional_memory();
  49. return (void *) (real + conventional_memory);
  50. }
  51. void *far2ptr(unsigned int farptr)
  52. {
  53. return real2ptr(((farptr & ~0xffff) >>12) + (farptr&0xffff));
  54. }
  55. unsigned int ptr2far(void *ptr)
  56. {
  57. return ((ptr2real(ptr)&~0xf) << 12) + (ptr2real(ptr) & 0xf);
  58. }
  59. int dos_inportb(int port)
  60. {
  61. return inportb(port);
  62. }
  63. int dos_inportw(int port)
  64. {
  65. return inportw(port);
  66. }
  67. void dos_outportb(int port, int val)
  68. {
  69. outportb(port, val);
  70. }
  71. void dos_outportw(int port, int val)
  72. {
  73. outportw(port, val);
  74. }
  75. void dos_irqenable(void)
  76. {
  77. enable();
  78. }
  79. void dos_irqdisable(void)
  80. {
  81. disable();
  82. }
  83. //
  84. // Returns 0 on success
  85. //
  86. int dos_int86(int vec)
  87. {
  88. int rc;
  89. regs.x.ss = regs.x.sp = 0;
  90. rc = _go32_dpmi_simulate_int(vec, (_go32_dpmi_registers *) &regs);
  91. return rc || (regs.x.flags & 1);
  92. }
  93. int dos_int386(int vec, regs_t *inregs, regs_t *outregs)
  94. {
  95. int rc;
  96. memcpy(outregs, inregs, sizeof(regs_t));
  97. outregs->x.ss = outregs->x.sp = 0;
  98. rc = _go32_dpmi_simulate_int(vec, (_go32_dpmi_registers *) outregs);
  99. return rc || (outregs->x.flags & 1);
  100. }
  101. //
  102. // Because of a quirk in dj's alloc-dos-memory wrapper, you need to keep
  103. // the seginfo structure around for when you free the mem.
  104. //
  105. static _go32_dpmi_seginfo seginfo[10];
  106. void *dos_getmemory(int size)
  107. {
  108. int rc;
  109. _go32_dpmi_seginfo info;
  110. static int firsttime=1;
  111. int i;
  112. if (firsttime)
  113. {
  114. memset(seginfo, 0, sizeof(seginfo));
  115. firsttime = 0;
  116. }
  117. info.size = (size+15) / 16;
  118. rc = _go32_dpmi_allocate_dos_memory(&info);
  119. if (rc)
  120. return 0;
  121. for (i=0;i<10;i++)
  122. if (!seginfo[i].rm_segment) break;
  123. seginfo[i] = info;
  124. return real2ptr((int) info.rm_segment << 4);
  125. }
  126. void dos_freememory(void *ptr)
  127. {
  128. int i;
  129. int segment;
  130. segment = ptr2real(ptr) >> 4;
  131. for (i=0 ; i<10 ; i++)
  132. if (seginfo[i].rm_segment == segment)
  133. {
  134. _go32_dpmi_free_dos_memory(&seginfo[i]);
  135. seginfo[i].rm_segment = 0;
  136. break;
  137. }
  138. }
  139. static struct handlerhistory_s
  140. {
  141. int intr;
  142. _go32_dpmi_seginfo pm_oldvec;
  143. } handlerhistory[4];
  144. static int handlercount=0;
  145. void dos_registerintr(int intr, void (*handler)(void))
  146. {
  147. _go32_dpmi_seginfo info;
  148. struct handlerhistory_s *oldstuff;
  149. oldstuff = &handlerhistory[handlercount];
  150. // remember old handler
  151. _go32_dpmi_get_protected_mode_interrupt_vector(intr, &oldstuff->pm_oldvec);
  152. oldstuff->intr = intr;
  153. info.pm_offset = (int) handler;
  154. _go32_dpmi_allocate_iret_wrapper(&info);
  155. // set new protected mode handler
  156. _go32_dpmi_set_protected_mode_interrupt_vector(intr, &info);
  157. handlercount++;
  158. }
  159. void dos_restoreintr(int intr)
  160. {
  161. int i;
  162. struct handlerhistory_s *oldstuff;
  163. // find and reinstall previous interrupt
  164. for (i=0 ; i<handlercount ; i++)
  165. {
  166. oldstuff = &handlerhistory[i];
  167. if (oldstuff->intr == intr)
  168. {
  169. _go32_dpmi_set_protected_mode_interrupt_vector(intr,
  170. &oldstuff->pm_oldvec);
  171. oldstuff->intr = -1;
  172. break;
  173. }
  174. }
  175. }
  176. void dos_usleep(int usecs)
  177. {
  178. usleep(usecs);
  179. }
  180. int dos_getheapsize(void)
  181. {
  182. return _go32_dpmi_remaining_physical_memory();
  183. }
  184. int dos_lockmem(void *addr, int size)
  185. {
  186. __dpmi_meminfo info;
  187. info.address = (long) addr + __djgpp_base_address;
  188. info.size = size;
  189. if (__dpmi_lock_linear_region(&info))
  190. return __dpmi_error;
  191. else
  192. return 0;
  193. }
  194. int dos_unlockmem(void *addr, int size)
  195. {
  196. __dpmi_meminfo info;
  197. info.address = (long) addr + __djgpp_base_address;
  198. info.size = size;
  199. if (__dpmi_unlock_linear_region(&info))
  200. return __dpmi_error;
  201. else
  202. return 0;
  203. }