https://github.com/vidundergunder/simulated-8-bit-adder
Simulated 8-bit Ripple Carry Adder implemented in TypeScript.
https://github.com/vidundergunder/simulated-8-bit-adder
8-bit circuit crash-course electronics logic logic-gates simulation typescript
Last synced: about 2 months ago
JSON representation
Simulated 8-bit Ripple Carry Adder implemented in TypeScript.
- Host: GitHub
- URL: https://github.com/vidundergunder/simulated-8-bit-adder
- Owner: VidunderGunder
- Created: 2021-04-25T22:05:48.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-04-25T22:32:02.000Z (about 5 years ago)
- Last Synced: 2025-02-05T12:13:15.701Z (over 1 year ago)
- Topics: 8-bit, circuit, crash-course, electronics, logic, logic-gates, simulation, typescript
- Language: TypeScript
- Homepage:
- Size: 2.22 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Simulated 8-bit Adder in TypeScript
Personal experiment to practice TypeScript. Playing around with overload signatures, bitwise operators and logic.
Type checks feel bloated to me - any tips to clean them up would be greatly appreciated!
Inspired by [Crash Course's ALU-video](https://youtu.be/1I5ZMmrOfnA).
---
[](https://youtu.be/1I5ZMmrOfnA)
Diagram from Crash Course's video illustrating an 8-bit ripple carry adder
---
```ts
// ---------------------
// SIMULATED LOGIC GATES
// ---------------------
type BitInt = 0 | 1;
type BitString = "0" | "1";
function xor(a: boolean, b: boolean): boolean;
function xor(a: BitInt, b: BitInt): BitInt;
function xor(a: BitString, b: BitString): BitString;
function xor(a: any, b: any): any {
if (typeof a !== typeof b) throw "Inputs must be of same type.";
if (typeof a === "boolean" && typeof b === "boolean") return a !== b;
if (typeof a === "number" && typeof b === "number") return a ^ b;
if (typeof a === "string" && typeof b === "string")
return String(Number(a) ^ Number(b));
}
function and(a: boolean, b: boolean): boolean;
function and(a: BitInt, b: BitInt): BitInt;
function and(a: BitString, b: BitString): BitString;
function and(a: any, b: any): any {
if (typeof a !== typeof b) throw "Inputs must be of same type.";
if (typeof a === "boolean" && typeof b === "boolean") return a && b;
if (typeof a === "number" && typeof b === "number") return a & b;
if (typeof a === "string" && typeof b === "string")
return String(Number(a) & Number(b));
}
function or(a: boolean, b: boolean): boolean;
function or(a: BitInt, b: BitInt): BitInt;
function or(a: BitString, b: BitString): BitString;
function or(a: any, b: any): any {
if (typeof a !== typeof b) throw "Inputs must be of same type.";
if (typeof a === "boolean" && typeof b === "boolean") return a || b;
if (typeof a === "number" && typeof b === "number") return a | b;
if (typeof a === "string" && typeof b === "string")
return String(Number(a) | Number(b));
}
// ---------------------
// SIMULATED HALF ADDER
// ---------------------
function halfAdder(a: boolean, b: boolean): [boolean, boolean];
function halfAdder(a: BitInt, b: BitInt): [BitInt, BitInt];
function halfAdder(a: BitString, b: BitString): [BitString, BitString];
function halfAdder(a: any, b: any): [any, any] {
const sum = xor(a, b); // XOR
const carry = and(a, b); // AND
return [carry, sum];
}
// ---------------------
// SIMULATED FULL ADDER
// ---------------------
function fullAdder(a: boolean, b: boolean, c: boolean): [boolean, boolean];
function fullAdder(a: BitInt, b: BitInt, c: BitInt): [BitInt, BitInt];
function fullAdder(
a: BitString,
b: BitString,
c: BitString
): [BitString, BitString];
function fullAdder(a: any, b: any, c: any): [any, any] {
const [c1, s1] = halfAdder(a, b);
const [c2, s2] = halfAdder(s1, c);
const c3 = xor(c1, c2);
return [c3, s2];
}
// ---------------------
// SIMULATED 8-bit ADDER
// ---------------------
type ByteInt = [BitInt, BitInt, BitInt, BitInt, BitInt, BitInt, BitInt, BitInt];
type ByteString = `${BitString}${BitString}${BitString}${BitString}${BitString}${BitString}${BitString}${BitString}`;
type ByteBool = [
boolean,
boolean,
boolean,
boolean,
boolean,
boolean,
boolean,
boolean
];
function add8Bit(a: ByteInt, b: ByteInt): ByteInt;
function add8Bit(a: ByteBool, b: ByteBool): ByteBool;
function add8Bit(a: ByteString, b: ByteString): ByteString;
function add8Bit(a: any, b: any): any {
let [c, s] = halfAdder(a[7], b[7]);
const sum = [
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
s,
];
for (let i = 6; i >= 0; i--) {
[c, s] = fullAdder(a[i], b[i], c);
sum[i] = s;
}
if (typeof a === "string" && typeof b === "string" && typeof c == "string")
return [sum.join(""), c];
return [sum, c];
}
```