From 2902842fcc062552cf6ca50896b8c62a587df7ea Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Mon, 18 Apr 2022 17:26:11 -0400 Subject: [PATCH] Now the machine supports PEEK/POKE to RAM. --- src/buttons.js | 36 ++++++++++++++++++++++++++++-------- tests/basic_tests.js | 36 ++++++++++++++++++++++-------------- 2 files changed, 50 insertions(+), 22 deletions(-) diff --git a/src/buttons.js b/src/buttons.js index 6eb6a31..1640178 100644 --- a/src/buttons.js +++ b/src/buttons.js @@ -1,14 +1,13 @@ - - export class ButtonMachine { constructor(code) { this.stack = []; + this.ram = new Array(64).fill(0); this.ip = 0; this.code = code; - this.max_clicks = 128; + this.max_clicks = 256; this.tick = 0; - this.registers = {}; + this.registers = {'IX': 0}; this.error = ''; this.error_line = 0; this.halted = false; @@ -29,7 +28,7 @@ export class ButtonMachine { /* Need to use a function because @babel/plugin-proposal-class-properties */ static register_names() { - return ['AX', 'BX', 'CX', 'DX']; + return ['AX', 'BX', 'CX', 'DX', 'IX']; } static operations() { @@ -160,6 +159,29 @@ export class ButtonMachine { this.next(); } + op_PEEK(inc) { + let index = Math.abs(this.registers.IX) % this.ram.length; + this.stack.push(this.ram[index]); + if(inc) this.registers.IX = index + inc; + this.next(); + } + + op_POKE(inc) { + let index = Math.abs(this.registers.IX) % this.ram.length; + this.ram[index] = this.stack_top; + if(inc) this.registers.IX = index + inc; + this.next(); + } + + op_SWAP() { + if(!this.assert(this.stack.length >= 2, "Not enough elements on the stack to swap.")) return; + + let [top, n] = [this.stack.pop(), this.stack.pop()]; + this.stack.push(top); + this.stack.push(n); + this.next(); + } + get running() { return this.halted === false && this.tick < this.max_clicks && this.ip < this.code.length && this.cur_op !== undefined; } @@ -192,6 +214,4 @@ export class ButtonMachine { } } -export default { - ButtonMachine -}; +export default { ButtonMachine }; diff --git a/tests/basic_tests.js b/tests/basic_tests.js index fd2dae9..3b4a988 100644 --- a/tests/basic_tests.js +++ b/tests/basic_tests.js @@ -1,17 +1,25 @@ -import { ButtonMachine } from "../src/buttons.js"; +import { ButtonMachine } from '../src/buttons.js'; -const cpu = new ButtonMachine([ - ["PUSH", 10], - ["PUSH", 1], - ["SUB"], - ["JZ", 5], - ["JUMP", 1], - ["PUSH", 100], - ["HALT"] -]); +let code = [ + ['PUSH', -10], // start at -10 + ['PUSH', 1], // increment by 1 + ['ADD'], + ['POKE', 1], // put it in ram, IX++ + ['JNZ', 1], // the previous test fails so it jumps to loop again + ['STOR', 'IX'], + ['POP'], + ['PEEK', 1], + ['JNZ', 6], +]; -cpu.run(); +let machine = new ButtonMachine(code); -console.log("STACK TOP", cpu.stack_top); -console.log("REGISTER", cpu.register_entries); -console.log("STACK", cpu.stack); +const ops = ButtonMachine.operations(); +const registers = ButtonMachine.register_names(); + +machine.run(); + +console.log("STACK TOP", machine.stack_top); +console.log("REGISTER", machine.register_entries); +console.log("STACK", machine.stack); +console.log("RAM", machine.ram);