WI

WIP NandToTetris Emulator in pure C – logic gates to ALU to CPU to PC

Hacker News

WIP NandToTetris Emulator in pure C – logic gates to ALU to CPU to PC

* OPEN TO CONTRIBUTIONS * NandToTetris is a course which has you build a full computer from: Logic gates -> Chips -> RAM -> CPU -> Computer -> Assembler -> Compiler -> OS -> Tetris All this is done via software defined hardware emulation. I'm building an emulator for this entire stack in C. How is my approach different to other projects that build emulators? - No external dependencies (so far) - Start with a single software defined NAND gate in C - EVERYTHING is built from this base chip - Don't use certain programming utilities: boolean logic operators, bitwise logic operators etc Instead we leverage the gates/chips to implement such logic I build more and more base chips from the NAND gate - Simple gates: OR, AND, NOT, XOR (5 total "gates") - Simple chips: DMux, Mux (11 so far, "combinatorial" chips - 16 bit variants For comparison, most emulator projects start right at the CPU level and don't sequentially build primitive structures. So a lay-person, or person unfamiliar with PC architectures is missing some lower-level information. Typical emulators look at CPU truth table / Instruction set and implement that logic directly in code. More straight forward, way fewer lines of code - but you skip all the gates/chips fun. ------ Confused? Heres example code for my NAND gate: void nand_gate(Nand *nand) { nand->output.out = !(nand->input.a & nand->input.b); } From this gate I build a NOT gate (note, no boolean operators) void not_gate(Not * not ) { Nand nand = { .input.a = not ->input.in, .input.b = not ->input.in, }; nand_gate(&nand); not ->output.out = nand.output.out; } Then OR / AND / XOR / MUX / DMUX ..... and their 16 bit versions. Heres a more complex chip, a 16bit Mux-8-way chip /* * 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 */ void mux16_8_way_chip(Mux16_8_Way *mux16_8_way) { Mux16_4_Way mux16_4_way_chip_a, mux16_4_way_chip_b; Mux16 mux16_chip_c; // Mux a memcpy(mux16_4_way_chip_a.input.sel, mux16_8_way- >input.sel, sizeof(mux16_4_way_chip_a.input.sel)); memcpy(mux16_4_way_chip_a.input.a, mux16_8_way->input.a, sizeof(mux16_4_way_chip_a.input.a)); memcpy(mux16_4_way_chip_a.input.b, mux16_8_way->input.b, sizeof(mux16_4_way_chip_a.input.b)); memcpy(mux16_4_way_chip_a.input.c, mux16_8_way->input.c, sizeof(mux16_4_way_chip_a.input.c)); memcpy(mux16_4_way_chip_a.input.d, mux16_8_way->input.d, sizeof(mux16_4_way_chip_a.input.d)); mux16_4_way_chip(&mux16_4_way_chip_a); // Mux b memcpy(mux16_4_way_chip_b.input.sel, mux16_8_way->input.sel, sizeof(mux16_4_way_chip_b.input.sel)); memcpy(mux16_4_way_chip_b.input.a, mux16_8_way->input.e, sizeof(mux16_4_way_chip_b.input.a)); memcpy(mux16_4_way_chip_b.input.b, mux16_8_way->input.f, sizeof(mux16_4_way_chip_b.input.b)); memcpy(mux16_4_way_chip_b.input.c, mux16_8_way->input.g, sizeof(mux16_4_way_chip_b.input.c)); memcpy(mux16_4_way_chip_b.input.d, mux16_8_way->input.h, sizeof(mux16_4_way_chip_b.input.d)); mux16_4_way_chip(&mux16_4_way_chip_b); // Mux c mux16_chip_c.input.sel = mux16_8_way->input.sel[2]; memcpy(mux16_chip_c.input.a, mux16_4_way_chip_a.output.out, sizeof(mux16_chip_c.input.a)); memcpy(mux16_chip_c.input.b, mux16_4_way_chip_b.output.out, sizeof(mux16_chip_c.input.b)); mux16_chip(&mux16_chip_c); memcpy(mux16_8_way->output.out, mux16_chip_c.output.out, sizeof(mux16_8_way->output.out)); } ----- Progress: I have only started this project yesterday, so have completed 1 out of 7 hardware projects so far

Share card

Actual performance

43points
11comments
Made the leaderboard

Launch Intel predictions

Analyze your own launch →
Product HuntOn track for Day 1 leaderboard · Strong signals: mcp, computer, single · Missing: mac, agents, macos
72%72% predicted probability of success on Product Hunt, based on ML models trained on real launch data.
best fitHighest predicted score across all platforms for this description.
Indie HackersFits the IH revenue-focused audience · Strong signals: started · Missing: supports, reddit linkedin, podcasting
65%65% predicted probability of success on Indie Hackers, based on ML models trained on real launch data.
Hacker NewsStrong engagement from HN community · Strong signals: 000, io · Missing: https docs, excited, just released
63%63% predicted probability of success on Hacker News, based on ML models trained on real launch data.
nativeThis product was originally launched on this platform.
TrustMRRLess likely to generate early MRR · Strong signals: way · Missing: mobile apps, ios, personal
39%39% predicted probability of success on TrustMRR, based on ML models trained on real launch data.
AppSumoMay struggle as an AppSumo deal · Missing: plus, platform, intuitive
32%32% predicted probability of success on AppSumo, based on ML models trained on real launch data.
Acquire.comPre-revenue stage for this audience · Missing: arr, mrr, revenue
16%16% predicted probability of success on Acquire.com, based on ML models trained on real launch data.
BetaListMay not resonate with beta-testers · Missing: web3, chat, crypto
0%0% predicted probability of success on BetaList, based on ML models trained on real launch data.

Correct prediction on native model

Similar products

I
I made a game where you build a CPU from logic gates64%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

I made a game where you build a CPU from logic gates

Hacker News102
Ga
Game Boy on the Beam – A DMG Emulator in Pure Elixir74%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Game Boy on the Beam – A DMG Emulator in Pure Elixir

Hacker News6
Ho
Homebrew 16bit CPU from 74HC logic with C compiler and Unix-like OS56%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Homebrew 16bit CPU from 74HC logic with C compiler and Unix-like OS

Hacker News192
Lu
LuaGB – A Gameboy emulator written in pure Lua84%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

LuaGB – A Gameboy emulator written in pure Lua

Hacker News136
I
I made an easy-to-understand 6502 emulator in pure C63%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

I made an easy-to-understand 6502 emulator in pure C

Hacker News88
Ha
Harvard CPU in Verilog and Assembler in Go54%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Harvard CPU in Verilog and Assembler in Go

Hacker News4
H2
H2 Forth CPU58%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

H2 Forth CPU

Hacker News120
Bottleneck Calculator
Bottleneck Calculator44%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Find out if your CPU or GPU is holding your PC back

Indie Hackers1games
Ad
Adding a logic channel next to pure text47%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Adding a logic channel next to pure text

Hacker News1
CPU Ranker
CPU Ranker38%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

CPU Databse, CPU, Processors, Mobile CPU, CPU List

Product Hunt+4