Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

tools: add byteswap

  • Loading branch information...
commit 3f22930b1fced27081d110ebf0d7f825ed4e07ae 1 parent 07120e3
Sébastien Bourdeauducq authored February 19, 2013
1  .gitignore
@@ -8,3 +8,4 @@ build/*
8 8
 tools/bin2hex
9 9
 tools/flterm
10 10
 tools/mkmmimg
  11
+tools/byteswap
4  tools/Makefile
... ...
@@ -1,5 +1,5 @@
1  
-TARGETS=mkmmimg flterm
2  
-CC=clang
  1
+TARGETS=mkmmimg flterm byteswap
  2
+CC=gcc
3 3
 
4 4
 all: $(TARGETS)
5 5
 
60  tools/byteswap.c
... ...
@@ -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 notes on commit 3f22930

Please sign in to comment.
Something went wrong with that request. Please try again.