forked from mappum/DCPU-16
/
cpu.js
588 lines (509 loc) · 16.9 KB
/
cpu.js
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
(function (root) {
"use strict";
function formatWord(word) {
if(typeof word === 'undefined') {
return 'null';
}
word &= 0xFFFF;
word = word.toString(16);
while(word.length < 4) {
word = '0' + word;
}
return word;
}
function getSigned(word) {
var sign = word >> 15;
var value = word & 0x7fff;
return value + (sign * -0xffff);
}
var OPCODES = {
//assembler directives
'DAT': {id: null, cost: 0, args: Infinity},
//basic ops
'SET': {id: 0x01, cost: 1, args: 2},
'ADD': {id: 0x02, cost: 2, args: 2},
'SUB': {id: 0x03, cost: 2, args: 2},
'MUL': {id: 0x04, cost: 2, args: 2},
'MLI': {id: 0x05, cost: 2, args: 2},
'DIV': {id: 0x06, cost: 3, args: 2},
'DVI': {id: 0x07, cost: 3, args: 2},
'MOD': {id: 0x08, cost: 3, args: 2},
'AND': {id: 0x09, cost: 1, args: 2},
'BOR': {id: 0x0a, cost: 1, args: 2},
'XOR': {id: 0x0b, cost: 1, args: 2},
'SHR': {id: 0x0c, cost: 2, args: 2},
'ASR': {id: 0x0d, cost: 2, args: 2},
'SHL': {id: 0x0e, cost: 2, args: 2},
'IFB': {id: 0x10, cost: 2, args: 2},
'IFC': {id: 0x11, cost: 2, args: 2},
'IFE': {id: 0x12, cost: 2, args: 2},
'IFN': {id: 0x13, cost: 2, args: 2},
'IFG': {id: 0x14, cost: 2, args: 2},
'IFA': {id: 0x15, cost: 2, args: 2},
'IFL': {id: 0x16, cost: 2, args: 2},
'IFU': {id: 0x17, cost: 2, args: 2},
'ADX': {id: 0x1a, cost: 3, args: 2},
'SUX': {id: 0x1b, cost: 3, args: 2},
//non-basic ops
'JSR': {id: 0x20, cost: 3, args: 1},
'BRK': {id: 0x40, cost: 0, args: 0},
'INT': {id: 0x100, cost: 4, args: 1},
'ING': {id: 0x120, cost: 1, args: 1},
'INS': {id: 0x140, cost: 1, args: 1},
'HWN': {id: 0x200, cost: 2, args: 1},
'HWQ': {id: 0x220, cost: 4, args: 1},
'HWI': {id: 0x240, cost: 4, args: 1}
};
var REGISTER_NAMES = ['a', 'b', 'c', 'x', 'y', 'z', 'i', 'j'];
var FLAG_LITERAL = 0x10000;
var CPU = function CPU() {
// Memory storage
this.mem = [];
this.ramSize = 0x10000;
//ram size in words
this.wordSize = 2;
//word size in bytes
this.maxValue = Math.pow(2, this.wordSize * 8) - 1;
//max value of words
this.mem.length = this.ramSize;
// device list
this._devices = [];
// Execution parameters
this.throttled = true; //whether or not to control speed
this.speedScale = 14; //how fast to auto-adjust execution speed
// (higher means smoother speeds, lower means more accuracy)
this.speed = 100000; //speed in hz
this.loopBatch = 1600; //the number of loops to execute at a time in run
this._stop = false;
this._endListeners = [];
this.clear();
};
CPU.prototype = {
nextWord: function() {
var pc = this.get('pc');
var word = this.get(pc);
this.set('pc', pc + 1);
this.cycle++;
return word;
},
nextInstruction: function() {
var word = this.nextWord();
if((word & 0x1f) === 0) {
return {
opcode: word & 0x3ff,
a: this.addressFor((word & 0xfc00) >> 10)
};
} else {
return {
opcode: word & 0x1f,
b: this.addressFor((word & 0x3e0) >> 5),
a: this.addressFor((word & 0xfc00) >> 10, true)
};
}
},
// Returns the memory address at which the value resides.
addressFor: function(value, a) {
var r, address;
// Handle the simple register cases first
if(value <= 0x17 || value === 0x1a) {
r = REGISTER_NAMES[value % 8] || 'stack';
if(0x00 <= value && value <= 0x07) {
address = r;
} else if(0x08 <= value && value <= 0x0F) {
address = this.get(r);
} else {
address = (this.nextWord() + this.get(r)) & 0xffff;
}
return address;
}
// Encoded literals
if(value >= 0x20 && value <= 0x3f) {
return (((value - 0x21) & 0xffff) | FLAG_LITERAL);
}
// Other kinds of values
switch(value) {
// stack pointer
case 0x18:
if(a) {
var pre = this.get('stack');
this.set('stack', pre + 1);
return pre;
} else {
var output = this.get('stack') - 1;
this.set('stack', output);
return output;
}
case 0x19:
return this.get('stack');
// other registers
case 0x1b:
return 'stack';
case 0x1c:
return 'pc';
case 0x1d:
return 'ex';
// extended instruction values
case 0x1e: // as address
return this.nextWord();
case 0x1f: // as literal
return this.nextWord() | FLAG_LITERAL;
default:
throw new Error('Encountered unknown argument type 0x' + value.toString(16));
}
},
get: function(key) {
if (typeof key === 'number') {
// If the key is flagged as a literal, return the value immediately.
if (key & FLAG_LITERAL) return key & this.maxValue;
key &= this.maxValue;
}
return this.mem[key];
},
// Assigns 'value' into the memory location referenced by 'key'
set: function(key, value) {
if (typeof key === 'number') {
// If the key is flagged as a literal, don't set.
if (key & FLAG_LITERAL) return;
key &= this.maxValue;
}
value &= this.maxValue;
this.mem[key] = value;
},
step: function() {
var insn, aVal, aRaw, bVal, result;
// Fetch the instruction
insn = this.nextInstruction();
this.cycle += OPCODE[insn.opcode].cost || 0;
// Read the arguments
if(insn.opcode !== 0) {
bVal = this.get(insn.b);
}
aVal = this.get(insn.a);
switch (insn.opcode) {
// SET
case 0x1:
this.set(insn.b, aVal);
break;
// ADD
case 0x2:
result = bVal + aVal;
this.set(insn.b, result);
this.set('ex', (result > this.maxValue) ? 0x0001 : 0x0000);
break;
// SUB
case 0x3:
result = bVal - aVal;
this.set(insn.b, result);
this.set('ex', (result < 0) ? this.maxValue : 0x0000);
break;
// MUL
case 0x4:
result = bVal * aVal;
this.set(insn.b, result);
this.set('ex', result >> 16);
break;
// MLI
case 0x5:
result = getSigned(bVal) * getSigned(aVal);
this.set(insn.b, result);
this.set('ex', result >> 16);
break;
// DIV
case 0x6:
if(aVal === 0) {
this.set(insn.b, 0x0000);
this.set('ex', 0x0000);
} else {
this.set(insn.b, Math.floor(bVal / aVal));
this.set('ex', (bVal << 16) / aVal);
}
break;
// DVI
case 0x7:
if(aVal === 0) {
this.set(insn.b, 0x0000);
this.set('ex', 0x0000);
} else {
this.set(insn.b, Math.floor(getSigned(bVal) / getSigned(aVal)));
this.set('ex', (getSigned(bVal) << 16) / getSigned(aVal));
}
break;
// MOD
case 0x8:
this.set(insn.b, (aVal === 0) ? 0x0000 : bVal % aVal);
break;
// AND
case 0x9:
this.set(insn.b, bVal & aVal);
break;
// BOR
case 0xa:
this.set(insn.b, bVal | aVal);
break;
// XOR
case 0xb:
this.set(insn.b, bVal ^ aVal);
break;
// SHR
case 0xc:
this.set(insn.b, bVal >>> aVal);
this.set('ex', (bVal << 16) >> aVal);
break;
// ASR
case 0xd:
this.set(insn.b, bVal >> getSigned(aVal));
this.set('ex', (bVal << 16) >>> aVal);
break;
// SHL
case 0xe:
this.set(insn.b, bVal << aVal);
this.set('ex', (bVal << aVal) >> 16);
break;
// IFB
case 0x10:
if((bVal & aVal) === 0) {
this.nextInstruction(); // skip
this.cycle += 1;
}
break;
// IFC
case 0x11:
if(bVal & aVal) {
this.nextInstruction(); // skip
this.cycle += 1;
}
break;
// IFE
case 0x12:
if(bVal !== aVal) {
this.nextInstruction(); // skip
this.cycle += 1;
}
break;
// IFN
case 0x13:
if(bVal === aVal) {
this.nextInstruction(); // skip
this.cycle += 1;
}
break;
// IFG
case 0x14:
if(bVal <= aVal) {
this.nextInstruction(); // skip
this.cycle += 1;
}
break;
// IFA
case 0x15:
if(getSigned(bVal) <= getSigned(aVal)) {
this.nextInstruction(); // skip
this.cycle += 1;
}
break;
// IFL
case 0x16:
if(bVal >= aVal) {
this.nextInstruction(); // skip
this.cycle += 1;
}
break;
// IFU
case 0x17:
if(getSigned(bVal) >= getSigned(aVal)) {
this.nextInstruction(); // skip
this.cycle += 1;
}
break;
// JSR
case 0x20:
var stack = (this.get('stack') - 1) & this.maxValue;
this.set('stack', stack);
this.set(stack, this.get('pc'));
this.set('pc', bVal);
break;
// BRK (non-standard)
case 0x40:
this.stop();
break;
// INT
case 0x100:
if(this.mem.ia !== 0) {
this.set('push', this.mem.pc);
this.set('push', this.mem.a);
this.set('pc', this.mem.ia);
this.set('a', aVal);
}
break;
// ING
case 0x120:
this.set(this.mem[aVal], this.mem.ia);
break;
// INS
case 0x140:
this.set('ia', aVal);
break;
// HWN
case 0x200:
this.set(instr.a, this._devices.length);
break;
// HWQ
case 0x220:
if(this._devices[aVal]) {
this.set('a', this._devices[aVal].type >> 16);
this.set('b', this._devices[aVal].type & 0xff);
this.set('c', this._devices[aVal].revision);
this.set('x', this._devices[aVal].manufacturer >> 16);
this.set('y', this._devices[aVal].manufacturer & 0xff);
} else {
this.set('a', 0);
this.set('b', 0);
this.set('c', 0);
this.set('x', 0);
this.set('y', 0);
}
break;
// HWI
case 0x240:
if(this._devices[aVal] && this._devices[aVal].onInterrupt) {
this._devices[aVal].onInterrupt();
}
break;
default:
throw new Error('Encountered invalid opcode 0x' + insn.opcode.toString(16));
}
},
load: function(binary, origin) {
if (!origin || typeof origin !== "number") {
origin = 0x0000;
}
for (var i = 0; i < binary.length; ++i) {
this.set((origin+i) & this.maxValue, binary[i]);
}
},
run: function(onLoop) {
var $this = this, startTime = new Date().getTime();
var stackCounter = 0;
this.running = true;
function loop() {
if(!$this._stop && $this.running) {
for(var i = 0; i < $this.loopBatch; i++) {
if(!$this._stop && $this.running) {
$this.step();
} else if($this.running) {
$this.end();
}
}
if($this.throttled) {
var throttledTime = $this.cycle / ($this.speed / 1000),
realTime = new Date().getTime() - startTime;
setTimeout(loop, Math.round(throttledTime - realTime));
} else {
if( typeof process !== 'undefined' && process.nextTick) {
process.nextTick(loop);
} else {
setTimeout(loop, 0);
}
}
if(onLoop) {
setTimeout(onLoop, 0);
}
} else if($this.running) {
$this.end();
}
}
loop();
},
stop: function() {
if(this.running)
this._stop = true;
},
clear: function() {
var i = 0, _len;
for( _len = REGISTER_NAMES.length; i < _len; ++i) {
this.mem[REGISTER_NAMES[i]] = 0;
}
for( i = 0, _len = this.ramSize; i < _len; ++i) {
this.mem[i] = 0;
}
this.mem.pc = 0;
this.mem.stack = 0;
this.mem.ex = 0;
this.mem.ia = 0;
this.cycle = 0;
this.running = false;
this.timer = 0;
},
addDevice: function(device) {
if(device && this._devices.length < 65536
&& typeof device.type !== 'undefined'
&& typeof device.revision !== 'undefined'
&& typeof device.manufacturer !== 'undefined')
this._devices.push(device);
},
end: function() {
var i, _len = this._endListeners.length;
for( i = 0; i < _len; ++i) {
this._endListeners[i]();
}
this._stop = false;
this.running = false;
},
//EVENT LISTENER REGISTRATION
onEnd: function(callback) {
this._endListeners.push(callback);
},
getDump: function() {
var output = '', populated, i, j;
output += '==== REGISTERS: ====\n';
output += 'A: ' + formatWord(this.mem.a) + '\n';
output += 'B: ' + formatWord(this.mem.b) + '\n';
output += 'C: ' + formatWord(this.mem.c) + '\n';
output += 'X: ' + formatWord(this.mem.x) + '\n';
output += 'Y: ' + formatWord(this.mem.y) + '\n';
output += 'Z: ' + formatWord(this.mem.z) + '\n';
output += 'I: ' + formatWord(this.mem.i) + '\n';
output += 'J: ' + formatWord(this.mem.j) + '\n\n';
output += 'PC: ' + formatWord(this.mem.pc) + '\n';
output += 'SP: ' + formatWord(this.mem.stack) + '\n';
output += 'EX: ' + formatWord(this.mem.ex) + '\n\n';
output += 'CPU CYCLES: ' + this.cycle + '\n\n';
output += '======= RAM: =======';
for( i = 0; i < this.ramSize; i += 8) {
populated = false;
for( j = 0; j < 8; j++) {
if(this.mem[i + j] || this.mem.pc === i + j || this.mem.stack === i + j) {
populated = true;
break;
}
}
if(populated) {
output += '\n' + formatWord(i) + ':';
for( j = 0; j < 8; j++) {
if(this.mem.pc === i + j) {
output += '[';
} else if(this.mem.pc === i + j - 1) {
output += ']';
} else if(this.mem.stack === i + j) {
output += '*';
} else if(this.mem.stack === i + j - 1) {
output += '*';
} else {
output += ' ';
}
output += formatWord(this.mem[i + j]);
}
}
}
return output;
}
};
CPU.FLAG_LITERAL = FLAG_LITERAL;
CPU.REGISTER_NAMES = REGISTER_NAMES;
CPU.OPCODES = OPCODES;
if (typeof module === 'undefined') {
(root.DCPU16 = (root.DCPU16 || {})).CPU = CPU;
} else {
module.exports = CPU;
}
})(this);