2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
26 #define _BORLAND_DOS_REGS 1
28 #include <sys/segments.h>
29 #include <sys/nearptr.h>
31 #define FP_SEG(p) _my_ds()
32 #define FP_OFF(p) (int)p
33 int _crt0_startup_flags=_CRT0_FLAG_NONMOVE_SBRK+_CRT0_FLAG_FILL_SBRK_MEMORY+_CRT0_FLAG_FILL_DEADBEEF+_CRT0_FLAG_NEARPTR+_CRT0_FLAG_NO_LFN;
47 int dpmi_find_dos_memory()
51 memset(&r,0,sizeof(r));
52 r.x.eax = 0x0100; // DPMI allocate DOS memory
53 r.x.ebx = 0xffff; // Number of paragraphs requested
54 int386 (0x31, &r, &r);
55 //if ( (r.x.eax & 0xffff) == 0x08 )
56 //if ( (r.x.eax & 0xffff) == 0x08 )
58 return ((r.x.ebx & 0xffff)*16);
63 void *dpmi_real_malloc( int size, ushort *selector )
67 memset(&r,0,sizeof(r));
68 r.x.eax = 0x0100; // DPMI allocate DOS memory
69 r.x.ebx = (size + 15) >> 4; // Number of paragraphs requested
70 int386 (0x31, &r, &r);
72 if (r.x.cflag) // Failed
76 *selector = r.x.edx & 0xFFFF;
79 return (void *) ((r.x.eax & 0xFFFF) << 4)+__djgpp_conventional_base;
81 return (void *) ((r.x.eax & 0xFFFF) << 4);
85 void dpmi_real_free( ushort selector )
89 memset(&r,0,sizeof(r));
90 r.x.eax = 0x0101; // DPMI free DOS memory
91 r.x.ebx = selector; // Selector to free
92 int386 (0x31, &r, &r);
95 int dos_stack_initialized = 0;
96 ubyte * dos_stack = NULL;
97 ubyte * dos_stack_top = NULL;
98 #define DOS_STACK_SIZE (4*1024) // A big ol' 4K stack!!!
100 static void dpmi_setup_stack(dpmi_real_regs *rregs) {
101 ushort temp_selector;
103 if ( !dos_stack_initialized ) {
104 dos_stack_initialized = 1;
105 dos_stack = dpmi_real_malloc( DOS_STACK_SIZE, &temp_selector );
106 if ( dos_stack == NULL ) {
107 printf( "Error allocating real mode stack!\n" );
108 dos_stack_top = NULL;
110 dos_stack_top = &dos_stack[DOS_STACK_SIZE];
114 // Give this puppy a stack!!!
115 if ( dos_stack_top ) {
116 rregs->ss = DPMI_real_segment(dos_stack_top);
117 rregs->sp = DPMI_real_offset(dos_stack_top);
122 void dpmi_real_int386x( ubyte intno, dpmi_real_regs * rregs )
127 /* Use DMPI call 300h to issue the DOS interrupt */
129 dpmi_setup_stack(rregs);
130 memset(®s,0,sizeof(regs));
131 memset(&sregs,0,sizeof(sregs));
136 sregs.es = FP_SEG(rregs);
137 regs.x.edi = FP_OFF(rregs);
138 int386x( 0x31, ®s, ®s, &sregs );
141 void dpmi_real_call(dpmi_real_regs * rregs)
146 dpmi_setup_stack(rregs);
148 /* Use DMPI call 301h to call real mode procedure */
149 memset(®s,0,sizeof(regs));
150 memset(&sregs,0,sizeof(sregs));
154 sregs.es = FP_SEG(rregs);
155 regs.x.edi = FP_OFF(rregs);
156 int386x( 0x31, ®s, ®s, &sregs );
163 int dpmi_unlock_region(void *address, unsigned length)
168 linear = (unsigned int) address;
170 linear += __djgpp_base_address;
173 total_bytes -= length;
174 //mprintf( 1, "DPMI unlocked %d bytes\n", total_bytes );
176 memset(®s,0,sizeof(regs));
177 regs.w.ax = 0x601; // DPMI Unlock Linear Region
178 regs.w.bx = (linear >> 16); // Linear address in BX:CX
179 regs.w.cx = (linear & 0xFFFF);
181 regs.w.si = (length >> 16); // Length in SI:DI
182 regs.w.di = (length & 0xFFFF);
183 int386 (0x31, ®s, ®s);
184 return (! regs.w.cflag); // Return 0 if can't lock
187 int dpmi_lock_region(void *address, unsigned length)
192 linear = (unsigned int) address;
194 linear += __djgpp_base_address;
197 total_bytes += length;
198 //mprintf( 1, "DPMI Locked down %d bytes\n", total_bytes );
200 memset(®s,0,sizeof(regs));
201 regs.w.ax = 0x600; // DPMI Lock Linear Region
202 regs.w.bx = (linear >> 16); // Linear address in BX:CX
203 regs.w.cx = (linear & 0xFFFF);
205 regs.w.si = (length >> 16); // Length in SI:DI
206 regs.w.di = (length & 0xFFFF);
207 int386 (0x31, ®s, ®s);
208 return (! regs.w.cflag); // Return 0 if can't lock
212 int dpmi_modify_selector_base( ushort selector, void * address )
217 linear = (unsigned int)address;
219 linear += __djgpp_base_address;
222 memset(®s,0,sizeof(regs));
223 regs.w.ax = 0x0007; // DPMI Change Selector Base Addres
224 regs.w.bx = selector; // Selector to change
225 regs.w.cx = (linear >> 16); // Base address
226 regs.w.dx = (linear & 0xFFFF);
227 int386 (0x31, ®s, ®s); // call dpmi
229 return 0; // Return 0 if error
235 int dpmi_modify_selector_limit( ushort selector, int size )
238 unsigned int segment_limit;
240 segment_limit = (unsigned int) size;
242 memset(®s,0,sizeof(regs));
243 regs.w.ax = 0x0008; // DPMI Change Selector Limit
244 regs.w.bx = selector; // Selector to change
245 regs.w.cx = (segment_limit >> 16); // Size of selector
246 regs.w.dx = (segment_limit & 0xFFFF);
247 int386 (0x31, ®s, ®s); // call dpmi
249 return 0; // Return 0 if error
255 int dpmi_allocate_selector( void * address, int size, ushort * selector )
260 memset(®s,0,sizeof(regs));
261 regs.w.ax = 0; // DPMI Allocate Selector
262 regs.w.cx = 1; // Allocate 1 selector
263 int386 (0x31, ®s, ®s); // call dpmi
265 return 0; // Return 0 if error
266 *selector = regs.w.ax;
268 if ( !dpmi_modify_selector_base( *selector, address ) )
271 if ( !dpmi_modify_selector_limit( *selector, size ) )
274 // mprintf( 0, "Selector 0x%4x has base of 0x%8x, size %d bytes\n", *selector, linear,segment_limit);
279 static void * dpmi_dos_buffer = NULL;
280 static ushort dpmi_dos_selector = 0;
284 if (dpmi_dos_selector!=0) {
285 dpmi_dos_buffer = NULL;
286 dpmi_dos_selector = 0;
290 typedef struct mem_data {
291 int largest_block_bytes;
292 int max_unlocked_page_allocation;
293 int largest_lockable_pages;
296 int unused_physical_pages;
297 int total_physical_pages;
298 int free_linear_pages;
299 int paging_size_pages;
303 unsigned int dpmi_virtual_memory=0;
304 unsigned int dpmi_available_memory=0;
305 unsigned int dpmi_physical_memory=0;
306 unsigned int dpmi_dos_memory = 0;
309 extern void cdecl _GETDS();
310 extern void cdecl cstart_();
313 int dpmi_init(int verbose)
319 dpmi_dos_memory = dpmi_find_dos_memory();
321 dpmi_dos_buffer = dpmi_real_malloc( 1024, &dpmi_dos_selector);
322 if (!dpmi_dos_buffer) {
323 dpmi_dos_selector = 0;
324 printf( "Error allocating 1K of DOS memory\n" );
330 memset(®s,0,sizeof(regs));
331 regs.x.eax = 0x400; // DPMI Get Memory Info
332 int386( 0x31, ®s, ®s );
334 if (verbose) printf( "V%d.%d, CPU:%d, VMM:", regs.h.ah, regs.h.al, regs.h.cl );
336 if (verbose) printf( "1" );
337 dpmi_virtual_memory = 1;
339 if (verbose) printf( "0" );
343 //--------- Find available memory
344 memset(®s,0,sizeof(regs));
345 memset(&sregs,0,sizeof(sregs));
346 regs.x.eax = 0x500; // DPMI Get Memory Info
347 sregs.es = FP_SEG(&mi);
348 regs.x.edi = FP_OFF(&mi);
349 int386x( 0x31, ®s, ®s, &sregs );
351 if (verbose) printf( ", P:%dK", mi.largest_lockable_pages*4 );
352 if (dpmi_virtual_memory)
353 if (verbose) printf( ", A:%dK", mi.largest_block_bytes/1024 );
354 //dpmi_physical_memory = mi.largest_lockable_pages*4096;
355 //dpmi_available_memory = mi.largest_block_bytes;
356 dpmi_physical_memory = mi.total_physical_pages*4096;
357 dpmi_available_memory = mi.total_pages * 4096;
359 if (verbose) printf( "MemInfo failed!" );
360 dpmi_physical_memory = 16*1024*1024; // Assume 16 MB
361 dpmi_available_memory = 16*1024*1024; // Assume 16 MB
365 if (!dpmi_lock_region( _GETDS, 4096 )) {
366 printf( "Error locking _GETDS" );
369 if (!dpmi_lock_region( cstart_, 4096 )) {
370 printf( "Error locking cstart" );
373 if (!dpmi_lock_region( _chain_intr, 4096 )) {
374 printf( "Error locking _chain_intr" );
381 void *dpmi_get_temp_low_buffer( int size )
383 if ( dpmi_dos_buffer == NULL ) return NULL;
384 if ( size > 1024 ) return NULL;
386 return dpmi_dos_buffer;
389 int dpmi_set_pm_handler(unsigned intnum, void far * isr )
393 /* Use DMPI call 204h to get pm interrrupt */
394 memset(®s,0,sizeof(regs));
397 regs.w.cx = FP_SEG(isr);
398 regs.x.edx = FP_OFF(isr);
399 int386( 0x31, ®s, ®s );