miSim

misim
Class Processor

java.lang.Object
  |
  +--misim.MachineState
        |
        +--misim.Processor
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
PIC16X84

public abstract class Processor
extends MachineState
implements java.lang.Runnable

The Processor class extends MachineState to provide functions for managing the contents of the microcontroller's program memory and running the emulation. The Processor essentially manages the overall knowledge of the microcontroller's architecture - the instruction set, program memory and execution of instructions in sequence.

The Processor class must be extended by a specific Processor implementation to provide details unique to that Processor - such as memory space, instruction set and so on.


Field Summary
 int programMemorySize
          This specifies the instruction memory size
static int RUN_ACCURATELY
          Constant that defines that the processor should attempt to emulate a specific clock speed.
static int RUN_FAST
          Constant that defines that the processor should run as fast as possible, at the cost of some accuracy
static int RUN_OVER
          Constant that defines that the processor should emulate a single instruction, or in the case of a CALL instruction, a single called routine
static int RUN_SPEED
          Constant that defines that the processor should attempt to emulate a specific clock speed.
static java.lang.String SETTING_CLOCK
           
static java.lang.String SETTING_FAST_LOOP
           
static java.lang.String SETTING_INTERRUPT
           
static java.lang.String SETTING_RUN_TYPE
           
static int STATE_CLEAR
           
static int STATE_HALTED
           
static int STATE_LOADED
           
static int STATE_RUNNING
           
static int STATE_STOPPED
           
 
Fields inherited from class misim.MachineState
aliases, carryBit, chipData, clockListener, clockRate, clocks, CONFIG_CP, CONFIG_DP, CONFIG_OSC4, CONFIG_PWRTE, CONFIG_PWRTEI, CONFIG_WDTE, configStart, decimalBit, deviceId, eepromStart, instructionSet, PACKAGE_DIP, PACKAGE_PLCC, PACKAGE_QFP, PACKAGE_SOIC, PARAM_BANK_SIZE, PARAM_BANKS, PARAM_BIT_CP0, PARAM_BIT_DP0, PARAM_BIT_FOSC0, PARAM_BIT_FOSC1, PARAM_BIT_PWRTE, PARAM_BIT_PWRTEI, PARAM_BIT_WDTE, PARAM_CONFIG_ADDR, PARAM_CONFIG_START, PARAM_CONFIG_WORD, PARAM_DATA_SIZE, PARAM_DEVICES, PARAM_EEPROM_SIZE, PARAM_EEPROM_START, PARAM_ID_ADDR, PARAM_ID_SIZE, PARAM_ID_WIDTH, PARAM_INSTR_WIDTH, PARAM_PACKAGES, PARAM_PINS, PARAM_PROG_SIZE, PARAM_STACK_SIZE, pc, regOffset, regs, RESET_AS_PROGRAMMED, RESET_MCLR, RESET_POWER_UP, RESET_WATCHDOG, sleep, stack, stackPtr, trisBankOffset, w, wdTimer, zeroBit
 
Constructor Summary
Processor(SettingsGroup settings, java.lang.String deviceName, InstructionSet instructionSet, Aliases aliases)
          Constructor for the processor, Creates eeprom areas for instructions and data, and resets the machine state
 
Method Summary
 void addProcessorListener(ProcessorListener pl)
          Add a processor listener to this processor.
 int addressWidth(int address)
          Return the bit width of the given address word.
 java.lang.String disassemble(int address)
          Return a disassembly of the instruction at the given address.
 void eraseAll()
          Set all memory back to a null state.
 boolean fastLoop()
           
 int getConfig()
          Get the configuration word for this device
 int getCpuSpeed()
          Returns the cpu speed, in KHz, achieved during the last run.
 int getDataSize()
          Returns the size of array needed to store the eeprom data loaded.
 int getMemSize()
          Returns the size of array needed to store the instructions loaded.
 int getRunType()
          Get the run type, one of the RUN_ constants.
 int getState()
          Method to retrieve the state of this processor
 boolean interruptOnStep()
          Return the value of the Interrupt On Step flag, which indicates if interrupts should be emulated for the Step and Over functions.
 boolean isAddressValid(int address)
          Return true if this address is valid - ie.
 int loadBinary(java.io.InputStreamReader input, LogListener logging)
          Read a binary file, initialising the processor memory and instruction arrays as appropriate
 void optimise()
          Optimise the loaded binary by looking for 'shortcuts' for emulation.
 void overPic()
          Single step the processor, unless the command is a call, in which case execute the entire called routine.
 Instruction readInstruction(int address)
          Read an instruction from the given address.
 void removeProcessorListener(ProcessorListener pl)
          Remove a processor listener from this processor.
 void reset(int resetType)
          Reset the processor, including the data stored in the eeprom, to the values originally loaded.
 void resetData()
           
 void run()
          Execute some pic instructions depending on the current run type.
 void runAccurately()
          Try to run full emulation at an accurate speed, including any clock listeners.
 void runFastest()
          Run the emulator as fast as possible, at the expense of some accuracy.
 void runOver()
          Run the emulator until a given breakpoint is reached.
 void runPic()
          Initialise the emulator to run for an indefinite time, then call startThread() to run the emulator in a separate thread.
 void runToSpeed()
          This method attempts to run the processor at a given clock speed.
 void setConfig(int configWord)
          Set the configuration word for this device
 void setEeprom(int address, Instruction instr)
           
 void setFastLoop(boolean newValue)
           
 void setInterruptOnStep(boolean newValue)
          Set the value of the Interrupt On Step flag, which indicates if interrupts should be emulated for the Step and Over functions.
 void setPC(int pc)
          Set a new value for the Program Counter.
 void setRunType(int runType)
          Set the run type for emulation, one of the RUN_ constants.
 void setState(int newState)
          Set the processor state to a new value
protected  void startThread()
          Start the processor running in a separate thread.
 void stepPic()
          Single step the processor, executing a single instruction.
 void stopThread()
           
 void validateAddress(int address)
          Validate that the given address has been initialised correctly.
 boolean writeData(int address, int value)
          Write byte data to the chip.
 void writeInstruction(int address, Instruction instruction)
          Write an instruction to the given address in this processor.
 
Methods inherited from class misim.MachineState
addClockListener, addPortListener, addPortListener, addResetListener, addressName, disassemble, enableGlobalInterrupts, eraseEeprom, getAliases, getAvailablePorts, getClockRate, getClocks, getDeviceId, getDeviceName, getInstructionSet, getNewInstruction, getNthRegAddress, getNthRegName, getParameter, getParameter, getPC, getPinName, getPort, getPortPin, getReg, getTicks, getW, isNamed, namedRegisters, regChar, regName, regString, removeClockListener, removePortListener, removeResetListener, resetMachine, resetRegs, setAddresses, setAddressName, setAliases, setClockListener, setClockRate, setClocks, setInstructionSet, setReg, setRegisters, setRegName, setW, validReg
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

programMemorySize

public int programMemorySize
This specifies the instruction memory size

STATE_CLEAR

public static final int STATE_CLEAR

STATE_LOADED

public static final int STATE_LOADED

STATE_RUNNING

public static final int STATE_RUNNING

STATE_STOPPED

public static final int STATE_STOPPED

STATE_HALTED

public static final int STATE_HALTED

SETTING_CLOCK

public static final java.lang.String SETTING_CLOCK

SETTING_RUN_TYPE

public static final java.lang.String SETTING_RUN_TYPE

SETTING_INTERRUPT

public static final java.lang.String SETTING_INTERRUPT

SETTING_FAST_LOOP

public static final java.lang.String SETTING_FAST_LOOP

RUN_FAST

public static final int RUN_FAST
Constant that defines that the processor should run as fast as possible, at the cost of some accuracy

RUN_SPEED

public static final int RUN_SPEED
Constant that defines that the processor should attempt to emulate a specific clock speed.

RUN_ACCURATELY

public static final int RUN_ACCURATELY
Constant that defines that the processor should attempt to emulate a specific clock speed.

RUN_OVER

public static final int RUN_OVER
Constant that defines that the processor should emulate a single instruction, or in the case of a CALL instruction, a single called routine
Constructor Detail

Processor

public Processor(SettingsGroup settings,
                 java.lang.String deviceName,
                 InstructionSet instructionSet,
                 Aliases aliases)
          throws MiSimException
Constructor for the processor, Creates eeprom areas for instructions and data, and resets the machine state
Method Detail

eraseAll

public void eraseAll()
Set all memory back to a null state. This clears all data previously loaded into the processor, including instructions, config and eeprom data

reset

public void reset(int resetType)
Reset the processor, including the data stored in the eeprom, to the values originally loaded.

resetData

public void resetData()

setPC

public void setPC(int pc)
           throws MiSimException
Set a new value for the Program Counter. This method overrides the method in MachineState to include checking for valid address values for this device.
Overrides:
setPC in class MachineState
Parameters:
pc - The new value for the program counter
Throws:
MiSimException - if the new address is invalid

setEeprom

public void setEeprom(int address,
                      Instruction instr)

addProcessorListener

public void addProcessorListener(ProcessorListener pl)
Add a processor listener to this processor.
Parameters:
pl - - The processor listener to be added

removeProcessorListener

public void removeProcessorListener(ProcessorListener pl)
Remove a processor listener from this processor.
Parameters:
pl - - The processor listener to be removed

getState

public int getState()
Method to retrieve the state of this processor
Returns:
The state of this processor which will be one of STATE_CLEAR The processor has no program loaded STATE_LOADED A program and associated data is loaded STATE_RUNNING The processor is running the loaded program STATE_STOPPED The running program has been stopped STATE_HALTED The running program terminated unexpectedly.

setState

public void setState(int newState)
Set the processor state to a new value
Parameters:
newState - The new state for the processor
See Also:
getState()

interruptOnStep

public boolean interruptOnStep()
Return the value of the Interrupt On Step flag, which indicates if interrupts should be emulated for the Step and Over functions.
Returns:
True if interrupts are emulated

setInterruptOnStep

public void setInterruptOnStep(boolean newValue)
Set the value of the Interrupt On Step flag, which indicates if interrupts should be emulated for the Step and Over functions.
Parameters:
newValue - True if interrupts should be emulated

fastLoop

public boolean fastLoop()

setFastLoop

public void setFastLoop(boolean newValue)

setConfig

public void setConfig(int configWord)
Set the configuration word for this device

getConfig

public int getConfig()
Get the configuration word for this device

writeData

public boolean writeData(int address,
                         int value)
Write byte data to the chip. The address is the BYTE address of the data. This method updates the hidata and himem values as appropriate. If data is written to the instruction space, the relevant instruction is cleared. Either writeInstruction or decodeFile can be used to fix this.
Parameters:
address - The byte address of data to write
value - The byte of data to write
Returns:
True if the address to be written was valid

addressWidth

public int addressWidth(int address)
Return the bit width of the given address word. Note that the address is a WORD address, not byte address as used by writeData
Parameters:
address - The word address to find the bit width
Returns:
The bit width of the word at the given address

writeInstruction

public void writeInstruction(int address,
                             Instruction instruction)
Write an instruction to the given address in this processor. Note that the address is a WORD address, not byte address as used by writeData
Parameters:
address - The word address of the instruction
instruction - The instruction to write

validateAddress

public void validateAddress(int address)
                     throws MiSimException
Validate that the given address has been initialised correctly. If this address should contain an instruction, check that one has been created, and if not, create one from the data stored for that address. Note that the address is a WORD address, as for writeInstruction.
Parameters:
address - The word address to validate.

readInstruction

public Instruction readInstruction(int address)
Read an instruction from the given address. Note that the address is a WORD address, not byte address as used by writeData
Parameters:
address - The word address of the instruction
Returns:
The instruction at that address

loadBinary

public int loadBinary(java.io.InputStreamReader input,
                      LogListener logging)
               throws MiSimException,
                      java.io.FileNotFoundException
Read a binary file, initialising the processor memory and instruction arrays as appropriate
Parameters:
input - The file InputStreamReader
Throws:
MiSimException - If an error is found in the file
java.io.FileNotFoundException - If the file cannot be loaded

getMemSize

public int getMemSize()
Returns the size of array needed to store the instructions loaded.
Returns:
The maximum instruction address

getDataSize

public int getDataSize()
Returns the size of array needed to store the eeprom data loaded.
Returns:
The number of eeprom bytes loaded

getCpuSpeed

public int getCpuSpeed()
Returns the cpu speed, in KHz, achieved during the last run.
Returns:
The cpu speed for the last run

optimise

public void optimise()
Optimise the loaded binary by looking for 'shortcuts' for emulation. The main shortcut is looking for delay loops (a decfszf followed by a goto back to the decfszf instruction). These can be emulated by calculating the delay and altering the clock count as appropriate. This routine sets a flag in the decfszf command to allow it to do a quick loop emulation.

getRunType

public int getRunType()
Get the run type, one of the RUN_ constants.
Returns:
The current default run type

setRunType

public void setRunType(int runType)
Set the run type for emulation, one of the RUN_ constants.
Parameters:
runType - The new run type

run

public void run()
Execute some pic instructions depending on the current run type. This allows execution to be carried out in a separate thread, so that the user interface can respond to user events.
Specified by:
run in interface java.lang.Runnable

runFastest

public void runFastest()
Run the emulator as fast as possible, at the expense of some accuracy. This simply calls instructions in sequence without performing any other checks - such as breakpoints, or interrupts

Note that the calling method must catch SleepExceptions and any other unexpected Exceptions that might occur. The latter can be caused by plugins that are called during the emulation, so we cannot presume that they will not occur.


runOver

public void runOver()
Run the emulator until a given breakpoint is reached. As the breakpoint is checked after each instruction, this runs somewhat slower than the runFastest() method.

runToSpeed

public void runToSpeed()
This method attempts to run the processor at a given clock speed.

It tries to do this by executing a number of instructions that should complete in a known time. Then, the real time elapsed is checked, and the system pauses until the time elapsed is correct for the clock count. This should mean that the processor will always run at approximately the correct speed regardless of how fast the JVM executes. However, it also means that emulation is 'choppy', and the emulated clock speed is only accurate on average.


runAccurately

public void runAccurately()
Try to run full emulation at an accurate speed, including any clock listeners.

stepPic

public void stepPic()
Single step the processor, executing a single instruction.

overPic

public void overPic()
Single step the processor, unless the command is a call, in which case execute the entire called routine. Any called routine is emulated in the run thread so that should the processor run away, the GUI can be used to halt emulation.

runPic

public void runPic()
Initialise the emulator to run for an indefinite time, then call startThread() to run the emulator in a separate thread.
See Also:
startThread()

startThread

protected void startThread()
Start the processor running in a separate thread.

stopThread

public void stopThread()

disassemble

public java.lang.String disassemble(int address)
Return a disassembly of the instruction at the given address.
Parameters:
address - The address to disassemble
Returns:
The disassembled instruction at this address

isAddressValid

public boolean isAddressValid(int address)
Return true if this address is valid - ie. it is within the address range of the processor and has a decoded instruction stored there.
Parameters:
address - The address to validate
Returns:
true if the address is valid

miSim