Created
August 26, 2020 09:13
-
-
Save richlloydmiles/32ac7c3146ff8d41decc296329f53a2f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const nand = (a, b) => (a === 1 && b === 1 ? 0 : 1) | |
// NAND - A and B are both 1 return 0 | |
// const A = 1 | |
// const B = 0 | |
// console.log(nand(A, B)) | |
// NOT - return not A (OR B) | |
// const A = 1 | |
const not = (x) => nand(x, x) | |
// console.log(not(A)) | |
// const B = 1 | |
// const not = (x) => nand(x, x) | |
// console.log(not(B)) | |
// AND - A and B are both 1 return 1 | |
// const A = 0 | |
// const B = 0 | |
const and = (x, y) => nand(nand(x, y), nand(x, y)) | |
// console.log(and(A, B)) | |
// OR - A and B are both 0 return 0 | |
// const A = 0 | |
// const B = 0 | |
const or = (x, y) => nand(nand(x, x), nand(y, y)) | |
// console.log(or(A, B)) | |
// NOR - A and B are 0 return 1 | |
// const A = 0 | |
// const B = 0 | |
const nor = (x, y) => nand(or(x, y), or(x, y)) | |
// console.log(nor(A, B)) | |
// XOR - A and B are not equal return 1 | |
// const A = 0 | |
// const B = 0 | |
const xor = (x, y) => or(and(x, not(y)), and(y, not(x))) | |
// console.log(xor(A, B)) | |
// MUX - SEL equal 0 return A else B | |
// const A = 1 | |
// const B = 0 | |
// const SEL = 0 | |
const mux = (x, y, input) => or(and(x, not(input)), and(input, y)) | |
// console.log(mux(A, B, SEL)) | |
// ------------------ | |
// DEMUX - SEL equal 0 in = A and B = 0 else A = 0 and B = in | |
// const SEL = 0 | |
// const INPUT = 1 | |
const demux = (x, y) => ({ a: and(y, not(x)), b: and(y, x) }) | |
// console.log(demux(SEL, INPUT)) | |
// Multi-Bit Not | |
const not16 = (x) => x.map((z) => not(z)) | |
// console.log(not16(Array(16).fill(0))) | |
// Multi-Bit And | |
const and16 = (x, y) => x.map((z, index) => and(z, y[index])) | |
// console.log(and16(Array(16).fill(1), Array(16).fill(1))) | |
// Multi-Bit Or | |
const or16 = (x, y) => x.map((z, index) => or(z, y[index])) | |
// console.log(or16(Array(16).fill(0), Array(16).fill(1))) | |
// Multi-Bit Multiplexor - SEL equal 0 return A else B | |
const mux16 = (x, y, sel) => x.map((z, index) => mux(z, y[index], sel)) | |
// console.log(mux16(Array(16).fill(0), Array(16).fill(1), 1)) | |
// Or8Way - if any input is 1 output 1 else 0 | |
const or8Way = (x) => x.reduce((total, z) => or(z, total), 0) | |
const or16Way = or8Way | |
// console.log(or8Way([0, 0, 0, 0, 0, 0, 0, 1])) | |
// console.log(or8Way([0, 0, 0, 0, 0, 0, 0, 0])) | |
// Multi-Way/Multi-Bit Multiplexor | |
// * 4-way 16-bit multiplexor. | |
// * out = a if sel==00 | |
// * b if sel==01 | |
// * c if sel==10 | |
// * d if sel==11 | |
const mux4Way16 = (a, b, c, d, sel) => | |
mux16(mux16(a, b, sel[1]), mux16(c, d, sel[1]), sel[0]) | |
// console.log( | |
// mux4Way16( | |
// Array(16).fill(0), | |
// Array(16).fill(0), | |
// Array(16).fill(0), | |
// Array(16).fill(1), | |
// [1, 1] | |
// ) | |
// ) | |
// Mux8Way16 | |
/** | |
* 8-way 16-bit multiplexor. | |
* out = a if sel==000 | |
* b if sel==001 | |
* c if sel==010 | |
* d if sel==011 | |
* e if sel==100 | |
* f if sel==101 | |
* g if sel==110 | |
* h if sel==111 | |
*/ | |
const mux8Way16 = (a, b, c, d, e, f, g, h, sel) => | |
mux16( | |
mux16(mux16(a, b, sel[2]), mux16(c, d, sel[2]), sel[1]), | |
mux16(mux16(e, f, sel[2]), mux16(g, h, sel[2]), sel[1]), | |
sel[0] | |
) | |
// console.log( | |
// mux8Way16( | |
// Array(16).fill(0), | |
// Array(16).fill(0), | |
// Array(16).fill(0), | |
// Array(16).fill(0), | |
// Array(16).fill(0), | |
// Array(16).fill(0), | |
// Array(16).fill(1), | |
// Array(16).fill(0), | |
// [1, 1, 0] | |
// ) | |
// ) | |
// DMux4Way | |
// SEL equal 0 in = A and B = 0 else A = 0 and B = in | |
// * 4-way demultiplexor. | |
// * {a,b,c,d} = {in,0,0,0} if sel==00 | |
// * {0,in,0,0} if sel==01 | |
// * {0,0,in,0} if sel==10 | |
// * {0,0,0,in} if sel==11 | |
const dmux4Way = (input, sel) => { | |
const { a: ao, b: bo } = demux(sel[0], input) | |
const { a, b } = demux(sel[1], ao) | |
const { a: c, b: d } = demux(sel[1], bo) | |
return { a, b, c, d } | |
} | |
// console.log(dmux4Way(1, [1, 1])) | |
module.exports = { | |
nand, | |
not, | |
and, | |
or, | |
nor, | |
xor, | |
mux, | |
demux, | |
not16, | |
and16, | |
or16, | |
mux16, | |
or8Way, | |
or16Way, | |
mux4Way16, | |
mux8Way16, | |
dmux4Way, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment