Commit 45c8d607908b35eb3b965468c3806d902d5f5eb9

Con Kolivas 2013-09-19T17:34:26

Add crc initialisation tables and helper functions for hashfast driver.

diff --git a/cgminer.c b/cgminer.c
index 2d28bdc..e76a683 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -54,6 +54,12 @@
 #include "bench_block.h"
 #include "scrypt.h"
 
+#if defined(unix) || defined(__APPLE__)
+	#include <errno.h>
+	#include <fcntl.h>
+	#include <sys/wait.h>
+#endif
+
 #ifdef USE_AVALON
 #include "driver-avalon.h"
 #endif
@@ -62,10 +68,8 @@
 #include "driver-bflsc.h"
 #endif
 
-#if defined(unix) || defined(__APPLE__)
-	#include <errno.h>
-	#include <fcntl.h>
-	#include <sys/wait.h>
+#ifdef USE_HASHFAST
+#include "driver-hashfast.h"
 #endif
 
 #if defined(USE_BITFORCE) || defined(USE_ICARUS) || defined(USE_AVALON) || defined(USE_MODMINER)
@@ -7858,6 +7862,8 @@ int main(int argc, char *argv[])
 #endif
 
 #ifdef USE_HASHFAST
+	hf_init_crc8();
+	hf_init_crc32();
 	hashfast_drv.drv_detect();
 #endif
 
diff --git a/driver-hashfast.c b/driver-hashfast.c
index b6e1816..409fa92 100644
--- a/driver-hashfast.c
+++ b/driver-hashfast.c
@@ -17,6 +17,82 @@
 
 #include "driver-hashfast.h"
 
+////////////////////////////////////////////////////////////////////////////////
+// Support for the CRC's used in header (CRC-8) and packet body (CRC-32)
+////////////////////////////////////////////////////////////////////////////////
+
+#define GP8  0x107   /* x^8 + x^2 + x + 1 */
+#define DI8  0x07
+
+static unsigned char crc8_table[256];	/* CRC-8 table */
+static uint32_t crc32_table[256];	/* CRC-32 table */
+
+void hf_init_crc8(void)
+{
+	int i,j;
+	unsigned char crc;
+
+	for (i = 0; i < 256; i++) {
+		crc = i;
+		for (j = 0; j < 8; j++)
+			crc = (crc << 1) ^ ((crc & 0x80) ? DI8 : 0);
+		crc8_table[i] = crc & 0xFF;
+	}
+}
+
+static unsigned char __maybe_unused hf_crc8(unsigned char *h)
+{
+	int i;
+	unsigned char crc;
+
+	h++;	// Preamble not included
+	for (i = 1, crc = 0xff; i < 7; i++)
+		crc = crc8_table[crc ^ *h++];
+
+	return crc;
+}
+
+#define DI32 0x04c11db7L
+
+void hf_init_crc32(void)
+{
+	uint32_t i, j;
+	uint32_t crc;
+
+	for (i = 0; i < 256; i++){
+		crc = i << 24;
+		for (j = 0; j < 8; j++) {
+			if (crc & 0x80000000L)
+				crc = (crc << 1) ^ DI32;
+			else
+				crc = (crc << 1);
+		}
+		crc32_table[i] = crc;
+	}
+}
+
+static uint32_t __maybe_unused hf_crc32(unsigned char *p, int len, int plug_in)
+{
+	uint32_t crc = 0xffffffffU, crc_sav;
+	uint32_t i;
+
+	while (len--) {
+		i = ((crc >> 24) ^ *p++) & 0xff;
+		crc = (crc << 8) ^ crc32_table[i];
+	}
+
+	crc_sav = crc;
+
+	applog(LOG_DEBUG, "hf_crc32: crc is 0x%08x", crc);
+
+	if (plug_in) {
+		for (i = 0; i < 4; i++, crc >>= 8)
+			*p++ = crc & 0xff;
+	}
+
+	return crc_sav;
+}
+
 static hf_info_t **hashfast_infos;
 struct device_drv hashfast_drv;
 
diff --git a/driver-hashfast.h b/driver-hashfast.h
index 155eb87..042c2bd 100644
--- a/driver-hashfast.h
+++ b/driver-hashfast.h
@@ -216,7 +216,10 @@ typedef struct hf_info_t {
 #define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1]
 ASSERT1(sizeof(uint32_t) == 4);
 
-extern hf_info_t **hashfast_info;
+hf_info_t **hashfast_info;
+
+void hf_init_crc8(void);
+void hf_init_crc32(void);
 
 #endif /* USE_HASHFAST */
 #endif	/* HASHFAST_H */