hdl-js
Version:
Hardware definition language (HDL) and Hardware simulator
164 lines (122 loc) • 8.36 kB
JavaScript
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var BuiltInGate = require('../BuiltInGate');
var _require = require('../../../util/numbers'),
int16Table = _require.int16Table,
int16 = _require.int16;
/**
* Canonical truth table for the `ALU` gate.
*/
// prettier-ignore
var TRUTH_TABLE = int16Table([{ x: 0, y: 65535, zx: 1, nx: 0, zy: 1, ny: 0, f: 1, no: 0, out: 0, zr: 1, ng: 0 }, { x: 0, y: 65535, zx: 1, nx: 1, zy: 1, ny: 1, f: 1, no: 1, out: 1, zr: 0, ng: 0 }, { x: 0, y: 65535, zx: 1, nx: 1, zy: 1, ny: 0, f: 1, no: 0, out: 65535, zr: 0, ng: 1 }, { x: 0, y: 65535, zx: 0, nx: 0, zy: 1, ny: 1, f: 0, no: 0, out: 0, zr: 1, ng: 0 }, { x: 0, y: 65535, zx: 1, nx: 1, zy: 0, ny: 0, f: 0, no: 0, out: 65535, zr: 0, ng: 1 }, { x: 0, y: 65535, zx: 0, nx: 0, zy: 1, ny: 1, f: 0, no: 1, out: 65535, zr: 0, ng: 1 }, { x: 0, y: 65535, zx: 1, nx: 1, zy: 0, ny: 0, f: 0, no: 1, out: 0, zr: 1, ng: 0 }, { x: 0, y: 65535, zx: 0, nx: 0, zy: 1, ny: 1, f: 1, no: 1, out: 0, zr: 1, ng: 0 }, { x: 0, y: 65535, zx: 1, nx: 1, zy: 0, ny: 0, f: 1, no: 1, out: 1, zr: 0, ng: 0 }, { x: 0, y: 65535, zx: 0, nx: 1, zy: 1, ny: 1, f: 1, no: 1, out: 1, zr: 0, ng: 0 }, { x: 0, y: 65535, zx: 1, nx: 1, zy: 0, ny: 1, f: 1, no: 1, out: 0, zr: 1, ng: 0 }, { x: 0, y: 65535, zx: 0, nx: 0, zy: 1, ny: 1, f: 1, no: 0, out: 65535, zr: 0, ng: 1 }, { x: 0, y: 65535, zx: 1, nx: 1, zy: 0, ny: 0, f: 1, no: 0, out: 65534, zr: 0, ng: 1 }, { x: 0, y: 65535, zx: 0, nx: 0, zy: 0, ny: 0, f: 1, no: 0, out: 65535, zr: 0, ng: 1 }, { x: 0, y: 65535, zx: 0, nx: 1, zy: 0, ny: 0, f: 1, no: 1, out: 1, zr: 0, ng: 0 }, { x: 0, y: 65535, zx: 0, nx: 0, zy: 0, ny: 1, f: 1, no: 1, out: 65535, zr: 0, ng: 1 }, { x: 0, y: 65535, zx: 0, nx: 0, zy: 0, ny: 0, f: 0, no: 0, out: 0, zr: 1, ng: 0 }, { x: 0, y: 65535, zx: 0, nx: 1, zy: 0, ny: 1, f: 0, no: 1, out: 65535, zr: 0, ng: 1 }, { x: 17, y: 3, zx: 1, nx: 0, zy: 1, ny: 0, f: 1, no: 0, out: 0, zr: 1, ng: 0 }, { x: 17, y: 3, zx: 1, nx: 1, zy: 1, ny: 1, f: 1, no: 1, out: 1, zr: 0, ng: 0 }, { x: 17, y: 3, zx: 1, nx: 1, zy: 1, ny: 0, f: 1, no: 0, out: 65535, zr: 0, ng: 1 }, { x: 17, y: 3, zx: 0, nx: 0, zy: 1, ny: 1, f: 0, no: 0, out: 17, zr: 0, ng: 0 }, { x: 17, y: 3, zx: 1, nx: 1, zy: 0, ny: 0, f: 0, no: 0, out: 3, zr: 0, ng: 0 }, { x: 17, y: 3, zx: 0, nx: 0, zy: 1, ny: 1, f: 0, no: 1, out: 65518, zr: 0, ng: 1 }, { x: 17, y: 3, zx: 1, nx: 1, zy: 0, ny: 0, f: 0, no: 1, out: 65532, zr: 0, ng: 1 }, { x: 17, y: 3, zx: 0, nx: 0, zy: 1, ny: 1, f: 1, no: 1, out: 65519, zr: 0, ng: 1 }, { x: 17, y: 3, zx: 1, nx: 1, zy: 0, ny: 0, f: 1, no: 1, out: 65533, zr: 0, ng: 1 }, { x: 17, y: 3, zx: 0, nx: 1, zy: 1, ny: 1, f: 1, no: 1, out: 18, zr: 0, ng: 0 }, { x: 17, y: 3, zx: 1, nx: 1, zy: 0, ny: 1, f: 1, no: 1, out: 4, zr: 0, ng: 0 }, { x: 17, y: 3, zx: 0, nx: 0, zy: 1, ny: 1, f: 1, no: 0, out: 16, zr: 0, ng: 0 }, { x: 17, y: 3, zx: 1, nx: 1, zy: 0, ny: 0, f: 1, no: 0, out: 2, zr: 0, ng: 0 }, { x: 17, y: 3, zx: 0, nx: 0, zy: 0, ny: 0, f: 1, no: 0, out: 20, zr: 0, ng: 0 }, { x: 17, y: 3, zx: 0, nx: 1, zy: 0, ny: 0, f: 1, no: 1, out: 14, zr: 0, ng: 0 }, { x: 17, y: 3, zx: 0, nx: 0, zy: 0, ny: 1, f: 1, no: 1, out: 65522, zr: 0, ng: 1 }, { x: 17, y: 3, zx: 0, nx: 0, zy: 0, ny: 0, f: 0, no: 0, out: 1, zr: 0, ng: 0 }, { x: 17, y: 3, zx: 0, nx: 1, zy: 0, ny: 1, f: 0, no: 1, out: 19, zr: 0, ng: 0 }]);
/**
* The ALU (Arithmetic Logic Unit).
*
* Computes one of the following functions:
*
* x+y, x-y, y-x, 0, 1, -1, x, y, -x, -y, !x, !y,
* x+1, y+1, x-1, y-1, x&y, x|y on two 16-bit inputs,
* according to 6 input bits denoted zx, nx, zy, ny, f, no.
*
* In addition, the ALU computes two 1-bit outputs:
*
* if the ALU output == 0, zr is set to 1; otherwise zr is set to 0;
* if the ALU output < 0, ng is set to 1; otherwise ng is set to 0.
*
* Implementation: the ALU logic manipulates the x and y inputs
* and operates on the resulting values, as follows:
*
* if (zx === 1) set x = 0 // 16-bit constant
* if (nx === 1) set x = ~x // bitwise not
* if (zy === 1) set y = 0 // 16-bit constant
* if (ny === 1) set y = ~y // bitwise not
* if (f === 1) set out = x + y // integer 2's complement addition
* if (f === 0) set out = x & y // bitwise and
* if (no === 1) set out = !out // bitwise not
* if (out === 0) set zr = 1
* if (out < 0) set ng = 1
*/
var ALU = function (_BuiltInGate) {
_inherits(ALU, _BuiltInGate);
function ALU() {
_classCallCheck(this, ALU);
return _possibleConstructorReturn(this, (ALU.__proto__ || Object.getPrototypeOf(ALU)).apply(this, arguments));
}
_createClass(ALU, [{
key: 'eval',
/**
* Evaluates ALU logic.
*/
value: function _eval() {
var x = this.getInputPins()[0].getValue();
var y = this.getInputPins()[1].getValue();
var zx = this.getInputPins()[2].getValue();
var nx = this.getInputPins()[3].getValue();
var zy = this.getInputPins()[4].getValue();
var ny = this.getInputPins()[5].getValue();
var f = this.getInputPins()[6].getValue();
var no = this.getInputPins()[7].getValue();
var out = void 0;
if (zx === 1) {
x = 0;
}
if (zy === 1) {
y = 0;
}
if (nx === 1) {
x = int16(~x);
}
if (ny === 1) {
y = int16(~y);
}
if (f === 1) {
out = int16(x + y);
} else if (f === 0) {
out = int16(x & y);
}
if (no === 1) {
out = int16(~out);
}
this.getOutputPins()[0].setValue(out); // main out
this.getOutputPins()[1].setValue(out === 0 ? 1 : 0); // zr
this.getOutputPins()[2].setValue(out < 0 ? 1 : 0); // ng
}
}]);
return ALU;
}(BuiltInGate);
/**
* Specification of the `ALU` gate.
*/
ALU.Spec = {
name: 'ALU',
description: 'The ALU (Arithmetic Logic Unit).\n\nComputes one of the following functions:\n\nx+y, x-y, y-x, 0, 1, -1, x, y, -x, -y, !x, !y,\nx+1, y+1, x-1, y-1, x&y, x|y on two 16-bit inputs,\naccording to 6 input bits denoted zx, nx, zy, ny, f, no.\n\nIn addition, the ALU computes two 1-bit outputs:\n\nif the ALU output == 0, zr is set to 1; otherwise zr is set to 0;\nif the ALU output < 0, ng is set to 1; otherwise ng is set to 0.\n',
inputPins: [
// Main inputs.
{ name: 'x', size: 16 }, { name: 'y', size: 16 },
// Should zero `x`?
{ name: 'zx', size: 1 },
// Should negate `x`?
{ name: 'nx', size: 1 },
// Should zero `y`?
{ name: 'zy', size: 1 },
// Should negate `y`?
{ name: 'ny', size: 1 },
// Opcode: 1 - And, 0 - Add.
{ name: 'f', size: 1 },
// Should negate `out`?
{ name: 'no', size: 1 }],
outputPins: [{ name: 'out', size: 16 },
// Is result zero?
{ name: 'zr', size: 1 },
// Is result negative?
{ name: 'ng', size: 1 }],
truthTable: TRUTH_TABLE
};
module.exports = ALU;