hdl-js
Version:
Hardware definition language (HDL) and Hardware simulator
140 lines (118 loc) • 3.52 kB
JavaScript
/**
* 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 `RAM4K` 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: 4095,
out: 0
}, {
$clock: -4,
in: 0,
load: 1,
address: 4095,
out: 65535
}, {
$clock: +4,
in: 0,
load: 1,
address: 4095,
out: 65535
}, {
$clock: -5,
in: 0,
load: 0,
address: 4095,
out: 0
}]);
/**
* RAM chip of 4K 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[12];
* OUT out[16];
*
* DMux8Way(in=load, sel=address, ...);
* RAM512(in=in, load=l1, address=address[0..8], out=r1);
RAM512(in=in, load=l2, address=address[0..8], out=r2);
* ...
* Mux8Way16(...);
*/
var RAM4K = function (_RAM) {
_inherits(RAM4K, _RAM);
function RAM4K(options) {
_classCallCheck(this, RAM4K);
return _possibleConstructorReturn(this, (RAM4K.__proto__ || Object.getPrototypeOf(RAM4K)).call(this, Object.assign({ size: 4 * 1024 }, options)));
}
return RAM4K;
}(RAM);
/**
* Specification of the `RAM4K` gate.
*/
RAM4K.Spec = {
name: 'RAM4K',
description: ['Memory chip consisting of 4K 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: 12 }],
outputPins: [{ name: 'out', size: 16 }],
truthTable: TRUTH_TABLE
};
module.exports = RAM4K;