diff options
author | sanine-a <sanine.not@pm.me> | 2023-05-23 12:20:41 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2023-05-23 12:20:41 -0500 |
commit | 70c42ab50d6eb78d0cc825d2d7f0b318398819ba (patch) | |
tree | c5b599c12f0eca4c4bdc8b18068b96288a3aa2eb | |
parent | d9dcab1271568f1c4bf70e97b707fb8cf42e10fd (diff) |
implement JMZ
-rw-r--r-- | src/vm/instruction.js | 17 | ||||
-rw-r--r-- | src/vm/instruction.test.js | 39 |
2 files changed, 55 insertions, 1 deletions
diff --git a/src/vm/instruction.js b/src/vm/instruction.js index 878d20b..2afd3f7 100644 --- a/src/vm/instruction.js +++ b/src/vm/instruction.js @@ -107,3 +107,20 @@ exports.JMP = function(core, pc, ins) { const dstLoc = core.getLocation(pc, ins.a); return [dstLoc]; } + + +exports.JMZ = function(core, pc, ins) { + let test + if (ins.b.mode === AddrMode.Immediate) { + test = (ins.b.value === 0); + } else { + const src = core.getValue(pc, ins.b); + test = (src.b.value === 0); + } + + if (test) { + return [core.getLocation(pc, ins.a)]; + } else { + return [core.normalize(pc, 1)]; + } +} diff --git a/src/vm/instruction.test.js b/src/vm/instruction.test.js index 8efa47f..6175ab2 100644 --- a/src/vm/instruction.test.js +++ b/src/vm/instruction.test.js @@ -4,7 +4,7 @@ const { Core } = require('./core.js'); const { DAT, MOV, ADD, SUB, CMP, SLT, - JMP, + JMP, JMZ, JMN, DJN, SPL, } = require('./instruction.js'); @@ -391,3 +391,40 @@ test('JMP correctly jumps', () => { expect(JMP(core, pc, ins)).toEqual([CORESIZE-1]); }); + + +test('JMZ correctly jumps in non-immediate mode', () => { + const core = new Core(CORESIZE); + const pc = 2; + core.data[pc] = { + opcode: 'JMZ', + a: { mode: 'direct', value: -3 }, + b: { mode: 'direct', value: 1 }, + }; + core.data[pc+1] = { + opcode: 'JMZ', + a: { mode: 'direct', value: -3 }, + b: { mode: 'direct', value: 1 }, + }; + const ins = core.data[pc]; + + expect(JMZ(core, pc, ins)).toEqual([pc+1]); + core.data[pc+1].b.value = 0; + expect(JMZ(core, pc, ins)).toEqual([CORESIZE-1]); +}); + + +test('JMZ correctly jumps in immediate mode', () => { + const core = new Core(CORESIZE); + const pc = 2; + core.data[pc] = { + opcode: 'JMZ', + a: { mode: 'direct', value: -3 }, + b: { mode: 'immediate', value: 1 }, + }; + const ins = core.data[pc]; + + expect(JMZ(core, pc, ins)).toEqual([pc+1]); + core.data[pc].b.value = 0; + expect(JMZ(core, pc, ins)).toEqual([CORESIZE-1]); +}); |