cs101: digital systems construction intro.pdf · cs101, digital systems construction, shimon...
TRANSCRIPT
CS101, Digital Systems Construction, Shimon Schocken Harvard University, Fall 2005
CS101 1 Course at a Glance
Here’s a constantand an operator.Now go build a computer
How?
One step at a time
False
Nand
CS101: Digital Systems Construction
Building a Modern Computer From First PrinciplesInstructor: Shimon Schocken
CS101 2 Course at a Glance
The Course Theme: Let’s Build a ComputerCourse Goals
Explicit: Let’s build a computer!
Implicit: Understand ...
Key hardware & software abstractions
Key interfaces: compilation, VM, O/S
Appreciate: Science history and method
Plus: Have fun.CS101
Course Methodology
Constructive: hands-on
Self-contained: only requirement is programming
Guided: all "plans" are given
Focused: Occam’s razor.False
Nand
CS101, Digital Systems Construction, Shimon Schocken Harvard University, Fall 2005
CS101 3 Course at a Glance
Sample Applications
CS101 4 Course at a Glance
Sample Applications
CS101, Digital Systems Construction, Shimon Schocken Harvard University, Fall 2005
CS101 5 Course at a Glance
assembler
machine language
assembly
stack machine
virtual machine
compiler
operating system prog. language
Software hierarchy
various combinational chips various sequential chips
ALU / CPU memory units
computer architecture
machine language
essential theory(Boolean algebra and gate logic)
hardware platform
Course map
P1
P2 P3
P5
P4
P = Instructional unit(chapter/lecture/project/week)
Each unit is self-contained
Each unit provides the building blocks with which we build the unit above it.
P7,8
P9
P10,11
P12
P6
CS101 6 Course at a Glance
Hardware projects
Tools:
HDL (Hard. Descr. Language)
Test Description Language
Hardware simulator.
Hardware projects:
P1: Elementary logic gates
P2: Combinational gates (ALU)
P3: Sequential gates (memory)
P4: Machine language
P5: Computer architecturevarious combinational chips various sequential chips
ALU / CPU memory units
computer architecture
machine language
essential theory(Boolean algebra and gate logic)
hardware platform
P1
P2 P3
P5
P4
CS101, Digital Systems Construction, Shimon Schocken Harvard University, Fall 2005
CS101 7 Course at a Glance
Project 1: Elementary logic gates
Given: Nand(a,b), false
Not(a) = Nand(a,a)
true = Not(false)
And(a,b) = Not(Nand(a,b))
Or(a,b) = . . .
Mux(s,a,b) = . . .
Etc. - 12 gates altogether.
a b Nand(a,b)0 0 10 1 1 1 0 1 1 1 0
a b Nand(a,b)0 0 10 1 1 1 0 1 1 1 0
CS101 8 Course at a Glance
outa
bAnd
a b out0 0 00 1 0 1 0 0 1 1 1
a b out0 0 00 1 0 1 0 0 1 1 1
And.cmp
load And.hdl,output-file And.out,compare-to And.cmp,output-list a b out;set a 0,set b 0,eval,output;set a 0,set b 1,eval,output;set a 1,set b 0,eval,output;set a 1, set b 1, eval, output;
load And.hdl,output-file And.out,compare-to And.cmp,output-list a b out;set a 0,set b 0,eval,output;set a 0,set b 1,eval,output;set a 1,set b 0,eval,output;set a 1, set b 1, eval, output;
And.tstAnd.hdl
CHIP And { IN a, b;
OUT out;
// implementation missing}
CHIP And { IN a, b;
OUT out;
// implementation missing}
Building an And gate
Contract:
When running your .hdl on our .tst, your .out should be the same asour .cmp.
CS101, Digital Systems Construction, Shimon Schocken Harvard University, Fall 2005
CS101 9 Course at a Glance
CHIP And { IN a, b;
OUT out;Nand(a = a,
b = b,out = x);
Not(in = x, out = out)}
CHIP And { IN a, b;
OUT out;Nand(a = a,
b = b,out = x);
Not(in = x, out = out)}
Implementation: And(a,b) = Not(Nand(a,b))
outNOT
a
b
outNANDa in out
xb
Building an And gate
And.hdl
CS101 10 Course at a Glance
Hardware simulator
And
And
Or out
a
b
test script
gate diagram
HDL program
CS101, Digital Systems Construction, Shimon Schocken Harvard University, Fall 2005
CS101 11 Course at a Glance
CHIP Add16 {
IN a[16], b[16];
OUT out[16];
PARTS:
...
FullAdder(...);
...
}
CHIP Add16 {
IN a[16], b[16];
OUT out[16];
PARTS:
...
FullAdder(...);
...
}
CHIP FullAdder {
IN a, b, c;
OUT sum,
carry;
PARTS:
HalfAdder(...);
...
}
CHIP FullAdder {
IN a, b, c;
OUT sum,
carry;
PARTS:
HalfAdder(...);
...
}
CHIP HalfAdder {
IN a, b;
OUT sum,
carry;
PARTS:
Xor(...);
...
}
CHIP HalfAdder {
IN a, b;
OUT sum,
carry;
PARTS:
Xor(...);
...
}
CHIP Xor {
IN a, b;
OUT out;
PARTS:
...
And(...);
}
CHIP Xor {
IN a, b;
OUT out;
PARTS:
...
And(...);
}
CHIP And {
IN a, b;
OUT out;
PARTS:
Not(...);
...
}
CHIP And {
IN a, b;
OUT out;
PARTS:
Not(...);
...
}
CHIP Not {
IN in;
OUT out;
PARTS:
Nand(a=in,b=in,out=out);
}
CHIP Not {
IN in;
OUT out;
PARTS:
Nand(a=in,b=in,out=out);
}
CHIP Nand {
IN a, b;
OUT out;
PARTS:
BUILTIN Nand; // Implemented by Nand.java
}
CHIP Nand {
IN a, b;
OUT out;
PARTS:
BUILTIN Nand; // Implemented by Nand.java
}
Chip anatomy
CS101 12 Course at a Glance
Project 2: Combinational chips
h alf ad d er
a s um
b c arryfu l l
ad d er
asum
bc arry
c
outa
16
1 6 -b itad d e r
b16
16
zx no
zr
nx zy ny f
ALU
ng
16 bits
16 bits
x
y 16 bitsout
out(x, y, control bits) =
x+y, x-y, y–x,
0, 1, -1,
x, y, -x, -y,
x!, y!,
x+1, y+1, x-1, y-1,
x&y, x|y
CS101, Digital Systems Construction, Shimon Schocken Harvard University, Fall 2005
CS101 13 Course at a Glance
A glimpse ahead:
ALU
Mux
D
out
A/M
a
D
AA
M
c1,c2,…,c6
RAM
CS101 14 Course at a Glance
Project 3: Sequential chips
Bit Bit register
RAM8Register
RAM 8
RAM 64
8
8
register
..
.register
..
.
RAM8
. . .Bit . . .
DFF > Bit > Register > RAM8 > RAM64 > ... > RAM32K
CS101, Digital Systems Construction, Shimon Schocken Harvard University, Fall 2005
CS101 15 Course at a Glance
Hardware projects
various combinational chips various sequential chips
ALU / CPU memory units
computer architecture
machine language
essential theory(Boolean algebra and gate logic)
hardware platform
P1
P2 P3
P5
Hardware projects:
P1: Elementary logic gates
P2: Combinational gates (ALU)
P3: Sequential gates (memory)
P4: Machine language
P5: Computer architecture
CS101 16 Course at a Glance
Machine Language: A-instruction
value (v = 0 or 1)
0 v v v v v v v v v v v v v v vBinary:
@value // Where value is either a non-negative decimal number // or a symbol referring to such number.
Symbolic:
@value // A register = value@value // A register = value
CS101, Digital Systems Construction, Shimon Schocken Harvard University, Fall 2005
CS101 17 Course at a Glance
Machine Language: C-instruction
0,1,-1,D,A,!D,!A,-D,-A,D+1,A+1,D-1,A-1,D+A,D-A,A-D,D&A,D|A,
M, !M, -M, M+1, M-1,D+M,D-M,M-D,D&M,D|M
0,1,-1,D,A,!D,!A,-D,-A,D+1,A+1,D-1,A-1,D+A,D-A,A-D,D&A,D|A,
M, !M, -M, M+1, M-1,D+M,D-M,M-D,D&M,D|M
comp is one of:
Null, M, D, MD, A, AM, AD, AMDNull, M, D, MD, A, AM, AD, AMD
dest is one of:
Null, JGT, JEQ, JGE,JLT, JNE, JLE,JMP Null, JGT, JEQ, JGE,JLT, JNE, JLE,JMP
jump is one of:
dest = comp ; jump // If dest is null, the “=“ is ommitted
// If jump is null, the “;“ is ommitted
dest = comp ; jump // If dest is null, the “=“ is ommitted
// If jump is null, the “;“ is ommitted
ALU
Mux
D
out
A/M
a
D
AA
M
c1,c2,…,c6
RAM
CS101 18 Course at a Glance
Hardware projects
various combinational chips various sequential chips
ALU / CPU memory units
computer architecture
machine language
essential theory(Boolean algebra and gate logic)
hardware platform
P1
P2 P3
P5
Hardware projects:
P1: Elementary logic gates
P2: Combinational gates (ALU)
P3: Sequential gates (memory)
P4: Machine language
P5: Computer architecture
CS101, Digital Systems Construction, Shimon Schocken Harvard University, Fall 2005
CS101 19 Course at a Glance
Project 5: CPU
ALU
Mux
D
Mux
reset
inM
addressM
pc
outM
A/Minstruction
decode
C
C
C
C
C
D
A
PC
C
C
A
A
A
M
ALU output
reset
reset
writeMC
C
CHIP CPU {
IN inM[16], instruction[16], reset;
OUT outM[16], writeM, addressM[15], pc[15];
PARTS:
...
}
CHIP CPU {
IN inM[16], instruction[16], reset;
OUT outM[16], writeM, addressM[15], pc[15];
PARTS:
...
}
CS101 20 Course at a Glance
Project 5: Computer
DataMemory
(Memory)
instruction
CPU
InstructionMemory
(ROM32K)
inM
outM
addressM
writeM
pc
reset
Many other computer models can be built.
CHIP Computer {IN reset;PARTS:...
}
CHIP Computer {IN reset;PARTS:...
}
CS101, Digital Systems Construction, Shimon Schocken Harvard University, Fall 2005
CS101 21 Course at a Glance
Input / Output Devices
load
out
in
16
15
16
RAM(16K)
address
0x0000
0X3FFF
screenmemory map
(8K)
0x4000
0x5FFF
0x6000keyboard
memory map
Data Memory
Keyboard
screen
CS101 22 Course at a Glance
Course map
assembler
machine language
assembly
stack machine
virtual machine
compiler
operating system prog. language
Software hierarchy
various combinational chips various sequential chips
ALU / CPU memory units
computer architecture
machine language
essential theory(Boolean algebra and gate logic)
hardware platform
CS101, Digital Systems Construction, Shimon Schocken Harvard University, Fall 2005
CS101 23 Course at a Glance
Project 6: Assembler
Sum.asm
// Computes sum=1+2+ … +100.@i // i=1M=1@sum // sum=0M=0
(LOOP)@i // if i-100>0 goto ENDD=M@100D=D-A@ENDD;jgt@i // sum+=iD=M@sumM=D+M@i // i++M=M+1@LOOP // goto LOOP0;jmp
(END)@END0;JMP
// Computes sum=1+2+ … +100.@i // i=1M=1@sum // sum=0M=0
(LOOP)@i // if i-100>0 goto ENDD=M@100D=D-A@ENDD;jgt@i // sum+=iD=M@sumM=D+M@i // i++M=M+1@LOOP // goto LOOP0;jmp
(END)@END0;JMP
000000000001000011101111110010000000000000010001111010101000100000000000000100001111110000010000000000000110010011100100110100000000000000010010111000110000000100000000000100001111110000010000000000000001000111110000100010000000000000010000111111011100100000000000000001001110101010000111
000000000001000011101111110010000000000000010001111010101000100000000000000100001111110000010000000000000110010011100100110100000000000000010010111000110000000100000000000100001111110000010000000000000001000111110000100010000000000000010000111111011100100000000000000001001110101010000111
Sum.bin
Assembler
CS101 24 Course at a Glance
Assembler in action
CS101, Digital Systems Construction, Shimon Schocken Harvard University, Fall 2005
CS101 25 Course at a Glance
assembler
machine language
assembly
stack machine
virtual machine
compiler
operating system prog. language
Software hierarchy
Software projects
P7, 8
P9
P10, 11
P12
P6
CS101 26 Course at a Glance
The Big Picture
. . .RISC
machine
VM language
other digital platforms, each equippedwith its VM implementation
RISCmachinelanguage
Hackcomputer
Hackmachinelanguage
CISCmachinelanguage
CISCmachine
. . .
Projects10-11
written ina high-levellanguage
Anycomputer
. . .
VMimplementation
over CISCplatforms
VM imp.over RISCplatforms
VM imp.over the Hack
platformVM
emulator
Some Otherlanguage
Jacklanguage
Somecompiler Some Other
compilerJack
compiler
. . .
Projects7-8
Somelanguage
. . .
Projects1-6
Proj. 9: building an app.
Proj. 12: building the OS
CS101, Digital Systems Construction, Shimon Schocken Harvard University, Fall 2005
CS101 27 Course at a Glance
The VM language
Arithmetic commands
add
sub
neg
eq
gt
lt
and
or
not
Arithmetic commands
add
sub
neg
eq
gt
lt
and
or
not
Memory access commands
pop x
push x
Program flow commands
label symbol
goto symbol
if-goto symbol
Function calling commands
function funcationName nLocals
call functionName nArgs
return
Memory access commands
pop x
push x
Program flow commands
label symbol
goto symbol
if-goto symbol
Function calling commands
function funcationName nLocals
call functionName nArgs
return
CS101 28 Course at a Glance
The VM abstraction: a Stack Machine
function mult(x,y) {int result, j;result=0;j=y;while ~(j=0) {
result=result+x;j=j-1;
}return result;
}
function mult(x,y) {int result, j;result=0;j=y;while ~(j=0) {
result=result+x;j=j-1;
}return result;
}
High-level codefunction mult(x,y)
push 0pop resultpush ypop j
label looppush jpush 0eqif-goto endpush resultpush xaddpop resultpush jpush 1subpop jgoto loop
label endpush resultreturn
function mult(x,y) push 0pop resultpush ypop j
label looppush jpush 0eqif-goto endpush resultpush xaddpop resultpush jpush 1subpop jgoto loop
label endpush resultreturn
Stack machine code
CS101, Digital Systems Construction, Shimon Schocken Harvard University, Fall 2005
CS101 29 Course at a Glance
VM code
virtual memory segments
working stack (the RAM is
not part of the VM)
default test script
host RAMglobal stack
CS101 30 Course at a Glance
Projects 7,8: Implement the VM over the Hack platform
Mult.vm
function mult 2 // 2 local variablespush constant 0 // result=0pop local 0push argument 1 // j=ypop local 1
label looppush constant 0 // if j==0 goto endpush local 1eqif-goto endpush local 0 // result=result+xpush argument 0addpop local 0push local 1 // j=j-1push constant 1subpop local 1goto loop
label endpush local 0 // return resultreturn
function mult 2 // 2 local variablespush constant 0 // result=0pop local 0push argument 1 // j=ypop local 1
label looppush constant 0 // if j==0 goto endpush local 1eqif-goto endpush local 0 // result=result+xpush argument 0addpop local 0push local 1 // j=j-1push constant 1subpop local 1goto loop
label endpush local 0 // return resultreturn
Mult.asm
VM Translator
...A=M-1M=0@5D=A@LCLA=M-DD=M@R6M=D@SPAM=M-1D=M@ARGA=MM=DD=A@SPM=D+1@LCLD=M...
...A=M-1M=0@5D=A@LCLA=M-DD=M@R6M=D@SPAM=M-1D=M@ARGA=MM=DD=A@SPM=D+1@LCLD=M...
CS101, Digital Systems Construction, Shimon Schocken Harvard University, Fall 2005
CS101 31 Course at a Glance
assembler
machine language
assembly
stack machine
virtual machine
compiler
operating system prog. language
Software hierarchy
Software projects
CS101 32 Course at a Glance
Jack Language/** A Graphic Bat for a Pong Game */class Bat { field int x, y; // screen location of the bat's top-left corner field int width, height; // bat's width & height
// The class constructor and most of the class methods are omitted
/** Draws (color=true) or erases (color=false) the bat */ method void draw(boolean color) { do Screen.setColor(color); do Screen.drawRectangle(x,y,x+width,y+height); return; }
/** Moves the bat one step (4 pixels) to the right. */ method void moveR() { do draw(false); // erase the bat at the current location let x = x + 4; // change the bat's X-location // but don't go beyond the screen's right border if ((x + width) > 511) { let x = 511 - width; } do draw(true); // re-draw the bat in the new location return; }}
Ballabstraction
Batabstraction
CS101, Digital Systems Construction, Shimon Schocken Harvard University, Fall 2005
CS101 33 Course at a Glance
Compiler project I: Syntax Analysis
(5+y)*2 – sqrt(x*4)(5+y)*2 – sqrt(x*4)
Prog.jack
<expression><term><symbol> ( </symbol><expression><term><integerConstant> 5 </integerConstant>
</term><symbol> + </symbol><term><identifier> y </identifier >
</term>...
</expression>
<expression><term><symbol> ( </symbol><expression><term><integerConstant> 5 </integerConstant>
</term><symbol> + </symbol><term><identifier> y </identifier >
</term>...
</expression>
Prog.xml Prog.vm
-
5
sqrt
+
y
2
x 4
*
*
SyntaxAnalyzer
JackGrammar
CS101 34 Course at a Glance
Compiler project II: Code Generation
Prog.vm
-
5
sqrt
+
y
2
x 4
*
*
(5+y)*2 – sqrt(x*4)(5+y)*2 – sqrt(x*4)
Prog.jack
Syntax analyzer
Project 9
push 5push yaddpush 2call multpush xpush 4call multcall sqrtsub
push 5push yaddpush 2call multpush xpush 4call multcall sqrtsub
Prog.vm
Code generator
Project 10
CS101, Digital Systems Construction, Shimon Schocken Harvard University, Fall 2005
CS101 35 Course at a Glance
assembler
machine language
assembly
stack machine
virtual machine
compiler
operating system prog. language
Software hierarchy
Software projects
CS101 36 Course at a Glance
/** Computes the average of a sequence of integers. */class Main {function void main() {var Array a; var int length;var int i, sum;
let length = Keyboard.readInt(”How many numbers? ”);let a = Array.new(length); // Constructs the arraylet i = 0;
while (i < length) {let a[i] = Keyboard.readInt(”Enter the next number: ”);let sum = sum + a[i];let i = i + 1;
}
do Output.printString(”The average is: ”);do Output.printInt(sum / length);do Output.println();return;
}}
/** Computes the average of a sequence of integers. */class Main {function void main() {var Array a; var int length;var int i, sum;
let length = Keyboard.readInt(”How many numbers? ”);let a = Array.new(length); // Constructs the arraylet i = 0;
while (i < length) {let a[i] = Keyboard.readInt(”Enter the next number: ”);let sum = sum + a[i];let i = i + 1;
}
do Output.printString(”The average is: ”);do Output.printInt(sum / length);do Output.println();return;
}}
Typical Jack code
CS101, Digital Systems Construction, Shimon Schocken Harvard University, Fall 2005
CS101 37 Course at a Glance
OS Libraries
Math: Provides basic mathematical operations;
String: Implements the String type and string-related operations;
Array: Implements the Array type and array-related operations;
Output: Handles text output to the screen;
Screen: Handles graphic output to the screen;
Keyboard: Handles user input from the keyboard;
Memory: Handles memory operations;
Sys: Provides some execution-related services.
CS101 38 Course at a Glance
Recap
250 lines of Jack code1000 lines of VM code
4,000 lines of Assembly30,000 lines of Hack code
500,000 bits2,000,000 logic gates
1 Godlogicchip-set
architecturemachine language
assemblyvirtual machine
high-level language / OS
application (e.g. Pong) 15 obj-oriented bat / ball operations
SW
False
Nand
ALU
Mux
D
Mux
reset
inM
addressM
pc
outM
A/Minstruction
decode
C
C
C
C
C
D
A
PC
C
C
A
A
A
M
ALU output
reset
reset
writeMC
C
HW
CS101, Digital Systems Construction, Shimon Schocken Harvard University, Fall 2005
CS101 39 Course at a Glance
“We deliberate not about ends, but about means. For a doctor does not deliberate whether he shall heal, nor an orator whether he shall persuade ... They assume the end and consider how and by what means it is attained, and if it seems easily and best produced thereby; And if it is achieved by some means, they consider how it will be achieved, and by what means this will be achieved, until they come to the first cause. And what is last in the order of analysis seems to be first in the order of becoming.”
Perspective