UNPKG

hdl-js

Version:

Hardware definition language (HDL) and Hardware simulator

127 lines (102 loc) 5.59 kB
/** * The MIT License (MIT) * Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com> */ 'use strict'; var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); 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'); /** * Canonical truth table for the MipsAlu */ // prettier-ignore var TRUTH_TABLE = [ // Add { a: 1, b: 1, na: 0, nb: 0, less: 0, cin: 0, op: 2, out: 0, cout: 1, set: 0 }, { a: 1, b: 1, na: 0, nb: 0, less: 0, cin: 1, op: 2, out: 1, cout: 1, set: 0 }, { a: 0, b: 0, na: 0, nb: 0, less: 0, cin: 1, op: 2, out: 1, cout: 0, set: 0 }, // Sub { a: 1, b: 1, na: 0, nb: 1, less: 0, cin: 1, op: 2, out: 0, cout: 1, set: 0 }, // And { a: 1, b: 1, na: 0, nb: 0, less: 0, cin: 0, op: 0, out: 1, cout: 0, set: 0 }, // Or { a: 1, b: 1, na: 0, nb: 0, less: 0, cin: 0, op: 1, out: 1, cout: 0, set: 0 }, { a: 0, b: 1, na: 0, nb: 0, less: 0, cin: 0, op: 1, out: 1, cout: 0, set: 0 }, { a: 0, b: 0, na: 0, nb: 0, less: 0, cin: 0, op: 1, out: 0, cout: 0, set: 0 }]; /** * A 1-bit MipsAlu */ var MipsAlu = function (_BuiltInGate) { _inherits(MipsAlu, _BuiltInGate); function MipsAlu() { _classCallCheck(this, MipsAlu); return _possibleConstructorReturn(this, (MipsAlu.__proto__ || Object.getPrototypeOf(MipsAlu)).apply(this, arguments)); } _createClass(MipsAlu, [{ key: 'eval', /** * Logic: * * a & b: op = 0 * a | b: op = 1 * a NOR b => ~a & ~b: op = 0, na = 1, nb = 1 * * Math: * * a + b: op = 2 * a - b: op = 2, nb = 1, cin = 1 * b - a: op = 2, na = 1, cin = 1 * */ value: function _eval() { var _getInputPins$map = this.getInputPins().map(function (pin) { return pin.getValue(); }), _getInputPins$map2 = _slicedToArray(_getInputPins$map, 7), a = _getInputPins$map2[0], b = _getInputPins$map2[1], na = _getInputPins$map2[2], nb = _getInputPins$map2[3], less = _getInputPins$map2[4], cin = _getInputPins$map2[5], op = _getInputPins$map2[6]; var A = na ? 1 - a : a; var B = nb ? 1 - b : b; switch (op) { case 0: { this.getOutputPins()[0].setValue(A & B); break; } case 1: { this.getOutputPins()[0].setValue(A | B); break; } case 2: { var sum = A + B + cin; this.getOutputPins()[0].setValue(sum % 2); this.getOutputPins()[1].setValue(Math.trunc(sum / 2)); break; } case 3: { this.getOutputPins()[0].setValue(less); break; } } } }]); return MipsAlu; }(BuiltInGate); /** * Specification of the `MipsAlu` gate. */ MipsAlu.Spec = { name: 'MipsAlu', description: '1-bit Mips Alu.\n\nImplements 1-bit MIPS ALU chip.\n\nThe actual ALU operation is controlled by the 2-bit "op" input, which takes the following values:\n\n- 0 - AND\n- 1 - OR\n- 2 - ADD\n- 3 - propagate "less" input\n\nOther operations can be achieved by manipulating different inputs, and the "op" code.\nFor example, to do a subtraction operation "a - b", we need to set cin = 1, and nb = 1.\n\nSetting inputs "na" and "nb" negates the inputs "a" and "b" before performing the operation.\n', inputPins: ['a', 'b', 'na', 'nb', 'less', 'cin', { name: 'op', size: 2 }], outputPins: ['out', 'cout', 'set'], truthTable: TRUTH_TABLE }; module.exports = MipsAlu;