Debian Codegraph @

src/dos/dos_memory.cpp

raw
1 /*
2 * Copyright (C) 2002-2010 The DOSBox Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /* $Id: dos_memory.cpp,v 1.45 2009-07-15 17:05:07 c2woody Exp $ */
20
21 #include "dosbox.h"
22 #include "mem.h"
23 #include "dos_inc.h"
24 #include "callback.h"
25
26 #define UMB_START_SEG 0x9fff
27
28 static Bit16u memAllocStrategy = 0x00;
29
30 static void DOS_CompressMemory(void) {
31 Bit16u mcb_segment=dos.firstMCB;
32 DOS_MCB mcb(mcb_segment);
33 DOS_MCB mcb_next(0);
34
35 while (mcb.GetType()!=0x5a) {
36 mcb_next.SetPt((Bit16u)(mcb_segment+mcb.GetSize()+1));
37 if ((mcb.GetPSPSeg()==0) && (mcb_next.GetPSPSeg()==0)) {
38 mcb.SetSize(mcb.GetSize()+mcb_next.GetSize()+1);
39 mcb.SetType(mcb_next.GetType());
40 } else {
41 mcb_segment+=mcb.GetSize()+1;
42 mcb.SetPt(mcb_segment);
43 }
44 }
45 }
46
47 void DOS_FreeProcessMemory(Bit16u pspseg) {
48 Bit16u mcb_segment=dos.firstMCB;
49 DOS_MCB mcb(mcb_segment);
50 for (;;) {
51 if (mcb.GetPSPSeg()==pspseg) {
52 mcb.SetPSPSeg(MCB_FREE);
53 }
54 if (mcb.GetType()==0x5a) {
55 /* check if currently last block reaches up to the PCJr graphics memory */
56 if ((machine==MCH_PCJR) && (mcb_segment+mcb.GetSize()==0x17fe) &&
57 (real_readb(0x17ff,0)==0x4d) && (real_readw(0x17ff,1)==8)) {
58 /* re-enable the memory past segment 0x2000 */
59 mcb.SetType(0x4d);
60 } else break;
61 }
62 mcb_segment+=mcb.GetSize()+1;
63 mcb.SetPt(mcb_segment);
64 }
65
66 Bit16u umb_start=dos_infoblock.GetStartOfUMBChain();
67 if (umb_start==UMB_START_SEG) {
68 DOS_MCB umb_mcb(umb_start);
69 for (;;) {
70 if (umb_mcb.GetPSPSeg()==pspseg) {
71 umb_mcb.SetPSPSeg(MCB_FREE);
72 }
73 if (umb_mcb.GetType()!=0x4d) break;
74 umb_start+=umb_mcb.GetSize()+1;
75 umb_mcb.SetPt(umb_start);
76 }
77 } else if (umb_start!=0xffff) LOG(LOG_DOSMISC,LOG_ERROR)("Corrupt UMB chain: %x",umb_start);
78
79 DOS_CompressMemory();
80 }
81
82 Bit16u DOS_GetMemAllocStrategy() {
83 return memAllocStrategy;
84 }
85
86 bool DOS_SetMemAllocStrategy(Bit16u strat) {
87 if ((strat&0x3f)<3) {
88 memAllocStrategy = strat;
89 return true;
90 }
91 /* otherwise an invalid allocation strategy was specified */
92 return false;
93 }
94
95 bool DOS_AllocateMemory(Bit16u * segment,Bit16u * blocks) {
96 DOS_CompressMemory();
97 Bit16u bigsize=0;
98 Bit16u mem_strat=memAllocStrategy;
99 Bit16u mcb_segment=dos.firstMCB;
100
101 Bit16u umb_start=dos_infoblock.GetStartOfUMBChain();
102 if (umb_start==UMB_START_SEG) {
103 /* start with UMBs if requested (bits 7 or 6 set) */
104 if (mem_strat&0xc0) mcb_segment=umb_start;
105 } else if (umb_start!=0xffff) LOG(LOG_DOSMISC,LOG_ERROR)("Corrupt UMB chain: %x",umb_start);
106
107 DOS_MCB mcb(0);
108 DOS_MCB mcb_next(0);
109 DOS_MCB psp_mcb(dos.psp()-1);
110 char psp_name[9];
111 psp_mcb.GetFileName(psp_name);
112 Bit16u found_seg=0,found_seg_size=0;
113 for (;;) {
114 mcb.SetPt(mcb_segment);
115 if (mcb.GetPSPSeg()==0) {
116 /* Check for enough free memory in current block */
117 Bit16u block_size=mcb.GetSize();
118 if (block_size<(*blocks)) {
119 if (bigsize<block_size) {
120 /* current block is largest block that was found,
121 but still not as big as requested */
122 bigsize=block_size;
123 }
124 } else if ((block_size==*blocks) && ((mem_strat & 0x3f)<2)) {
125 /* MCB fits precisely, use it if search strategy is firstfit or bestfit */
126 mcb.SetPSPSeg(dos.psp());
127 *segment=mcb_segment+1;
128 return true;
129 } else {
130 switch (mem_strat & 0x3f) {
131 case 0: /* firstfit */
132 mcb_next.SetPt((Bit16u)(mcb_segment+*blocks+1));
133 mcb_next.SetPSPSeg(MCB_FREE);
134 mcb_next.SetType(mcb.GetType());
135 mcb_next.SetSize(block_size-*blocks-1);
136 mcb.SetSize(*blocks);
137 mcb.SetType(0x4d);
138 mcb.SetPSPSeg(dos.psp());
139 mcb.SetFileName(psp_name);
140 //TODO Filename
141 *segment=mcb_segment+1;
142 return true;
143 case 1: /* bestfit */
144 if ((found_seg_size==0) || (block_size<found_seg_size)) {
145 /* first fitting MCB, or smaller than the last that was found */
146 found_seg=mcb_segment;
147 found_seg_size=block_size;
148 }
149 break;
150 default: /* everything else is handled as lastfit by dos */
151 /* MCB is large enough, note it down */
152 found_seg=mcb_segment;
153 found_seg_size=block_size;
154 break;
155 }
156 }
157 }
158 /* Onward to the next MCB if there is one */
159 if (mcb.GetType()==0x5a) {
160 if ((mem_strat&0x80) && (umb_start==UMB_START_SEG)) {
161 /* bit 7 set: try high memory first, then low */
162 mcb_segment=dos.firstMCB;
163 mem_strat&=(~0xc0);
164 } else {
165 /* finished searching all requested MCB chains */
166 if (found_seg) {
167 /* a matching MCB was found (cannot occur for firstfit) */
168 if ((mem_strat & 0x3f)==0x01) {
169 /* bestfit, allocate block at the beginning of the MCB */
170 mcb.SetPt(found_seg);
171
172 mcb_next.SetPt((Bit16u)(found_seg+*blocks+1));
173 mcb_next.SetPSPSeg(MCB_FREE);
174 mcb_next.SetType(mcb.GetType());
175 mcb_next.SetSize(found_seg_size-*blocks-1);
176
177 mcb.SetSize(*blocks);
178 mcb.SetType(0x4d);
179 mcb.SetPSPSeg(dos.psp());
180 mcb.SetFileName(psp_name);
181 //TODO Filename
182 *segment=found_seg+1;
183 } else {
184 /* lastfit, allocate block at the end of the MCB */
185 mcb.SetPt(found_seg);
186 if (found_seg_size==*blocks) {
187 /* use the whole block */
188 mcb.SetPSPSeg(dos.psp());
189 //Not consistent with line 124. But how many application will use this information ?
190 mcb.SetFileName(psp_name);
191 *segment = found_seg+1;
192 return true;
193 }
194 *segment = found_seg+1+found_seg_size - *blocks;
195 mcb_next.SetPt((Bit16u)(*segment-1));
196 mcb_next.SetSize(*blocks);
197 mcb_next.SetType(mcb.GetType());
198 mcb_next.SetPSPSeg(dos.psp());
199 mcb_next.SetFileName(psp_name);
200 // Old Block
201 mcb.SetSize(found_seg_size-*blocks-1);
202 mcb.SetPSPSeg(MCB_FREE);
203 mcb.SetType(0x4D);
204 }
205 return true;
206 }
207 /* no fitting MCB found, return size of largest block */
208 *blocks=bigsize;
209 DOS_SetError(DOSERR_INSUFFICIENT_MEMORY);
210 return false;
211 }
212 } else mcb_segment+=mcb.GetSize()+1;
213 }
214 return false;
215 }
216
217
218 bool DOS_ResizeMemory(Bit16u segment,Bit16u * blocks) {
219 if (segment < DOS_MEM_START+1) {
220 LOG(LOG_DOSMISC,LOG_ERROR)("Program resizes %X, take care",segment);
221 }
222
223 DOS_MCB mcb(segment-1);
224 if ((mcb.GetType()!=0x4d) && (mcb.GetType()!=0x5a)) {
225 DOS_SetError(DOSERR_MCB_DESTROYED);
226 return false;
227 }
228
229 DOS_CompressMemory();
230 Bit16u total=mcb.GetSize();
231 DOS_MCB mcb_next(segment+total);
232 if (*blocks<=total) {
233 if (GCC_UNLIKELY(*blocks==total)) {
234 /* Nothing to do */
235 return true;
236 }
237 /* Shrinking MCB */
238 DOS_MCB mcb_new_next(segment+(*blocks));
239 mcb.SetSize(*blocks);
240 mcb_new_next.SetType(mcb.GetType());
241 if (mcb.GetType()==0x5a) {
242 /* Further blocks follow */
243 mcb.SetType(0x4d);
244 }
245
246 mcb_new_next.SetSize(total-*blocks-1);
247 mcb_new_next.SetPSPSeg(MCB_FREE);
248 mcb.SetPSPSeg(dos.psp());
249 return true;
250 }
251 /* MCB will grow, try to join with following MCB */
252 if (mcb.GetType()!=0x5a) {
253 if (mcb_next.GetPSPSeg()==MCB_FREE) {
254 total+=mcb_next.GetSize()+1;
255 }
256 }
257 if (*blocks<total) {
258 if (mcb.GetType()!=0x5a) {
259 /* save type of following MCB */
260 mcb.SetType(mcb_next.GetType());
261 }
262 mcb.SetSize(*blocks);
263 mcb_next.SetPt((Bit16u)(segment+*blocks));
264 mcb_next.SetSize(total-*blocks-1);
265 mcb_next.SetType(mcb.GetType());
266 mcb_next.SetPSPSeg(MCB_FREE);
267 mcb.SetType(0x4d);
268 mcb.SetPSPSeg(dos.psp());
269 return true;
270 }
271
272 /* at this point: *blocks==total (fits) or *blocks>total,
273 in the second case resize block to maximum */
274
275 if ((mcb_next.GetPSPSeg()==MCB_FREE) && (mcb.GetType()!=0x5a)) {
276 /* adjust type of joined MCB */
277 mcb.SetType(mcb_next.GetType());
278 }
279 mcb.SetSize(total);
280 mcb.SetPSPSeg(dos.psp());
281 if (*blocks==total) return true; /* block fit exactly */
282
283 *blocks=total; /* return maximum */
284 DOS_SetError(DOSERR_INSUFFICIENT_MEMORY);
285 return false;
286 }
287
288
289 bool DOS_FreeMemory(Bit16u segment) {
290 //TODO Check if allowed to free this segment
291 if (segment < DOS_MEM_START+1) {
292 LOG(LOG_DOSMISC,LOG_ERROR)("Program tried to free %X ---ERROR",segment);
293 DOS_SetError(DOSERR_MB_ADDRESS_INVALID);
294 return false;
295 }
296
297 DOS_MCB mcb(segment-1);
298 if ((mcb.GetType()!=0x4d) && (mcb.GetType()!=0x5a)) {
299 DOS_SetError(DOSERR_MB_ADDRESS_INVALID);
300 return false;
301 }
302 mcb.SetPSPSeg(MCB_FREE);
303 // DOS_CompressMemory();
304 return true;
305 }
306
307
308 void DOS_BuildUMBChain(bool umb_active,bool ems_active) {
309 if (umb_active && (machine!=MCH_TANDY)) {
310 Bit16u first_umb_seg = 0xd000;
311 Bit16u first_umb_size = 0x2000;
312 if(ems_active || (machine == MCH_PCJR)) first_umb_size = 0x1000;
313
314 dos_infoblock.SetStartOfUMBChain(UMB_START_SEG);
315 dos_infoblock.SetUMBChainState(0); // UMBs not linked yet
316
317 DOS_MCB umb_mcb(first_umb_seg);
318 umb_mcb.SetPSPSeg(0); // currently free
319 umb_mcb.SetSize(first_umb_size-1);
320 umb_mcb.SetType(0x5a);
321
322 /* Scan MCB-chain for last block */
323 Bit16u mcb_segment=dos.firstMCB;
324 DOS_MCB mcb(mcb_segment);
325 while (mcb.GetType()!=0x5a) {
326 mcb_segment+=mcb.GetSize()+1;
327 mcb.SetPt(mcb_segment);
328 }
329
330 /* A system MCB has to cover the space between the
331 regular MCB-chain and the UMBs */
332 Bit16u cover_mcb=(Bit16u)(mcb_segment+mcb.GetSize()+1);
333 mcb.SetPt(cover_mcb);
334 mcb.SetType(0x4d);
335 mcb.SetPSPSeg(0x0008);
336 mcb.SetSize(first_umb_seg-cover_mcb-1);
337 mcb.SetFileName("SC ");
338
339 } else {
340 dos_infoblock.SetStartOfUMBChain(0xffff);
341 dos_infoblock.SetUMBChainState(0);
342 }
343 }
344
345 bool DOS_LinkUMBsToMemChain(Bit16u linkstate) {
346 /* Get start of UMB-chain */
347 Bit16u umb_start=dos_infoblock.GetStartOfUMBChain();
348 if (umb_start!=UMB_START_SEG) {
349 if (umb_start!=0xffff) LOG(LOG_DOSMISC,LOG_ERROR)("Corrupt UMB chain: %x",umb_start);
350 return false;
351 }
352
353 if ((linkstate&1)==(dos_infoblock.GetUMBChainState()&1)) return true;
354
355 /* Scan MCB-chain for last block before UMB-chain */
356 Bit16u mcb_segment=dos.firstMCB;
357 Bit16u prev_mcb_segment=dos.firstMCB;
358 DOS_MCB mcb(mcb_segment);
359 while ((mcb_segment!=umb_start) && (mcb.GetType()!=0x5a)) {
360 prev_mcb_segment=mcb_segment;
361 mcb_segment+=mcb.GetSize()+1;
362 mcb.SetPt(mcb_segment);
363 }
364 DOS_MCB prev_mcb(prev_mcb_segment);
365
366 switch (linkstate) {
367 case 0x0000: // unlink
368 if ((prev_mcb.GetType()==0x4d) && (mcb_segment==umb_start)) {
369 prev_mcb.SetType(0x5a);
370 }
371 dos_infoblock.SetUMBChainState(0);
372 break;
373 case 0x0001: // link
374 if (mcb.GetType()==0x5a) {
375 mcb.SetType(0x4d);
376 dos_infoblock.SetUMBChainState(1);
377 }
378 break;
379 default:
380 LOG_MSG("Invalid link state %x when reconfiguring MCB chain",linkstate);
381 return false;
382 }
383
384 return true;
385 }
386
387
388 static Bitu DOS_default_handler(void) {
389 LOG(LOG_CPU,LOG_ERROR)("DOS rerouted Interrupt Called %X",lastint);
390 return CBRET_NONE;
391 }
392
393 static CALLBACK_HandlerObject callbackhandler;
394 void DOS_SetupMemory(void) {
395 /* Let dos claim a few bios interrupts. Makes DOSBox more compatible with
396 * buggy games, which compare against the interrupt table. (probably a
397 * broken linked list implementation) */
398 callbackhandler.Allocate(&DOS_default_handler,"DOS default int");
399 Bit16u ihseg = 0x70;
400 Bit16u ihofs = 0x08;
401 real_writeb(ihseg,ihofs+0x00,(Bit8u)0xFE); //GRP 4
402 real_writeb(ihseg,ihofs+0x01,(Bit8u)0x38); //Extra Callback instruction
403 real_writew(ihseg,ihofs+0x02,callbackhandler.Get_callback()); //The immediate word
404 real_writeb(ihseg,ihofs+0x04,(Bit8u)0xCF); //An IRET Instruction
405 RealSetVec(0x01,RealMake(ihseg,ihofs)); //BioMenace (offset!=4)
406 RealSetVec(0x02,RealMake(ihseg,ihofs)); //BioMenace (segment<0x8000)
407 RealSetVec(0x03,RealMake(ihseg,ihofs)); //Alien Incident (offset!=0)
408 RealSetVec(0x04,RealMake(ihseg,ihofs)); //Shadow President (lower byte of segment!=0)
409 // RealSetVec(0x0f,RealMake(ihseg,ihofs)); //Always a tricky one (soundblaster irq)
410
411 // Create a dummy device MCB with PSPSeg=0x0008
412 DOS_MCB mcb_devicedummy((Bit16u)DOS_MEM_START);
413 mcb_devicedummy.SetPSPSeg(MCB_DOS); // Devices
414 mcb_devicedummy.SetSize(1);
415 mcb_devicedummy.SetType(0x4d); // More blocks will follow
416 // mcb_devicedummy.SetFileName("SD ");
417
418 Bit16u mcb_sizes=2;
419 // Create a small empty MCB (result from a growing environment block)
420 DOS_MCB tempmcb((Bit16u)DOS_MEM_START+mcb_sizes);
421 tempmcb.SetPSPSeg(MCB_FREE);
422 tempmcb.SetSize(4);
423 mcb_sizes+=5;
424 tempmcb.SetType(0x4d);
425
426 // Lock the previous empty MCB
427 DOS_MCB tempmcb2((Bit16u)DOS_MEM_START+mcb_sizes);
428 tempmcb2.SetPSPSeg(0x40); // can be removed by loadfix
429 tempmcb2.SetSize(16);
430 mcb_sizes+=17;
431 tempmcb2.SetType(0x4d);
432
433 DOS_MCB mcb((Bit16u)DOS_MEM_START+mcb_sizes);
434 mcb.SetPSPSeg(MCB_FREE); //Free
435 mcb.SetType(0x5a); //Last Block
436 if (machine==MCH_TANDY) {
437 /* memory up to 608k available, the rest (to 640k) is used by
438 the tandy graphics system's variable mapping of 0xb800 */
439 mcb.SetSize(0x97FF - DOS_MEM_START - mcb_sizes);
440 } else if (machine==MCH_PCJR) {
441 /* memory from 128k to 640k is available */
442 mcb_devicedummy.SetPt((Bit16u)0x2000);
443 mcb_devicedummy.SetPSPSeg(MCB_FREE);
444 mcb_devicedummy.SetSize(0x9FFF - 0x2000);
445 mcb_devicedummy.SetType(0x5a);
446
447 /* exclude PCJr graphics region */
448 mcb_devicedummy.SetPt((Bit16u)0x17ff);
449 mcb_devicedummy.SetPSPSeg(MCB_DOS);
450 mcb_devicedummy.SetSize(0x800);
451 mcb_devicedummy.SetType(0x4d);
452
453 /* memory below 96k */
454 mcb.SetSize(0x1800 - DOS_MEM_START - (2+mcb_sizes));
455 mcb.SetType(0x4d);
456 } else {
457 /* complete memory up to 640k available */
458 /* last paragraph used to add UMB chain to low-memory MCB chain */
459 mcb.SetSize(0x9FFE - DOS_MEM_START - mcb_sizes);
460 }
461
462 dos.firstMCB=DOS_MEM_START;
463 dos_infoblock.SetFirstMCB(DOS_MEM_START);
464 }