Class Device

Inheritance Relationships

Derived Types

Class Documentation

class Device

Basic AVR device model.

This is the top-level object for a AVR MCU simulation model.

Subclassed by ArchAVR_Device, ArchXT_Device

Public Types

enum State

Device model state enum.

Values:

enumerator State_Limbo

Device constructed but not yet initialised.

enumerator State_Ready

Device initialised but no firmware loaded yet.

enumerator State_Running

Device executing the firmware.

enumerator State_Sleeping

Device in sleep mode.

enumerator State_Halted

CPU halted but peripherals are running normally.

enumerator State_Reset

Device being reset (taken into account at the next cycle)

enumerator State_Break

Device halted by a BREAK instruction.

enumerator State_Done

Final state without any error.

enumerator State_Crashed

Final state with error.

enumerator State_Destroying

Transiting state during destruction.

enum ResetFlag

Reset source enum

Values:

enumerator Reset_PowerOn

Power-On reset source.

enumerator Reset_WDT

Watchdog Timer reset source.

enumerator Reset_BOD

Brown-out Detector reset source.

enumerator Reset_SW

Software reset source.

enumerator Reset_Ext

External pin reset source.

enumerator Reset_Halt
enum Option

Device option enum

These options are to be used with set_option() and test_option() to alter the behaviour of the simulation model.

Values:

enumerator Option_ResetOnPinShorting

By default, the device will crash if a pin shorting is detected. If this option is set, it will instead simulate a BOD-triggered MCU reset.

enumerator Option_IgnoreBadCpuIO

By default the device will crash on the following CPU I/O access errors:

  • Reading from a unallocated register,

  • Writing to a unallocated register,

  • Changing the value of a read-only field,

  • Writing a ‘1’ to an unused bit.

If this option is set, these errors will be ignored.

enumerator Option_IgnoreBadCpuLPM

By default the device will crash if the CPU reads an unprogrammed address of the flash. If this option is set, the operation will succeed.

enumerator Option_DisablePseudoSleep

This option disables the pseudo-sleep mode.

enumerator Option_InfiniteLoopDetect

This option makes the simulation loop exit when the device enters a sleep mode or an infinite loop instruction (“rjmp .-2”) with the GIE bit cleared. It is set by default.

Public Functions

Device(Core &core, const DeviceConfiguration &config)

Construct a device model

virtual ~Device()

Destroy the device model and all the attached peripheral.

inline Core &core() const
void set_option(Option option, bool value)

Set or clear a device option.

See also

Device::Option

bool test_option(Option option) const

Returns whether a device option is set.

See also

Device::Option

inline const DeviceConfiguration &config() const
inline State state() const
inline cycle_count_t cycle() const
inline SleepMode sleep_mode() const
inline unsigned long frequency() const
bool init(CycleManager &cycle_manager)

Initialise a device. This must be called once before the simulation is started. This function allows all peripherals to allocate resources and connect signals.

Parameters:

cycle_manager – used for scheduling timers during the device model execution.

Returns:

true if the initialisation has succeeded, false if it failed.

bool load_firmware(const Firmware &firmware)

Load a firmware into the device non-volatile memories.

See also

Firmware

Returns:

true if the load succeeded, false if it failed

void reset(int reset_flags = Reset_PowerOn)

Simulates a MCU reset.

Parameters:

reset_flag – combination of ResetFlag enum values, indicating the source of the reset signal

cycle_count_t exec_cycle()

Execute one instruction cycle.

Returns:

the number of clock cycle consumed by the instruction, or 0 if something wrong happened.

void attach_peripheral(Peripheral &ctl)

Attach a peripheral to the device. The device takes ownership of the peripheral and will destroy it upon destruction.

Parameters:

ctl – peripheral to attach

void add_ioreg_handler(reg_addr_t addr, IO_RegHandler &handler, uint8_t ro_mask = 0x00)

Adds a handler to a I/O register.

Note

The register is allocated if it does not exist yet. All bits of the register are marked as used and bits marked as ‘1’ in ro_mask are marked as read-only. This is OR’ed with any pre-defined read-only mask.

Parameters:
  • addr – address of the I/O register, in I/O address space

  • handler – handler to add

  • ro_mask – optional read-only bit mask. By default = 0x00 (all bits are R/W)

void add_ioreg_handler(const regbit_t &rb, IO_RegHandler &handler, bool readonly = false)

Adds a handler to a part of a I/O register.

Note

The register is allocated if it does not exist yet. All bits of the regbit mask are marked as used and also marked as read-only if ‘readonly’ is true

Parameters:
  • rb – address/mask of the bits I/O register, in I/O address space

  • handler – handler to add

  • readonly

Peripheral *find_peripheral(const char *name)
Peripheral *find_peripheral(ctl_id_t id)

Finds a peripheral given its identifier the device itself.

Returns:

the peripheral if found or nullptr

bool ctlreq(ctl_id_t id, ctlreq_id_t req, ctlreq_data_t *reqdata = nullptr)

Process a peripheral request. This is the mechanism used to interrogate peripherals or the device itself.

Parameters:
  • id – identifier of the peripheral to interrogate

  • req – request identifier, specific to each peripheral

  • reqdata – data structure of the request

Returns:

true if the request could be processed, false otherwise.

inline CycleManager *cycle_manager()
Pin *find_pin(const char *name)

Find a device pin with the given name

Returns:

the pin if found, or nullptr

Pin *find_pin(pin_id_t id)

Find a device pin with the given identifier

Returns:

the pin if found, or nullptr

inline PinManager &pin_manager()
inline LogHandler &log_handler()
inline Logger &logger()
void crash(uint16_t reason, const char *text)

Set the device to the crashed state

Parameters:
  • reason – one of the CRASH_XXX codes

  • text – message of the crash

Device(const Device&) = delete
Device &operator=(const Device&) = delete

Protected Functions

virtual bool arch_init()

Stub for any architecture specific initialisation step. The default implementation does nothing.

Returns:

true if initialisation is successful, false in case of error.

virtual bool core_ctlreq(ctlreq_id_t req, ctlreq_data_t *reqdata)

Callback for processing the requests to the core.

See also

ctlreq()

virtual bool program(const Firmware &firmware)

Implementation of the programming of the non-volatile memories of the device. The basic implementation only loads the flash and the fuses, the rest is the responsibility of architecture-specific implementations.

See also

Firmware

Returns:

true if the load succeeded, false if it failed

void erase_peripherals()
virtual flash_addr_t reset_vector()

Returns the reset vector address in bytes. The default implementation returns 0x0000.