-
Notifications
You must be signed in to change notification settings - Fork 32
/
emulator.c
140 lines (125 loc) · 4.08 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
/*
* 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>
#include "emulator.h"
extern u16 *disassemble(u16 *pc, char *out);
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);
}
}
static u8 skiptable[32] = { /* operand forms that advance pc */
[0x10] = 1, [0x11] = 1, [0x12] = 1, [0x13] = 1,
[0x14] = 1, [0x15] = 1, [0x16] = 1, [0x17] = 1,
[0x1E] = 1, [0x1F] = 1,
};
void dcpu_skip(struct dcpu *d) {
u16 op = d->m[d->pc++];
d->pc += skiptable[op >> 10];
if ((op & 15) == 0)
d->pc += skiptable[(op >> 4) & 31];
}
void dcpu_step(struct dcpu *d) {
u16 op = d->m[d->pc++];
u16 dst;
u32 res;
u16 a, b, *aa;
if ((op & 0xF) == 0) goto extended;
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; d->ov = res >> 16; break;
case 0x3: res = a - b; d->ov = res >> 16; break;
case 0x4: res = a * b; d->ov = res >> 16; break;
case 0x5: if (b) { res = a / b; } else { res = 0; } d->ov = res >> 16; break;
case 0x6: if (b) { res = a % b; } else { res = 0; } break;
case 0x7: res = a << b; d->ov = res >> 16; break;
case 0x8: res = a >> b; d->ov = res >> 16; break;
case 0x9: res = a & b; break;
case 0xA: res = a | b; break;
case 0xB: res = a ^ b; break;
case 0xC: if (a!=b) dcpu_skip(d); return;
case 0xD: if (a==b) dcpu_skip(d); return;
case 0xE: if (a<=b) dcpu_skip(d); return;
case 0xF: if ((a&b)==0) dcpu_skip(d); return;
}
if (dst < 0x1f) *aa = res;
return;
extended:
a = *dcpu_opr(d, op >> 10);
switch ((op >> 4) & 0x3F) {
case 0x01:
d->m[--(d->sp)] = d->pc;
d->pc = a;
return;
default:
fprintf(stderr, "< ILLEGAL OPCODE >\n");
exit(0);
}
}