diff options
author | sanine <sanine.not@pm.me> | 2023-05-23 10:08:35 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-05-23 10:08:35 -0500 |
commit | 2880c1565102d8a969846c5e68300ced88066943 (patch) | |
tree | 8374eaf85ae2258f03e8f409f545e582e29ec439 | |
parent | a6a59d51f626426bb3257ad32ea86cec08d7e3b9 (diff) |
implement SLT
-rw-r--r-- | src/vm/instruction.js | 20 | ||||
-rw-r--r-- | src/vm/instruction.test.js | 55 |
2 files changed, 74 insertions, 1 deletions
diff --git a/src/vm/instruction.js b/src/vm/instruction.js index df5369d..a79427b 100644 --- a/src/vm/instruction.js +++ b/src/vm/instruction.js @@ -81,3 +81,23 @@ exports.CMP = function(core, pc, ins) { } } } + + +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]; + } else { + return [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]; + } else { + return [pc + 1]; + } + } +} diff --git a/src/vm/instruction.test.js b/src/vm/instruction.test.js index 63112fb..a93a3b0 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, CMP } = require('./instruction.js'); +const { DAT, MOV, ADD, SUB, CMP, SLT } = require('./instruction.js'); const CORESIZE = 8000; @@ -294,3 +294,56 @@ test('CMP properly compares B-field', () => { expect(CMP(core, pc, ins)).toEqual([pc+2]); }); + + +test('SLT properly compares full instructions', () => { + const core = new Core(CORESIZE); + const pc = 20; + core.data[pc] = { + opcode: 'SLT', + a: { mode: 'direct', value: 1 }, + b: { mode: 'direct', value: 2 }, + }; + core.data[pc+1] = { + opcode: 'DAT', + a: { mode: 'immediate', value: 3 }, + b: { mode: 'immediate', value: 4 }, + }; + core.data[pc+2] = { + opcode: 'DAT', + a: { mode: 'immediate', value: 3 }, + b: { mode: 'immediate', value: 4 }, + }; + const ins = core.data[pc]; + + expect(SLT(core, pc, ins)).toEqual([pc+1]); + + core.data[pc+2].b.value = 5; + + expect(SLT(core, pc, ins)).toEqual([pc+2]); +}); + + + +test('SLT properly compares B-fields', () => { + const core = new Core(CORESIZE); + const pc = 20; + core.data[pc] = { + opcode: 'SLT', + a: { mode: 'immediate', value: 7 }, + 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(SLT(core, pc, ins)).toEqual([pc+1]); + + core.data[pc+2].b.value = 8; + + expect(SLT(core, pc, ins)).toEqual([pc+2]); +}); + |