-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_bus.cpp
158 lines (113 loc) · 3.77 KB
/
memory_bus.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "error.h"
#include "optimize.h"
#include "memory_bus.h"
#include "processor_utils.h"
#include "exceptions.h"
#ifdef _DEBUG
uint64_t index_dist = 0, index_cnt = 0;
#endif
memory_bus::memory_bus() : list(NULL), n_elements(0)
{
}
memory_bus::~memory_bus()
{
free(list);
}
void memory_bus::register_memory(uint64_t offset, uint64_t size, memory *target)
{
n_elements++;
list = (memory_segment_t *)realloc(list, n_elements * sizeof(memory_segment_t));
if (!list)
error_exit("Memory allocation error (register_memory)");
list[n_elements - 1].offset_start = offset;
list[n_elements - 1].offset_end = offset + size;
if (list[n_elements - 1].offset_end < list[n_elements - 1].offset_start)
error_exit("Segment wraps");
list[n_elements - 1].target = target;
// pdc -> dc_log("BUS: register %016llx / %016llx", offset, size);
last_index_i = last_index = 0;
last_psegment_i = last_psegment = &list[last_index];
}
// r/w might overlap segments? FIXME
const memory_segment_t * memory_bus::find_segment(uint64_t offset)
{
int start_last_index = last_index;
for(;;)
{
if (offset >= last_psegment -> offset_start && offset < last_psegment -> offset_end)
return last_psegment;
last_index++;
last_index %= n_elements;
if (last_index == start_last_index)
break;
last_psegment = &list[last_index];
}
// pdc -> dc_log("%016llx is not mapped", offset);
throw processor_exception(offset, -1, -1, PEE_MEM, -1);
return NULL; // should not be reached
}
const memory_segment_t * memory_bus::find_segment_i(uint64_t offset)
{
int start_last_index = last_index_i;
for(;;)
{
if (offset >= last_psegment_i -> offset_start && offset < last_psegment_i -> offset_end)
return last_psegment_i;
last_index_i++;
last_index_i %= n_elements;
if (last_index_i == start_last_index)
break;
last_psegment_i = &list[last_index_i];
}
// pdc -> dc_log("%016llx is not mapped", offset);
throw processor_exception(offset, -1, -1, PEE_MEM, -1);
return NULL; // should not be reached
}
void memory_bus::read_64b(uint64_t offset, uint64_t *data)
{
const memory_segment_t * segment = find_segment(offset);
segment -> target -> read_64b(offset - segment -> offset_start, data);
}
void memory_bus::write_64b(uint64_t offset, uint64_t data)
{
const memory_segment_t * segment = find_segment(offset);
segment -> target -> write_64b(offset - segment -> offset_start, data);
}
void memory_bus::read_32b_i(uint64_t offset, uint32_t *data)
{
const memory_segment_t * segment = find_segment_i(offset);
segment -> target -> read_32b(offset - segment -> offset_start, data);
}
void memory_bus::read_32b(uint64_t offset, uint32_t *data)
{
const memory_segment_t * segment = find_segment(offset);
segment -> target -> read_32b(offset - segment -> offset_start, data);
}
void memory_bus::write_32b(uint64_t offset, uint32_t data)
{
const memory_segment_t * segment = find_segment(offset);
segment -> target -> write_32b(offset - segment -> offset_start, data);
}
void memory_bus::read_16b(uint64_t offset, uint16_t *data)
{
const memory_segment_t * segment = find_segment(offset);
segment -> target -> read_16b(offset - segment -> offset_start, data);
}
void memory_bus::write_16b(uint64_t offset, uint16_t data)
{
const memory_segment_t * segment = find_segment(offset);
segment -> target -> write_16b(offset - segment -> offset_start, data);
}
void memory_bus::read_8b(uint64_t offset, uint8_t *data)
{
const memory_segment_t * segment = find_segment(offset);
segment -> target -> read_8b(offset - segment -> offset_start, data);
}
void memory_bus::write_8b(uint64_t offset, uint8_t data)
{
const memory_segment_t * segment = find_segment(offset);
segment -> target -> write_8b(offset - segment -> offset_start, data);
}