Lab 5 -- The Central Processing Unit and the Intrepreter

Related Reading

5.1 The CPU Interface

The central processing unit is usually called the CPU. The CPU is the core of the machine; it contains the ALU as well as a variety of registers. In our machine, the registers are:

The specification of the processor is simple:

public interface ProcessorI {
  ALUI getALU ();

  Word fetchPC ();
  Word fetchACC ();
  Instruction fetchIR ();

  void storePC (Word newpc);
  void storeACC (Word newacc);
  void storeIR (Instruction newir);
}
For this part of the assignment, you are to write a class Processor that implements ProcessorI and test it (see 5.3 part 1 for more details).

5.2 Interpreter

Note: this part of the assignment relies on the successful implementation of the Processor described in 5.1.

Add an execute method for each of the 16 machine instruction classes that you created in Lab 4. Consult the text (Schneider & Gersting, pp. 194-199) for information on how each instruction operates. The following 2 examples should get you started:

public class LoadIns extends Instruction {

	....

	public void execute (MemoryI mem, ProcessorI proc, ioUnitI io) throws MachineE {
		Word w = mem.fetch(getAddress());
		proc.storeACC(w);
	}
}

public class AddIns extends Instruction {

	....

	public void execute (MemoryI mem, ProcessorI proc, ioUnitI io) throws MachineE {
		Word w1 = proc.fetchACC();
		Word w2 = mem.fetch(getAddress());	
		proc.storeACC(proc.getALU().add(w1,w2));
	}
}

Since the HALT instruction is used to terminate the execution of a program, and this is an exceptional situation, we will use an exception to encapsulate this information:

public class HaltE extends MachineE {
	public HaltE () {}
}
The execute method of the HALT instruction should just throw an exception (HaltE).

5.3 Exercise

Part 1 (CPU):

Write a class Processor that implements ProcessorI and test it. The constructor for the processor should create the ALU and initialize the registers to sensible values.

The help files are at the URL: http://www.cs.uoregon.edu/~sabry/duckMachine/processor/ and include:

Part 2 (Interpreter):

Implement the execute methods for all 16 machine instruction classes. You will be responsible for testing your own code, but to give you an idea of how you might do this, the following program tests the execute methods of the LoadIns and the AddIns.

package duckMachine.architecture;

public class TestExecute {

  public static void main (String [] args) {
	try {
	  MemoryI mem = new Memory();
  	  ProcessorI proc = new Processor();
  	  ioUnitI io = new ioUnit();

  	  //store number 23 in memory location 50
  	  Address a1 = new Address(50);
  	  Data d1 = new Data(23);
  	  mem.store(a1,d1);

  	  //test LoadIns
  	  //-- set accumulator to contents of memory location 50 (23)
  	  System.out.println("Accumulator before Load = " + proc.fetchACC() );
  	  LoadIns load = new LoadIns(a1);
  	  load.execute(mem, proc, io);
  	  System.out.println("Accumulator after Load = " + proc.fetchACC() );

 	  //store number 8 in memory location 75
  	  Address a2 = new Address(75);
  	  Data d2 = new Data(8);
	  mem.store(a2,d2);

 	  //test AddIns
  	  // -- add contents of memory location 75 to accumulator
  	  //  should be 23 + 8
  	  System.out.println("Accumulator before Add= " + proc.fetchACC() );
  	  AddIns add = new AddIns(a2);
  	  add.execute(mem, proc, io);
  	  System.out.println("Accumulator after Add= " + proc.fetchACC() );
	}
	catch (MachineE e) {
	  System.err.println(e.toString());
	}
  }
}

Turn in the code for Processor.java and the 16 instruction classes during class on Monday, May 10th.


Visited times since March 23, 1999 (or the last crash).

Last modified: 04/28/99 09:28:25

yreimer@cs.uoregon.edu