UNPKG

hdl-js

Version:

Hardware definition language (HDL) and Hardware simulator

140 lines (118 loc) 3.52 kB
/** * The MIT License (MIT) * Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com> */ 'use strict'; 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 RAM = require('./RAM'); var _require = require('../../../util/numbers'), int16Table = _require.int16Table; /** * Canonical truth table for the `RAM512` 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 }, { $clock: +3, in: 65535, load: 1, address: 511, out: 0 }, { $clock: -4, in: 0, load: 1, address: 511, out: 65535 }, { $clock: +4, in: 0, load: 1, address: 511, out: 65535 }, { $clock: -5, in: 0, load: 0, address: 511, out: 0 }]); /** * RAM chip of 512 16-bit registers. * * 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[9]; * OUT out[16]; * * DMux8Way(in=load, sel=address, ...); * RAM64(in=in, load=l1, address=address[0..5], out=r1); RAM64(in=in, load=l2, address=address[0..5], out=r2); * ... * Mux8Way16(...); */ var RAM512 = function (_RAM) { _inherits(RAM512, _RAM); function RAM512(options) { _classCallCheck(this, RAM512); return _possibleConstructorReturn(this, (RAM512.__proto__ || Object.getPrototypeOf(RAM512)).call(this, Object.assign({ size: 512 }, options))); } return RAM512; }(RAM); /** * Specification of the `RAM512` gate. */ RAM512.Spec = { name: 'RAM512', description: ['Memory chip consisting of 512 16-bit registers.', '', '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: 9 }], outputPins: [{ name: 'out', size: 16 }], truthTable: TRUTH_TABLE }; module.exports = RAM512;