hdl-js
Version:
Hardware definition language (HDL) and Hardware simulator
93 lines (68 loc) • 3.84 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;
/**
* Canonical truth table for the `DMux4Way` gate.
*/
var TRUTH_TABLE = int16Table([{ in: 0, sel: 0, a: 0, b: 0, c: 0, d: 0 }, { in: 0, sel: 1, a: 0, b: 0, c: 0, d: 0 }, { in: 0, sel: 2, a: 0, b: 0, c: 0, d: 0 }, { in: 0, sel: 3, a: 0, b: 0, c: 0, d: 0 }, { in: 1, sel: 0, a: 1, b: 0, c: 0, d: 0 }, { in: 1, sel: 1, a: 0, b: 1, c: 0, d: 0 }, { in: 1, sel: 2, a: 0, b: 0, c: 1, d: 0 }, { in: 1, sel: 3, a: 0, b: 0, c: 0, d: 1 }]);
/**
* 1 bit 4-way demultiplexor.
*
* The 2-bit sel choose to which output to channel the input (0->a .. 3->d).
* The other outputs are set to 0.
*
* {a, b, c, d} = {in, 0, 0, 0} if sel == 00
* {0, in, 0, 0} if sel == 01
* {0, 0, in, 0} if sel == 10
* {0, 0, 0, in} if sel == 11
*
* Abstract:
*
* IN in, sel[2];
* OUT a, b, c, d;
*
* PARTS:
*
* DMux(in=in, sel=sel[1], a=ab, b=cd);
* DMux(in=ab, sel=sel[0], a=a, b=b);
* DMux(in=cd, sel=sel[0], a=c, b=d);
*/
var DMux4Way = function (_BuiltInGate) {
_inherits(DMux4Way, _BuiltInGate);
function DMux4Way() {
_classCallCheck(this, DMux4Way);
return _possibleConstructorReturn(this, (DMux4Way.__proto__ || Object.getPrototypeOf(DMux4Way)).apply(this, arguments));
}
_createClass(DMux4Way, [{
key: 'eval',
value: function _eval() {
var _in = this.getInputPins()[0].getValue();
var sel = this.getInputPins()[1].getValue();
this.getOutputPins()[0].setValue(sel === 0 ? _in : 0);
this.getOutputPins()[1].setValue(sel === 1 ? _in : 0);
this.getOutputPins()[2].setValue(sel === 2 ? _in : 0);
this.getOutputPins()[3].setValue(sel === 3 ? _in : 0);
}
}]);
return DMux4Way;
}(BuiltInGate);
/**
* Specification of the `DMux4Way` gate.
*/
DMux4Way.Spec = {
name: 'DMux4Way',
description: ['1 bit 4-way demultiplexor.', '', 'The 2-bit sel choose to which output to channel the input (0->a .. 3->d).', 'The other outputs are set to 0.'].join('\n'),
inputPins: ['in', { name: 'sel', size: 2 }],
outputPins: ['a', 'b', 'c', 'd'],
truthTable: TRUTH_TABLE
};
module.exports = DMux4Way;