-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project1.c
499 lines (446 loc) · 12.6 KB
/
Project1.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#define LINE_MAX 900
extern int errno;
void CPU(int *mem_pipe, int *cpu_pipe, int timer);
void Memory(int *mem_pipe, int *cpu_pipe, char input_file[]);
void load_instructions(int memory[], char input_file[]);
void mem_read(int *mem_pipe,int *cpu_pipe, int *mem);
void mem_write(int *mem_pipe, int *mem);
int main(int argc, char *argv[]) {
/**
* Checks that a file with instructions has been passed
*/
if(argc <= 1){
printf("Not enough arguments, need instruction file\n");
return -1;
}
/**
* Checks that timer is realistic
*/
if(argc >=3){
if(argv[2] <= 0){
printf("Error, timer is not a realistic value! Exiting...");
return -1;
}
}
/**
* memory_pipe: (from CPU to memory)
* [0] : input for memory
* [1] : output for CPU
* cpu_pipe: (from memory to CPU)
* [0] : input for CPU
* [1] : output for memory
*/
srand(time(NULL));
int memory_pipe[2];
int cpu_pipe[2];
int timer = 0;
if(argc >=3)
timer = atoi(argv[2]);
pipe(cpu_pipe);
pipe(memory_pipe);
pid_t pid;
pid = fork();
/**
* Fork split
*/
if(pid == 0)
CPU(memory_pipe, cpu_pipe, timer);
else
Memory(memory_pipe, cpu_pipe, argv[1]);
}
/**
* CPU Process that does all the logic and makes requests to Memory
*/
void CPU(int *mem_pipe, int *cpu_pipe, int timer){
close(mem_pipe[0]);
close(cpu_pipe[1]);
int PC=0,SP=999,IR=0,AC=0,X=0,Y=0;
char read_buf[40];
char write_buf[40];
int timer_counter = timer;
do{
memset(read_buf,0,sizeof(read_buf));
memset(write_buf,0,sizeof(write_buf));
if((timer_counter == 0)&&(PC <1000)&&(timer != 0)){
write(mem_pipe[1], "2", 2);
sprintf(write_buf, "%d", 1999);
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
sprintf(write_buf, "%d", SP);
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
SP = 1998;
write(mem_pipe[1], "2", 2);
sprintf(write_buf, "%d", SP);
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
sprintf(write_buf, "%d", PC);
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
PC = 1000;
timer_counter = timer;
}
else if((timer_counter > 0)&&(PC <1000)&&(timer!=0)){
timer_counter--;
}
write(mem_pipe[1], "1", 2);
sprintf(write_buf, "%d", PC);
PC++;
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
read(cpu_pipe[0], read_buf,sizeof(read_buf));
IR = atoi(read_buf);
switch(IR){
case 1:
write(mem_pipe[1], "1", 2);
sprintf(write_buf, "%d", PC);
PC++;
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
read(cpu_pipe[0], read_buf, sizeof(read_buf));
AC = atoi(read_buf);
break;
case 2:
write(mem_pipe[1], "1", 2);
sprintf(write_buf, "%d", PC);
PC++;
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
read(cpu_pipe[0], read_buf, sizeof(read_buf));
if((atoi(read_buf) > 999)&&(PC < 1000)){
printf("Error: Loading from protected memory, exiting...\n");
IR = 50;
break;
}
write(mem_pipe[1], "1", 2);
write(mem_pipe[1], read_buf, (strlen(read_buf)+1));
read(cpu_pipe[0], read_buf, sizeof(read_buf));
AC = atoi(read_buf);
break;
case 3:
write(mem_pipe[1], "1", 2);
sprintf(write_buf, "%d", PC);
PC++;
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
read(cpu_pipe[0], read_buf, sizeof(read_buf));
if((atoi(read_buf) > 999)&&(PC < 1000)){
printf("Error: Loading from protected memory, exiting...\n");
IR = 50;
break;
}
write(mem_pipe[1], "1", 2);
write(mem_pipe[1], read_buf, (strlen(read_buf)+1));
read(cpu_pipe[0], read_buf, sizeof(read_buf));
if((atoi(read_buf) > 999)&&(PC < 1000)){
printf("Error: Loading from protected memory, exiting...\n");
IR = 50;
break;
}
write(mem_pipe[1], "1", 2);
write(mem_pipe[1], read_buf, (strlen(read_buf)+1));
read(cpu_pipe[0], read_buf, sizeof(read_buf));
AC = atoi(read_buf);
break;
case 4:
write(mem_pipe[1], "1", 2);
sprintf(write_buf, "%d", PC);
PC++;
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
read(cpu_pipe[0], read_buf, sizeof(read_buf));
sprintf(write_buf, "%d", atoi(read_buf) + X);
if((atoi(write_buf) > 999)&&(PC < 1000)){
printf("Error: Loading from protected memory, exiting...\n");
IR = 50;
break;
}
write(mem_pipe[1], "1", 2);
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
read(cpu_pipe[0], read_buf, sizeof(read_buf));
AC = atoi(read_buf);
break;
case 5:
write(mem_pipe[1], "1", 2);
sprintf(write_buf, "%d", PC);
PC++;
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
read(cpu_pipe[0], read_buf, sizeof(read_buf));
sprintf(write_buf, "%d", atoi(read_buf) + Y);
if((atoi(write_buf) > 999)&&(PC < 1000)){
printf("Error: Loading from protected memory, exiting...\n");
IR = 50;
break;
}
write(mem_pipe[1], "1", 2);
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
read(cpu_pipe[0], read_buf, sizeof(read_buf));
AC = atoi(read_buf);
break;
case 6:
write(mem_pipe[1], "1", 2);
sprintf(write_buf, "%d", SP + X);
if((atoi(write_buf) > 999)&&(PC < 1000)){
printf("Error: Loading from protected memory, exiting...\n");
IR = 50;
break;
}
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
read(cpu_pipe[0], read_buf, sizeof(read_buf));
AC = atoi(read_buf);
break;
case 7:
write(mem_pipe[1], "1", 2);
sprintf(write_buf, "%d", PC);
PC++;
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
read(cpu_pipe[0], read_buf, sizeof(read_buf));
write(mem_pipe[1], "2", 2);
write(mem_pipe[1], read_buf, (strlen(read_buf)+1));
sprintf(write_buf, "%d", AC);
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
break;
case 8:
AC = (rand() % 100) + 1;
break;
case 9:
write(mem_pipe[1], "1", 2);
sprintf(write_buf, "%d", PC);
PC++;
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
read(cpu_pipe[0], read_buf, sizeof(read_buf));
if(atoi(read_buf) == 1)
printf("%d",AC);
else if(atoi(read_buf) == 2)
printf("%c",(char)AC);
else
printf("Port number %d unrecognized!\n", atoi(read_buf));
break;
case 10:
AC = AC + X;
break;
case 11:
AC = AC + Y;
break;
case 12:
AC = AC - X;
break;
case 13:
AC = AC - Y;
break;
case 14:
X = AC;
break;
case 15:
AC = X;
break;
case 16:
Y = AC;
break;
case 17:
AC = Y;
break;
case 18:
SP = AC;
break;
case 19:
AC = SP;
break;
case 20:
write(mem_pipe[1], "1", 2);
sprintf(write_buf, "%d", PC);
PC++;
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
read(cpu_pipe[0], read_buf, sizeof(read_buf));
PC = atoi(read_buf);
break;
case 21:
write(mem_pipe[1], "1", 2);
sprintf(write_buf, "%d", PC);
PC++;
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
read(cpu_pipe[0], read_buf, sizeof(read_buf));
if(AC == 0){
PC = atoi(read_buf);
}
break;
case 22:
write(mem_pipe[1], "1", 2);
sprintf(write_buf, "%d", PC);
PC++;
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
read(cpu_pipe[0], read_buf, sizeof(read_buf));
if(AC != 0){
PC = atoi(read_buf);
}
break;
case 23:
//Jump to address
write(mem_pipe[1], "1", 2);
sprintf(write_buf, "%d", PC);
PC++;
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
read(cpu_pipe[0], read_buf, sizeof(read_buf));
SP--;
write(mem_pipe[1], "2", 2);
sprintf(write_buf, "%d", SP);
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
sprintf(write_buf, "%d", PC);
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
PC = atoi(read_buf);
break;
case 24:
//Read Address from stack
write(mem_pipe[1], "1", 2);
sprintf(write_buf, "%d", SP);
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
read(cpu_pipe[0], read_buf, sizeof(read_buf));
SP++;
PC = atoi(read_buf);
break;
case 25:
X++;
break;
case 26:
X--;
break;
case 27:
SP--;
write(mem_pipe[1], "2", 2);
sprintf(write_buf, "%d", SP);
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
sprintf(write_buf, "%d", AC);
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
break;
case 28:
write(mem_pipe[1], "1", 2);
sprintf(write_buf, "%d", SP);
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
read(cpu_pipe[0], read_buf, sizeof(read_buf));
SP++;
AC = atoi(read_buf);
break;
case 29:
write(mem_pipe[1], "2", 2);
write(mem_pipe[1], "1999", 5);
sprintf(write_buf, "%d", SP);
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
SP = 1998;
write(mem_pipe[1], "2", 2);
sprintf(write_buf, "%d", SP);
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
sprintf(write_buf, "%d", PC);
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
PC = 1500;
break;
case 30:
write(mem_pipe[1], "1", 2);
sprintf(write_buf, "%d", SP);
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
read(cpu_pipe[0], read_buf, sizeof(read_buf));
PC = atoi(read_buf);
SP++;
write(mem_pipe[1], "1", 2);
sprintf(write_buf, "%d", SP);
write(mem_pipe[1], write_buf, (strlen(write_buf)+1));
read(cpu_pipe[0], read_buf, sizeof(read_buf));
SP = atoi(read_buf);
break;
case 50:
break;
default:
printf("Unrecognized Instruction... Exiting Program...");
write(mem_pipe[1],"0",2);
IR = 50;
break;
}
} while(IR != 50);
write(mem_pipe[1],"0",2);
}
void Memory(int *mem_pipe, int *cpu_pipe, char input_file[]){
close(mem_pipe[1]);
close(cpu_pipe[0]);
/**
* 0-999: User Program
* 1000-1999: System Code
*/
int memory[2000];
load_instructions(memory, input_file);
int i;
/*for(i=0;i<30;i++){
printf("Memory %d: %d\n", i,memory[i]);
}
for(i=1000;i<1010;i++){
printf("Memory %d: %d\n", i,memory[i]);
}
*/
int read_byte;
char read_buf[2];
do{
read_byte = read(mem_pipe[0], read_buf, sizeof(read_buf));
switch(read_buf[0]) {
case '0':
printf("Exiting Program...\n");
break;
case '1':
//printf("READ\n");
mem_read(mem_pipe, cpu_pipe, memory);
break;
case '2':
//printf("WRITE\n");
mem_write(mem_pipe, memory);
break;
default:
printf("Error: Unrecognized Request -> %c. Exiting Program...\n", read_buf[0]);
read_buf[0] = '0';
break;
}
} while(read_buf[0] != '0');
}
/**
* Reads instructions from file and loads them into mememory for the
* memeory function.
*
* Lines may only start with space, new line or numbers, otherwise can cause error.
*/
void load_instructions(int memory[], char input_file[]){
FILE *ifp;
int errnum;
ifp = fopen(input_file,"r");
if (ifp == NULL) {
errnum = errno;
fprintf(stderr, "Value of errno: %d\n", errno);
perror("Error printed by perror");
fprintf(stderr, "Error opening file: %s\n", strerror(errno));
}
int instr,instr_counter = 0;
char buf[LINE_MAX];
while (fgets(buf, sizeof buf, ifp) != NULL) {
if(buf[0] == ' ' || buf[0] == '\n')
continue;
else if(buf[0] == '.'){
instr_counter = atoi(&buf[1]);
continue;
}
else {
sscanf(buf, "%d", &instr);
memory[instr_counter] = instr;
instr_counter++;
}
}
}
void mem_read(int *mem_pipe, int *cpu_pipe, int *mem){
char read_buf[20];
read(mem_pipe[0], read_buf, sizeof(read_buf));
int address = atoi(read_buf);
char data[20];
sprintf(data, "%d", mem[address]);
write(cpu_pipe[1], data, (strlen(data)+1));
}
void mem_write(int *mem_pipe, int *mem){
char read_buf[40];
int address, data;
read(mem_pipe[0], read_buf, sizeof(read_buf));
address = atoi(read_buf);
read(mem_pipe[0], read_buf, sizeof(read_buf));
data = atoi(read_buf);
mem[address] = data;
}