Hardware Simulator Tutorial PDF
Hardware Simulator Tutorial PDF
Simulators
(HardwareSimulator, CPUEmulator, VMEmulator):
Used to build hardware platforms and
execute programs;
This tutorial is
about the Supplied by us.
hardware
simulator. Translators (Assembler, JackCompiler):
Used to translate from high-level to low-level;
Developed by the students, using the books
specs; Executable solutions supplied by us.
Other
Bin: simulators and translators software;
I. Getting started
V. GUI-empowered chips
Part I:
Getting Started
/**
/** Exclusive-or
Exclusive-or gate.
gate. out
out == aa xor
xor bb */
*/
chip CHIP
CHIP Xor
Xor {{
interface IN
IN a,
a, b;
b;
OUT
OUT out;
out;
//
// Implementation
Implementation missing.
missing.
}}
Chip interface:
Name of the chip
Names of its input and output pins
Documentation of the intended chip operation
Typically supplied by the chip architect; similar to an API, or a contract.
/**
/** Exclusive-or
Exclusive-or gate.
gate. out
out == aa xor
xor bb */
*/
chip CHIP
CHIP Xor
Xor {{
interface IN
IN a,
a, b;
b;
OUT
OUT out;
out;
PARTS:
PARTS:
Not(in=a,
Not(in=a, out=nota);
out=nota);
chip
Not(in=b,
Not(in=b, out=notb);
out=notb);
implementation
And(a=a,
And(a=a, b=notb,
b=notb, out=w1);
out=w1);
And(a=nota, b=b,
And(a=nota, b=b, out=w2);
out=w2);
Or(a=w1,
Or(a=w1, b=w2,
b=w2, out=out);
out=out);
}}
Any given chip can be implemented in several different ways. This particular
implementation is based on: Xor(a,b) = Or(And(a,Not(b)), And(b,Not(a)))
Not, And, Or: Internal parts (previously built chips), invoked by the HDL
programmer
nota, notb, w1, w2: internal pins, created and named by the HDL programmer;
used to connect internal parts.
HW Simulator Tutorial www.nand2tetris.org Tutorial Index Slide 8/49
Loading a Chip
Navigate to a
directory and select
an .hdl file.
Part II:
Test Scripts
set
set aa 0,
0, Generated Are written in a simple language described
set
set bb 1,
1,
output file in Appendix B of the book
eval, (Xor.out)
eval, Simulation
output; Can createstep
an output file that records the
output;
Etc.
Etc.
results of the chip test
|| aa || bb || out
out ||
|| 00 || 00 || 00 || If the script specifies a compare file, the
|| 00 || 11 || 11 || simulator will compare the .out file to
|| 11 || 00 || 11 || the .cmp file, line by line.
|| 11 || 11 || 00 ||
Controls
the script
execution
speed Script =
series of
simulation
steps, each
Resets
ending with
the script
a semicolon.
Pauses the
script execution
Multi-step execution,
until a pause
Script
exec-
Typical init code:
ution
1. Loads a chip definition (.hdl) file
flow
2. Initializes an output (.out) file
3. Specifies a compare (.cmp) file
4. Declares an output line format.
Script
exec-
ution
ends
Observation:
This output file
looks like a Xor
truth table
Part III:
Built-in Chips
Standard interface.
Built-in implementation.
When any HDL file is loaded, the simulator parses its definition. For each internal
chip Xxx(...) mentioned in the PARTS section, the simulator looks for an Xxx.hdl
file in the same directory (e.g. Not.hdl, And.hdl, and Or.hdl in this example).
If Xxx.hdl is found in the current directory (e.g. if it was also written by the user), the
simulator uses its HDL logic in the evaluation of the overall chip.
If Xxx.hdl is not found in the current directory, the simulator attempts to invoke the
file tools/builtIn/Xxx.hdl instead.
And since tools/builtIn includes executable versions of all the chips mentioned in
the book, it is possible to build and test any of these chips before first building their
lower-level parts.
HW Simulator Tutorial www.nand2tetris.org Tutorial Index Slide 26/49
Hardware Simulation Tutorial
Part IV:
Clocked Chips
(Sequential Logic)
During a tick-tock, the internal states of all the clocked chips are allowed to change,
but their outputs are latched
At the beginning of the next tick, the outputs of all the clocked chips in the
architecture commit to the new values
1. User: enters
some input
values and
clicks the clock
icon once (tick)
A built-in,
clocked
chip
(RAM8) is
loaded
1. User: enters
some input
values and
2. Simulator:
clicks the clock
changes the
icon once (tick)
internal state of
the chip, but note
that the chips
A built-in, output pin is not
clocked yet effected.
chip
(RAM8) is
loaded
3. User: clicks
the clock icon
again (tock)
1. User: enters
some input
values and
2. Simulator:
clicks the clock
changes the
icon once (tick)
internal state of
the chip, but note
that the chips
A built-in, output pin is not
clocked yet effected.
chip
(RAM8) is
loaded
3. User: clicks
the clock icon
again (tock)
1. User: enters
some input
values and
4. Simulator: 2. Simulator:
clicks the clock
commits the changes the
icon once (tick)
chips output pin internal state of
to the value of the chip, but note
the chips that the chips
A built-in, internal state. output pin is not
clocked yet effected.
chip
(RAM8) is
loaded
Part V:
GUI-Empowered
chips
1. A chip whose
parts include Note: the signature of the internal part does
built-in chips not reveal if the part is implemented by a
was loaded into built-in chip or by another chip built by the
the simulator user. Thus in this example you have to
(ignore the chip believe us that all the parts of this loaded chip
logic for now) are built-in chips.
2. If the loaded chip or For each GUI-empowered built-in chip that appears
some of its parts have in the definition of the loaded chip, the simulator
GUI side-effects, the does its best to putGUI of the
the chip GUIbuilt-in
in this area.
simulator displays the Screen.hdl chip
The actual GUIs behaviors are then effected by the
GUIs here. Java classes that implement the built-in chips.
1. A chip whose
parts include GUI of the built-in
built-in chips Keyboard.hdl chip
was loaded into
the simulator
GUI of the built-in
(ignore the chip RAM16K.hdl chip
logic for now)
Effect: When the simulator evaluates this chip, it displays the GUI side-
effects of its built-in chip parts
Chip logic: The only purpose of this demo chip is to force the simulator to
show the GUI of some built-in chips. Other than that, the chip logic is
meaningless: it simultaneously feeds the 16-bit data input (in) into the
RAM16K and the Screen chips, and it does nothing with the keyboard.
3. 16 black
2. User: pixels are
runs the drawn
clock beginning in
row = 156
col = 320
1. User enters:
in = 1
Explanation: According to the specification of
(=16 1s in binary)
the computer architecture3. The chipinlogic
described the
address = 5012 routesscreen
book, the pixels of the physical the inarevalue
load = 1 continuously refreshed fromsimultaneously
an 8K RAM- into
the Screen
resident memory map implemented bychip
the and
Screen.hdl chip. The exact the RAM16K
mappingchip
between this memory chip and the actual
pixels is specified in Chapter 5. The refresh
process is carried out by the simulator.
Part VI:
Debugging tools
Time: the number of time-units (clock-cycles) that elapsed since the script
started running is stored in the variable time
Pins: the values of all the input, output, and internal pins of the simulated chip
are accessible as variables, using the names of the pins in the HDL code
GUI elements: the values stored in the states of GUI-empowered built-in chips
can be accessed via variables. For example, the value of register 3 of the
RAM8 chip can be accessed via RAM8[3].
All these variables can be used in scripts and breakpoints, for debugging.
2. Previously-
1. Open the declared
breakpoints breakpoints
panel
3. To update an existing
breakpoint, double-click it
Part VII:
The Hack
Hardware Platform
Hang
Maze
Man
Grades
Pong
Stats
These programs (and many more) were written in the Jack programming language,
running in the Jack OS environment over the Hack hardware platform. The hardware
platform is built in chapters 1-5, and the software hierarchy in chapters 6-12.
HW Simulator Tutorial www.nand2tetris.org Tutorial Index Slide 47/49
The Hack Chip-Set and Hardware Platform
Elementary logic gates Combinational chips Sequential chips Computer Architecture
(Project 1): (Project 2): (Project 3): (Project 5):
Nand (primitive) HalfAdder DFF (primitive) Memory
Not FullAdder Bit CPU
And
Add16 Register Computer
Or
Inc16 RAM8
Xor
ALU RAM64
Mux
RAM512
Dmux
RAM4K
Not16
And16 RAM16K
Or16 PC
Mux16
Or8Way Most of these chips are generic, meaning that they can be
used in the construction of many different computers.
Mux4Way16
Mux8Way16 The Hack chip-set and hardware platform can be built using
the hardware simulator, starting with primitive Nand.hdl and
DMux4Way
DFF.hdl gates and culminating in the Computer.hdl chip.
DMux8Way
This construction is described in chapters 1,2,3,5 of the book,
and carried out in the respective projects.