com.monkeycoder.monkeynes.hardware
Class NesCpu

java.lang.Object
  |
  +--com.monkeycoder.monkeynes.hardware.NesCpu

public class NesCpu
extends Object

This is the brain of the NES. It contains registers, does the work of the OpCodes, and keeps track of machine cycles.

Originally part of the MonkeyNES project.

Author:
David Stephens

Field Summary
static int IRQ_VECTOR
           
static int NMI_VECTOR
           
static int RESET_VECTOR
           
static int STACK_PAGE
           
 
Constructor Summary
NesCpu(NesMainMemory inMainMem, NesVideoMemory inVidMem)
          Creates a new NesCpu.
 
Method Summary
 boolean crossesPageBoundry(int inOpCode, int byte1, int byte2)
          Determines whether or not an operation is going to cross a page boundry.
 void doCycle()
          Called to tell the CPU to do a cycle.
 int getAccumulatorValue()
          Gets the value of the Accumulator (ACC) register.
 int getAvailCycles()
          Gets the number of cycles the CPU has waiting to be used by the next operation.
 int getNeedCycles()
          Gets the number of cycles the CPU is waiting for before completing the next operation.
 int getOpAddress(int inOpCode, int byte1, int byte2)
          Gets the address at which the parameter value is located.
 int getOpValue(int inOpCode, int byte1, int byte2)
          Gets the parameter value based on the OpCode.
 int getProcessorStatusValue()
          Gets the value of the Processor Status (PS) register.
 int getProgramCounterValue()
          Gets the value of the Program Counter (PC) register.
 int getStackPointerValue()
          Gets the value of the Stack Pointer (SP) register.
 int getXIndexValue()
          Gets the value of the X-Index (IX) register.
 int getYIndexValue()
          Gets the value of the Y-Index (IX) register.
 void powerOff()
          Called to tell the CPU that power is now off.
 void powerOn()
          Called to tell the CPU that power is now on.
 void reset()
          Called to tell the CPU that a reset interrupt was triggered.
 void setAccumulatorValue(int inValue)
          Sets the value of the Accumulator (ACC) register.
 void setProcessorStatusValue(int inValue)
          Sets the value of the Processor Status (PS) register.
 void setProgramCounterValue(int inValue)
          Sets the value of the Program Counter (PC) register.
 void setStackPointerValue(int inValue)
          Sets the value of the Stack Pointer (SP) register.
 void setXIndexValue(int inValue)
          Sets the value of the X-Index (IX) register.
 void setYIndexValue(int inValue)
          Sets the value of the Y-Index (IX) register.
 int testExecuteInstruction(int inOpCode, int inByte1, int inByte2)
          For use by the OpCode Test Shell.
 ExecutedInstructionInfo testStepInstruction()
          For use by the OpCode Test Shell.
 boolean willBranch(int inOpCode)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STACK_PAGE

public static int STACK_PAGE

NMI_VECTOR

public static int NMI_VECTOR

RESET_VECTOR

public static int RESET_VECTOR

IRQ_VECTOR

public static int IRQ_VECTOR
Constructor Detail

NesCpu

public NesCpu(NesMainMemory inMainMem,
              NesVideoMemory inVidMem)
Creates a new NesCpu.

Parameters:
inMainMem - Main memory bank accessed by this CPU.
inVidMem - Video memory bank accessed by this CPU.
Method Detail

powerOn

public void powerOn()
Called to tell the CPU that power is now on. This sets up register values and prepares the system for operation.


reset

public void reset()
Called to tell the CPU that a reset interrupt was triggered.


powerOff

public void powerOff()
Called to tell the CPU that power is now off. This clears register values and shuts down the system.


doCycle

public void doCycle()
Called to tell the CPU to do a cycle. Unlike most other cycle dependent hardware in the system, the CPU requires different numbers of cycles depending on what it's currently working on. This is all tracked internally.


getOpValue

public int getOpValue(int inOpCode,
                      int byte1,
                      int byte2)
Gets the parameter value based on the OpCode.

Parameters:
inOpCode - The OpCode whose mode will be used to determine the value.
byte1 - The first byte of the param used to lookup the full value.
byte2 - The second byte of the param used to lookup the full value.
Returns:
The final operation parameter value.
See Also:
getOpAddress(int,int,int)

getOpAddress

public int getOpAddress(int inOpCode,
                        int byte1,
                        int byte2)
Gets the address at which the parameter value is located. This method acts as the memory pointer counterpart to the function getOpValue(). (As that makes sense based on OpCode mode.)

Parameters:
inOpCode - The OpCode whose mode will be used to determine the value.
byte1 - The first byte of the param used to lookup the full value.
byte2 - The second byte of the param used to lookup the full value.
Returns:
The address of the final operation parameter value.
See Also:
getOpValue(int,int,int)

crossesPageBoundry

public boolean crossesPageBoundry(int inOpCode,
                                  int byte1,
                                  int byte2)
Determines whether or not an operation is going to cross a page boundry.

Parameters:
inOpCode - The OpCode whose mode will be used to determine the boundry crossing effect.
byte1 - The first byte of the param used.
byte2 - The second byte of the param used.
Returns:
Whether or not the operation crosses a page boundry and requires additional cycles.

willBranch

public boolean willBranch(int inOpCode)

testExecuteInstruction

public int testExecuteInstruction(int inOpCode,
                                  int inByte1,
                                  int inByte2)
For use by the OpCode Test Shell. This function will likely have trouble with jump and brance instructions. The number of cycles needed to complete the requested operation are automatically and instantly granted. The operation binary data is loaded at $0000-$0002 and will destroy whatever data is already there in the process. The data bytes are expected to be in exactly the same order as they would appear in a ROM.

Parameters:
inOpCode - The raw OpCode value.
inByte1 - The first byte of raw parameter data.
inByte2 - The second byte of raw parameter data.
Returns:
The number of cycles the operation took.
See Also:
OpCodeTestShellGui

testStepInstruction

public ExecutedInstructionInfo testStepInstruction()
For use by the OpCode Test Shell. This function does not alter any memory directly and will simply execute the next instruction as found in memory at the point currently referenced by the program counter.

Returns:
An ExecutedInstructionInfo object containing info about what was just executed.
See Also:
OpCodeTestShellGui

getProgramCounterValue

public int getProgramCounterValue()
Gets the value of the Program Counter (PC) register.

Returns:
The value of the PC register.

getStackPointerValue

public int getStackPointerValue()
Gets the value of the Stack Pointer (SP) register.

Returns:
The value of the SP register.

getAccumulatorValue

public int getAccumulatorValue()
Gets the value of the Accumulator (ACC) register.

Returns:
The value of the ACC register.

getXIndexValue

public int getXIndexValue()
Gets the value of the X-Index (IX) register.

Returns:
The value of the IX register.

getYIndexValue

public int getYIndexValue()
Gets the value of the Y-Index (IX) register.

Returns:
The value of the IX register.

getProcessorStatusValue

public int getProcessorStatusValue()
Gets the value of the Processor Status (PS) register.

Returns:
The value of the PS register.

setProgramCounterValue

public void setProgramCounterValue(int inValue)
Sets the value of the Program Counter (PC) register.

Returns:
The value of the PC register.

setStackPointerValue

public void setStackPointerValue(int inValue)
Sets the value of the Stack Pointer (SP) register.

Returns:
The value of the SP register.

setAccumulatorValue

public void setAccumulatorValue(int inValue)
Sets the value of the Accumulator (ACC) register.

Returns:
The value of the ACC register.

setXIndexValue

public void setXIndexValue(int inValue)
Sets the value of the X-Index (IX) register.

Returns:
The value of the IX register.

setYIndexValue

public void setYIndexValue(int inValue)
Sets the value of the Y-Index (IX) register.

Returns:
The value of the IX register.

setProcessorStatusValue

public void setProcessorStatusValue(int inValue)
Sets the value of the Processor Status (PS) register.

Returns:
The value of the PS register.

getNeedCycles

public int getNeedCycles()
Gets the number of cycles the CPU is waiting for before completing the next operation.

Returns:
The number of cycles the CPU requires to complete the next operation.

getAvailCycles

public int getAvailCycles()
Gets the number of cycles the CPU has waiting to be used by the next operation.

Returns:
The number of cycles the CPU has waiting to be used by the next operation.