CPSC 220, Fall 2012
Lab 4: Writing a Larc Simulator

The assignment for this lab is to write a Java program that simulates the Larc computer. The program will be a "functional simulator." That is, it makes no attempt to simulate Larc on a hardware level. Instead, it acts an an interpretor for Larc machine language programs: It imitates Larc's fetch-and-execute cycle by carrying out the operation indicated by each machine language instruction, without trying to perform that operation in the same way that Larc would.

There are many ways to design the simulation program. You are not required to use any particular design. But whatever design you choose, it should be well-organized and should follow all the usual Java programming style rules. Your program should, of course, be beautiful and a pleasure to read.

Your program is due next week at the beginning of lab. Place a copy of your completed program, LarcSim.java, into your homework folder in /classes/cs220/homework. Note that this is an individual assignment. By turning it in, you are affirming that you have not worked on the program with anyone else and that you have not discussed the program with anyone else, except possibly for the instructor and teaching assistants. Receiving or giving help on this project is considered academic dishonesty can can result in a zero on the assignment, a failing grade in the course, or a referral to the Committee on Standards.

Some Setup

As a starting point, you can use the file LarcSim.java. This program stub has a main() routine that reads a Larc machine language program file and converts it into an array of int. Each int in the array is one instruction in the machine language program. See the comment on the main() routine for more information. You can find a copy of LarcSim.java in the folder /classes/cs220. There is also a copy of TextIO.java in that folder, in case you want to use it for input.

When LarcSim is run, it needs a machine language program to execute. To give the name of the program on the command line, run the program with a command of the form

java  LarcSim  program.out

where program.out is the name of the file that contains the machine language program. The directory larcML, which you can find in /classes/cs220, contains some sample machine language programs that you can use to try out your simulator. (These programs are part of the Larc distribution and were written by Marc Corliss.) You could also use the programs that you wrote for Lab 3. The machine language program does not have to be in the same directory as your simulator; you can use a path to the file. For example, to run the sample program calc.out from the larcML directory, you can say

java  LarcSim  /classes/cs220/larcML/calc.out

If you run LarcSim with no file on the command line, it will put up a file chooser dialog box where you can select the input file. This will work for example, if you run the program from inside Eclipse.

The Simulator

As we discussed in class, your program will need variables to represent at least: the 16 registers, the 65536-location memory, and the program counter. At the core of your simulation is a loop that imitates the fetch-and-execute cycle. In this loop, you fetch an instruction from memory, increment the program counter, and simulate the execution of the instruction.

There are 16 types of instruction, determined by the opcode — the four high-order bits of the instruction. If the opcode is syscall (15, or 1111 in binary), you will have to simulate a system trap. For the purposes of this simulation, you can use standard Java I/O (System.out.println and either TextIO or Scanner) to simulate the four I/O traps.

In all of this, you will have to carefully follow the instruction set architecture described in Chapter 3 of the Larc manual (which was handed out in class).

Error Conditions

In addition to simulating the execution of legal machine language programs, your simulator should detect certain error conditions. When such an error is encountered, you should halt the simulation with an error message such as, "System halted by error," with a description of the error. In particular, your simulator program should not crash when an error occurs; it should report the error and exit gracefully. Error conditions that you should detect include

Note that while register 0 must always hold the value 0, it is not an error to try to store a value into register 0. However, doing so will have no effect on the value stored in the register.