Fixed problem in DeviceSettings::strParam, returned wrong string
[avr-sim.git] / src / Program.cpp
blob731d7b5c7864256c51d903a24de93d8c8ae8fda3
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 "Program.h"
20 #include <iostream>
21 #include <algorithm>
23 namespace avr {
25 const std::string Program::empty;
27 void Program::addSymbolFlash(int addr, const char *name) {
28 #ifdef DEBUG
29 std::cout << "Flash Symbol: " << name << " (" << std::hex << addr << std::dec << ")" << std::endl;
30 #endif
31 flashSymbols.insert( std::make_pair(addr, name) );
34 void Program::addSymbolEeprom(int addr, const char *name) {
35 #ifdef DEBUG
36 std::cout << "Eeprom Symbol: " << name << " (" << std::hex << addr << std::dec << ")" << std::endl;
37 #endif
40 void Program::addSymbolRam(int addr, const char *name) {
41 #ifdef DEBUG
42 std::cout << "Ram Symbol: " << name << " (" << std::hex << addr << std::dec << ")" << std::endl;
43 #endif
44 dataSymbols.insert( std::make_pair(addr, name) );
47 const std::string & Program::functionName(int addr) const {
48 SymTable::const_iterator it = flashSymbols.upper_bound( addr );
49 if( it == flashSymbols.end() )
50 return empty;
52 if( it->first != addr )
53 it--;
55 return it->second;
58 class SymbolFinder {
59 public:
60 SymbolFinder(const std::string & name) : name(name) {}
62 public:
63 bool operator() (const std::pair<int, std::string> & p) {
64 return p.second == name;
67 private:
68 const std::string & name;
71 int Program::functionAddress(const std::string & name) const {
72 SymTable::const_iterator it =
73 std::find_if(flashSymbols.begin(), flashSymbols.end(), SymbolFinder(name) );
75 if( it == flashSymbols.end() )
76 return -1;
78 return it->first;
81 int Program::dataAddress(const std::string & name) const {
82 SymTable::const_iterator it =
83 std::find_if(dataSymbols.begin(), dataSymbols.end(), SymbolFinder(name) );
85 if( it == dataSymbols.end() )
86 return -1;
88 return it->first;
91 const std::string & Program::dataName(int addr) const {
92 SymTable::const_iterator it = dataSymbols.upper_bound( addr );
93 if( it == dataSymbols.end() )
94 return empty;
96 if( it->first != addr )
97 it--;
99 return it->second;