Skip to content

Commit 6fa2523

Browse files
authored
new input filter : nibble_swap (#62)
* input filters: add -Nibble-Swap filter Simply exchanges upper and lower nibbles of each bytes. This should only be a few opcodes per byte on most CPUs; probably faster than a look-up table (e.g. like the bit-reverse filter).
1 parent 56818b9 commit 6fa2523

File tree

9 files changed

+203
-0
lines changed

9 files changed

+203
-0
lines changed

doc/etc/README.man

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,10 @@ minimum
316316
The \f[I]minimum\fP filter may be used to insert the minimum data address
317317
into the data.
318318
.\" ---------- N ---------------------------------------------------------
319+
.\" nibble swap
320+
.TP 8n
321+
\fB\-Nibble_Swap\fP
322+
This filter may be used to swap upper and lower nibbles of each byte.
319323
.\" ---------- O ---------------------------------------------------------
320324
.TP 8n
321325
offset

doc/man1/srec_input.1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,10 @@ The number of bytes defaults to 4.
947947
The same as the \fB\-MINimum_Big_Endian\fP filter,
948948
except the value will be written with the least significant byte first.
949949
.\" ---------- N ---------------------------------------------------------
950+
.\" nibble swap
951+
.TP 8n
952+
\fB\-Nibble_Swap\fP
953+
This filter may be used to swap upper and lower nibbles of each byte.
950954
.\" not
951955
.TP 8n
952956
\fB\-NOT\fP

srecord/arglex/tool.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ srecord::arglex_tool::arglex_tool(int argc, char **argv) :
159159
{ "-MsBin", token_msbin, },
160160
{ "-MULTiple", token_multiple, },
161161
{ "-Needham_Hexadecimal", token_needham_hex, },
162+
{ "-Nibble_Swap", token_nibble_swap, },
162163
{ "-NOT", token_not, },
163164
{ "-Not_AUGment", token_crc16_augment_not },
164165
{ "-Not_CONSTant", token_constant_not, },

srecord/arglex/tool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ class arglex_tool:
146146
token_msbin,
147147
token_multiple,
148148
token_needham_hex,
149+
token_nibble_swap,
149150
token_not,
150151
token_offset,
151152
token_ohio_scientific,

srecord/arglex/tool/input.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
#include <srecord/input/filter/message/fletcher16.h>
8484
#include <srecord/input/filter/message/fletcher32.h>
8585
#include <srecord/input/filter/message/gcrypt.h>
86+
#include <srecord/input/filter/nibble_swap.h>
8687
#include <srecord/input/filter/not.h>
8788
#include <srecord/input/filter/offset.h>
8889
#include <srecord/input/filter/or.h>
@@ -912,6 +913,11 @@ srecord::arglex_tool::get_input()
912913
}
913914
break;
914915

916+
case token_nibble_swap:
917+
token_next();
918+
ifp = input_filter_nibble_swap::create(ifp);
919+
break;
920+
915921
case token_not:
916922
token_next();
917923
ifp = input_filter_not::create(ifp);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// Copyright (c) 2023 fenugrec
3+
//
4+
// srecord filter: exchanges upper and lower nibble of each byte,
5+
// e.g. "0xA6" => "0x6A"
6+
//
7+
// This program is free software; you can redistribute it and/or modify
8+
// it under the terms of the GNU Lesser General Public License as published by
9+
// the Free Software Foundation; either version 3 of the License, or (at
10+
// your option) any later version.
11+
//
12+
// This program is distributed in the hope that it will be useful,
13+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
// General Public License for more details.
16+
//
17+
// You should have received a copy of the GNU Lesser General Public License
18+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
//
20+
21+
#include <srecord/input/filter/nibble_swap.h>
22+
#include <srecord/record.h>
23+
24+
25+
srecord::input_filter_nibble_swap::input_filter_nibble_swap(
26+
const srecord::input::pointer &arg
27+
) :
28+
srecord::input_filter(arg)
29+
{
30+
}
31+
32+
33+
srecord::input::pointer
34+
srecord::input_filter_nibble_swap::create(const input::pointer &a_deeper)
35+
36+
{
37+
return pointer(new srecord::input_filter_nibble_swap(a_deeper));
38+
}
39+
40+
41+
bool
42+
srecord::input_filter_nibble_swap::read(srecord::record &record)
43+
{
44+
if (!srecord::input_filter::read(record))
45+
return false;
46+
if (record.get_type() == srecord::record::type_data)
47+
{
48+
for (size_t j = 0; j < record.get_length(); ++j) {
49+
uint8_t tmp = record.get_data(j);
50+
record.set_data(j, ((tmp & 0x0F) << 4) | ((tmp & 0xF0) >> 4));
51+
}
52+
}
53+
return true;
54+
}

srecord/input/filter/nibble_swap.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//
2+
// Copyright (c) 2023 fenugrec
3+
//
4+
// srecord filter: exchanges upper and lower nibble of each byte,
5+
// e.g. "0xA6" => "0x6A"
6+
//
7+
// This program is free software; you can redistribute it and/or modify
8+
// it under the terms of the GNU Lesser General Public License as published by
9+
// the Free Software Foundation; either version 3 of the License, or (at
10+
// your option) any later version.
11+
//
12+
// This program is distributed in the hope that it will be useful,
13+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
// General Public License for more details.
16+
//
17+
// You should have received a copy of the GNU Lesser General Public License
18+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
//
20+
21+
#ifndef SRECORD_INPUT_FILTER_NIBBLE_SWAP_H
22+
#define SRECORD_INPUT_FILTER_NIBBLE_SWAP_H
23+
24+
#include <srecord/input/filter.h>
25+
26+
namespace srecord {
27+
28+
/**
29+
* The srecord::input_filter_nibble_swap class is used to represent the
30+
* input state of a filter which swaps upper/lower nibbles of each byte.
31+
*/
32+
class input_filter_nibble_swap:
33+
public input_filter
34+
{
35+
public:
36+
/**
37+
* The destructor.
38+
*/
39+
~input_filter_nibble_swap() override = default;
40+
41+
private:
42+
/**
43+
* The constructor.
44+
*
45+
* @param deeper
46+
* The deeper input to be used as a data source.
47+
*/
48+
input_filter_nibble_swap(const input::pointer &deeper);
49+
50+
public:
51+
/**
52+
* The create class method is used to create new dynamically
53+
* allocated instances of this class.
54+
*
55+
* @param deeper
56+
* The incoming data source to be filtered
57+
*/
58+
static pointer create(const input::pointer &deeper);
59+
60+
protected:
61+
// See base class for documentation.
62+
bool read(record &record) override;
63+
64+
public:
65+
/**
66+
* The default constructor.
67+
*/
68+
input_filter_nibble_swap() = delete;
69+
70+
/**
71+
* The copy constructor.
72+
*/
73+
input_filter_nibble_swap(const input_filter_nibble_swap &) = delete;
74+
75+
/**
76+
* The assignment operator.
77+
*/
78+
input_filter_nibble_swap &operator= \
79+
(const input_filter_nibble_swap &) = delete;
80+
};
81+
82+
};
83+
84+
#endif

srecord/srecord.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
#include <srecord/input/filter/message/fletcher16.h>
8585
#include <srecord/input/filter/message/fletcher32.h>
8686
#include <srecord/input/filter/message/gcrypt.h>
87+
#include <srecord/input/filter/nibble_swap.h>
8788
#include <srecord/input/filter/not.h>
8889
#include <srecord/input/filter/offset.h>
8990
#include <srecord/input/filter/or.h>

test/02/t0269a.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/sh
2+
#
3+
# srecord - manipulate eprom load files
4+
# Copyright (C) 2023 fenugrec
5+
#
6+
# This program is free software; you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation; either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with this program. If not, see
18+
# <http://www.gnu.org/licenses/>.
19+
#
20+
21+
TEST_SUBJECT="nibble swap filter"
22+
. test_prelude.sh
23+
24+
cat > test.in << 'fubar'
25+
S00600004844521B
26+
S1070000F08008552B
27+
S5030001FB
28+
fubar
29+
if test $? -ne 0; then no_result; fi
30+
31+
cat > test.ok << 'fubar'
32+
S00600004844521B
33+
S10700000F0880550C
34+
S5030001FB
35+
fubar
36+
if test $? -ne 0; then no_result; fi
37+
38+
srec_cat test.in -ns -o test.out
39+
if test $? -ne 0; then fail; fi
40+
41+
diff test.ok test.out
42+
if test $? -ne 0; then fail; fi
43+
44+
#
45+
# The things tested here, worked.
46+
# No other guarantees are made.
47+
#
48+
pass

0 commit comments

Comments
 (0)