diff options
Diffstat (limited to 'src/vm')
| -rw-r--r-- | src/vm/core.js | 3 | ||||
| -rw-r--r-- | src/vm/instruction.js | 111 | ||||
| -rw-r--r-- | src/vm/instruction.test.js | 15 | 
3 files changed, 20 insertions, 109 deletions
| diff --git a/src/vm/core.js b/src/vm/core.js index 292ceb0..399cfc6 100644 --- a/src/vm/core.js +++ b/src/vm/core.js @@ -49,3 +49,6 @@ class Core {  		return this.data[this.getLocation(pc, address)];  	}  } + + +exports.Core = Core; diff --git a/src/vm/instruction.js b/src/vm/instruction.js index 0fe922d..ac4cd83 100644 --- a/src/vm/instruction.js +++ b/src/vm/instruction.js @@ -3,114 +3,7 @@  const { AddrMode } = require('./enum.js'); -function DoDat(core, pc, ins) { -	// do nothing +exports.DAT = function(core, pc, ins) { +	// do nothing and die  	return [];  } - - -function DoMov(core, pc, ins) { -	if (ins.a.mode === AddrMode.Immediate) { -		let mem = core.getValue(pc, ins.b); -		mem.b.value = ins.a.value; -	} else { -		const src = core.getValue(pc, ins.a); -		const dstLoc = core.getLocation(pc, ins.b); - -		// hacky deep copy -		core.data[dstLoc] = JSON.parse(JSON.stringify(src)); -	} -	return [pc + 1]; -} - - -function DoAdd(core, pc, ins) { -	if (ins.a.mode === AddrMode.Immediate) { -		let dst = core.getValue(pc, ins.b); -		dst.b += ins.a.value; -	} else { -		let src = core.getValue(pc, ins.a); -		let dst = core.getValue(pc, ins.b); -		dst.a.value += src.a.value; -		dst.b.value += src.b.value; -	} -	return [pc + 1]; -} - - -function DoSub(core, pc, ins) { -	if (ins.a.mode === AddrMode.Immediate) { -		let dst = core.getValue(pc, ins.b); -		dst.b -= ins.a.value; -	} else { -		let src = core.getValue(pc, ins.a); -		let dst = core.getValue(pc, ins.b); -		dst.a.value -= src.a.value; -		dst.b.value -= src.b.value; -	} -	return [pc + 1]; -} - - -function DoJmp(core, pc, ins) { -	return [core.getLocation(pc, ins.a)]; -} - - -function DoJmz(core, pc, ins) { -	let test -	if (ins.b.mode === AddrMode.Immediate) { -		test = ins; -	} else { -		test = core.getValue(pc, ins.b); -	} - -	if (test.b.value === 0) { -		return [core.getLocation(pc, ins.a)]; -	} else { -		return [pc + 1]; -	} -} - - -function DoJmn(core, pc, ins) { -	let test -	if (ins.b.mode === AddrMode.Immediate) { -		test = ins; -	} else { -		test = core.getValue(pc, ins.b); -	} - -	if (test.b.value !== 0) { -		return [core.getLocation(pc, ins.a)]; -	} else { -		return [pc + 1]; -	} -} - - -function DoCmp(core, pc, ins) { -	if (ins.a.mode === AddrMode.Immediate) { -		const test = core.getValue(pc, ins.b); -		if (ins.a.value === test.b.value) { -			return [pc + 2]; -		} else { -			return [pc + 1]; -		} -	} else { -		const a = core.getValue(pc, ins.a); -		const b = core.getValue(pc, ins.b); - -		if ( -			a.opcode === b.opcode && -			a.a.value === b.a.value && -			a.b.value === b.b.value) -		) { -			return [pc + 2]; -		} else { -			return [pc + 1]; -		} -	} -} - - diff --git a/src/vm/instruction.test.js b/src/vm/instruction.test.js new file mode 100644 index 0000000..5dc7eea --- /dev/null +++ b/src/vm/instruction.test.js @@ -0,0 +1,15 @@ +'use strict'; + +const { Core } = require('./core.js'); +const { DAT } = require('./instruction.js'); + + +const CORESIZE = 8000; + + +test('DAT does nothing and kills the program', () => { +	const core = new Core(CORESIZE); +	const pc = 0; +	const ins = core.data[pc]; +	expect(DAT(core, pc, ins)).toEqual([]); +}); | 
