UNPKG

hdl-js

Version:

Hardware definition language (HDL) and Hardware simulator

110 lines (84 loc) 3.98 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 colors = require('colors'); var BuiltInGate = require('../BuiltInGate'); /** * Canonical truth table for the `Bit` gate. */ var TRUTH_TABLE = [{ $clock: -0, in: 0, load: 0, out: 0 }, { $clock: +0, in: 1, load: 1, out: 0 }, { $clock: -1, in: 1, load: 0, out: 1 }, { $clock: +1, in: 1, load: 0, out: 1 }, { $clock: -2, in: 1, load: 0, out: 1 }, { $clock: +2, in: 0, load: 1, out: 1 }, { $clock: -3, in: 0, load: 0, out: 0 }]; /** * 1 bit memory register. * If load[t]=1 then out[t+1] = in[t] else out does not change. * * Abstract: * * IN in, load; * OUT out; * * Mux(a=t0, b=in, sel=load, out=t1); * DFF(in=t1, out=t0, out=out); */ var Bit = function (_BuiltInGate) { _inherits(Bit, _BuiltInGate); function Bit() { _classCallCheck(this, Bit); return _possibleConstructorReturn(this, (Bit.__proto__ || Object.getPrototypeOf(Bit)).apply(this, arguments)); } _createClass(Bit, [{ key: 'init', value: function init() { /** * The state (0/1) of the bit. */ this._state = 0; } /** * On rising edge Bit updates the internal state * if the `load` is set, otherwise -- preserves the state. */ }, { key: 'clockUp', value: function clockUp() { var load = this.getInputPins()[1].getValue(); if (load) { this._state = this.getInputPins()[0].getValue(); } } /** * On the falling edge Bit propagates the state * to the output pin. */ }, { key: 'clockDown', value: function clockDown() { this.getOutputPins()[0].setValue(this._state); } }], [{ key: 'isClocked', /** * Bit is a sequential gate. */ value: function isClocked() { return true; } }]); return Bit; }(BuiltInGate); /** * Specification of the `Bit` gate. */ Bit.Spec = { name: 'Bit', description: ['1 bit memory register.', '', 'If load[t]=1 then out[t+1] = in[t] else out does not change.', '', 'Clock rising edge updates internal state from the input,', 'if the `load` is set; otherwise, preserves the state.', '', ' ' + colors.bold('↗') + ' : state = load ? in : state', '', 'Clock falling edge propagates the internal state to the output:', '', ' ' + colors.bold('↘') + ' : out = state'].join('\n'), inputPins: ['in', 'load'], outputPins: ['out'], truthTable: TRUTH_TABLE }; module.exports = Bit;