/
dcpuopt.c
213 lines (186 loc) · 4.68 KB
/
dcpuopt.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
/*
DCPU-16 emulator in C. Marginally inefficient.
Written by Kang Seonghoon (http://mearie.org/, @senokay). Public Domain.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
typedef uint16_t word;
typedef uint32_t xword;
#define CASEx8(x) \
case (x)+0: case (x)+1: case (x)+2: case (x)+3: \
case (x)+4: case (x)+5: case (x)+6: case (x)+7
int wc;
word pc, sp, of, reg[8], mem[1<<16];
// a mapping used for optimizing decoder part
const int remap[64] = {
8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
10, 10, 10, 10, 10, 10, 10, 10, 0, 1, 2, 3, 4, 5, 6, 7,
11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
};
word bitmap[4096];
void init_bitmap(void)
{
int a, b;
for (a = 0; a < 64; ++a) {
for (b = 0; b < 64; ++b) {
bitmap[a*64+b] = (remap[a] * 12 + remap[b] - a * 64 - b) * 16;
}
}
}
// sink is used for literals, which of course do not have an address but
// we need it anyway for consistency. the sink is discarded later.
word *get(word v, word *sink)
{
switch (v) {
CASEx8(0x00): return ®[v];
CASEx8(0x08): return &mem[reg[v-0x08]];
CASEx8(0x10): ++wc; return &mem[(word)(reg[v-0x10] + mem[pc++])];
case 0x18: return &mem[sp++];
case 0x19: return &mem[sp];
case 0x1a: return &mem[(word)--sp];
case 0x1b: return &sp;
case 0x1c: return &pc;
case 0x1d: return &of;
case 0x1e: ++wc; return &mem[mem[pc++]];
case 0x1f: ++wc; *sink = mem[pc++]; return sink; // literal
CASEx8(0x20):
CASEx8(0x28):
CASEx8(0x30):
CASEx8(0x3f): *sink = v-0x20; return sink; // literal
default: abort();
}
}
// the number of words in this instruction (1 to 3)
int length(word c)
{
static const uint32_t hasnext = 0x00008f00;
int anext = (hasnext >> ((c >> 5) & 31)) & 1;
int bnext = (hasnext >> (c >> 11)) & 1;
return 1 + anext + bnext;
}
void tick(void)
{
word c = mem[pc++];
word cmasked = c + bitmap[c >> 4];
xword t, u;
word v, sk;
switch (cmasked) {
#include "dcpuopt-cases.h"
default: {// now we do the slow things.
word aa = (c >> 4) & 63, bb = c >> 10;
word b0;
switch (aa) {
case 0x00: // extended
fprintf(stderr, "Reserved opcode %d:0:0\n", (int)bb);
wc += 1;
break;
case 0x01: // JSR
v = *get(bb, &b0);
mem[--sp] = pc;
pc = v;
wc += 2;
break;
default:
if (aa >= 0x02 && aa <= 0x3f) {
fprintf(stderr, "Reserved opcode %d:0\n", (int)aa);
wc += 1;
break;
}
abort();
}
}}
}
void init(void)
{
init_bitmap();
wc = 0;
pc = 0x0000;
sp = 0x0000; // well, the first PUSH is made to 0xffff, so...
of = 0x0000;
memset(reg, 0, sizeof reg);
}
int dumpv(word v, word pc)
{
static const char ops[8] = "ABCXYZIJ";
switch (v) {
CASEx8(0x00): putchar(ops[v]); return 0;
CASEx8(0x08): printf("[%c]", ops[v-0x08]); return 0;
CASEx8(0x10): printf("[%c%+d]", ops[v-0x10], (int)mem[pc]); return 1;
case 0x18: printf("[SP++]"); return 0;
case 0x19: printf("[SP]"); return 0;
case 0x1a: printf("[--SP]"); return 0;
case 0x1b: printf("SP"); return 0;
case 0x1c: printf("PC"); return 0;
case 0x1d: putchar('O'); return 0;
case 0x1e: printf("[%#x]", (int)mem[pc]); return 1;
case 0x1f: printf("%#x", (int)mem[pc]); return 1;
CASEx8(0x20):
CASEx8(0x28):
CASEx8(0x30):
CASEx8(0x38): printf("%d", (int)(v-0x20)); return 0;
default: abort();
}
}
word dump(word pc)
{
word oldpc = pc;
word c = mem[pc++];
word op = c & 15, a = (c >> 4) & 63, b = c >> 10;
printf("%04x: ", oldpc);
if (op) {
static const char opcodes[16][4] = {
"???", "SET", "ADD", "SUB", "MUL", "DIV", "MOD", "SHL",
"SHR", "AND", "BOR", "XOR", "IFE", "IFN", "IFG", "IFB",
};
printf("%s ", opcodes[op]);
pc += dumpv(a, pc);
printf(", ");
pc += dumpv(b, pc);
printf("\n");
} else {
switch (a) {
case 0x00:
printf("; Reserved opcode %d:0:0\n", (int)b);
break;
case 0x01:
printf("JSR ");
pc += dumpv(b, pc);
printf("\n");
break;
default:
if (a >= 0x02 && a <= 0x3f) {
printf("; Reserved opcode %d:0\n", (int)a);
break;
}
abort();
}
}
return pc - oldpc;
}
void stat(void)
{
printf("PC=%04x SP=%04x O=%04x Reg=%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x (t=%d)\n",
pc, sp, of, reg[0], reg[1], reg[2], reg[3], reg[4], reg[5], reg[6], reg[7], wc);
dump(pc);
}
int main(void) {
word initmem[] = {
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,
};
int i;
for (i = 0; i < (int)(sizeof(initmem) / sizeof(*initmem)); ++i) {
mem[i] = initmem[i];
}
init();
while (wc < 100) {
stat();
tick();
}
return 0;
}