'use strict'; const { AddrMode } = require('./enum.js'); exports.DAT = function(core, pc, ins) { // do nothing and die return []; } exports.MOV = function(core, pc, ins) { if (ins.a.mode === AddrMode.Immediate) { const dst = core.getValue(pc, ins.b); dst.b.value = ins.a.value; } else { const src = core.getValue(pc, ins.a); const dstLocation = core.getLocation(pc, ins.b); // hacky deep copy core.data[dstLocation] = JSON.parse(JSON.stringify(src)); } return [core.normalize(pc, 1)]; } exports.ADD = function(core, pc, ins) { if (ins.a.mode === AddrMode.Immediate) { const dst = core.getValue(pc, ins.b); dst.b.value += ins.a.value; } else { const src = core.getValue(pc, ins.a); const dst = core.getValue(pc, ins.b); dst.a.value += src.a.value; dst.b.value += src.b.value; } return [core.normalize(pc, 1)]; } exports.SUB = function(core, pc, ins) { if (ins.a.mode === AddrMode.Immediate) { const dst = core.getValue(pc, ins.b); dst.b.value -= ins.a.value; } else { const src = core.getValue(pc, ins.a); const dst = core.getValue(pc, ins.b); dst.a.value -= src.a.value; dst.b.value -= src.b.value; } return [core.normalize(pc, 1)]; } exports.CMP = function(core, pc, ins) { if (ins.a.mode === AddrMode.Immediate) { const test = core.getValue(pc, ins.b); if (test.b.value === ins.a.value) { return [core.normalize(pc, 2)]; } else { return [core.normalize(pc, 1)]; } } else { const left = core.getValue(pc, ins.a); const right = core.getValue(pc, ins.b); if ( // compare opcode left.opcode === right.opcode && // compare a-field left.a.mode === right.a.mode && left.a.value === right.a.value && // compare b-field left.b.mode === right.b.mode && left.b.value === right.b.value ) { return [core.normalize(pc, 2)]; } else { return [core.normalize(pc, 1)]; } } } exports.SLT = function(core, pc, ins) { if (ins.a.mode === AddrMode.Immediate) { const test = core.getValue(pc, ins.b); if (ins.a.value < test.b.value) { return [core.normalize(pc, 2)]; } else { return [core.normalize(pc, 1)]; } } else { const left = core.getValue(pc, ins.a); const right = core.getValue(pc, ins.b); if (left.b.value < right.b.value) { return [core.normalize(pc, 2)]; } else { return [core.normalize(pc, 1)]; } } } exports.JMP = function(core, pc, ins) { const dstLoc = core.getLocation(pc, ins.a); return [dstLoc]; } exports.JMZ = function(core, pc, ins) { let test if (ins.b.mode === AddrMode.Immediate) { test = (ins.b.value === 0); } else { const src = core.getValue(pc, ins.b); test = (src.b.value === 0); } if (test) { return [core.getLocation(pc, ins.a)]; } else { return [core.normalize(pc, 1)]; } } exports.JMN = function(core, pc, ins) { let test; if (ins.b.mode === AddrMode.Immediate) { test = (ins.b.value !== 0); } else { const src = core.getValue(pc, ins.b); test = (src.b.value !== 0); } if (test) { return [core.getLocation(pc, ins.a)]; } else { return [core.normalize(pc, 1)]; } } exports.DJN = function(core, pc, ins) { let test; if (ins.b.mode === AddrMode.Immediate) { ins.b.value -= 1; test = (ins.b.value !== 0); } else { const src = core.getValue(pc, ins.b); src.b.value -= 1; test = (src.b.value !== 0); } if (test) { return [core.getLocation(pc, ins.a)]; } else { return [core.normalize(pc, 1)]; } } exports.SPL = function(core, pc, ins) { return [ core.normalize(pc, 1), core.getLocation(pc, ins.a), ]; }