Logic Circuit Des Lec1
Logic Circuit Des Lec1
Lecture 1
Boolean Logic
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 1
Chapter 1: Boolean logic
• Multi-bit buses
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 2
Boolean values
off
on
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 3
Boolean values
off no false 0
on yes true 1
George Boole
Different labels, all referring to two possible states.
1815 - 1864
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 4
Boolean values
b1 b0
• 1 binary variable: 2 possible states
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 5
Boolean values
b1 b0
• 1 binary variable: 2 possible states
• 2 binary variables: 4 possible states
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 6
Boolean values
b2 b1 b0
• 1 binary variable: 2 possible states
• 2 binary variables: 4 possible states
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 7
Boolean values
... b2 b1 b0
• 1 binary variable: 2 possible states
• 2 binary variables: 4 possible states
• 3 binary variables: 8 possible states
• ...
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 8
Boolean values
... b2 b1 b0
• 1 binary variable: 2 possible states
• 2 binary variables: 4 possible states
• 3 binary variables: 8 possible states
• ...
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 9
Boolean functions
x y f
0 0 0
0 1 0
1 0 0
1 1 1
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 10
Boolean functions
x y f
0 0 0
0 1 0
1 0 0
1 1 1
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 11
Boolean functions
x
x y f f(x,y)
y f
0 0 0
0 1 0
1 0 0 1 when x == 1 and y == 1
f(x,y) =
1 1 1 0 otherwise
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 12
Boolean functions
x
x y And And (x, y)
y And
0 0 0
0 1 0
1 0 0 1 when x == 1 and y == 1
And(x,y) =
1 1 1 0 otherwise
Boolean function
A function that operates on boolean variables, and returns a boolean value
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 13
Boolean functions
x And y x Or y Not(x)
x y And x y Or x Not
0 0 0 0 0 0 0 1
0 1 0 0 1 1 1 0
1 0 0 1 0 1
1 1 1 1 1 1
0 1 1 0 1 1 ... 0 1 v2
1 0 1 1 0 1 1 0 v3
1 1 0 1 1 0 1 1 v4
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 14
Boolean functions
x And y x Or y Question:
x y And x y Or How many Boolean functions
x f y exist over two binary
0 0 0 0 0 0
(2-valued) variables?
0 1 0 0 1 1
1 0 0 1 0 1
Answer: 16
!
N binary variables span 2!
1 1 1 1 1 1
Boolean functions.
0 1 1 0 1 1 ... 0 1 v2
1 0 1 1 0 1 1 0 v3
1 1 0 1 1 0 1 1 v4
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 15
Chapter 1: Boolean logic
• Multi-bit buses
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 16
The expressive power of Nand
Observations Thus:
• Not ( x ) = x Nand x • Not can be realized using Nand
• x And y = Not ( x Nand y) • And can be realized using Nand
• x Or y = Not(Not(x) And Not(y)) • Or can be realized using Nand
• (De Morgan)
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 17
The expressive power of Nand
Theorem: Any Boolean function can be realized using only Nand.
Computers:
Machines that realize
Boolean functions:
f (input bits) = output bits
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 18
Chapter 1: Boolean logic
• Multi-bit buses
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 19
Logic gates
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 20
Elementary gates
And
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 21
Elementary gates
And
And circuit
Or circuit
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 22
Composite gates
And3
Possible implementations:
Physical Logical
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 23
Chapter 1: Boolean logic
• Multi-bit buses
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 24
Building a chip
a
Xor out ?
b
The process
• Design the chip architecture
• Specify the architecture in HDL
• Test the chip in a hardware simulator
• Optimize the design
• Realize the optimized design in silicon.
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 25
Building a chip
a
Xor out ?
b
The process
ü Design the chip architecture
ü Specify the architecture in HDL
ü Test the chip in a hardware simulator
• Optimize the design
• Realize the optimized design in silicon.
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 26
Design: Requirements
a a b out
Xor out 0 0 0
Requirement
b
0 1 1 Build a chip that
delivers this
if ((a == 0 and b == 1) or (a == 1 and b == 0)) 1 0 1
functionality
out = 1 1 1 0
else
out = 1
/** out = (a And Not(b)) Or (Not(a) And b)) */ /** Chips set (APIs): */
CHIP Xor { ...
IN a, b; Not (in = , out = );
OUT out;
And (a = , b = , out = );
PARTS:
Or (a = , b = , out = );
// Missing implementation
Xor (a = , b = , out = );
...
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 27
Design: Implementation
a
a out
b
And
Gate diagram
in out aAndNotb
Not
notb
a
Or out
b
out
Not nota
in out
notaAndb
a
out
And
b
b
/** out = (a And Not(b)) Or (Not(a) And b)) */ /** Chips set (APIs): */
CHIP Xor { ...
IN a, b; Not (in = , out = );
OUT out;
And (a = , b = , out = );
PARTS:
Or (a = , b = , out = );
// Missing implementation
Xor (a = , b = , out = );
...
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 28
Design: Implementation
a
a out
b
And
Gate diagram
in out aAndNotb
Not
notb
a
Or out
b
out
Not nota
in out
notaAndb
a
out
And
b
b
/** out = (a And Not(b)) Or (Not(a) And b)) */ /** Chips set (APIs): */
CHIP Xor { ...
IN a, b; Not (in = , out = );
OUT out;
And (a = , b = , out = );
PARTS:
Or (a = , b = , out = );
// Missing implementation
Xor (a = , b = , out = );
...
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 29
Design: Implementation
a
a out
b
And
in out aAndNotb
Not
notb
a
Or out
b
out
Not nota
in out
notaAndb
a
out
And
b
b
/** out = (a And Not(b)) Or (Not(a) And b)) */ /** Chips set (APIs): */
CHIP Xor { ...
IN a, b; Not (in = , out = );
OUT out;
And (a = , b = , out = );
PARTS:
Or (a = , b = , out = );
// Missing implementation
Xor (a = , b = , out = );
...
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 30
Design: Implementation
a
a out
b
And
in out aAndNotb
Not
notb
a
Or out
b
out
Not nota
in out
notaAndb
a
out
And
b
b
/** out = (a And Not(b)) Or (Not(a) And b)) */ /** Chips set (APIs): */
CHIP Xor { ...
IN a, b; Not (in = , out = );
OUT out;
And (a = , b = , out = );
PARTS:
Or (a = , b = , out = );
Not (in=a, out=nota);
Not (in=b, out=notb); Xor (a = , b = , out = );
And (a=a, b=notb, out=aAndNotb); ...
And (a=nota, b=b, out=notaAndb);
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 31
Design: Implementation
a
a out
b
And
in out aAndNotb
Not
notb
a
Or out
b
out
Not nota
in out
notaAndb
a
out
And
b
b
/** out = (a And Not(b)) Or (Not(a) And b)) */ /** Chips set (APIs): */
CHIP Xor { ...
IN a, b; Not (in = , out = );
OUT out;
And (a = , b = , out = );
PARTS:
Or (a = , b = , out = );
Not (in=a, out=nota);
Not (in=b, out=notb); Xor (a = , b = , out = );
And (a=a, b=notb, out=aAndNotb); ...
And (a=nota, b=b, out=notaAndb);
}
Question: What is the missing HDL line?
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 32
Design: Implementation
a
a out
b
And
in out aAndNotb
Not
notb
a
Or out
b
out
Not nota
in out
notaAndb
a
out
And
b
b
/** out = (a And Not(b)) Or (Not(a) And b)) */ /** Chips set (APIs): */
CHIP Xor { ...
IN a, b; Not (in = , out = );
OUT out;
And (a = , b = , out = );
PARTS:
Or (a = , b = , out = );
Not (in=a, out=nota);
Not (in=b, out=notb); Xor (a = , b = , out = );
And (a=a, b=notb, out=aAndNotb); ...
And (a=nota, b=b, out=notaAndb);
Or (a=aAndNotb, b=notaAndb, out=out);
}
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 33
Interface / Implementation
a
a out
b
And
in out aAndNotb
Not
notb
a
Or out
b
out
Not nota
in out
notaAndb
a
out
And
b
b
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 34
Hardware description languages
Observations
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 35
Hardware description languages
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 36
Chapter 1: Boolean logic
• Multi-bit buses
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 37
Hardware simulation (in a nutshell)
CHIP Xor {
IN a, b;
OUT out;
HDL code hardware
PARTS: simulator
Not (in=a, out=nota);
Not (in=b, out=notb);
And (a=a, b=notb, out=aAndNotb);
load
And (a=nota, b=b, out=notaAndb);
Or (a=aAndNotb, b=notaAndb, out=out);
}
Simulation options
• Interactive
• Script-based.
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 38
Interactive simulation
1. Load an
HDL program
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 39
Interactive simulation
3. evaluate the
1. Load an
chip logic
HDL program
4. inspect
output pins
2. manipulate
input pins
5. inspect
internal pins
HDL code
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 40
Hardware simulation in a nutshell
CHIP Xor {
IN a, b;
OUT out;
HDL code
PARTS:
Not (in=a, out=nota);
load
Not (in=b, out=notb);
And (a=a, b=notb, out=aAndNotb);
And (a=nota, b=b, out=notaAndb); hardware
load Xor.hdl,
Or (a=aAndNotb, test script
b=notaAndb, out=out);
simulator
} output-file And.out,
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;
Simulation options
• Interactive
• Script-based.
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 41
Script-based simulation
Xor.hdl Xor.tst
CHIP Xor {
load Xor.hdl;
IN a, b;
OUT out; set a 0, set b 0, eval;
set a 0, set b 1, eval;
PARTS: set a 1, set b 0, eval;
Not (in=a, out=nota); set a 1, set b 1, eval;
Not (in=b, out=notb);
And (a=a, b=notb, out=aAndNotb);
And (a=nota, b=b, out=notaAndb);
Or (a=aAndNotb, b=notaAndb, out=out);
test script = sequence of
}
commands to the simulator
Benefits:
• “Automatic” testing
• Replicable testing.
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 42
Script-based simulation, with an output file
Xor.hdl Xor.tst
CHIP Xor {
IN a, b;
load Xor.hdl,
tested
OUT out; output-file Xor.out,
chip output-list a b out;
PARTS: set a 0, set b 0, eval, output;
Not (in=a, out=nota); set a 0, set b 1, eval, output;
Not (in=b, out=notb);
set a 1, set b 0, eval, output;
And (a=a, b=notb, out=aAndNotb);
set a 1, set b 1, eval, output;
And (a=nota, b=b, out=notaAndb);
Or (a=aAndNotb, b=notaAndb, out=out);
}
Xor.out test
script
The logic of a typical test script | a | b |out|
• Initialize: | 0 | 0 | 0 |
| 0 | 1 | 1 |
q Loads an HDL file
| 1 | 0 | 1 |
q Creates an empty output file | 1 | 1 | 0 |
q Lists the names of the pins whose
values will be written to the output file Output File, created
by the test script,
• Repeat:
as a side-effect of the
q Set (inputs) – eval (chip logic) – output (print) simulation process
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 43
Script-based simulation
1. Load a
test script
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 44
Script-based simulation
2. run
1. Load a
the script
test script
test
script
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 45
Script-based simulation
inspect the
2. run output file
1. Load a
the script
test script
test
script
Xor.out
| a | b |out|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
HDL | 1 | 1 | 0 |
code
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 46
Script-based simulation
Xor.hdl Xor.tst
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 47
Script-based simulation, with a compare file
Xor.hdl Xor.tst
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 48
Script-based simulation, with a compare file
Xor.hdl Xor.tst
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 49
Chapter 1: Boolean logic
• Multi-bit buses
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 50
Multi-bit bus
• Sometimes we wish to manipulate a sequence of bits as a single entity
• Such a multi-bit entity is termed “bus”
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1 0 0 0 1 0 0 0 1 1 0 1 1 1 0 1
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 51
Working with buses: Example
/* Adds two 16-bit values. */ 16
CHIP Adder { a 16
16-bit
IN a[16], b[16]; out
16 adder
OUT out[16]; b
PARTS:
...
}
/* Adds three 16-bit inputs. */
15 ... 1 0 CHIP Adder3Way {
a: 1 ... 1 1 IN a[16], b[16], c[16];
OUT out[16];
b: 0 ... 1 0
PARTS:
c: 0 ... 0 1 Adder(a= , b= , out= );
Adder(a= , b= , out= );
out: 1 ... 1 0 }
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 52
Working with buses: Example
/* Adds two 16-bit values. */ 16
CHIP Adder { a 16
16-bit
IN a[16], b[16]; out
16 adder
OUT out[16]; b
PARTS:
...
}
/* Adds three 16-bit inputs. */
15 ... 1 0 CHIP Adder3Way {
IN a[16], b[16], c[16];
n-bit value (bus) can be
a: 1 ... 1 1
OUT out[16]; treated as a single entity
b: 0 ... 1 0
PARTS:
c: 0 ... 0 1 Adder(a=a , b=b, out=ab); Creates an internal
Adder(a=ab, b=c, out=out); bus pin (ab)
out: 1 ... 1 0 }
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 53
Working with individual bits within buses
/* Returns 1 if a==1 and b==1,
0 otherwise. */
CHIP And {
IN a, b; /* 4-way And: Ands 4 bits. */
OUT out; CHIP And4Way {
... IN a[4];
} OUT out;
3 2 1 0 PARTS:
And(a= , b= , out= );
a: 0 1 1 1
And(a= , b= , out= );
And(a= , b= , out= );
out: 0
}
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 54
Working with individual bits within buses
/* Returns 1 if a==1 and b==1,
0 otherwise. */
CHIP And {
IN a, b; /* 4-way And: Ands 4 bits. */
OUT out; CHIP And4Way {
... IN a[4]; Input bus pins can
} OUT out; be subscripted.
3 2 1 0 PARTS:
And(a=a[0], b=a[1], out=and01);
a: 0 1 1 1
And(a=and01, b=a[2], out=and012);
And(a=and012, b=a[3], out=out);
out: 0
}
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 55
Working with individual bits within buses
/* Returns 1 if a==1 and b==1,
0 otherwise. */
CHIP And {
IN a, b; /* 4-way And: Ands 4 bits. */
OUT out; CHIP And4Way {
... IN a[4]; Input bus pins can
} OUT out; be subscripted.
3 2 1 0 PARTS:
And(a=a[0], b=a[1], out=and01);
a: 0 1 1 1
And(a=and01, b=a[2], out=and012);
And(a=and012, b=a[3], out=out);
out: 0
}
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 56
Working with individual bits within buses
/* Returns 1 if a==1 and b==1,
0 otherwise. */
CHIP And {
IN a, b; /* 4-way And: Ands 4 bits. */
OUT out; CHIP And4Way {
... IN a[4]; Input bus pins can
} OUT out; be subscripted.
3 2 1 0 PARTS:
And(a=a[0], b=a[1], out=and01);
a: 0 1 1 1
And(a=and01, b=a[2], out=and012);
And(a=and012, b=a[3], out=out);
out: 0
}
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 57
Chapter 1: Boolean logic
• Multi-bit buses
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 58
Chapter 1: Boolean logic
• Multi-bit buses
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 59
Built-in chips
We provide built-in versions of the chips built in this course (in tools/builtInChips).
For example:
Xor.hdl Xor.hdl
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 60
Hardware construction projects
Key players:
Architect
q Decides which chips are needed
Developers
q Build / test the chips
In Nand to Tetris:
The architect is the course team; the developers are the students
For each chip, the architect supplies:
q Built-in chip
q Chip API (skeletal HDL program = stub file)
q Test script
q Compare file
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 61
The developer’s view (of, say, a Xor gate)
Xor.hdl Xor.tst
compare Xor.cmp
These files specify:
file
• The chip interface (.hdl) | a | b |out|
| 0 | 0 | 0 |
• How the chip is supposed to behave (.cmp) | 0 | 1 | 1 |
| 1 | 0 | 1 |
• How to test the chip (.tst) | 1 | 1 | 0 |
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 62
The developer’s view (of, say, a Xor gate)
Xor.hdl Xor.tst
compare Xor.cmp
These files specify:
file
• The chip interface (.hdl) | a | b |out|
| 0 | 0 | 0 |
• How the chip is supposed to behave (.cmp) | 0 | 1 | 1 |
| 1 | 0 | 1 |
• How to test the chip (.tst) | 1 | 1 | 0 |
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 63
Project 1
abstraction
machine
language
assembler p6
abstraction p4 p5
hardware platform
building a p2 p3
computer abstraction
computer building p1
ALU, RAM abstraction
chips building
elementary d
logic gates gates Nan
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 64
Project 1
abstraction
machine
language
assembler p6
abstraction p4 p5
hardware platform
building a p2 p3
computer abstraction
computer building p1
ALU, RAM abstraction
chips building
elementary d
logic gates gates Nan
Project 1
Build 15 elementary logic gates
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 65
Project 1
Given: Nand
Goal: Build the following gates:
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 66
Project 1
Given: Nand
Goal: Build the following gates:
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 67
Multiplexor / Demultiplexor
if (sel == 0) if (sel == 0)
out = a {a, b} = {in, 0}
else else
out = b {a, b} = {0, in}
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 68
Example 1: Using Mux logic to build a programmable gate
a b sel out
0 0 0 0
0 1 0 0
When sel == 0
1 0 0 0 the gate acts like an And gate
1 1 0 1
0 0 1 1
if (sel == 0)
0 1 1 1 When sel == 1
out = a And b
1 0 1 1 the gate acts like an Or gate
else
out = a Or b 1 1 1 0
Mux.hdl
CHIP AndMuxOr {
IN a, b, sel;
OUT out;
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 69
Example 1: Using Mux logic to build a programmable gate
a b sel out
0 0 0 0
0 1 0 0
When sel == 0
1 0 0 0 the gate acts like an And gate
1 1 0 1
0 0 1 1
if (sel == 0)
0 1 1 1 When sel == 1
out = a And b
1 0 1 1 the gate acts like an Or gate
else
out = a Or b 1 1 1 0
Mux.hdl
CHIP AndMuxOr {
IN a, b, sel;
OUT out;
PARTS:
And (a=a, b=b, out=andOut);
Or (a=a, b=b, out=orOut);
Mux (a=andOut, b=orOut, sel=sel, out=out);
}
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 70
Example 2: Using Mux logic to build an interleaved channel
source destination
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 71
Multiplexor
a b sel out sel out
0 0 0 0 0 a abbreviated
0 1 0 0 1 b truth table
1 0 0 1
1 1 0 1
if (sel == 0) 0 0 1 0
out = a 0 1 1 1
else 1 0 1 0
out = b 1 1 1 1
Mux.hdl
CHIP Mux {
IN a, b, sel; Implementation tip
OUT out;
Can be implemented from And, Or, Not.
PARTS:
// Put your code here:
}
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 72
Demultiplexor
in sel a b
if (sel == 0) 0 0 0 0
{a, b} = {in, 0} 0 1 0 0
else 1 0 1 0
{a, b} = {0, in} 1 1 0 1
DMux.hdl
CHIP DMux {
IN in, sel;
OUT a, b; Implementation tip
PARTS: Simple truth table, simple implementation.
// Put your code here:
}
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 73
Project 1
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 74
Project 1
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 75
And16
Example:
a = 1 0 1 0 1 0 1 1 0 1 0 1 1 1 0 0
b = 0 0 1 0 1 1 0 1 0 0 1 0 1 0 1 0
out = 0 0 1 0 1 0 0 1 0 0 0 0 1 0 0 0
CHIP And16 {
IN a[16], b[16];
Implementation tip
OUT out[16];
A straightforward 16-bit extension
PARTS: of the elementary And gate
// Put your code here:
(See the HDL documentation
}
about working with multi-bit buses).
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 76
Project 1
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 77
Project 1
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 78
16-bit, 4-way multiplexor
Mux4Way16.hdl
CHIP Mux4Way16 {
IN a[16], b[16], c[16], d[16],
sel[2];
OUT out[16]; Implementation tip:
PARTS: Can be built from Mux16 gates.
// Put your code here:
}
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 79
Chapter 1: Boolean logic
• Multi-bit buses
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 80
Project 1
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 81
Files
Xor.cmp
a
Xor out a b out
For every chip built in the course
b 0 0 0
0 1 1 (using Xor as an example), we supply
if ((a == 0 and b == 1) or 1 0 1
(a == 1 and b == 0)) 1 1 0
these three files
sets out = 1
else
sets out = 0
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 82
Files
Xor.cmp
a The contract:
Xor out a b out
b 0 0 0 When running your Xor.hdl on the
0 1 1
1 0 1
supplied Xor.tst,
if ((a == 0 and b == 1) or
(a == 1 and b == 0)) 1 1 0 your Xor.out should be the same as
sets out = 1
else the supplied Xor.cmp
sets out = 0
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 83
Chip interfaces
a
a out
b
And
in out aAndNotb
Not
notb
a
Or out
b
out
Not nota
in out
notaAndb
a
out
And
b
b
CHIP Xor {
IN a, b;
OUT out; If I want to use some chip-parts,
PARTS: how do I figure out their signatures?
Not (in= , out= );
Not (in= , out= );
And (a= , b=, out=);
And (a= , b=, out=);
Or (a= , b=, out=);
}
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 84
Chip interfaces: Hack chip set API
Open the Hack chip set API in a window, and copy-paste
Add16 (a= ,b= ,out= );
chip signatures into your HDL code, as needed
ALU (x= ,y= ,zx= ,nx= ,zy= ,ny= ,f= ,no= ,out= ,zr= ,ng= );
And16 (a= ,b= ,out= );
And (a= ,b= ,out= ); Mux8Way (a= ,b= ,c= ,d= ,e= ,f= ,g= ,h= ,sel= ,out= );
Aregister (in= ,load= ,out= ); Mux (a= ,b= ,sel= ,out= );
Bit (in= ,load= ,out= ); Nand (a= ,b= ,out= );
Not16,pc=
CPU (inM= ,instruction= ,reset= ,outM= ,writeM= ,addressM= (in=);,out= );
DFF (in= ,out= ); Not (in= ,out= );
DMux4Way (in= ,sel= ,a= ,b= ,c= ,d= ); Or16 (a= ,b= ,out= );
DMux8Way (in= ,sel= ,a= ,b= ,c= ,d= ,e= ,f= ,g= ,h= ); Or8Way (in= ,out= );
DMux (in= ,sel= ,a= ,b= ); Or (a= ,b= ,out= );
Dregister (in= ,load= ,out= ); PC (in= ,load= ,inc= ,reset= ,out= );
FullAdder (a= ,b= ,c= ,sum= ,carry= ); PCLoadLogic (cinstr= ,j1= ,j2= ,j3= ,load= ,inc= );
HalfAdder (a= ,b= ,sum= , carry= ); RAM16K (in= ,load= ,address= ,out= );
Inc16 (in= ,out= ); RAM4K (in= ,load= ,address= ,out= );
Keyboard (out= ); RAM512 (in= ,load= ,address= ,out= );
Memory (in= ,load= ,address= ,out= ); RAM64 (in= ,load= ,address= ,out= );
Mux16 (a= ,b= ,sel= ,out= ); RAM8 (in= ,load= ,address= ,out= );
Mux4Way16 (a= ,b= ,c= ,d= ,sel= ,out= ); Register (in= ,load= ,out= );
ROM32K (address= ,out= );
Mux8Way16 (a= ,b= ,c= ,d= ,e= ,f= ,g= ,h= ,sel= ,out= );
Screen (in= ,load= ,address= ,out= );
Xor (a= ,b= ,out= );
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 85
Built-in chips
CHIP Foo {
IN ...;
OUT ...; Q: How can I play with / use a chip-part before implementing it?
PARTS: A: The simulator features built-in chip implementations
...
Bar(...)
...
}
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 86
Project 1
Tools
• Text editor (for completing the given .hdl stub-files)
• Hardware simulator: nand2tetris/tools (on your PC)
Guides
• Hardware simulator tutorial
• HDL guide
• Hack chip set API
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 87
Best practice advice
• Implement the chips in the order in which they appear in the project guidelines
• If you don’t implement some chips, you can still use them as chip-parts in other
chips (use their built-in implementations)
• You can invent additional, “helper chips”; However, this is not necessary.
Implement and use only the chips that the architects (we) specified
• In each chip implementation, strive to use as few chip-parts as possible
• When defining 16-bit chips, the same chip-parts may appear many times.
That’s fine, use copy-paste-edit.
That’s It!
Go Do Project 1!
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 88
What’s next?
abstraction
machine
language
assembler p6
abstraction p4 p5
hardware platform
building a p2 p3
computer abstraction
computer building p1
ALU, RAM abstraction
chips building
elementary d
logic gates gates Nan
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 89
What’s next?
abstraction
machine
language
Next:
We’ll build chips that do arithmetic,
assembler p6
ending up with an ALU,
using the chips built in project 1
abstraction p4 p5
hardware platform
building a p2 p3
computer abstraction
computer building p1
ALU, RAM abstraction
chips building
elementary d
logic gates gates Nan
Nand to Tetris / www.nand2tetris.org / Chapter 1 / Copyright © Noam Nisan and Shimon Schocken Slide 90