This program is part of the software suite - PowerPoint PPT Presentation

About This Presentation
Title:

This program is part of the software suite

Description:

CPU Emulator Tutorial, www.idc.ac.il/tecs. Tutorial Index ... can be emulated on the software-based CPU Emulator described in this tutorial. ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 41
Provided by: shimons
Category:
Tags: part | program | software | suite | tut

less

Transcript and Presenter's Notes

Title: This program is part of the software suite


1
CPU Emulator Tutorial
  • This program is part of the software suite
  • that accompanies the book
  • The Elements of Computing Systems
  • by Noam Nisan and Shimon Schocken
  • MIT Press
  • www.idc.ac.il/tecs
  • This software was developed by students at the
  • Efi Arazi School of Computer Science at IDC
  • Chief Software Architect Yaron Ukrainitz

2
Background
The CPU Emulator is part of the software suite
that accompanies The Elements of Computing
Systems. This book evolves around the
construction of a complete computer system, done
in the framework of a 1- or 2-semesters
course. In the first part of the book, we build
the hardware platform of a simple yet powerful
computer, called Hack. All the operations of
this computer can be emulated on the
software-based CPU Emulator described in this
tutorial. In the second part of the book, we
build the computers software hierarchy,
consisting of an assembler, a virtual machine, a
simple Java-like language called Jack, a compiler
for it, and a simple operating system, written
in Jack. The book is completely self-contained,
requiring only programming as a
pre-requisite. The books software suite includes
some 200 test programs, test scripts, and all
the software tools necessary for doing all the
projects.
3
The books software suite
  • Simulators (HardwareSimulator, CPUEmulator,
    VMEmulator)
  • Used to build hardware platforms andexecute
    programs
  • Supplied by us.
  • All the tools are dual-platform
  • Xxx.bat starts Xxx in Windows
  • Xxx.sh starts Xxx in Unix.
  • 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
  • builtIn executable versions of all the logic
    gates and chips mentioned in the book
  • OS executable version of the Jack OS
  • TextComparer a text comparison utility.

4
Tutorial Objective
Learn how to use the CPU Emulator for simulating
the execution of machine language programs on the
Hack computer
5
The Hack computer
This CPU Emulator simulates the operations of the
Hack computer, built in chapters 1-5 of the
book. Hack -- a 16-bit computer equipped with a
screen and a keyboard -- resembles hand-held
computers like game machines, PDAs, and cellular
telephones. Before such devices are actually
built in hardware, they are planned and simulated
in software. The CPU Emulator is one of the
software tools used for this purpose.
6
CPU Emulator Tutorial Contents
  • I. Basic Platform
  • II. I/O devices
  • III. Interactive simulation
  • IV. Script-based simulation
  • V. Debugging
  • Relevant reading (from The Elements of Computing
    Systems)
  • Chapter 4 Machine Language
  • Chapter 5 Computer Architecture
  • Appendix B Test Scripting Language

7
CPU Emulator Tutorial
Part I Basic Platform
8
The Hack Computer Platform
9
The Hack Computer Platform
Screen
Datamemory
Keyboard enabler
Instruction memory
ALU
10
Instruction memory
Instruction memory (32K) Holds a machine
language program
11
Data memory (RAM)
  • Data memory (32K RAM), used for
  • General-purpose data storage (variables,
    arrays, objects, etc.)
  • Screen memory map
  • Keyboard memory map

12
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

13
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. MM-1, MDDA , A0, etc.)
  • The ALU operation (LHS destination, function, RHS
    operands) is specified by the current instruction.

Nextinstruction
14
CPU Emulator Tutorial
Part II I/O Devices
15
I/O devices screen and keyboard
Simulated screen 256 columns by 512 rows, black
white memory-mapped device. The pixels are
continuously refreshed from respective bits in an
8K memory-map, located at RAM16384 - RAM24575.
Simulated keyboardOne click on this button
causes the CPU Emulator to intercept all the keys
subsequently pressed on your real computers
keyboard another click disengages the real
keyboard from the Emulator.
16
Screen action demo
Perspective Thats how computer programs put all
images (text, pictures, video) on the screen
they write bits into some display-oriented memory
device. This is rather hard to do in machine
language programming, but quite easy in
high-level languages that write to the screen
indirectly, using OS routines like printString or
drawCircle, as we will see in Chapters 9 and
12. Since all high level programs and OS routines
are eventually translated into machine language,
they all end up doing something like this example.
  • Select a word in the RAM region that serves as
    the screen memory map, e.g. address 16384 (the
    first word in the screen memory map).
  • Enter a value, say 1 (1111111111111111 in binary)

17
Keyboard action demo
  • Click the keyboard enabler
  • Press some key on the real keyboard, say S

3. Watch here
18
Keyboard action demo
Perspective Thats how computer programs read
from the keyboard they peek some
keyboard-oriented memory device, one character at
a time. This is rather tedious in machine
language programming, but quite easy in
high-level languages that handle the keyboard
indirectly, using OS routines like readLine or
readInt, as we will see in Chapters 9 and
12. Since all high level programs and OS routines
are eventually translated into machine language,
they all end up doing something like this example.
Visual echo (convenient GUI effect, not part of
the hardware platform)
19
CPU Emulator Tutorial
Part III Interactive Simulation
20
Loading a program
1. To load a new program, click the Load button.
2. Navigate to a directory and select a .hack or
.asm file.
21
Loading a program
Can switch from binary to symbolic representation
22
Running a program
4. Watch here
1. Enter a number,say 50.
2. Click the run button.
3. To speed up execution, use the speed control
slider
This programs logic Draws a rectangle at the
top left corner of the screen. The rectangles
width is 16 pixels, and its length is determined
by the current contents of RAM0. Note There is
no need to understand the programs code in order
to understand whats going on.
23
Running a program
4. Watch here
1. Enter a number,say 50.
2. Click the run button.
3. To speed up execution, use the speed control
slider
This programs logic Draws a rectangle at the
top left corner of the screen. The rectangles
width is 16 pixels, and its length is determined
by the current contents of RAM0. Note There is
no need to understand the programs code in order
to understand whats going on.
24
Running a program
This programs logic Draws a rectangle at the
top left corner of the screen. The rectangles
width is 16 pixels, and its length is determined
by the current contents of RAM0. Note There is
no need to understand the programs code in order
to understand whats going on.
25
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
  • Usage tip To execute any non-trivial program
    quickly, select no animation.

26
CPU Emulator Tutorial
Part IV Script-Based Simulation
27
Interactive VS Script-based simulation
  • A program can be executed and debugged
  • Interactively, by ad-hoc playing with the
    Emulators GUI(as we have done so far in this
    tutorial)
  • Batch-ly, by running a pre-planned set of tests,
    specified in a script.
  • Script-based simulation enables us to plan and
    use tests that are
  • Pro-active
  • Documented
  • Replicable
  • Complete (as much as possible)
  • 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.

28
The basic setting
29
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.
  • Hack language at a glance
  • (label) // defines a label
  • _at_xxx // sets the A register
    // to xxxs value
  • The other commands are self-explanatory Jump
    directives like JGT and JMP mean Jump to the
    address currently stored in the A register
  • Before any command involving a RAM location (M),
    the A register must be set to the desired RAM
    address (_at_address)
  • Before any command involving a jump, the A
    register must be set to the desired ROM address
    (_at_label).

// Computes M2max(M0,M1) where M stands
for RAM _at_0 DM // D M0
_at_1 DD-M // D D - M1
_at_FIRST_IS_GREATER DJGT // If Dgt0
goto FIRST_IS_GREATER _at_1 DM
// D M1 _at_SECOND_IS_GREATER 0JMP
// Goto SECOND_IS_GREATER (FIRST_IS_GREATER)
_at_0 DM // Dfirst
number (SECOND_IS_GREATER) _at_2 MD
// M2D (greater number) (INFINITE_LOOP)
_at_INFINITE_LOOP // Infinite loop (our standard
0JMP // way to terminate programs).
30
Sample test script Max.tst
  • The scripting language
  • has commands for
  • 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
  • As it turns out, the Max program requires 14
    cycles to complete its execution
  • All relevant files (.asm,.tst,.cmp) must be in
    the same directory.

// Load the program and set up load
Max.asm, output-file Max.out, compare-to
Max.cmp, output-list RAM0D2.6.2
RAM1D2.6.2 RAM2D2.6.2 //
Test 1 max(15,32) set RAM0 15, set RAM1
32 repeat 14 ticktock output // to the
Max.out file // Test 2 max(47,22) set PC 0, //
Reset prog. counter set RAM0 47, set RAM1
22 repeat 14 ticktock output // test 3
max(12,12) // Etc.
Output
RAM0 RAM1 RAM2 15
32 32 47 22
47
31
Using test scripts
Interactive loading of the tested program itself
(.asm or .hack file) is typically not necessary,
since test scripts typically begin with a load
program command.
32
Using test scripts
Speed control
Load a script
Script a series of simulation steps, each
ending with a semicolon
Resetthe script
Important point Whenever an assembly program
(.asm file) is loaded into the emulator, the
program is assembled on the fly into machine
language code, and this is the code that actually
gets loaded. In the process, all comments and
white space are removed from the code, and all
symbols resolve to the numbers that they stand
for.
Pause the simulation
Execute step after step repeatedly
Execute the next simulation step
33
Using test scripts
  • View options
  • Script Shows the current script
  • Output Shows the generated output file
  • Compare Shows the given comparison file
  • Screen Shows the simulated screen

When the script terminates, the comparison of the
script output and the compare file is reported
34
The default script (and a deeper understanding
of the CPU Emulator logic)
If you load a program file without first loading
a script file, the Emulator loads a default
script (always). The default script consists of a
loop that runs the computer clock in infinitely.
35
CPU Emulator Tutorial
Part V Debugging
36
Breakpoints a powerful debugging tool
  • The CPU Emulator keeps track of
  • A value of the A register
  • D value of the D register
  • PC value of the Program Counter
  • RAMi value of any RAM location
  • time number of elapsed machine cycles
  • Breakpoints
  • A breakpoint is a pair ltvariable, valuegt where
    variable is one of A, D, PC, RAMi, 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
    programs execution with a proper message.

37
Breakpoints declaration
1. Open the breakpoints panel
3. Add, delete, or update breakpoints
38
Breakpoints declaration
1. Select the system variable on which you want
to break
2. Enter the value at which the break should occur
39
Breakpoints usage
1. New breakpoint
2. Run the program
3. When the A register will be 2, or RAM20 will
be 5, or 12 time units (cycles) will elapse, or
RAM21 will be 200, the Emulator will pause the
programs execution with an appropriate
message. A powerful debugging tool!
40
Postscript Maurice Wilkes (computer pioneer)
discovers debugging
  • As soon as we started programming, we found to
    our surprise that it wasn't as easy to get
    programs right as we had thought. Debugging had
    to be discovered. I can remember the exact
    instant when I realized that a large part of my
    life from then on was going to be spent in
    finding mistakes in my own programs.
  • (Maurice Wilkes, 1949).
Write a Comment
User Comments (0)
About PowerShow.com