summaryrefslogtreecommitdiff
path: root/src/vm
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2023-05-23 11:52:02 -0500
committersanine-a <sanine.not@pm.me>2023-05-23 11:52:02 -0500
commitd9dcab1271568f1c4bf70e97b707fb8cf42e10fd (patch)
tree10b1d24e0db7e25a26a95bdaacd1219e3af5e64f /src/vm
parent2880c1565102d8a969846c5e68300ced88066943 (diff)
fix core boundary wrapping
Diffstat (limited to 'src/vm')
-rw-r--r--src/vm/core.js7
-rw-r--r--src/vm/instruction.js28
-rw-r--r--src/vm/instruction.test.js46
3 files changed, 68 insertions, 13 deletions
diff --git a/src/vm/core.js b/src/vm/core.js
index 6303ee5..9b0fe77 100644
--- a/src/vm/core.js
+++ b/src/vm/core.js
@@ -19,7 +19,12 @@ class Core {
normalize(pc, value) {
- return (pc + value) % this.data.length;
+ const v = (pc + value) % this.data.length;
+ if (v < 0) {
+ return v + this.data.length;
+ } else {
+ return v;
+ }
}
diff --git a/src/vm/instruction.js b/src/vm/instruction.js
index a79427b..878d20b 100644
--- a/src/vm/instruction.js
+++ b/src/vm/instruction.js
@@ -20,7 +20,7 @@ exports.MOV = function(core, pc, ins) {
core.data[dstLocation] = JSON.parse(JSON.stringify(src));
}
- return [pc + 1];
+ return [core.normalize(pc, 1)];
}
@@ -35,7 +35,7 @@ exports.ADD = function(core, pc, ins) {
dst.b.value += src.b.value;
}
- return [pc + 1];
+ return [core.normalize(pc, 1)];
}
@@ -50,7 +50,7 @@ exports.SUB = function(core, pc, ins) {
dst.b.value -= src.b.value;
}
- return [pc + 1];
+ return [core.normalize(pc, 1)];
}
@@ -58,9 +58,9 @@ exports.CMP = function(core, pc, ins) {
if (ins.a.mode === AddrMode.Immediate) {
const test = core.getValue(pc, ins.b);
if (test.b.value === ins.a.value) {
- return [pc + 2];
+ return [core.normalize(pc, 2)];
} else {
- return [pc + 1];
+ return [core.normalize(pc, 1)];
}
} else {
const left = core.getValue(pc, ins.a);
@@ -75,9 +75,9 @@ exports.CMP = function(core, pc, ins) {
left.b.mode === right.b.mode &&
left.b.value === right.b.value
) {
- return [pc + 2];
+ return [core.normalize(pc, 2)];
} else {
- return [pc + 1];
+ return [core.normalize(pc, 1)];
}
}
}
@@ -87,17 +87,23 @@ exports.SLT = function(core, pc, ins) {
if (ins.a.mode === AddrMode.Immediate) {
const test = core.getValue(pc, ins.b);
if (ins.a.value < test.b.value) {
- return [pc + 2];
+ return [core.normalize(pc, 2)];
} else {
- return [pc + 1];
+ return [core.normalize(pc, 1)];
}
} else {
const left = core.getValue(pc, ins.a);
const right = core.getValue(pc, ins.b);
if (left.b.value < right.b.value) {
- return [pc + 2];
+ return [core.normalize(pc, 2)];
} else {
- return [pc + 1];
+ return [core.normalize(pc, 1)];
}
}
}
+
+
+exports.JMP = function(core, pc, ins) {
+ const dstLoc = core.getLocation(pc, ins.a);
+ return [dstLoc];
+}
diff --git a/src/vm/instruction.test.js b/src/vm/instruction.test.js
index a93a3b0..8efa47f 100644
--- a/src/vm/instruction.test.js
+++ b/src/vm/instruction.test.js
@@ -1,7 +1,11 @@
'use strict';
const { Core } = require('./core.js');
-const { DAT, MOV, ADD, SUB, CMP, SLT } = require('./instruction.js');
+const {
+ DAT,
+ MOV, ADD, SUB, CMP, SLT,
+ JMP,
+} = require('./instruction.js');
const CORESIZE = 8000;
@@ -109,6 +113,33 @@ test('MOV correctly handles predecrement indirection', () => {
});
+test('MOV correctly handles core boundaries', () => {
+ const core = new Core(CORESIZE);
+ const pc = CORESIZE-1;
+ core.data[pc] = {
+ opcode: 'MOV',
+ a: { mode: 'immediate', value: 100 },
+ b: { mode: 'predecrement', value: 2 },
+ };
+ const ins = core.data[pc];
+ expect(core.data[0].opcode).toBe('DAT');
+ expect(MOV(core, pc, ins)).toEqual([0]);
+ expect(core.data[1]).toEqual({
+ opcode: 'DAT',
+ a: { mode: 'immediate', value: 0 },
+ b: { mode: 'immediate', value: -1 },
+ });
+
+ expect(core.data[0]).toEqual({
+ opcode: 'DAT',
+ a: { mode: 'immediate', value: 0 },
+ b: { mode: 'immediate', value: 100 },
+ });
+
+});
+
+
+
test('ADD correctly adds two full instructions', () => {
const core = new Core(CORESIZE);
const pc = 20;
@@ -347,3 +378,16 @@ test('SLT properly compares B-fields', () => {
expect(SLT(core, pc, ins)).toEqual([pc+2]);
});
+
+test('JMP correctly jumps', () => {
+ const core = new Core(CORESIZE);
+ const pc = 2;
+ core.data[pc] = {
+ opcode: 'JMP',
+ a: { mode: 'direct', value: -3 },
+ b: { mode: 'direct', value: 0 },
+ };
+ const ins = core.data[pc];
+
+ expect(JMP(core, pc, ins)).toEqual([CORESIZE-1]);
+});