From 15323007750e0f0f7c36a6a6fa01ad1d303a4a16 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Tue, 23 May 2023 14:53:53 -0500 Subject: implement basic vm runner --- src/vm/core.js | 15 +++++++++++++++ src/vm/vm.js | 34 +++++++++++++++++++++++----------- 2 files changed, 38 insertions(+), 11 deletions(-) (limited to 'src/vm') diff --git a/src/vm/core.js b/src/vm/core.js index a8fbce7..6363a60 100644 --- a/src/vm/core.js +++ b/src/vm/core.js @@ -133,6 +133,21 @@ class Core { const index = this.getLocation(pc, address); return this.data[index]; } + + + pretty(ins) { + const prettyMode = { + 'immediate': '#', + 'direct': ' ', + 'indirect': '@', + 'predecrement': '<', + }; + + return `\ +${ins.opcode} \ +${prettyMode[ins.a.mode]}${ins.a.value}, \ +${prettyMode[ins.b.mode]}${ins.b.value}`; + } } diff --git a/src/vm/vm.js b/src/vm/vm.js index 6add546..eea949a 100644 --- a/src/vm/vm.js +++ b/src/vm/vm.js @@ -23,7 +23,12 @@ class Warrior { next() { const pc = this.queue[0]; this.queue = this.queue.slice(1); - return next; + return pc; + } + + peek() { + const pc = this.queue[0]; + return pc; } }; @@ -38,7 +43,8 @@ class RedcodeVm { step() { let running = 0; - for (let warrior of this.warriors) { + for (let i=0; i warrior.isDead() ? count : count+1, + 0 + ); + } + run(maxSteps) { let steps = 0; - let running = this.warriors.length; - while(running > 0 && steps < maxSteps) { - running = this.step(); + while(this.warriorsAlive() > 1 && steps < maxSteps) { + this.step(); steps += 1; } - if (running !== 0) { - console.log("draw, maximum steps exceeded"); - } - for (let i=0; i ({ warrior: i, alive: !w.isDead() }) + ); } }; -- cgit v1.2.1