From cd00c3cded8f9c3e3e7b5c57ceabda8fe1c145d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Sat, 26 May 2018 22:51:19 +0300 Subject: [PATCH] Implement 8-bit wraparound for multiplies as well and handle empty memory in ircbotRun() --- gir.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/gir.js b/gir.js index 5662a83..18c783d 100644 --- a/gir.js +++ b/gir.js @@ -705,8 +705,8 @@ function runVM(state, maxCycles = null) { memory.set(index, 0); } let old = memory.get(index); - memory.set(index, old + - multiplier * change); + let value = old + multiplier * change; + memory.set(index, value & 0xff); } ip++; @@ -972,10 +972,12 @@ function ircbotRun(program, input, maxCycles = 400000) { // Find min and max of the existant array indinces, since // there is no good way to easily get them and we need them // for the output - let min = Array.from(result.state.memory.keys()).reduce( - (x, y) => Math.min(x, y)); - let max = Array.from(result.state.memory.keys()).reduce( - (x, y) => Math.max(x, y)); + // Default to both being set to tapeHead, because that way + // even if there are no indices we can get values that + // the rest of the code can work with + let memoryIndices = Array.from(result.state.memory.keys()); + let min = memoryIndices.reduce(Math.min, tapeHead); + let max = memoryIndices.reduce(Math.max, tapeHead); // Get 15 cells of context on each side of tape head // Exception is if max or min comes up before that, in which