Introduction: From Nand to Tetris Building a Modern Computer From First Principles www.nand2tetris.org Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction slide 1 The course at a glance Objectives: Understand how hardware and software systems are built, and how they work together Learn how to break complex problems into simpler ones Learn how large scale development projects are planned and executed Have fun Methodology: Build a complete, general-purpose, and working computer system Play and experiment with this computer, at any level of interest. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction slide 2 Some course details 12 projects, can be done by pairs Hardware projects are done and simulated in HDL (Hardware Description Language) Software projects can be done in any language of your choice (we recommend Java) Projects methodology: o Design (API) + test materials are given o Implementation done by students Tools: simulators, tutorials, test scripts Book Q&A policy Course grade. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction slide 3 Demo Pong, 1985 Pong, 2011 Pong, on our computer Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction slide 4 Course theme and structure Human Thought Abstract design Software hierarchy abstract interface Chapters 9, 12 H.L. Language & Operating Sys. Compiler abstract interface Chapters 10 - 11 Virtual Machine VM Translator abstract interface Chapters 7 - 8 Assembly Language Assembler Chapter 6 abstract interface Machine Language Computer Architecture abstract interface Chapters 4 - 5 Hardware Platform Hardware hierarchy Gate Logic abstract interface Chapters 1 - 3 Chips & Logic Gates Electrical Engineering Physics (Abstraction–implementation paradigm) Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction slide 5 Application level: Pong (example app) Ball abstraction Bat abstraction Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction slide 6 The big picture Human Thought Abstract design Software hierarchy abstract interface Chapters 9, 12 H.L. Language & Operating Sys. Compiler abstract interface Chapters 10 - 11 Virtual Machine VM Translator abstract interface Chapters 7 - 8 Assembly Language Assembler Chapter 6 abstract interface Machine Language Computer Architecture abstract interface Chapters 4 - 5 Hardware Platform Hardware hierarchy Gate Logic abstract interface Chapters 1 - 3 Chips & Logic Gates Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction Electrical Engineering Physics slide 7 High-level programming (our very own Jack language) /** /** AA Graphic Graphic Bat Bat for for aa Pong Pong Game Game */ */ class Bat { class Bat { field // field int int x, x, y; y; // screen screen location location of of the the bat's bat's top-left top-left corner corner field int width, height; // bat's width & height field int width, height; // bat's width & height // // The The class class constructor constructor and and most most of of the the class class methods methods are are omitted omitted /** /** Draws Draws (color=true) (color=true) or or erases erases (color=false) (color=false) the the bat bat */ */ method void draw(boolean color) { method void draw(boolean color) { do do Screen.setColor(color); Screen.setColor(color); do Screen.drawRectangle(x,y,x+width,y+height); do Screen.drawRectangle(x,y,x+width,y+height); return; return; }} Typical call to an OS method /** /** Moves Moves the the bat bat one one step step (4 (4 pixels) pixels) to to the the right. right. */ */ method void moveR() { method void moveR() { do do draw(false); draw(false); // // erase erase the the bat bat at at the the current current location location let x = x + 4; // change the bat's X-location let x = x + 4; // change the bat's X-location // but don't go beyond // but don't go beyond the the screen's screen's right right border border if ((x + width) > 511) { if ((x + width) > 511) { let let xx == 511 511 -- width; width; }} do do draw(true); draw(true); // // re-draw re-draw the the bat bat in in the the new new location location return; return; }} Ball abstraction Bat abstraction }} Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction slide 8 Operating system level (our very own Jack OS) /** /** An An OS-level OS-level screen screen driver driver that that abstracts abstracts the the computer's computer's physical physical screen screen */ */ class class Screen Screen {{ static static boolean boolean currentColor; currentColor; // // the the current current color color // // The The Screen Screen class class is is aa collection collection of of methods, methods, each each implementing implementing one one // abstract screen-oriented operation. Most of this code is omitted. // abstract screen-oriented operation. Most of this code is omitted. /** /** Draws Draws aa rectangle rectangle in in the the current current color. color. */ */ // the rectangle's top left corner is anchored // the rectangle's top left corner is anchored at at screen screen location location (x0,y0) (x0,y0) // // and and its its width width and and length length are are x1 x1 and and y1, y1, respectively. respectively. function function void void drawRectangle(int drawRectangle(int x0, x0, int int y0, y0, int int x1, x1, int int y1) y1) {{ var var int int x, x, y; y; let x = x0; let x = x0; while while (x (x << x1) x1) {{ let let yy == y0; y0; while(y while(y << y1) y1) {{ do do Screen.drawPixel(x,y); Screen.drawPixel(x,y); let let yy == y+1; y+1; }} let let xx == x+1; x+1; Ball abstraction Bat abstraction }} }} }} Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction slide 9 The big picture Human Thought Abstract design Software hierarchy abstract interface Chapters 9, 12 H.L. Language & Operating Sys. Compiler abstract interface Chapters 10 - 11 Virtual Machine VM Translator abstract interface Chapters 7 - 8 Assembly Language Assembler Chapter 6 abstract interface Machine Language Computer Architecture abstract interface Chapters 4 - 5 Hardware Platform Hardware hierarchy Gate Logic abstract interface Chapters 1 - 3 Chips & Logic Gates Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction Electrical Engineering Physics slide 10 A modern compilation model Some language ... Some compiler ... Some Other language Jack language Proj. 9: building an app. Proj. 12: building the OS Jack compiler Some Other compiler Projects 10-11 VM language VM implementation over CISC platforms CISC machine language VM imp. over RISC platforms RISC machine language CISC machine RISC machine written in a high-level language ... ... VM imp. over the Hack platform VM emulator Projects 7-8 Hack machine language Projects ... other digital platforms, each equipped with its VM implementation Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction 1-6 Any computer Hack computer slide 11 Compilation 101 Intermediate code > Source code (x (x ++ width) width) >> 511 511 parsing + 511 Code generation push push xx push push width width add add push push 511 511 gt gt x Abstraction Syntax Analysis width Parse Tree Semantic Synthesis Implementation Observations: Modularity Abstraction / implementation interplay The implementation uses abstract services from the level below. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction slide 12 The Virtual Machine (our very own VM, modeled after Java’s JVM) if ((x+width)>511) { let x=511-width; } ... width 75 450 ... // VM implementation s2 memory (before) x 450 sp 75 ... push x // s1 push width // s2 add // s3 push 511 // s4 525 gt // s5 511 if-goto L1 // s6 goto L2 // s7 s4 s5 s9 1 511 sp 450 sp sp L1: push 511 // s8 push width // s9 sub // s10 pop x // s11 s10 memory (after) ... 61 sp width ... L2: ... Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction 450 x 61 ... slide 13 The big picture Human Thought Abstract design Software hierarchy abstract interface Chapters 9, 12 H.L. Language & Operating Sys. Compiler abstract interface Chapters 10 - 11 Virtual Machine VM Translator abstract interface Chapters 7 - 8 Assembly Language Assembler Chapter 6 abstract interface Machine Language Computer Architecture abstract interface Chapters 4 - 5 Hardware Platform Hardware hierarchy Gate Logic abstract interface Chapters 1 - 3 Chips & Logic Gates Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction Electrical Engineering Physics slide 14 Low-level programming (on the Hack computer) For now, ignore all details! Virtual machine program ... push x push width add push 511 gt if-goto L1 goto L2 L1: push 511 push width sub pop x L2: ... Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction slide 15 Low-level programming (on the Hack computer) For now, ignore all details! Virtual machine program ... push x push width add push 511 VM translator Assembly program gt if-goto L1 // push 511 goto L2 @511 L1: D=A push 511 @SP push width A=M sub M=D pop x @SP L2: // D=511 // *SP=D M=M+1 // SP++ ... Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction slide 16 Low-level programming (on the Hack computer) For now, ignore all details! Virtual machine program ... push x push width add push 511 VM translator Assembly program gt if-goto L1 // push 511 goto L2 @511 L1: D=A // D=511 push 511 @SP push width A=M sub M=D pop x @SP @SP M=M+1 // SP++ L2: Assembler // *SP=D Executable 0000000000000000 1110110010001000 ... Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction slide 17 The big picture Human Thought Abstract design Software hierarchy abstract interface Chapters 9, 12 H.L. Language & Operating Sys. Compiler abstract interface Chapters 10 - 11 Virtual Machine VM Translator abstract interface Chapters 7 - 8 Assembly Language Assembler Chapter 6 abstract interface Machine Language Computer Architecture abstract interface Chapters 4 - 5 Hardware Platform Hardware hierarchy Gate Logic abstract interface Chapters 1 - 3 Chips & Logic Gates Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction Electrical Engineering Physics slide 18 For now, ignore all details! Machine language semantics (our very own Hack platform) Code semantics, as interpreted by the Hack hardware platform Instruction code (0=“address” inst.) Address Code syntax 0 0000000000000000 @0 1111110111001000 M=M-1 1 0 1 0 0 1 1 Instruction code (1=“compute” inst.) 0 1 0 1 0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 ALU operation code Destination Code Jump Code (M-1) (M) (no jump) We need a hardware architecture that realizes this semantics The hardware platform should be designed to: o Parse instructions, and o Execute them. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction slide 19 For now, ignore all details! Computer architecture (Hack platform, approx.) instruction Instruction Memory A ALU D Data Memory data out (M) M address of next instruction Program Counter data in RAM(A) A typical Von Neumann machine Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction slide 20 The big picture Human Thought Abstract design Software hierarchy abstract interface Chapters 9, 12 H.L. Language & Operating Sys. Compiler abstract interface Chapters 10 - 11 Virtual Machine VM Translator abstract interface Chapters 7 - 8 Assembly Language Assembler Chapter 6 abstract interface Machine Language Computer Architecture abstract interface Chapters 4 - 5 Hardware Platform Hardware hierarchy Gate Logic abstract interface Chapters 1 - 3 Chips & Logic Gates Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction Electrical Engineering Physics slide 21 Logic design Combinational logic Sequential logic (leading to an ALU) (leading to a RAM) Putting the whole thing together (leading to a Computer) Using … gate logic. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction slide 22 Gate logic Hardware platform = inter-connected set of chips Chips are made of simpler chips, all the way down to elemantary logic gates Logic gate = hardware element that implements a certain Boolean function Every chip and gate has an interface, specifying WHAT it is doing, and an implementation, specifying HOW it is doing it. Implementation Interface a a Xor out And Not b Or a b out 0 0 1 1 0 1 0 1 0 1 1 0 out Not b Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction And slide 23 Hardware Description Language (HDL) a And Not Or out Not b And CHIP Xor { IN a,b; OUT out; PARTS: Not(in=a,out=Nota); Not(in=b,out=Notb); And(a=a,b=Notb,out=w1); And(a=Nota,b=b,out=w2); Or(a=w1,b=w2,out=out); } Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction slide 24 The tour ends: One implementation option (CMOS) Interface a out Nand b a b out 0 0 1 1 0 1 0 1 1 1 1 0 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction slide 25 The tour map, revisited Human Thought Abstract design Software hierarchy abstract interface Chapters 9, 12 H.L. Language & Operating Sys. Compiler abstract interface Chapters 10 - 11 Virtual Machine VM Translator abstract interface Chapters 7 - 8 Assembly Language Assembler Chapter 6 Course overview: Building this world, from the ground up abstract interface Machine Language Computer Architecture abstract interface Chapters 4 - 5 Hardware Platform Hardware hierarchy Gate Logic abstract interface Chapters 1 - 3 Chips & Logic Gates Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Introduction Electrical Engineering Physics slide 26