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.h
105 lines (84 loc) · 2.58 KB
/
memory.h
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
#ifndef MEMORY_H_
#define MEMORY_H_
#include <cstdio>
#include <stdint.h>
#include <vector>
#include "common.h"
#include "device/device.h"
enum class ENDIAN { BIG, LITTLE, };
namespace Device {
class Base;
}
struct MapInfo {
uint32_t addr;
uint32_t end_addr;
Device::Base *dev;
uint8_t *mem;
};
class Memory {
public:
ENDIAN endian;
int size;
Memory();
~Memory();
void Init(int size);
int GetSize();
bool IsVirt() const { return virt_flg; }
void EnableVirt(){ virt_flg=true; }
void UnableVirt(){ virt_flg=false; }
std::vector<uint8_t>& GetRaw(){ return mem; }
// Memory* operator->(){ return this; }
// little endianでのuint32_tの読み込み処理はEmulatorクラスから持ってくる
uint8_t operator [] (uint32_t addr){
return GetData8(addr);
}
//int8_t operator [] (uint32_t addr){ return static_cast<int8_t>(operator=(addr)); }
// uint32_t operator[](uint32_t addr);
uint8_t GetData8(uint32_t addr);
uint16_t GetData16Big(uint32_t addr){ throw "not implemented: big endian"; }
uint16_t GetData16Little(uint32_t addr);
inline uint16_t GetData16(uint32_t addr){
if(endian == ENDIAN::BIG) return GetData32Big(addr);
return GetData32Little(addr);
}
uint32_t GetData32Big(uint32_t addr){ throw "not implemented: big endian"; }
uint32_t GetData32Little(uint32_t addr);
inline uint32_t GetData32(uint32_t addr){
if(endian == ENDIAN::BIG) return GetData32Big(addr);
return GetData32Little(addr);
}
void SetData8(uint32_t addr, uint8_t val){
if(addr > static_cast<uint32_t>(size)){
DOUT("addr=0x"<<std::hex<<addr);
throw "out of mem";
}
mem[addr] = val;
}
void SetData16Big(uint32_t addr, uint16_t val){ throw "not implemented: big endian"; }
void SetData16Little(uint32_t addr, uint16_t val);
inline void SetData16(uint32_t addr, uint16_t val){
if(endian == ENDIAN::BIG)
SetData16Big(addr,val);
else
SetData16Little(addr,val);
}
void SetData32Big(uint32_t addr, uint32_t val){ throw "not implemented: big endian"; }
void SetData32Little(uint32_t addr, uint32_t val);
inline void SetData32(uint32_t addr, uint32_t val){
if(endian == ENDIAN::BIG)
SetData32Big(addr, val);
else
SetData32Little(addr, val);
}
void MapDevice(Device::Base *dev, uint32_t addr, unsigned int size);
void MapMemory(uint8_t *mem, uint32_t addr, unsigned int size);
void LoadBinary(FILE *fp, uint32_t addr, size_t size);
void LoadBinary(const char *fname, uint32_t addr, size_t size);
void Dump(const char *fname, uint32_t addr, unsigned int size);
private:
std::vector<uint8_t> mem;
std::vector<MapInfo> minfo;
int minfo_num;
bool virt_flg;
};
#endif