This repository has been archived by the owner on Apr 15, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
memory.cc
138 lines (115 loc) · 2.94 KB
/
memory.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <stdio.h>
#include <iostream>
#include <sstream>
#include "memory.h"
using namespace std;
Memory::Memory():size(0x00){
virt_flg = false;
}
Memory::~Memory(){}
void Memory::Init(int size){
this->size = size;
if(mem.size() == 0)
mem.clear();
mem.resize(size);
virt_flg = false;
minfo.clear();
return;
}
int Memory::GetSize(){
return size;
}
uint8_t Memory::GetData8(uint32_t addr) {
if(addr > static_cast<uint32_t>(size)){
stringstream ss;
ss<<"memory: out of range address ("<<hex<<showbase<<addr<<")";
throw ss.str();
}
uint8_t ret = mem[addr];
// std::cout<<"minfo num ="<<minfo.size()<<std::endl;
if(minfo.empty()) return ret;
for(unsigned int i=0;i<minfo.size();i++){
if(addr < minfo[i].addr) continue;
if(addr > minfo[i].end_addr) continue;
if(minfo[i].mem != nullptr){
ret = minfo[i].mem[addr - minfo[i].addr];
return ret;
}
if(minfo[i].dev != nullptr){
minfo[i].dev->MemoryMappedProc(this, addr);
return ret;
}
}
return ret;
}
uint16_t Memory::GetData16Little(uint32_t addr){
uint32_t ret = 0x00;
for(auto i=0;i<2;i++)
ret |= GetData8(addr + i) << (i*8);
return ret;
}
uint32_t Memory::GetData32Little(uint32_t addr){
uint32_t ret = 0x00;
for(int i=0;i<4;i++)
ret |= GetData8(addr + i) << (i*8);
return ret;
}
/*
uint32_t Memory::operator[](uint32_t addr){
if(endian == ENDIAN::LITTLE){
return GetData32Little(addr);
}else{
throw "big endian";
}
}
*/
void Memory::SetData16Little(uint32_t addr, uint16_t val){
for(auto i=0;i<2;i++)
SetData8(addr+i, val>>(i*8));
}
void Memory::SetData32Little(uint32_t addr, uint32_t val){
for(int i=0;i<4;i++)
SetData8(addr+i, val>>(i*8));
return;
}
void Memory::MapDevice(Device::Base *dev, uint32_t addr, unsigned int size){
// throw "Memory: MapDevice() not implemented.";
MapInfo mi;
mi.dev = dev;
mi.mem = nullptr;
mi.addr = addr;
mi.end_addr = addr + size;
minfo.push_back(mi);
}
void Memory::MapMemory(uint8_t *mem, uint32_t addr, unsigned int size){
// throw "Memory: MapMemory() not implemented.";
MapInfo mi;
mi.dev = nullptr;
mi.mem = mem;
mi.addr = addr;
mi.end_addr = addr + size;
minfo.push_back(mi);
}
void Memory::LoadBinary(FILE *fp, uint32_t addr, size_t size){
if(fp == nullptr) throw "FILE* is nullptr";
// fread(mem + addr, 1, size, fp);
cout<<"fread: "<<"size: "<<size<<std::endl;
fread(&mem[addr], sizeof(uint8_t), size, fp);
}
void Memory::LoadBinary(const char *fname, uint32_t addr, size_t size){
FILE *fp;
// uint8_t test = this->operator[](addr+size);
cout<<"loading binary from file: \""<<fname<<"\""<<endl;
fp = fopen(fname, "rb");
if(fp == nullptr) throw "memory: LoadBinary: can't open file";
LoadBinary(fp, addr, size);
fclose(fp);
}
void Memory::Dump(const char *fname, uint32_t addr, unsigned int size){
// uint8_t test = this->operator[](addr+size);
FILE *fp;
fp = fopen(fname, "wb");
if(fp == NULL) throw "memory: Dump: can't open file.";
fwrite(&mem[addr], sizeof(uint8_t), size, fp);
fclose(fp);
}