Cpu Emulator PDF
Cpu Emulator PDF
Tutorial Index
Slide 1/40
Background
The Elements of Computing Systems evolves around
Tutorial Index
Slide 2/40
Simulators
(HardwareSimulator, CPUEmulator, VMEmulator):
This tutorial is
about the
CPU emulator.
Other
Bin: simulators and translators software;
builtIn: executable versions of all the logic
Tutorial Index
Slide 3/40
Tutorial Objective
Tutorial Index
Slide 4/40
Tutorial Index
Slide 5/40
I.
Basic Platform
Chapter 4:
Machine Language
Chapter 5:
Computer Architecture
Tutorial Index
Slide 6/40
Part I:
Basic Platform
Tutorial Index
Slide 7/40
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.
Tutorial Index
Slide 8/40
screen
data
memory
keyboard
enabler
instruction
memory
ALU
registers
Tutorial Index
Slide 9/40
Instruction memory
Instruction memory
(32K): Holds a machine
language program
Next instruction
is highlighted
Tutorial Index
Slide 10/40
Tutorial Index
Slide 11/40
Registers
Registers (all 16-bit):
D: Data register
A: Address register
M: Stands for the memory register
whose address is the current
value of the Address register
M (=RAM[A])
D
Tutorial Index
Slide 12/40
Arithmetic/Logic Unit
Arithmetic logic unit (ALU)
The ALU can compute various arithmetic
and logical functions (lets call them f) on
subsets of the three registers {M,A,D}
All ALU instructions are of the form
{M,A,D} = f ({M,A,D})
(e.g. M=M-1, MD=D+A , A=0, etc.)
Current
instruction
M (=RAM[A])
Tutorial Index
Slide 13/40
Part II:
I/O Devices
Tutorial Index
Slide 14/40
Simulated keyboard:
One click on this button causes the
CPU emulator to intercept all the
keys subsequently pressed on the
real computers keyboard; another
click disengages the real keyboard
from the emulator.
Tutorial Index
Slide 15/40
Tutorial Index
Slide 16/40
3. Watch here:
Keyboard memory
map
(a single 16-bit
memory location)
Tutorial Index
Slide 17/40
Keyboard memory
map
(a single 16-bit
memory location)
Tutorial Index
Slide 18/40
Part III:
Interactive
Simulation
Tutorial Index
Slide 19/40
Loading a program
Navigate to a
directory and select
a .hack or .asm file.
Tutorial Index
Slide 20/40
Loading a program
Can switch
from binary to
symbolic
representation
Tutorial Index
Slide 21/40
Running a program
4. Watch
here
2. Click the
run button.
1. Enter a
number,
say 50.
3. To speed up
execution,
use the speed
control slider
Tutorial Index
Slide 22/40
Program action:
Since RAM[0] happens to be 50,
the program draws a 16X50
rectangle. In this example the
user paused execution when
there are 14 more rows to draw.
Tutorial Index
Slide 23/40
Animation options
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
The simulator can
animate both program
flow and data flow
Tutorial Index
Slide 24/40
Part IV:
Script-Based
Simulation
Tutorial Index
Slide 25/40
Tutorial Index
Slide 26/40
test script
tested program
Tutorial Index
Slide 27/40
Example: Max.asm
Note: For now, it is not necessary to understand either the Hack
machine language or the Max program. It is only important to
grasp the programs logic. But if youre interested, we give a
language overview on the right.
//
// Computes
Computes M[2]=max(M[0],M[1])
M[2]=max(M[0],M[1]) where
where MM stands
stands for
for RAM
RAM
@0
@0
D=M
//
D=M
// DD == M[0]
M[0]
@1
@1
D=D-M
//
D=D-M
// DD == DD -- M[1]
M[1]
@FIRST_IS_GREATER
@FIRST_IS_GREATER
D;JGT
//
D;JGT
// If
If D>0
D>0 goto
goto FIRST_IS_GREATER
FIRST_IS_GREATER
@1
@1
D=M
//
D=M
// DD == M[1]
M[1]
@SECOND_IS_GREATER
@SECOND_IS_GREATER
0;JMP
//
0;JMP
// Goto
Goto SECOND_IS_GREATER
SECOND_IS_GREATER
(FIRST_IS_GREATER)
(FIRST_IS_GREATER)
@0
@0
D=M
//
D=M
// D=first
D=first number
number
(SECOND_IS_GREATER)
(SECOND_IS_GREATER)
@2
@2
M=D
//
M=D
// M[2]=D
M[2]=D (greater
(greater number)
number)
(INFINITE_LOOP)
(INFINITE_LOOP)
@INFINITE_LOOP
@INFINITE_LOOP //
// Infinite
Infinite loop
loop (our
(our standard
standard
0;JMP
//
way
to
terminate
programs).
0;JMP
// way to terminate programs).
CPU Emulator Tutorial, www.idc.ac.il/tecs
Tutorial Index
@xxx
Loading programs
Setting up output and compare files
Writing values into RAM locations
Writing values into registers
Executing the next command (ticktack)
Looping (repeat)
And more (see Appendix B).
Notes:
//
// test
test 3:
3: max(12,12)
max(12,12)
//
Etc.
// Etc.
CPU Emulator Tutorial, www.idc.ac.il/tecs
Tutorial Index
Slide 29/40
Speed
control
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.
Execute the next
simulation step
Tutorial Index
Slide 30/40
Tutorial Index
Slide 31/40
Part V:
Debugging
Tutorial Index
Slide 32/40
A:
D:
PC:
Breakpoints:
Tutorial Index
Slide 33/40
Breakpoints declaration
2. Previouslydeclared
breakpoints
1. Open the
breakpoints
panel
3. Add, delete,
or update
breakpoints
Tutorial Index
Slide 34/40
Breakpoints declaration
Tutorial Index
Slide 35/40
Breakpoints usage
1. New
breakpoint
2. Run the
program
Tutorial Index
Slide 36/40
Tutorial Index
Slide 37/40