Specifications for the Computer Class
The Computer class for assignment #4 represents a computer and must
meet the following specifications.
Public static constants: Constants are needed to represent
the size of memory (which will be 32),
each machine language instruction (such as HALT = 0,
LDA = 1, STA = 2, and so on),
and status information.
Use a different integer value for each.
The main program should use the status constants when checking the status
returned by the loadProgram method and the runProgram method.
Use the following status constants.
- SUCCESS - a value that represents a successful operation (such as
loading or running a program)
- OUT_OF_MEMORY - a value that is returned when the user tries to load
a program that is too long
- INVALID_OPCODE - a value returned when the operation code for an
instruction is invalid (the valid values are 0 - 6).
- INVALID_PC_ADDRESS - a value returned when a memory address
in the program counter is
invalid (valid memory addresses will be in the range 0 up to but not
including the length of the current program - a program may not use
memory outside of its allocated space).
- INVALID_OP_ADDRESS - a value returned with a memory address for
the operand is invalid.
Instance Variables: The class must have the following instance
data.
- A variable of type int representing the program counter.
- A variable of type int representing the instruction register.
- A variable of type int representing the accumulator.
- An array of ints representing memory.
- A variable of type int representing the length of the current program.
Public Methods: The class must have the following public methods.
Private methods: The class should have the following helper
methods that will be called by the runProgram method.
- fetch() - simulates the "fetch" part of the fetch-execute
cycle. It checks to make sure the current value in the program
counter is a valid memory reference (it must be in the range of 0 up
to the length of the current program); if it is, it stores the value in
memory at that address in the instruction register
and increments the program counter. If it isn't
valid,
a status of INVALID_PC_ADDRESS should be returned; otherwise the SUCCESS
status should be returned.
- decodeExecute() - simulates the "decode the instruction
and send signals to execute it" part of the fetch-execute cycle.
The method should return one of the following ints: INVALID_OPCODE
(if the integer in the instruction register does not have a
valid opcode (0 - 6)), INVALID_OP_ADDRESS (if the address part
is out of the range for the program), HALT (if the instruction is
the HALT instruction), or SUCCESS (if the instruction was correct
and not the HALT instruction).
The method gets the opcode from the instruction in the
instruction register (think about how to use integer division / to
get the first digit, the hundreds digit, in a 3-digit number). If the
opcode is not valid the status
should be set
to INVALID_OPCODE. If it is valid, the method should then get
the address of the operand (how can you use % to get the last two
digits?). If it is not valid the
status should be set to INVALID_OP_ADDRESS.
If both the opcode and operand are valid (the status is SUCCESS)
and the instruction is not the halt instruction,
the appropriate
method is called to execute the instruction. The method returns the integer
representing HALT or the status.
- A method for each machine language instruction (except halt).
Each method takes
one integer parameter representing the address of the instruction
operand. Since the decodeExecute method has already checked the
validity of the address, these methods just needs to
"execute" the instruction (for example, all the load method does
is put the value from memory at the given address in the accumulator).
These methods will have void return type.