Bug fix: check if vm exists
[avr-sim.git] / Flash.cpp
blob456139a06800518956e58d88f2e72f056a2e38f7
1 #include "Flash.h"
2 #include "Instruction.h"
3 #include "Util.h"
5 #include <algorithm>
7 namespace avr {
9 Flash::Flash(unsigned int size) : Memory(size), decodedMem(size/2) {
10 fill(0xff);
11 decode(0, size);
14 Flash::~Flash() {
15 std::for_each( decodedMem.begin(), decodedMem.end(), Delete<Instruction>() );
18 static Instruction *decodeOpcode(word opcode) {
19 return 0;
22 void Flash::write(unsigned int offset, unsigned char *block, unsigned int size /*= 1*/) {
23 Memory::write(offset, block, size);
24 decode(offset, size);
27 void Flash::decode(unsigned int offset, unsigned int size) {
28 // Convert to words
29 unsigned int addr = offset / 2;
30 unsigned int count = size / 2;
32 for(unsigned int i = 0; i < count; ++i) {
33 word opcode = readWord(offset);
34 // Swap MSB and LSB
35 opcode = ((opcode)>>8) | ((opcode & 0xff)<<8);
36 offset += 2;
38 if( decodedMem[addr] != 0 )
39 delete decodedMem[addr];
40 decodedMem[addr] = decodeOpcode(opcode);
41 addr++;