Release 980601
[wine/multimedia.git] / msdos / dosmem.c
blob0226450d846cf3431f31cac7dd3b6bf6a565a779
1 /*
2 * DOS memory emulation
4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Marcus Meissner
6 */
8 #include <signal.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "windows.h"
12 #include "winbase.h"
13 #include "global.h"
14 #include "ldt.h"
15 #include "miscemu.h"
16 #include "module.h"
17 #include "debug.h"
19 HANDLE16 DOSMEM_BiosSeg; /* BIOS data segment at 0x40:0 */
21 #pragma pack(1)
23 typedef struct
25 WORD Com1Addr; /* 00: COM1 I/O address */
26 WORD Com2Addr; /* 02: COM2 I/O address */
27 WORD Com3Addr; /* 04: COM3 I/O address */
28 WORD Com4Addr; /* 06: COM4 I/O address */
29 WORD Lpt1Addr; /* 08: LPT1 I/O address */
30 WORD Lpt2Addr; /* 0a: LPT2 I/O address */
31 WORD Lpt3Addr; /* 0c: LPT3 I/O address */
32 WORD Lpt4Addr; /* 0e: LPT4 I/O address */
33 WORD InstalledHardware; /* 10: Installed hardware flags */
34 BYTE POSTstatus; /* 12: Power-On Self Test status */
35 WORD MemSize WINE_PACKED; /* 13: Base memory size in Kb */
36 WORD unused1 WINE_PACKED; /* 15: Manufacturing test scratch pad */
37 BYTE KbdFlags1; /* 17: Keyboard flags 1 */
38 BYTE KbdFlags2; /* 18: Keyboard flags 2 */
39 BYTE unused2; /* 19: Keyboard driver workspace */
40 WORD NextKbdCharPtr; /* 1a: Next character in kbd buffer */
41 WORD FirstKbdCharPtr; /* 1c: First character in kbd buffer */
42 WORD KbdBuffer[16]; /* 1e: Keyboard buffer */
43 BYTE DisketteStatus1; /* 3e: Diskette recalibrate status */
44 BYTE DisketteStatus2; /* 3f: Diskette motor status */
45 BYTE DisketteStatus3; /* 40: Diskette motor timeout */
46 BYTE DisketteStatus4; /* 41: Diskette last operation status */
47 BYTE DiskStatus[7]; /* 42: Disk status/command bytes */
48 BYTE VideoMode; /* 49: Video mode */
49 WORD VideoColumns; /* 4a: Number of columns */
50 WORD VideoPageSize; /* 4c: Video page size in bytes */
51 WORD VideoPageStartAddr; /* 4e: Video page start address */
52 BYTE VideoCursorPos[16]; /* 50: Cursor position for 8 pages */
53 WORD VideoCursorType; /* 60: Video cursor type */
54 BYTE VideoCurPage; /* 62: Video current page */
55 WORD VideoCtrlAddr WINE_PACKED; /* 63: Video controller address */
56 BYTE VideoReg1; /* 65: Video mode select register */
57 BYTE VideoReg2; /* 66: Video CGA palette register */
58 DWORD ResetEntry WINE_PACKED; /* 67: Warm reset entry point */
59 BYTE LastIRQ; /* 6b: Last unexpected interrupt */
60 DWORD Ticks; /* 6c: Ticks since midnight */
61 BYTE TicksOverflow; /* 70: Timer overflow if past midnight */
62 BYTE CtrlBreakFlag; /* 71: Ctrl-Break flag */
63 WORD ResetFlag; /* 72: POST Reset flag */
64 BYTE DiskOpStatus; /* 74: Last hard-disk operation status */
65 BYTE NbHardDisks; /* 75: Number of hard disks */
66 BYTE DiskCtrlByte; /* 76: Disk control byte */
67 BYTE DiskIOPort; /* 77: Disk I/O port offset */
68 BYTE LptTimeout[4]; /* 78: Timeouts for parallel ports */
69 BYTE ComTimeout[4]; /* 7c: Timeouts for serial ports */
70 WORD KbdBufferStart; /* 80: Keyboard buffer start */
71 WORD KbdBufferEnd; /* 82: Keyboard buffer end */
72 } BIOSDATA;
74 #pragma pack(4)
76 static BIOSDATA *pBiosData = NULL;
77 static char *DOSMEM_dosmem;
78 static char *DOSMEM_top;
80 DWORD DOSMEM_CollateTable;
82 /* use 2 low bits of 'size' for the housekeeping */
84 #define DM_BLOCK_DEBUG 0xABE00000
85 #define DM_BLOCK_TERMINAL 0x00000001
86 #define DM_BLOCK_FREE 0x00000002
87 #define DM_BLOCK_MASK 0x001FFFFC
90 #define __DOSMEM_DEBUG__
93 typedef struct {
94 unsigned size;
95 } dosmem_entry;
97 typedef struct {
98 unsigned blocks;
99 unsigned free;
100 } dosmem_info;
102 static dosmem_entry* root_block = NULL;
103 static dosmem_info* info_block = NULL;
105 #define NEXT_BLOCK(block) \
106 (dosmem_entry*)(((char*)(block)) + \
107 sizeof(dosmem_entry) + ((block)->size & DM_BLOCK_MASK))
109 /***********************************************************************
110 * DOSMEM_FillBiosSegment
112 * Fill the BIOS data segment with dummy values.
114 static void DOSMEM_FillBiosSegment(void)
116 pBiosData = (BIOSDATA *)GlobalLock16( DOSMEM_BiosSeg );
118 /* Clear all unused values */
119 memset( pBiosData, 0, sizeof(*pBiosData) );
121 /* FIXME: should check the number of configured drives and ports */
123 pBiosData->Com1Addr = 0x3e8;
124 pBiosData->Com2Addr = 0x2e8;
125 pBiosData->Lpt1Addr = 0x378;
126 pBiosData->Lpt2Addr = 0x278;
127 pBiosData->InstalledHardware = 0x8443;
128 pBiosData->MemSize = 640;
129 pBiosData->NextKbdCharPtr = 0x1e;
130 pBiosData->FirstKbdCharPtr = 0x1e;
131 pBiosData->VideoMode = 0;
132 pBiosData->VideoColumns = 80;
133 pBiosData->VideoPageSize = 80 * 25 * 2;
134 pBiosData->VideoPageStartAddr = 0xb800;
135 pBiosData->VideoCtrlAddr = 0x3d4;
136 pBiosData->Ticks = INT1A_GetTicksSinceMidnight();
137 pBiosData->NbHardDisks = 2;
138 pBiosData->KbdBufferStart = 0x1e;
139 pBiosData->KbdBufferEnd = 0x3e;
142 /***********************************************************************
143 * DOSMEM_InitCollateTable
145 * Initialises the collate table (character sorting, language dependent)
147 static void DOSMEM_InitCollateTable()
149 DWORD x;
150 unsigned char *tbl;
151 int i;
153 x = GlobalDOSAlloc(258);
154 DOSMEM_CollateTable = MAKELONG(0,(x>>16));
155 tbl = DOSMEM_MapRealToLinear(DOSMEM_CollateTable);
156 *(WORD*)tbl = 0x100;
157 tbl += 2;
158 for ( i = 0; i < 0x100; i++) *tbl++ = i;
161 /***********************************************************************
162 * DOSMEM_InitMemory
164 * Initialises the DOS memory structures.
166 static void DOSMEM_InitMemory()
168 /* Low 64Kb are reserved for DOS/BIOS so the useable area starts at
169 * 1000:0000 and ends at 9FFF:FFEF. */
171 dosmem_entry* dm;
173 DOSMEM_top = DOSMEM_dosmem+0x9FFFC; /* 640K */
174 info_block = (dosmem_info*)( DOSMEM_dosmem + 0x10000 );
176 /* first block has to be paragraph-aligned relative to the DOSMEM_dosmem */
178 root_block = (dosmem_entry*)( DOSMEM_dosmem + 0x10000 +
179 ((((sizeof(dosmem_info) + 0xf) & ~0xf) - sizeof(dosmem_entry))));
180 root_block->size = DOSMEM_top - (((char*)root_block) + sizeof(dosmem_entry));
182 info_block->blocks = 0;
183 info_block->free = root_block->size;
185 dm = NEXT_BLOCK(root_block);
186 dm->size = DM_BLOCK_TERMINAL;
187 root_block->size |= DM_BLOCK_FREE
188 #ifdef __DOSMEM_DEBUG__
189 | DM_BLOCK_DEBUG;
190 #endif
194 /***********************************************************************
195 * DOSMEM_Init
197 * Create the dos memory segments, and store them into the KERNEL
198 * exported values.
200 BOOL32 DOSMEM_Init(void)
202 /* Allocate 1 MB dosmemory
203 * - it is mostly wasted but we use can some of it to
204 * store internal translation tables, etc...
206 DOSMEM_dosmem = VirtualAlloc( NULL, 0x100000, MEM_COMMIT,
207 PAGE_EXECUTE_READWRITE );
208 if (!DOSMEM_dosmem)
210 WARN(dosmem, "Could not allocate DOS memory.\n" );
211 return FALSE;
213 DOSMEM_BiosSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_dosmem+0x400,0x100,
214 0, FALSE, FALSE, FALSE, NULL );
215 DOSMEM_FillBiosSegment();
216 DOSMEM_InitMemory();
217 DOSMEM_InitCollateTable();
218 return TRUE;
222 /***********************************************************************
223 * DOSMEM_Tick
225 * Increment the BIOS tick counter. Called by timer signal handler.
227 void DOSMEM_Tick(void)
229 if (pBiosData) pBiosData->Ticks++;
232 /***********************************************************************
233 * DOSMEM_GetBlock
235 * Carve a chunk of the DOS memory block (without selector).
237 LPVOID DOSMEM_GetBlock(UINT32 size, UINT16* pseg)
239 UINT32 blocksize;
240 char *block = NULL;
241 dosmem_entry *dm;
242 #ifdef __DOSMEM_DEBUG_
243 dosmem_entry *prev = NULL;
244 #endif
246 if( size > info_block->free ) return NULL;
247 dm = root_block;
249 while (dm && dm->size != DM_BLOCK_TERMINAL)
251 #ifdef __DOSMEM_DEBUG__
252 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
254 WARN(dosmem,"MCB overrun! [prev = 0x%08x]\n", 4 + (UINT32)prev);
255 return NULL;
257 prev = dm;
258 #endif
259 if( dm->size & DM_BLOCK_FREE )
261 dosmem_entry *next = NEXT_BLOCK(dm);
263 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
265 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
266 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
267 next = NEXT_BLOCK(dm);
270 blocksize = dm->size & DM_BLOCK_MASK;
271 if( blocksize >= size )
273 block = ((char*)dm) + sizeof(dosmem_entry);
274 if( blocksize - size > 0x20 )
276 /* split dm so that the next one stays
277 * paragraph-aligned (and dm loses free bit) */
279 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
280 sizeof(dosmem_entry));
281 next = (dosmem_entry*)(((char*)dm) +
282 sizeof(dosmem_entry) + dm->size);
283 next->size = (blocksize - (dm->size +
284 sizeof(dosmem_entry))) | DM_BLOCK_FREE
285 #ifdef __DOSMEM_DEBUG__
286 | DM_BLOCK_DEBUG
287 #endif
289 } else dm->size &= DM_BLOCK_MASK;
291 info_block->blocks++;
292 info_block->free -= dm->size;
293 if( pseg ) *pseg = (block - DOSMEM_dosmem) >> 4;
294 #ifdef __DOSMEM_DEBUG__
295 dm->size |= DM_BLOCK_DEBUG;
296 #endif
297 break;
299 dm = next;
301 else dm = NEXT_BLOCK(dm);
303 return (LPVOID)block;
306 /***********************************************************************
307 * DOSMEM_FreeBlock
309 BOOL32 DOSMEM_FreeBlock(void* ptr)
311 if( ptr >= (void*)(((char*)root_block) + sizeof(dosmem_entry)) &&
312 ptr < (void*)DOSMEM_top && !((((char*)ptr) - DOSMEM_dosmem) & 0xf) )
314 dosmem_entry *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
316 if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
317 #ifdef __DOSMEM_DEBUG__
318 && ((dm->size & DM_BLOCK_DEBUG) == DM_BLOCK_DEBUG )
319 #endif
322 info_block->blocks--;
323 info_block->free += dm->size;
325 dm->size |= DM_BLOCK_FREE;
326 return TRUE;
329 return FALSE;
333 /***********************************************************************
334 * DOSMEM_MapLinearToDos
336 * Linear address to the DOS address space.
338 UINT32 DOSMEM_MapLinearToDos(LPVOID ptr)
340 if (((char*)ptr >= DOSMEM_dosmem) &&
341 ((char*)ptr < DOSMEM_dosmem + 0x100000))
342 return (UINT32)ptr - (UINT32)DOSMEM_dosmem;
343 return (UINT32)ptr;
347 /***********************************************************************
348 * DOSMEM_MapDosToLinear
350 * DOS linear address to the linear address space.
352 LPVOID DOSMEM_MapDosToLinear(UINT32 ptr)
354 if (ptr < 0x100000) return (LPVOID)(ptr + (UINT32)DOSMEM_dosmem);
355 return (LPVOID)ptr;
359 /***********************************************************************
360 * DOSMEM_MapRealToLinear
362 * Real mode DOS address into a linear pointer
364 LPVOID DOSMEM_MapRealToLinear(DWORD x)
366 LPVOID lin;
368 lin=DOSMEM_dosmem+(x&0xffff)+(((x&0xffff0000)>>16)*16);
369 TRACE(selector,"(0x%08lx) returns 0x%p.\n",
370 x,lin );
371 return lin;
374 /***********************************************************************
375 * DOSMEM_AllocSelector
377 * Allocates a protected mode selector for a realmode segment.
379 WORD DOSMEM_AllocSelector(WORD realsel)
381 HMODULE16 hModule = GetModuleHandle16("KERNEL");
382 WORD sel;
384 sel=GLOBAL_CreateBlock(
385 GMEM_FIXED,DOSMEM_dosmem+realsel*16,0x10000,
386 hModule,FALSE,FALSE,FALSE,NULL
388 TRACE(selector,"(0x%04x) returns 0x%04x.\n",
389 realsel,sel
391 return sel;