From 5bf914860d64670f3160331cdbb30f2a4c9b8861 Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Thu, 18 Aug 2022 23:13:43 +0800 Subject: Enable markdown in HTML block, fix nand2tetris_1 formatting --- docs/projects/nand2tetris_1.md | 52 ++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 27 deletions(-) (limited to 'docs') diff --git a/docs/projects/nand2tetris_1.md b/docs/projects/nand2tetris_1.md index 12a8704..865595d 100644 --- a/docs/projects/nand2tetris_1.md +++ b/docs/projects/nand2tetris_1.md @@ -61,10 +61,10 @@ And this is one of the few chips that have so few pins exposed (hence "elementary") that you can craft a truth table for it, and thus can test them manually. -
- - How I built and optimized the XOR gate by toying with boolean - expressions (click to expand) +
+ + __How I built and optimized the XOR gate by toying with boolean + expressions (click to expand)__ Here is the truth table of XOR, and you have to implement it with chips @@ -93,7 +93,7 @@ row that outputs 1: ``` Then we tie a NOT to each 0 input (excuse the pun), thus synthesizing two -sufficient conditions so that out=1: +sufficient conditions so that `out=1`: ``` (NOT a) AND b => out=1 @@ -128,7 +128,7 @@ XOR | ? ``` That's 9 in total. Can we optimize it? Let's try eliminating the OR first, -based on the fact that a OR b = NOT((NOT a) AND (NOT b)): +based on the fact that `a OR b = NOT((NOT a) AND (NOT b))`: ``` ((NOT a) AND b) OR (a AND (NOT b)) @@ -136,7 +136,7 @@ based on the fact that a OR b = NOT((NOT a) AND (NOT b)): ``` At first glance this might seem more complicated, but remember, -NOT(a AND b) = a NAND b… which is exactly our atomic building +`NOT(a AND b) = a NAND b`… which is exactly our atomic building block! We continue our equation: ``` @@ -216,8 +216,8 @@ But wait! Didn't we just see "x-y" and "y-x"? How can an Adder possibly do that? Note that there is no "Subtractor"; to do `x-y` you simply calculate `~(~x + y)` where `~` denotes bitwise negation, thanks to 2's complement. -
-Proof that x-y = ~(~x + y) +
+__Proof that `x-y = ~(~x + y)`__ We know in 2's complement, @@ -267,8 +267,8 @@ proceeds to look for builtin chips implemented in Java. No other directory is in its path, even project 01. This explains why the optimization I made in project 01 does not matter. -
-If it did matter, how many NANDs are there in this ALU? +
+__If it did matter, how many NANDs are there in this ALU?__ It's Python scripting time! I just need to recursively find out how many NANDs in each subchip, then add them up. The syntax of HDL is pretty @@ -353,23 +353,21 @@ HDL. While I was drawing the schematics, a question came to mind: If I set `f` to zero, will the Adder be working the same way as if `f=1`? -
-Answer +
+__Answer__ Yes, it will. Simply put, chips "downstream" do not directly affect those "upstream" as far as this course is concerned. The implication is that, when you give an x and a y to the ALU, it does both computations at once: boolean AND and binary addition. It is the Mux's job to select which branch to pass downstream, and which one to discard. This is, in -a stretch, called - speculative execution. +a stretch, called [speculative execution](https://en.wikipedia.org/wiki/Speculative_execution) -Schematic of ALU. The AND, Adder, and "f" mux are
-highlighted +![Schematic of ALU. The AND, Adder, and "f" mux are highlighted](../img/nand2tetris/alu_highlighted.png) -As you see in the highlighted area, both of these gates are +As you see in the highlighted area, _both_ of these gates are switching internally, and both of them consume power, even when one of -them does not generate useful output. Recall the if statement +them does not generate useful output. Recall the `if` statement in programming: ``` @@ -380,7 +378,7 @@ if (boolean) { } ``` -Either but not both of do_a or do_b will be run. +Either but not both of `do_a` or `do_b` will be run. This is one of the major mindblowers I came into, and I think it's worth writing about it.
@@ -496,8 +494,8 @@ which can be realized using this line of assembly (or instruction): @42 ``` -
-Expand to learn more about the difference between A and D +
+__Expand to learn more about the difference between A and D__ In HACK assembly, the A register is the only one you can directly assign an value to, like we just did. In contrast, if you want to write 42 into @@ -540,7 +538,7 @@ AD=1; // set A and D to 1 simultaneously AMD=1 // set A, M and D to 1 simultaneously ``` -C-instructions can also do something really cool, which is jumping. +C-instructions can also do something really cool, which is __jumping__. The syntax is different from what you might find in an industrial assembly language. @@ -549,12 +547,12 @@ language. D;JEQ // this jumps to ROM[42] if D=0 ``` -The JEQ here is a jump instruction. A jump instruction +The `JEQ` here is a __jump instruction__. A jump instruction compares ALU output to zero, and jumps if it is greater/equal/not equal/etc. -
-What if I want to jump to 42 if RAM[69] is zero? +
+__What if I want to jump to 42 if RAM[69] is zero?__ Can I just: @@ -564,7 +562,7 @@ Can I just: M;JEQ // does this work? ``` -No. @42 completely overwrote @69, so the M on +No. `@42` completely overwrote `@69`, so the M on line 3 is actually RAM[42]. To perform this maneuver, we need to make use of D. -- cgit v1.2.3