summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-05-23 10:01:05 -0500
committersanine <sanine.not@pm.me>2023-05-23 10:01:05 -0500
commita6a59d51f626426bb3257ad32ea86cec08d7e3b9 (patch)
tree349723a10a8b8c9a842b11b2e91df92a880e8103
parentcf71a97881e170fa018bf5fd0f02cdb13b874af8 (diff)
implement CMP
-rw-r--r--src/vm/instruction.js29
-rw-r--r--src/vm/instruction.test.js53
2 files changed, 81 insertions, 1 deletions
diff --git a/src/vm/instruction.js b/src/vm/instruction.js
index 146a88b..df5369d 100644
--- a/src/vm/instruction.js
+++ b/src/vm/instruction.js
@@ -52,3 +52,32 @@ exports.SUB = function(core, pc, ins) {
return [pc + 1];
}
+
+
+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];
+ } else {
+ return [pc + 1];
+ }
+ } else {
+ const left = core.getValue(pc, ins.a);
+ const right = core.getValue(pc, ins.b);
+ 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 [pc + 2];
+ } else {
+ return [pc + 1];
+ }
+ }
+}
diff --git a/src/vm/instruction.test.js b/src/vm/instruction.test.js
index 7bd2068..63112fb 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, MOV, ADD, SUB } = require('./instruction.js');
+const { DAT, MOV, ADD, SUB, CMP } = require('./instruction.js');
const CORESIZE = 8000;
@@ -243,3 +243,54 @@ test('SUB correctly subtracts B-field', () => {
b: { mode: 'immediate', value: 3 },
});
});
+
+
+test('CMP properly compares full instructions', () => {
+ const core = new Core(CORESIZE);
+ const pc = 20;
+ core.data[pc] = {
+ opcode: 'CMP',
+ a: { mode: 'direct', value: 1 },
+ b: { mode: 'direct', value: 2 },
+ };
+ core.data[pc+1] = {
+ opcode: 'DAT',
+ a: { mode: 'immediate', value: 10 },
+ b: { mode: 'immediate', value: 20 },
+ };
+ core.data[pc+2] = {
+ opcode: 'DAT',
+ a: { mode: 'immediate', value: 3 },
+ b: { mode: 'immediate', value: 4 },
+ };
+ const ins = core.data[pc];
+
+ expect(CMP(core, pc, ins)).toEqual([pc+1]);
+
+ core.data[pc+2] = JSON.parse(JSON.stringify(core.data[pc+1]));
+
+ expect(CMP(core, pc, ins)).toEqual([pc+2]);
+});
+
+
+test('CMP properly compares B-field', () => {
+ const core = new Core(CORESIZE);
+ const pc = 20;
+ core.data[pc] = {
+ opcode: 'CMP',
+ a: { mode: 'immediate', value: 1 },
+ b: { mode: 'direct', value: 2 },
+ };
+ core.data[pc+2] = {
+ opcode: 'DAT',
+ a: { mode: 'immediate', value: 3 },
+ b: { mode: 'immediate', value: 4 },
+ };
+ const ins = core.data[pc];
+
+ expect(CMP(core, pc, ins)).toEqual([pc+1]);
+
+ core.data[pc+2].b.value = 1;
+
+ expect(CMP(core, pc, ins)).toEqual([pc+2]);
+});