-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulator.c
171 lines (142 loc) · 3.79 KB
/
simulator.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "simulator_files/memory.h"
#include "simulator_files/func.h"
//reads the object code from the file and loads it into the memory
int loader(FILE *objfile)
{
int startAddress = relocationCounter;
int memaddress = relocationCounter;
while (!feof(objfile))
{
char *line;
line = (char *)malloc(1000 * sizeof(char));
fgets(line, 1000, objfile);
char *pos;
if ((pos = strchr(line, '\n')) != NULL)
*pos = '\0';
if(!strcmp(line,""))
{
continue;
}
printf("%s\n",line);//debug print
char *p;
p = (char *)malloc(10 * sizeof(char));
p = strtok(line, " ");
char *opcode;
opcode = (char *)malloc(10 * sizeof(char));
strcpy(opcode, p);
strcpy(memory[memaddress], opcode);
memaddress++;
}
relocationCounter = memaddress;
for (int i = startAddress; i < relocationCounter; i++)
memory[i];
return startAddress;
}
//runs the program
int run(int startAddress)
{
pc = startAddress;
while (1)
{
int returncode = function(memory[pc]);
if (returncode == 0)
{
return 0;
}
readnext();
}
}
//Output for the user
void showOutput()
{
printf("-----------------------------------------\n");
printf("\n\nOUTPUT:\n");
while (1)
{
printf("\nEnter the memory address (hexadecimal): ");
char addr[10];
scanf("%s", addr);
getchar();
fflush(stdin);
printf("Value at memory[%s] = %s\n", addr, memory[hex_decimal(addr)]);
printf("Enter more addresses(y/n): ");
char choice;
scanf("%c", &choice);
getchar();
fflush(stdin);
if (choice != 'y' && choice != 'Y')
break;
}
}
//Taking data input from user
void userInput()
{
char upperlimit[5] = "3FFF";
char lowerlimit[5] = "2000";
printf("-----------------------------------------\n");
printf("\n\nINPUT:\n");
printf("(Input data must be placed from %sh to %sh memory locations)\n",lowerlimit,upperlimit);
while (1)
{
printf("\nEnter data (y/n): ");
char choice;
scanf("%c", &choice);
getchar();
fflush(stdin);
if (choice != 'y' && choice != 'Y')
break;
printf("\nEnter the memory address (hexadecimal): ");
char addr[10];
scanf("%s", addr);
getchar();
fflush(stdin);
if (hex_decimal(lowerlimit) <= hex_decimal(addr) && hex_decimal(addr) < hex_decimal(upperlimit))
{
printf("Enter the value (hexdecimal): ");
char val[3];
scanf("%s", val);
getchar();
fflush(stdin);
strcpy(memory[hex_decimal(addr)], val);
}
else
{
printf("Input data must be placed from %sh to %sh memory locations\n",lowerlimit,upperlimit);
}
}
}
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Please Enter object file as argument\n");
exit(0);
}
//objfilename contains the objectcode file path
char *objfilename = argv[1];
FILE *objfile = fopen(objfilename, "r");
//check if file doesnot exit
if (objfile == NULL)
{
printf("File not found!\n");
exit(0);
}
initializeMachine();
userInput();
printf("Simulation started\n");
int startAddress = loader(objfile);
printf("Running prog\n");
run(startAddress);
fclose(objfile);
printf("\nacc : %d\n",acc);
showOutput();
FILE* out=fopen("memory.txt","w");
for(int i=0;i<10000;i++)
{
fprintf(out,"Value at address %x = %s\n",i,memory[i]);
}
return 0;
}