0% found this document useful (0 votes)
25 views6 pages

CP Phase1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views6 pages

CP Phase1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Name : Prabhakar Uparkar

Roll No : 58
Course Project Phase 1
Q. Design and implement Operating System Phase 1 to perform following task:
i. CPU/ Machine Simulation
ii. Supervisor Call through interrupt

Code:
#include <stdio.h>
#include <string.h>

char M[100][4];
char R[4];
char IR[4];
int IC = 0;
char buffer[41];
int SI = 0;

void init() {
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 4; j++) {
M[i][j] = ' ';
}
}
for (int i = 0; i < 4; i++) {
R[i] = ' ';
IR[i] = ' ';
}
IC = 0;
SI = 0;
}

void Read(FILE *input) {


int address = (IR[2] - '0') * 10 + (IR[3] - '0');
if (fgets(buffer, sizeof(buffer), input)) {
size_t len = strlen(buffer);
if (len > 0 && buffer[len - 1] == '\n') {
buffer[len - 1] = '\0';
}
for (int i = len; i < 40; i++) {
buffer[i] = ' ';
}
buffer[40] = '\0';
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 4; j++) {
if (i * 4 + j < 40) {
M[address + i][j] = buffer[i * 4 + j];
} else {
M[address + i][j] = ' ';
}
}
}
}
}

void Write(FILE *output) {


int address = (IR[2] - '0') * 10 + (IR[3] - '0');
for (int i = 0; i < 10; i++) {
int empty = 1;
for (int j = 0; j < 4; j++) {
if (M[address + i][j] != '\0') {
empty = 0;
break;
}
}
if (!empty) {
for (int j = 0; j < 4; j++) {
if (M[address + i][j] == '\0') {
fprintf(output, " ");
} else {
fprintf(output, "%c", M[address + i][j]);
}
}
}
}
fprintf(output, "\n");
}

void terminate(FILE *output) {


fprintf(output, "\n\n");
}

void MOS(FILE *input, FILE *output) {


if (SI == 1) {
Read(input);
SI = 0;
} else if (SI == 2) {
Write(output);
SI = 0;
} else if (SI == 3) {
terminate(output);
SI = 0;
}
}

void StoreRegister() {
int address = (IR[2] - '0') * 10 + (IR[3] - '0');
for (int i = 0; i < 4; i++) {
M[address][i] = R[i];
}
}

void ExecuteUserProgram(FILE *input, FILE *output) {


while (1) {
for (int i = 0; i < 4; i++) {
IR[i] = M[IC][i];
}
printf("Executing instruction: %c%c at IC=%d\n", IR[0], IR[1],
IC);
if (strncmp(IR, "GD", 2) == 0) {
SI = 1;
MOS(input, output);
IC++;
} else if (strncmp(IR, "PD", 2) == 0) {
SI = 2;
MOS(input, output);
IC++;
} else if (strncmp(IR, "LR", 2) == 0) {
int address = (IR[2] - '0') * 10 + (IR[3] - '0');
for (int i = 0; i < 4; i++) {
R[i] = M[address][i];
}
IC++;
} else if (strncmp(IR, "SR", 2) == 0) {
StoreRegister();
IC++;
} else if (strncmp(IR, "CR", 2) == 0) {
int address = (IR[2] - '0') * 10 + (IR[3] - '0');
int equal = 1;
for (int i = 0; i < 4; i++) {
if (R[i] != M[address][i]) {
equal = 0;
break;
}
}
IC++;
} else if (strncmp(IR, "BT", 2) == 0) {
if (strncmp(R, M[(IR[2] - '0') * 10 + (IR[3] - '0')], 4) ==
0) {
IC = (IR[2] - '0') * 10 + (IR[3] - '0');
} else {
IC++;
}
} else if (strncmp(IR, "H", 1) == 0) {
SI = 3;
MOS(input, output);
break;
} else {
printf("Unrecognized instruction: %c%c\n", IR[0], IR[1]);
break;
}
}
}

void Load(FILE *input, FILE *output) {


int memIndex = 0;
while (fgets(buffer, sizeof(buffer), input)) {
if (strncmp(buffer, "$AMJ", 4) == 0) {
init();
printf("Loading job\n");
memIndex = 0;
} else if (strncmp(buffer, "$DTA", 4) == 0) {
IC = 0;
ExecuteUserProgram(input, output);
} else if (strncmp(buffer, "$END", 4) == 0) {
printf("End of job\n");
continue;
} else if (memIndex < 100) {
for (int i = 0; i < 10 && memIndex < 100; i++) {
if (buffer[i * 4] == '\0') break;
strncpy(M[memIndex], buffer + i * 4, 4);
memIndex++;
}
}
}
}

int main() {
FILE *input = fopen("input_phase1.txt", "r");
FILE *output = fopen("output_phase2.txt", "w");

if (input == NULL || output == NULL) {


printf("Error opening files\n");
return 1;
}

Load(input, output);

fclose(input);
fclose(output);

return 0;
}

Input:
$AMJ000100030001
GD10PD10H
$DTA
Hello
$END0001

$AMJ000100050001
GD10PD10GD20PD20H
$DTA
Hello
SY D Students
$END0001
Output:

You might also like