com.cmpware.cmp
Class Processor

java.lang.Object
  extended by com.cmpware.cmp.Memory
      extended by com.cmpware.cmp.Processor
All Implemented Interfaces:
ReadWriteInterface
Direct Known Subclasses:
ALU, AutoModel, AutoModelVLIW, FastModel, FastModel_old, HardNode, MicroBlaze_old, NIOS, NIOS2, Null, Sparc

public abstract class Processor
extends Memory

This implements the generic processor interface and mechanisms. Note that this model assumes fixed length instructions. For variable length instructions, or a simpler interface, see AutoModel.

Copyright (c) 2004, 2005, 2006 Cmpware, Inc. All Rights Reserved.

Author:
SAG

Field Summary
protected  byte[] bkpt
          The breakpoint instruction
protected  int branchAddr
          The branch address
protected  int branchDelay
          The branch delay slot size
protected  boolean breakpointHit
          The Breakpoint hit flag
static java.lang.String copyright
          Copyright string
protected  int currentBranchDelay
          The current branch delay value
protected  int currentInstr
          The current instruction
protected  int currentInstrCode
          The current instruction code
protected  long cycleCount
          The cycle count
protected  Elf elf
          The most recently loaded ELF file
protected  float idlePower
          The power consumption while the processor is idle (mW)
protected  int[] iMix
          The instruction mix
protected  int instructionSize
          The instruction size (in bytes)
protected  java.lang.String name
          The processor name
protected  byte[] noop
          The noop instruction
protected  java.lang.String[] opcodeName
          The Opcode names
protected  int pc
          The current Program Counter (cached from getPC())
protected  int prevInstr
          The previous instruction
protected  int prevInstrCode
          The previous instruction code
protected  int[] r
          The General Purpose register set
protected  java.lang.String[] regName
          The General Purpose Register names
protected  float runningPower
          The power consumption while the processor is running (mW)
protected  int[] sr
          The special purpose register set
protected  java.lang.String[] sregName
          The Special Purpose Register names
protected  boolean stall
          The stall flag
protected  long stallCount
          The Stall cycle count
 
Fields inherited from class com.cmpware.cmp.Memory
BIG, LITTLE
 
Constructor Summary
Processor()
           
 
Method Summary
 void advancePC()
          This method advances the Program Counter.
 void branch(int branchAddr)
          This method should be called whenever an instruction branches.
 void branchNoDelay(int branchAddr)
          This method should be called whenever an instruction branches and the delay slot is not executed.
abstract  java.lang.String dasm(byte[] instr)
          This method disassembles an instruction.
abstract  int decode(int instr)
          A unique code identifying the instruction is returned by this method.
 void defineBranchDelay(int d)
          This method defines the size of the branch delay slot.
 void defineBreakpoint(byte[] bkpt)
          This method defines the code used as a breakpoint.
 void defineInstructionSize(int instructionSize)
          This method defines the instruction size, in bytes.
 void defineName(java.lang.String name)
          This method defines a sinple string identifying the processor.
 void defineNoop(byte[] n)
          This method defines the code used as a noop.
 void defineOpcodeNames(java.lang.String[] opcodeName)
          This method defines the opcode names used by the processor.
 void defineRegisterNames(java.lang.String[] regName)
          This method defines the general purpose register names used by the processor.
 void defineRegisters(int i)
          This method defines the number of the general purpose registers used by the processor.
 void defineSpecialRegisterNames(java.lang.String[] sregName)
          This method defines the special purpose register names used by the processor.
 void defineSpecialRegisters(int i)
          This method defines the number of the special purpose registers used by the processor.
abstract  void execute(int instr)
          This method is called after the fetch() and decode() and represents the execution phase of the instruction.
 void fetch()
          This method fetches an instruction from memory and updates the currentInstr and prevInstr.
static Processor get(java.lang.String processorName)
          This static method is used to return a newly allocated Processor object from the processor name string.
 byte[] getBreakpoint()
          This method returns the code used as a breakpoint.
 byte[] getBreakpointData(int addr)
          This method returns the byte array that was replaced by the breakpoint.
 int[] getBreakpoints()
          This method returns an array of Integer objects that give the addresses of all set breakpoints.
 long getCycleCount()
          This method returns the current cycle count.
 int[] getDebugRegisters()
          This method returns the current values of all of the general purpose registers as they are used by a debugger.
 Elf getElf()
          This method returns the most recently loaded ELF file.
 float getIdlePower()
          This method returns the power consumption in mW of the processor while idle (stalled).
 byte[] getInstruction(int addr)
          This method fetches an instruction from memory.
 int[] getInstructionMix()
          This method returns an array of integers giving the instruction mix.
 int getInstructionSize()
          This method returns the instruction size in bytes.
 java.lang.String getName()
          This method returns a simple string identifying the processor.
 byte[] getNoop()
          This method returns the code used as a NOOP.
 java.lang.String[] getOpcodeNames()
          This method returns the names of all of the opcodes.
abstract  int getPC()
          This method returns the current Program Counter.
 int getRegister(int i)
          This method returns a general purpose register.
 java.lang.String[] getRegisterNames()
          This method returns the names of all of the General Purpose registers.
 int[] getRegisters()
          This method returns the current values of all of the general purpose registers.
 float getRunningPower()
          This method returns the power consumption in mW of the processor while running.
 int getSpecialRegister(int i)
          This method returns a Special Purpose register.
 java.lang.String[] getSpecialRegisterNames()
          This method returns the names of all of the special purpose registers.
 int[] getSpecialRegisters()
          This method returns the current values of all of the special purpose registers.
 long getStallCount()
          This method returns the current stall cycle count.
 boolean isBreakpoint(int addr)
          This method returns true if the address contains a breakpoint and a false otherwise.
 boolean isStalled()
          This method returns the current stall state of the processor.
 void load(Elf elf, int offset)
          This method loads the memory of the processor with data from the ELF file.
 void postLoad()
          This method it used to perform any operations that may be required after a file is loaded.
 void removeAllBreakpoints()
          This method removes all breakpoints.
 void removeBreakpoint(int addr)
          This method removes a breakpoint.
 void reset()
          This method resets the processor.
 void setBreakpoint(int addr)
          This method sets a breakpoint.
 void setIdlePower(float idlePower)
          This method sets the power consumption in mW of the processor while idle (stalled).
abstract  void setPC(int pc)
          This method sets the current Program Counter.
 void setRegister(int i, int a)
          This method sets a general purpose register.
 void setRunningPower(float runningPower)
          This method returns the power consumption in mW of the processor while running.
 void setSpecialRegister(int i, int a)
          This method sets a special purpose register.
 void stall()
          This method is used to stall the processor.
 void statistics()
          This method keeps various statistics about the most recently exercuted instruction.
 void step(long globalCycleCount)
          This method steps the simulation one cycle.
 
Methods inherited from class com.cmpware.cmp.Memory
addInput, addMemory, addOutput, getEndian, getLocalMemory, getMemoryManager, getMmio, getProfile, hexDump, hexDump, isReadable, isValidAddress, isWriteable, profile, read, read, read16, read24, read32, read64, removeMemory, resize, setEndian, size, toInt, toShort, write, write, write16, write24, write32, write64
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

copyright

public static final java.lang.String copyright
Copyright string

See Also:
Constant Field Values

breakpointHit

protected boolean breakpointHit
The Breakpoint hit flag


name

protected java.lang.String name
The processor name


instructionSize

protected int instructionSize
The instruction size (in bytes)


branchDelay

protected int branchDelay
The branch delay slot size


currentBranchDelay

protected int currentBranchDelay
The current branch delay value


branchAddr

protected int branchAddr
The branch address


stall

protected boolean stall
The stall flag


cycleCount

protected long cycleCount
The cycle count


stallCount

protected long stallCount
The Stall cycle count


iMix

protected int[] iMix
The instruction mix


regName

protected java.lang.String[] regName
The General Purpose Register names


sregName

protected java.lang.String[] sregName
The Special Purpose Register names


opcodeName

protected java.lang.String[] opcodeName
The Opcode names


bkpt

protected byte[] bkpt
The breakpoint instruction


noop

protected byte[] noop
The noop instruction


currentInstr

protected int currentInstr
The current instruction


prevInstr

protected int prevInstr
The previous instruction


currentInstrCode

protected int currentInstrCode
The current instruction code


prevInstrCode

protected int prevInstrCode
The previous instruction code


pc

protected int pc
The current Program Counter (cached from getPC())


idlePower

protected float idlePower
The power consumption while the processor is idle (mW)


runningPower

protected float runningPower
The power consumption while the processor is running (mW)


r

protected int[] r
The General Purpose register set


sr

protected int[] sr
The special purpose register set


elf

protected Elf elf
The most recently loaded ELF file

Constructor Detail

Processor

public Processor()
Method Detail

decode

public abstract int decode(int instr)
                    throws IllegalOpcodeException,
                           MemoryAccessException
A unique code identifying the instruction is returned by this method. In some processors with simple fixed-field opcodes this may be a very simple method to define. Other architectures with more complex decoding will require a more complex implementation.

Parameters:
instr - The instruction to be decoded.
Returns:
A unique code identifying the instruction is returned by this method.
Throws:
IllegalOpcodeException - if an illegal opcode is encountered.
MemoryAccessException - A Memory Access Exception is thrown is an illegal memory access is attempted.

execute

public abstract void execute(int instr)
                      throws MemoryAccessException,
                             IllegalRegisterException,
                             IllegalOpcodeException
This method is called after the fetch() and decode() and represents the execution phase of the instruction. All update of processor state occurs in this method. This is where most of the processor definition will occur. See examples for suggestions on implementation of this method.

Parameters:
instr - The instruction to be executed.
Throws:
MemoryAccessException - A Memory Access Exception is thrown is an illegal memory access is attempted.
IllegalRegisterException - if an attempt is made to access a register illegally.
IllegalOpcodeException - is added for models which may combine decode and execute in the same method (such as AutoModel).

getPC

public abstract int getPC()
This method returns the current Program Counter.

Returns:
This method returns the current Program Counter.

setPC

public abstract void setPC(int pc)
This method sets the current Program Counter.


dasm

public abstract java.lang.String dasm(byte[] instr)
This method disassembles an instruction. Note that a byte array is provided as the interface. This is to make this method as generic as possible.

Parameters:
instr - The instruction to be disassembled.
Returns:
This method returns the disassembled instruction.

defineName

public void defineName(java.lang.String name)
This method defines a sinple string identifying the processor. This is usually done once, at initialization.

Parameters:
name - The processor name.

getName

public java.lang.String getName()
This method returns a simple string identifying the processor.

Returns:
This method returns a simple string identifying the processor.

defineInstructionSize

public void defineInstructionSize(int instructionSize)
This method defines the instruction size, in bytes. It is usually called once, during initialization.

Parameters:
instructionSize - The instruction size, in bytes.

getInstructionSize

public int getInstructionSize()
This method returns the instruction size in bytes. A fixed instruction width is assumed.

Returns:
This method returns the instruction size in bytes.

defineRegisters

public void defineRegisters(int i)
This method defines the number of the general purpose registers used by the processor. It is usually called once, during initialization.

Parameters:
i - The number of general purpose registers.

defineRegisterNames

public void defineRegisterNames(java.lang.String[] regName)
This method defines the general purpose register names used by the processor. It is usually called once, during initialization.

Parameters:
regName - An array of strings representing the General Purpose Register names.

getRegisterNames

public java.lang.String[] getRegisterNames()
This method returns the names of all of the General Purpose registers.

Returns:
This method returns the names of all of the general purpose registers.

getRegisters

public int[] getRegisters()
This method returns the current values of all of the general purpose registers.

Returns:
This method returns the current values of all of the general purpose registers.

getDebugRegisters

public int[] getDebugRegisters()
This method returns the current values of all of the general purpose registers as they are used by a debugger. This is added for machines that (for instance) have 128 bit registers of which the debugger only uses 32 bits. This is intended to be overloaded in these situations. This method is used exclusively by debuggers and debugger-like tools.

Returns:
This method returns the current values of all of the general purpose registers.

setRegister

public void setRegister(int i,
                        int a)
                 throws IllegalRegisterException
This method sets a general purpose register.

Parameters:
i - The index of the general purpose register to set.
a - The value to set the register to.
Throws:
IllegalRegisterException - if an attempt is made to access a register illegally.

getRegister

public int getRegister(int i)
                throws IllegalRegisterException
This method returns a general purpose register.

Parameters:
i - The index of the general purpose register to get.
Throws:
IllegalRegisterException - if an attempt is made to access a register illegally.

defineSpecialRegisters

public void defineSpecialRegisters(int i)
This method defines the number of the special purpose registers used by the processor. It is usually called once, during initialization.

Parameters:
i - The number of special purpose registers.

defineSpecialRegisterNames

public void defineSpecialRegisterNames(java.lang.String[] sregName)
This method defines the special purpose register names used by the processor. It is usually called once, during initialization.

Parameters:
sregName - An array of strings representing the Special Purpose Register names.

getSpecialRegisterNames

public java.lang.String[] getSpecialRegisterNames()
This method returns the names of all of the special purpose registers.

Returns:
This method returns the names of all of the special purpose registers.

getSpecialRegisters

public int[] getSpecialRegisters()
This method returns the current values of all of the special purpose registers.

Returns:
This method returns the current values of all of the special purpose registers.

setSpecialRegister

public void setSpecialRegister(int i,
                               int a)
                        throws IllegalRegisterException
This method sets a special purpose register.

Parameters:
i - The index of the special purpose register to set.
a - The value to set the register to.
Throws:
IllegalRegisterException - if an attempt is made to access a register illegally.

getSpecialRegister

public int getSpecialRegister(int i)
                       throws IllegalRegisterException
This method returns a Special Purpose register.

Parameters:
i - The index of the special purpose register to get.
Throws:
IllegalRegisterException - if an attempt is made to access a register illegally.

defineOpcodeNames

public void defineOpcodeNames(java.lang.String[] opcodeName)
This method defines the opcode names used by the processor. The entry at each index in the array should map to the associated instruction code. This method is usually called once, during initialization.

Parameters:
opcodeName - An array of strings representing the opcode names.

getOpcodeNames

public java.lang.String[] getOpcodeNames()
This method returns the names of all of the opcodes.

Returns:
This method returns the names of all of the opcodes.

defineBreakpoint

public void defineBreakpoint(byte[] bkpt)
This method defines the code used as a breakpoint. This may be a dedicated code in some CPUs or simply a particular illegal opcode.

Parameters:
bkpt - The breakpoint instruction.

getBreakpoint

public byte[] getBreakpoint()
This method returns the code used as a breakpoint. This may be a dedicated code in some CPUs or simply a particular illegal opcode.

Returns:
This method returns the code used as a breakpoint.

defineNoop

public void defineNoop(byte[] n)
This method defines the code used as a noop. This may be a dedicated code in some CPUs or simply a particular operation that does not change the processor state. This is used primarily in the disassembly.

Parameters:
n - The noop instruction.

getNoop

public byte[] getNoop()
This method returns the code used as a NOOP. This is mostly used as an aid in disassemblies.

Returns:
This method returns the code used as a NOOP.

defineBranchDelay

public void defineBranchDelay(int d)
This method defines the size of the branch delay slot. The default is zero.

Parameters:
d - The size of the branch delay slot.

getCycleCount

public long getCycleCount()
This method returns the current cycle count.

Returns:
This method returns the current cycle count.

getStallCount

public long getStallCount()
This method returns the current stall cycle count.

Returns:
This method returns the current stall cycle count.

getInstructionMix

public int[] getInstructionMix()
This method returns an array of integers giving the instruction mix. Each entry in the array is a count of the number of times a particular instruction has been executed. Note that the array indicies may correspond to the actual opcodes, but in some architectures, they may not. Use the method getOpcodeNames() to see the which instruction corresponds to which entry.

Returns:
This method returns an array of integers giving the instruction mix.

getIdlePower

public float getIdlePower()
This method returns the power consumption in mW of the processor while idle (stalled).

Returns:
This method returns the power consumption in mW of the processor while idle (stalled).

setIdlePower

public void setIdlePower(float idlePower)
This method sets the power consumption in mW of the processor while idle (stalled).


getRunningPower

public float getRunningPower()
This method returns the power consumption in mW of the processor while running.

Returns:
This method returns the power consumption in mW of the processor while running.

setRunningPower

public void setRunningPower(float runningPower)
This method returns the power consumption in mW of the processor while running.


fetch

public void fetch()
           throws MemoryAccessException
This method fetches an instruction from memory and updates the currentInstr and prevInstr. Note that breakpoints are taken into account. If the previous instruction executed was a breakpoint, the actual instruction is fetched from the saved value in the breakpoint table.

Throws:
a - Memory Access Exception is thrown if the fetch is to an illegal memory address.
MemoryAccessException

getInstruction

public byte[] getInstruction(int addr)
                      throws MemoryAccessException
This method fetches an instruction from memory. Note that breakpoints are taken into account. If the instruction is a breakpoint, the actual instruction is fetched from the saved value in the breakpoint table. Note that this is generally for off-line use and not a replacement for the fetch() method.

Parameters:
addr - The address at which to fetch the instruction.
Throws:
a - Memory Access Exception is thrown if the fetch is to an illegal memory address.
MemoryAccessException

step

public void step(long globalCycleCount)
          throws MemoryAccessException,
                 IllegalOpcodeException,
                 IllegalRegisterException,
                 BreakpointException
This method steps the simulation one cycle.

Parameters:
globalCycleCount - This is the clock cycle as maintained by the multiprocessor. If the processor itself has gotten ahead of this value this method simply returns. The local cycle count should never be less than the global cycle count.
Throws:
A - Memory Access Exception is thrown is an illegal memory access is attempted.
A - Memory Access Exception is thrown is an illegal memory access is attempted.
MemoryAccessException - A Memory Access Exception is thrown is an illegal memory access is attempted.
IllegalOpcodeException - if an attempt is made to execute an illegal opcode.
IllegalRegisterException - if an attempt is made to access a register illegally.
BreakpointException - if a breakpoint is encountered on this cycle.

advancePC

public void advancePC()
This method advances the Program Counter. It takes into account branches and stalls. This is provided as a seperate method (rather than being included as part of step()) to permit it to be overloaded.


reset

public void reset()
This method resets the processor. Classes which inherit from Processor may want to define their own processor-specifif reset() method, but it should call this methos first via super.reset().


stall

public void stall()
This method is used to stall the processor.


isStalled

public boolean isStalled()
This method returns the current stall state of the processor.


statistics

public void statistics()
This method keeps various statistics about the most recently exercuted instruction. This includes cycle counts, memory access profiling and instruction mix profiling.


branch

public void branch(int branchAddr)
This method should be called whenever an instruction branches. Use of this method will ensure that proper delay slotting is performed.

Parameters:
branchAddr - The branch address.

branchNoDelay

public void branchNoDelay(int branchAddr)
This method should be called whenever an instruction branches and the delay slot is not executed.

Parameters:
branchAddr - The branch address.

setBreakpoint

public void setBreakpoint(int addr)
                   throws MemoryAccessException
This method sets a breakpoint.

Parameters:
addr - The address at which to set the breakpoint.
Throws:
MemoryAccessException - if an attempt is bade to set a breakpoint at an illegal memory address.

removeBreakpoint

public void removeBreakpoint(int addr)
This method removes a breakpoint. If the breakpoint does not exist this method returns silently.

Parameters:
addr - The address of the breakpoint.

getBreakpoints

public int[] getBreakpoints()
This method returns an array of Integer objects that give the addresses of all set breakpoints.

Returns:
This method returns an array of Integer objects that give the addresses of all set breakpoints.

removeAllBreakpoints

public void removeAllBreakpoints()
This method removes all breakpoints.


isBreakpoint

public boolean isBreakpoint(int addr)
This method returns true if the address contains a breakpoint and a false otherwise.

Parameters:
addr - The address of the breakpoint.
Returns:
This method returns true if the address contains a breakpoint and a false otherwise.

getBreakpointData

public byte[] getBreakpointData(int addr)
This method returns the byte array that was replaced by the breakpoint. If there is no breakpoint at this address a null is returned.

Parameters:
addr - The address of the breakpoint.
Returns:
This method returns the byte array that was replaced by the breakpoint. If there is no breakpoint at this address a null is returned.

load

public void load(Elf elf,
                 int offset)
          throws MemoryAccessException
This method loads the memory of the processor with data from the ELF file. If the data is not in ELF format, it is assumed to be a raw binary and is loaded at the address given by the offset parameter. The offset parameter is ignored if the file is in ELF format. This method also sets the program counter to the entry point for an ELF file and to the offset for a raw binary.

Parameters:
elf - The ELF data to be loaded.
offset - The address at which to load a non-ELF (raw binary) file. This parameter is ignored for data in proper ELF format.
Throws:
MemoryAccessException - if an attempt is made to illegally access memory.

getElf

public Elf getElf()
This method returns the most recently loaded ELF file. This is useful for getting DWARF debug data.

Returns:
This method returns the most recently loaded ELF file. This is useful for getting DWARF debug data.

postLoad

public void postLoad()
This method it used to perform any operations that may be required after a file is loaded. In the PowerPC, for instance, this method sets the TOC address for ELF files which use a TOC section. This method currently does nothing and must be overloaded by specific architectures.


get

public static Processor get(java.lang.String processorName)
                     throws ProcessorException
This static method is used to return a newly allocated Processor object from the processor name string.

Parameters:
processorName - The name of the processor. This is the class name of the Link class file under com.cmpware.cmp.models.
Returns:
This method returns a newly created Processor object from the link name string.
Throws:
ProcessorException - if any problems we encountered allocating the new Processorobject.