hdl-js
Version:
Hardware definition language (HDL) and Hardware simulator
311 lines (281 loc) • 6.07 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 `Mux8Way16` gate.
*/
var TRUTH_TABLE = int16Table([{
a: 0,
b: 0,
c: 0,
d: 0,
e: 0,
f: 0,
g: 0,
h: 0,
sel: 0,
out: 0
}, {
a: 0,
b: 0,
c: 0,
d: 0,
e: 0,
f: 0,
g: 0,
h: 0,
sel: 1,
out: 0
}, {
a: 0,
b: 0,
c: 0,
d: 0,
e: 0,
f: 0,
g: 0,
h: 0,
sel: 2,
out: 0
}, {
a: 0,
b: 0,
c: 0,
d: 0,
e: 0,
f: 0,
g: 0,
h: 0,
sel: 3,
out: 0
}, {
a: 0,
b: 0,
c: 0,
d: 0,
e: 0,
f: 0,
g: 0,
h: 0,
sel: 4,
out: 0
}, {
a: 0,
b: 0,
c: 0,
d: 0,
e: 0,
f: 0,
g: 0,
h: 0,
sel: 5,
out: 0
}, {
a: 0,
b: 0,
c: 0,
d: 0,
e: 0,
f: 0,
g: 0,
h: 0,
sel: 6,
out: 0
}, {
a: 0,
b: 0,
c: 0,
d: 0,
e: 0,
f: 0,
g: 0,
h: 0,
sel: 7,
out: 0
}, {
a: 4660,
b: 9029,
c: 13398,
d: 17767,
e: 22136,
f: 26505,
g: 30874,
h: 35243,
sel: 0,
out: 4660
}, {
a: 4660,
b: 9029,
c: 13398,
d: 17767,
e: 22136,
f: 26505,
g: 30874,
h: 35243,
sel: 1,
out: 9029
}, {
a: 4660,
b: 9029,
c: 13398,
d: 17767,
e: 22136,
f: 26505,
g: 30874,
h: 35243,
sel: 2,
out: 13398
}, {
a: 4660,
b: 9029,
c: 13398,
d: 17767,
e: 22136,
f: 26505,
g: 30874,
h: 35243,
sel: 3,
out: 17767
}, {
a: 4660,
b: 9029,
c: 13398,
d: 17767,
e: 22136,
f: 26505,
g: 30874,
h: 35243,
sel: 4,
out: 22136
}, {
a: 4660,
b: 9029,
c: 13398,
d: 17767,
e: 22136,
f: 26505,
g: 30874,
h: 35243,
sel: 5,
out: 26505
}, {
a: 4660,
b: 9029,
c: 13398,
d: 17767,
e: 22136,
f: 26505,
g: 30874,
h: 35243,
sel: 6,
out: 30874
}, {
a: 4660,
b: 9029,
c: 13398,
d: 17767,
e: 22136,
f: 26505,
g: 30874,
h: 35243,
sel: 7,
out: 35243
}]);
/**
* 8-way 16-bit multiplexor.
*
* The three sel[0..2] bits select the output to be one of the eight input buses
* (0->a ... 7->h).
*
* out = a if sel == 000
* b if sel == 001
* ...
* h if sel == 111
*/
var Mux8Way16 = function (_BuiltInGate) {
_inherits(Mux8Way16, _BuiltInGate);
function Mux8Way16() {
_classCallCheck(this, Mux8Way16);
return _possibleConstructorReturn(this, (Mux8Way16.__proto__ || Object.getPrototypeOf(Mux8Way16)).apply(this, arguments));
}
_createClass(Mux8Way16, [{
key: 'eval',
/**
* IN a[16], b[16], c[16], d[16],
* e[16], f[16], g[16], h[16],
* sel[3];
*
* OUT out[16];
*
* Abstract:
*
* Mux4Way16(a=a, b=b, c=c, d=d, sel=sel[0..1], out=abcd);
* Mux4Way16(a=e, b=f, c=g, d=h, sel=sel[0..1], out=efgh);
* Mux16(a=abcd, b=efgh, sel=sel[2], 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 e = this.getInputPins()[4].getValue();
var f = this.getInputPins()[5].getValue();
var g = this.getInputPins()[6].getValue();
var h = this.getInputPins()[7].getValue();
var sel = this.getInputPins()[8].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;
case 4:
out = e;
break;
case 5:
out = f;
break;
case 6:
out = g;
break;
case 7:
out = h;
break;
}
this.getOutputPins()[0].setValue(out);
}
}]);
return Mux8Way16;
}(BuiltInGate);
/**
* Specification of the `Mux8Way16` gate.
*/
Mux8Way16.Spec = {
name: 'Mux8Way16',
description: ['8-way 16-bit multiplexor.', '', 'The three sel[0..2] bits select the output to be one ', 'of the eight input buses: (0->a ... 7->h).'].join('\n'),
inputPins: [
// Data pins.
{ name: 'a', size: 16 }, { name: 'b', size: 16 }, { name: 'c', size: 16 }, { name: 'd', size: 16 }, { name: 'e', size: 16 }, { name: 'f', size: 16 }, { name: 'g', size: 16 }, { name: 'h', size: 16 },
// Selector.
{ name: 'sel', size: 3 }],
outputPins: [{ name: 'out', size: 16 }],
truthTable: TRUTH_TABLE
};
module.exports = Mux8Way16;