1 //
2 // srecord - manipulate eprom load files
3 // Copyright (C) 1998, 1999, 2001, 2002, 2006-2010 Peter Miller
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this program. If not, see
17 // <http://www.gnu.org/licenses/>.
18 //
19
20 #include <srecord/input/filter/split.h>
21 #include <srecord/record.h>
22
23
~input_filter_split()24 srecord::input_filter_split::~input_filter_split()
25 {
26 }
27
28
input_filter_split(const input::pointer & a1,int a2,int a3,int a4)29 srecord::input_filter_split::input_filter_split(const input::pointer &a1,
30 int a2, int a3, int a4) :
31 srecord::input_filter(a1),
32 modulus(a2),
33 offset(a2 - a3),
34 width(a4),
35 buffer(),
36 buffer_pos(0)
37 {
38 //
39 // The offset is expressed as a positive number (by subtracting
40 // the inherently negative offset from the modulus). This avoids
41 // modulo operator problems near zero.
42 //
43 }
44
45
46 srecord::input::pointer
create(const srecord::input::pointer & a_deeper,int a2,int a3,int a4)47 srecord::input_filter_split::create(const srecord::input::pointer &a_deeper,
48 int a2, int a3, int a4)
49 {
50 return pointer(new srecord::input_filter_split(a_deeper, a2, a3, a4));
51 }
52
53
54 bool
read(srecord::record & record)55 srecord::input_filter_split::read(srecord::record &record)
56 {
57 for (;;)
58 {
59 while
60 (
61 buffer.get_type() != srecord::record::type_data
62 ||
63 buffer_pos >= buffer.get_length()
64 )
65 {
66 if (!srecord::input_filter::read(buffer))
67 return false;
68 if (buffer.get_type() != srecord::record::type_data)
69 {
70 record = buffer;
71 return true;
72 }
73 buffer_pos = 0;
74 }
75
76 srecord::record::address_t addr =
77 (buffer.get_address() + offset + buffer_pos);
78 srecord::record::address_t phase = addr % modulus;
79 if (phase < width)
80 {
81 unsigned char c = buffer.get_data(buffer_pos++);
82 //
83 // Because the offset was made positive by
84 // subtracting it from the modulus (to avoid
85 // problems with the C modulo operator (%) near
86 // zero) this means the address is actually one
87 // multiple of modulus too high. Subtract it
88 // off before the width multiplication.
89 //
90 addr = (addr / modulus - 1) * width + phase;
91 record = srecord::record(srecord::record::type_data, addr, &c, 1);
92 return true;
93 }
94 ++buffer_pos;
95 }
96 }
97