The Nand2Tetris part I at coursera is very much my first completed course. It was so fun to actually work through the material and it feels amazing to know how simple it is to actually build a computer from scratch. While it is simple, it doesn’t mean the course itself is easy though. I was struggling to get the CPU wired up properly that I spent two to three days just to get it working.
I don’t quite remember when was the first time hearing about the course but I always find it fascinating. So I was quite surprised to see it available on coursera in April, and quickly subscribed to the course. Fast forward to today, I have completed the course for part I and can’t wait for the second part of the course involving the writing of OS, Compiler and actual program.
Considering I am asked to not publish my work (still thinking whether I should publish them to github), I would just briefly talk about the course.
In Week 1 we were asked to implement some chips by using just NAND chips. Practically all chips are possible with just AND, OR, NOT gates, however for some reason having NAND is enough. One of the reasons seems to be it is possible to construct AND, OR, and NOT gates using just combinations of NAND gates.
The first time I was exposed to logic gates was during my high school physics class, however we didn’t really do much with them. Then it was revisited in my discrete mathematics in college. While I probably lost a fair amount of hair when solving boolean logic questions, the fun actually made the thing worthwhile. Little did I know, all this are important not only while writing codes, but also building a computer.
With the basic chips implemented, in week 2 we proceeded with implementing chips that does actual arithmetic calculations, namely the adders and ALU. Completing the ALU does made me felt like a wizard as things just work. While I probably can’t see how to use the ALU properly to do simple arithmetic operations, but knowing it is possible is already enough to blow my mind.
In the third week, the passage of time came into play. So as the concept of time being introduced, we are required to build chips that has the ability to store state, or hold value for a given time.
Then the course continued with week 4, which is the introduction to assembly language for the particular CPU that we were about to build in the following week. I am fine with the structure of the course, although normally one would probably expect to actually build the CPU before starting to learn about the assembly. Anyway, in this week, we were taught how to write assembly for the computer named “Hack”, and due to the nature of this course, it is very much functional though the simplistic design.
I had no prior experience in writing low-level code, so this was an eye-opening experience. I didn’t really spent much time in it (only made sure the code passed the tests), as working at such level is really a pain. A simple statement in “modern” high level languages might actually take two or more lines in assembly (and in turns, machine language).
Then it came the most difficult part of the course in week 5 (IMO), where we were asked to implement the CPU. Though a partial design is given, implementing it properly still took me quite a few days. I was stuck mainly at the wiring of ALU as I failed to see the relationship between the c-bits and the various operations the ALU supplies. The other part that is tough, is about wiring the Program Counter properly for it to correctly output the address of next statement. After numerous failures, it finally passed the unit tests at coursera.
I can’t thank enough to the kind folks at the course forum.
Speaking of this, there are quite a number of things that I don’t quite understand. My chips seem to work fine, but sometimes, running the unit tests locally through the supplied tools would fail. Especially when keyboard input is involved. However, uploading the same piece of work over to coursera returns full pass.
Next comes week 6, where we were asked to build an assembler, that translate assembly to actual binary machine code. I intend to run it in browser, so I wrote it in javascript. I cheated a little bit and did not follow the contract fully, as I didn’t make the script to read/write files. However, the core functionality is there, which is to convert assembly to machine language.
The current revision of the code is horribly written, and isn’t unit-tested (and I probably won’t be going that route anyway). Another part that could be done properly is the parsing part, where I really should be using some tokenization library or some sort (only included underscore, jQuery and sprintf libraries). However, as the language itself is very simplified, so I used regular expression instead to read the statements (evil, I know). I will probably spend some time polishing the code (the function naming is really, really bad) and publish them when the course is officially ended.
Overall, it was really an amazing ride, I enjoy it very much and would recommend everyone to register the course in future. The other option, is to grab a copy of the textbook, and the material from the official website to start now.