CPU Emulator Tutorial
CPU Emulator Tutorial
Simulators
(HardwareSimulator, CPUEmulator, VMEmulator):
Used to build hardware platforms and
This tutorial is execute programs;
about the
Supplied by us.
CPU emulator.
I. Basic Platform
IV.
Script-based simulation
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
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
//
As it turns out, the Max program requires 14
// Test
Test 2:
2: max(47,22)
max(47,22)
set cycles to complete its execution
set PC 0, // Reset
PC 0, // Reset prog.
prog. counter
counter
set RAM[0] 47,
set RAM[0] 47,
set
All relevant files (.asm,.tst,.cmp) must
set RAM[1]
RAM[1] 22;22; be present in the same directory.
repeat 14
repeat 14 { {
ticktock;
ticktock;
}} Output
output;
output; || RAM[0] || RAM[1]
RAM[1] || RAM[2]
|RAM[0]
|
RAM[2]
//
// test
test 3:
3: max(12,12)
max(12,12) || 15
15 || 32
32 || 32
32
||
// Etc.
// Etc. || 47
47 || 22
22 || 47
47 ||
CPU Emulator Tutorial, www.nand2tetris.org Tutorial Index Slide 30/40
Using test scripts
Load a
script
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
removed from the code, and all symbols
after step resolve to the numbers that they stand for.
repeatedly
Execute the next
simulation step
View options:
Script: Shows the current script;
Output: Shows the generated output file;
Compare: Shows the given comparison file;
Screen: Shows the simulated screen.
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