2 #include "DeviceSettings.h"
4 #include "HardwareFactory.h"
6 #include "DebugInterface.h"
10 #include "RuntimeException.h"
17 Device::Device(SimulationClock
& clock
, const char *devicename
)
18 : SimulationObject(clock
), core(0) {
20 intVectors
.resize( Bus::intVectorsSize
);
23 avr::DeviceSettings::load( this, devicename
);
28 frq
= 1000000000 / defaultFrequency
;
29 clock
.setBreakDelta( frq
, this );
37 void Device::load(const Program
& program
) {
39 while( program
.readNextSection(sec
) ) {
40 //only read flash bytes and data
42 core
->loadFlash( sec
.data(), sec
.address(), sec
.size() );
45 std::cout
<< "Flash: " << sec
.size() << " bytes at " << sec
.address() << std::endl
;
49 if( sec
.isEeprom() ) {
50 eeprom
->write( sec
.address(), sec
.data(), sec
.size() );
53 std::cout
<< "Eeprom: " << sec
.size() << " bytes at " << sec
.address() << std::endl
;
59 void Device::setClockFrequency(ClockFrequency frq
) {
60 this->frq
= 1000000000 / frq
;
61 clock
.reassignBreak( this, this->frq
);
64 void Device::buildCore(unsigned int ioSpaceSize
, unsigned int ramSize
,
65 unsigned int flashSize
, unsigned int stackMask
,
66 int pcBytes
/*= 2*/, ERam
*eram
/*= 0*/) {
67 core
= new Core(bus
, ioSpaceSize
, ramSize
,
68 flashSize
, stackMask
, pcBytes
, eram
);
71 void Device::buildHardware(const char *hwname
, HardwareSettings
& hws
) {
72 Hardware
*hw
= HardwareFactory::build(hwname
, hws
, bus
);
74 std::string name
, binding
;
75 while( hws
.getBinding(name
, binding
) ) {
77 std::cout
<< "Binding \"" << name
<< "\" to \"" << binding
<< "\"" << std::endl
;
80 if( ! hw
->attachReg( name
.c_str(), core
->getIoreg( binding
) ) )
81 throw util::RuntimeException(
82 util::format("%s::attachReg: unknown register %s") % hwname
% name
);
85 bus
.addHardware( hw
);
87 // Special handling for eeprom
88 if( strcmp(hwname
, "eeprom") )
89 eeprom
= static_cast<Eeprom
*>( hw
);
92 void Device::addIOReg(unsigned int address
,
93 const std::string
& name
, unsigned char intial
) {
94 core
->addIOReg(address
, name
, intial
);
97 void Device::addInterrupt(unsigned int vector
, unsigned int address
,
99 intVectors
[vector
- 1] = IntVect( address
, name
);
102 void Device::addAnalyzers(ScriptEngine
*vm
,
103 const std::list
<const char *> & analyzers
) {
107 void Device::reset(unsigned int type
) {
112 void Device::step() {
113 bool instrFinished
= false;
114 if( ! bus
.isHoldingCPU() )
115 instrFinished
= core
->step();
120 * TODO Is this the correct place to do this?
121 * - What if the hardware is holding the CPU?
124 if( instrFinished
&& bus
.pendingInterrupt(vect
) ) {
125 unsigned int addr
= intVectors
[ vect
].address
;
126 if( core
->interrupt( vect
, addr
) )
127 bus
.clearInterrupt(vect
);
130 clock
.setBreakDelta( frq
, this );
133 DebugInterface
*Device::debugInterface() {
134 return new DebugInterface(*this, *core
);