Add crc initialisation tables and helper functions for hashfast driver.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
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 */