UNPKG

hdl-js

Version:

Hardware definition language (HDL) and Hardware simulator

272 lines (219 loc) 6.89 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; /** * Canonical truth table for the `RAM` gate. */ var TRUTH_TABLE = int16Table([{ $clock: -0, in: 0, load: 0, address: 0, out: 0 }, { $clock: +0, in: 21, load: 1, address: 0, out: 0 }, { $clock: -1, in: 1, load: 0, address: 0, out: 21 }, { $clock: +1, in: 21, load: 0, address: 0, out: 21 }, { $clock: -2, in: 21, load: 0, address: 0, out: 21 }, { $clock: +2, in: 53781, load: 1, address: 2, out: 0 }, { $clock: -3, in: 53781, load: 0, address: 2, out: 53781 }]); /** * RAM chip of a variable size, each memory location is 16 bit-wide. * * The output is the value stored at the memory location specified by address. * If load=1, loads the input into the memory location specified by address. * * Abstract: * * IN in[16], load, address[3]; * OUT out[16]; * * DMux8Way(in=load, sel=address, ...); * Register(in=in, load=l1, out=r1); * Register(in=in, load=l2, out=r2); * ... * Mux8Way16(...); */ var RAM = function (_BuiltInGate) { _inherits(RAM, _BuiltInGate); /** * The size is the number of registers (machine words). */ function RAM(options) { _classCallCheck(this, RAM); var _this = _possibleConstructorReturn(this, (RAM.__proto__ || Object.getPrototypeOf(RAM)).call(this, options)); _this._size = options.size || 8; _this._storage = new Int16Array(_this.getSize()); // Addresses which are updated (used in `reset`). _this._modifiedAddresses = new Set(); return _this; } /** * RAM is a sequential gate. */ _createClass(RAM, [{ key: 'getSize', /** * Number of machine words (registers) in this memory unit. */ value: function getSize() { return this._size; } /** * Returns the storage. */ }, { key: 'getStroage', value: function getStroage() { return this._storage; } /** * Sets storage. */ }, { key: 'setStorage', value: function setStorage(storage) { this._storage = storage; return this; } /** * Returns values at address. */ }, { key: 'getValueAt', value: function getValueAt(address) { return this._storage[this._checkAddress(address)]; } /** * Returns values at address. */ }, { key: 'setValueAt', value: function setValueAt(address, value) { this._storage[this._checkAddress(address)] = value; this._modifiedAddresses.add(address); return this; } /** * Resets the memory to all zeros. */ }, { key: 'reset', value: function reset() { var _this2 = this; this._modifiedAddresses.forEach(function (address) { _this2._storage[address] = 0; }); this._modifiedAddresses.clear(); return this; } /** * Checks address range. */ }, { key: '_checkAddress', value: function _checkAddress(address) { if (address < 0 || address > this._size - 1) { throw new TypeError('Chip "' + this.getClass().name + '": invalid address ' + address + ', ' + ('the max address is ' + (this._size - 1) + '.')); } return address; } /** * Update output for address. */ }, { key: 'eval', value: function _eval() { this._updateOutput(); } /** * On rising edge RAM updates the value by the address if the * `load` is set, otherwise -- preserves the value. */ }, { key: 'clockUp', value: function clockUp() { var load = this.getInputPins()[1].getValue(); if (load) { var address = this.getInputPins()[2].getValue(); var value = this.getInputPins()[0].getValue(); this.setValueAt(address, value); } } /** * On the falling edge RAM propagates * the value to the output pin. */ }, { key: 'clockDown', value: function clockDown() { this._updateOutput(); } /** * Updates the output on address change, and on clock down. */ }, { key: '_updateOutput', value: function _updateOutput() { var address = this.getInputPins()[2].getValue(); this.getOutputPins()[0].setValue(this._storage[address]); } }], [{ key: 'isClocked', value: function isClocked() { return true; } }]); return RAM; }(BuiltInGate); /** * Specification of the `RAM` gate. */ RAM.Spec = { name: 'RAM', description: ['Abstract memory chip of a variable size (default 8).', '', 'If load[t]=1 then out[t+1] = in[t] else out does not change.', '', 'Clock rising edge updates the value from the input by the address,', 'if the `load` is set; otherwise, preserves the state.', '', ' ' + colors.bold('↗') + ' : value[address] = load ? in : value[address]', '', 'Clock falling edge propagates the value at the address to the output:', '', ' ' + colors.bold('↘') + ' : out = value[address]'].join('\n'), inputPins: [{ name: 'in', size: 16 }, { name: 'load', size: 1 }, { name: 'address', size: 3 }], outputPins: [{ name: 'out', size: 16 }], truthTable: TRUTH_TABLE }; module.exports = RAM;