my_mem.pas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. unit My_Mem;
  2. INTERFACE
  3. CONST
  4. SegEMMFrame : word = $d000;
  5. EMMVersion : byte = $32;
  6. AbsEMM : pointer = Ptr($D000,0);
  7. Type
  8. EMMRec = record
  9. EMMHandle : word;
  10. PageCount : word;
  11. end;
  12. Function InitEMM:boolean;
  13. Function TestAllocEMM(asize : LongInt):boolean;
  14. Function AllocEMM(aSize : longInt;var aRec:EMMRec):boolean;
  15. Function FreeEMM(var aRec:EMMRec):boolean;
  16. Function MapEMMMemory(aRec:EMMRec;aPageBeg,aPageEnd:word):boolean;
  17. Function MaxEMM:LongInt;
  18. IMPLEMENTATION
  19. CONST
  20. EMMInited : boolean = false;
  21. type
  22. EMSMoveInfoRec = record
  23. RngLen : LongInt;
  24. SrcType : byte;
  25. SrcHandle : word;
  26. SrcAddress : word;
  27. SrcOffset : word;
  28. DestType : byte;
  29. DestHandle : word;
  30. DestAddress: word;
  31. DestOffset : word;
  32. end;
  33. {-------------}
  34. Function MoveEMMMemory(var aRec:EMSMoveInfoRec):byte;assembler;
  35. asm
  36. push ds
  37. mov ax, 5700h
  38. lds di, aRec
  39. int 67h
  40. mov al, ah
  41. mov ah, 0
  42. pop ds
  43. end;{MoveEMMMemory}
  44. {------------------}
  45. Function GetEMMStatus:byte;assembler;
  46. asm
  47. mov ax, 4000h
  48. int 67h
  49. mov al, ah
  50. mov ah, 0
  51. end;{GetEMMStatus}
  52. {---------------------------------}
  53. Function TestEMMInstalled:Boolean;assembler;
  54. asm
  55. mov ax, 3d00h
  56. mov dx, offset @@Name
  57. push ds
  58. push cs
  59. pop ds
  60. int 21h
  61. pop ds
  62. jc @@NoEMS
  63. mov bx, ax
  64. mov ax, 4407h
  65. int 21h
  66. test al, al
  67. mov ah, 3eh
  68. int 21h
  69. jnz @@EMS
  70. jmp @@NoEMS
  71. @@EMS:
  72. mov ax, 1
  73. ret
  74. @@NoEMS:
  75. xor ax,ax
  76. ret
  77. @@Name:
  78. db 'EMMXXXX0',0
  79. end;{TestEMMInstalled}
  80. {--------------------------}
  81. Function GetEmmFrameSeg(var aSeg:word):byte;assembler;
  82. asm
  83. mov ax, 4100h
  84. int 67h
  85. les di, aSeg
  86. mov es:[di], bx
  87. mov al, ah
  88. mov ah, 0
  89. end;{GetEmmFrameSeg}
  90. {-----------------------------------}
  91. Function GetEmmVersion(var aVer:byte):byte;assembler;
  92. asm
  93. mov ax, 4600h
  94. int 67h
  95. les di, aVer
  96. mov es:[di], al
  97. mov al, ah
  98. mov ah, 0
  99. end;{GetEmmFrameSeg}
  100. {-----------------------------------}
  101. Function InitEMM;
  102. begin
  103. InitEMM := false;
  104. if TestEMMInstalled then
  105. if (GetEmmStatus = 0) and (GetEmmFrameSeg(SegEMMFrame) = 0) and
  106. (GetEMMVersion(EMMVersion)=0) then
  107. begin
  108. AbsEMM := Ptr(SegEMMFrame,0);
  109. EMMInited := true;
  110. InitEMM:=true;
  111. end;
  112. end;{InitEMM}
  113. {---------------}
  114. Function GetMaxPages(var a:word):byte;assembler;
  115. asm
  116. mov ax, 4200h
  117. int 67h
  118. les di, a
  119. mov es:[di], bx
  120. mov al, ah
  121. mov ah, 0
  122. end;{GetMaxPages}
  123. {----------------------}
  124. Function MaxEMM;
  125. var
  126. MaxMem : longint;
  127. Pages : word;
  128. begin
  129. MaxEMM:=0;
  130. if not EMMInited then exit;
  131. if GetMaxPages(Pages) = 0 then
  132. begin
  133. MaxMem := Pages;
  134. MaxEMM := MaxMem shl 14;
  135. end
  136. end;{MaxEMM}
  137. {----------------------}
  138. Function TestAllocEMM(asize : LongInt):boolean;
  139. var
  140. MaxPages : word;
  141. begin
  142. TestAllocEMM:=false;
  143. if not EMMInited then exit;
  144. if GetMaxPages(MaxPages) = 0 then
  145. if MaxPages > ((aSize shr 14)+1) then TestAllocEMM := true;
  146. end;{TestAlloc}
  147. {------------------------}
  148. Function AllocEMMPages(var aHandle:word;aP:word):byte;assembler;
  149. asm
  150. mov ax, 4300h
  151. mov bx, aP
  152. int 67h
  153. les di, aHandle
  154. mov es:[di], dx
  155. mov al, ah
  156. mov ah, 0
  157. end;{AllocEMMPages}
  158. {---------------------------}
  159. Function AllocEMM(aSize : longInt;var aRec:EMMRec):boolean;
  160. begin
  161. AllocEMM:=false;
  162. if not EMMInited then exit;
  163. aRec.PageCount := aSize shr 14;
  164. if (aSize mod 16384) <> 0 then Inc(aRec.PageCount);
  165. if AllocEMMPages(aRec.EMMHandle,aRec.PageCount) = 0 then
  166. AllocEMM:=true;
  167. end;{AllocEMM}
  168. {------------------------}
  169. Function FreeEMMPages(aHandle:word):byte;assembler;
  170. asm
  171. mov ax, 4500h
  172. mov dx, word ptr aHandle
  173. int 67h
  174. mov al, ah
  175. mov ah, 0
  176. end;{FreeEMMPages}
  177. {---------------------------}
  178. Function FreeEMM(var aRec:EMMRec):boolean;
  179. begin
  180. FreeEMM:=false;
  181. if not EMMInited then exit;
  182. if FreeEMMPages(aRec.EMMHandle) = 0 then
  183. with aRec do begin
  184. EMMHandle := 0;
  185. PageCount := 0;
  186. FreeEMM:=true;
  187. end;
  188. end;
  189. {----------------------}
  190. Function MapEMMPages(aHandle,aPageBeg,aPageEnd:word):byte;assembler;
  191. asm
  192. mov al, 0
  193. mov bx, aPageBeg
  194. mov dx, word ptr aHandle
  195. @@LOOP:
  196. mov ah, 44h
  197. int 67h
  198. test ah, ah
  199. jnz @@OUT
  200. inc al
  201. inc bx
  202. cmp bx, aPageEnd
  203. jnz @@LOOP
  204. @@OUT:
  205. mov al, ah
  206. mov ah, 0
  207. end;{MapEMMPages}
  208. {--------------------------------}
  209. Function MapEMMMemory;
  210. begin
  211. MapEMMMemory:=false;
  212. if not EMMInited then exit;
  213. if (aPageBeg>aPageEnd) or (aPageEnd > aRec.PageCount) or
  214. ((aPageEnd - aPageBeg) > 3) then exit;
  215. if MapEMMPages(aRec.EMMHandle,aPageBeg,aPageEnd+1) = 0 then
  216. MapEMMMemory:=true;
  217. end;
  218. END.