Device Class Reference

yasimavr: Device Class Reference
yasimavr
Loading...
Searching...
No Matches

Basic AVR device model. More...

#include <sim_device.h>

Inheritance diagram for Device:
ArchAVR_Device ArchXT_Device

Public Types

enum  State {
  State_Limbo = 0x00 , State_Ready = 0x10 , State_Running = 0x21 , State_Sleeping = 0x31 ,
  State_Halted = 0x41 , State_Reset = 0x51 , State_Break = 0x60 , State_Done = 0x70 ,
  State_Crashed = 0x80 , State_Destroying = 0xFF
}
 
enum  ResetFlag {
  Reset_PowerOn = 0x00000001 , Reset_WDT = 0x00000002 , Reset_BOD = 0x00000004 , Reset_SW = 0x00000008 ,
  Reset_Ext = 0x00000010 , Reset_Halt = 0x00010000
}
 
enum  Option {
  Option_ResetOnPinShorting = 0x01 , Option_IgnoreBadCpuIO = 0x02 , Option_IgnoreBadCpuLPM = 0x04 , Option_DisablePseudoSleep = 0x08 ,
  Option_InfiniteLoopDetect = 0x10
}
 

Public Member Functions

 Device (Core &core, const DeviceConfiguration &config)
 
virtual ~Device ()=default
 
Corecore () const
 
void set_option (Option option, bool value)
 
bool test_option (Option option) const
 
const DeviceConfigurationconfig () const
 
State state () const
 
cycle_count_t cycle () const
 
SleepMode sleep_mode () const
 
unsigned long frequency () const
 
bool init (CycleManager &cycle_manager)
 
bool load_firmware (const Firmware &firmware)
 
void reset (int reset_flags=Reset_PowerOn)
 
cycle_count_t exec_cycle ()
 
void attach_peripheral (Peripheral &ctl)
 
void add_ioreg_handler (reg_addr_t addr, IORegHandler &handler, bitmask_t bits, IORegister::BitMode bitmode)
 
Peripheralfind_peripheral (ctl_id_t id)
 
bool ctlreq (ctl_id_t id, ctlreq_id_t req, ctlreq_data_t *reqdata=nullptr)
 
CycleManagercycle_manager ()
 
Pinfind_pin (pin_id_t id)
 
PinManagerpin_manager ()
 
LogHandlerlog_handler ()
 
Loggerlogger ()
 
void crash (uint16_t reason, const char *text)
 
 Device (const Device &)=delete
 
Deviceoperator= (const Device &)=delete
 

Protected Member Functions

virtual bool arch_init ()
 
virtual bool core_ctlreq (ctlreq_id_t req, ctlreq_data_t *reqdata)
 
virtual bool program (const Firmware &firmware)
 
void erase_peripherals ()
 
virtual flash_addr_t reset_vector ()
 

Friends

class DeviceDebugProbe
 

Detailed Description

Basic AVR device model.

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

Member Enumeration Documentation

◆ Option

Device option enum

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

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.

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.

See also
Core::cpu_read_ioreg, Core::cpu_write_ioreg
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.

Option_DisablePseudoSleep 

This option disables the pseudo-sleep mode.

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.

◆ ResetFlag

Reset source enum

Enumerator
Reset_PowerOn 

Power-On reset source.

Reset_WDT 

Watchdog Timer reset source.

Reset_BOD 

Brown-out Detector reset source.

Reset_SW 

Software reset source.

Reset_Ext 

External pin reset source.

Reset_Halt 

◆ State

Device model state enum.

Enumerator
State_Limbo 

Device constructed but not yet initialised.

State_Ready 

Device initialised but no firmware loaded yet.

State_Running 

Device executing the firmware.

State_Sleeping 

Device in sleep mode.

State_Halted 

CPU halted but peripherals are running normally.

State_Reset 

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

State_Break 

Device halted by a BREAK instruction.

State_Done 

Final state without any error.

State_Crashed 

Final state with error.

State_Destroying 

Transiting state during destruction.

Constructor & Destructor Documentation

◆ Device() [1/2]

Device::Device ( Core core,
const DeviceConfiguration config 
)

Construct a device model

◆ ~Device()

virtual Device::~Device ( )
virtualdefault

◆ Device() [2/2]

Device::Device ( const Device )
delete

Member Function Documentation

◆ add_ioreg_handler()

void Device::add_ioreg_handler ( reg_addr_t  addr,
IORegHandler handler,
bitmask_t  bits,
IORegister::BitMode  bitmode 
)

Adds a handler to a I/O register.

Parameters
addraddress of the I/O register, in I/O address space
handlerhandler to add
bitsmasks for adding bits to use in the register
bitmodemode for the bits added
Note
The register is allocated if it does not exist yet.

◆ arch_init()

bool Device::arch_init ( )
protectedvirtual

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

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

◆ attach_peripheral()

void Device::attach_peripheral ( Peripheral ctl)

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

Parameters
ctlperipheral to attach

◆ config()

const DeviceConfiguration & Device::config ( ) const
inline

◆ core()

Core & Device::core ( ) const
inline

◆ core_ctlreq()

bool Device::core_ctlreq ( ctlreq_id_t  req,
ctlreq_data_t reqdata 
)
protectedvirtual

Callback for processing the requests to the core.

See also
ctlreq()

Reimplemented in ArchAVR_Device, and ArchXT_Device.

◆ crash()

void Device::crash ( uint16_t  reason,
const char *  text 
)

Set the device to the crashed state

Parameters
reasonone of the CRASH_XXX codes
textmessage of the crash

◆ ctlreq()

bool Device::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
ididentifier of the peripheral to interrogate
reqrequest identifier, specific to each peripheral
reqdatadata structure of the request
Returns
true if the request could be processed, false otherwise.

◆ cycle()

cycle_count_t Device::cycle ( ) const
inline

◆ cycle_manager()

CycleManager * Device::cycle_manager ( )
inline

◆ erase_peripherals()

void Device::erase_peripherals ( )
protected

◆ exec_cycle()

cycle_count_t Device::exec_cycle ( )

Execute one instruction cycle.

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

◆ find_peripheral()

Peripheral * Device::find_peripheral ( ctl_id_t  id)

Finds a peripheral given its identifier the device itself.

Returns
the peripheral if found or nullptr

◆ find_pin()

Pin * Device::find_pin ( pin_id_t  id)

Find a device pin with the given identifier

Returns
the pin if found, or nullptr

◆ frequency()

unsigned long Device::frequency ( ) const
inline

◆ init()

bool Device::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_managerused for scheduling timers during the device model execution.
Returns
true if the initialisation has succeeded, false if it failed.

◆ load_firmware()

bool Device::load_firmware ( const Firmware firmware)

Load a firmware into the device non-volatile memories.

Returns
true if the load succeeded, false if it failed
See also
Firmware

◆ log_handler()

LogHandler & Device::log_handler ( )
inline

◆ logger()

Logger & Device::logger ( )
inline

◆ operator=()

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

◆ pin_manager()

PinManager & Device::pin_manager ( )
inline

◆ program()

bool Device::program ( const Firmware firmware)
protectedvirtual

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.

Returns
true if the load succeeded, false if it failed
See also
Firmware

Reimplemented in ArchAVR_Device, and ArchXT_Device.

◆ reset()

void Device::reset ( int  reset_flag = Reset_PowerOn)

Simulates a MCU reset.

Parameters
reset_flagcombination of ResetFlag enum values, indicating the source of the reset signal

◆ reset_vector()

flash_addr_t Device::reset_vector ( )
protectedvirtual

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

Reimplemented in ArchAVR_Device.

◆ set_option()

void Device::set_option ( Option  option,
bool  value 
)

Set or clear a device option.

See also
Device::Option

◆ sleep_mode()

SleepMode Device::sleep_mode ( ) const
inline

◆ state()

Device::State Device::state ( ) const
inline

◆ test_option()

bool Device::test_option ( Option  option) const

Returns whether a device option is set.

See also
Device::Option

Friends And Related Symbol Documentation

◆ DeviceDebugProbe

friend class DeviceDebugProbe
friend

The documentation for this class was generated from the following files: