Cpu Emulator

Embed Size (px)

Citation preview

  • 7/28/2019 Cpu Emulator

    1/37

    Slide 1/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    This program is part of the software suitethat 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

    CPU Emulator Tutorial

    http://www.idc.ac.il/tecshttp://www.idc.ac.il/tecs
  • 7/28/2019 Cpu Emulator

    2/37

    Slide 2/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    Background

    The Elements of Computing Systemsevolves aroundthe construction of a complete computer system,done in the framework of a 1- or 2-semester course.

    In the first part of the book/course, we build the

    hardware platform of a simple yet powerfulcomputer, called Hack. In the second part, we buildthe computers software hierarchy, consisting of anassembler, a virtual machine, a simple Java-likelanguage called Jack, a compiler for it, and a mini

    operating system, written in Jack.The book/course is completely self-contained,requiring only programming as a pre-requisite.

    The books web site includes some 200 test

    programs, test scripts, and all the softwaretools necessary for doing all the projects.

  • 7/28/2019 Cpu Emulator

    3/37

    Slide 3/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    The books software suite

    This tutorial isabout theCPU emulator.

    Translators (Assembl er ,J ackCompi l er ):

    Used to translate from high-level to low-level; Developed by the students, using the books

    specs; Executable solutions supplied by us.

    Other

    Bi n: simulators and translators software;

    bui l t I n: executable versions of all the logic

    gates and chips mentioned in the book;

    OS: executable version of the J ack OS;

    Text Compar er : a text comparison utility.

    (All the supplied tools are dual-platform: Xxx. bat startsXxx in Windows, and Xxx. sh starts it in Unix)

    Simulators(HardwareSi mul at or , CPUEmul at or , VMEmul at or ):

    Used to build hardware platforms andexecute programs;

    Supplied by us.

  • 7/28/2019 Cpu Emulator

    4/37Slide 4/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    Tutorial Objective

    Learn how to use the

    CPU Emulator

    for simulating the

    execution of machine

    language programs onthe Hack computer

  • 7/28/2019 Cpu Emulator

    5/37Slide 5/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    The Hack computer

    This CPU emulator simulates theoperations of the Hack computer,built in chapters 1-5 of the book.

    Hack -- a 16-bit computer equippedwith a screen and a keyboard --resembles hand-held computerslike game machines, PDAs, andcellular telephones.

    Before such devices are actuallybuilt in hardware, they are plannedand simulated in software.

    The CPU emulator is one of

    the software tools used forthis purpose.

  • 7/28/2019 Cpu Emulator

    6/37Slide 6/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    CPU Emulator Tutorial

    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/28/2019 Cpu Emulator

    7/37Slide 7/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    CPU Emulator Tutorial

    Part I:

    Basic Platform

  • 7/28/2019 Cpu Emulator

    8/37

  • 7/28/2019 Cpu Emulator

    9/37

    Slide 9/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    instruction

    memory

    registers

    keyboard

    enabler

    screen

    datamemory

    ALU

    The Hack Computer Platform

  • 7/28/2019 Cpu Emulator

    10/37

    Slide 10/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    Instruction memory

    Instruction memory

    (32K): Holds a machine

    language program

    The loaded code can be viewed

    either in binary, or in symbolic

    notation (present view)

    Program counter(PC) (16-bit):Selects the next instruction.

    Next instruction

    is highlighted

  • 7/28/2019 Cpu Emulator

    11/37

    Slide 11/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    Data memory (RAM)

    Data memory (32K RAM), used for:

    General-purpose data storage(variables, arrays, objects, etc.)

    Screen memory map

    Keyboard memory map

    Address (A) register, used to:

    Select the current RAM location

    OR

    Set the Program Counter (PC) forjumps (relevant only if the currentinstruction includes a jump directive).

  • 7/28/2019 Cpu Emulator

    12/37

    Slide 12/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    Registers

    Registers (all 16-bit):

    D: Data register

    A: Address register

    M: Stands for the memory register

    whose address is the currentvalue of the Address register

    M (=RAM[A])

    D

    A

  • 7/28/2019 Cpu Emulator

    13/37

    Slide 13/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    Arithmetic/Logic Unit

    Arithmetic logic unit (ALU)

    The ALU can compute various arithmeticand logical functions (lets call them f) onsubsets 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.)

    The ALU operation (LHS destination,function, RHS operands) is specified bythe current instruction.

    Current

    instruction

    M (=RAM[A])

    D

    A

  • 7/28/2019 Cpu Emulator

    14/37

    Slide 14/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    CPU Emulator Tutorial

    Part II:

    I/O Devices

  • 7/28/2019 Cpu Emulator

    15/37

    Slide 15/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    I/O devices: screen and keyboard

    Simulated screen: 256 columns by512 rows, black & white memory-mapped device. The pixels arecontinuously refreshed from respectivebits in an 8K memory-map, located at

    RAM[16384] - RAM[24575].

    Simulated keyboard:One click on this button causes theCPU emulator to intercept all thekeys subsequently pressed on thereal computers keyboard; anotherclick disengages the real keyboardfrom the emulator.

  • 7/28/2019 Cpu Emulator

    16/37

    Slide 16/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    Screen action demo

    1. Select a word in the RAM regionthat serves as the screen memory

    map, e.g. address 16384 (the firstword in the screen memory map).

    2. Enter a value, say 1(1111111111111111 in binary)

    3. Buil t-in Refresh action:

    The emulator draws thecorresponding pixels on thescreen. In this case, 16 blackpixels, one for each binary 1.

    Perspective:Thats how computer

    programs put images (text, pictures,video) on the screen: they write bits intosome display-oriented memory device.

    This is rather hard to do in machinelanguage programming, but quite easy

    in high-level languages that write to thescreen indirectly, using OS routines likepr i nt St r i ng or dr awCi r cl e, as we

    will see in chapters 9 and 12.

    Since all high level programs and OS

    routines are eventually translated intomachine language, they all end updoing something like this example.

    K b d i d

  • 7/28/2019 Cpu Emulator

    17/37

    Slide 17/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    Keyboard action demo

    1. Click the keyboard enabler

    2. Press some key on the

    real keyboard, say S

    3. Watch here:

    Keyboard memory

    map

    (a single 16-bit

    memory location)

    K b d ti d

  • 7/28/2019 Cpu Emulator

    18/37

    Slide 18/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    Keyboard action demo

    Keyboard memory

    map

    (a single 16-bit

    memory location)

    As long a keyis pressed,

    The emulator displaysIts character code in thekeyboard memory map

    Perspective:Thats how computerprograms read from the keyboard: theypeek some keyboard-oriented memorydevice, one character at a time.

    This is rather tedious in machine

    language programming, but quite easy inhigh-level languages that handle thekeyboard indirectly, using OS routineslike r eadLi ne or r eadI nt , as we will see

    in Chapters 9 and 12.

    Since all high level programs and OSroutines are eventually translated intomachine language, they all end up doingsomething like this example.

    Visual echo

    (convenient GUIeffect, not part of thehardware platform)

    CPU E l t T t i l

  • 7/28/2019 Cpu Emulator

    19/37

    Slide 19/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    CPU Emulator Tutorial

    Part III:

    Interactive

    Simulation

    Loading a program

  • 7/28/2019 Cpu Emulator

    20/37

    Slide 20/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    Loading a program

    Navigate to adirectory and selecta . hack or . asmfile.

    Loading a program

  • 7/28/2019 Cpu Emulator

    21/37

    Slide 21/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    Loading a program

    Can switchfrom binary tosymbolicrepresentation

    Running a program

  • 7/28/2019 Cpu Emulator

    22/37

    Slide 22/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    Running a program

    1. Enter anumber,say 50.

    2. Click therunbutton.

    4. Watch

    here

    3. To speed upexecution,use the speedcontrol slider

    Programs description: Draws a rectangle at the

    top left corner of the screen. The rectangles widthis 16 pixels, and its length is determined by thecurrent contents of RAM[0].

    Note:There is no need to understand the programscode in order to understand whats going on.

    Hack programming at a glance (optional)

  • 7/28/2019 Cpu Emulator

    23/37

    Slide 23/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    Hack programming at a glance (optional)

    Program action:

    Since RAM[0] happens to be 50,

    the program draws a 16X50rectangle. In this example theuser paused execution whenthere are 14 more rows to draw.

    Programs description: Draws a rectangle at the

    top left corner of the screen. The rectangles widthis 16 pixels, and its length is determined by thecurrent contents of RAM[0].

    Note:There is no need to understand the programscode in order to understand whats going on.

    Next instruction is M=- 1.

    Since presently A=17536, the next ALUinstruction will effect RAM[17536] =1111111111111111. The 17536 address,which falls in the screen memory map,corresponds to the row just below therectangles current bottom. In the next screen

    refresh, a new row of 16 black pixels will bedrawn there.

    Animation options

  • 7/28/2019 Cpu Emulator

    24/37

    Slide 24/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    Animation options

    Animation control: Pr ogr am f l ow(default): highlights the

    current instruction in the instruction memoryand the currently selected RAM location

    Pr ogr am&dat a f l ow: animates all

    program and data flow in the computer No ani mat i on: disables all animation

    Usage tip:To execute any non-trivial programquickly, select no animation.The simulator can

    animate both program

    flow and data flow

    Controls execution

    (and animation)speed.

    CPU Emulator Tutorial

  • 7/28/2019 Cpu Emulator

    25/37

    Slide 25/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    CPU Emulator Tutorial

    Part IV:

    Script-Based

    Simulation

    Interactive VS Script-Based Simulation

  • 7/28/2019 Cpu Emulator

    26/37

    Slide 26/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    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 planning and using 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.

    The basic setting

  • 7/28/2019 Cpu Emulator

    27/37

    Slide 27/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    The basic setting

    tested program

    test script

    Example: Max. asm

  • 7/28/2019 Cpu Emulator

    28/37

    Slide 28/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    Example: Max. asm

    / / Comput es M[ 2] =max( M[ 0] , M[ 1] ) where M st ands f or RAM

    @0

    D=M / / D = M[ 0]@1

    D=D- M / / D = D - M[ 1]

    @FI RST_I S_GREATER

    D; J GT / / I f D>0 goto FI RST_I S_GREATER

    @1

    D=M / / D = M[ 1]

    @SECOND_I S_GREATER

    0; J MP / / Got o SECOND_I S_GREATER

    ( FI RST_I S_GREATER)

    @0

    D=M / / D=f i r st number

    ( SECOND_I S_GREATER)

    @2

    M=D / / M[ 2]=D ( great er number )

    ( I NFI NI TE_LOOP)

    @I NFI NI TE_LOOP / / I nf i ni t e l oop ( our st andar d0; J MP / / way t o t er mi nat e pr ogr ams) .

    / / Comput es M[ 2] =max( M[ 0] , M[ 1] ) where M st ands f or RAM

    @0

    D=M / / D = M[ 0]

    @1

    D=D- M / / D = D - M[ 1]

    @FI RST_I S_GREATER

    D; J GT / / I f D>0 goto FI RST_I S_GREATER

    @1

    D=M / / D = M[ 1]@SECOND_I S_GREATER

    0; J MP / / Got o SECOND_I S_GREATER

    ( FI RST_I S_GREATER)

    @0

    D=M / / D=f i r st number( SECOND_I S_GREATER)

    @2

    M=D / / M[ 2] =D ( great er number )

    ( I NFI NI TE_LOOP)

    @I NFI NI TE_LOOP / / I nf i ni t e l oop ( our st andar d0; J MP / / way t o t er mi nat e pr ogr ams) .

    Hack language at a glance:

    ( l abel ) // defines a label

    @xxx // sets the A register

    // to xxxs value

    The other commands are self-explanatory; J ump directiveslikeJ GT andJ MP mean J ump

    to the address currently storedin the A register

    Before any command involvinga RAM location (M), the A

    register must be set to the

    desired RAM address(@addr ess)

    Before any command involvinga jump, the A register must be

    set to the desired ROMaddress (@l abel ).

    Note: For now, it is not necessary to understand either the Hackmachine language or the Max program. It is only important to

    grasp the programs logic. But if youre interested, we give alanguage overview on the right.

    Sample test script: Max. t st

  • 7/28/2019 Cpu Emulator

    29/37

    Slide 29/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    p p

    | RAM[ 0] | RAM[ 1] | RAM[ 2] || 15 | 32 | 32 |

    | 47 | 22 | 47 |

    | RAM[ 0] | RAM[ 1] | RAM[ 2] || 15 | 32 | 32 |

    | 47 | 22 | 47 |

    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 (t i ckt ack)

    Looping (r epeat )

    And more (see Appendix B).

    Notes: As it turns out, the Max program requires 14

    cycles to complete its execution

    All relevant files ( . asm, . t st , . cmp) must

    be present in the same directory.

    Output

    / / Load t he pr ogr am and set up:l oad Max. asm,out put - f i l e Max. out ,compare- t o Max. cmp,out put - l i st RAM[ 0] %D2. 6. 2

    RAM[ 1] %D2. 6. 2RAM[ 2] %D2. 6. 2;

    / / Test 1: max(15, 32)set RAM[ 0] 15,set RAM[ 1] 32;r epeat 14 {

    t i ckt ock;}out put ; / / t o t he Max. out f i l e

    / / Test 2: max(47, 22)set PC 0, / / Reset pr og. count erset RAM[ 0] 47,set RAM[ 1] 22;r epeat 14 {

    t i ckt ock;}out put ;

    / / t est 3: max( 12, 12)/ / E tc .

    / / Load t he pr ogr am and set up:l oad Max. asm,

    out put - f i l e Max. out ,compare- t o Max. cmp,out put - l i st RAM[ 0] %D2. 6. 2

    RAM[ 1] %D2. 6. 2RAM[ 2] %D2. 6. 2;

    / / Test 1: max(15, 32)set RAM[ 0] 15,set RAM[ 1] 32;r epeat 14 {

    t i ckt ock;}

    out put ; / / t o t he Max. out f i l e

    / / Test 2: max(47, 22)set PC 0, / / Reset pr og. count erset RAM[ 0] 47,set RAM[ 1] 22;

    r epeat 14 {t i ckt ock;

    }out put ;

    / / t est 3: max( 12, 12)

    / / E tc .

    Using test scriptsS d

  • 7/28/2019 Cpu Emulator

    30/37

    Slide 30/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    g p

    Execute the nextsimulation step

    Execute step afterstep repeatedly

    Pause thesimulation

    Reset

    the script

    Load ascript

    Script = a series ofsimulation steps, eachending with a semicolon;

    Speedcontrol

    Important point: Whenever an assemblyprogram (.asm file) is loaded into theemulator, the program is assembled on thefly into machine language code, and this isthe code that actually gets loaded. In theprocess, all comments and white space are

    removed from the code, and all symbolsresolve to the numbers that they stand for.

    The default script (and a deeper understanding of the CPU emulator logic)

  • 7/28/2019 Cpu Emulator

    31/37

    Slide 31/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    p ( p g g )

    If you load a program file without firstloading a script file, the emulatorloads a default script (always). The

    default script consists of a loop thatruns the computer clock infinitely.

    Note that these run/stop buttons dontcontrol the program. They control thescript, which controls the computersclock, which causes the computerhardware to fetch and execute theprograms instructions, one instruction

    per clock cycle.

    CPU Emulator Tutorial

  • 7/28/2019 Cpu Emulator

    32/37

    Slide 32/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    Part V:

    Debugging

    Breakpoints: a powerful debugging tool

  • 7/28/2019 Cpu Emulator

    33/37

    Slide 33/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    p p gg g

    The CPU emulator continuously keeps track of: A: value of the A register

    D: value of the D register

    PC: value of the Program Counter

    RAM[ i ] : value of any RAM location

    t i me: number of elapsed machine cycles

    Breakpoints: A breakpoint is a pair where variable is one of

    {A, D, PC, RAM[ i ] , t i me}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, theemulator pauses the programs execution with a proper message.

    Breakpoints declaration

  • 7/28/2019 Cpu Emulator

    34/37

    Slide 34/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    1. Open thebreakpointspanel

    3. Add, delete,or updatebreakpoints

    2. Previously-declaredbreakpoints

    Breakpoints declaration

  • 7/28/2019 Cpu Emulator

    35/37

    Slide 35/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    2. Enter the valueat which the breakshould occur

    1. Select the systemvariable on which you

    want to break

    Breakpoints usage

  • 7/28/2019 Cpu Emulator

    36/37

    Slide 36/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    1. New

    breakpoint

    3. When the A register will be 2, orRAM[ 20] will be 5, or 12 time units(cycles) will elapse, or RAM[ 21] will

    be 200, the emulator will pause theprograms execution with an

    appropriate message.

    A powerful debugging tool!

    2. Run theprogram

    Postscript: Maurice Wilkes (computer pioneer) discovers debugging:

  • 7/28/2019 Cpu Emulator

    37/37

    Slide 37/40CPU Emulator Tutorial, www.idc.ac.il/tecs Tutorial Index

    As soon as we started programming, we found to our

    surprise that it wasn't as easy to get programs rightas 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 bespent in finding mistakes in my own programs.

    (Maurice Wilkes, 1949).