UNPKG

hdl-js

Version:

Hardware definition language (HDL) and Hardware simulator

149 lines (114 loc) 5.42 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 `PC` gate. */ var TRUTH_TABLE = int16Table([{ $clock: -0, in: 0, load: 0, inc: 0, reset: 0, out: 0 }, // PC = 5 { $clock: +0, in: 5, load: 1, inc: 0, reset: 0, out: 0 }, { $clock: -1, in: 5, load: 1, inc: 0, reset: 0, out: 5 }, // PC++ { $clock: +1, in: 5, load: 0, inc: 1, reset: 0, out: 5 }, { $clock: -2, in: 5, load: 0, inc: 1, reset: 0, out: 6 }, // PC++ { $clock: +2, in: 6, load: 0, inc: 1, reset: 0, out: 6 }, { $clock: -3, in: 6, load: 0, inc: 1, reset: 0, out: 7 }, // Reset (PC = 0) { $clock: +3, in: 6, load: 0, inc: 0, reset: 1, out: 6 }, { $clock: -4, in: 6, load: 0, inc: 0, reset: 1, out: 0 }, // Preserve { $clock: +4, in: 0, load: 0, inc: 0, reset: 0, out: 0 }, { $clock: -5, in: 0, load: 0, inc: 0, reset: 0, out: 0 }]); /** * A 16-bit counter with load and reset controls. * * out[t+1] = 0, when reset[t] = 1 * out[t+1] = in[t], when load[t] = 1 * out[t+1] = out[t] + 1, when inc[t] = 1 (default counting behavior) * out[t+1] = out[t], otherwise (preserves as a register) * * Abstract: * * IN in[16], load, inc, reset; * OUT out[16]; * * Inc16(in=t0, out=t1); * Mux16(a=t0, b=t1, sel=inc, out=t2); * ... * Register(in=t4, load=true, out=t0, out=out); */ var PC = function (_BuiltInGate) { _inherits(PC, _BuiltInGate); function PC() { _classCallCheck(this, PC); return _possibleConstructorReturn(this, (PC.__proto__ || Object.getPrototypeOf(PC)).apply(this, arguments)); } _createClass(PC, [{ key: 'init', value: function init() { /** * The 16-bit value of the PC register. */ this._value = 0; } /** * On rising edge PC register updates the internal * value according to the logic. */ }, { key: 'clockUp', value: function clockUp() { var _in = this.getInputPins()[0].getValue(); var load = this.getInputPins()[1].getValue(); var inc = this.getInputPins()[2].getValue(); var reset = this.getInputPins()[3].getValue(); if (reset === 1) { this._value = 0; } else if (load === 1) { this._value = int16(_in); } else if (inc === 1) { this._value++; } } /** * On the falling edge PC register propagates * the value to the output pin. */ }, { key: 'clockDown', value: function clockDown() { this.getOutputPins()[0].setValue(this._value); } }], [{ key: 'isClocked', /** * PC is a sequential gate. */ value: function isClocked() { return true; } }]); return PC; }(BuiltInGate); /** * Specification of the `PC` gate. */ PC.Spec = { name: 'PC', description: '\nA 16-bit counter with load and reset controls.\n\nout[t+1] = 0, when reset[t] = 1\nout[t+1] = in[t], when load[t] = 1\nout[t+1] = out[t] + 1, when inc[t] = 1 (default counting behavior)\nout[t+1] = out[t], otherwise, preserves the value\n\nClock rising edge PC updates the value from the input,\nif the `load` is set; otherwise, preserves the state.\n\n ' + colors.bold('↗') + ' : value = in, when ' + colors.bold('load') + '\n 0, when ' + colors.bold('reset') + '\n +1, when ' + colors.bold('inc') + '\n\nClock falling edge PC propagates the value to the output:\n\n ' + colors.bold('↘') + ' : out = value\n', inputPins: [ // 16-bit value. { name: 'in', size: 16 }, // Control bits. { name: 'load', size: 1 }, { name: 'inc', size: 1 }, { name: 'reset', size: 1 }], outputPins: [{ name: 'out', size: 16 }], truthTable: TRUTH_TABLE }; module.exports = PC;