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 WARN(dosmem
, "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();
223 /***********************************************************************
226 * Increment the BIOS tick counter. Called by timer signal handler.
228 void DOSMEM_Tick(void)
230 if (pBiosData
) pBiosData
->Ticks
++;
233 /***********************************************************************
236 * Carve a chunk of the DOS memory block (without selector).
238 LPVOID
DOSMEM_GetBlock(UINT32 size
, UINT16
* pseg
)
243 #ifdef __DOSMEM_DEBUG_
244 dosmem_entry
*prev
= NULL
;
247 if( size
> info_block
->free
) return NULL
;
250 while (dm
&& dm
->size
!= DM_BLOCK_TERMINAL
)
252 #ifdef __DOSMEM_DEBUG__
253 if( (dm
->size
& DM_BLOCK_DEBUG
) != DM_BLOCK_DEBUG
)
255 WARN(dosmem
,"MCB overrun! [prev = 0x%08x]\n", 4 + (UINT32
)prev
);
260 if( dm
->size
& DM_BLOCK_FREE
)
262 dosmem_entry
*next
= NEXT_BLOCK(dm
);
264 while( next
->size
& DM_BLOCK_FREE
) /* collapse free blocks */
266 dm
->size
+= sizeof(dosmem_entry
) + (next
->size
& DM_BLOCK_MASK
);
267 next
->size
= (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
);
268 next
= NEXT_BLOCK(dm
);
271 blocksize
= dm
->size
& DM_BLOCK_MASK
;
272 if( blocksize
>= size
)
274 block
= ((char*)dm
) + sizeof(dosmem_entry
);
275 if( blocksize
- size
> 0x20 )
277 /* split dm so that the next one stays
278 * paragraph-aligned (and dm loses free bit) */
280 dm
->size
= (((size
+ 0xf + sizeof(dosmem_entry
)) & ~0xf) -
281 sizeof(dosmem_entry
));
282 next
= (dosmem_entry
*)(((char*)dm
) +
283 sizeof(dosmem_entry
) + dm
->size
);
284 next
->size
= (blocksize
- (dm
->size
+
285 sizeof(dosmem_entry
))) | DM_BLOCK_FREE
286 #ifdef __DOSMEM_DEBUG__
290 } else dm
->size
&= DM_BLOCK_MASK
;
292 info_block
->blocks
++;
293 info_block
->free
-= dm
->size
;
294 if( pseg
) *pseg
= (block
- DOSMEM_dosmem
) >> 4;
295 #ifdef __DOSMEM_DEBUG__
296 dm
->size
|= DM_BLOCK_DEBUG
;
302 else dm
= NEXT_BLOCK(dm
);
304 return (LPVOID
)block
;
307 /***********************************************************************
310 BOOL32
DOSMEM_FreeBlock(void* ptr
)
312 if( ptr
>= (void*)(((char*)root_block
) + sizeof(dosmem_entry
)) &&
313 ptr
< (void*)DOSMEM_top
&& !((((char*)ptr
) - DOSMEM_dosmem
) & 0xf) )
315 dosmem_entry
*dm
= (dosmem_entry
*)(((char*)ptr
) - sizeof(dosmem_entry
));
317 if( !(dm
->size
& (DM_BLOCK_FREE
| DM_BLOCK_TERMINAL
))
318 #ifdef __DOSMEM_DEBUG__
319 && ((dm
->size
& DM_BLOCK_DEBUG
) == DM_BLOCK_DEBUG
)
323 info_block
->blocks
--;
324 info_block
->free
+= dm
->size
;
326 dm
->size
|= DM_BLOCK_FREE
;
334 /***********************************************************************
335 * DOSMEM_MapLinearToDos
337 * Linear address to the DOS address space.
339 UINT32
DOSMEM_MapLinearToDos(LPVOID ptr
)
341 if (((char*)ptr
>= DOSMEM_dosmem
) &&
342 ((char*)ptr
< DOSMEM_dosmem
+ 0x100000))
343 return (UINT32
)ptr
- (UINT32
)DOSMEM_dosmem
;
348 /***********************************************************************
349 * DOSMEM_MapDosToLinear
351 * DOS linear address to the linear address space.
353 LPVOID
DOSMEM_MapDosToLinear(UINT32 ptr
)
355 if (ptr
< 0x100000) return (LPVOID
)(ptr
+ (UINT32
)DOSMEM_dosmem
);
360 /***********************************************************************
361 * DOSMEM_MapRealToLinear
363 * Real mode DOS address into a linear pointer
365 LPVOID
DOSMEM_MapRealToLinear(DWORD x
)
369 lin
=DOSMEM_dosmem
+(x
&0xffff)+(((x
&0xffff0000)>>16)*16);
370 TRACE(selector
,"(0x%08lx) returns 0x%p.\n",
375 /***********************************************************************
376 * DOSMEM_AllocSelector
378 * Allocates a protected mode selector for a realmode segment.
380 WORD
DOSMEM_AllocSelector(WORD realsel
)
382 HMODULE16 hModule
= GetModuleHandle16("KERNEL");
385 sel
=GLOBAL_CreateBlock(
386 GMEM_FIXED
,DOSMEM_dosmem
+realsel
*16,0x10000,
387 hModule
,FALSE
,FALSE
,FALSE
,NULL
389 TRACE(selector
,"(0x%04x) returns 0x%04x.\n",