Commit 2486b9ce81b46db3897a7744af1b7a3e5093c72b

Con Kolivas 2014-01-04T14:29:11

Export reused components of bitfury management into a libbitfury and use for bab and bitfury drivers.

diff --git a/Makefile.am b/Makefile.am
index 63aacfd..11ff5bc 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -51,6 +51,10 @@ if WANT_USBUTILS
 cgminer_SOURCES += usbutils.c usbutils.h
 endif
 
+if WANT_LIBBITFURY
+cgminer_SOURCES += libbitfury.c libbitfury.h
+endif
+
 # Device drivers
 if HAS_AVALON
 cgminer_SOURCES += driver-avalon.c driver-avalon.h
diff --git a/configure.ac b/configure.ac
index 862d940..e0f6ffa 100644
--- a/configure.ac
+++ b/configure.ac
@@ -288,8 +288,15 @@ else
 	want_usbutils=false
 fi
 
+if test x$bab$bitfury != xnono; then
+	want_libbitfury = true
+else
+	want_libbitfury = false
+fi
+
 AM_CONDITIONAL([NEED_FPGAUTILS], [test x$modminer != xno])
 AM_CONDITIONAL([WANT_USBUTILS], [test x$want_usbutils != xfalse])
+AM_CONDITIONAL([WANT_LIBBITFURY], [test x$want_libbitfury != xfalse])
 AM_CONDITIONAL([HAVE_CURSES], [test x$curses = xyes])
 AM_CONDITIONAL([HAVE_WINDOWS], [test x$have_win32 = xtrue])
 AM_CONDITIONAL([HAVE_x86_64], [test x$have_x86_64 = xtrue])
diff --git a/driver-bab.c b/driver-bab.c
index 7db3df1..d40bf47 100644
--- a/driver-bab.c
+++ b/driver-bab.c
@@ -15,6 +15,7 @@
 #include "compat.h"
 #include "miner.h"
 #include "sha2.h"
+#include "libbitfury.h"
 
 /*
  * Tested on RPi running both Raspbian and Arch
@@ -1323,42 +1324,6 @@ static void bab_flush_work(struct cgpu_info *babcgpu)
 	mutex_unlock(&(babinfo->did_lock));
 }
 
-static void ms3steps(uint32_t *p)
-{
-	uint32_t a, b, c, d, e, f, g, h, new_e, new_a;
-	int i;
-
-	a = p[0];
-	b = p[1];
-	c = p[2];
-	d = p[3];
-	e = p[4];
-	f = p[5];
-	g = p[6];
-	h = p[7];
-	for (i = 0; i < 3; i++) {
-		new_e = p[i+16] + sha256_k[i] + h + CH(e,f,g) + SHA256_F2(e) + d;
-		new_a = p[i+16] + sha256_k[i] + h + CH(e,f,g) + SHA256_F2(e) +
-			SHA256_F1(a) + MAJ(a,b,c);
-		d = c;
-		c = b;
-		b = a;
-		a = new_a;
-		h = g;
-		g = f;
-		f = e;
-		e = new_e;
-	}
-	p[15] = a;
-	p[14] = b;
-	p[13] = c;
-	p[12] = d;
-	p[11] = e;
-	p[10] = f;
-	p[9] = g;
-	p[8] = h;
-}
-
 #define DATA_MERKLE7 16
 #define DATA_NTIME 17
 #define DATA_BITS 18
@@ -1369,31 +1334,6 @@ static void ms3steps(uint32_t *p)
 #define WORK_BITS (18*4)
 #define WORK_NONCE (19*4)
 
-static uint32_t decnonce(uint32_t in)
-{
-	uint32_t out;
-
-	/* First part load */
-	out = (in & 0xFF) << 24;
-	in >>= 8;
-
-	/* Byte reversal */
-	in = (((in & 0xaaaaaaaa) >> 1) | ((in & 0x55555555) << 1));
-	in = (((in & 0xcccccccc) >> 2) | ((in & 0x33333333) << 2));
-	in = (((in & 0xf0f0f0f0) >> 4) | ((in & 0x0f0f0f0f) << 4));
-
-	out |= (in >> 2) & 0x3FFFFF;
-
-	/* Extraction */
-	if (in & 1)
-		out |= (1 << 23);
-	if (in & 2)
-		out |= (1 << 22);
-
-	out -= 0x800004;
-	return out;
-}
-
 /*
  * Find the matching work item by checking the nonce against each work
  * item for the chip
diff --git a/driver-bitfury.c b/driver-bitfury.c
index bbfc343..b59b082 100644
--- a/driver-bitfury.c
+++ b/driver-bitfury.c
@@ -13,6 +13,7 @@
 #include "driver-bitfury.h"
 #include "sha2.h"
 #include "mcp2210.h"
+#include "libbitfury.h"
 
 int opt_bxf_temp_target = BXF_TEMP_TARGET / 10;
 
@@ -691,28 +692,6 @@ static bool bitfury_prepare(struct thr_info *thr)
 	}
 }
 
-static uint32_t decnonce(uint32_t in)
-{
-	uint32_t out;
-
-	/* First part load */
-	out = (in & 0xFF) << 24; in >>= 8;
-
-	/* Byte reversal */
-	in = (((in & 0xaaaaaaaa) >> 1) | ((in & 0x55555555) << 1));
-	in = (((in & 0xcccccccc) >> 2) | ((in & 0x33333333) << 2));
-	in = (((in & 0xf0f0f0f0) >> 4) | ((in & 0x0f0f0f0f) << 4));
-
-	out |= (in >> 2)&0x3FFFFF;
-
-	/* Extraction */
-	if (in & 1) out |= (1 << 23);
-	if (in & 2) out |= (1 << 22);
-
-	out -= 0x800004;
-	return out;
-}
-
 #define BT_OFFSETS 3
 const uint32_t bf_offsets[] = {-0x800000, 0, -0x400000};
 
diff --git a/libbitfury.c b/libbitfury.c
new file mode 100644
index 0000000..aa066b1
--- /dev/null
+++ b/libbitfury.c
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2014 Con Kolivas
+ * Copyright 2013 Andrew Smith
+ * Copyright 2013 bitfury
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.  See COPYING for more details.
+ */
+
+#include "sha2.h"
+void ms3steps(uint32_t *p)
+{
+	uint32_t a, b, c, d, e, f, g, h, new_e, new_a;
+	int i;
+
+	a = p[0];
+	b = p[1];
+	c = p[2];
+	d = p[3];
+	e = p[4];
+	f = p[5];
+	g = p[6];
+	h = p[7];
+	for (i = 0; i < 3; i++) {
+		new_e = p[i+16] + sha256_k[i] + h + CH(e,f,g) + SHA256_F2(e) + d;
+		new_a = p[i+16] + sha256_k[i] + h + CH(e,f,g) + SHA256_F2(e) +
+			SHA256_F1(a) + MAJ(a,b,c);
+		d = c;
+		c = b;
+		b = a;
+		a = new_a;
+		h = g;
+		g = f;
+		f = e;
+		e = new_e;
+	}
+	p[15] = a;
+	p[14] = b;
+	p[13] = c;
+	p[12] = d;
+	p[11] = e;
+	p[10] = f;
+	p[9] = g;
+	p[8] = h;
+}
+
+uint32_t decnonce(uint32_t in)
+{
+	uint32_t out;
+
+	/* First part load */
+	out = (in & 0xFF) << 24;
+	in >>= 8;
+
+	/* Byte reversal */
+	in = (((in & 0xaaaaaaaa) >> 1) | ((in & 0x55555555) << 1));
+	in = (((in & 0xcccccccc) >> 2) | ((in & 0x33333333) << 2));
+	in = (((in & 0xf0f0f0f0) >> 4) | ((in & 0x0f0f0f0f) << 4));
+
+	out |= (in >> 2) & 0x3FFFFF;
+
+	/* Extraction */
+	if (in & 1)
+		out |= (1 << 23);
+	if (in & 2)
+		out |= (1 << 22);
+
+	out -= 0x800004;
+	return out;
+}
diff --git a/libbitfury.h b/libbitfury.h
new file mode 100644
index 0000000..e4cef89
--- /dev/null
+++ b/libbitfury.h
@@ -0,0 +1,15 @@
+/*
+ * Copyright 2014 Con Kolivas
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.  See COPYING for more details.
+ */
+
+#ifndef LIBBITFURY_H
+#define LIBBITFURY_H
+void ms3steps(uint32_t *p);
+uint32_t decnonce(uint32_t in);
+
+#endif /* LIBBITFURY_H */