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

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


function getOp(core, pc, op) {
	switch(op.mode) {
		case AddrMode.Immediate:
			return { mode: AddrMode.Immediate, value: op.value };
		default:
			return { mode: 'precomp', value: core.getLocation(pc, op) };
	}
}


function Instruction(f) {
	return function(core, pc, ins) {
		const a = getOp(core, pc, ins.a);
		const b = getOp(core, pc, ins.b);
		return f(core, pc, a, b);
	}
}


exports.DAT = Instruction((core, pc, a, b) => {
	// do nothing and die
	return [];
});


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

	return [core.normalize(pc, 1)];
});


exports.ADD = Instruction((core, pc, a, b) => {
	if (a.mode === AddrMode.Immediate) {
		const dst = core.data[b.value];
		dst.b.value += a.value;
	} else {
		const src = core.data[a.value];
		const dst = core.data[b.value];
		dst.a.value += src.a.value;
		dst.b.value += src.b.value;
	}

	return [core.normalize(pc, 1)];
});


exports.SUB = Instruction((core, pc, a, b) => {
	if (a.mode === AddrMode.Immediate) {
		const dst = core.data[b.value];
		dst.b.value -= a.value;
	} else {
		const src = core.data[a.value];
		const dst = core.data[b.value];
		dst.a.value -= src.a.value;
		dst.b.value -= src.b.value;
	}

	return [core.normalize(pc, 1)];
});


exports.CMP = Instruction((core, pc, a, b) => {
	if (a.mode === AddrMode.Immediate) {
		const test = core.data[b.value];
		if (test.b.value === a.value) {
			return [core.normalize(pc, 2)];
		} else {
			return [core.normalize(pc, 1)];
		}
	} else {
		const left = core.data[a.value];
		const right = core.data[b.value];
		if (
			// compare opcode
			left.opcode  === right.opcode  &&
			// compare a-field
			left.a.mode  === right.a.mode  &&
			left.a.value === right.a.value &&
			// compare b-field
			left.b.mode  === right.b.mode  &&
			left.b.value === right.b.value
		) {
			return [core.normalize(pc, 2)];
		} else {
			return [core.normalize(pc, 1)];
		}
	}
});


exports.SLT = Instruction((core, pc, a, b) => {
	if (a.mode === AddrMode.Immediate) {
		const test = core.data[b.value];
		if (a.value < test.b.value) {
			return [core.normalize(pc, 2)];
		} else {
			return [core.normalize(pc, 1)];
		}
	} else {
		const left = core.data[a.value];
		const right = core.data[b.value];
		if (left.b.value < right.b.value) {
			return [core.normalize(pc, 2)];
		} else {
			return [core.normalize(pc, 1)];
		}
	}
});


exports.JMP = Instruction((core, pc, a, b) => {
	return [a.value];
});


exports.JMZ = Instruction((core, pc, a, b) => {
	let test
	if (b.mode === AddrMode.Immediate) {
		test = (b.value === 0);
	} else {
		const src = core.data[b.value];
		test = (src.b.value === 0);
	}

	if (test) {
		return [a.value];
	} else {
		return [core.normalize(pc, 1)];
	}
});


exports.JMN = Instruction((core, pc, a, b) => {
	let test;

	if (b.mode === AddrMode.Immediate) {
		test = (b.value !== 0);
	} else {
		const src = core.data[b.value];
		test = (src.b.value !== 0);
	}

	if (test) {
		return [a.value];
	} else {
		return [core.normalize(pc, 1)];
	}
});


exports.DJN = Instruction((core, pc, a, b) => {
	let test;

	if (b.mode === AddrMode.Immediate) {
		core.data[pc].b.value -= 1;
		b.value -= 1;
		test = (b.value !== 0);
	} else {
		const src = core.data[b.value];
		src.b.value -= 1;
		test = (src.b.value !== 0);
	}

	if (test) {
		return [a.value];
	} else {
		return [core.normalize(pc, 1)];
	}
});


exports.SPL = Instruction((core, pc, a, b) => {
	return [
		core.normalize(pc, 1),
		a.value,
	];
});