hdl-js
Version:
Hardware definition language (HDL) and Hardware simulator
97 lines (71 loc) • 4.97 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 `DMux8Way` gate.
*/
var TRUTH_TABLE = int16Table([{ in: 0, sel: 0, a: 0, b: 0, c: 0, d: 0, e: 0, f: 0, g: 0, h: 0 }, { in: 0, sel: 1, a: 0, b: 0, c: 0, d: 0, e: 0, f: 0, g: 0, h: 0 }, { in: 0, sel: 2, a: 0, b: 0, c: 0, d: 0, e: 0, f: 0, g: 0, h: 0 }, { in: 0, sel: 3, a: 0, b: 0, c: 0, d: 0, e: 0, f: 0, g: 0, h: 0 }, { in: 0, sel: 4, a: 0, b: 0, c: 0, d: 0, e: 0, f: 0, g: 0, h: 0 }, { in: 0, sel: 5, a: 0, b: 0, c: 0, d: 0, e: 0, f: 0, g: 0, h: 0 }, { in: 0, sel: 6, a: 0, b: 0, c: 0, d: 0, e: 0, f: 0, g: 0, h: 0 }, { in: 0, sel: 7, a: 0, b: 0, c: 0, d: 0, e: 0, f: 0, g: 0, h: 0 }, { in: 1, sel: 0, a: 1, b: 0, c: 0, d: 0, e: 0, f: 0, g: 0, h: 0 }, { in: 1, sel: 1, a: 0, b: 1, c: 0, d: 0, e: 0, f: 0, g: 0, h: 0 }, { in: 1, sel: 2, a: 0, b: 0, c: 1, d: 0, e: 0, f: 0, g: 0, h: 0 }, { in: 1, sel: 3, a: 0, b: 0, c: 0, d: 1, e: 0, f: 0, g: 0, h: 0 }, { in: 1, sel: 4, a: 0, b: 0, c: 0, d: 0, e: 1, f: 0, g: 0, h: 0 }, { in: 1, sel: 5, a: 0, b: 0, c: 0, d: 0, e: 0, f: 1, g: 0, h: 0 }, { in: 1, sel: 6, a: 0, b: 0, c: 0, d: 0, e: 0, f: 0, g: 1, h: 0 }, { in: 1, sel: 7, a: 0, b: 0, c: 0, d: 0, e: 0, f: 0, g: 0, h: 1 }]);
/**
* 1 bit 8-way demultiplexor.
*
* The 3-bit sel choose to which output to channel the input (0->a .. 7->h).
* The other outputs are set to 0.
*
* {a, b, c, d, e, f, g, h} = {in, 0, 0, 0, 0, 0, 0, 0} if sel == 000
* {0, in, 0, 0, 0, 0, 0, 0} if sel == 001
* ...
* {0, 0, 0, 0, 0, 0, 0, in} if sel == 111
*/
var DMux8Way = function (_BuiltInGate) {
_inherits(DMux8Way, _BuiltInGate);
function DMux8Way() {
_classCallCheck(this, DMux8Way);
return _possibleConstructorReturn(this, (DMux8Way.__proto__ || Object.getPrototypeOf(DMux8Way)).apply(this, arguments));
}
_createClass(DMux8Way, [{
key: 'eval',
/**
* IN in, sel[3];
* OUT a, b, c, d, e, f, g, h;
*
* Abstract:
*
* DMux(in=in, sel=sel[2], a=abcd, b=efgh);
* DMux4Way(in=abcd, sel=sel[0..1], a=a, b=b, c=c, d=d);
* DMux4Way(in=efgh, sel=sel[0..1], a=e, b=f, c=g, d=h);
*/
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);
this.getOutputPins()[4].setValue(sel === 4 ? _in : 0);
this.getOutputPins()[5].setValue(sel === 5 ? _in : 0);
this.getOutputPins()[6].setValue(sel === 6 ? _in : 0);
this.getOutputPins()[7].setValue(sel === 7 ? _in : 0);
}
}]);
return DMux8Way;
}(BuiltInGate);
/**
* Specification of the `DMux8Way` gate.
*/
DMux8Way.Spec = {
name: 'DMux8Way',
description: ['1 bit 8-way demultiplexor.', '', 'The 3-bit sel choose to which output to channel the input (0->a .. 7->h).', 'The other outputs are set to 0.'].join('\n'),
inputPins: ['in', { name: 'sel', size: 3 }],
outputPins: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
truthTable: TRUTH_TABLE
};
module.exports = DMux8Way;