Skip to content

Commit 3f22930

Browse files
author
Sebastien Bourdeauducq
committedFeb 19, 2013
tools: add byteswap
1 parent 07120e3 commit 3f22930

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ build/*
88
tools/bin2hex
99
tools/flterm
1010
tools/mkmmimg
11+
tools/byteswap

‎tools/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
TARGETS=mkmmimg flterm
2-
CC=clang
1+
TARGETS=mkmmimg flterm byteswap
2+
CC=gcc
33

44
all: $(TARGETS)
55

‎tools/byteswap.c

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Milkymist SoC
3+
* Copyright (C) 2007, 2008, 2009, 2010 Sebastien Bourdeauducq
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, version 3 of the License.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#include <stdlib.h>
19+
#include <string.h>
20+
#include <stdio.h>
21+
22+
int main(int argc, char *argv[])
23+
{
24+
FILE *fdi, *fdo;
25+
unsigned short wi;
26+
unsigned short wo;
27+
int i;
28+
29+
if(argc != 3) {
30+
fprintf(stderr, "Usage: byteswap <infile> <outfile>\n");
31+
return 1;
32+
}
33+
fdi = fopen(argv[1], "rb");
34+
if(!fdi) {
35+
perror("Unable to open input file");
36+
return 1;
37+
}
38+
fdo = fopen(argv[2], "w");
39+
if(!fdo) {
40+
perror("Unable to open output file");
41+
fclose(fdi);
42+
return 1;
43+
}
44+
while(1) {
45+
if(fread(&wi, 2, 1, fdi) <= 0) break;
46+
wo = 0;
47+
for(i=0;i<16;i++)
48+
if(wi & (1 << i))
49+
wo |= (0x8000 >> i);
50+
/* comment out the next line on big endian machines! */
51+
wo = ((wo & 0x00ff) << 8) | ((wo & 0xff00) >> 8);
52+
fwrite(&wo, 2, 1, fdo);
53+
}
54+
fclose(fdi);
55+
if(fclose(fdo) != 0) {
56+
perror("Unable to close output file");
57+
return 1;
58+
}
59+
return 0;
60+
}

0 commit comments

Comments
 (0)
Please sign in to comment.