UNPKG

hdl-js

Version:

Hardware definition language (HDL) and Hardware simulator

166 lines (136 loc) 4.22 kB
/** * 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; /** * Canonical truth table for the `Mux4Way16` gate. */ var TRUTH_TABLE = int16Table([{ a: 0, b: 0, c: 0, d: 0, sel: 0, out: 0 }, { a: 0, b: 0, c: 0, d: 0, sel: 1, out: 0 }, { a: 0, b: 0, c: 0, d: 0, sel: 2, out: 0 }, { a: 0, b: 0, c: 0, d: 0, sel: 3, out: 0 }, { a: 4660, b: 39030, c: 43690, d: 21845, sel: 0, out: 4660 }, { a: 4660, b: 39030, c: 43690, d: 21845, sel: 1, out: 39030 }, { a: 4660, b: 39030, c: 43690, d: 21845, sel: 2, out: 43690 }, { a: 4660, b: 39030, c: 43690, d: 21845, sel: 3, out: 21845 }]); /** * 4-way 16-bit multiplexor. * The two sel[0..1] bits select the output to be one of the four input buses: * (0->a ... 3->d). */ var Mux4Way16 = function (_BuiltInGate) { _inherits(Mux4Way16, _BuiltInGate); function Mux4Way16() { _classCallCheck(this, Mux4Way16); return _possibleConstructorReturn(this, (Mux4Way16.__proto__ || Object.getPrototypeOf(Mux4Way16)).apply(this, arguments)); } _createClass(Mux4Way16, [{ key: 'eval', /** * IN a[16], b[16], c[16], d[16], sel[2]; * OUT out[16]; * * Abstract: * * Mux16(a=a, b=b, sel=sel[0], out=ab); * Mux16(a=c, b=d, sel=sel[0], out=cd); * Mux16(a=ab, b=cd, sel=sel[1], out=out); */ value: function _eval() { var a = this.getInputPins()[0].getValue(); var b = this.getInputPins()[1].getValue(); var c = this.getInputPins()[2].getValue(); var d = this.getInputPins()[3].getValue(); var sel = this.getInputPins()[4].getValue(); var out = 0; switch (sel) { case 0: out = a; break; case 1: out = b; break; case 2: out = c; break; case 3: out = d; break; } this.getOutputPins()[0].setValue(out); } }]); return Mux4Way16; }(BuiltInGate); /** * Specification of the `Mux4Way16` gate. */ Mux4Way16.Spec = { name: 'Mux4Way16', description: ['4-way 16-bit multiplexor gate.', '', 'The two sel[0..1] bits select the output to be one ', 'of the four input buses: (0->a ... 3->d).'].join('\n'), inputPins: [ // Data pins. { name: 'a', size: 16 }, { name: 'b', size: 16 }, { name: 'c', size: 16 }, { name: 'd', size: 16 }, // Selector. { name: 'sel', size: 2 }], outputPins: [{ name: 'out', size: 16 }], truthTable: TRUTH_TABLE }; module.exports = Mux4Way16;