Hardware Simulator Tutorial Overview
Hardware Simulator Tutorial Overview
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 [Link]/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 [Link]/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, ([Link])
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 [Link]
file in the same directory (e.g. [Link], [Link], and [Link] in this example).
If [Link] 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 [Link] is not found in the current directory, the simulator attempts to invoke the
file tools/builtIn/[Link] 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 [Link]/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 [Link] 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 [Link] chip
was loaded into
the simulator
GUI of the built-in
(ignore the chip [Link] 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
[Link] 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 [Link]/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 [Link] and
DMux4Way
[Link] gates and culminating in the [Link] 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 [Link]
[Link] 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)
A hardware simulator is employed in chip design and verification by enabling the user to load HDL files, manipulate input values, and observe the resultant output and internal processes in real-time. It supports interactive testing to explore chip logic, allows for observing internal calculations and part interfaces, and enables script-driven testing to automate comparison of output against expected results . The simulator also facilitates the exploration of clocked chips' behavior by controlling the clock cycles and examining GUI and non-GUI operations of the chips under testing .
The educational value of constructing a complete computer system from hardware to software lies in its comprehensive and integrative learning approach, which deepens understanding across the entire spectrum of computer engineering. This methodology, detailed in "The Elements of Computing Systems," actively engages students in building both a simple hardware platform and a software hierarchy, encompassing assemblers, virtual machines, compilers, and operating systems. It is self-contained and develops practical skills and conceptual understanding through the project's progression, requiring the application of theoretical knowledge in a hands-on manner . Such a journey fosters critical thinking and problem-solving abilities, creating a holistic grasp of computer systems' functions and interdependencies .
A virtual machine assembler functions as a crucial intermediary in the software hierarchy by translating high-level language code into machine-level instructions that can be executed by a computer. Within "The Elements of Computing Systems," the Jack Compiler generates virtual machine programs, which are then translated by the assembler into lower-level code that interacts with hardware. This process illustrates a layered approach to software development, where high-level logic is methodically converted into actionable commands, ensuring structured and efficient execution on hardware platforms .
Setting and using breakpoints in hardware simulation is significant for isolating and diagnosing specific issues within a program. A breakpoint allows the developer to pause execution when a certain condition is met, such as reaching a specific cycle count or when a variable has a particular value. This aids in scrutinizing the internal state of the program at critical junctures, allowing the developer to verify or refute hypotheses about the source of errors. This not only streamlines debugging but also enhances the understanding of how different parts of the program interact .
The concept of 'symbol versus meaning' applies to understanding computing systems by emphasizing the importance of understanding the underlying principles and functionalities rather than just their symbolic representations. Richard Feynman suggested that knowing the name of something in all languages does not equate to understanding it; true comprehension comes from observing and understanding its behavior and properties. This philosophical stance encourages a deep, functional understanding, which is crucial in computing systems where knowing the functionality behind symbolic representations is necessary for effective programming and hardware design .
GUI-empowered chips contribute to hardware simulation and user interaction by making it easier for the user to visually understand and debug the chip's behavior. GUI effects are displayed when the simulator evaluates chip parts that include built-in chips with GUI capabilities, such as RAM16K, Screen, and Keyboard. This allows users to observe the effect of inputs and system states in a more intuitive and comprehensive manner compared to non-GUI chips, thereby facilitating better user interaction and understanding of the chip logic .
The structure of simulation scripts enhances the testing process of chip logic by providing a systematic and repeatable framework for comparing expected and actual outputs under various scenarios. Scripts typically include steps to load a chip definition, initialize outputs, and specify comparison files, enabling automated verification against a predefined truth table or expected results . This formal scripting approach reduces manual errors and increases the reliability of testing by consistently applying the same testing conditions, facilitating both functional verification and performance evaluation of the chip logic .
Built-in chips play a critical role in the design and simulation of hardware by providing standard, pre-implemented functionality that aids in simulation speed and memory use efficiency. They are used to implement primitive gates like Nand and DFF, peripheral side-effect chips (e.g., I/O devices), and facilitate behavioral simulation before HDL construction . Built-in chips enhance usability by allowing the use of Java-implemented components and GUI effects, crucial for visual debugging and simulation .
The hardware description language (HDL) facilitates building complex chips like an XOR gate by allowing designers to define chip interfaces and internal logic using components such as NOT, AND, and OR gates. In the XOR gate example, the HDL file specifies the gate's input/output pins and implements the logic with parts created by the programmer, such as intermediate pins and connections between internal parts . This modular approach allows for multiple implementation strategies and aids in visualization, debugging, and testing of chip logic .
Debugging is considered an integral part of programming because it was recognized early in computer science history as essential to finding and fixing errors in code, a conclusion reached by individuals like Maurice Wilkes. The need for debugging stems from the complexity of developing correct and efficient programs, which are often filled with unanticipated errors . Debugging involves examining the behavior of code under various conditions, often using tools like breakpoints to identify where programs deviate from expected behavior, thus ensuring the reliability and accuracy of software systems .