-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMEMORY.h
85 lines (68 loc) · 2.09 KB
/
MEMORY.h
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
/*
* MEMORY.h
*
*
* Description:
* This module controls the memory read and write.
* Gloabal variables:
* MEM: in an unsigned long(32bit) array which acts as our memory.
* PageTableIndex : points to the page table that current job uses.
*
* MEMORY function: Reads or Writes and Dumps the memory as specified in specifications.
*
*/
#include<stdio.h>
using namespace std;
int PageTableIndex;
void MEMORY(string X, int Y, unsigned long &Z) {
ofstream myFS;
PageTableIndex = currentJobPCB.PageTablePoint;
// Replace a page in memory if the requested page is not on memory
// i.e. its valid bit is 0
if (PageTable[PageTableIndex][Y / 16][1] == 0){
replacePage(PageTableIndex, (Y / 16));
}
// Taking cases to switch on
int z;
if (X == "READ"){
z = 1;
}else if (X == "WRIT"){
z = 2;
}else if (X == "DUMP"){
z = 3;
}
// Switching through read,write and dump
switch (z) {
case 1: // Read from memory to MemoryBufferRegester
PageTable[PageTableIndex][Y / 16][2] = 1; //Reference bit
Y = calculatePhysicalLocation(PageTable[PageTableIndex], Y,
currentJobPCB.programLength);
Z = MEM[Y];
break;
case 2: // Write from MemoryBufferRegister to memory
PageTable[PageTableIndex][Y / 16][2] = 1; //Reference bit
PageTable[PageTableIndex][Y / 16][3] = 1; //Dirty bit
Y = calculatePhysicalLocation(PageTable[PageTableIndex], Y,
currentJobPCB.programLength);
MEM[Y] = Z;
break;
case 3: // Dump
myFS.open(FN[0], ios_base::app);
myFS << "\n\n CLOCK(dec):\t" << CLOCK << "\tJOBID(hex): "
<< currentJobPCB.JobID << "\n";
for (int i = 0; i < currentJobPCB.programLength; i = i + 8){
myFS << "\t" << setfill('0') << setw(4) << hex << i << "\t";
for (int j = 0; j < 8 && i + j < currentJobPCB.programLength; j++){
myFS << setfill('0') << setw(8) << hex
<< MEM[calculatePhysicalLocation(PageTable[PageTableIndex], (i + j),
currentJobPCB.programLength)] << " ";
}
myFS << "\n";
}
myFS.close();
break;
default:
// Does not reach here.
break;
}
}