simulavr  1.1.0
memory.cpp
Go to the documentation of this file.
1 /*
2  ****************************************************************************
3  *
4  * simulavr - A simulator for the Atmel AVR family of microcontrollers.
5  * Copyright (C) 2001, 2002, 2003 Klaus Rudolph
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  ****************************************************************************
22  *
23  * $Id$
24  */
25 
26 #include <string.h> //strcpy()
27 #include <sstream>
28 #include <iostream>
29 
30 #include "memory.h"
31 #include "avrerror.h"
32 
33 using namespace std;
34 
35 unsigned int Memory::GetAddressAtSymbol(const string &s) {
36 
37  // feature: use a number instead of a symbol
38  char *dummy;
39  char *copy = avr_new(char, s.length() + 1);
40  unsigned int retval = 0;
41  unsigned int convlen = 0;
42 
43  strcpy(copy, s.c_str());
44  retval = strtoul(copy, &dummy, 16);
45  convlen = (unsigned int)(dummy - copy);
46  avr_free(copy);
47 
48  if((retval != 0) && ((unsigned int)s.length() == convlen)) {
49  // number found, return this
50  return retval;
51  }
52 
53  // isn't a number, try to find symbol ...
54  multimap<unsigned int, string>::iterator ii;
55 
56  for(ii = sym.begin(); ii != sym.end(); ii++) {
57  if(ii->second == s) {
58  return ii->first;
59  }
60  }
61 
62  avr_error("symbol '%s' not found!", s.c_str());
63 
64  return 0; // to avoid warnings, avr_error aborts the program
65 }
66 
67 string Memory::GetSymbolAtAddress(unsigned int add){
68  string lastName;
69  unsigned int lastAddr = 0;
70  multimap<unsigned int, string>::iterator ii;
71  multimap<unsigned int, string>::iterator last_ii;
72 
73  ii = sym.begin();
74  last_ii = ii;
75  if(ii == sym.end())
76  return ""; // we have no symbols at all
77  do {
78  if(lastAddr != ii->first) {
79  last_ii = ii;
80  lastName = ii->second;
81  }
82  lastAddr = ii->first;
83 
84  if(ii->first == add)
85  break; // found symbol
86  ii++;
87  if((ii != sym.end()) && (ii->first > add))
88  break; // behind the right symbol
89  } while(ii != sym.end());
90 
91  ostringstream os;
92 
93  os << lastName;
94  ii = last_ii;
95  while((++ii) != sym.end()) {
96  if(lastAddr != ii->first)
97  break;
98  os << "," << ii->second;
99  };
100 
101  unsigned int offset = add - lastAddr;
102  if((offset) != 0) {
103  os << "+0x" << hex << offset;
104  }
105 
106  return os.str();
107 }
108 
109 Memory::Memory(int _size): size(_size) {
110  myMemory = avr_new(unsigned char, size);
111 }
112 
std::string GetSymbolAtAddress(unsigned int add)
Definition: memory.cpp:67
unsigned char * myMemory
Definition: memory.h:45
void avr_free(void *ptr)
Free malloc&#39;d memory.
Definition: avrmalloc.cpp:182
STL namespace.
Memory(int size)
Definition: memory.cpp:109
#define avr_error(...)
Definition: avrerror.h:135
#define avr_new(type, count)
Macro for allocating memory.
Definition: avrmalloc.h:40
unsigned int size
Definition: memory.h:41
unsigned int GetAddressAtSymbol(const std::string &s)
Definition: memory.cpp:35