diff options
author | sanine <sanine.not@pm.me> | 2023-05-23 00:29:12 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-05-23 00:29:12 -0500 |
commit | cf71a97881e170fa018bf5fd0f02cdb13b874af8 (patch) | |
tree | 02e4a2dbd495498543cce85fd040d0d468cc3df0 /src/vm | |
parent | 19a297f42a2f321ead4c03290d38a8ccdbf8dc18 (diff) |
implement SUB
Diffstat (limited to 'src/vm')
-rw-r--r-- | src/vm/instruction.js | 15 | ||||
-rw-r--r-- | src/vm/instruction.test.js | 70 |
2 files changed, 84 insertions, 1 deletions
diff --git a/src/vm/instruction.js b/src/vm/instruction.js index c9048e1..146a88b 100644 --- a/src/vm/instruction.js +++ b/src/vm/instruction.js @@ -37,3 +37,18 @@ exports.ADD = function(core, pc, ins) { return [pc + 1]; } + + +exports.SUB = 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 dst = core.getValue(pc, ins.b); + dst.a.value -= src.a.value; + dst.b.value -= src.b.value; + } + + return [pc + 1]; +} diff --git a/src/vm/instruction.test.js b/src/vm/instruction.test.js index fd402e4..7bd2068 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 } = require('./instruction.js'); +const { DAT, MOV, ADD, SUB } = require('./instruction.js'); const CORESIZE = 8000; @@ -175,3 +175,71 @@ test('ADD correctly adds B-field', () => { b: { mode: 'immediate', value: 5 }, }); }); + + +test('SUB correctly subtracts two full instructions', () => { + const core = new Core(CORESIZE); + const pc = 20; + core.data[pc] = { + opcode: 'SUB', + 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(SUB(core, pc, ins)).toEqual([pc+1]); + expect(core.data[pc+1]).toEqual({ + opcode: 'DAT', + a: { mode: 'immediate', value: 10 }, + b: { mode: 'immediate', value: 20 }, + }); + expect(core.data[pc+2]).toEqual({ + opcode: 'DAT', + a: { mode: 'immediate', value: -7 }, + b: { mode: 'immediate', value: -16 }, + }); +}); + + +test('SUB correctly subtracts B-field', () => { + const core = new Core(CORESIZE); + const pc = 20; + core.data[pc] = { + opcode: 'SUB', + a: { mode: 'immediate', 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(SUB(core, pc, ins)).toEqual([pc+1]); + expect(core.data[pc+1]).toEqual({ + opcode: 'DAT', + a: { mode: 'immediate', value: 10 }, + b: { mode: 'immediate', value: 20 }, + }); + expect(core.data[pc+2]).toEqual({ + opcode: 'DAT', + a: { mode: 'immediate', value: 3 }, + b: { mode: 'immediate', value: 3 }, + }); +}); |