Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add atan2 functon and update project #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,253 changes: 555 additions & 698 deletions lib/IC.js

Large diffs are not rendered by default.

501 changes: 330 additions & 171 deletions lib/instructions/Branch.js

Large diffs are not rendered by default.

18 changes: 12 additions & 6 deletions lib/instructions/Device.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

module.exports = function (ic) {
ic._registerOpcode("l", [["r", "a"], ["d", "a"], ["s"]], _instruction_l, "device");

ic._registerOpcode("s", [["d", "a"], ["s"], ["r", "i", "f", "a"]], _instruction_s, "device");

ic._registerOpcode("ls", [["r", "a"], ["d", "a"], ["r", "i", "a"], ["s"]], _instruction_ls, "device");

ic._registerOpcode("lr", [["r", "a"], ["d", "a"], ["s"], ["s"]], _instruction_lr, "device");

ic._registerOpcode("lb", [["r", "a"], ["r", "i", "a"], "s", "s"], _instruction_lb, "device");

ic._registerOpcode("sb", [["r", "i", "a"], "s", ["r", "i", "f", "a"]], _instruction_sb, "device");
};

Expand All @@ -20,30 +23,33 @@ function _instruction_s(fields, allowedTypes, ic) {
}

function _instruction_ls(fields, allowedTypes, ic) {
var deviceNumber = ic._resolveDeviceNumber(fields[1], allowedTypes[1]);
var slotNumber = Number(ic._getRegister(fields[2], undefined, allowedTypes[2])).toString();
let deviceNumber = ic._resolveDeviceNumber(fields[1], allowedTypes[1]);

let slotNumber = Number(ic._getRegister(fields[2], undefined, allowedTypes[2])).toString();

if (!Object.keys(ic._ioSlot[deviceNumber]).includes(slotNumber.toString()) || !Object.keys(ic._ioSlot[deviceNumber][slotNumber]).includes(fields[3])) {
ic.setIOSlot(deviceNumber, slotNumber, fields[3], 0);
}

var value = ic._ioSlot[deviceNumber][slotNumber][fields[3]];
let value = ic._ioSlot[deviceNumber][slotNumber][fields[3]];

ic._setRegister(fields[0], value, undefined, allowedTypes[0]);
}

function _instruction_lr(fields, allowedTypes, ic) {
var deviceNumber = ic._resolveDeviceNumber(fields[1], allowedTypes[1]);
let deviceNumber = ic._resolveDeviceNumber(fields[1], allowedTypes[1]);

if (!Object.keys(ic._ioReagent[deviceNumber]).includes(fields[3]) || !Object.keys(ic._ioReagent[deviceNumber][fields[3]]).includes(fields[2])) {
ic.setIOReagent(deviceNumber, fields[3], fields[2], 0);
}

var value = ic._ioReagent[deviceNumber][fields[3]][fields[2]];
let value = ic._ioReagent[deviceNumber][fields[3]][fields[2]];

ic._setRegister(fields[0], value, undefined, allowedTypes[0]);
}

/* eslint no-unused-vars: 0 */


function _instruction_lb(fields, allowedTypes, ic) {}

function _instruction_sb(fields, allowedTypes, ic) {}
31 changes: 19 additions & 12 deletions lib/instructions/Logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,42 @@

module.exports = function (ic) {
ic._registerOpcode("and", [["r", "a"], ["r", "i", "f", "a"], ["r", "i", "f", "a"]], _instruction_and, "logic");

ic._registerOpcode("or", [["r", "a"], ["r", "i", "f", "a"], ["r", "i", "f", "a"]], _instruction_or, "logic");

ic._registerOpcode("xor", [["r", "a"], ["r", "i", "f", "a"], ["r", "i", "f", "a"]], _instruction_xor, "logic");

ic._registerOpcode("nor", [["r", "a"], ["r", "i", "f", "a"], ["r", "i", "f", "a"]], _instruction_nor, "logic");
};

function _instruction_and(fields, allowedTypes, ic) {
var valueOne = ic._getRegister(fields[1], undefined, allowedTypes[1]) != 0;
var valueTwo = ic._getRegister(fields[2], undefined, allowedTypes[2]) != 0;
var result = valueOne && valueTwo ? 1 : 0;
let valueOne = ic._getRegister(fields[1], undefined, allowedTypes[1]) != 0;
let valueTwo = ic._getRegister(fields[2], undefined, allowedTypes[2]) != 0;
let result = valueOne && valueTwo ? 1 : 0;

ic._setRegister(fields[0], result, undefined, allowedTypes[0]);
}

function _instruction_or(fields, allowedTypes, ic) {
var valueOne = ic._getRegister(fields[1], undefined, allowedTypes[1]) != 0;
var valueTwo = ic._getRegister(fields[2], undefined, allowedTypes[2]) != 0;
var result = valueOne || valueTwo ? 1 : 0;
let valueOne = ic._getRegister(fields[1], undefined, allowedTypes[1]) != 0;
let valueTwo = ic._getRegister(fields[2], undefined, allowedTypes[2]) != 0;
let result = valueOne || valueTwo ? 1 : 0;

ic._setRegister(fields[0], result, undefined, allowedTypes[0]);
}

function _instruction_xor(fields, allowedTypes, ic) {
var valueOne = ic._getRegister(fields[1], undefined, allowedTypes[1]) != 0;
var valueTwo = ic._getRegister(fields[2], undefined, allowedTypes[2]) != 0;
var result = valueOne ^ valueTwo ? 1 : 0;
let valueOne = ic._getRegister(fields[1], undefined, allowedTypes[1]) != 0;
let valueTwo = ic._getRegister(fields[2], undefined, allowedTypes[2]) != 0;
let result = valueOne ^ valueTwo ? 1 : 0;

ic._setRegister(fields[0], result, undefined, allowedTypes[0]);
}

function _instruction_nor(fields, allowedTypes, ic) {
var valueOne = ic._getRegister(fields[1], undefined, allowedTypes[1]) != 0;
var valueTwo = ic._getRegister(fields[2], undefined, allowedTypes[2]) != 0;
var result = !valueOne && !valueTwo ? 1 : 0;
let valueOne = ic._getRegister(fields[1], undefined, allowedTypes[1]) != 0;
let valueTwo = ic._getRegister(fields[2], undefined, allowedTypes[2]) != 0;
let result = !valueOne && !valueTwo ? 1 : 0;

ic._setRegister(fields[0], result, undefined, allowedTypes[0]);
}
55 changes: 46 additions & 9 deletions lib/instructions/Math.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,83 @@

module.exports = function (ic) {
ic._registerOpcode("add", [["r", "a"], ["r", "i", "f", "a"], ["r", "i", "f", "a"]], _instruction_add, "math");

ic._registerOpcode("sub", [["r", "a"], ["r", "i", "f", "a"], ["r", "i", "f", "a"]], _instruction_sub, "math");

ic._registerOpcode("mul", [["r", "a"], ["r", "i", "f", "a"], ["r", "i", "f", "a"]], _instruction_mul, "math");

ic._registerOpcode("div", [["r", "a"], ["r", "i", "f", "a"], ["r", "i", "f", "a"]], _instruction_div, "math");

ic._registerOpcode("mod", [["r", "a"], ["r", "i", "f", "a"], ["r", "i", "f", "a"]], _instruction_mod, "math");

ic._registerOpcode("sqrt", [["r", "a"], ["r", "i", "f", "a"]], _instruction_sqrt, "math");

ic._registerOpcode("round", [["r", "a"], ["r", "i", "f", "a"]], _instruction_round, "math");

ic._registerOpcode("trunc", [["r", "a"], ["r", "i", "f", "a"]], _instruction_trunc, "math");

ic._registerOpcode("ceil", [["r", "a"], ["r", "i", "f", "a"]], _instruction_ceil, "math");

ic._registerOpcode("floor", [["r", "a"], ["r", "i", "f", "a"]], _instruction_floor, "math");

ic._registerOpcode("max", [["r", "a"], ["r", "i", "f", "a"], ["r", "i", "f", "a"]], _instruction_max, "math");

ic._registerOpcode("min", [["r", "a"], ["r", "i", "f", "a"], ["r", "i", "f", "a"]], _instruction_min, "math");

ic._registerOpcode("abs", [["r", "a"], ["r", "i", "f", "a"]], _instruction_abs, "math");

ic._registerOpcode("log", [["r", "a"], ["r", "i", "f", "a"]], _instruction_log, "math");

ic._registerOpcode("exp", [["r", "a"], ["r", "i", "f", "a"]], _instruction_exp, "math");

ic._registerOpcode("rand", [["r", "a"]], _instruction_rand, "math");

ic._registerOpcode("sin", [["r", "a"], ["r", "i", "f", "a"]], _instruction_sin, "math");

ic._registerOpcode("asin", [["r", "a"], ["r", "i", "f", "a"]], _instruction_asin, "math");

ic._registerOpcode("tan", [["r", "a"], ["r", "i", "f", "a"]], _instruction_tan, "math");

ic._registerOpcode("atan", [["r", "a"], ["r", "i", "f", "a"]], _instruction_atan, "math");

ic._registerOpcode("atan2", [["r", "a"], ["r", "i", "f", "a"], ["r", "i", "f", "a"]], _instruction_atan2, "math");

ic._registerOpcode("cos", [["r", "a"], ["r", "i", "f", "a"]], _instruction_cos, "math");

ic._registerOpcode("acos", [["r", "a"], ["r", "i", "f", "a"]], _instruction_acos, "math");
};

function _instruction_add(fields, allowedTypes, ic) {
var outputValue = ic._getRegister(fields[1], undefined, allowedTypes[1]) + ic._getRegister(fields[2], undefined, allowedTypes[2]);
let outputValue = ic._getRegister(fields[1], undefined, allowedTypes[1]) + ic._getRegister(fields[2], undefined, allowedTypes[2]);

ic._setRegister(fields[0], outputValue, undefined, allowedTypes[0]);
}

function _instruction_sub(fields, allowedTypes, ic) {
var outputValue = ic._getRegister(fields[1], undefined, allowedTypes[1]) - ic._getRegister(fields[2], undefined, allowedTypes[2]);
let outputValue = ic._getRegister(fields[1], undefined, allowedTypes[1]) - ic._getRegister(fields[2], undefined, allowedTypes[2]);

ic._setRegister(fields[0], outputValue, undefined, allowedTypes[0]);
}

function _instruction_mul(fields, allowedTypes, ic) {
var outputValue = ic._getRegister(fields[1], undefined, allowedTypes[1]) * ic._getRegister(fields[2], undefined, allowedTypes[2]);
let outputValue = ic._getRegister(fields[1], undefined, allowedTypes[1]) * ic._getRegister(fields[2], undefined, allowedTypes[2]);

ic._setRegister(fields[0], outputValue, undefined, allowedTypes[0]);
}

function _instruction_div(fields, allowedTypes, ic) {
var outputValue = ic._getRegister(fields[1], undefined, allowedTypes[1]) / ic._getRegister(fields[2], undefined, allowedTypes[2]);
let outputValue = ic._getRegister(fields[1], undefined, allowedTypes[1]) / ic._getRegister(fields[2], undefined, allowedTypes[2]);

ic._setRegister(fields[0], outputValue, undefined, allowedTypes[0]);
}

function _instruction_mod(fields, allowedTypes, ic) {
var valueOne = ic._getRegister(fields[1], undefined, allowedTypes[1]);
var valueTwo = ic._getRegister(fields[2], undefined, allowedTypes[2]);
let valueOne = ic._getRegister(fields[1], undefined, allowedTypes[1]);

let valueTwo = ic._getRegister(fields[2], undefined, allowedTypes[2]);

let outputValue = valueOne % valueTwo;

var outputValue = valueOne % valueTwo;
if (outputValue < 0) {
outputValue += valueTwo;
}
Expand Down Expand Up @@ -78,12 +107,14 @@ function _instruction_floor(fields, allowedTypes, ic) {
}

function _instruction_max(fields, allowedTypes, ic) {
var outputValue = Math.max(ic._getRegister(fields[1], undefined, allowedTypes[1]), ic._getRegister(fields[2], undefined, allowedTypes[2]));
let outputValue = Math.max(ic._getRegister(fields[1], undefined, allowedTypes[1]), ic._getRegister(fields[2], undefined, allowedTypes[2]));

ic._setRegister(fields[0], outputValue, undefined, allowedTypes[0]);
}

function _instruction_min(fields, allowedTypes, ic) {
var outputValue = Math.min(ic._getRegister(fields[1], undefined, allowedTypes[1]), ic._getRegister(fields[2], undefined, allowedTypes[2]));
let outputValue = Math.min(ic._getRegister(fields[1], undefined, allowedTypes[1]), ic._getRegister(fields[2], undefined, allowedTypes[2]));

ic._setRegister(fields[0], outputValue, undefined, allowedTypes[0]);
}

Expand Down Expand Up @@ -119,6 +150,12 @@ function _instruction_atan(fields, allowedTypes, ic) {
ic._setRegister(fields[0], Math.atan(ic._getRegister(fields[1], undefined, allowedTypes[1])), undefined, allowedTypes[0]);
}

function _instruction_atan2(fields, allowedTypes, ic) {
let outputValue = Math.atan2(ic._getRegister(fields[1], undefined, allowedTypes[1]), ic._getRegister(fields[2], undefined, allowedTypes[2]));

ic._setRegister(fields[0], outputValue, undefined, allowedTypes[0]);
}

function _instruction_cos(fields, allowedTypes, ic) {
ic._setRegister(fields[0], Math.cos(ic._getRegister(fields[1], undefined, allowedTypes[1])), undefined, allowedTypes[0]);
}
Expand Down
5 changes: 3 additions & 2 deletions lib/instructions/Misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ module.exports = function (ic) {

ic._registerOpcode("sleep", [["r", "i", "f", "a"]], _instruction_sleep, "misc");
};

/* eslint no-unused-vars: 0 */


function _instruction_move(fields, allowedTypes, ic) {
var outputValue = ic._getRegister(fields[1], undefined, allowedTypes[1]);
let outputValue = ic._getRegister(fields[1], undefined, allowedTypes[1]);

ic._setRegister(fields[0], outputValue, undefined, allowedTypes[0]);
}

Expand Down
Loading