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 {
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 };

@ -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);

Loading…
Cancel
Save