4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Marcus Meissner
20 HANDLE16 DOSMEM_BiosSeg
; /* BIOS data segment at 0x40:0 */
26 WORD Com1Addr
; /* 00: COM1 I/O address */
27 WORD Com2Addr
; /* 02: COM2 I/O address */
28 WORD Com3Addr
; /* 04: COM3 I/O address */
29 WORD Com4Addr
; /* 06: COM4 I/O address */
30 WORD Lpt1Addr
; /* 08: LPT1 I/O address */
31 WORD Lpt2Addr
; /* 0a: LPT2 I/O address */
32 WORD Lpt3Addr
; /* 0c: LPT3 I/O address */
33 WORD Lpt4Addr
; /* 0e: LPT4 I/O address */
34 WORD InstalledHardware
; /* 10: Installed hardware flags */
35 BYTE POSTstatus
; /* 12: Power-On Self Test status */
36 WORD MemSize WINE_PACKED
; /* 13: Base memory size in Kb */
37 WORD unused1 WINE_PACKED
; /* 15: Manufacturing test scratch pad */
38 BYTE KbdFlags1
; /* 17: Keyboard flags 1 */
39 BYTE KbdFlags2
; /* 18: Keyboard flags 2 */
40 BYTE unused2
; /* 19: Keyboard driver workspace */
41 WORD NextKbdCharPtr
; /* 1a: Next character in kbd buffer */
42 WORD FirstKbdCharPtr
; /* 1c: First character in kbd buffer */
43 WORD KbdBuffer
[16]; /* 1e: Keyboard buffer */
44 BYTE DisketteStatus1
; /* 3e: Diskette recalibrate status */
45 BYTE DisketteStatus2
; /* 3f: Diskette motor status */
46 BYTE DisketteStatus3
; /* 40: Diskette motor timeout */
47 BYTE DisketteStatus4
; /* 41: Diskette last operation status */
48 BYTE DiskStatus
[7]; /* 42: Disk status/command bytes */
49 BYTE VideoMode
; /* 49: Video mode */
50 WORD VideoColumns
; /* 4a: Number of columns */
51 WORD VideoPageSize
; /* 4c: Video page size in bytes */
52 WORD VideoPageStartAddr
; /* 4e: Video page start address */
53 BYTE VideoCursorPos
[16]; /* 50: Cursor position for 8 pages */
54 WORD VideoCursorType
; /* 60: Video cursor type */
55 BYTE VideoCurPage
; /* 62: Video current page */
56 WORD VideoCtrlAddr WINE_PACKED
; /* 63: Video controller address */
57 BYTE VideoReg1
; /* 65: Video mode select register */
58 BYTE VideoReg2
; /* 66: Video CGA palette register */
59 DWORD ResetEntry WINE_PACKED
; /* 67: Warm reset entry point */
60 BYTE LastIRQ
; /* 6b: Last unexpected interrupt */
61 DWORD Ticks
; /* 6c: Ticks since midnight */
62 BYTE TicksOverflow
; /* 70: Timer overflow if past midnight */
63 BYTE CtrlBreakFlag
; /* 71: Ctrl-Break flag */
64 WORD ResetFlag
; /* 72: POST Reset flag */
65 BYTE DiskOpStatus
; /* 74: Last hard-disk operation status */
66 BYTE NbHardDisks
; /* 75: Number of hard disks */
67 BYTE DiskCtrlByte
; /* 76: Disk control byte */
68 BYTE DiskIOPort
; /* 77: Disk I/O port offset */
69 BYTE LptTimeout
[4]; /* 78: Timeouts for parallel ports */
70 BYTE ComTimeout
[4]; /* 7c: Timeouts for serial ports */
71 WORD KbdBufferStart
; /* 80: Keyboard buffer start */
72 WORD KbdBufferEnd
; /* 82: Keyboard buffer end */
77 static BIOSDATA
*pBiosData
= NULL
;
78 static char *DOSMEM_dosmem
;
79 static char *DOSMEM_top
;
81 DWORD DOSMEM_CollateTable
;
83 /* use 2 low bits of 'size' for the housekeeping */
85 #define DM_BLOCK_DEBUG 0xABE00000
86 #define DM_BLOCK_TERMINAL 0x00000001
87 #define DM_BLOCK_FREE 0x00000002
88 #define DM_BLOCK_MASK 0x001FFFFC
91 #define __DOSMEM_DEBUG__
103 static dosmem_entry
* root_block
= NULL
;
104 static dosmem_info
* info_block
= NULL
;
106 #define NEXT_BLOCK(block) \
107 (dosmem_entry*)(((char*)(block)) + \
108 sizeof(dosmem_entry) + ((block)->size & DM_BLOCK_MASK))
110 /***********************************************************************
111 * DOSMEM_FillBiosSegment
113 * Fill the BIOS data segment with dummy values.
115 static void DOSMEM_FillBiosSegment(void)
117 pBiosData
= (BIOSDATA
*)GlobalLock16( DOSMEM_BiosSeg
);
119 /* Clear all unused values */
120 memset( pBiosData
, 0, sizeof(*pBiosData
) );
122 /* FIXME: should check the number of configured drives and ports */
124 pBiosData
->Com1Addr
= 0x3e8;
125 pBiosData
->Com2Addr
= 0x2e8;
126 pBiosData
->Lpt1Addr
= 0x378;
127 pBiosData
->Lpt2Addr
= 0x278;
128 pBiosData
->InstalledHardware
= 0x8443;
129 pBiosData
->MemSize
= 640;
130 pBiosData
->NextKbdCharPtr
= 0x1e;
131 pBiosData
->FirstKbdCharPtr
= 0x1e;
132 pBiosData
->VideoMode
= 0;
133 pBiosData
->VideoColumns
= 80;
134 pBiosData
->VideoPageSize
= 80 * 25 * 2;
135 pBiosData
->VideoPageStartAddr
= 0xb800;
136 pBiosData
->VideoCtrlAddr
= 0x3d4;
137 pBiosData
->Ticks
= INT1A_GetTicksSinceMidnight();
138 pBiosData
->NbHardDisks
= 2;
139 pBiosData
->KbdBufferStart
= 0x1e;
140 pBiosData
->KbdBufferEnd
= 0x3e;
143 /***********************************************************************
144 * DOSMEM_InitCollateTable
146 * Initialises the collate table (character sorting, language dependent)
148 static void DOSMEM_InitCollateTable()
154 x
= GlobalDOSAlloc(258);
155 DOSMEM_CollateTable
= MAKELONG(0,(x
>>16));
156 tbl
= DOSMEM_MapRealToLinear(DOSMEM_CollateTable
);
159 for ( i
= 0; i
< 0x100; i
++) *tbl
++ = i
;
162 /***********************************************************************
165 * Initialises the DOS memory structures.
167 static void DOSMEM_InitMemory()
169 /* Low 64Kb are reserved for DOS/BIOS so the useable area starts at
170 * 1000:0000 and ends at 9FFF:FFEF. */
174 DOSMEM_top
= DOSMEM_dosmem
+0x9FFFC; /* 640K */
175 info_block
= (dosmem_info
*)( DOSMEM_dosmem
+ 0x10000 );
177 /* first block has to be paragraph-aligned relative to the DOSMEM_dosmem */
179 root_block
= (dosmem_entry
*)( DOSMEM_dosmem
+ 0x10000 +
180 ((((sizeof(dosmem_info
) + 0xf) & ~0xf) - sizeof(dosmem_entry
))));
181 root_block
->size
= DOSMEM_top
- (((char*)root_block
) + sizeof(dosmem_entry
));
183 info_block
->blocks
= 0;
184 info_block
->free
= root_block
->size
;
186 dm
= NEXT_BLOCK(root_block
);
187 dm
->size
= DM_BLOCK_TERMINAL
;
188 root_block
->size
|= DM_BLOCK_FREE
189 #ifdef __DOSMEM_DEBUG__
195 /***********************************************************************
198 * Create the dos memory segments, and store them into the KERNEL
201 BOOL32
DOSMEM_Init(void)
203 /* Allocate 1 MB dosmemory
204 * - it is mostly wasted but we use can some of it to
205 * store internal translation tables, etc...
207 DOSMEM_dosmem
= VirtualAlloc( NULL
, 0x100000, MEM_COMMIT
,
208 PAGE_EXECUTE_READWRITE
);
211 fprintf( stderr
, "Could not allocate DOS memory.\n" );
214 DOSMEM_BiosSeg
= GLOBAL_CreateBlock(GMEM_FIXED
,DOSMEM_dosmem
+0x400,0x100,
215 0, FALSE
, FALSE
, FALSE
, NULL
);
216 DOSMEM_FillBiosSegment();
218 DOSMEM_InitCollateTable();
222 void DOSMEM_InitExports(HMODULE16 hKernel
)
224 #define SET_ENTRY_POINT(num,addr) \
225 MODULE_SetEntryPoint( hKernel, (num), GLOBAL_CreateBlock( GMEM_FIXED, \
226 DOSMEM_dosmem+(addr), 0x10000, hKernel, \
227 FALSE, FALSE, FALSE, NULL ))
229 SET_ENTRY_POINT( 174, 0xa0000 ); /* KERNEL.174: __A000H */
230 SET_ENTRY_POINT( 181, 0xb0000 ); /* KERNEL.181: __B000H */
231 SET_ENTRY_POINT( 182, 0xb8000 ); /* KERNEL.182: __B800H */
232 SET_ENTRY_POINT( 195, 0xc0000 ); /* KERNEL.195: __C000H */
233 SET_ENTRY_POINT( 179, 0xd0000 ); /* KERNEL.179: __D000H */
234 SET_ENTRY_POINT( 190, 0xe0000 ); /* KERNEL.190: __E000H */
235 SET_ENTRY_POINT( 173, 0xf0000 ); /* KERNEL.173: __ROMBIOS */
236 SET_ENTRY_POINT( 194, 0xf0000 ); /* KERNEL.194: __F000H */
237 MODULE_SetEntryPoint(hKernel
, 193,DOSMEM_BiosSeg
); /* KERNEL.193: __0040H */
239 #undef SET_ENTRY_POINT
242 /***********************************************************************
245 * Increment the BIOS tick counter. Called by timer signal handler.
247 void DOSMEM_Tick(void)
249 if (pBiosData
) pBiosData
->Ticks
++;
252 /***********************************************************************
255 * Carve a chunk of the DOS memory block (without selector).
257 LPVOID
DOSMEM_GetBlock(UINT32 size
, UINT16
* pseg
)
262 #ifdef __DOSMEM_DEBUG_
263 dosmem_entry
*prev
= NULL
;
266 if( size
> info_block
->free
) return NULL
;
269 while (dm
&& dm
->size
!= DM_BLOCK_TERMINAL
)
271 #ifdef __DOSMEM_DEBUG__
272 if( (dm
->size
& DM_BLOCK_DEBUG
) != DM_BLOCK_DEBUG
)
274 fprintf(stderr
,"DOSMEM_GetBlock: MCB overrun! [prev = 0x%08x]\n", 4 + (UINT32
)prev
);
279 if( dm
->size
& DM_BLOCK_FREE
)
281 dosmem_entry
*next
= NEXT_BLOCK(dm
);
283 while( next
->size
& DM_BLOCK_FREE
) /* collapse free blocks */
285 dm
->size
+= sizeof(dosmem_entry
) + (next
->size
& DM_BLOCK_MASK
);
286 next
->size
= (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
);
287 next
= NEXT_BLOCK(dm
);
290 blocksize
= dm
->size
& DM_BLOCK_MASK
;
291 if( blocksize
>= size
)
293 block
= ((char*)dm
) + sizeof(dosmem_entry
);
294 if( blocksize
- size
> 0x20 )
296 /* split dm so that the next one stays
297 * paragraph-aligned (and dm loses free bit) */
299 dm
->size
= (((size
+ 0xf + sizeof(dosmem_entry
)) & ~0xf) -
300 sizeof(dosmem_entry
));
301 next
= (dosmem_entry
*)(((char*)dm
) +
302 sizeof(dosmem_entry
) + dm
->size
);
303 next
->size
= (blocksize
- (dm
->size
+
304 sizeof(dosmem_entry
))) | DM_BLOCK_FREE
305 #ifdef __DOSMEM_DEBUG__
309 } else dm
->size
&= DM_BLOCK_MASK
;
311 info_block
->blocks
++;
312 info_block
->free
-= dm
->size
;
313 if( pseg
) *pseg
= (block
- DOSMEM_dosmem
) >> 4;
314 #ifdef __DOSMEM_DEBUG__
315 dm
->size
|= DM_BLOCK_DEBUG
;
321 else dm
= NEXT_BLOCK(dm
);
323 return (LPVOID
)block
;
326 /***********************************************************************
329 BOOL32
DOSMEM_FreeBlock(void* ptr
)
331 if( ptr
>= (void*)(((char*)root_block
) + sizeof(dosmem_entry
)) &&
332 ptr
< (void*)DOSMEM_top
&& !((((char*)ptr
) - DOSMEM_dosmem
) & 0xf) )
334 dosmem_entry
*dm
= (dosmem_entry
*)(((char*)ptr
) - sizeof(dosmem_entry
));
336 if( !(dm
->size
& (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
))
337 #ifdef __DOSMEM_DEBUG__
338 && ((dm
->size
& DM_BLOCK_DEBUG
) == DM_BLOCK_DEBUG
)
342 info_block
->blocks
--;
343 info_block
->free
+= dm
->size
;
345 dm
->size
|= DM_BLOCK_FREE
;
353 /***********************************************************************
354 * DOSMEM_MapLinearToDos
356 * Linear address to the DOS address space.
358 UINT32
DOSMEM_MapLinearToDos(LPVOID ptr
)
360 if (((char*)ptr
>= DOSMEM_dosmem
) &&
361 ((char*)ptr
< DOSMEM_dosmem
+ 0x100000))
362 return (UINT32
)ptr
- (UINT32
)DOSMEM_dosmem
;
367 /***********************************************************************
368 * DOSMEM_MapDosToLinear
370 * DOS linear address to the linear address space.
372 LPVOID
DOSMEM_MapDosToLinear(UINT32 ptr
)
374 if (ptr
< 0x100000) return (LPVOID
)(ptr
+ (UINT32
)DOSMEM_dosmem
);
379 /***********************************************************************
380 * DOSMEM_MapRealToLinear
382 * Real mode DOS address into a linear pointer
384 LPVOID
DOSMEM_MapRealToLinear(DWORD x
)
388 lin
=DOSMEM_dosmem
+(x
&0xffff)+(((x
&0xffff0000)>>16)*16);
389 dprintf_selector(stddeb
,"DOSMEM_MapR2L(0x%08lx) returns 0x%p.\n",
394 /***********************************************************************
395 * DOSMEM_AllocSelector
397 * Allocates a protected mode selector for a realmode segment.
399 WORD
DOSMEM_AllocSelector(WORD realsel
)
401 HMODULE16 hModule
= GetModuleHandle16("KERNEL");
404 sel
=GLOBAL_CreateBlock(
405 GMEM_FIXED
,DOSMEM_dosmem
+realsel
*16,0x10000,
406 hModule
,FALSE
,FALSE
,FALSE
,NULL
408 dprintf_selector(stddeb
,"DOSMEM_AllocSelector(0x%04x) returns 0x%04x.\n",