-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOsPhase1_final.cpp
357 lines (300 loc) · 7.7 KB
/
OsPhase1_final.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
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
// A.Y.2020-2021 CAOS course project
// OS-Phase-1 Implementation
// SY-CS-D Group-17
#include <bits/stdc++.h>
using namespace std;
ifstream fin;
ofstream fout;
// Function to initialise all the register variables and memory
void INIT(char M[100][4], char *IR, char *R, int &IC, int &SI, int &C)
{
// Initialising memory to null
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 4; j++)
{
M[i][j] = '\0';
}
}
// Initialising Instruction register and general purpose reg "R" to null
for (int i = 0; i < 4; i++)
{
IR[i] = '\0';
R[i] = '\0';
}
// Initialising Instruction counter, toggle reg "C" and Interrupt to zero
IC = 0;
SI = 0;
C = 0;
}
// Getting the data from the data card and storing it to memory block
// GD [mem] Instruction
void getData(char M[100][4], char *IR)
{
// To get memory operand
int a1 = IR[2] - '0';
int a0 = IR[3] - '0';
int mem = a1 * 10 + a0;
// Reading the line from data card
char line[40];
string line2;
getline(fin, line2);
strcpy(line, line2.c_str());
// Storing the input characters to the memory loc specified
char c = line[0];
int index = 0;
int memIndex = 0;
while (c != '\0')
{
M[mem][memIndex] = c;
index++;
c = line[index];
if (memIndex == 3)
mem += 1;
memIndex = index % 4;
}
}
// Reading the data from the memory and writing it to output file
// PD [mem] Instruction
void putData(char M[100][4], char *IR)
{
// To get memory operand
int a1 = IR[2] - '0';
int a0 = IR[3] - '0';
int mem = a1 * 10 + a0;
// To read data at memory
char ch, *buffer;
buffer = (char *)malloc(sizeof(char) * 4);
int index = 0, flag = 0;
for (int i = 0; i < 10; i++)
{
// Getting characters from memory into buffer
for (int k = 0; k < 4; k++)
{
buffer[k] = M[mem][k];
}
for (int j = 0; j < 4; j++)
{
// Writing characters to the output file
ch = buffer[j];
fout.put(ch);
//cout << ch;
}
mem += 1;
}
fout.put('\n');
}
// Ends the job
// H Instruction
void Halt(int &terminate)
{
// Inserting two '\n' charcters to indicate end of Job
fout.put('\n');
fout.put('\n');
// Varible used for Terminating the Job
terminate = 1;
}
// Load the contents at specified memory location to general purpose reg 'R'
// LR [mem] Instruction
void loadData(char M[100][4], char *IR, char *R)
{
int a1 = IR[2] - '0';
int a0 = IR[3] - '0';
int mem = a1 * 10 + a0;
for (int k = 0; k < 4; k++)
{
R[k] = M[mem][k];
}
}
// Store the contents of general purpose reg 'R' to specified memory location
// SR [mem] Instruction
void storeData(char M[100][4], char *IR, char *R)
{
int a1 = IR[2] - '0';
int a0 = IR[3] - '0';
int mem = a1 * 10 + a0;
for (int k = 0; k < 4; k++)
{
M[mem][k] = R[k];
}
}
// Comapre the contents at specified memory location and contents of general purpose register 'R'
// CR [mem] Instruction
void compare(char M[100][4], char *IR, char *R, int &C)
{
int a1 = IR[2] - '0';
int a0 = IR[3] - '0';
int mem = a1 * 10 + a0;
char operand2[4];
int result;
for (int k = 0; k < 4; k++)
{
operand2[k] = M[mem][k];
}
result = (operand2[0] == R[0]);
for (int i = 1; i < 4; i++)
{
result &= (operand2[i] == R[i]);
}
C = result;
}
// Transfers the program flow to specified memory location in program card
// BR [mem] Instruction
void branch(char *IR, int &IC, int &C)
{
if (C)
{
int a1 = IR[2] - '0';
int a0 = IR[3] - '0';
int mem = a1 * 10 + a0;
IC = mem;
C = 0;
}
}
// Master mode
void MOS(char M[100][4], char *IR, int &SI, int &terminate)
{
switch (SI)
{
case 1:
getData(M, IR);
break;
case 2:
putData(M, IR);
break;
case 3:
Halt(terminate);
break;
default:
cout << "ERROR IN MOS SI CODE!!" << endl;
break;
}
SI = 0;
}
// Slave mode
void EXECUTEUSERPROGRAM(char M[100][4], char *IR, char *R, int &IC, int &SI, int &C)
{
IC = 0;
C = 0;
SI = 0;
int terminate = 0;
while (!terminate)
{
for (int k = 0; k < 4; k++)
{
IR[k] = M[IC][k];
}
IC++;
//cout << IR[0] << IR[1] << IR[2] << IR[3] << endl;
if (IR[0] == 'G')
{
// Set Interrupt to 1 for reading data from data card (INPUT)
SI = 1;
MOS(M, IR, SI, terminate);
}
else if (IR[0] == 'P')
{
// Set Interrupt to 2 for writing data to outpt file (OUTPUT)
SI = 2;
MOS(M, IR, SI, terminate);
}
else if (IR[0] == 'H')
{
// Set interrupt to 3 for Halt
SI = 3;
MOS(M, IR, SI, terminate);
}
else if (IR[0] == 'L')
{
loadData(M, IR, R);
//cout << R[0] << R[1] << R[2] << R[3] << "-----" << endl;
}
else if (IR[0] == 'S')
{
storeData(M, IR, R);
}
else if (IR[0] == 'C')
{
compare(M, IR, R, C);
}
else if (IR[0] == 'B')
{
branch(IR, IC, C);
}
}
}
// Starts the execution of program
void STARTEXECUTION(char M[100][4], char *IR, char *R, int &IC, int &SI, int &C)
{
IC = 0;
EXECUTEUSERPROGRAM(M, IR, R, IC, SI, C);
}
// Job execution
void LOAD(char M[100][4], char *IR, char *R, int &IC, int &SI, int &C)
{
// instC - total number of instructions in a job
int instC;
// pcCount - total number of program card
int pcCount;
int m;
string line;
while (!fin.eof())
{
getline(fin, line);
if (!(line.find("$AMJ") == string::npos))
{
m = 0;
instC = stoi(line.substr(8, 4));
if (instC % 10 == 0)
pcCount = instC / 10;
else
pcCount = (instC / 10) + 1;
// Reading program card and storing instructions in the memory
for (int i = 0; i < pcCount; i++)
{
getline(fin, line);
for (int j = 0; j < line.length(); j += 4)
{
for (int k = 0; k < 4; k++)
{
M[m][k] = line[j + k];
}
m++;
}
}
}
else if (!(line.find("$DTA") == string::npos))
{
STARTEXECUTION(M, IR, R, IC, SI, C);
}
else if (!(line.find("$END") == string::npos))
{
// cout << "END DETECTED" << endl;
INIT(M, IR, R, IC, SI, C);
}
}
}
int main()
{
// Reading the input file
fin.open("input_phase1.txt");
// For writing in output file
fout.open("output.txt");
// 2D array for memory
char M[100][4];
// Instruction register
char IR[4];
// General Purpose Register
char R[4];
// Toggle
int C = 0;
// Instruction counter
int IC = 0;
// Interuppt
int SI = 0;
INIT(M, IR, R, IC, SI, C);
LOAD(M, IR, R, IC, SI, C);
fin.close();
fout.close();
return 0;
}