aboutsummaryrefslogtreecommitdiff
path: root/fesvr/memif.cc
blob: e56bd94326f9a86a134772dc493490dce8765284 (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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// See LICENSE for license details.

#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <stdexcept>
#include "memif.h"

void memif_t::read(addr_t addr, size_t len, void* bytes)
{
  size_t align = cmemif->chunk_align();
  if (len && (addr & (align-1)))
  {
    size_t this_len = std::min(len, align - size_t(addr & (align-1)));
    uint8_t chunk[align];

    cmemif->read_chunk(addr & ~(align-1), align, chunk);
    memcpy(bytes, chunk + (addr & (align-1)), this_len);

    bytes = (char*)bytes + this_len;
    addr += this_len;
    len -= this_len;
  }

  if (len & (align-1))
  {
    size_t this_len = len & (align-1);
    size_t start = len - this_len;
    uint8_t chunk[align];

    cmemif->read_chunk(addr + start, align, chunk);
    memcpy((char*)bytes + start, chunk, this_len);

    len -= this_len;
  }

  // now we're aligned
  for (size_t pos = 0; pos < len; pos += cmemif->chunk_max_size())
    cmemif->read_chunk(addr + pos, std::min(cmemif->chunk_max_size(), len - pos), (char*)bytes + pos);
}

void memif_t::write(addr_t addr, size_t len, const void* bytes)
{
  size_t align = cmemif->chunk_align();
  if (len && (addr & (align-1)))
  {
    size_t this_len = std::min(len, align - size_t(addr & (align-1)));
    uint8_t chunk[align];

    cmemif->read_chunk(addr & ~(align-1), align, chunk);
    memcpy(chunk + (addr & (align-1)), bytes, this_len);
    cmemif->write_chunk(addr & ~(align-1), align, chunk);

    bytes = (char*)bytes + this_len;
    addr += this_len;
    len -= this_len;
  }

  if (len & (align-1))
  {
    size_t this_len = len & (align-1);
    size_t start = len - this_len;
    uint8_t chunk[align];

    cmemif->read_chunk(addr + start, align, chunk);
    memcpy(chunk, (char*)bytes + start, this_len);
    cmemif->write_chunk(addr + start, align, chunk);

    len -= this_len;
  }

  // now we're aligned
  bool all_zero = len != 0;
  for (size_t i = 0; i < len; i++)
    all_zero &= ((const char*)bytes)[i] == 0;

  if (all_zero) {
    cmemif->clear_chunk(addr, len);
  } else {
    size_t max_chunk = cmemif->chunk_max_size();
    for (size_t pos = 0; pos < len; pos += max_chunk)
      cmemif->write_chunk(addr + pos, std::min(max_chunk, len - pos), (char*)bytes + pos);
  }
}

#define MEMIF_READ_FUNC \
  if(addr & (sizeof(val)-1)) \
    throw std::runtime_error("misaligned address"); \
  this->read(addr, sizeof(val), &val); \
  return val

#define MEMIF_WRITE_FUNC \
  if(addr & (sizeof(val)-1)) \
    throw std::runtime_error("misaligned address"); \
  this->write(addr, sizeof(val), &val)

target_endian<uint8_t> memif_t::read_uint8(addr_t addr)
{
  target_endian<uint8_t> val;
  MEMIF_READ_FUNC;
}

target_endian<int8_t> memif_t::read_int8(addr_t addr)
{
  target_endian<int8_t> val;
  MEMIF_READ_FUNC;
}

void memif_t::write_uint8(addr_t addr, target_endian<uint8_t> val)
{
  MEMIF_WRITE_FUNC;
}

void memif_t::write_int8(addr_t addr, target_endian<int8_t> val)
{
  MEMIF_WRITE_FUNC;
}

target_endian<uint16_t> memif_t::read_uint16(addr_t addr)
{
  target_endian<uint16_t> val;
  MEMIF_READ_FUNC;
}

target_endian<int16_t> memif_t::read_int16(addr_t addr)
{
  target_endian<int16_t> val;
  MEMIF_READ_FUNC;
}

void memif_t::write_uint16(addr_t addr, target_endian<uint16_t> val)
{
  MEMIF_WRITE_FUNC;
}

void memif_t::write_int16(addr_t addr, target_endian<int16_t> val)
{
  MEMIF_WRITE_FUNC;
}

target_endian<uint32_t> memif_t::read_uint32(addr_t addr)
{
  target_endian<uint32_t> val;
  MEMIF_READ_FUNC;
}

target_endian<int32_t> memif_t::read_int32(addr_t addr)
{
  target_endian<int32_t> val;
  MEMIF_READ_FUNC;
}

void memif_t::write_uint32(addr_t addr, target_endian<uint32_t> val)
{
  MEMIF_WRITE_FUNC;
}

void memif_t::write_int32(addr_t addr, target_endian<int32_t> val)
{
  MEMIF_WRITE_FUNC;
}

target_endian<uint64_t> memif_t::read_uint64(addr_t addr)
{
  target_endian<uint64_t> val;
  MEMIF_READ_FUNC;
}

target_endian<int64_t> memif_t::read_int64(addr_t addr)
{
  target_endian<int64_t> val;
  MEMIF_READ_FUNC;
}

void memif_t::write_uint64(addr_t addr, target_endian<uint64_t> val)
{
  MEMIF_WRITE_FUNC;
}

void memif_t::write_int64(addr_t addr, target_endian<int64_t> val)
{
  MEMIF_WRITE_FUNC;
}