aboutsummaryrefslogtreecommitdiffstats
path: root/os/various/memtest.cpp
blob: 49828eca25ec62a2a47e5914d4e169671a77ae53 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
    ChibiOS/RT - Copyright (C) 2013-2014 Uladzimir Pylinsky aka barthess

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>

#include "memtest.hpp"

/*
 *
 */
template <typename T>
class Generator {
public:
  Generator(void) : pattern(0) {;}
  virtual T get(void) = 0;
  virtual testtype get_type(void) = 0;
  virtual void init(T seed) {
    pattern = seed;
  }
protected:
  T pattern;
};

/*
 *
 */
template <typename T>
class GeneratorWalkingOne : public Generator<T> {
  T get(void) {
    T ret = this->pattern;

    this->pattern <<= 1;
    if (0 == this->pattern)
      this->pattern = 1;

    return ret;
  }

  testtype get_type(void) {
    return MEMTEST_WALKING_ONE;
  }
};

/*
 *
 */
template <typename T>
class GeneratorWalkingZero : public Generator<T> {
  T get(void) {
    T ret = ~this->pattern;

    this->pattern <<= 1;
    if (0 == this->pattern)
      this->pattern = 1;

    return ret;
  }

  testtype get_type(void) {
    return MEMTEST_WALKING_ZERO;
  }
};

/*
 *
 */
template <typename T>
class GeneratorOwnAddress : public Generator<T> {
  T get(void) {
    T ret = this->pattern;
    this->pattern++;
    return ret;
  }

  testtype get_type(void) {
    return MEMTEST_OWN_ADDRESS;
  }
};

/*
 *
 */
template <typename T>
class GeneratorMovingInv : public Generator<T> {
  T get(void) {
    T ret = this->pattern;
    this->pattern = ~this->pattern;
    return ret;
  }

  testtype get_type(void) {
    if ((this->pattern == 0) || ((this->pattern & 0xFF) == 0xFF))
      return MEMTEST_MOVING_INVERSION_ZERO;
    else
      return MEMTEST_MOVING_INVERSION_55AA;
  }
};

/*
 *
 */
template <typename T>
class GeneratorMovingInvRand : public Generator<T> {
public:
  GeneratorMovingInvRand(void) : step(0), prev(0){;}
  void init(T seed) {
    srand(seed);
    step = 0;
    prev = 0;
  }

  T get(void) {
    T ret;
    T mask = -1;
    if ((step & 1) == 0) {
      ret = rand() & mask;
      prev = ret;
    }
    else {
      ret = ~prev & mask;
    }
    step++;

    return ret;
  }

  testtype get_type(void) {
    return MEMTEST_MOVING_INVERSION_RAND;
  }

private:
  size_t step;
  T prev;
};

/*
 *
 */
template <typename T>
static void memtest_sequential(memtest_t *testp, Generator<T> &generator, T seed) {
  const size_t steps = testp->size / sizeof(T);
  size_t i;
  T *mem = static_cast<T *>(testp->start);

  /* fill ram */
  generator.init(seed);
  for (i=0; i<steps; i++)
    mem[i] = generator.get();

  /* read back and compare */
  generator.init(seed);
  for (i=0; i<steps; i++) {
    if (mem[i] != generator.get()) {
      testp->ecb(testp, generator.get_type(), i*sizeof(T));
      return;
    }
  }
}

template <typename T>
static void walking_one(memtest_t *testp) {
  GeneratorWalkingOne<T> generator;
  memtest_sequential<T>(testp, generator, 1);
}

template <typename T>
static void walking_zero(memtest_t *testp) {
  GeneratorWalkingZero<T> generator;
  memtest_sequential<T>(testp, generator, 1);
}

template <typename T>
static void own_address(memtest_t *testp) {
  GeneratorOwnAddress<T> generator;
  memtest_sequential<T>(testp, generator, 0);
}

template <typename T>
static void moving_inversion_zero(memtest_t *testp) {
  GeneratorMovingInv<T> generator;
  T mask = -1;
  memtest_sequential<T>(testp, generator, 0);
  memtest_sequential<T>(testp, generator, 0xFFFFFFFF & mask);
}

template <typename T>
static void moving_inversion_55aa(memtest_t *testp) {
  GeneratorMovingInv<T> generator;
  T mask = -1;
  memtest_sequential<T>(testp, generator, 0x55555555 & mask);
  memtest_sequential<T>(testp, generator, 0xAAAAAAAA & mask);
}

template <typename T>
static void moving_inversion_rand(memtest_t *testp) {
  GeneratorMovingInvRand<T> generator;
  T mask = -1;
  memtest_sequential<T>(testp, generator, testp->rand_seed & mask);
}

/*
 *
 */
static void memtest_wrapper(memtest_t *testp,
                            void (*p_u8)(memtest_t *testp),
                            void (*p_u16)(memtest_t *testp),
                            void (*p_u32)(memtest_t *testp)) {
  switch(testp->width) {
  case MEMTEST_WIDTH_32:
    p_u8(testp);
    p_u16(testp);
    p_u32(testp);
    break;
  case MEMTEST_WIDTH_16:
    p_u8(testp);
    p_u16(testp);
    break;
  case MEMTEST_WIDTH_8:
    p_u8(testp);
    break;
  }
}

/*
 *
 */
void memtest_run(memtest_t *testp, uint32_t testmask) {

  if (testmask & MEMTEST_WALKING_ONE) {
    memtest_wrapper(testp,
        walking_one<uint8_t>,
        walking_one<uint16_t>,
        walking_one<uint32_t>);
  }

  if (testmask & MEMTEST_WALKING_ZERO) {
    memtest_wrapper(testp,
        walking_zero<uint8_t>,
        walking_zero<uint16_t>,
        walking_zero<uint32_t>);
  }

  if (testmask & MEMTEST_OWN_ADDRESS) {
    memtest_wrapper(testp,
        own_address<uint8_t>,
        own_address<uint16_t>,
        own_address<uint32_t>);
  }

  if (testmask & MEMTEST_MOVING_INVERSION_ZERO) {
    memtest_wrapper(testp,
        moving_inversion_zero<uint8_t>,
        moving_inversion_zero<uint16_t>,
        moving_inversion_zero<uint32_t>);
  }

  if (testmask & MEMTEST_MOVING_INVERSION_55AA) {
    memtest_wrapper(testp,
        moving_inversion_55aa<uint8_t>,
        moving_inversion_55aa<uint16_t>,
        moving_inversion_55aa<uint32_t>);
  }

  if (testmask & MEMTEST_MOVING_INVERSION_RAND) {
    memtest_wrapper(testp,
        moving_inversion_rand<uint8_t>,
        moving_inversion_rand<uint16_t>,
        moving_inversion_rand<uint32_t>);
  }
}