/
dcpu16.c
331 lines (299 loc) · 7.33 KB
/
dcpu16.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
@author Brian Carrigan
@date 1/31/2013
@file dcpu16.c
Email brian.c.carrigan@gmail.com
This file is part of the DCPU C Emulator LPC-Library.
The DCPU C Emulator LPC-Library is free software: you
can redistribute it and/or modify it under the terms of the GNU
General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later
version.
The DCPU C Emulator LPC-Library is distributed in
the hope that it will be useful, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with the DCPU C Emulator LPC-Library. If not,
see <http://www.gnu.org/licenses/>.
*/
#include "dcpu16.h"
// Branch/Add test
//unsigned short DCPU_Memory[256] = {0x7DE1, 0x0003, 0x81C1, 0x0000};
// Loop Test
//unsigned short DCPU_Memory[256] = {0xFC01, 0x8081, 0xA48E, 0x99C1, 0x8482, 0x89C1, 0x81C1};
// Notch's Example 196608Kb memory needed.
unsigned short DCPU_Memory[65536] = { 0x7c01, 0x0030, 0x7de1, 0x1000, 0x0020, 0x7803, 0x1000, 0xc00d,
0x7dc1, 0x001a, 0xa861, 0x7c01, 0x2000, 0x2161, 0x2000, 0x8463,
0x806d, 0x7dc1, 0x000d, 0x9031, 0x7c10, 0x0018, 0x7dc1, 0x001a,
0x9037, 0x61c1, 0x7dc1, 0x001a, 0x0000, 0x0000, 0x0000, 0x0000 };
unsigned short A, B, C, X, Y, Z, I, J, O, PC, SP;
unsigned short *REGISTER_LOCATION[8] = {&A, &B, &C, &X, &Y, &Z, &I, &J};
unsigned char waitState;
// Initialize the DCPU-16
void DCPU_Init(void)
{
A = 0;
B = 0;
C = 0;
X = 0;
Y = 0;
Z = 0;
I = 0;
J = 0;
O = 0;
PC = 0;
SP = 0;
waitState = 0;
}
/**
* @brief Goes through cycles.
*
*/
void DCPU_Run(void)
{
while(1)
{
// Wait for 100kHz timer
DCPU_Cycle();
}
} // Function
/**
* @brief Emulates a single cycle of the DCPU-16.
*
* This is the meat of the project. If you want it to emulate the DCPU as laid out
* by Notch, run this at function at 100kHz.
*/
void DCPU_Cycle(void)
{
// If there are no wait states from multi cycle instructions, execute next code.
if(waitState == 0)
{
// Temporary Variables
unsigned short *a, *b, a_n, b_n, a_val, b_val;
unsigned short currentInstruction;
unsigned short opCode;
// Fetch Instruction, Increment PC
currentInstruction = DCPU_Memory[PC];
PC++;
// Get A/B/OP Code
a_n = (currentInstruction & 0x03F0) >> 4;
b_n = (currentInstruction & 0xFC00) >> 10;
opCode = currentInstruction & 0xF;
// Find the address of A
if(a_n < 0x08)
{
a = REGISTER_LOCATION[a_n];
} else if (a_n < 0x10) {
a = &DCPU_Memory[*REGISTER_LOCATION[a_n - 0x08]];
} else if (a_n < 0x18) {
a = &DCPU_Memory[*REGISTER_LOCATION[a_n - 0x10] + DCPU_Memory[PC]];
PC++;
waitState++;
} else if (a_n == 0x18) {
a = &DCPU_Memory[SP];
SP++;
} else if (a_n == 0x19) {
a = &DCPU_Memory[SP];
} else if (a_n == 0x1A) {
SP--;
a = &DCPU_Memory[SP];
} else if (a_n == 0x1B) {
a = &SP;
} else if (a_n == 0x1C) {
a = &PC;
} else if (a_n == 0x1D) {
a = &O;
} else if (a_n == 0x1E) {
a = &DCPU_Memory[DCPU_Memory[PC]];
PC++;
waitState++;
} else if (a_n == 0x1F) {
a = &DCPU_Memory[PC];
PC++;
waitState++;
} else {
a_val = a_n - 0x20;
a = &a_val;
}
// Find the address of B
if(b_n < 0x08)
{
b = REGISTER_LOCATION[b_n];
} else if (b_n < 0x10) {
b = &DCPU_Memory[*REGISTER_LOCATION[b_n - 0x08]];
} else if (b_n < 0x18) {
b = &DCPU_Memory[*REGISTER_LOCATION[b_n - 0x10] + DCPU_Memory[PC]];
PC++;
waitState++;
} else if (b_n == 0x18) {
b = &DCPU_Memory[SP];
SP++;
} else if (b_n == 0x19) {
b = &DCPU_Memory[SP];
} else if (b_n == 0x1A) {
SP--;
b = &DCPU_Memory[SP];
} else if (b_n == 0x1B) {
b = &SP;
} else if (b_n == 0x1C) {
b = &PC;
} else if (b_n == 0x1D) {
b = &O;
} else if (b_n == 0x1E) {
b = &DCPU_Memory[DCPU_Memory[PC]];
PC++;
waitState++;
} else if (b_n == 0x1F) {
b = &DCPU_Memory[PC];
PC++;
waitState++;
} else {
b_val = b_n - 0x20;
b = &b_val;
}
// Carry out instruction
switch(opCode)
{
// Special!
case 0x00:
switch(a_n)
{
// JSR
case 0x01:
SP--;
DCPU_Memory[SP] = PC;
PC = *b;
waitState++;
break;
}
break;
// Set - 1 Cycle
case 0x01:
*a = *b;
break;
// Add - 2 Cycle
case 0x02:
unsigned int addition;
waitState++;
addition = *a + *b;
*a = addition & 0xFFFF;
O = (addition & 0xFFFF0000) >> 16;
break;
// Sub - 2 Cycle
case 0x03:
waitState++;
*a = *a - *b;
break;
// Mul - 2 Cycle
case 0x04:
waitState++;
*a = *a * *b;
break;
// Div - 2 Cycle
case 0x05:
waitState++;
*a = *a / *b;
break;
// Mod - 2 Cycle
case 0x06:
unsigned int multiplication;
waitState++;
multiplication = *a * *b;
*a = addition & 0xFFFF;
O = (addition & 0xFFFF0000) >> 16;
// SHL - 2 Cycle
case 0x07:
waitState++;
*a = *a << *b;
break;
// SHR - 2 Cycle
case 0x08:
waitState++;
*a = *a >> *b;
break;
// AND - 1 Cycle
case 0x09:
*a = *a & *b;
break;
// BOR - 1 Cycle
case 0x0a:
*a = *a | *b;
break;
// XOR - 1 Cycle
case 0x0b:
*a = *a ^ *b;
break;
// IFE - 2 Cycles
case 0x0c:
waitState++;
if(*a != *b)
{
// If it fails, wait one cycle and skip next instruction.
unsigned short a_test, b_test;
a_test = (DCPU_Memory[PC] & 0x03F0) >> 4;
b_test = (DCPU_Memory[PC] & 0xFC00) >> 10;
if((a_test >= 0x10 && a_test <0x18) || a_test == 0x1E || a_test == 0x1F)
PC++;
if((b_test >= 0x10 && b_test <0x18) || b_test == 0x1E || b_test == 0x1F)
PC++;
PC++;
waitState++;
}
break;
// IFN - 2 Cycles
case 0x0d:
waitState++;
if(*a == *b)
{
// If it fails, wait one cycle and skip next instruction.
unsigned short a_test, b_test;
a_test = (DCPU_Memory[PC] & 0x03F0) >> 4;
b_test = (DCPU_Memory[PC] & 0xFC00) >> 10;
if((a_test >= 0x10 && a_test <0x18) || a_test == 0x1E || a_test == 0x1F)
PC++;
if((b_test >= 0x10 && b_test <0x18) || b_test == 0x1E || b_test == 0x1F)
PC++;
PC++;
waitState++;
}
break;
// IFG - 2 Cycles
case 0x0e:
waitState++;
if(*a <= *b)
{
// If it fails, wait one cycle and skip next instruction.
unsigned short a_test, b_test;
a_test = (DCPU_Memory[PC] & 0x03F0) >> 4;
b_test = (DCPU_Memory[PC] & 0xFC00) >> 10;
if((a_test >= 0x10 && a_test <0x18) || a_test == 0x1E || a_test == 0x1F)
PC++;
if((b_test >= 0x10 && b_test <0x18) || b_test == 0x1E || b_test == 0x1F)
PC++;
PC++;
waitState++;
}
break;
// IFB - 2 Cycles
case 0x0f:
waitState++;
if((*a & *b) == 0)
{
// If it fails, wait one cycle and skip next instruction.
unsigned short a_test, b_test;
a_test = (DCPU_Memory[PC] & 0x03F0) >> 4;
b_test = (DCPU_Memory[PC] & 0xFC00) >> 10;
if((a_test >= 0x10 && a_test <0x18) || a_test == 0x1E || a_test == 0x1F)
PC++;
if((b_test >= 0x10 && b_test <0x18) || b_test == 0x1E || b_test == 0x1F)
PC++;
PC++;
waitState++;
}
break;
}
} else {
waitState--;
}
}