UNPKG

hdl-js

Version:

Hardware definition language (HDL) and Hardware simulator

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