-
Notifications
You must be signed in to change notification settings - Fork 11
/
Bit.js
99 lines (86 loc) · 2.04 KB
/
Bit.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
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
'use strict';
const colors = require('colors');
const BuiltInGate = require('../BuiltInGate');
/**
* Canonical truth table for the `Bit` gate.
*/
const TRUTH_TABLE = [
{$clock: -0, in: 0, load: 0, out: 0},
{$clock: +0, in: 1, load: 1, out: 0},
{$clock: -1, in: 1, load: 0, out: 1},
{$clock: +1, in: 1, load: 0, out: 1},
{$clock: -2, in: 1, load: 0, out: 1},
{$clock: +2, in: 0, load: 1, out: 1},
{$clock: -3, in: 0, load: 0, out: 0},
];
/**
* 1 bit memory register.
* If load[t]=1 then out[t+1] = in[t] else out does not change.
*
* Abstract:
*
* IN in, load;
* OUT out;
*
* Mux(a=t0, b=in, sel=load, out=t1);
* DFF(in=t1, out=t0, out=out);
*/
class Bit extends BuiltInGate {
/**
* Bit is a sequential gate.
*/
static isClocked() {
return true;
}
init() {
/**
* The state (0/1) of the bit.
*/
this._state = 0;
}
/**
* On rising edge Bit updates the internal state
* if the `load` is set, otherwise -- preserves the state.
*/
clockUp() {
const load = this.getInputPins()[1].getValue();
if (load) {
this._state = this.getInputPins()[0].getValue();
}
}
/**
* On the falling edge Bit propagates the state
* to the output pin.
*/
clockDown() {
this.getOutputPins()[0].setValue(this._state);
}
}
/**
* Specification of the `Bit` gate.
*/
Bit.Spec = {
name: 'Bit',
description: [
'1 bit memory register.',
'',
'If load[t]=1 then out[t+1] = in[t] else out does not change.',
'',
'Clock rising edge updates internal state from the input,',
'if the `load` is set; otherwise, preserves the state.',
'',
` ${colors.bold('↗')} : state = load ? in : state`,
'',
'Clock falling edge propagates the internal state to the output:',
'',
` ${colors.bold('↘')} : out = state`,
].join('\n'),
inputPins: ['in', 'load'],
outputPins: ['out'],
truthTable: TRUTH_TABLE,
};
module.exports = Bit;