-
Notifications
You must be signed in to change notification settings - Fork 4
/
memory.cpp
90 lines (66 loc) · 1.49 KB
/
memory.cpp
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
#include <endian.h>
#include <string.h>
#include "error.h"
#include "debug.h"
#include "memory.h"
memory::memory() : pm(NULL), len(0)
{
}
memory::memory(uint64_t size, bool init) : len(size)
{
if (size == 0)
error_exit("memory::memory invalid size");
pm = new unsigned char[size];
if (init)
memset(pm, 0x00, size);
}
memory::memory(unsigned char *p, uint64_t size) : pm(p), len(size)
{
if (size == 0)
error_exit("memory::memory invalid size");
}
memory::~memory()
{
delete [] pm;
}
void memory::read_64b(uint64_t offset, uint64_t *data)
{
ASSERT(offset + 7 < len);
unsigned char *dummy_p = &pm[offset];
*data = be64toh(*(uint64_t *)dummy_p);
}
void memory::read_32b(uint64_t offset, uint32_t *data)
{
ASSERT(offset + 3 < len);
*data = be32toh(*(uint32_t *)&pm[offset]);
}
void memory::read_16b(uint64_t offset, uint16_t *data)
{
ASSERT(offset + 1 < len);
*data = be16toh(*(uint16_t *)&pm[offset]);
}
void memory::read_8b(uint64_t offset, uint8_t *data)
{
ASSERT(offset < len);
*data = pm[offset];
}
void memory::write_64b(uint64_t offset, uint64_t data)
{
ASSERT(offset + 7 < len);
*(uint64_t *)&pm[offset] = htobe64(data);
}
void memory::write_32b(uint64_t offset, uint32_t data)
{
ASSERT(offset + 3 < len);
*(uint32_t *)&pm[offset] = htobe32(data);
}
void memory::write_16b(uint64_t offset, uint16_t data)
{
ASSERT(offset + 1 < len);
*(uint16_t *)&pm[offset] = htobe16(data);
}
void memory::write_8b(uint64_t offset, uint8_t data)
{
ASSERT(offset < len);
pm[offset] = data;
}