avrora.sim
Class Simulator

java.lang.Object
  extended byavrora.sim.Simulator
All Implemented Interfaces:
IORegisterConstants
Direct Known Subclasses:
ATMega128L.SimImpl

public abstract class Simulator
extends java.lang.Object
implements IORegisterConstants

The Simulator class implements a full processor simulator for the AVR instruction set. It is the base class of specific implementations that implement processor-specific behavior.

See Also:
Program, Instr, BaseInterpreter

Nested Class Summary
static class Simulator.BreakPointException
          The BreakPointException is an exception that is thrown by the simulator before it executes an instruction which has a breakpoint.
 class Simulator.ClockCycleTimeout
          The InstructionCountTimeout class is a probe that simply counts down and throws an exception when the count reaches zero.
static interface Simulator.Event
          The Simulator.Event interface represents an event that is fired when a timed event occurs within the simulator.
static class Simulator.InstructionCountTimeout
          The InstructionCountTimeout class is a probe that simply counts down and throws a TimeoutException when the count reaches zero.
static interface Simulator.Interrupt
          The Interrupt interface represents the behavior of an interrupt (how it manipulates the state of the processor) when it is posted and when it is triggered (handler is executed by the processor).
 class Simulator.Printer
          The Simulator.Printer class is a printer that is tied to a specific Simulator instance.
static interface Simulator.Probe
          The Simulator.Probe interface represents a programmer-defined probe that can be inserted at a particular instruction in the program. or at every instruction.
static class Simulator.TimeoutException
          The TimeoutException is thrown by the simulator when a timeout reaches zero.
static interface Simulator.Watch
          The MemoryProbe interface represents a user probe that is fired when a watchpoint detects an access to an address where this memory probe has been inserted.
 
Field Summary
protected  MainClock clock
          The clock field stores a reference to the MainClock instance that tracks the clock cycles that have passed for this simulator.
protected  InterpreterFactory factory
          The factory field stores a reference to the InterpreterFactory which should be used to build an interpreter for this simulator.
protected  int id
          The id field stores a unique identifier used to differentiate this simulator from others that might be running in the same simulation.
static Simulator.Interrupt IGNORE
          The IGNORE field stores a reference to a singleton anonymous class that ignores posting and firing of an interrupt.
protected  BaseInterpreter interpreter
          The interpreter field stores a reference to the instruction set interpreter.
protected  Simulator.Interrupt[] interrupts
          The interrupts array stores a reference to an Interrupt instance for each of the interrupt vectors supported in the simulator.
static int MAX_INTERRUPTS
          The MAX_INTERRUPTS fields stores the maximum number of interrupt vectors supported by the simulator.
protected  Microcontroller microcontroller
          The microcontroller field stores a reference to the microcontroller being simulated.
static int[] PRECISION_TABLE
           
protected  Program program
          The program field allows descendants of the Simulator class to access the program that is currently loaded in the simulator.
static boolean REPORT_SECONDS
          The REPORT_SECONDS field controls whether times in the Simulator.Printer output will be reported in clock cycles or in seconds.
static int SECONDS_PRECISION
          The SECONDS_PRECISION field controls the number of decimal places of precision reported for times in seconds when outputting events from the simulator.
 
Fields inherited from interface avrora.sim.IORegisterConstants
ACSR, ADCH, ADCL, ADCSRA, ADMUX, ASSR, DDRA, DDRB, DDRC, DDRD, DDRE, DDRF, DDRG, EEARH, EEARL, EECR, EEDR, EICRA, EICRB, EIFR, EIMSK, ETIFR, ETIMSK, ICR1H, ICR1L, ICR3H, ICR3L, MCUCR, MCUCSR, NUM_REGS, OCDR, OCR0, OCR1AH, OCR1AL, OCR1BH, OCR1BL, OCR1CH, OCR1CL, OCR2, OCR3AH, OCR3AL, OCR3BH, OCR3BL, OCR3CH, OCR3CL, OSCCAL, PINA, PINB, PINC, PIND, PINE, PINF, PING, PORTA, PORTB, PORTC, PORTD, PORTE, PORTF, PORTG, RAMPZ, SFIOR, SPCR, SPDR, SPH, SPL, SPMCSR, SPSR, SREG, SREG_C, SREG_H, SREG_I, SREG_N, SREG_S, SREG_T, SREG_V, SREG_Z, TCCR0, TCCR1A, TCCR1B, TCCR1C, TCCR2, TCCR3A, TCCR3B, TCCR3C, TCNT0, TCNT1H, TCNT1L, TCNT2, TCNT3H, TCNT3L, TIFR, TIMSK, TWAR, TWBR, TWCR, TWDR, TWSR, UBRR0H, UBRR0L, UBRR1H, UBRR1L, UCSR0A, UCSR0B, UCSR0C, UCSR1A, UCSR1B, UCSR1C, UDR0, UDR1, WDTCR, XDIV, XMCRA, XMCRB
 
Constructor Summary
Simulator(int i, InterpreterFactory f, Microcontroller mcu, Program p)
          The constructor creates the internal data structures and initial state of the processor.
 
Method Summary
 void delay(long cycles)
          The delay() method introduces a delay in the execution of the instructions of the program.
 void forceInterrupt(int num)
          The forceInterrupt() method forces the simulator to post the specified interrupt regardless of the normal source of the interrupt.
 Clock getClock()
          The getClock() method gets a reference to the Clock that this simulator is driving.
 EnergyControl getEnergyControl()
           
 int getID()
          The getID() method simply returns this node's unique ID.
 java.lang.String getIDTimeString()
          The getIDTimeString() is a public helper method that gets the node ID and the time (in clock cycles) and converts them into a string appropriately justified for printing as the first part of an event reported in simulation.
 BaseInterpreter getInterpreter()
          The getInterpreter() method returns the interpreter that is currently attached to this simulator.
protected  int getInterruptVectorAddress(int inum)
          The getInterruptVectorAddress() method computes the location in memory to jump to for the given interrupt number.
 Microcontroller getMicrocontroller()
          The getMicrocontroller() method gets a reference to the microcontroller being simulated.
 Simulator.Printer getPrinter(java.lang.String c)
          The getPrinter() method returns a Simulator.Printer instance for the named verbose channel.
 Program getProgram()
          The getProgram() method gets a reference to the program that has been loaded onto this simulator.
 State getState()
          The getState() retrieves a reference to the current state of the simulation, including the values of all registers, the SRAM, the IO register, the program memory, program counter, etc.
 void insertBreakPoint(int addr)
          The insertBreakPoint() method inserts a breakpoint at the instruction at the specified address.
 void insertEvent(Simulator.Event e, long cycles)
          The insertEvent() method inserts an event into the event queue of the simulator with the specified delay in clock cycles.
 PeriodicEvent insertPeriodicEvent(Simulator.Event e, long period)
          The insertPeriodicEvent() method inserts an event into the event queue of the simulator with the specified period.
 void insertProbe(Simulator.Probe p)
          The insertProbe() method allows a probe to be inserted that is executed before and after every instruction that is executed by the simulator
 void insertProbe(Simulator.Probe p, int addr)
          The insertProbe() method allows a probe to be inserted at a particular address in the program that corresponds to an instruction.
 void insertTimeout(long cycles)
          The insertTimeout() method inserts an event into the event queue of the simulator that causes it to stop execution and throw a Simulator.TimeoutException when the specified number of clock cycles have expired.
 void insertWatch(Simulator.Watch p, int data_addr)
          The insertWatch() method allows a watch to be inserted at a memory location.
 void removeBreakPoint(int addr)
          The removeBreakPoint method removes all breakpoints at the specified instruction at the specified address.
 void removeEvent(Simulator.Event e)
          The removeEvent() method removes an event from the event queue of the simulator.
 void removeProbe(Simulator.Probe b)
          The removeProbe() method removes a probe from the global probe table (the probes executed before and after every instruction).
 void removeProbe(Simulator.Probe p, int addr)
          The removeProbe() method removes a probe from the instruction at the specified the address.
 void removeWatch(Simulator.Watch p, int data_addr)
          The removeWatch() method removes a given watch from the memory location.
 void reset()
          The reset() method stops the simulation and resets its state to the default initial state.
 void start()
          The start() method begins the simulation.
 void stop()
          The stop() method stops the simulation if it is running.
protected  void triggerInterrupt(int num)
          The triggerInterrupt() method is used by device implementations when they detect that an interrupt should be triggered.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REPORT_SECONDS

public static boolean REPORT_SECONDS
The REPORT_SECONDS field controls whether times in the Simulator.Printer output will be reported in clock cycles or in seconds.


SECONDS_PRECISION

public static int SECONDS_PRECISION
The SECONDS_PRECISION field controls the number of decimal places of precision reported for times in seconds when outputting events from the simulator.


PRECISION_TABLE

public static final int[] PRECISION_TABLE

program

protected final Program program
The program field allows descendants of the Simulator class to access the program that is currently loaded in the simulator.


microcontroller

protected final Microcontroller microcontroller
The microcontroller field stores a reference to the microcontroller being simulated.


interpreter

protected BaseInterpreter interpreter
The interpreter field stores a reference to the instruction set interpreter.


interrupts

protected Simulator.Interrupt[] interrupts
The interrupts array stores a reference to an Interrupt instance for each of the interrupt vectors supported in the simulator.


clock

protected MainClock clock
The clock field stores a reference to the MainClock instance that tracks the clock cycles that have passed for this simulator.


id

protected final int id
The id field stores a unique identifier used to differentiate this simulator from others that might be running in the same simulation.


factory

protected final InterpreterFactory factory
The factory field stores a reference to the InterpreterFactory which should be used to build an interpreter for this simulator. This interpreter factory will create an interpreter with the correct options that were specified on the command line.


MAX_INTERRUPTS

public static int MAX_INTERRUPTS
The MAX_INTERRUPTS fields stores the maximum number of interrupt vectors supported by the simulator.


IGNORE

public static final Simulator.Interrupt IGNORE
The IGNORE field stores a reference to a singleton anonymous class that ignores posting and firing of an interrupt. This is the default value for interrupts in a freshly initialized Simulator instance.

Constructor Detail

Simulator

public Simulator(int i,
                 InterpreterFactory f,
                 Microcontroller mcu,
                 Program p)
The constructor creates the internal data structures and initial state of the processor. It constructs an instance of the simulator that is ready to have devices attached, IO registers probed, and probes and events inserted. Users should not create Simulator instances directly, but instead should get an instance of the appropriate processor and load the program into it.

Parameters:
p - the program to load into the simulator
Method Detail

getIDTimeString

public java.lang.String getIDTimeString()
The getIDTimeString() is a public helper method that gets the node ID and the time (in clock cycles) and converts them into a string appropriately justified for printing as the first part of an event reported in simulation. This method helps to align all the events in a columnar fashion.

Returns:
a string representation of the node ID and time appropriately formatted

getPrinter

public Simulator.Printer getPrinter(java.lang.String c)
The getPrinter() method returns a Simulator.Printer instance for the named verbose channel. The verbose channel is either enabled or disabled.

Parameters:
c - the name of the verbose channel
Returns:
a Simulator.Printer instance for this channel tied to this Simulator instance

getMicrocontroller

public Microcontroller getMicrocontroller()
The getMicrocontroller() method gets a reference to the microcontroller being simulated.

Returns:
a reference to the microcontroller being simulated

getProgram

public Program getProgram()
The getProgram() method gets a reference to the program that has been loaded onto this simulator.

Returns:
a reference to the Program instance representing the program loaded onto this Simulator object

getClock

public Clock getClock()
The getClock() method gets a reference to the Clock that this simulator is driving.

Returns:
a reference to the clock for this simulator

getID

public int getID()
The getID() method simply returns this node's unique ID.

Returns:
the unique ID of this node

getInterpreter

public BaseInterpreter getInterpreter()
The getInterpreter() method returns the interpreter that is currently attached to this simulator.

Returns:
the current interpreter

getState

public State getState()
The getState() retrieves a reference to the current state of the simulation, including the values of all registers, the SRAM, the IO register, the program memory, program counter, etc. This state is mutable.

Returns:
a reference to the current state of the simulation

start

public void start()
The start() method begins the simulation. It causes the simulator to enter a runLoop that executes instructions, firing probes and events as it executes. The start() method returns normally when the break AVR instruction is executed, when a BreakPointException is thrown, when a TimeoutException is thrown, or when the stop() method on this simulator instance is called.


stop

public void stop()
The stop() method stops the simulation if it is running. This method can be called from within a probe or event or from another thread.


reset

public void reset()
The reset() method stops the simulation and resets its state to the default initial state. Probes inserted in the program are retained. All events are removed.


getInterruptVectorAddress

protected int getInterruptVectorAddress(int inum)
The getInterruptVectorAddress() method computes the location in memory to jump to for the given interrupt number. On the Atmega128L, the starting point is the beginning of memory and each interrupt vector slot is 4 bytes. On older architectures, this is not the case, therefore this method has to be implemented according to the specific device being simulated.

Parameters:
inum - the interrupt number
Returns:
the byte address that represents the address in the program to jump to when this interrupt is fired

insertProbe

public void insertProbe(Simulator.Probe p)
The insertProbe() method allows a probe to be inserted that is executed before and after every instruction that is executed by the simulator

Parameters:
p - the probe to insert

insertProbe

public void insertProbe(Simulator.Probe p,
                        int addr)
The insertProbe() method allows a probe to be inserted at a particular address in the program that corresponds to an instruction. The probe is then fired before and after that particular instruction is executed.

Parameters:
p - the probe to insert
addr - the address at which to insert the probe

removeProbe

public void removeProbe(Simulator.Probe b)
The removeProbe() method removes a probe from the global probe table (the probes executed before and after every instruction). The comparison used is reference equality, not .equals().

Parameters:
b - the probe to remove

removeProbe

public void removeProbe(Simulator.Probe p,
                        int addr)
The removeProbe() method removes a probe from the instruction at the specified the address. The comparison used is reference equality, not .equals().

Parameters:
p - the probe to remove
addr - the address from which to remove the probe

insertBreakPoint

public void insertBreakPoint(int addr)
The insertBreakPoint() method inserts a breakpoint at the instruction at the specified address. At most one breakpoint can be inserted at a particular instruction. Subsequent calls to this method would then have no effect for the same address.

Parameters:
addr -

removeBreakPoint

public void removeBreakPoint(int addr)
The removeBreakPoint method removes all breakpoints at the specified instruction at the specified address.

Parameters:
addr -

insertWatch

public void insertWatch(Simulator.Watch p,
                        int data_addr)
The insertWatch() method allows a watch to be inserted at a memory location. The probe will be executed before every read or write to that memory location.

Parameters:
p - the probe to insert
data_addr - the data address at which to insert the probe

removeWatch

public void removeWatch(Simulator.Watch p,
                        int data_addr)
The removeWatch() method removes a given watch from the memory location. Reference equality is used to check for equality when removing probes, not .equals().

Parameters:
p - the probe to remove
data_addr - the data address from which to remove the probe

forceInterrupt

public void forceInterrupt(int num)
The forceInterrupt() method forces the simulator to post the specified interrupt regardless of the normal source of the interrupt. If there is a flag register associated with the specified interrupt, then the flag register's value will be set as if the original source of the interrupt (e.g. a timer) had posted the interrupt. As with a normal post of the interrupt, if the interrupt is masked out via a mask register or the master interrupt enable bit, the interrupt will not be delivered. The main reason that this interface exists is for forcing programs to handle interrupts and observe their behavior.

Parameters:
num - the interrupt number to force

triggerInterrupt

protected void triggerInterrupt(int num)
The triggerInterrupt() method is used by device implementations when they detect that an interrupt should be triggered. This method will check whether this interrupt is enabled by consulting its own internal table of interrupts that is kept consistent during writes to IO registers.

Parameters:
num - the number of the interrupt to trigger

insertEvent

public void insertEvent(Simulator.Event e,
                        long cycles)
The insertEvent() method inserts an event into the event queue of the simulator with the specified delay in clock cycles. The event will then be executed at the future time specified

Parameters:
e - the event to be inserted
cycles - the number of cycles in the future at which to fire

insertTimeout

public void insertTimeout(long cycles)
The insertTimeout() method inserts an event into the event queue of the simulator that causes it to stop execution and throw a Simulator.TimeoutException when the specified number of clock cycles have expired.

Parameters:
cycles - the number of cycles to run before timing out

insertPeriodicEvent

public PeriodicEvent insertPeriodicEvent(Simulator.Event e,
                                         long period)
The insertPeriodicEvent() method inserts an event into the event queue of the simulator with the specified period. The PeriodicEvent instance created will continually reinsert the event after each firing to achieve predictable periodic behavior.

Parameters:
e - the event to insert
period - the period in clock cycles
Returns:
the PeriodicEvent instance inserted

removeEvent

public void removeEvent(Simulator.Event e)
The removeEvent() method removes an event from the event queue of the simulator. The comparison used is reference equality, not .equals().

Parameters:
e - the event to remove

delay

public void delay(long cycles)
The delay() method introduces a delay in the execution of the instructions of the program. This is used by some devices for correct timing behavior. For example, the EEPROM, when written, causes a small delay in which no instructions are executed.

Parameters:
cycles - the number of cycles to delay the simulation

getEnergyControl

public EnergyControl getEnergyControl()