-
Notifications
You must be signed in to change notification settings - Fork 0
/
ID_PM.C
1199 lines (1031 loc) · 27.3 KB
/
ID_PM.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// ID_PM.C
// Id Engine's Page Manager v1.0
// Primary coder: Jason Blochowiak
//
#include "ID_HEADS.H"
#pragma hdrstop
// Main Mem specific variables
boolean MainPresent;
memptr MainMemPages[PMMaxMainMem];
PMBlockAttr MainMemUsed[PMMaxMainMem];
int MainPagesAvail;
// EMS specific variables
boolean EMSPresent;
word EMSAvail,EMSPagesAvail,EMSHandle,
EMSPageFrame,EMSPhysicalPage;
EMSListStruct EMSList[EMSFrameCount];
// XMS specific variables
boolean XMSPresent;
word XMSAvail,XMSPagesAvail,XMSHandle;
longword XMSDriver;
int XMSProtectPage = -1;
// File specific variables
char PageFileName[13] = {"VSWAP."};
int PageFile = -1;
word ChunksInFile;
word PMSpriteStart,PMSoundStart;
// General usage variables
boolean PMStarted,
PMPanicMode,
PMThrashing;
word XMSPagesUsed,
EMSPagesUsed,
MainPagesUsed,
PMNumBlocks;
long PMFrameCount;
PageListStruct far *PMPages,
_seg *PMSegPages;
static char *ParmStrings[] = {"nomain","noems","noxms",nil};
/////////////////////////////////////////////////////////////////////////////
//
// EMS Management code
//
/////////////////////////////////////////////////////////////////////////////
//
// PML_MapEMS() - Maps a logical page to a physical page
//
void
PML_MapEMS(word logical,word physical)
{
_AL = physical;
_BX = logical;
_DX = EMSHandle;
_AH = EMS_MAPPAGE;
asm int EMS_INT
if (_AH)
Quit("PML_MapEMS: Page mapping failed");
}
//
// PML_StartupEMS() - Sets up EMS for Page Mgr's use
// Checks to see if EMS driver is present
// Verifies that EMS hardware is present
// Make sure that EMS version is 3.2 or later
// If there's more than our minimum (2 pages) available, allocate it (up
// to the maximum we need)
//
char EMMDriverName[9] = "EMMXXXX0";
boolean
PML_StartupEMS(void)
{
int i;
long size;
EMSPresent = false; // Assume that we'll fail
EMSAvail = 0;
_DX = (word)EMMDriverName;
_AX = 0x3d00;
geninterrupt(0x21); // try to open EMMXXXX0 device
asm jnc gothandle
goto error;
gothandle:
_BX = _AX;
_AX = 0x4400;
geninterrupt(0x21); // get device info
asm jnc gotinfo;
goto error;
gotinfo:
asm and dx,0x80
if (!_DX)
goto error;
_AX = 0x4407;
geninterrupt(0x21); // get status
asm jc error
if (!_AL)
goto error;
_AH = 0x3e;
geninterrupt(0x21); // close handle
_AH = EMS_STATUS;
geninterrupt(EMS_INT);
if (_AH)
goto error; // make sure EMS hardware is present
_AH = EMS_VERSION;
geninterrupt(EMS_INT);
if (_AH || (_AL < 0x32)) // only work on EMS 3.2 or greater (silly, but...)
goto error;
_AH = EMS_GETFRAME;
geninterrupt(EMS_INT);
if (_AH)
goto error; // find the page frame address
EMSPageFrame = _BX;
_AH = EMS_GETPAGES;
geninterrupt(EMS_INT);
if (_AH)
goto error;
if (_BX < 2)
goto error; // Require at least 2 pages (32k)
EMSAvail = _BX;
// Don't hog all available EMS
size = EMSAvail * (long)EMSPageSize;
if (size - (EMSPageSize * 2) > (ChunksInFile * (long)PMPageSize))
{
size = (ChunksInFile * (long)PMPageSize) + EMSPageSize;
EMSAvail = size / EMSPageSize;
}
_AH = EMS_ALLOCPAGES;
_BX = EMSAvail;
geninterrupt(EMS_INT);
if (_AH)
goto error;
EMSHandle = _DX;
mminfo.EMSmem += EMSAvail * (long)EMSPageSize;
// Initialize EMS mapping cache
for (i = 0;i < EMSFrameCount;i++)
EMSList[i].baseEMSPage = -1;
EMSPresent = true; // We have EMS
error:
return(EMSPresent);
}
//
// PML_ShutdownEMS() - If EMS was used, deallocate it
//
void
PML_ShutdownEMS(void)
{
if (EMSPresent)
{
asm mov ah,EMS_FREEPAGES
asm mov dx,[EMSHandle]
asm int EMS_INT
if (_AH)
Quit ("PML_ShutdownEMS: Error freeing EMS");
}
}
/////////////////////////////////////////////////////////////////////////////
//
// XMS Management code
//
/////////////////////////////////////////////////////////////////////////////
//
// PML_StartupXMS() - Starts up XMS for the Page Mgr's use
// Checks for presence of an XMS driver
// Makes sure that there's at least a page of XMS available
// Allocates any remaining XMS (rounded down to the nearest page size)
//
boolean
PML_StartupXMS(void)
{
XMSPresent = false; // Assume failure
XMSAvail = 0;
asm mov ax,0x4300
asm int XMS_INT // Check for presence of XMS driver
if (_AL != 0x80)
goto error;
asm mov ax,0x4310
asm int XMS_INT // Get address of XMS driver
asm mov [WORD PTR XMSDriver],bx
asm mov [WORD PTR XMSDriver+2],es // function pointer to XMS driver
XMS_CALL(XMS_QUERYFREE); // Find out how much XMS is available
XMSAvail = _AX;
if (!_AX) // AJR: bugfix 10/8/92
goto error;
XMSAvail &= ~(PMPageSizeKB - 1); // Round off to nearest page size
if (XMSAvail < (PMPageSizeKB * 2)) // Need at least 2 pages
goto error;
_DX = XMSAvail;
XMS_CALL(XMS_ALLOC); // And do the allocation
XMSHandle = _DX;
if (!_AX) // AJR: bugfix 10/8/92
{
XMSAvail = 0;
goto error;
}
mminfo.XMSmem += XMSAvail * 1024;
XMSPresent = true;
error:
return(XMSPresent);
}
//
// PML_XMSCopy() - Copies a main/EMS page to or from XMS
// Will round an odd-length request up to the next even value
//
void
PML_XMSCopy(boolean toxms,byte far *addr,word xmspage,word length)
{
longword xoffset;
struct
{
longword length;
word source_handle;
longword source_offset;
word target_handle;
longword target_offset;
} copy;
if (!addr)
Quit("PML_XMSCopy: zero address");
xoffset = (longword)xmspage * PMPageSize;
copy.length = (length + 1) & ~1;
copy.source_handle = toxms? 0 : XMSHandle;
copy.source_offset = toxms? (long)addr : xoffset;
copy.target_handle = toxms? XMSHandle : 0;
copy.target_offset = toxms? xoffset : (long)addr;
asm push si
_SI = (word)©
XMS_CALL(XMS_MOVE);
asm pop si
if (!_AX)
Quit("PML_XMSCopy: Error on copy");
}
#if 1
#define PML_CopyToXMS(s,t,l) PML_XMSCopy(true,(s),(t),(l))
#define PML_CopyFromXMS(t,s,l) PML_XMSCopy(false,(t),(s),(l))
#else
//
// PML_CopyToXMS() - Copies the specified number of bytes from the real mode
// segment address to the specified XMS page
//
void
PML_CopyToXMS(byte far *source,int targetpage,word length)
{
PML_XMSCopy(true,source,targetpage,length);
}
//
// PML_CopyFromXMS() - Copies the specified number of bytes from an XMS
// page to the specified real mode address
//
void
PML_CopyFromXMS(byte far *target,int sourcepage,word length)
{
PML_XMSCopy(false,target,sourcepage,length);
}
#endif
//
// PML_ShutdownXMS()
//
void
PML_ShutdownXMS(void)
{
if (XMSPresent)
{
_DX = XMSHandle;
XMS_CALL(XMS_FREE);
if (_BL)
Quit("PML_ShutdownXMS: Error freeing XMS");
}
}
/////////////////////////////////////////////////////////////////////////////
//
// Main memory code
//
/////////////////////////////////////////////////////////////////////////////
//
// PM_SetMainMemPurge() - Sets the purge level for all allocated main memory
// blocks. This shouldn't be called directly - the PM_LockMainMem() and
// PM_UnlockMainMem() macros should be used instead.
//
void
PM_SetMainMemPurge(int level)
{
int i;
for (i = 0;i < PMMaxMainMem;i++)
if (MainMemPages[i])
MM_SetPurge(&MainMemPages[i],level);
}
//
// PM_CheckMainMem() - If something besides the Page Mgr makes requests of
// the Memory Mgr, some of the Page Mgr's blocks may have been purged,
// so this function runs through the block list and checks to see if
// any of the blocks have been purged. If so, it marks the corresponding
// page as purged & unlocked, then goes through the block list and
// tries to reallocate any blocks that have been purged.
// This routine now calls PM_LockMainMem() to make sure that any allocation
// attempts made during the block reallocation sweep don't purge any
// of the other blocks. Because PM_LockMainMem() is called,
// PM_UnlockMainMem() needs to be called before any other part of the
// program makes allocation requests of the Memory Mgr.
//
void
PM_CheckMainMem(void)
{
boolean allocfailed;
int i,n;
memptr *p;
PMBlockAttr *used;
PageListStruct far *page;
if (!MainPresent)
return;
for (i = 0,page = PMPages;i < ChunksInFile;i++,page++)
{
n = page->mainPage;
if (n != -1) // Is the page using main memory?
{
if (!MainMemPages[n]) // Yep, was the block purged?
{
page->mainPage = -1; // Yes, mark page as purged & unlocked
page->locked = pml_Unlocked;
}
}
}
// Prevent allocation attempts from purging any of our other blocks
PM_LockMainMem();
allocfailed = false;
for (i = 0,p = MainMemPages,used = MainMemUsed;i < PMMaxMainMem;i++,p++,used++)
{
if (!*p) // If the page got purged
{
if (*used & pmba_Allocated) // If it was allocated
{
*used &= ~pmba_Allocated; // Mark as unallocated
MainPagesAvail--; // and decrease available count
}
if (*used & pmba_Used) // If it was used
{
*used &= ~pmba_Used; // Mark as unused
MainPagesUsed--; // and decrease used count
}
if (!allocfailed)
{
MM_BombOnError(false);
MM_GetPtr(p,PMPageSize); // Try to reallocate
if (mmerror) // If it failed,
allocfailed = true; // don't try any more allocations
else // If it worked,
{
*used |= pmba_Allocated; // Mark as allocated
MainPagesAvail++; // and increase available count
}
MM_BombOnError(true);
}
}
}
if (mmerror)
mmerror = false;
}
//
// PML_StartupMainMem() - Allocates as much main memory as is possible for
// the Page Mgr. The memory is allocated as non-purgeable, so if it's
// necessary to make requests of the Memory Mgr, PM_UnlockMainMem()
// needs to be called.
//
void
PML_StartupMainMem(void)
{
int i,n;
memptr *p;
MainPagesAvail = 0;
MM_BombOnError(false);
for (i = 0,p = MainMemPages;i < PMMaxMainMem;i++,p++)
{
MM_GetPtr(p,PMPageSize);
if (mmerror)
break;
MainPagesAvail++;
MainMemUsed[i] = pmba_Allocated;
}
MM_BombOnError(true);
if (mmerror)
mmerror = false;
if (MainPagesAvail < PMMinMainMem)
Quit("PM_SetupMainMem: Not enough main memory");
MainPresent = true;
}
//
// PML_ShutdownMainMem() - Frees all of the main memory blocks used by the
// Page Mgr.
//
void
PML_ShutdownMainMem(void)
{
int i;
memptr *p;
// DEBUG - mark pages as unallocated & decrease page count as appropriate
for (i = 0,p = MainMemPages;i < PMMaxMainMem;i++,p++)
if (*p)
MM_FreePtr(p);
}
/////////////////////////////////////////////////////////////////////////////
//
// File management code
//
/////////////////////////////////////////////////////////////////////////////
//
// PML_ReadFromFile() - Reads some data in from the page file
//
void
PML_ReadFromFile(byte far *buf,long offset,word length)
{
if (!buf)
Quit("PML_ReadFromFile: Null pointer");
if (!offset)
Quit("PML_ReadFromFile: Zero offset");
if (lseek(PageFile,offset,SEEK_SET) != offset)
Quit("PML_ReadFromFile: Seek failed");
if (!CA_FarRead(PageFile,buf,length))
Quit("PML_ReadFromFile: Read failed");
}
//
// PML_OpenPageFile() - Opens the page file and sets up the page info
//
void
PML_OpenPageFile(void)
{
int i;
long size;
void _seg *buf;
longword far *offsetptr;
word far *lengthptr;
PageListStruct far *page;
PageFile = open(PageFileName,O_RDONLY + O_BINARY);
if (PageFile == -1)
Quit("PML_OpenPageFile: Unable to open page file");
// Read in header variables
read(PageFile,&ChunksInFile,sizeof(ChunksInFile));
read(PageFile,&PMSpriteStart,sizeof(PMSpriteStart));
read(PageFile,&PMSoundStart,sizeof(PMSoundStart));
// Allocate and clear the page list
PMNumBlocks = ChunksInFile;
MM_GetPtr(&(memptr)PMSegPages,sizeof(PageListStruct) * PMNumBlocks);
MM_SetLock(&(memptr)PMSegPages,true);
PMPages = (PageListStruct far *)PMSegPages;
_fmemset(PMPages,0,sizeof(PageListStruct) * PMNumBlocks);
// Read in the chunk offsets
size = sizeof(longword) * ChunksInFile;
MM_GetPtr(&buf,size);
if (!CA_FarRead(PageFile,(byte far *)buf,size))
Quit("PML_OpenPageFile: Offset read failed");
offsetptr = (longword far *)buf;
for (i = 0,page = PMPages;i < ChunksInFile;i++,page++)
page->offset = *offsetptr++;
MM_FreePtr(&buf);
// Read in the chunk lengths
size = sizeof(word) * ChunksInFile;
MM_GetPtr(&buf,size);
if (!CA_FarRead(PageFile,(byte far *)buf,size))
Quit("PML_OpenPageFile: Length read failed");
lengthptr = (word far *)buf;
for (i = 0,page = PMPages;i < ChunksInFile;i++,page++)
page->length = *lengthptr++;
MM_FreePtr(&buf);
}
//
// PML_ClosePageFile() - Closes the page file
//
void
PML_ClosePageFile(void)
{
if (PageFile != -1)
close(PageFile);
if (PMSegPages)
{
MM_SetLock(&(memptr)PMSegPages,false);
MM_FreePtr(&(void _seg *)PMSegPages);
}
}
/////////////////////////////////////////////////////////////////////////////
//
// Allocation, etc., code
//
/////////////////////////////////////////////////////////////////////////////
//
// PML_GetEMSAddress()
//
// Page is in EMS, so figure out which EMS physical page should be used
// to map our page in. If normal page, use EMS physical page 3, else
// use the physical page specified by the lock type
//
#if 1
#pragma argsused // DEBUG - remove lock parameter
memptr
PML_GetEMSAddress(int page,PMLockType lock)
{
int i,emspage;
word emsoff,emsbase,offset;
emsoff = page & (PMEMSSubPage - 1);
emsbase = page - emsoff;
emspage = -1;
// See if this page is already mapped in
for (i = 0;i < EMSFrameCount;i++)
{
if (EMSList[i].baseEMSPage == emsbase)
{
emspage = i; // Yep - don't do a redundant remapping
break;
}
}
// If page isn't already mapped in, find LRU EMS frame, and use it
if (emspage == -1)
{
longword last = MAXLONG;
for (i = 0;i < EMSFrameCount;i++)
{
if (EMSList[i].lastHit < last)
{
emspage = i;
last = EMSList[i].lastHit;
}
}
EMSList[emspage].baseEMSPage = emsbase;
PML_MapEMS(page / PMEMSSubPage,emspage);
}
if (emspage == -1)
Quit("PML_GetEMSAddress: EMS find failed");
EMSList[emspage].lastHit = PMFrameCount;
offset = emspage * EMSPageSizeSeg;
offset += emsoff * PMPageSizeSeg;
return((memptr)(EMSPageFrame + offset));
}
#else
memptr
PML_GetEMSAddress(int page,PMLockType lock)
{
word emspage;
emspage = (lock < pml_EMSLock)? 3 : (lock - pml_EMSLock);
PML_MapEMS(page / PMEMSSubPage,emspage);
return((memptr)(EMSPageFrame + (emspage * EMSPageSizeSeg)
+ ((page & (PMEMSSubPage - 1)) * PMPageSizeSeg)));
}
#endif
//
// PM_GetPageAddress() - Returns the address of a given page
// Maps in EMS if necessary
// Returns nil if block isn't cached into Main Memory or EMS
//
//
memptr
PM_GetPageAddress(int pagenum)
{
PageListStruct far *page;
page = &PMPages[pagenum];
if (page->mainPage != -1)
return(MainMemPages[page->mainPage]);
else if (page->emsPage != -1)
return(PML_GetEMSAddress(page->emsPage,page->locked));
else
return(nil);
}
//
// PML_GiveLRUPage() - Returns the page # of the least recently used
// present & unlocked main/EMS page (or main page if mainonly is true)
//
int
PML_GiveLRUPage(boolean mainonly)
{
int i,lru;
long last;
PageListStruct far *page;
for (i = 0,page = PMPages,lru = -1,last = MAXLONG;i < ChunksInFile;i++,page++)
{
if
(
(page->lastHit < last)
&& ((page->emsPage != -1) || (page->mainPage != -1))
&& (page->locked == pml_Unlocked)
&& (!(mainonly && (page->mainPage == -1)))
)
{
last = page->lastHit;
lru = i;
}
}
if (lru == -1)
Quit("PML_GiveLRUPage: LRU Search failed");
return(lru);
}
//
// PML_GiveLRUXMSPage() - Returns the page # of the least recently used
// (and present) XMS page.
// This routine won't return the XMS page protected (by XMSProtectPage)
//
int
PML_GiveLRUXMSPage(void)
{
int i,lru;
long last;
PageListStruct far *page;
for (i = 0,page = PMPages,lru = -1,last = MAXLONG;i < ChunksInFile;i++,page++)
{
if
(
(page->xmsPage != -1)
&& (page->lastHit < last)
&& (i != XMSProtectPage)
)
{
last = page->lastHit;
lru = i;
}
}
return(lru);
}
//
// PML_PutPageInXMS() - If page isn't in XMS, find LRU XMS page and replace
// it with the main/EMS page
//
void
PML_PutPageInXMS(int pagenum)
{
int usexms;
PageListStruct far *page;
if (!XMSPresent)
return;
page = &PMPages[pagenum];
if (page->xmsPage != -1)
return; // Already in XMS
if (XMSPagesUsed < XMSPagesAvail)
page->xmsPage = XMSPagesUsed++;
else
{
usexms = PML_GiveLRUXMSPage();
if (usexms == -1)
Quit("PML_PutPageInXMS: No XMS LRU");
page->xmsPage = PMPages[usexms].xmsPage;
PMPages[usexms].xmsPage = -1;
}
PML_CopyToXMS(PM_GetPageAddress(pagenum),page->xmsPage,page->length);
}
//
// PML_TransferPageSpace() - A page is being replaced, so give the new page
// the old one's address space. Returns the address of the new page.
//
memptr
PML_TransferPageSpace(int orig,int new)
{
memptr addr;
PageListStruct far *origpage,far *newpage;
if (orig == new)
Quit("PML_TransferPageSpace: Identity replacement");
origpage = &PMPages[orig];
newpage = &PMPages[new];
if (origpage->locked != pml_Unlocked)
Quit("PML_TransferPageSpace: Killing locked page");
if ((origpage->emsPage == -1) && (origpage->mainPage == -1))
Quit("PML_TransferPageSpace: Reusing non-existent page");
// Copy page that's about to be purged into XMS
PML_PutPageInXMS(orig);
// Get the address, and force EMS into a physical page if necessary
addr = PM_GetPageAddress(orig);
// Steal the address
newpage->emsPage = origpage->emsPage;
newpage->mainPage = origpage->mainPage;
// Mark replaced page as purged
origpage->mainPage = origpage->emsPage = -1;
if (!addr)
Quit("PML_TransferPageSpace: Zero replacement");
return(addr);
}
//
// PML_GetAPageBuffer() - A page buffer is needed. Either get it from the
// main/EMS free pool, or use PML_GiveLRUPage() to find which page to
// steal the buffer from. Returns a far pointer to the page buffer, and
// sets the fields inside the given page structure appropriately.
// If mainonly is true, free EMS will be ignored, and only main pages
// will be looked at by PML_GiveLRUPage().
//
byte far *
PML_GetAPageBuffer(int pagenum,boolean mainonly)
{
byte far *addr = nil;
int i,n;
PMBlockAttr *used;
PageListStruct far *page;
page = &PMPages[pagenum];
if ((EMSPagesUsed < EMSPagesAvail) && !mainonly)
{
// There's remaining EMS - use it
page->emsPage = EMSPagesUsed++;
addr = PML_GetEMSAddress(page->emsPage,page->locked);
}
else if (MainPagesUsed < MainPagesAvail)
{
// There's remaining main memory - use it
for (i = 0,n = -1,used = MainMemUsed;i < PMMaxMainMem;i++,used++)
{
if ((*used & pmba_Allocated) && !(*used & pmba_Used))
{
n = i;
*used |= pmba_Used;
break;
}
}
if (n == -1)
Quit("PML_GetPageBuffer: MainPagesAvail lied");
addr = MainMemPages[n];
if (!addr)
Quit("PML_GetPageBuffer: Purged main block");
page->mainPage = n;
MainPagesUsed++;
}
else
addr = PML_TransferPageSpace(PML_GiveLRUPage(mainonly),pagenum);
if (!addr)
Quit("PML_GetPageBuffer: Search failed");
return(addr);
}
//
// PML_GetPageFromXMS() - If page is in XMS, find LRU main/EMS page and
// replace it with the page from XMS. If mainonly is true, will only
// search for LRU main page.
// XMSProtectPage is set to the page to be retrieved from XMS, so that if
// the page from which we're stealing the main/EMS from isn't in XMS,
// it won't copy over the page that we're trying to get from XMS.
// (pages that are being purged are copied into XMS, if possible)
//
memptr
PML_GetPageFromXMS(int pagenum,boolean mainonly)
{
byte far *checkaddr;
memptr addr = nil;
PageListStruct far *page;
page = &PMPages[pagenum];
if (XMSPresent && (page->xmsPage != -1))
{
XMSProtectPage = pagenum;
checkaddr = PML_GetAPageBuffer(pagenum,mainonly);
if (FP_OFF(checkaddr))
Quit("PML_GetPageFromXMS: Non segment pointer");
addr = (memptr)FP_SEG(checkaddr);
PML_CopyFromXMS(addr,page->xmsPage,page->length);
XMSProtectPage = -1;
}
return(addr);
}
//
// PML_LoadPage() - A page is not in main/EMS memory, and it's not in XMS.
// Load it into either main or EMS. If mainonly is true, the page will
// only be loaded into main.
//
void
PML_LoadPage(int pagenum,boolean mainonly)
{
byte far *addr;
PageListStruct far *page;
addr = PML_GetAPageBuffer(pagenum,mainonly);
page = &PMPages[pagenum];
PML_ReadFromFile(addr,page->offset,page->length);
}
//
// PM_GetPage() - Returns the address of the page, loading it if necessary
// First, check if in Main Memory or EMS
// Then, check XMS
// If not in XMS, load into Main Memory or EMS
//
#pragma warn -pia
memptr
PM_GetPage(int pagenum)
{
memptr result;
if (pagenum >= ChunksInFile)
Quit("PM_GetPage: Invalid page request");
#if 0 // for debugging
asm mov dx,STATUS_REGISTER_1
asm in al,dx
asm mov dx,ATR_INDEX
asm mov al,ATR_OVERSCAN
asm out dx,al
asm mov al,10 // bright green
asm out dx,al
#endif
if (!(result = PM_GetPageAddress(pagenum)))
{
boolean mainonly = (pagenum >= PMSoundStart);
if (!PMPages[pagenum].offset) // JDC: sparse page
Quit ("Tried to load a sparse page!");
if (!(result = PML_GetPageFromXMS(pagenum,mainonly)))
{
if (PMPages[pagenum].lastHit == PMFrameCount)
PMThrashing++;
PML_LoadPage(pagenum,mainonly);
result = PM_GetPageAddress(pagenum);
}
}
PMPages[pagenum].lastHit = PMFrameCount;
#if 0 // for debugging
asm mov dx,STATUS_REGISTER_1
asm in al,dx
asm mov dx,ATR_INDEX
asm mov al,ATR_OVERSCAN
asm out dx,al
asm mov al,3 // blue
asm out dx,al
asm mov al,0x20 // normal
asm out dx,al
#endif
return(result);
}
#pragma warn +pia
//
// PM_SetPageLock() - Sets the lock type on a given page
// pml_Unlocked: Normal, page can be purged
// pml_Locked: Cannot be purged
// pml_EMS?: Same as pml_Locked, but if in EMS, use the physical page
// specified when returning the address. For sound stuff.
//
void
PM_SetPageLock(int pagenum,PMLockType lock)
{
if (pagenum < PMSoundStart)
Quit("PM_SetPageLock: Locking/unlocking non-sound page");
PMPages[pagenum].locked = lock;
}
//
// PM_Preload() - Loads as many pages as possible into all types of memory.
// Calls the update function after each load, indicating the current
// page, and the total pages that need to be loaded (for thermometer).
//
void
PM_Preload(boolean (*update)(word current,word total))
{
int i,j,
page,oogypage;
word current,total,
totalnonxms,totalxms,
mainfree,maintotal,
emsfree,emstotal,
xmsfree,xmstotal;
memptr addr;
PageListStruct far *p;
mainfree = (MainPagesAvail - MainPagesUsed) + (EMSPagesAvail - EMSPagesUsed);
xmsfree = (XMSPagesAvail - XMSPagesUsed);
xmstotal = maintotal = 0;
for (i = 0;i < ChunksInFile;i++)
{
if (!PMPages[i].offset)
continue; // sparse
if ( PMPages[i].emsPage != -1 || PMPages[i].mainPage != -1 )
continue; // already in main mem
if ( mainfree )
{
maintotal++;
mainfree--;
}
else if ( xmsfree && (PMPages[i].xmsPage == -1) )
{
xmstotal++;
xmsfree--;
}
}
total = maintotal + xmstotal;
if (!total)
return;
page = 0;
current = 0;
//
// cache main/ems blocks
//
while (maintotal)
{
while ( !PMPages[page].offset || PMPages[page].mainPage != -1
|| PMPages[page].emsPage != -1 )