blob: ddd9e4c126b69342e9975565ce8418580ac10aff (
plain)
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
|
#include "Memory.h"
#include <algorithm>
#include <cstddef>
#include <vector>
#include "Assembly.h"
Memory::Memory() noexcept :
content {}
{}
void Memory::apply(AssemblyContext ctx)
{
ctx.bind("/read", [&](std::size_t len, std::size_t off) -> std::vector<char> { return read(len, off); });
ctx.bind("/write", [&](const std::vector<char> data, std::size_t off) -> bool { return write(data, off); });
}
auto Memory::read(std::size_t len, std::size_t off) const -> std::vector<char>
{
if (content.size() < off)
return {};
auto real_len = std::min(len, content.size() - off);
auto data = content.data() + off;
return {data, data + real_len};
}
bool Memory::write(const std::vector<char> data, std::size_t off)
{
if (content.size() < off || data.empty())
return false;
std::size_t in_data = 0;
std::size_t in_content = off;
for (; in_data < data.size() && in_content < content.size(); ++in_data, ++in_content)
content[in_content] = data[in_data];
return true;
}
|