Download presentation
Presentation is loading. Please wait.
Published byMisael Fedder Modified over 9 years ago
1
Slide 1CPU Emulator Tutorial This program is part of the software suite that accompanies the book The Digital Core, by Noam Nisan and Shimon Schocken 2003, www.idc.ac.il/csd, forthcoming by MIT Presswww.idc.ac.il/csd The software suite was developed by students at the Efi Arazi School of Computer Science at IDC Chief Software Architect: Yaron Ukrainitz
2
Slide 2CPU Emulator Tutorial Tutorial Objective Learn how to use the CPU Emulator for simulating the execution of machine language programs on the Hack computer
3
Slide 3CPU Emulator Tutorial 256 by 512, black & white, simulated screen. The pixels are continuously refreshed from their respective bits in the memory-mapped screen (RAM). The Computer The current keystroke is displayed here. A simulated keyboard. Must be activated (by clicking this button) to capture the user’s keystrokes on the real keyboard Address register, used to: Select the current RAM location Set the program counter for jumps Load constants under program control General-purpose data register (16-bit) When accessing the RAM, the accessed address is highlighted. Program counter (16-bit). Selects the current instruction. Arithmetic logic unit (ALU). Takes two inputs (D and M/A) and produces one output. Current instruction is highlighted in the ROM. Instruction memory (32K). Holds a machine code program Data memory (32K), used for Data storage Memory-mapped screen Memory-mapped keyboard.
4
Slide 4CPU Emulator Tutorial Loading a program To locate a specific address, click the search button and type the desired address. Click one of the Load Program buttons to select a program file (.prg or.asm) Select the desired display format. Clears the ROM.
5
Slide 5CPU Emulator Tutorial The max program // This program computes: M[2] = max(M[0], M[1]) @0 D=M // D = first number @1 D=D-M // D = first number - second number @output_first D;JGT // if D>0 (first is greater) goto output_first @1 D=M // D = second number @output_d 0;JMP // goto output_D (output_first) @0 D=M // D = first number (output_d) @2 M=D // M[2] = D (greatest number) (infinite_loop) @infinite_loop 0;JMP // jump infinitely // This program computes: M[2] = max(M[0], M[1]) @0 D=M // D = first number @1 D=D-M // D = first number - second number @output_first D;JGT // if D>0 (first is greater) goto output_first @1 D=M // D = second number @output_d 0;JMP // goto output_D (output_first) @0 D=M // D = first number (output_d) @2 M=D // M[2] = D (greatest number) (infinite_loop) @infinite_loop 0;JMP // jump infinitely Example of an assembly program. Computes M[2] = max(M[0], M[1])
6
Slide 6CPU Emulator Tutorial Tests scripts rom-load max.prg, output-file output, compare-to max.cmp, output-list RAM[0]%D2.6.2; set RAM[0] 15, set RAM[1] 32; repeat 14 { ticktack; } output; set PC 0, set RAM[0] 47, set RAM[1] 22; repeat 14 { ticktack; } output; rom-load max.prg, output-file output, compare-to max.cmp, output-list RAM[0]%D2.6.2; set RAM[0] 15, set RAM[1] 32; repeat 14 { ticktack; } output; set PC 0, set RAM[0] 47, set RAM[1] 22; repeat 14 { ticktack; } output; Used for automated program testing. Everything that can be done interactively in the emulator can be done automatically by the script. Written in TDL.
7
Slide 7CPU Emulator Tutorial Using test scripts To stop execution, click the Stop button. To reset the script to its beginning, click Rewind. To select a script, click the Load Script button To execute step after step repeatedly, click the Fast Forward button. To execute one step from the script, click the Single Step button. Execution speed is controlled by the speed slider.
8
Slide 8CPU Emulator Tutorial Testing using the default script The emulator's default script runs the simulated clock in an infinite loop. Fast-forward runs the TickTack command repeatedly until the user clicks Stop. Simulates continuous program execution with the ability to check the state of the computer after each instruction. Single-step performs one TickTack command (single time-unit), simulating the execution of one instruction at a time Enter the data that the program expects to see (if any).
9
Slide 9CPU Emulator Tutorial Testing programs using test scripts rom-load max.prg, output-file output, compare-to max.cmp, output-list RAM[0]%D2.6.2 RAM[1]%D2.6.2 RAM[2]%D2.6.2; set RAM[0] 15, set RAM[1] 32; repeat 14 { ticktack; } output; set PC 0, set RAM[0] 47, set RAM[1] 22; repeat 14 { ticktack; } output; rom-load max.prg, output-file output, compare-to max.cmp, output-list RAM[0]%D2.6.2 RAM[1]%D2.6.2 RAM[2]%D2.6.2; set RAM[0] 15, set RAM[1] 32; repeat 14 { ticktack; } output; set PC 0, set RAM[0] 47, set RAM[1] 22; repeat 14 { ticktack; } output; | RAM[0] | RAM[1] | RAM[2] | | 15 | 32 | 32 | | 47 | 22 | 47 | | RAM[0] | RAM[1] | RAM[2] | | 15 | 32 | 32 | | 47 | 22 | 47 | A test script is usually designed to test a specific program. Can create an “output dump” of the test results. Along with the script, a comparison file is sometimes supplied, to which the output is compared. All relevant files (.prg,.tst,.cmp) should be in the same directory.
10
Slide 10CPU Emulator Tutorial Testing programs using test scripts (cont.) Use the control buttons to run the script When the script ends, the comparison results are reported Program execution stops automatically when a comparison failure occurs To view the output results or the comparison file, select “output” or “compare” from the “view" box Load the script
11
Slide 11CPU Emulator Tutorial Debugging tool: Breakpoints Use the + and - buttons to add and remove breakpoints The CPU Emulator recognizes the following variables: A – the contents of the address register. D – the contents of the data register. PC – the contents of the program counter. RAM[i] – the contents of the RAM at location i. ROM[i] – the contents of the ROM at location i. Time – number of time units since the beginning of the script. When the variable reaches the specified value, a message is displayed. To update an existing breakpoint, double click it To open the breakpoints panel, click the breakpoints button A breakpoint consists of a variable name and a specified value A very powerful debugging tool
12
Slide 12CPU Emulator Tutorial Writing test scripts Load Computer, Output-file output, Output-list ARegister[]%D1.6.1 PC[]%D1.6.1, Breakpoint RAM16K[10] 137; ROM32K Load add.prg, Set reset 0, Set RAM16K[1] 12345, Output; Repeat 6 { Tick, Tack, Output; } Clear-breakpoints; Load Computer, Output-file output, Output-list ARegister[]%D1.6.1 PC[]%D1.6.1, Breakpoint RAM16K[10] 137; ROM32K Load add.prg, Set reset 0, Set RAM16K[1] 12345, Output; Repeat 6 { Tick, Tack, Output; } Clear-breakpoints; Users may write their own test scripts, according to specific debugging needs. Usually, the first step of the script should load the tested program. The rest of the script may use various scripting commands: Output tables Loops Breakpoints Variables manipulation Simulation commands: Eval, Tick, Tack. … Some Built-In chips support special commands. For example, the command “ROM32K Load ” loads a program into the ROM32K chip.
13
Slide 13CPU Emulator Tutorial Visual options To control animation speed, use the speed slider. Select the desired numeric format: Decimal, Hexadecimal or Binary. Select one of the animation options: Program flow: animates only the program flow in the ROM. Program & data flow: animates all program and data flow in the computer. No animation: program and data flow are not animated. When running long programs, choose No animation. The GUI will not be updated during simulation, increasing execution speed. Select one of the additional view options: Script: shows the current script. Output: shows the output file. Compare: shows the comparison file. None: Shows the simulated computer only.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.