-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew_cpu.c
More file actions
252 lines (232 loc) · 7.89 KB
/
new_cpu.c
File metadata and controls
252 lines (232 loc) · 7.89 KB
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
/* ******************************************************************************************************
PROGRAM NAME: cpu_setup.c
OBJECTIVE: Develop Best CPU.
DESCRIPTION: Setup CPU architecture with LOAD and STORE data functionality.
TEAM MEMBERS: Sanket Dhami, Karthik Sadanand, Ramyashree, Neha Rege
DATE: 15th Sep, 2016
****************************************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include<stdbool.h>
#define MEMORY_SIZE 2048
#define REG_COUNT 16
#define TEMP_REG_COUNT 16
#define DATA_MEMORY_BASE_ADD 1024 /* Base Address of Data Memory = 1024 */
#define INSTR_MEMORY_BASE_ADD 256 /* Base Address of Instruction Memory = 256 */
#define BOOT_MEMORY_BASE_ADD 0 /* Base Address of BIOS = 0 */
#define STORE_OPCODE 3 /* Opcode for STORE instruction */
#define LOAD_OPCODE 4 /* Opcode for LOAD instruction */
unsigned int memory[MEMORY_SIZE]; /* Total Memory size = 2048 bytes */
unsigned int reg[REG_COUNT]; /* Number of General Purpose Registers = 16 */
unsigned int temp_reg[TEMP_REG_COUNT]; /* Number of Temporary Registers = 16 */
enum registers{A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P};
unsigned int src=0, dest=0; /* Index for source and destination registers */
unsigned int opcode = 0;
unsigned int offset=0;
bool flags[16]={ false }; /* flag register for setting various flags*/
/*Carry Flag = flags[0]
* Parity Flag = flags[2]
* Auxiliary Carry Flag = flags[4]
* Zero Flag = flags[6]
* Sign Flag = flags[7]
* Trap Flag = flags[8]
* Interrupt Flag = flags[9]
* Direction flag = flags[10]
* Overflow flag = flags[11]
* All Others are reserved
* */
/* Program Counter */
unsigned int pc = INSTR_MEMORY_BASE_ADD; /* PC initialized to starting address of instruction memory */
/* Stack Pointer */
unsigned int sp = MEMORY_SIZE-1;
char *p1 = NULL;
int address;
void init_memory( )
{
int i;
for(i=0;i<MEMORY_SIZE;i++)
{
memory[i] = 0;
}
memory[1070]=100;
}
/****************Function for LOAD Instruction *********************************************/
/* On executing LOAD destreg,offset(srcreg) instruction, the address or
value present in source register is added with offset which will result
into final address. The value present in final address is then loaded in
destination register.*/
/*******************************************************************************************/
int execute_load(){
opcode = memory[pc];
src = memory[pc+1];
offset = memory[pc+2];
dest = memory[pc+3];
address = reg[src] + offset;
if(address >= DATA_MEMORY_BASE_ADD && address < MEMORY_SIZE){
reg[dest] = memory[address];
pc = pc + 4;
printf("LOAD Instruction executed successfully\n\n\n");
return 0;
}
else{
printf("Invalid location\n");
}
}
/****************Function for STORE Instruction *********************************************/
/* On executing STORE destreg,offset(srcreg) instruction, the address or
value present in source register is added with offset which will result
into final address. The value present in destination register is then
loaded in memory's final address.*/
/*******************************************************************************************/
int execute_store(){
opcode = memory[pc];
src = memory[pc+1];
offset = memory[pc+2];
dest = memory[pc+3];
address = reg[src] + offset;
if(address >= DATA_MEMORY_BASE_ADD && address < MEMORY_SIZE){
memory[address] = reg[dest];
pc = pc + 4;
printf("STORE Instruction executed successfully\n\n\n");
return 0;
}
else{
printf("Invalid location\n");
}
}
/********Function for printing values of registers, memory, flags *********/
int print_values(){
printf("**************Values of Registers****************\n");
int x = 0;
char reg_char;
for(x =0; x<REG_COUNT; x=x+2){
reg_char = 'A' + x;
printf("Register %c: %d\t\t\t\tRegister %c: %d\n",reg_char,reg[x],reg_char+1,reg[x+1]);
}
printf("*****************Value of Instruction Register******************\n");
printf("Instruction Register:%s\n", p1);
printf("*****************Value of MAR******************\n");
printf("MAR(dec):%d\n",address);
printf("MAR(HEX):%xH\n",address);
printf("*****************Value of MDR******************\n");
printf("MDR:%u\n",memory[address]);
printf("*****************Current Memory location******************\n");
printf("Memory Location(dec):%d\n",address);
printf("Memory Location(HEX):%xH\n",address);
printf("*****************Value of Program Counter******************\n");
printf("Program Counter(dec):%d\n",pc);
printf("Program Counter(HEX):%xH\n",pc);
printf("*****************Value of Flag Register******************\n");
printf("Carry Flag = %d\t",flags[0]);
printf("Parity Flag = %d\t",flags[2]);
printf("Auxiliary Flag = %d\t",flags[4]);
printf("Zero Flag = %d\t",flags[6]);
printf("Sign Flag = %d\t",flags[7]);
printf("Trap Flag = %d\t",flags[8]);
printf("Interrupt Flag = %d\t",flags[9]);
printf("Direction Flag = %d\t",flags[10]);
printf("Overflow Flag = %d\n",flags[11]);
printf("*****************Value of Stack Pointer******************\n");
printf("Stack Pointer(dec):%d\n",sp);
printf("Stack Pointer(HEX):%xH\n",sp);
printf("\n\n\n");
return 0;
}
int main(){
int i = 0;
char *p2 = NULL, *destreg = NULL, *srcreg = NULL;
char instruction[20];
int result;
init_memory();
reg[0] = 1024;
reg[1] = 1040;
result = print_values();
while(pc < MEMORY_SIZE){
printf("Enter instructions LOAD/STORE/EXIT\n");
printf("1. Format for LOAD operation: LOAD dest_reg,offset(src_reg) Eg: LOAD A,30(B) \n");
printf("2 .Format for STORE opcode: STORE dest_reg,offset(src_reg) Eg: STORE A,30(B) \n");
fgets(instruction,20,stdin);
p1 = strtok(instruction, " ,()");
if (strcasecmp(p1, "LOAD")==0) {
opcode = LOAD_OPCODE;
p2 = strtok(NULL," ");
destreg = strtok(p2,",");
dest = destreg[0] - 'A'; /*Index for Destination register*/
if(dest >= P){
printf("Destination registers should be from Register A-Q \n");
break;
}
p2 = strtok(NULL,",");
offset = atoi(strtok(p2,"("));
p2 = strtok(NULL,"(");
if(p2 == NULL){
offset = 0;
}
srcreg = strtok(p2,")");
src = srcreg[0] - 'A'; /*Index for source register*/
if(src >= P){
printf("Source registers should be from Register A-Q \n");
break;
}
memory[INSTR_MEMORY_BASE_ADD + i] = opcode;
memory[INSTR_MEMORY_BASE_ADD + i+1] = src;
memory[INSTR_MEMORY_BASE_ADD + i+2] = offset;
memory[INSTR_MEMORY_BASE_ADD + i+3] = dest;
i = i+4;
result = execute_load();
if (result !=0){
printf("Error in executing Load Instruction \n");
break;
}
result = print_values();
if (result !=0){
printf("Error in printing values \n");
break;
}
}
else if (strcasecmp(p1, "STORE")==0) {
opcode = STORE_OPCODE;
p2 = strtok(NULL," ");
destreg = strtok(p2,",");
dest = destreg[0] - 'A'; /*Index for Destination register*/
if(dest >= P){
printf("Destination registers should be from Register A-Q \n");
break;
}
p2 = strtok(NULL,",");
offset = atoi(strtok(p2,"("));
p2 = strtok(NULL,"(");
if(p2 == NULL){
offset = 0;
}
srcreg = strtok(p2,")");
src = srcreg[0] - 'A'; /*Index for source register*/
if(src >= P){
printf("Source registers should be from Register A-Q \n");
break;
}
memory[INSTR_MEMORY_BASE_ADD + i] = opcode;
memory[INSTR_MEMORY_BASE_ADD + i+1] = src;
memory[INSTR_MEMORY_BASE_ADD + i+2] = offset;
memory[INSTR_MEMORY_BASE_ADD + i+3] = dest;
i = i+4;
result = execute_store();
if (result !=0){
printf("Error in executing Load Instruction \n");
break;
}
result = print_values();
if (result !=0){
printf("Error in printing values \n");
break;
}
}
else {
printf("Enter valid instructions \n");
break;
}
}
return 0;
}