hdl-js
Version:
Hardware definition language (HDL) and Hardware simulator
97 lines (72 loc) • 3.39 kB
JavaScript
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
;
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 colors = require('colors');
var BuiltInGate = require('../BuiltInGate');
/**
* Canonical truth table for the `DFF` gate.
*/
var TRUTH_TABLE = [{ $clock: -0, in: 0, out: 0 }, { $clock: +0, in: 1, out: 0 }, { $clock: -1, in: 0, out: 1 }, { $clock: +1, in: 0, out: 0 }];
/**
* Data/Delay Flip-Flop chip.
*/
var DFF = function (_BuiltInGate) {
_inherits(DFF, _BuiltInGate);
function DFF() {
_classCallCheck(this, DFF);
return _possibleConstructorReturn(this, (DFF.__proto__ || Object.getPrototypeOf(DFF)).apply(this, arguments));
}
_createClass(DFF, [{
key: 'init',
value: function init() {
/**
* The state (0/1) of the D-flip-flop.
*/
this._state = 0;
}
/**
* On rising edge DFF updates the internal state
* from the input pin.
*/
}, {
key: 'clockUp',
value: function clockUp() {
this._state = this.getInputPins()[0].getValue();
}
/**
* On the falling edge DFF propagates the state
* to the output pin.
*/
}, {
key: 'clockDown',
value: function clockDown() {
this.getOutputPins()[0].setValue(this._state);
}
}], [{
key: 'isClocked',
/**
* DFF is a sequential gate.
*/
value: function isClocked() {
return true;
}
}]);
return DFF;
}(BuiltInGate);
/**
* Specification of the `DFF` gate.
*/
DFF.Spec = {
name: 'DFF',
description: ['DFF (Data/Delay Flip-Flop) chip.', '', 'Clock rising edge updates internal state from the input:', '', ' ' + colors.bold('↗') + ' : state = in', '', 'Clock falling edge propagates the internal state to the output:', '', ' ' + colors.bold('↘') + ' : out = state'].join('\n'),
inputPins: ['in'],
outputPins: ['out'],
truthTable: TRUTH_TABLE
};
module.exports = DFF;