Hardware Simulator Tutorial: The Elements of Computing Systems
Hardware Simulator Tutorial: The Elements of Computing Systems
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 book’s
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.
HW Simulator Tutorial www.idc.ac.il/tecs Tutorial Index Slide 7/49
Chip Definition (.hdl file)
/**
/** 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, b=w2,
Or(a=w1, 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.idc.ac.il/tecs 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,
output; Can
Simulation
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.idc.ac.il/tecs Tutorial Index Slide 24/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
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)
chip’s output pin internal state of
to the value of the chip, but note
the chip’s that the chip’s
A built-in, internal state. output pin is not
clocked yet effected.
chip
(RAM8) is
loaded
Part V:
GUI-Empowered
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 GUI’s behaviors are then effected by the
GUI’s 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 1’s 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.idc.ac.il/tecs Tutorial Index Slide 41/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.
Simulators
(HardwareSimulator, CPUEmulator, VMEmulator):
Used to build hardware platforms and
This tutorial is execute programs;
about the
CPU emulator. Supplied by us.
I. Basic Platform
V. Debugging
Part I:
Basic Platform
Travel Advice:
This tutorial includes some examples
of programs written in the Hack
machine language (chapter 4).
There is no need however to
understand either the language or the
programs in order to learn how to use
the CPU emulator.
Rather, it is only important to grasp the
general logic of these programs,
as explained (when relevant)
in the tutorial.
screen
data
memory
keyboard
instruction enabler
memory
registers ALU
Instruction memory
(32K): Holds a machine
language program
Next instruction
is highlighted
M (=RAM[A])
Part II:
I/O Devices
Simulated keyboard:
One click on this button causes the
CPU emulator to intercept all the
keys subsequently pressed on the
real computer’s keyboard; another
click disengages the real keyboard
from the emulator.
3. Watch here:
Keyboard memory
map
(a single 16-bit
memory location)
Part III:
Interactive
Simulation
Navigate to a
directory and select
a .hack or .asm file.
Can switch
from binary to
symbolic
representation
4. Watch
here
2. Click the 1. Enter a
“run” button. number, 3. To speed up
say 50. execution,
use the speed
control slider
Controls execution
(and animation)
speed.
Animation control:
Program flow (default): highlights the
current instruction in the instruction memory
and the currently selected RAM location
Program & data flow: animates all
program and data flow in the computer
No animation: disables all animation
Usage tip: To execute any non-trivial program
The simulator can quickly, select no animation.
animate both program
flow and data flow
Part IV:
Script-Based
Simulation
Test scripts:
Are written in a Test Description Language (described in Appendix B)
Can cause the emulator to do anything that can be done interactively,
and quite a few things that cannot be done interactively.
test script
tested program
//
Writing values into registers
// Test
Test 1:
1: max(15,32)
max(15,32)
set RAM[0] 15,
set RAM[0] 15, Executing the next command (“ticktack”)
set
set RAM[1]
RAM[1] 32;
repeat 14 {
32; Looping (“repeat”)
repeat 14 {
ticktock;
ticktock; And more (see Appendix B).
}}
output;
output; // // to
to the
the Max.out
Max.out file
file Notes:
Load a
script
Script = a series of
simulation steps, each
ending with a semicolon;
Reset
Important point: Whenever an assembly
the script
program (.asm file) is loaded into the
emulator, the program is assembled on the
Pause
fly into the
machine language code, and this is
simulation
the code that actually gets loaded. In the
process, all comments and white space are
Execute step after from the code, and all symbols
removed
step repeatedly
resolve to the numbers that they stand for.
Part V:
Debugging
Breakpoints:
A breakpoint is a pair <variable, value> where variable is one of
{A, D, PC, RAM[i], time} and i is between 0 and 32K.
Breakpoints can be declared either interactively, or via script commands.
For each declared breakpoint, when the variable reaches the value, the
emulator pauses the program’s execution with a proper message.
2. Previously-
declared
breakpoints
1. Open the
breakpoints
panel
3. Add, delete,
or update
breakpoints
1. New
breakpoint
2. Run the
program
Simulators
(HardwareSimulator, CPUEmulator, VMEmulator):
This tutorial is Used to build hardware platforms and
about the execute programs;
assembler
Supplied by us.
Part I:
Assembly
Programming
at a Glance
Part II:
Learn how to invoke the
supplied assembler from
the OS shell level.
(the assembler that you have
to write in project 6 should
have the same GUI and
behavior)
Display the
assembly source
code (contents of
the .asm text file)
Source
code is
shown
Part III:
Navigate to a
directory and select
an .asm file.
Immediate
translation
(no animation)
Pause the
translation
Translate the
entire program
Translate
line-by-line
2. The
1. Click an corresponding
assembly translated code
command is highlighted
Saves the
translated code
in a .hack file
The “save” operation is
enabled only if the
translation was error-free;
Otherwise, the translation
stops with an error
message.
1. Load a
compare file
2. Select a compare
(.hack) file
2. Translate the
program (any
translation mode
can be used)
1. Compare file is
shown
The translation of
the highlighted line
does not match the
corresponding line
in the compare file.
Simulators
(HardwareSimulator, CPUEmulator, VMEmulator):
Used to build hardware platforms and
execute programs;
Supplied by us.
I. Getting Started
III. Debugging
Part I:
Getting Started
VM program
(In this
example: Screen:
Pong code (In this example:
+ OS code) Pong game action)
Virtual
memory
The VM emulator serves three purposes:
segments Keyboard
Running programs enabler
Debugging programs
Visualizing the VM’s anatomy
The emulator’s GUI is rather crowded, but each
Working
GUIstack:
element has an important debugging role.
Topmost part of the
global stack, as seen
by the VM program Not Part of the VM!
(displayed in the VM emulator
for reference purposes)
Call stack:
Hierarchy of all the
Global stack: Host RAM:
functions that are
Function frames Stores the global
currently running
+ working stack stack, heap, etc.
Impact of first
13 “vmsteps”
Won’t work!
Why? Because Pong is a
multi-file program, and ALL
these files must be loaded.
Solution: navigate back to the
directory level, and load it.
Part II:
Virtual Memory
Segments
Part II:
Using Scripts
set
set sp
sp 256,
256, Typical memory
Next set
set local
local 300,
300, segments initialization
simulation set
set argument
argument 400,
400, commands
step set
set this
this 3000,
3000,
set that 3010;
set that 3010;
Repeated repeat
repeat 25
25 {{
simulation vmstep,
vmstep, Typical execution loop
step output;
output;
}}
Navigate to a
directory and select
a .tst file.
Script = a series of
simulation steps, each
ending with a semicolon;
Reset
the script
Pause the
simulation
A loop that
Impact after
executes the
first 10
loaded VM
commands
program
are executed
Part III:
Debugging
Speed control
(of both execution
and animation)
Animation control:
source
Program flow (default): highlights the next
transit
VM command to be executed;
Program & data flow: highlights the next
VM command and animates data flow;
destn. No animation: disables all animation
Usage tip: To execute any non-trivial program
quickly, select no animation.
data flow animation related to
the last VM command (in this
example: push argument 0)
local, argument, this, that: Base addresses of these segments in the host RAM
Breakpoints:
A breakpoint is a pair <variable, value> where variable is one of the labels listed above
(e.g. local[5], argument, line, etc.) and value is a valid value
For each declared breakpoint, when the variable reaches the value, the emulator
pauses the program’s execution with a proper message.
2. Previously-
declared
breakpoints
repeat
repeat 33 {{
• Write variable values (output)
vmStep,
}}
vmStep, • Repeated execution (while)
output;
output; • Set/clear Breakpoints
while
while sp
sp << 260
260 {{ • Etc. (see Appendix B.)
vmstep;
vmstep;
}}
output;
output;
clear-breakpoints;
clear-breakpoints;
//
// Etc.
Etc.
(Ken Thompson,
1983 Turing Award lecture)