Imprint | Privacy Policy

Nand2Tetris

(Usage hints for this presentation)

IT Systems, Summer Term 2024
Dr. Jens Lechtenbörger (License Information)

1. Introduction

Video by co-creator of Nand to Tetris

1.1. Today’s Core Question

  • How to start with Nand to Tetris?

1.2. Learning Objectives

  • Build simple chips with HDL
  • Simulate chips in Hardware Simulator

1.3. Retrieval Practice

  • What do you remember about Part 1 of IT Systems?
  • Other parts?

1.4. Recall: Goal of Part 1

  • Recall: Build general-purpose, programmable computer; here, the Hack platform

    Computer hardware with layers of abstraction

    • Programmable in machine language
    • Built from chips and gates specified in hardware description language (HDL)
      • (Gates are “simple” chips)
    • Nand2Tetris with sequence of projects

Agenda

2. Preview

2.1. Hack Machine Language

  • Hack CPU executes instructions in machine language

    • Each Hack instruction consists of 16 bits

      • Consider increment of variable: i = i + 1
      • Translated into two Hack instructions
      • 0000000000010000 (A-instruction, starts with 0, binary number for 16)
        • Human-readable assembly language instruction: @16
        • Used here to specify memory (RAM) location for value of variable
      • 1111 110111 001 000 (C-instruction, starts with 1, encodes operation)
        • Human-readable assembly language instruction: M=M+1
        • 6 red bits specify operation (compute M+1), 3 blue bits specify where to store the result (in M, the memory location for our variable)
    • Modern processors behave similarly

2.2. Preview: Hack Computer Architecture

Hack Computer Architecture

Hack Computer Architecture” under CC0 1.0; from GitLab

3. Projects of Nand2Tetris

3.1. What to Expect in a Project

  • Projects focus on successively more complex chips

    • From simple logic gates to entire computer

    • Digital/binary devices with bit as basic unit of information

      • Bit = Logical state with two values: true/false or 1/0
      • Data and instructions to be represented as sequences of bits
    • Chips come with specifications/interfaces (and tests)

      • What to do. Abstraction!
    • Results/implementations for each project are building blocks for subsequent ones

      • Abstraction and modularity!
    • You work on projects individually

      • Without immediate impact on grading, but lasting impact on brains (and, thus, on exam results)
      • (Solutions can be found on the web)

3.2. What to Expect in Project 1

  • Project 1

    1. Given: Nand(x, y), false

      x y Nand(x, y)
      0 0 1
      0 1 1
      1 0 1
      1 1 0
      • Nand is a binary Boolean function
        • Specified by truth table to right
        • 0 represents false, 1 represents true
    1. Learn about Boolean logic

    2. Implement sequence of chips (from specifications)

      1. Not(x)

        • Not flips its argument: true = Not(false), false = Not(true)
        • Implementation: Not(x) = Nand(x, x)
      1. And

        • True if and only if all arguments are true; revisited subsequently
      2. Xor, Or, Mux, and several more…

4. And Gate

4.1. Specification of And Gate

  • Three files for binary function And(a, b)

    1. HDL file with interface and specification, And.hdl

      // This file is part of www.nand2tetris.org
      // and the book "The Elements of Computing Systems"
      // by Nisan and Schocken, MIT Press.
      // File name: projects/01/And.hdl
      
      /**
       * And gate:
       * out = 1 if (a == 1 and b == 1)
       *       0 otherwise
       */
      
      CHIP And {
          IN a, b;
          OUT out;
      
          PARTS:
          // Put your code here:
      }
      
    1. File And.cmp

      a b out
      0 0 0
      0 1 0
      1 0 0
      1 1 1
      • Test cases (here, truth table)
    2. File And.tst

      • Test file for hardware simulator
      • To check implementation against test cases

4.2. Implementation of And Gate (1/3)

  • What an And gate should do
    • Specification: out == 1 if and only if a == b == 1

    • Interface in HDL skeleton: And.hdl

      CHIP And {
          IN a, b;
          OUT out;
      
          PARTS:
      	// Put your code here:
      
      }
      

4.3. Implementation of And Gate (2/3)

  • Idea to implement And gate
    • Gate logic: And(a, b) = Not(Nand(a, b))

    • Interface in HDL skeleton: And.hdl

      CHIP And {
          IN a, b;
          OUT out;
      
          PARTS:
      	// Put your code here:
      
      }
      

4.4. Implementation of And Gate (3/3)

  • Implementation of And gate
    • Gate logic: And(a, b) = Not(Nand(a, b))

    • Completed HDL file: And.hdl

      CHIP And {
          IN a, b;
          OUT out;
      
          PARTS:
      	Nand(a = a, b = b, out = aNandB);
      	Not(in = aNandB, out = out);
      }
      

5. Nand To Tetris Resources

  • Download Java software: https://www.nand2tetris.org/software
    • ZIP archive, contains tools and project files
      • I had to do this in the tools subdirectory: chmod u+x *.sh
    • We start with the Hardware Simulator
    • (Later on, we also use the CPU Emulator)

5.1. Hardware Simulator

  • Previous And implementation in Hardware Simulator

    Screenshot of Hardware Simulator for Nand to Tetris

    Screenshot of Hardware Simulator for Nand to Tetris” under GPLv2; from GitLab

    • Major window parts
      • HDL file in lower left, test script on right
      • Values of “Input pins” can be entered, resulting “Output pins” and “Internal pins” can be computed
      • Note message area at bottom. If not shown, resize window!

5.2. Textbook for Nand to Tetris

  • (Nisan and Schocken 2005) The Elements of Computing Systems
  • Download chapters now, start to read and work on Project 1
    • Note “Tips” and “Steps” at end of Chapter 1

6. Conclusions

6.1. A Related Story, 2021

  • From Nand to Zero-Day Exploit
    • Google Project Zero on attack software targeting iPhones
      • Discussion with hyperlink to https://www.nand2tetris.org/!
      • Summary
        • NSO Group used logic operations (And, Or, Xor, Xnor; functionally complete) of an image format (JBIG2, mostly outdated) to hijack iPhones
        • 70,000 segment commands to define computer architecture with registers and 64-bit adder
      • Quote
        • “The bootstrapping operations for the sandbox escape exploit are written to run on this logic circuit and the whole thing runs in this weird, emulated environment created out of a single decompression pass through a JBIG2 stream. It’s pretty incredible, and at the same time, pretty terrifying.”
    • Quotes regarding working at Project Zero
      • “learn about coding and how computers work”
      • “learning the fundamentals of programming, operating systems, and machine architecture is a great starting point”

6.2. Summary

  • Nand to Tetris guides you to build a computer
    • Based on abstraction and modularity
    • Weekly projects, with guidance from us

6.3. Q&A

Uncovering questions

Uncovering questions” under CC0 1.0; background changed from Pixabay

Bibliography

Neumann, John von. 1945. “First Draft of a Report on the EDVAC.” University of Pennsylvania. https://web.mit.edu/STS.035/www/PDFs/edvac.pdf.
Nisan, Noam, and Shimon Schocken. 2005. The Elements of Computing Systems: Building a Modern Computer from First Principles. The MIT Press. https://www.nand2tetris.org/.

License Information

Source files are available on GitLab (check out embedded submodules) under free licenses. Icons of custom controls are by @fontawesome, released under CC BY 4.0.

Except where otherwise noted, the work “Nand2Tetris”, © 2024 Jens Lechtenbörger, is published under the Creative Commons license CC BY-SA 4.0.