-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicroprocessor.C
140 lines (116 loc) · 2.72 KB
/
Microprocessor.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
#include <iostream>
#include <string>
using namespace std;
// This is the main stack that contains all the data
int mainStack[100];
// This is the 'A' register in the CPU
int accumulator;
// The Opcode Functions
int write(int address, int mainStack[]){
int userInput = 0;
cin >> userInput;
mainStack[address] = userInput;
return 0;
}
int read(int address, int mainStack[]){
return mainStack[address];
//return 0;
}
int load(int address, int mainStack[]){
accumulator = mainStack[address];
return 0;
}
int store(int address, int mainStack[]){
mainStack[address] = accumulator;
return 0;
}
int add(int address, int mainStack[]){
accumulator += mainStack[address];
return 0;
}
int subtract(int address, int mainStack[]){
int value = accumulator - mainStack[address];
accumulator = value;
return 0;
}
int divide(int address, int mainStack[]){
int value = accumulator / mainStack[address];
accumulator = value;
return 0;
}
int multiply(int address, int mainStack[]){
int value = accumulator * mainStack[address];
accumulator = value;
return 0;
}
// The Prompt to take input
int prompt() {
int currentInstruction;
int opcode ;
int operand ;
int val=0;
int currentLine = 0;
int op;
while (true) {
// 99 is the command to quit.
cin >> opcode;
if(opcode==99)
break;
cin >> operand;
currentInstruction= opcode*100+operand;
mainStack[currentLine] = currentInstruction;
currentLine++;
switch ( opcode ) {
case 10:
write(operand, mainStack);
break;
case 11:{
val=read(operand, mainStack);
op=operand;
break;}
case 20:
load(operand, mainStack);
break;
case 21:
store(operand, mainStack);
break;
case 30:
add(operand, mainStack);
break;
case 31:
subtract(operand, mainStack);
break;
case 32:
divide(operand, mainStack);
break;
case 33:
multiply(operand, mainStack);
break;
case 99: // END
break;
default:
cout << "\n Please enter a valid Opcode \n";
return 1;
break;
}
}// End Of while
cout<<"\n\t Executing the program .";
for(long i=0;i<=100000000;i++) // Time delay Loop
{
if(i%20000000==0)
cout<<".";
}
cout<<"\n\t Output Stored in "<<op<<": "<<val<<endl;
return 0;
}
//Main Method
int main() {
cout << "--> Welcome to Paradox Microprocessors \n"
<< "--> Please enter you program below \n"
<< "--> Enter an instruction or instruction plus data \n"
<< "--> the Memory size is of 100 bits. \n"
<< "--> 10:WD 11:RD 20:LDA 21:STA 00-99:Available Memory\n"
<< "--> 30:ADD 31:SUB 32:DIV 33:MUL 99:HLT\n";
prompt();
return 0;
}