Browse Source

bitfury: Port to Windows

Luke Dashjr 12 years ago
parent
commit
b5f7c06d1b
9 changed files with 97 additions and 70 deletions
  1. 9 1
      Makefile.am
  2. 29 0
      configure.ac
  3. 4 2
      driver-littlefury.c
  4. 0 4
      driver-metabank.c
  5. 18 51
      libbitfury.c
  6. 6 7
      libbitfury.h
  7. 10 0
      miner.c
  8. 14 4
      spidevc.c
  9. 7 1
      util.h

+ 9 - 1
Makefile.am

@@ -215,7 +215,15 @@ bfgminer_SOURCES += driver-ztex.c libztex.c libztex.h
 endif
 
 if HAS_BITFURY
-bfgminer_SOURCES += driver-bitfury.c driver-bitfury.h libbitfury.c libbitfury.h spidevc.h spidevc.c driver-metabank.c tm_i2c.h tm_i2c.c driver-bfsb.c
+bfgminer_SOURCES += driver-bitfury.c driver-bitfury.h libbitfury.c libbitfury.h spidevc.h spidevc.c
+
+if HAS_BFSB
+bfgminer_SOURCES += driver-bfsb.c
+endif
+
+if HAS_METABANK
+bfgminer_SOURCES += driver-metabank.c tm_i2c.h tm_i2c.c
+endif
 
 if HAS_LITTLEFURY
 bfgminer_SOURCES += driver-littlefury.c

+ 29 - 0
configure.ac

@@ -73,6 +73,7 @@ AC_CHECK_HEADERS(syslog.h)
 AC_CHECK_HEADERS([sys/epoll.h])
 AC_CHECK_HEADERS([sys/prctl.h])
 AC_CHECK_HEADERS([sys/file.h])
+AC_CHECK_HEADERS([linux/spi/spidev.h])
 
 AC_FUNC_ALLOCA
 
@@ -452,6 +453,20 @@ fi
 AM_CONDITIONAL([HAS_BITFURY], [test x$bitfury = xyes])
 
 
+bfsb=no
+AC_ARG_ENABLE([bfsb],
+	[AC_HELP_STRING([--disable-bfsb],[Compile support for BFSB (default disabled)])],
+	[bfsb=$enableval]
+	)
+if test "x$bfsb" = "xyes"; then
+	if test "x$bitfury" = "xno"; then
+		AC_MSG_ERROR([You explicitly disabled Bitfury and explicitly enabled BFSB])
+	fi
+	AC_DEFINE([USE_BFSB], [1], [Defined to 1 if BFSB support is wanted])
+fi
+AM_CONDITIONAL([HAS_BFSB], [test x$bfsb = xyes])
+
+
 littlefury=auto
 AC_ARG_ENABLE([littlefury],
 	[AC_HELP_STRING([--disable-littlefury],[Compile support for LittleFury (default enabled)])],
@@ -472,6 +487,20 @@ fi
 AM_CONDITIONAL([HAS_LITTLEFURY], [test x$littlefury = xyes])
 
 
+metabank=no
+AC_ARG_ENABLE([metabank],
+	[AC_HELP_STRING([--disable-metabank],[Compile support for Metabank (default disabled)])],
+	[metabank=$enableval]
+	)
+if test "x$metabank" = "xyes"; then
+	if test "x$bitfury" = "xno"; then
+		AC_MSG_ERROR([You explicitly disabled Bitfury and explicitly enabled Metabank])
+	fi
+	AC_DEFINE([USE_METABANK], [1], [Defined to 1 if Metabank support is wanted])
+fi
+AM_CONDITIONAL([HAS_METABANK], [test x$metabank = xyes])
+
+
 if test "x$x6500$ztex" = "xnono"; then
 	libusb=no
 	LIBUSB_LIBS=''

+ 4 - 2
driver-littlefury.c

@@ -98,7 +98,7 @@ ssize_t keep_reading(int fd, void *buf, size_t count)
 		r = read(fd, buf, count);
 		if (unlikely(r <= 0))
 		{
-			applog(LOG_ERR, "Read of fd %d returned %d", fd, r);
+			applog(LOG_ERR, "Read of fd %d returned %d", fd, (int)r);
 			return rv ?: r;
 		}
 		rv += r;
@@ -251,7 +251,9 @@ bool littlefury_detect_one(const char *devpath)
 		goto err;
 	}
 	
-	devname = strndup((char*)&buf[4], bufsz - 4);
+	devname = malloc(bufsz - 3);
+	memcpy(devname, (char*)&buf[4], bufsz - 4);
+	devname[bufsz - 4] = '\0';
 	applog(LOG_DEBUG, "%s: Identified %s %d.%d.%d (features %02x)",
 	       littlefury_drv.dname, devname, buf[0], buf[1], buf[2], buf[3]);
 	

+ 0 - 4
driver-metabank.c

@@ -50,7 +50,6 @@ struct bitfury_device **metabank_detect_chips(int *out_count) {
 	int n = 0;
 	int i, j;
 	bool slot_on[32];
-	struct timespec t1, t2;
 	struct bitfury_device dummy_bitfury;
 	struct cgpu_info dummy_cgpu;
 	int max_devices = 100;
@@ -68,7 +67,6 @@ struct bitfury_device **metabank_detect_chips(int *out_count) {
 	for (i = 0; i < 32; i++) {
 		slot_on[i] = 0;
 	}
-	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
 	for (i = 0; i < 32; i++) {
 		int slot_detected = tm_i2c_detect(i) != -1;
 		slot_on[i] = slot_detected;
@@ -111,8 +109,6 @@ struct bitfury_device **metabank_detect_chips(int *out_count) {
 		}
 	}
 
-	clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t2);
-
 	*out_count = n;
 	return devicelist;
 }

+ 18 - 51
libbitfury.c

@@ -92,35 +92,6 @@ static const unsigned SHA_K[64] = {
 
 
 
-struct  timespec t_add(struct  timespec  time1, struct  timespec  time2) {
-    struct  timespec  result ;
-
-    result.tv_sec = time1.tv_sec + time2.tv_sec ;
-    result.tv_nsec = time1.tv_nsec + time2.tv_nsec ;
-    if (result.tv_nsec >= 1000000000L) {
-        result.tv_sec++ ;  result.tv_nsec = result.tv_nsec - 1000000000L ;
-    }
-
-    return (result) ;
-}
-
-
-
-struct timespec t_diff(struct timespec start, struct timespec end)
-{
-	struct timespec temp;
-	if (end.tv_nsec < start.tv_nsec) {
-		temp.tv_sec = end.tv_sec-start.tv_sec-1;
-		temp.tv_nsec = 1000000000LU;
-		temp.tv_nsec -= start.tv_nsec;
-		temp.tv_nsec += end.tv_nsec;
-	} else {
-		temp.tv_sec = end.tv_sec-start.tv_sec;
-		temp.tv_nsec = end.tv_nsec-start.tv_nsec;
-	}
-	return temp;
-}
-
 void ms3_compute(unsigned *p)
 {
 	unsigned a,b,c,d,e,f,g,h, ne, na,  i;
@@ -259,7 +230,6 @@ int detect_chip(struct spi_port *port, int chip_n) {
 	unsigned newbuf[17], oldbuf[17];
 	unsigned ocounter;
 	int odiff = 0;
-	struct timespec t1;
 
 	memset(newbuf, 0, 17 * 4);
 	memset(oldbuf, 0, 17 * 4);
@@ -288,7 +258,6 @@ int detect_chip(struct spi_port *port, int chip_n) {
 		spi_txrx(port);
 		memcpy(newbuf, spi_getrxbuf(port) + 4 + chip_n, 17*4);
 
-		clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
 		counter = get_counter(newbuf, oldbuf);
 		if (ocounter) {
 			unsigned int cdiff = c_diff(ocounter, counter);
@@ -424,13 +393,13 @@ void libbitfury_sendHashData1(int chip_id, struct bitfury_device *d, struct thr_
 	struct bitfury_payload *p = &(d->payload);
 	struct bitfury_payload *op = &(d->opayload);
 	struct bitfury_payload *o2p = &(d->o2payload);
-	struct timespec d_time;
-	struct timespec time;
+	struct timeval d_time;
+	struct timeval time;
 	int smart = 0;
 	int chip = d->fasync;
 	int buf_diff;
 
-	clock_gettime(CLOCK_REALTIME, &(time));
+	timer_set_now(&time);
 
 	if (!d->second_run) {
 		d->predict2 = d->predict1 = time;
@@ -438,7 +407,7 @@ void libbitfury_sendHashData1(int chip_id, struct bitfury_device *d, struct thr_
 		d->req2_done = 0;
 	};
 
-	d_time = t_diff(time, d->predict1);
+	timersub(&time, &d->predict1, &d_time);
 	if (d_time.tv_sec < 0 && (d->req2_done || !smart)) {
 		d->otimer1 = d->timer1;
 		d->timer1 = time;
@@ -451,8 +420,8 @@ void libbitfury_sendHashData1(int chip_id, struct bitfury_device *d, struct thr_
 		if (smart) {
 			config_reg(port, 3, 0);
 		}
-		clock_gettime(CLOCK_REALTIME, &(time));
-		d_time = t_diff(time, d->predict1);
+		timer_set_now(&time);
+		timersub(&time, &d->predict1, &d_time);
 		spi_txrx(port);
 		memcpy(newbuf, spi_getrxbuf(port)+4 + chip, 17*4);
 		d->counter1 = get_counter(newbuf, oldbuf);
@@ -467,8 +436,8 @@ void libbitfury_sendHashData1(int chip_id, struct bitfury_device *d, struct thr_
 				spi_emit_break(port);
 				spi_emit_fasync(port, chip);
 				spi_emit_data(port, 0x3000, &d->atrvec[0], 19*4);
-				clock_gettime(CLOCK_REALTIME, &(time));
-				d_time = t_diff(time, d->predict1);
+				timer_set_now(&time);
+				timersub(&time, &d->predict1, &d_time);
 				spi_txrx(port);
 				memcpy(newbuf, spi_getrxbuf(port)+4 + chip, 17*4);
 				buf_diff = get_diff(newbuf, oldbuf);
@@ -528,9 +497,9 @@ void libbitfury_sendHashData1(int chip_id, struct bitfury_device *d, struct thr_
 		d->results_n = results_num;
 
 		if (smart) {
-			d_time = t_diff(d->timer2, d->timer1);
+			timersub(&d->timer2, &d->timer1, &d_time);
 		} else {
-			d_time = t_diff(d->otimer1, d->timer1);
+			timersub(&d->otimer1, &d->timer1, &d_time);
 		}
 		d->counter1 = get_counter(newbuf, oldbuf);
 		if (d->counter2 || !smart) {
@@ -542,7 +511,7 @@ void libbitfury_sendHashData1(int chip_id, struct bitfury_device *d, struct thr_
 			unsigned full_cycles, half_cycles;
 			double full_delay, half_delay;
 			long long unsigned delta;
-			struct timespec t_delta;
+			struct timeval t_delta;
 			double mhz;
 #ifdef BITFURY_SENDHASHDATA_DEBUG
 			int ccase;
@@ -565,7 +534,7 @@ void libbitfury_sendHashData1(int chip_id, struct bitfury_device *d, struct thr_
 				}
 			}
 			req1_cycles = 0x003FFFFF - d->counter1;
-			period = (long long unsigned int)d_time.tv_sec * 1000000000ULL + (long long unsigned int)d_time.tv_nsec;
+			period = timeval_to_us(&d_time) * 1000ULL;
 			ns = (double)period / (double)(cycles);
 			mhz = 1.0 / ns * 65.0 * 1000.0;
 
@@ -594,9 +563,8 @@ void libbitfury_sendHashData1(int chip_id, struct bitfury_device *d, struct thr_
 			half_delay = (double)half_cycles * ns * (1 +0.92);
 			full_delay = (double)full_cycles * ns;
 			delta = (long long unsigned)(full_delay + half_delay);
-			t_delta.tv_sec = delta / 1000000000ULL;
-			t_delta.tv_nsec = delta - t_delta.tv_sec * 1000000000ULL;
-			d->predict1 = t_add(time, t_delta);
+			t_delta = TIMEVAL_USECS(delta / 1000ULL);
+			timeradd(&time, &t_delta, &d->predict1);
 
 			if (smart) {
 				half_cycles = req1_cycles + shift;
@@ -608,9 +576,8 @@ void libbitfury_sendHashData1(int chip_id, struct bitfury_device *d, struct thr_
 			full_delay = (double)full_cycles * ns;
 			delta = (long long unsigned)(full_delay + half_delay);
 
-			t_delta.tv_sec = delta / 1000000000ULL;
-			t_delta.tv_nsec = delta - t_delta.tv_sec * 1000000000ULL;
-			d->predict2 = t_add(time, t_delta);
+			t_delta = TIMEVAL_USECS(delta / 1000ULL);
+			timeradd(&time, &t_delta, &d->predict2);
 			d->req2_done = 0; d->req1_done = 0;
 		}
 
@@ -621,8 +588,8 @@ void libbitfury_sendHashData1(int chip_id, struct bitfury_device *d, struct thr_
 		}
 	}
 
-	clock_gettime(CLOCK_REALTIME, &(time));
-	d_time = t_diff(time, d->predict2);
+	timer_set_now(&time);
+	timersub(&time, &d->predict2, &d_time);
 	if (d_time.tv_sec < 0 && !d->req2_done) {
 		if(smart) {
 			d->otimer2 = d->timer2;

+ 6 - 7
libbitfury.h

@@ -36,12 +36,12 @@ struct bitfury_device {
 	unsigned int stat_counter;
 	unsigned int future_nonce;
 	unsigned int old_nonce;
-	struct timespec timer1;
-	struct timespec timer2;
-	struct timespec otimer1;
-	struct timespec otimer2;
-	struct timespec predict1;
-	struct timespec predict2;
+	struct timeval timer1;
+	struct timeval timer2;
+	struct timeval otimer1;
+	struct timeval otimer2;
+	struct timeval predict1;
+	struct timeval predict2;
 	unsigned int counter1, counter2;
 	unsigned int ocounter1, ocounter2;
 	int rate; //per msec
@@ -63,7 +63,6 @@ int libbitfury_readHashData(unsigned int *res);
 extern void libbitfury_sendHashData1(int chip_id, struct bitfury_device *, struct thr_info *);
 void work_to_payload(struct bitfury_payload *p, struct work *w);
 extern void payload_to_atrvec(uint32_t *atrvec, struct bitfury_payload *);
-struct timespec t_diff(struct timespec start, struct timespec end);
 extern void send_reinit(struct spi_port *, int slot, int chip_n, int n);
 extern void send_shutdown(struct spi_port *, int slot, int chip_n);
 extern void send_freq(struct spi_port *, int slot, int chip_n, int bits);

+ 10 - 0
miner.c

@@ -9393,7 +9393,13 @@ extern struct device_drv ztex_drv;
 
 #ifdef USE_BITFURY
 extern struct device_drv bitfury_drv;
+#endif
+
+#ifdef USE_METABANK
 extern struct device_drv metabank_drv;
+#endif
+
+#ifdef USE_BFSB
 extern struct device_drv bfsb_drv;
 #endif
 
@@ -9509,8 +9515,12 @@ void drv_detect_all()
 	if (!opt_scrypt)
 	{
 		bitfury_drv.drv_detect();
+#ifdef USE_METABANK
 		metabank_drv.drv_detect();
+#endif
+#ifdef USE_BFSB
 		bfsb_drv.drv_detect();
+#endif
 	}
 #endif
 

+ 14 - 4
spidevc.c

@@ -21,9 +21,14 @@
  * THE SOFTWARE.
  */
 
+#include "config.h"
+
+#ifdef HAVE_LINUX_SPI_SPIDEV_H
+#define HAVE_LINUX_SPI
+#endif
+
 #include "spidevc.h"
 
-#include <sys/mman.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <unistd.h>
@@ -31,6 +36,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include <getopt.h>
+
+#ifdef HAVE_LINUX_SPI
+#include <sys/mman.h>
 #include <fcntl.h>
 #include <sys/ioctl.h>
 #include <linux/types.h>
@@ -42,11 +50,10 @@
 #include <linux/i2c.h>
 #include <linux/i2c-dev.h>
 #include <sys/stat.h>
+#endif
 
 #include "logging.h"
 
-#define HAVE_LINUX_SPI
-
 #ifdef HAVE_LINUX_SPI
 bool sys_spi_txrx(struct spi_port *port);
 static volatile unsigned *gpio;
@@ -256,6 +263,8 @@ void spi_emit_data(struct spi_port *port, uint16_t addr, const void *buf, size_t
 	spi_emit_buf(port, otmp, 3);
 	spi_emit_buf_reverse(port, buf, len*4);
 }
+
+#ifdef HAVE_LINUX_SPI
 void spi_bfsb_select_bank(int bank)
 {
 	static int last_bank = -2;
@@ -277,4 +286,5 @@ void spi_bfsb_select_bank(int bank)
 		}
 	}
 	last_bank = bank;
-}
+}
+#endif

+ 7 - 1
util.h

@@ -315,6 +315,12 @@ extern void bfg_init_time();
 	}  \
 )
 
+static inline
+long timeval_to_us(const struct timeval *tvp)
+{
+	return ((long)tvp->tv_sec * 1000000) + tvp->tv_usec;
+}
+
 #define timer_set_delay(tvp_timer, tvp_now, usecs)  do {  \
 	struct timeval tv_add = TIMEVAL_USECS(usecs);  \
 	timeradd(&tv_add, tvp_now, tvp_timer);  \
@@ -341,7 +347,7 @@ long timer_elapsed_us(const struct timeval *tvp_timer, const struct timeval *tvp
 	struct timeval tv;
 	const struct timeval *_tvp_now = _bfg_nullisnow(tvp_now, &tv);
 	timersub(_tvp_now, tvp_timer, &tv);
-	return ((long)tv.tv_sec * 1000000) + tv.tv_usec;
+	return timeval_to_us(&tv);
 }
 
 static inline