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/>.
24 #include "AccessViolation.h"
27 Memory::Memory(unsigned int size
) : siz(size
) {
28 mem
= new unsigned char[size
];
35 void Memory::write(unsigned int offset
, unsigned char *block
, unsigned int size
/*= 1*/) {
36 if( offset
+ size
> siz
)
37 throw AccessViolation("Memory::write: writing too much to memory");
39 memcpy( mem
+ offset
, block
, size
);
42 void Memory::fill(unsigned char val
) {
43 memset( mem
, val
, siz
);
46 const unsigned char *Memory::read(unsigned int offset
, unsigned int size
/*= 1*/) const {
47 if( offset
+ size
> siz
)
48 throw AccessViolation("Memory::read: reads too far");
54 * Reads \e size bytes of raw data from memory starting at
55 * offset \e offset. It returns a pointer to this data.
57 * \exception RuntimeException { When the data requested is
58 * not available, this exception is thrown }
60 byte
Memory::readByte(unsigned int offset
) const {
62 throw AccessViolation("Memory::read: reads too far");
67 word
Memory::readWord(unsigned int offset
) const {
69 throw AccessViolation("Memory::read: reads too far");
71 return *((word
*)(mem
+ offset
));
74 void Memory::dump(const char *fname
) const {
75 std::ofstream
fout( fname
, std::ios::out
| std::ios::binary
);
76 if( ! fout
.is_open() )
77 throw AccessViolation( util::format("Memory::dump: failed to open file: %s") % fname
);
79 fout
.write( reinterpret_cast<const char *>(mem
), siz
);