diff options
Diffstat (limited to 'src/vm/core.js')
-rw-r--r-- | src/vm/core.js | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/vm/core.js b/src/vm/core.js new file mode 100644 index 0000000..292ceb0 --- /dev/null +++ b/src/vm/core.js @@ -0,0 +1,51 @@ +'use strict'; + +const { Op, AddrMode} = require('./enum.js'); + + +class Core { + constructor(size) { + this.data = new Array(size); + + // initialize core to all DAT 0, 0 + for (let i=0; i<size; i++) { + this.data[i] = { + opcode: Op.DAT, + a: { value: 0, mode: AddrMode.Direct }, + b: { value: 0, mode: AddrMode.Direct }, + }; + } + } + + + normalize(pc, value) { + return (pc + value) % this.data.length; + } + + + getLocation(pc, address) { + switch(address.mode) { + case AddrMode.Immediate: + throw "Cannot get location from immediate-mode address"; + case AddrMode.Direct: + return this.normalize(pc, address.value); + case AddrMode.Indirect: { + let loc = this.normalize(pc, address.value); + let b = this.data[loc]; + return this.normalize(loc, b); + } + case AddrMode.Predecrement: { + let loc = this.normalize(pc, address.value); + this.data[loc].b -= 1; + let b = this.data[loc].b; + return this.normalize(loc, b); + } + default: + throw `Invalid addressing mode "${address.mode}"`; + } + } + + getValue(pc, address) { + return this.data[this.getLocation(pc, address)]; + } +} |