Fixed problem in DeviceSettings::strParam, returned wrong string
[avr-sim.git] / src / HardwareFactory.cpp
blobc4459d292f3a935857f81697b99cd42eeac3a3eb
1 /*
2 * avr-sim: An atmel AVR simulator
3 * Copyright (C) 2008 Tom Haber
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "HardwareFactory.h"
20 #include "Format.h"
21 #include "RuntimeException.h"
22 #include "DeviceSettings.h"
24 #ifdef DEBUG
25 # include <iostream>
26 #endif
28 #include "Eeprom.h"
29 #include "Port.h"
30 #include "Usart.h"
31 #include "Spi.h"
32 #include "ADC.h"
33 #include "Timer8.h"
34 #include "Timer16.h"
35 #include "TimerInterrupts.h"
37 #include <cstring>
39 namespace avr {
41 Hardware *HardwareFactory::build(const char *name,
42 HardwareSettings & hws, Bus & bus) {
43 #ifdef DEBUG
44 std::cout << "Building hardware: " << name << std::endl;
45 #endif
47 if( strcmp(name, "eeprom") == 0 )
48 return buildEeprom(hws, bus);
49 else if( strcmp(name, "hwport") == 0 )
50 return buildPort(hws, bus);
51 else if( strcmp(name, "timer8") == 0 )
52 return buildTimer8(hws, bus);
53 else if( strcmp(name, "timer16") == 0 )
54 return buildTimer16(hws, bus);
55 else if( strcmp(name, "timerirq") == 0 )
56 return buildTimerIrq(hws, bus);
57 else if( strcmp(name, "usart") == 0 )
58 return buildUsart(hws, bus);
59 else if( strcmp(name, "spi") == 0 )
60 return buildSpi(hws, bus);
61 else if( strcmp(name, "adc") == 0 )
62 return buildADC(hws, bus);
64 throw util::RuntimeException(
65 util::format("Trying to build unknown hardware: %s") % name );
68 Hardware *HardwareFactory::buildEeprom(HardwareSettings & hws, Bus & bus) {
69 unsigned int size = hws.intParam("size", 10);
70 unsigned int rdyVec = hws.intParam("rdyVec", 10);
71 return new Eeprom(bus, size, rdyVec);
74 Hardware *HardwareFactory::buildPort(HardwareSettings & hws, Bus & bus) {
75 const std::string & name = hws.strParam("name");
76 unsigned int mask = hws.intParamDef("mask", 0xff, 16);
77 return new Port(bus, name, mask);
80 Hardware *HardwareFactory::buildTimer8(HardwareSettings & hws, Bus & bus) {
81 unsigned char tovmask = hws.intParam("tov", 16);
83 unsigned int units = hws.intParam("units", 10);
84 unsigned char ocfmasks[units];
85 if( units >= 1 ) ocfmasks[0] = hws.intParam("ocf0", 16);
86 if( units >= 2 ) ocfmasks[1] = hws.intParam("ocf1", 16);
87 if( units >= 3 ) ocfmasks[2] = hws.intParam("ocf2", 16);
89 return new Timer8(bus, tovmask, units, ocfmasks);
92 Hardware *HardwareFactory::buildTimer16(HardwareSettings & hws, Bus & bus) {
93 unsigned int tovmask = hws.intParam("tov", 16);
95 unsigned int units = hws.intParam("units", 10);
96 unsigned char ocfmasks[units];
97 if( units >= 1 ) ocfmasks[0] = hws.intParam("ocf0", 16);
98 if( units >= 2 ) ocfmasks[1] = hws.intParam("ocf1", 16);
100 return new Timer16(bus, tovmask, units, ocfmasks);
103 Hardware *HardwareFactory::buildTimerIrq(HardwareSettings & hws, Bus & bus) {
104 unsigned int mask = hws.intParamDef("mask", 0xff, 16);
105 unsigned int bit0Vec = hws.intParamDef("bit0Vec", 0, 10) - 1;
106 unsigned int bit1Vec = hws.intParamDef("bit1Vec", 0, 10) - 1;
107 unsigned int bit2Vec = hws.intParamDef("bit2Vec", 0, 10) - 1;
108 unsigned int bit3Vec = hws.intParamDef("bit3Vec", 0, 10) - 1;
109 unsigned int bit4Vec = hws.intParamDef("bit4Vec", 0, 10) - 1;
110 unsigned int bit5Vec = hws.intParamDef("bit5Vec", 0, 10) - 1;
111 unsigned int bit6Vec = hws.intParamDef("bit6Vec", 0, 10) - 1;
112 unsigned int bit7Vec = hws.intParamDef("bit7Vec", 0, 10) - 1;
113 return new TimerInterrupts(bus, mask,
114 bit0Vec, bit1Vec, bit2Vec, bit3Vec,
115 bit4Vec, bit5Vec, bit6Vec, bit7Vec);
118 Hardware *HardwareFactory::buildUsart(HardwareSettings & hws, Bus & bus) {
119 const std::string & name = hws.strParam("name");
120 unsigned int udreVec = hws.intParam("udreVec", 10) - 1;
121 unsigned int rxVec = hws.intParam("rxVec", 10) - 1;
122 unsigned int txVec = hws.intParam("txVec", 10) - 1;
123 return new Usart(bus, name, udreVec, rxVec, txVec);
126 Hardware *HardwareFactory::buildSpi(HardwareSettings & hws, Bus & bus) {
127 unsigned int stcVec = hws.intParam("stcVec", 10) - 1;
128 return new Spi(bus, stcVec);
131 Hardware *HardwareFactory::buildADC(HardwareSettings & hws, Bus & bus) {
132 unsigned int ccVec = hws.intParam("ccVec", 10) - 1;
133 return new ADC(bus, ccVec);