[go: up one dir, main page]

File: memory.cc

package info (click to toggle)
srecord 1.58-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,144 kB
  • sloc: cpp: 26,774; sh: 7,053; makefile: 2,889; awk: 187; vhdl: 15
file content (466 lines) | stat: -rw-r--r-- 11,180 bytes parent folder | download | duplicates (2)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//
// srecord - manipulate eprom load files
// Copyright (C) 1998-2003, 2006-2011 Peter Miller
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see
// <http://www.gnu.org/licenses/>.
//

#include <cstring>

#include <srecord/input.h>
#include <srecord/memory.h>
#include <srecord/memory/walker/compare.h>
#include <srecord/memory/walker/continuity.h>
#include <srecord/record.h>


bool srecord::memory::overwrite = false;


srecord::memory::memory() :
    nchunks(0),
    nchunks_max(0),
    chunk(0),
    cache(0),
    find_next_chunk_index(0),
    header(0),
    execution_start_address(0)
{
}


srecord::memory::memory(const srecord::memory &arg) :
    nchunks(0),
    nchunks_max(0),
    chunk(0),
    cache(0),
    find_next_chunk_index(0),
    header(0),
    execution_start_address(0)
{
    copy(arg);
}


srecord::memory &
srecord::memory::operator=(const srecord::memory &arg)
{
    if (&arg != this)
    {
        clear();
        copy(arg);
    }
    return *this;
}


srecord::memory::~memory()
{
    clear();
}


void
srecord::memory::clear()
{
    delete header;
    header = 0;
    delete execution_start_address;
    execution_start_address = 0;
    for (int j = 0; j < nchunks; ++j)
        delete chunk[j];
    if (chunk)
        delete [] chunk;
    nchunks = 0;
    nchunks_max = 0;
    chunk = 0;
    cache = 0;
}


void
srecord::memory::copy(const srecord::memory &arg)
{
    delete header;
    header = 0;
    if (arg.header)
        header = new srecord::record(*arg.header);

    delete execution_start_address;
    execution_start_address = 0;
    if (arg.execution_start_address)
    {
        execution_start_address =
            new srecord::record(*arg.execution_start_address);
    }

    nchunks = arg.nchunks;
    while (nchunks_max < nchunks)
        nchunks_max = nchunks_max * 2 + 4;
    chunk = new srecord::memory_chunk * [nchunks_max];
    for (int j = 0; j < nchunks; ++j)
    {
        // use copy-new to make the copies
        chunk[j] = new srecord::memory_chunk(*(arg.chunk[j]));
    }
}


srecord::memory_chunk *
srecord::memory::find(unsigned long address)
    const
{
    //
    // Speed things up if we've been there recently.
    //
    if (cache && cache->get_address() == address)
        return cache;

    //
    // Binary chop to find the appropriate chunk.
    //
    int min = 0;
    int max = nchunks - 1;
    srecord::memory_chunk *mcp = 0;
    while (min <= max)
    {
        int mid = (min + max) / 2;
        mcp = chunk[mid];
        if (mcp->get_address() == address)
        {
            cache = mcp;
            return cache;
        }
        if (address < mcp->get_address())
            max = mid - 1;
        else
            min = mid + 1;
    }

    //
    // We need a new row.  Make sure there is enough room.
    //
    if (nchunks >= nchunks_max)
    {
        nchunks_max = nchunks_max * 2 + 4;
        srecord::memory_chunk **tmp =
            new srecord::memory_chunk * [nchunks_max];
        for (int j = 0; j < nchunks; ++j)
            tmp[j] = chunk[j];
        delete [] chunk;
        chunk = tmp;
    }

    //
    // Insert the new chunk.
    //
    mcp = new srecord::memory_chunk(address);
    for (int up = nchunks; up > min; --up)
        chunk[up] = chunk[up - 1];
    ++nchunks;
    chunk[min] = mcp;

    cache = mcp;
    return mcp;
}


void
srecord::memory::set(unsigned long address, int datum)
{
    unsigned long address_hi = address / srecord::memory_chunk::size;
    unsigned long address_lo = address % srecord::memory_chunk::size;
    srecord::memory_chunk *mcp = find(address_hi);
    mcp->set(address_lo, datum);
}


int
srecord::memory::get(unsigned long address)
    const
{
    unsigned long address_hi = address / srecord::memory_chunk::size;
    unsigned long address_lo = address % srecord::memory_chunk::size;
    srecord::memory_chunk *mcp = find(address_hi);
    return mcp->get(address_lo);
}


bool
srecord::memory::set_p(unsigned long address)
    const
{
    unsigned long address_hi = address / srecord::memory_chunk::size;
    unsigned long address_lo = address % srecord::memory_chunk::size;
    srecord::memory_chunk *mcp = find(address_hi);
    return mcp->set_p(address_lo);
}


bool
srecord::memory::equal(const srecord::memory &lhs, const srecord::memory &rhs)
{
    if (lhs.nchunks != rhs.nchunks)
        return false;
    for (int j = 0; j < lhs.nchunks; ++j)
        if (lhs.chunk[j][0] != rhs.chunk[j][0])
            return false;
    return true;
}



bool
srecord::memory::compare(const srecord::memory &lhs, const srecord::memory &rhs)
{
    srecord::memory_walker_compare::pointer wlhs =
        srecord::memory_walker_compare::create(rhs, true);
    lhs.walk(wlhs);
    wlhs->print("Left");
    srecord::memory_walker_compare::pointer wrhs =
        srecord::memory_walker_compare::create(lhs, false);
    rhs.walk(wrhs);
    wrhs->print("Right");
    return (!wlhs->same() || !wrhs->same());
}


unsigned long
srecord::memory::get_upper_bound()
    const
{
    if (nchunks == 0)
        return 0;
    return chunk[nchunks - 1]->get_upper_bound();
}


void
srecord::memory::walk(srecord::memory_walker::pointer w)
    const
{
    w->notify_upper_bound(get_upper_bound());
    w->observe_header(get_header());
    for (int j = 0; j < nchunks; ++j)
        chunk[j]->walk(w);

    // Only write an execution start address record if we were given one.
    if (execution_start_address)
        w->observe_start_address(get_execution_start_address());
}


void
srecord::memory::reader(const srecord::input::pointer &ifp, bool barf)
{
    srecord::record record;
    while (ifp->read(record))
    {
        switch (record.get_type())
        {
        case srecord::record::type_header:
            if (!header)
            {
                header = new srecord::record(record);
            }
            break;

        case srecord::record::type_unknown:
        case srecord::record::type_data_count:
            break;

        case srecord::record::type_data:
            //
            // For each data byte, we have to check for duplicates.  We
            // issue warnings for redundant settings, and we issue error
            // for contradictory settings.
            //
            for (size_t j = 0; j < record.get_length(); ++j)
            {
                srecord::record::address_t address = record.get_address() + j;
                int n = record.get_data(j);
                if (barf && set_p(address))
                {
                    int old = get(address);
                    if (n == old)
                    {
                        ifp->warning
                        (
                            "redundant 0x%08lX value (0x%02X)",
                            (long)address,
                            n
                        );
                    }
                    else if (overwrite)
                    {
                        ifp->warning
                        (
                            "multiple 0x%08lX values (previous = 0x%02X, "
                                "this 
                            (long)address,
                            old,
                            n
                        );
                    }
                    else
                    {
                        ifp->fatal_error
                        (
                            "contradictory 0x%08lX value (previous = 0x%02X, "
                                "this 
                            (long)address,
                            old,
                            n
                        );
                    }
                }
                set(address, n);
            }
            break;

        case srecord::record::type_execution_start_address:
            if (!execution_start_address)
            {
                execution_start_address = new srecord::record(record);
            }
            break;
        }
    }
}


bool
operator == (const srecord::memory &lhs, const srecord::memory &rhs)
{
    return srecord::memory::equal(lhs, rhs);
}


bool
operator != (const srecord::memory &lhs, const srecord::memory &rhs)
{
    return !srecord::memory::equal(lhs, rhs);
}


srecord::memory_chunk *
srecord::memory::find_next_chunk(unsigned long address)
    const
{
    //
    // This method is generally called sequentially, to visit each
    // and every byte, in cases where walk() is not appropriate.
    // As such, a binary chop isn't necessary.
    //
    if (find_next_chunk_index < nchunks)
    {
        srecord::memory_chunk *mcp = chunk[find_next_chunk_index];
        if (mcp->get_address() > address)
            find_next_chunk_index = 0;
    }
    while (find_next_chunk_index < nchunks)
    {
        srecord::memory_chunk *mcp = chunk[find_next_chunk_index];
        if (mcp->get_address() >= address)
            return mcp;
        find_next_chunk_index++;
    }
    return 0;
}


bool
srecord::memory::find_next_data(unsigned long &address, void *data,
    size_t &nbytes) const
{
    unsigned long address_hi = address / srecord::memory_chunk::size;
    for (;;)
    {
        srecord::memory_chunk *mcp = find_next_chunk(address_hi);
        if (!mcp)
            return false;
        if (mcp->find_next_data(address, data, nbytes))
            return true;
        address_hi = mcp->get_address() + 1;
        address = address_hi * srecord::memory_chunk::size;
    }
}


void
srecord::memory::allow_overwriting()
{
    overwrite = true;
}


srecord::record *
srecord::memory::get_header()
    const
{
    return header;
}


void
srecord::memory::set_header(const char *s)
{
    delete header;
    size_t len = strlen(s);
    if (len > srecord::record::max_data_length)
        len = srecord::record::max_data_length;
    header =
        new srecord::record
        (
            srecord::record::type_header,
            0,
            (srecord::record::data_t *)s,
            len
        );
}


srecord::record *
srecord::memory::get_execution_start_address()
    const
{
    return execution_start_address;
}


void
srecord::memory::set_execution_start_address(unsigned long addr)
{
    delete execution_start_address;
    execution_start_address =
        new srecord::record
        (
            srecord::record::type_execution_start_address,
            addr,
            0,
            0
        );
}


bool
srecord::memory::has_holes()
    const
{
    srecord::memory_walker_continuity::pointer sniffer =
        srecord::memory_walker_continuity::create();
    walk(sniffer);
    return (!sniffer->is_continuous());
}