-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.cpp
221 lines (184 loc) · 3.32 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
#include "cpu.h"
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
void cpu::init(){
pc = 0x00;
opcode = 0;
sr = 0;
x = true;
for(int i; i < 16; i++){
R[i] = 0;
}
for(int i; i < 256; i++){
memory[i] = 0;
}
A = 0;
cache = 0;
}
void cpu::cycle(){
opcode = memory[pc];
switch(opcode & 0xF0){
case 0x00:
A = R[opcode & 0x0F];
pc += 1;
printf("Copied\n");
break;
case 0x10:
A = opcode & 0x0F;
pc += 1;
printf("Written\n");
break;
case 0x20:
R[opcode & 0x0F] = A;
pc += 1;
printf("Copied\n");
break;
case 0x30:
cache = A + R[opcode & 0x0F];
A = cache;
pc += 1;
printf("Added\n");
break;
case 0x40:
cache = A - R[opcode & 0x0F];
A = cache;
pc += 1;
printf("Subtracted\n");
break;
case 0x50:
cache = A * R[opcode & 0x0F];
A = cache;
pc += 1;
printf("Multiplied");
break;
case 0x60:
cache = A / R[opcode & 0x0F];
A = cache;
pc += 1;
printf("Divided");
break;
case 0x70:
pc = opcode & 0x0F;
printf("Jumped");
break;
case 0x80:
if (A >= 0){
pc = opcode & 0x0F;
printf("Jumped");
}
else{
pc += 1;
printf("Continued");
}
break;
case 0x90:
if (A > 0){
pc = opcode & 0x0F;
printf("Jumped");
}
else{
pc += 1;
printf("Continued");
}
break;
case 0xA0:
if (A <= 0){
pc = opcode & 0x0F;
printf("Jumped");
}
else{
pc += 1;
printf("Continued");
}
break;
case 0xB0:
if (A < 0){
pc = opcode & 0x0F;
printf("Jumped");
}
else{
pc += 1;
printf("Continued");
}
break;
case 0xC0:
if (A == 0){
pc = opcode & 0x0F;
printf("Jumped");
}
else{
pc += 1;
printf("Continued");
}
break;
case 0xD0:
if (A != 0){
pc = opcode & 0x0F;
printf("Jumped");
}
else{
pc += 1;
printf("Continued");
}
break;
case 0xE0:
printf("End\n");
x = false;
break;
case 0xF0:
printf("Output: ");
cout << static_cast<unsigned>(A) << endl;
pc += 1;
break;
default:
printf ("Unknown opcode: 0x%X\n", opcode);
}
}
bool cpu::load(const char * filename)
{
init();
printf("Loading: %s\n", filename);
// Open file
FILE * pFile = fopen(filename, "rb");
if (pFile == NULL)
{
fputs ("File error", stderr);
return false;
}
// Check file size
fseek(pFile , 0 , SEEK_END);
long lSize = ftell(pFile);
rewind(pFile);
printf("Filesize: %d\n", (int)lSize);
// Allocate memory to contain the whole file
char * buffer = (char*)malloc(sizeof(char) * lSize);
if (buffer == NULL)
{
fputs ("Memory error", stderr);
return false;
}
// Copy the file into the buffer
size_t result = fread (buffer, 1, lSize, pFile);
if (result != lSize)
{
fputs("Reading error",stderr);
return false;
}
// Copy buffer to memory
if((256) > lSize)
{
for(int i = 0; i < lSize; ++i)
memory[i] = buffer[i];
while(x == true){
cycle();
}
}
else
printf("Error: ROM too big for memory");
// Close file, free buffer
fclose(pFile);
free(buffer);
return true;
}