Introduction

If you decide to use Java to simulate the multi-cycle datapath from Chapter 5 of Computer Organization and Design, 3rd Edition, you are faced with the problem of loading a machine language program into the simulated computer’s memory for testing. Rather than hand-assemble a program or write an assembler in addion to the simulator, you can take advantage of the assembler built into the SPIM simulator that comes with the book and using the resources provided here.

The first link below is to a Jar file that contains five classes: Loader, Memory, MIPS_Multi, Registers, and Utils. As their names imply, Loader and Memory provide a mechanism for you to load a program into simulated memory. The other classes provide enough additional features to make the JAR file a complete executable program. How to run that program is described below, but if you want to, you can just extract the source code files you need from the Jar file and work from there.

Procedure for using the Jar file as a program.

First, write a MIPS program using the instructions from Chapter 5 that you are going to simulate (lw, sw, R-type, j, and beq). If you want, you can use other instructions and code your simulator to ignore them. Or you can simulate them!

Load your program into the SPIM simulator in the usual way. If you want to link multiple source files together, you may load them together into SPIM. Be sure your code includes a main routine and that it actually runs on the SPIM simulator; that is, when your program is loaded, the SPIM PC should contain the address of the first executable instruction in your program. If you want to avoid the simulating startup code, you can single step SPIM until the PC contains the address of your own first instruction.

Now save a Log File from the SPIM File menu. This will produce a text file containing all the information you see in the various panes of the simulator window. Save this file in an easy-to-find location, such as your Desktop. Download the Jar file to the same location, open a Command Prompt window, and change to that directory. If you are running Windows, the Command Prompt program is at Start→Accessories. When you start it, you will be in the directory above your Desktop, and you can use a cd Desktop command to change to it.

To run the program as it is, use the command:

java -jar MIPS_Multi.jar PCSpim.log

This command assumes you did not change the default name of the SPIM log file. The program will read the log file, extract the information about register and memory contents, save the values in the simulated registers and memory, and then display a dump of all the information it found. You should compare the output of the program to the actual log file to make sure they agree with each other.

Possible Problems:

You will need to have a current version of Java installed; the program uses features from Sun’s Java version 1.5 and will not work with earlier ones. Also, the program should run on any operating system with an up to date version of Java, but the log file must come from PC-SPIM; you might be able to get xspim running on OS-X or Linux to print a log file with the same structure to a file, but I have not tried it.

Developing your own simulator

As mentioned above, you can extract the source code for the various classes from the Jar file and make them part of your own project. Alternatively, you can simply leave the Jar file as is and write your own code that uses the Jar file’s code as follows:

Create a directory named MIPS_Multi, declare all your classes to be part of the MIPS_Multi package, specify MIPS_Multi as the output directory when you compile your code, and include MIPS_Multi in the classpath when you compile your code. For example, you could create a file named Main.java containing the following program:

package MIPS_Multi; public class Main { public static void main(String[] args) { Registers.dump(); } }

If Main.java, MIPS_Multi.jar, and the MIPS_Multi directory are all in your current working directory, the following commands will build an executable program that simply prints the contents of all the registers on the console. (The registers will all contain zeros because they have not been assigned any values yet.)

javac -cp '.;MIPS_Multi.jar' Main.java java -cp '.;MIPS_Multi.jar' MIPS_Multi.Main

If you use Eclipse, create a new project, import MIPS_Multi.jar into it, and select your own Main class when you set up the Run configuration.

Note the documentation link below.

Resources