-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPU.cpp
241 lines (201 loc) · 5.23 KB
/
CPU.cpp
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
#include <iostream>
#include <string.h>
#include <cstdio>
#include "CPU.h"
using namespace std;
void::CPU::IR_load() {
strcpy(IR,MDR);
IR[7] = '\0';
PC++;
}
int::CPU::update_address_bus(int address) {
return address;
}
char*::CPU::update_instructionbus(char *instruction) {
return instruction;
}
void::CPU::IR_decode() {
yesOP = true;
strncpy(opcode,IR,3); /// Seperate IR into components
opcode[3] = '\0';
strncpy(operand1,IR + 3,5);
operand1[2] = '\0';
sscanf(operand1,"%d",&operand1_cast);
if (!strcmp(opcode, "VAR")){
yesOP = false;
}
else if (!strcmp(opcode, "NOP")){
yesOP = false;
}
else if (!strcmp(opcode, "STO") || !strcmp(opcode, "JMP")){
feedback = false;
}
else if (!strcmp(opcode, "MOV") || !strcmp(opcode, "CPY")){ /// Create a new component for operand 2
feedback = true;
strncpy(operand2,IR + 5,7);
operand2[2] = '\0';
sscanf(operand2,"%d",&operand2_cast);
}
else if (!strcmp(opcode, "HLT")){
yesOP = false;
Halt = true;
cout << "HALTING" << endl;
}
else {
feedback = true;
}
IR[5] = '\0';
}
void::CPU::MDR_decode() {
MDR[7] = '\0';
strncpy(MDR_opcode,MDR,3);
MDR_opcode[3] = '\0';
strncpy(MDR_operand1,MDR + 3,5);
MDR_operand1[2] = '\0';
sscanf(MDR_operand1,"%d",&MDR_operand1_cast);
}
void::CPU::CPU_execute(int data) {
if (!strcmp(opcode, "LOD")){
ALU_X = data;
return;
}
else if (!strcmp(opcode, "ADD")){
ALU_Y = data;
Accumulator = ALU_X + ALU_Y;
return;
}
else if (!strcmp(opcode, "ADX")){
ALU_Y = data;
Accumulator = Accumulator + ALU_Y;
return;
}
else if (!strcmp(opcode, "SUB")){
ALU_Y = data;
Accumulator = ALU_X - ALU_Y;
return;
}
else if (!strcmp(opcode, "SBX")){
ALU_Y = data;
Accumulator = Accumulator - ALU_Y;
return;
}
else if (!strcmp(opcode, "DIV")){
ALU_Y = data;
Accumulator = ALU_X / ALU_Y;
return;
}
else if (!strcmp(opcode, "DVX")){
ALU_Y = data;
Accumulator = Accumulator / ALU_Y;
return;
}
else if (!strcmp(opcode, "MUL")){
ALU_Y = data;
Accumulator = ALU_X * ALU_Y;
return;
}
else if (!strcmp(opcode, "MLX")){
ALU_Y = data;
Accumulator = Accumulator * ALU_Y;
return;
}
else if (!strcmp(opcode, "MOD")){
ALU_Y = data;
Accumulator = ALU_X % ALU_Y;
return;
}
else if (!strcmp(opcode, "MDX")){
ALU_Y = data;
Accumulator = Accumulator % ALU_Y;
return;
}
}
void::CPU::CPU_execute() {
if (!strcmp(opcode, "STO")){
int i = 0; // the array index
char a[256]; // the array
while (Accumulator) { // loop till there's nothing left
a[i++] = Accumulator % 10; // assign the last digit
Accumulator /= 10; // "right shift" the number ///Reference: http://stackoverflow.com/questions/15987666/converting-integer-into-array-of-digits
}
strncpy(opcode,"VAR",3); /// Save it as a variable
operand1[1] = a[0] + '0'; /// Save the number into the operand array
operand1[0] = a[1] + '0';
strncpy(feedback_instruction,opcode,3); /// Write the variable down as an instruction to be given back
strncpy(feedback_instruction + 3,operand1,2);
feedback_instruction[5] = '\0';
return;
}
else if (!strcmp(opcode, "JMP")){
if (jmp_cnt < 10) {
PC = operand1_cast;
jmp_cnt++;
}
else {
cout << "Jump limit reached! Please check if program memory has infinite recursive jumping!" << endl;
jmp_cnt = 0;
Halt = true;
}
return;
}
}
void::CPU::clearXY() {
ALU_X, ALU_Y = 0;
}
char*::CPU::get_IR() {
return IR;
}
int::CPU::get_PC() {
return PC;
}
char*::CPU::get_opcode() {
return opcode;
}
char*::CPU::get_feedback_instruction(){
return feedback_instruction;
}
char*::CPU::get_operand1() {
return operand1;
}
char*::CPU::get_operand2() {
return operand2;
}
int::CPU::get_operand1_cast() {
return operand1_cast;
}
int::CPU::get_operand2_cast() {
return operand2_cast;
}
char*::CPU::get_MDR() {
return MDR;
}
int::CPU::get_MAR() {
return MAR;
}
char*::CPU::get_MDR_opcode() {
return MDR_opcode;
}
int::CPU::get_MDR_operand() {
return MDR_operand1_cast;
}
void::CPU::print_ALU() {
cout << "ALUX:" << ALU_X << endl;
cout << "ALUY:" << ALU_Y << endl;
cout << "ACC:" << Accumulator << endl;
return;
}
void::CPU::update(modes modes1, int address, char data[]) {
mode = modes1;
MAR = address;
strcpy(MDR,data);
MDR[7] = '\0';
}
void::CPU::CPU_RELOAD() {
Halt = false;
yesOP = true;
PC = 0;
ALU_X,ALU_Y,Accumulator = 0;
}
//
// Created by Peter Chai on 28/03/2017.
//