summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2023-05-23 12:33:39 -0500
committersanine-a <sanine.not@pm.me>2023-05-23 12:33:39 -0500
commit4d5377870ee554036ac9aab570ac2bc809fc88bd (patch)
treef6c15b854daeab756caa3e247926e884dfdeff86
parent86793f0afd74092be507325a40d85bc62bd47597 (diff)
implement DJN
-rw-r--r--src/vm/instruction.js20
-rw-r--r--src/vm/instruction.test.js40
2 files changed, 60 insertions, 0 deletions
diff --git a/src/vm/instruction.js b/src/vm/instruction.js
index f323f84..c235b9a 100644
--- a/src/vm/instruction.js
+++ b/src/vm/instruction.js
@@ -142,3 +142,23 @@ exports.JMN = function(core, pc, ins) {
return [core.normalize(pc, 1)];
}
}
+
+
+exports.DJN = function(core, pc, ins) {
+ let test;
+
+ if (ins.b.mode === AddrMode.Immediate) {
+ ins.b.value -= 1;
+ test = (ins.b.value !== 0);
+ } else {
+ const src = core.getValue(pc, ins.b);
+ src.b.value -= 1;
+ 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 2880da7..82d9819 100644
--- a/src/vm/instruction.test.js
+++ b/src/vm/instruction.test.js
@@ -466,3 +466,43 @@ test('JMN correctly jumps in immediate mode', () => {
expect(JMN(core, pc, ins)).toEqual([CORESIZE-1]);
});
+
+test('DJN correctly jumps in non-immediate mode', () => {
+ const core = new Core(CORESIZE);
+ const pc = 2;
+ core.data[pc] = {
+ opcode: 'DJN',
+ a: { mode: 'direct', value: -3 },
+ b: { mode: 'direct', value: 1 },
+ };
+ core.data[pc+1] = {
+ opcode: 'DJN',
+ a: { mode: 'direct', value: -3 },
+ b: { mode: 'direct', value: 1 },
+ };
+ const ins = core.data[pc];
+
+ expect(DJN(core, pc, ins)).toEqual([pc+1]);
+ expect(core.data[pc+1].b.value).toBe(0);
+ expect(DJN(core, pc, ins)).toEqual([CORESIZE-1]);
+ expect(core.data[pc+1].b.value).toBe(-1);
+});
+
+
+test('DJN correctly jumps in immediate mode', () => {
+ const core = new Core(CORESIZE);
+ const pc = 2;
+ core.data[pc] = {
+ opcode: 'DJN',
+ a: { mode: 'direct', value: -3 },
+ b: { mode: 'immediate', value: 1 },
+ };
+ const ins = core.data[pc];
+
+ expect(DJN(core, pc, ins)).toEqual([pc+1]);
+ expect(core.data[pc].b.value).toBe(0);
+ expect(DJN(core, pc, ins)).toEqual([CORESIZE-1]);
+ expect(core.data[pc].b.value).toBe(-1);
+});
+
+