[go: up one dir, main page]

File: record.cc

package info (click to toggle)
srecord 1.39-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 3,624 kB
  • ctags: 1,822
  • sloc: cpp: 18,283; sh: 5,839; makefile: 1,701; awk: 201; vhdl: 15
file content (151 lines) | stat: -rw-r--r-- 2,986 bytes parent folder | download
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
//
//      srecord - manipulate eprom load files
//      Copyright (C) 1998, 1999, 2001, 2002, 2006, 2007 Peter Miller
//
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU 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 General Public License for more details.
//
//      You should have received a copy of the GNU General Public License
//      along with this program. If not, see
//      <http://www.gnu.org/licenses/>.
//


#include <cstring>
#include <lib/srec/record.h>


srec_record::srec_record():
    type(type_unknown),
    address(0),
    length(0)
{
}


srec_record::srec_record(const srec_record & arg) :
    type(arg.type),
    address(arg.address),
    length(arg.length)
{
    if (arg.length > 0)
        memcpy(data, arg.data, arg.length);
}


srec_record::srec_record(type_t arg) :
    type(arg),
    address(0),
    length(0)
{
}


srec_record::srec_record(type_t a1, address_t a2) :
    type(a1),
    address(a2),
    length(0)
{
}


srec_record::srec_record(type_t a1, address_t a2, const data_t * a3, int a4) :
    type(a1),
    address(a2),
    length(a4 > 0 ? a4 : 0)
{
    if (length > 0)
        memcpy(data, a3, length);
}


srec_record & srec_record::operator = (const srec_record & arg)
{
    if (this != &arg)
    {
        type = arg.type;
        address = arg.address;
        length = arg.length;
        if (arg.length > 0)
            memcpy(data, arg.data, arg.length);
    }
    return *this;
}


srec_record::~srec_record()
{
}


int
srec_record::maximum_data_length(address_t address)
{
    if (address < (1UL << 16))
        return 252;
    if (address < (1UL << 24))
        return 251;
    return 250;
}


srec_record::address_t
srec_record::decode_big_endian(data_t *buffer, int length)
{
    address_t result = 0;
    while (length-- > 0)
        result = (result << 8) | *buffer++;
    return result;
}


void
srec_record::encode_big_endian(data_t *buffer, address_t value, int length)
{
    while (length > 0)
    {
        buffer[--length] = value;
        value >>= 8;
    }
}


void
srec_record::encode_little_endian(data_t *buffer, address_t value, int length)
{
    while (length-- > 0)
    {
        *buffer++ = value;
        value >>= 8;
    }
}


bool srec_record::is_all_zero() const
{
    for (int j = 0; j < length; ++j)
        if (data[j])
            return false;
    return true;
}


void
srec_record::set_data_extend(int n, int d)
{
    // assert(n >= 0);
    // assert(n < max_data_length);
    if (0 <= n && n < max_data_length)
    {
        data[n] = d;
        if (length <= n)
            length = n + 1;
    }
}