Now the machine supports PEEK/POKE to RAM.

master
Zed A. Shaw 2 years ago
parent 40da242bd7
commit 2902842fcc
  1. 36
      src/buttons.js
  2. 36
      tests/basic_tests.js

@ -1,14 +1,13 @@
export class ButtonMachine { export class ButtonMachine {
constructor(code) { constructor(code) {
this.stack = []; this.stack = [];
this.ram = new Array(64).fill(0);
this.ip = 0; this.ip = 0;
this.code = code; this.code = code;
this.max_clicks = 128; this.max_clicks = 256;
this.tick = 0; this.tick = 0;
this.registers = {}; this.registers = {'IX': 0};
this.error = ''; this.error = '';
this.error_line = 0; this.error_line = 0;
this.halted = false; this.halted = false;
@ -29,7 +28,7 @@ export class ButtonMachine {
/* Need to use a function because @babel/plugin-proposal-class-properties */ /* Need to use a function because @babel/plugin-proposal-class-properties */
static register_names() { static register_names() {
return ['AX', 'BX', 'CX', 'DX']; return ['AX', 'BX', 'CX', 'DX', 'IX'];
} }
static operations() { static operations() {
@ -160,6 +159,29 @@ export class ButtonMachine {
this.next(); 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() { get running() {
return this.halted === false && this.tick < this.max_clicks && this.ip < this.code.length && this.cur_op !== undefined; 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 { export default { ButtonMachine };
ButtonMachine
};

@ -1,17 +1,25 @@
import { ButtonMachine } from "../src/buttons.js"; import { ButtonMachine } from '../src/buttons.js';
const cpu = new ButtonMachine([ let code = [
["PUSH", 10], ['PUSH', -10], // start at -10
["PUSH", 1], ['PUSH', 1], // increment by 1
["SUB"], ['ADD'],
["JZ", 5], ['POKE', 1], // put it in ram, IX++
["JUMP", 1], ['JNZ', 1], // the previous test fails so it jumps to loop again
["PUSH", 100], ['STOR', 'IX'],
["HALT"] ['POP'],
]); ['PEEK', 1],
['JNZ', 6],
];
cpu.run(); let machine = new ButtonMachine(code);
console.log("STACK TOP", cpu.stack_top); const ops = ButtonMachine.operations();
console.log("REGISTER", cpu.register_entries); const registers = ButtonMachine.register_names();
console.log("STACK", cpu.stack);
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);

Loading…
Cancel
Save