UNPKG

hdl-js

Version:

Hardware definition language (HDL) and Hardware simulator

185 lines (147 loc) 6.66 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; }; }(); var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }; 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 Gate = require('./Gate'); var Pin = require('./Pin'); /** * Base class for all builtin gates. */ var BuiltInGate = function (_Gate) { _inherits(BuiltInGate, _Gate); /** * Creates a gate instance with the given name. */ function BuiltInGate(options) { _classCallCheck(this, BuiltInGate); var _this = _possibleConstructorReturn(this, (BuiltInGate.__proto__ || Object.getPrototypeOf(BuiltInGate)).call(this, options)); _this._validate(); return _this; } /** * Validates inputs, and outputs of this gate. */ _createClass(BuiltInGate, [{ key: '_validate', value: function _validate() { this._validatePins(this.getInputPins(), 'inputPins'); this._validatePins(this.getOutputPins(), 'outputPins'); } /** * Validates pin numbers. */ }, { key: '_validatePins', value: function _validatePins(pins, kind) { var _this2 = this; var spec = BuiltInGate.validateSpec(this.getClass().Spec); if (pins.length !== spec[kind].length) { throw new Error('"' + this._name + '" gate: expect ' + spec[kind].length + ' ' + kind + ' ' + ('(' + spec[kind].join(', ') + '), got ' + pins.length + '.')); } // Check that for sized-pins, a `Pin` is passed. spec[kind].forEach(function (pinName, index) { var size = typeof pinName === 'string' ? null : pinName.size; if (size && pins[index].getSize() !== size) { throw new TypeError('"' + _this2._name + '" gate: expect gate #' + index + ' from ' + kind + ' to have ' + ('size ' + size + ', ' + pins[index].getSize() + ' is given.')); } }); } /** * Returns HDL code for this built-in gate. * * Describes inputs/outputs with the BUILTIN <Name> part. */ }, { key: 'eval', /** * Evaluates this gate. */ value: function _eval() { // Noop. return; } /** * Handler for the rising edge of the clock: updates internal state, * outputs are not updated ("latched"). */ }, { key: 'clockUp', value: function clockUp() { // Noop. return; } /** * Handler for the falling edge of the clock: commits the internal state, * values to the output. */ }, { key: 'clockDown', value: function clockDown() { // Noop. return; } /** * Whether this gate is clocked. */ }], [{ key: 'getHDLCode', value: function getHDLCode() { if (!this._hdlCode) { var spec = this.Spec; var docBlock = spec.description.split('\n').map(function (line) { return ' * ' + line; }).join('\n'); var inputs = spec.inputPins.map(function (pin) { return Pin.toFullName(pin); }).join(', '); var outputs = spec.outputPins.map(function (pin) { return Pin.toFullName(pin); }).join(', '); this._hdlCode = '/**\n' + docBlock + '\n */\nCHIP ' + spec.name + ' {\n IN ' + inputs + ';\n OUT ' + outputs + ';\n\n BUILTIN ' + spec.name + ';\n}'; } return this._hdlCode; } }, { key: 'validateSpec', value: function validateSpec(spec) { return _get(BuiltInGate.__proto__ || Object.getPrototypeOf(BuiltInGate), 'validateSpec', this).call(this, spec, ['name', 'description', 'inputPins', 'outputPins', 'truthTable']); } /** * Prints truth table. */ }, { key: 'printTruthTable', value: function printTruthTable() { var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, _ref$table = _ref.table, table = _ref$table === undefined ? null : _ref$table, _ref$columns = _ref.columns, columns = _ref$columns === undefined ? [] : _ref$columns, formatRadix = _ref.formatRadix, formatStringLengh = _ref.formatStringLengh, _ref$transformValue = _ref.transformValue, transformValue = _ref$transformValue === undefined ? null : _ref$transformValue; _get(BuiltInGate.__proto__ || Object.getPrototypeOf(BuiltInGate), 'printTruthTable', this).call(this, { table: table || BuiltInGate.validateSpec(this.Spec).truthTable, columns: columns, formatRadix: formatRadix, formatStringLengh: formatStringLengh, transformValue: transformValue }); } }, { key: 'isClocked', value: function isClocked() { // Child classes can override. return false; } }]); return BuiltInGate; }(Gate); module.exports = BuiltInGate;