-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCOA.cpp
204 lines (185 loc) · 5.39 KB
/
COA.cpp
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
#include<bits/stdc++.h>
using namespace std;
//Functions
void Initialize();
void FillMainMemory();
void PrintTable();
void DirectM(int Blockno);
void AssociativeM(int Blockno);
void SetM(int Blockno);
//Input Declearation
int directM[50],associativeM[50],setM[50],mainM[100],fetchOrder[50];
double timeToReadFromCache,timeToReadFromMemory,timeToCompare;
int noOfLines,noOfSet,noOfLinesInSet,fetchMemory;
//Output Declearation
double TTDM,TTAM,TTSM;
double timeForDM,timeForAM,timeForSM;
int hitDM,hitAM,hitSM;
int missDM,missAM,missSM;
// Main Function for taking Inputs
int main (){
// Take input for Creating Cache Memory
cout<<"\n\n\n\n\n\n\n\n\t\t\tWelcome to Cache Memory Mapping Simulator\n";
cout<<"\n\nEnter no. of lines in the Cache Memory: ";
cin>>noOfLines;
cout<<"\n\nEnter Time Required to Read data from Cache: ";
cin>>timeToReadFromCache;
cout<<"\n\nEnter Time Required to Read data from Main Memory: ";
cin>>timeToReadFromMemory;
cout<<"\n\nEnter Time Required to Compare data in Cache: ";
cin>>timeToCompare;
cout<<"\n\nEnter no. of Sets in Set Associative Mapping: ";
cin>>noOfSet;
noOfLinesInSet = noOfLines / noOfSet;
Initialize();
FillMainMemory();
cout<<"\n\nEnter no. of Memory calls to be made: ";
cin>>fetchMemory;
cout<<"\n\nEnter Memory read Calls in Order by specifing Block number:\n";
for(int i = 0; i < fetchMemory; i++)
{
cin>>fetchOrder[i];
}
for(int i = 0; i < fetchMemory; i++)
{
int number = fetchOrder[i];
cout<<"\n\n\n\nAfter "<<number<<" --> "<<mainM[number]<<"\n";
DirectM(number);
AssociativeM(number);
SetM(number);
PrintTable();
}
// Print final comparison Table
cout<<"\n\n\t\t\tComparison \n\n\tDirect Associative Set Associative\n";
cout<<"Time\t "<<TTDM<<"\t\t"<<TTAM<<"\t\t"<<TTSM<<"\n";
cout<<"Hit\t "<<hitDM<<"\t\t"<<hitAM<<"\t\t"<<hitSM<<"\n";
cout<<"miss\t "<<missDM<<"\t\t"<<missAM<<"\t\t"<<missSM<<"\n";
return 0;
}
// Initializes Parameters to default value
void Initialize(){
int i;
// Variable initialized to zero
timeForDM = 0; timeForAM = 0; timeForSM = 0;
hitDM =0; hitAM = 0; hitSM = 0;
missDM = 0; missAM = 0; missSM = 0;
// Array initialized to zero
for( i = 0; i < 50; i++)
{
directM[i] = 0; associativeM[i] = 0; setM[i] = 0;
}
for( i = 0; i < 100; i++)
{
mainM[i] = 0;
}
}
// Fills Values into main memory
void FillMainMemory(){
int n,i;
cout<<"\n\n\n\n\n\n\n\n\t\t\tMemory is Restricted from 0 to 99 Blocks Only\n";
cout<<"\n\nPass input in the given format { Block No. Data(in Numbers only)}\n";
cout<<"\n\nNo. of Blocks to be Added: ";
cin>>n;
for( i = 0; i < n; i++)
{
int val,data;
cin>>val>>data;
mainM[val] = data;
}
cout<<"\n\n\n\t\t\tMain Memory Visualization";
cout<<"\n\nBlock No. Data\n";
for(i = 0; i < 100; i++)
{
if (mainM[i]!=0) {
cout<<" "<<i<<" "<<mainM[i];
cout<<"\n";
}
}
}
// Print Table of Memory visualization
void PrintTable(){
// Code
cout<<"\n\n\t\t\tTable\n";
cout<<"Line no. Direct Associative Set\n";
for(int i = 0; i < noOfLines; i++)
{
cout<<" "<<i<<"\t\t"<<directM[i]<<"\t\t"<<associativeM[i]<<"\t\t"<<setM[i]<<"\n";
}
}
// Calculation for Direct Mapping
void DirectM(int number){
// Code
if (directM[number%noOfLines] == mainM[number]) {
cout<<"Time taken for the Operation in Direct Mapping = "<<timeToReadFromCache;
TTDM = TTDM + timeToReadFromCache;
hitDM++;
}
else
{
directM[number%noOfLines] = mainM[number];
cout<<"Time taken for the Operation in Direct Mapping = "<<timeToReadFromMemory+timeToReadFromCache;
TTDM = TTDM + timeToReadFromMemory + timeToReadFromCache;
missDM++;
}
}
// Calculation for Associative Mapping
void AssociativeM(int number){
// Code
int i;
for(i = 0; i < noOfLines; i++)
{
if ( associativeM[i] == mainM[number]) {
cout<<"\nTime taken for the Operation in Associative Mapping = "<<(i+1)*timeToCompare + timeToReadFromCache;
TTAM = TTAM + (i+1)*timeToCompare + timeToReadFromCache;
hitAM++;
break;
}
}
if ( i == noOfLines ){
int j;
for( j = 0; j < noOfLines; j++)
{
if(associativeM[j] == 0 ){
associativeM[j] = mainM[number];
break;
}
}
if (j == noOfLines){
associativeM[0] = mainM[number];
}
cout<<"\nTime taken for the Operation in Associative Mapping = "<<(i)*timeToCompare + timeToReadFromCache+ timeToReadFromMemory;
TTAM = TTAM + (i+j)*timeToCompare + timeToReadFromCache + timeToReadFromMemory;
missAM++;
}
}
// Calculation for Set Associative Mapping
void SetM(int number){
// Code
int set,i;
set = number % noOfSet;
for(i = set*noOfLinesInSet; i < (set + 1)*noOfLinesInSet; i++)
{
if( setM[i] == mainM[number]){
cout<<"\nTime taken for the Operation in Set Associative Mapping = "<< ((i-(set*noOfLinesInSet))*timeToCompare) + timeToReadFromCache;
TTSM = TTSM + ((i-(set*noOfLinesInSet))*timeToCompare) + timeToReadFromCache ;
hitSM++;
break;
}
}
if (i == (set + 1)*noOfLinesInSet){
int j;
for( j = set*noOfLinesInSet; j < (set + 1)*noOfLinesInSet; j++)
{
if(setM[j] == 0 ){
setM[j] = mainM[number];
break;
}
}
if (j == (set + 1)*noOfLinesInSet){
setM[set*noOfLinesInSet] = mainM[number];
}
cout<<"\nTime taken for the Operation in Set Associative Mapping = "<<((i-(set*noOfLinesInSet))*timeToCompare) + timeToReadFromCache + timeToReadFromMemory;
TTSM = TTSM + ((i + j -(set*noOfLinesInSet))*timeToCompare) + timeToReadFromCache + timeToReadFromMemory;
missSM++;
}
}