Skip to content

Instantly share code, notes, and snippets.

@hertzg
Last active December 2, 2023 02:21
Show Gist options
  • Save hertzg/672d2161e1371f5d91ced4174d2b8d81 to your computer and use it in GitHub Desktop.
Save hertzg/672d2161e1371f5d91ced4174d2b8d81 to your computer and use it in GitHub Desktop.
7 Segment to BCD encoder
const seg7_bcd = ([a, b, c, d, e, f, g]) => {
const w = (a & b & f & g) | (!b & !c & e) | (!a & e);
const x = (!a & b & g) | (a & !f & !g) | (a & !b);
const y = (!b & e & g) | (!d & e) | (a & !f);
const z = (!c & !d & g) | (c & !f) | (a & !e) | (!a & e);
return [w, x, y, z];
};
const TRUTH_TABLE = [
["0000000", "0000", " "],
["1111110", "0000", "0"],
["0110000", "0001", "1"],
["1101101", "0010", "2"],
["1111001", "0011", "3"],
["0110011", "0100", "4"],
["1011011", "0101", "5"],
["1011111", "0110", "6"],
["1110000", "0111", "7"],
["1111111", "1000", "8"],
["1110011", "1001", "9"],
["1110111", "1010", "A"],
["0011111", "1011", "b"],
["1001110", "1100", "C"],
["0111101", "1101", "d"],
["1001111", "1110", "E"],
["1000111", "1111", "F"],
]
.map(([i, o, l]) => [i, o, l])
.map(([[a, b, c, d, e, f, g], [w, x, y, z], l]) => [
...[a, b, c, d, e, f, g, w, x, y, z].map(Number),
l,
])
.map(([a, b, c, d, e, f, g, w, x, y, z, l]) => ({
i: { a, b, c, d, e, f, g },
o: { w, x, y, z },
l,
}));
for (const { i, o, l } of TRUTH_TABLE) {
const result = seg7_bcd(Object.values(i));
const comparison = result.every((v, i) => v === o[["w", "x", "y", "z"][i]]);
console.log(
`${comparison ? "✅" : "❌"} Letter ${l}: Input ${Object.values(
i
)} expected ${Object.values(o)} got ${result}`
);
}
LIBRARY ieee;
USE ieee.std_logic_1164.all;
ENTITY n7seg_bcd IS
PORT (
A, B, C, D, E, F, G : IN STD_LOGIC;
W, X, Y, Z : OUT STD_LOGIC
);
END n7seg_bcd;
ARCHITECTURE Behavioral OF n7seg_bcd IS
BEGIN
PROCESS (A, B, C, D, E, F, G)
BEGIN
-- W is set for letterss 1, 3, 5, 7, 9, B, D, F
W <= A and B and F and G or
not(B) and not(C) and E or
not(A) and E;
-- X is set for letters 2, 3, 6, 7, A, B, E, F
X <= not(A) and B and G or
A and not(F) and not(G) or
A and not(B);
-- Y is set for letters 4, 5, 6, 7, C, D, E, F
Y <= not(B) and E and G or
not(D) and E or
A and not(F);
-- Z is set for letters 8, 9, A, B, C, D, E, F
Z <= C and not(F) or
not(C) and not(D) and not(G) or
A and not(E) or
not(A) and E or
A and not(E);
END PROCESS;
END Behavioral;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment