forked from dcpu16/dcpu16-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emulator.c
201 lines (180 loc) · 5.01 KB
/
emulator.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
/*
* Copyright (c) 2012, Brian Swetland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/* A DCPU-16 Implementation */
/* DCPU-16 Spec is Copyright 2012 Mojang */
/* http://0x10c.com/doc/dcpu-16.txt */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
typedef uint16_t u16;
typedef uint32_t u32;
extern u16 *disassemble(u16 *pc, char *out);
struct dcpu {
u16 r[8];
u16 pc;
u16 sp;
u16 ov;
u16 skip;
u16 m[65536];
};
static u16 lit[0x20] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
};
u16 *dcpu_opr(struct dcpu *d, u16 code) {
switch (code) {
case 0x00: case 0x01: case 0x02: case 0x03:
case 0x04: case 0x05: case 0x06: case 0x07:
return d->r + code;
case 0x08: case 0x09: case 0x0a: case 0x0b:
case 0x0c: case 0x0d: case 0x0e: case 0x0f:
return d->m + d->r[code & 7];
case 0x10: case 0x11: case 0x12: case 0x13:
case 0x14: case 0x15: case 0x16: case 0x17:
return d->m + ((d->r[code & 7] + d->m[d->pc++]) & 0xffff);
case 0x18:
return d->m + d->sp++;
case 0x19:
return d->m + d->sp;
case 0x1a:
return d->m + (--(d->sp));
case 0x1b:
return &d->sp;
case 0x1c:
return &d->pc;
case 0x1d:
return &d->ov;
case 0x1e:
return d->m + d->m[d->pc++];
case 0x1f:
return d->m + d->pc++;
default:
return lit + (code & 0x1F);
}
}
void dcpu_step(struct dcpu *d) {
u16 op = d->m[d->pc++];
u16 dst;
u32 res;
u16 a, b, *aa;
if ((op & 0xF) == 0) {
switch ((op >> 4) & 0x3F) {
case 0x01:
a = *dcpu_opr(d, op >> 10);
if (d->skip) {
d->skip = 0;
} else {
d->m[--(d->sp)] = d->pc;
d->pc = a;
}
return;
default:
fprintf(stderr, "< ILLEGAL OPCODE >\n");
exit(0);
}
}
aa = dcpu_opr(d, dst = (op >> 4) & 0x3F);
a = *aa;
b = *dcpu_opr(d, op >> 10);
switch (op & 0xF) {
case 0x1: res = b; break;
case 0x2: res = a + b; break;
case 0x3: res = a - b; break;
case 0x4: res = a * b; break;
case 0x5: if (b) { res = a / b; } else { res = 0; } break;
case 0x6: if (b) { res = a % b; } else { res = 0; } break;
case 0x7: res = a << b; break;
case 0x8: res = a >> b; break;
case 0x9: res = a & b; break;
case 0xA: res = a | b; break;
case 0xB: res = a ^ b; break;
case 0xC: res = (a==b); break;
case 0xD: res = (a!=b); break;
case 0xE: res = (a>b); break;
case 0xF: res = ((a&b)!=0); break;
}
if (d->skip) {
d->skip = 0;
return;
}
switch (op & 0xF) {
case 0x2: case 0x3: case 0x4: case 0x5: case 0x7: case 0x8:
d->ov = res >> 16;
case 0x1: case 0x6: case 0x9: case 0xA: case 0xB:
if (dst < 0x1f) *aa = res;
break;
case 0xC: case 0xD: case 0xE: case 0xF:
d->skip = !res;
}
}
void dumpheader(void) {
fprintf(stderr,
"PC SP OV SKIP A B C X Y Z I J Instruction\n"
"---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- -----------\n");
}
void dumpstate(struct dcpu *d) {
char out[128];
disassemble(d->m + d->pc, out);
fprintf(stderr,
"%04x %04x %04x %04x %04x %04x %04x %04x %04x %04x %04x %04x %s\n",
d->pc, d->sp, d->ov, d->skip,
d->r[0], d->r[1], d->r[2], d->r[3],
d->r[4], d->r[5], d->r[6], d->r[7],
out);
}
void load(struct dcpu *d, const char *fn) {
FILE *fp;
char buf[128];
u16 n = 0;
if (!(fp = fopen(fn, "r"))) {
fprintf(stderr, "cannot open: %s\n", fn);
exit(1);
}
while (!feof(fp) && fgets(buf, 128, fp)) {
if (!isalnum(buf[0]))
continue;
d->m[n++] = strtoul(buf, 0, 16);
}
fprintf(stderr,"< LOADED %d WORDS >\n", n);
fclose(fp);
}
int main(int argc, char **argv) {
struct dcpu d;
memset(&d, 0, sizeof(d));
load(&d, argc > 1 ? argv[1] : "out.hex");
dumpheader();
for (;;) {
dumpstate(&d);
dcpu_step(&d);
}
return 0;
}