-
Notifications
You must be signed in to change notification settings - Fork 11
/
RAM.js
238 lines (212 loc) · 4.68 KB
/
RAM.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
'use strict';
const colors = require('colors');
const BuiltInGate = require('../BuiltInGate');
const {int16Table} = require('../../../util/numbers');
/**
* Canonical truth table for the `RAM` gate.
*/
const TRUTH_TABLE = int16Table([
{
$clock: -0,
in: 0b0000000000000000,
load: 0,
address: 0,
out: 0b0000000000000000,
},
{
$clock: +0,
in: 0b0000000000010101,
load: 1,
address: 0,
out: 0b0000000000000000,
},
{
$clock: -1,
in: 0b0000000000000001,
load: 0,
address: 0,
out: 0b0000000000010101,
},
{
$clock: +1,
in: 0b0000000000010101,
load: 0,
address: 0,
out: 0b0000000000010101,
},
{
$clock: -2,
in: 0b0000000000010101,
load: 0,
address: 0,
out: 0b0000000000010101,
},
{
$clock: +2,
in: 0b1101001000010101,
load: 1,
address: 2,
out: 0b0000000000000000,
},
{
$clock: -3,
in: 0b1101001000010101,
load: 0,
address: 2,
out: 0b1101001000010101,
},
]);
/**
* 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(...);
*/
class RAM extends BuiltInGate {
/**
* The size is the number of registers (machine words).
*/
constructor(options) {
super(options);
this._size = options.size || 8;
this._storage = new Int16Array(this.getSize());
// Addresses which are updated (used in `reset`).
this._modifiedAddresses = new Set();
}
/**
* RAM is a sequential gate.
*/
static isClocked() {
return true;
}
/**
* Number of machine words (registers) in this memory unit.
*/
getSize() {
return this._size;
}
/**
* Returns the storage.
*/
getStroage() {
return this._storage;
}
/**
* Sets storage.
*/
setStorage(storage) {
this._storage = storage;
return this;
}
/**
* Returns values at address.
*/
getValueAt(address) {
return this._storage[this._checkAddress(address)];
}
/**
* Returns values at address.
*/
setValueAt(address, value) {
this._storage[this._checkAddress(address)] = value;
this._modifiedAddresses.add(address);
return this;
}
/**
* Resets the memory to all zeros.
*/
reset() {
this._modifiedAddresses.forEach(address => {
this._storage[address] = 0;
});
this._modifiedAddresses.clear();
return this;
}
/**
* Checks address range.
*/
_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.
*/
eval() {
this._updateOutput();
}
/**
* On rising edge RAM updates the value by the address if the
* `load` is set, otherwise -- preserves the value.
*/
clockUp() {
const load = this.getInputPins()[1].getValue();
if (load) {
const address = this.getInputPins()[2].getValue();
const value = this.getInputPins()[0].getValue();
this.setValueAt(address, value);
}
}
/**
* On the falling edge RAM propagates
* the value to the output pin.
*/
clockDown() {
this._updateOutput();
}
/**
* Updates the output on address change, and on clock down.
*/
_updateOutput() {
const address = this.getInputPins()[2].getValue();
this.getOutputPins()[0].setValue(this._storage[address]);
}
}
/**
* 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;