Repository navigation
Expand file tree
/
Copy pathMemory.java
More file actions
166 lines (136 loc) · 4.3 KB
/
Copy pathMemory.java
File metadata and controls
166 lines (136 loc) · 4.3 KB
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
/**
* @author Devin Held Student ID: 26883102
*/
// Memory abstraction for the memory system
public class Memory {
private int[][] memory;
private Bitmap bitMap;
Memory() {
// The memory is of length 1024, and size 512 - represented by a nested
// integer array
this.memory = new int[Info.MEMORY_LENGTH][Info.MEMORY_SIZE];
// The bitmap helps track which position are full
this.bitMap = new Bitmap();
}
// Create a new segment with the segment index in the memory, and the memory
// location
public void segment(int seg, int location) {
memory[0][seg] = location;
// Fill the places in the bitmap
if (location != Info.ERROR) {
bitMap.fillPosition(location / Info.MEMORY_SIZE);
bitMap.fillPosition(location / Info.MEMORY_SIZE + 1);
}
}
// Create a new page given the page number, segment number, and location
// (memory address)
public void page(int page, int seg, int location) {
// If the page is greater than the number of pages, subtract the memory
// size from the page number
if (page >= Info.MEMORY_SIZE)
memory[memory[0][seg] / Info.MEMORY_SIZE + 1][page
- Info.MEMORY_SIZE] = location;
// Otherwise, set the location to the page index
else
memory[memory[0][seg] / Info.MEMORY_SIZE][page] = location;
// Fill the location in the bitmap
if (location != Info.ERROR)
bitMap.fillPosition(location / Info.MEMORY_SIZE);
}
// Get data from the memory
private String getData(int s, int p, int w) {
int frame = memory[0][s] / Info.MEMORY_SIZE, page;
// If p is greater than 512, subtract 512 from it to index
if (p >= Info.MEMORY_SIZE)
page = memory[frame + 1][p - Info.MEMORY_SIZE];
// Otherwise, index p to get the correct page
else
page = memory[frame][p];
// Return the appropriate string based on the value of page
switch (page) {
case Info.ERROR:
return Info.PF;
case Info.CLEAR:
return Info.ERR;
default:
return Integer.toString(page + w);
}
}
// Gets data from memory
public String getDataInMemory(int s, int p, int w) {
// Return the appropriate string based on the value of memory at
// position s
switch (memory[0][s]) {
case Info.ERROR:
return Info.PF;
case Info.CLEAR:
return Info.ERR;
default:
return getData(s, p, w);
}
}
// Adjust memory
private void adjustMem(int frame, int p) {
// Find an empty block in the bitmap
int emptyBlock = bitMap.findEmptyBlock();
// If p is greater than 512, subtract 512 from the page number to set
// the correct block to it
if (p > Info.MEMORY_SIZE - 1) {
memory[frame + 1][p - Info.MEMORY_SIZE] = emptyBlock
* Info.MEMORY_SIZE;
// Fill the bitmap position
bitMap.fillPosition(emptyBlock);
} else {
// Otherwise, simply index the page and fill the bitmap
memory[frame][p] = emptyBlock * Info.MEMORY_SIZE;
bitMap.fillPosition(emptyBlock);
}
}
// Read data from the memory
private String readData(int s, int p, int w) {
// Get the frame
int frame = memory[0][s] / Info.MEMORY_SIZE, page;
// If p is greater than 512, subtract 512 from the page number to set
// the correct block to it
if (p > Info.MEMORY_SIZE - 1)
page = memory[frame + 1][p - Info.MEMORY_SIZE];
// Otherwise, simply index with p to get the page
else
page = memory[frame][p];
// Return appropriate string based on the value of p
switch (page) {
case Info.ERROR:
return Info.PF;
case Info.CLEAR:
adjustMem(frame, p);
return writeDataInMemory(s, p, w);
default:
return Integer.toString(page + w);
}
}
// Assists in populating the bitmap
private String populateBitMap(int s, int p, int w) {
// Find the first empty block
int emptyBlock = bitMap.findEmptyBlock();
// Set the index s to the empty block time 512
memory[0][s] = emptyBlock * Info.MEMORY_SIZE;
// Fill the position where empty block and the subsequent block
bitMap.fillPosition(emptyBlock);
bitMap.fillPosition(emptyBlock + 1);
// Return the string resulting from writing data in memory
return writeDataInMemory(s, p, w);
}
// Writes data to memory
public String writeDataInMemory(int s, int p, int w) {
// Return the appropriate string based on the value in the memory at
// position s
switch (memory[0][s]) {
case Info.ERROR:
return Info.PF;
case Info.CLEAR:
return populateBitMap(s, p, w);
default:
return readData(s, p, w);
}
}
}