{"by":"purple-leafy","descendants":11,"id":42545303,"kids":[42546781,42547748,42545539,42545557,42547444,42546087],"score":43,"text":"* OPEN TO CONTRIBUTIONS *<p>NandToTetris is a course which has you build a full computer from:<p>Logic gates -&gt; Chips -&gt; RAM -&gt; CPU -&gt; Computer\n-&gt; Assembler -&gt; Compiler -&gt; OS -&gt; Tetris<p>All this is done via software defined hardware emulation.<p>I&#x27;m building an emulator for this entire stack in C.<p>How is my approach different to other projects that build emulators?<p><pre><code>    - No external dependencies (so far)\n    - Start with a single software defined NAND gate in C\n      - EVERYTHING is built from this base chip\n    - Don&#x27;t use certain programming utilities: boolean logic \n        operators, bitwise logic operators etc\n\n    Instead we leverage the gates&#x2F;chips to implement such logic\n\n    I build more and more base chips from the NAND gate\n    - Simple gates: OR, AND, NOT, XOR (5 total &quot;gates&quot;)\n    - Simple chips: DMux, Mux (11 so far, &quot;combinatorial&quot; \n        chips\n    - 16 bit variants\n</code></pre>\nFor comparison, most emulator projects start right at the CPU level and don&#x27;t sequentially build primitive structures. So a lay-person, or person unfamiliar with PC architectures is missing some lower-level information.<p>Typical emulators look at CPU truth table &#x2F; Instruction set and implement that logic directly in code.<p>More straight forward, way fewer lines of code - but you skip all the gates&#x2F;chips fun.<p>------<p>Confused? Heres example code for my NAND gate:<p><pre><code>    void nand_gate(Nand *nand)\n    {\n        nand-&gt;output.out = !(nand-&gt;input.a &amp; nand-&gt;input.b);\n    }\n</code></pre>\nFrom this gate I build a NOT gate (note, no boolean operators)<p><pre><code>    void not_gate(Not * not )\n    {\n        Nand nand = {\n            .input.a = not -&gt;input.in,\n            .input.b = not -&gt;input.in,\n        };\n        nand_gate(&amp;nand);\n        not -&gt;output.out = nand.output.out;\n    }\n</code></pre>\nThen OR &#x2F; AND &#x2F; XOR &#x2F; MUX &#x2F; DMUX ..... and their 16 bit versions.<p>Heres a more complex chip, a 16bit Mux-8-way chip<p><pre><code>    &#x2F;*\n     * out = a if sel = 000\n     *       b if sel = 001\n     *       c if sel = 010\n     *       d if sel = 011\n     *       e if sel = 100\n     *       f if sel = 101\n     *       g if sel = 110\n     *       h if sel = 111\n    *&#x2F;\n    void mux16_8_way_chip(Mux16_8_Way *mux16_8_way)\n    {\n      Mux16_4_Way mux16_4_way_chip_a, mux16_4_way_chip_b;\n      Mux16 mux16_chip_c;\n\n      &#x2F;&#x2F; Mux a\n      memcpy(mux16_4_way_chip_a.input.sel, mux16_8_way- &gt;input.sel, sizeof(mux16_4_way_chip_a.input.sel));\n      memcpy(mux16_4_way_chip_a.input.a, mux16_8_way-&gt;input.a, sizeof(mux16_4_way_chip_a.input.a));\n      memcpy(mux16_4_way_chip_a.input.b, mux16_8_way-&gt;input.b, sizeof(mux16_4_way_chip_a.input.b));\n      memcpy(mux16_4_way_chip_a.input.c, mux16_8_way-&gt;input.c, sizeof(mux16_4_way_chip_a.input.c));\n      memcpy(mux16_4_way_chip_a.input.d, mux16_8_way-&gt;input.d, sizeof(mux16_4_way_chip_a.input.d));\n      mux16_4_way_chip(&amp;mux16_4_way_chip_a);\n\n      &#x2F;&#x2F; Mux b\n      memcpy(mux16_4_way_chip_b.input.sel, mux16_8_way-&gt;input.sel, sizeof(mux16_4_way_chip_b.input.sel));\n      memcpy(mux16_4_way_chip_b.input.a, mux16_8_way-&gt;input.e, sizeof(mux16_4_way_chip_b.input.a));\n      memcpy(mux16_4_way_chip_b.input.b, mux16_8_way-&gt;input.f, sizeof(mux16_4_way_chip_b.input.b));\n      memcpy(mux16_4_way_chip_b.input.c, mux16_8_way-&gt;input.g, sizeof(mux16_4_way_chip_b.input.c));\n      memcpy(mux16_4_way_chip_b.input.d, mux16_8_way-&gt;input.h, sizeof(mux16_4_way_chip_b.input.d));\n      mux16_4_way_chip(&amp;mux16_4_way_chip_b);\n\n      &#x2F;&#x2F; Mux c\n      mux16_chip_c.input.sel = mux16_8_way-&gt;input.sel[2];\n      memcpy(mux16_chip_c.input.a, mux16_4_way_chip_a.output.out, sizeof(mux16_chip_c.input.a));\n      memcpy(mux16_chip_c.input.b, mux16_4_way_chip_b.output.out, sizeof(mux16_chip_c.input.b));\n      mux16_chip(&amp;mux16_chip_c);\n\n      memcpy(mux16_8_way-&gt;output.out, mux16_chip_c.output.out, sizeof(mux16_8_way-&gt;output.out));\n    }\n\n</code></pre>\n-----<p>Progress: I have only started this project yesterday, so have completed 1 out of 7 hardware projects so far","time":1735520749,"title":"Show HN: WIP NandToTetris Emulator in pure C – logic gates to ALU to CPU to PC","type":"story","url":"https://github.com/con-dog/hack_emulator_c"}