Allow gdb to request reads on invalid memory, reply with 0's
[avr-sim.git] / src / MMU.h
blobb35a05382ee3f44ff39a04211db59f9868c689ac
1 #ifndef AVR_MMU_H
2 #define AVR_MMU_H
4 #include "Memory.h"
5 #include "SRam.h"
6 #include "Registers.h"
8 namespace avr {
9 class ERam;
11 /**
12 * @author Tom Haber
13 * @date Apr 23, 2008
14 * @brief Memory Mapping Unit
16 * Manages address decoding for memory accesses: maps the different
17 * regions to the correct classes.
19 class MMU {
20 public:
21 MMU(unsigned int ioSpaceSize, unsigned int ramSize, ERam *eram = 0);
22 ~MMU();
24 public:
25 void addIOReg(unsigned int address,
26 const std::string & name, byte initial = 0);
27 const Register & getR(int r) const { return R[r]; }
29 Register & reg(int r);
30 const Register & reg(int r) const;
32 byte statusReg() const;
34 IORegister & getIoreg(unsigned int offset);
35 const IORegister & getIoreg(unsigned int offset) const;
36 IORegister *getIoreg(const std::string & name);
38 void reset();
40 public:
41 unsigned int ioregs() const { return regs.size(); }
42 const std::string & registerName(byte addr) const;
44 public:
45 /**
46 * Reads a single bytes of raw data from memory at
47 * offset \e offset.
49 * \exception AccessViolation { When the data requested is
50 * not available, this exception is thrown }
52 byte readByte(unsigned int offset) const;
54 /**
55 * Reads a word of raw data from memory starting at
56 * offset \e offset.
58 * \exception AccessViolation { When the data requested is
59 * not available, this exception is thrown }
61 word readWord(unsigned int offset) const;
63 /**
64 * Reads a single bytes of raw data from memory at
65 * offset \e offset.
67 * \exception AccessViolation { When the data requested is
68 * not available, this exception is thrown }
70 void writeByte(unsigned int offset, byte val);
72 /**
73 * Reads a word of raw data from memory starting at
74 * offset \e offset.
76 * \exception AccessViolation { When the data requested is
77 * not available, this exception is thrown }
79 void writeWord(unsigned int offset, word val);
81 const unsigned char *readRam(unsigned int offset, unsigned int size) const;
82 void writeRam(unsigned char *data, unsigned int offset, unsigned int size);
84 public:
85 static const unsigned int registerSpaceSize = 32;
87 private:
88 Register R[registerSpaceSize];
90 IORegisters regs;
91 SRam sram;
92 ERam *eram;
95 inline IORegister & MMU::getIoreg(unsigned int offset) {
96 return regs.getIoreg(offset);
99 inline const IORegister & MMU::getIoreg(unsigned int offset) const {
100 return regs.getIoreg(offset);
103 inline IORegister *MMU::getIoreg(const std::string & name) {
104 return regs.getIoreg(name);
107 inline byte MMU::statusReg() const {
108 return regs.getIoreg("SREG")->getAddress() - registerSpaceSize;
111 inline void MMU::reset() {
112 for(unsigned int i = 0; i < registerSpaceSize; ++i)
113 R[i] = 0;
114 regs.reset();
119 #endif /*AVR_MMU_H_*/