-
Notifications
You must be signed in to change notification settings - Fork 11
/
Mux.js
58 lines (47 loc) · 1.14 KB
/
Mux.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
/**
* The MIT License (MIT)
* Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
'use strict';
const BuiltInGate = require('../BuiltInGate');
/**
* Canonical truth table for the `Mux` gate.
*/
const TRUTH_TABLE = [
{a: 0, b: 0, sel: 0, out: 0},
{a: 0, b: 0, sel: 1, out: 0},
{a: 0, b: 1, sel: 0, out: 0},
{a: 0, b: 1, sel: 1, out: 1},
{a: 1, b: 0, sel: 0, out: 1},
{a: 1, b: 0, sel: 1, out: 0},
{a: 1, b: 1, sel: 0, out: 1},
{a: 1, b: 1, sel: 1, out: 1},
];
/**
* 1-bit 2-way multiplexor.
* if sel=1 out=b else out=a.
*/
class Mux extends BuiltInGate {
eval() {
const a = this.getInputPins()[0].getValue();
const b = this.getInputPins()[1].getValue();
const sel = this.getInputPins()[2].getValue();
this.getOutputPins()[0].setValue(sel === 0 ? a : b);
}
}
/**
* Specification of the `Mux` gate.
*/
Mux.Spec = {
name: 'Mux',
description: [
'Implements 1-bit 2-way multiplexor (Mux) gate.',
'',
'out = a, when sel = 0',
'out = b, when sel = 1',
].join('\n'),
inputPins: ['a', 'b', 'sel'],
outputPins: ['out'],
truthTable: TRUTH_TABLE,
};
module.exports = Mux;