summaryrefslogtreecommitdiff
path: root/src/vm/instruction.js
blob: 146a88b738eb82fa86bfc8594c21d87708383800 (plain)
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
'use strict';

const { AddrMode } = require('./enum.js');


exports.DAT = function(core, pc, ins) {
	// do nothing and die
	return [];
}


exports.MOV = function(core, pc, ins) {
	if (ins.a.mode === AddrMode.Immediate) {
		const dst = core.getValue(pc, ins.b);
		dst.b.value = ins.a.value;
	} else {
		const src = core.getValue(pc, ins.a);
		const dstLocation = core.getLocation(pc, ins.b);
		// hacky deep copy
		core.data[dstLocation] = JSON.parse(JSON.stringify(src));
	}

	return [pc + 1];
}


exports.ADD = function(core, pc, ins) {
	if (ins.a.mode === AddrMode.Immediate) {
		const dst = core.getValue(pc, ins.b);
		dst.b.value += ins.a.value;
	} else {
		const src = core.getValue(pc, ins.a);
		const dst = core.getValue(pc, ins.b);
		dst.a.value += src.a.value;
		dst.b.value += src.b.value;
	}

	return [pc + 1];
}


exports.SUB = function(core, pc, ins) {
	if (ins.a.mode === AddrMode.Immediate) {
		const dst = core.getValue(pc, ins.b);
		dst.b.value -= ins.a.value;
	} else {
		const src = core.getValue(pc, ins.a);
		const dst = core.getValue(pc, ins.b);
		dst.a.value -= src.a.value;
		dst.b.value -= src.b.value;
	}

	return [pc + 1];
}