summaryrefslogtreecommitdiff
path: root/src/vm
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-05-22 23:56:50 -0500
committersanine <sanine.not@pm.me>2023-05-22 23:56:50 -0500
commit6a4548df5e405adb080320080c67b7054b75bcaf (patch)
treee75502d7fde865d27a689a14794b023ad3bc88d0 /src/vm
parentc1709249728cb9ad7011b0d39d18ad87d4636f4b (diff)
implement MOV
Diffstat (limited to 'src/vm')
-rw-r--r--src/vm/core.js4
-rw-r--r--src/vm/instruction.js15
-rw-r--r--src/vm/instruction.test.js38
3 files changed, 54 insertions, 3 deletions
diff --git a/src/vm/core.js b/src/vm/core.js
index 399cfc6..074e156 100644
--- a/src/vm/core.js
+++ b/src/vm/core.js
@@ -11,8 +11,8 @@ class Core {
for (let i=0; i<size; i++) {
this.data[i] = {
opcode: Op.DAT,
- a: { value: 0, mode: AddrMode.Direct },
- b: { value: 0, mode: AddrMode.Direct },
+ a: { value: 0, mode: AddrMode.Immediate },
+ b: { value: 0, mode: AddrMode.Immediate },
};
}
}
diff --git a/src/vm/instruction.js b/src/vm/instruction.js
index ac4cd83..c991643 100644
--- a/src/vm/instruction.js
+++ b/src/vm/instruction.js
@@ -7,3 +7,18 @@ 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];
+}
diff --git a/src/vm/instruction.test.js b/src/vm/instruction.test.js
index 5dc7eea..3535b76 100644
--- a/src/vm/instruction.test.js
+++ b/src/vm/instruction.test.js
@@ -1,7 +1,7 @@
'use strict';
const { Core } = require('./core.js');
-const { DAT } = require('./instruction.js');
+const { DAT, MOV } = require('./instruction.js');
const CORESIZE = 8000;
@@ -13,3 +13,39 @@ test('DAT does nothing and kills the program', () => {
const ins = core.data[pc];
expect(DAT(core, pc, ins)).toEqual([]);
});
+
+
+test('MOV correctly moves a full instruction', () => {
+ const core = new Core(CORESIZE);
+ const pc = 20;
+ core.data[pc] = {
+ opcode: 'MOV',
+ a: { mode: 'direct', value: 0 },
+ b: { mode: 'direct', value: 1 },
+ };
+ const ins = core.data[pc];
+ expect(core.data[pc+1].opcode).toBe('DAT');
+ expect(MOV(core, pc, ins)).toEqual([pc+1]);
+ expect(core.data[pc]).toEqual(core.data[pc+1]);
+ expect(core.data[pc+1].opcode).toBe('MOV');
+});
+
+
+test('MOV correctly moves a B-field', () => {
+ const core = new Core(CORESIZE);
+ const pc = 20;
+ core.data[pc] = {
+ opcode: 'MOV',
+ a: { mode: 'immediate', value: 100 },
+ b: { mode: 'direct', value: 1 },
+ };
+ const ins = core.data[pc];
+ expect(core.data[pc+1].opcode).toBe('DAT');
+ expect(MOV(core, pc, ins)).toEqual([pc+1]);
+ expect(core.data[pc+1]).toEqual({
+ opcode: 'DAT',
+ a: { mode: 'immediate', value: 0 },
+ b: { mode: 'immediate', value: 100 },
+ });
+
+});