Class Device
Defined in File sim_device.h
Inheritance Relationships
Derived Types
public ArchAVR_Device(Class ArchAVR_Device)public ArchXT_Device(Class ArchXT_Device)
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_Halted
CPU halted but peripherals are running normally.
-
enumerator State_Done
Final state without any error.
-
enumerator State_Crashed
Final state with error.
-
enumerator State_Destroying
Transiting state during destruction.
-
enumerator State_Halted
-
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
-
enumerator Reset_PowerOn
-
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.
See also
-
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.
-
enumerator Option_ResetOnPinShorting
Public Functions
-
Device(Core &core, const DeviceConfiguration &config)
Construct a device model
-
virtual ~Device()
Destroy the device model and all the attached peripheral.
-
inline const DeviceConfiguration &config() const
-
inline cycle_count_t cycle() 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
- 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 LogHandler &log_handler()
-
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
Protected Functions
-
virtual bool core_ctlreq(ctlreq_id_t req, ctlreq_data_t *reqdata)
Callback for processing the requests to the core.
See also
-
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
- 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.
-
enum State