tools/flterm.py: small clean up
[litex.git] / tools / byteswap.c
1 /*
2 * MiSoC
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 }