simulavr  1.1.0
rwmem.h
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  * Copyright (C) 2009 Onno Kortmann
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  ****************************************************************************
23  *
24  * $Id$
25  */
26 
27 #ifndef RWMEM
28 #define RWMEM
29 
30 #include <string> // std::string
31 #include <vector>
32 
33 #include "traceval.h"
34 #include "avrerror.h"
35 #include "hardware.h"
36 
37 class TraceValue;
38 
40 
43 
44  public:
53  const std::string &tracename="",
54  const int index=-1);
57  RWMemoryMember(void);
58 #ifndef SWIG
59  operator unsigned char() const;
62  unsigned char operator=(unsigned char val);
64  unsigned char operator=(const RWMemoryMember &mm);
65 #endif
66  virtual ~RWMemoryMember() {}
67 
71  virtual void set_bit( unsigned int bitaddr ) {
72  // default as before SBI instruction rework
73  unsigned char val = this->get();
74  val |= 1 << bitaddr;
75  this->set(val);
76  }
77 
81  virtual void clear_bit( unsigned int bitaddr ) {
82  // default as before rework of CBI instruction
83  unsigned char val = this->get();
84  val &= ~(1 << bitaddr);
85  this->set(val);
86  }
87 
88  const std::string &GetTraceName(void) { return tracename; }
89  bool IsInvalid(void) const { return isInvalid; }
90 
91  protected:
95  virtual void set(unsigned char nv)=0;
98  virtual unsigned char get() const=0;
99 
105  mutable TraceValue *tv;
107  const std::string tracename;
108  const bool isInvalid;
109 };
110 
112 
113 class GPIORegister: public RWMemoryMember, public Hardware {
114 
115  public:
118  const std::string &tracename):
119  RWMemoryMember(registry, tracename),
120  Hardware(core)
121  { value = 0; }
122 
123  // from Hardware
124  void Reset(void) { value = 0; }
125 
126  protected:
127  unsigned char get() const { return value; }
128  void set(unsigned char v) { value = v; }
129 
130  private:
131  unsigned char value;
132 };
133 
135 class CLKPRRegister: public RWMemoryMember, public Hardware {
136 
137  public:
138  CLKPRRegister(AvrDevice *core,
140 
141  // from Hardware
142  void Reset(void);
143  unsigned int CpuCycle(void);
144 
145  protected:
146  unsigned char get() const { return value; }
147  void set(unsigned char v);
148 
149  private:
151  unsigned char value;
152  unsigned char activate;
153 };
154 
156 class XDIVRegister: public RWMemoryMember, public Hardware {
157 
158  public:
159  XDIVRegister(AvrDevice *core,
161 
162  // from Hardware
163  void Reset(void) { value = 0; }
164 
165  protected:
166  unsigned char get() const { return value; }
167  void set(unsigned char v);
168 
169  private:
170  unsigned char value;
171 };
172 
174 class OSCCALRegister: public RWMemoryMember, public Hardware {
175 
176  public:
177  // select type of oscillator, see AVR053 application note!
178  enum {
179  OSCCAL_V3 = 0,
180  OSCCAL_V4 = 1,
181  OSCCAL_V5 = 2
182  };
183 
186  int cal);
187 
188  // from Hardware
189  void Reset(void);
190 
191  protected:
192  unsigned char get() const { return value; }
193  void set(unsigned char v);
194 
195  private:
196  unsigned char value;
197  int cal_type;
198 };
199 
201 
202 class RAM : public RWMemoryMember {
203 
204  public:
206  const std::string &tracename,
207  const size_t number,
208  const size_t maxsize);
209 
210  protected:
211  unsigned char get() const;
212  void set(unsigned char);
213 
214  private:
215  unsigned char value;
217 };
218 
220 
221 class InvalidMem : public RWMemoryMember {
222  private:
224  int addr;
225 
226  public:
227  InvalidMem(AvrDevice *core, int addr);
228 
229  protected:
230  unsigned char get() const;
231  void set(unsigned char);
232 
233  private:
234  unsigned char value;
235 };
236 
239  private:
240  const char * obj_name;
241  const char * reg_name;
242 
243  public:
244  NotSimulatedRegister(const char * oname, const char * rname);
245 
246  enum {
247  // TWI registers
248  TWI_TWAMR = 0,
254  // ADC registers
258  //MCU registers
266  // On chip debug register
268  // external memory control register
271  // NSR array size
272  NSR_size
273  };
274 
275  static NotSimulatedRegister* getRegister(int reg);
276 
277  protected:
278  unsigned char get() const;
279  void set(unsigned char);
280 };
281 
283 
285 template<class P>
286 class IOReg: public RWMemoryMember {
287 
288  public:
289  typedef unsigned char(P::*getter_t)();
290  typedef void (P::*setter_t)(unsigned char);
291  typedef unsigned char(P::*getter_bit_t)(unsigned int);
292  typedef void (P::*setter_bit_t)(bool,unsigned int);
298  const std::string &tracename,
299  P *_p,
300  getter_t _g=0,
301  setter_t _s=0,
302  getter_bit_t _gb=0,
303  setter_bit_t _sb=0):
304  RWMemoryMember(registry, tracename),
305  p(_p),
306  g(_g),
307  s(_s),
308  gb(_gb),
309  sb(_sb)
310  {
311  // 'undefined state' doesn't really make sense for IO registers
312  if (tv)
313  tv->set_written();
314  }
315 
318  void hardwareChange(unsigned char val) { if(tv) tv->change(val); }
320  void releaseTraceValue(void) {
321  if(tv) {
323  tv = NULL;
324  }
325  }
326 
329  virtual void set_bit( unsigned int bitaddr ) {
330  if (sb) {
331  (p->*sb)( 1, bitaddr);
332  } else { // default to byte access
333  if (g && s) {
334  unsigned char val = (p->*g)();
335  val|= 1<<bitaddr;
336  (p->*s)(val);
337  } else {
338  avr_warning("Bitwise access of '%s' is not supported.", tv->name().c_str());
339  }
340  }
341  }
342 
345  virtual void clear_bit( unsigned int bitaddr ) {
346  if (sb) {
347  (p->*sb)( 0, bitaddr);
348  } else { // default to byte access
349  if (g && s) {
350  unsigned char val = (p->*g)();
351  val&= ~(1<<bitaddr);
352  (p->*s)(val);
353  } else {
354  avr_warning("Bitwise access of '%s' is not supported.", tv->name().c_str());
355  }
356  }
357  }
358 
359  protected:
360  unsigned char get() const {
361  if (g)
362  return (p->*g)();
363  else if (tv) {
364  avr_warning("Reading of '%s' is not supported.", tv->name().c_str());
365  }
366  return 0;
367  }
368  void set(unsigned char val) {
369  if (s)
370  (p->*s)(val);
371  else if (tv) {
372  avr_warning("Writing of '%s' (with %d) is not supported.", tv->name().c_str(), val);
373  }
374  }
375 
376  private:
377  P *p;
378  getter_t g;
379  setter_t s;
380  getter_bit_t gb;
381  setter_bit_t sb;
382 };
383 
384 class IOSpecialReg;
385 
387 
399 
400  protected:
401  friend class IOSpecialReg;
402 
407  virtual unsigned char set_from_reg(const IOSpecialReg* reg, unsigned char nv)=0;
408 
412  virtual unsigned char get_from_client(const IOSpecialReg* reg, unsigned char v)=0;
413 
414  public:
415  virtual ~IOSpecialRegClient() {}
416 
417 };
418 
423 
424  public:
426  IOSpecialReg(TraceValueRegister *registry, const std::string &tracename);
427 
429  void connectSRegClient(IOSpecialRegClient *c) { clients.push_back(c); }
430 
432  void Reset(void) { Reset(0); }
435  void Reset(unsigned char val) { value = 0; if(tv) tv->set_written(val); }
436 
439  void hardwareChange(unsigned char val) { if(tv) tv->change(val); }
440 
444  void hardwareChangeMask(unsigned char val, unsigned char mask) { if(tv) tv->change(val, mask); }
445 
446  protected:
447  std::vector<IOSpecialRegClient*> clients;
448 
449  unsigned char get() const;
450  void set(unsigned char);
451 
452  private:
453  unsigned char value;
454 };
455 
456 #endif
Basic AVR device, contains the core functionality.
Definition: avrdevice.h:66
void Reset(unsigned char val)
Definition: rwmem.h:435
unsigned char value
Definition: rwmem.h:215
const char * obj_name
Definition: rwmem.h:240
One byte in any AVR RAM.
Definition: rwmem.h:202
GPIORegister(AvrDevice *core, TraceValueRegister *registry, const std::string &tracename)
Definition: rwmem.h:116
const std::string & GetTraceName(void)
Definition: rwmem.h:88
void hardwareChangeMask(unsigned char val, unsigned char mask)
Definition: rwmem.h:444
void hardwareChange(unsigned char val)
Definition: rwmem.h:439
TraceValueRegister * registry
Definition: rwmem.h:106
virtual ~IOSpecialRegClient()
Definition: rwmem.h:415
unsigned char value
Definition: rwmem.h:196
int cal_type
Definition: rwmem.h:197
unsigned char value
Definition: rwmem.h:151
void Reset(void)
Definition: rwmem.h:124
Implement CLKPR register.
Definition: rwmem.h:135
const bool isInvalid
Definition: rwmem.h:108
unsigned char activate
Definition: rwmem.h:152
A register in IO register space unrelated to any peripheral. "GPIORx" in datasheets.
Definition: rwmem.h:113
void Reset(void)
Register reset functionality, sets internal register value to 0.
Definition: rwmem.h:432
TraceValueCoreRegister * corereg
Definition: rwmem.h:216
void Reset(void)
Definition: rwmem.h:163
Interface class to connect hardware units to control registers.
Definition: rwmem.h:398
void connectSRegClient(IOSpecialRegClient *c)
Registers a client to this IO register to inform this client on read or write access.
Definition: rwmem.h:429
unsigned char value
Internal register value.
Definition: rwmem.h:453
Memory on which access should be avoided! :-)
Definition: rwmem.h:221
Implement XDIV register.
Definition: rwmem.h:156
void set_written()
Definition: traceval.cpp:99
P * p
Definition: rwmem.h:377
IOReg(TraceValueRegister *registry, const std::string &tracename, P *_p, getter_t _g=0, setter_t _s=0, getter_bit_t _gb=0, setter_bit_t _sb=0)
Definition: rwmem.h:297
unsigned char value
Definition: rwmem.h:234
Build a register for TraceValue&#39;s.
Definition: traceval.h:442
TraceValue * tv
Definition: rwmem.h:105
std::vector< IOSpecialRegClient * > clients
clients-list with registered clients
Definition: rwmem.h:447
std::string name() const
Give name (fully qualified), including the index appended if it is >=0.
Definition: traceval.cpp:52
AvrDevice * core
Definition: rwmem.h:223
setter_bit_t sb
Definition: rwmem.h:381
Member of any memory area in an AVR device.
Definition: rwmem.h:42
getter_bit_t gb
Definition: rwmem.h:380
IO register to be specialized for a certain class/hardware.
Definition: pin.h:35
int addr
Definition: rwmem.h:224
unsigned char operator=(unsigned char val)
Write access on memory.
Definition: rwmem.cpp:72
virtual void clear_bit(unsigned int bitaddr)
Definition: rwmem.h:345
#define avr_warning(...)
Definition: avrerror.h:133
getter_t g
Definition: rwmem.h:378
RWMemoryMember(void)
Definition: rwmem.cpp:60
void hardwareChange(unsigned char val)
Definition: rwmem.h:318
void UnregisterTraceValue(TraceValue *t)
Unregisters a TraceValue, remove it from register.
Definition: traceval.cpp:237
const std::string tracename
Definition: rwmem.h:107
unsigned char value
Definition: rwmem.h:170
setter_t s
Definition: rwmem.h:379
bool IsInvalid(void) const
Definition: rwmem.h:89
void change(unsigned val)
Log a change on this value.
Definition: traceval.cpp:68
virtual void set_bit(unsigned int bitaddr)
Definition: rwmem.h:329
virtual ~RWMemoryMember()
Definition: rwmem.h:66
virtual void clear_bit(unsigned int bitaddr)
Definition: rwmem.h:81
const char * reg_name
Definition: rwmem.h:241
Implement OSCCAL register.
Definition: rwmem.h:174
An IO register which is not simulated in the moment. Reads and writes are ignored and produce warning...
Definition: rwmem.h:238
AvrDevice * _core
Definition: rwmem.h:150
unsigned char value
Definition: rwmem.h:131
virtual void set_bit(unsigned int bitaddr)
Definition: rwmem.h:71
void releaseTraceValue(void)
Definition: rwmem.h:320