driver-zeusminer.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright 2014 Nate Woolls
  3. * Copyright 2014 ZeusMiner Team
  4. * Copyright 2014 Luke Dashjr
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #include <stdbool.h>
  12. #include <stdint.h>
  13. #include <string.h>
  14. #include <math.h>
  15. #include "miner.h"
  16. #include "driver-icarus.h"
  17. #include "lowl-vcom.h"
  18. #define ZEUSMINER_IO_SPEED 115200
  19. #define ZEUSMINER_CHIP_GEN1_CORES 8
  20. #define ZEUSMINER_CHIP_CORES ZEUSMINER_CHIP_GEN1_CORES
  21. #define ZEUSMINER_CHIPS_COUNT_MAX 1
  22. #define ZEUSMINER_CHIPS_COUNT 6
  23. #define ZEUSMINER_DEFAULT_CLOCK 328
  24. #define ZEUSMINER_MIN_CLOCK 200
  25. #define ZEUSMINER_MAX_CLOCK 383
  26. BFG_REGISTER_DRIVER(zeusminer_drv)
  27. static
  28. const struct bfg_set_device_definition zeusminer_set_device_funcs[];
  29. // device helper functions
  30. static
  31. uint32_t zeusminer_calc_clk_header(uint16_t freq)
  32. {
  33. //set clk_reg based on chip_clk
  34. uint32_t clk_reg = (uint32_t)freq * 2 / 3;
  35. //clock speed mask for header
  36. uint32_t clk_header = (clk_reg << 24) + ((0xff - clk_reg) << 16);
  37. return clk_header;
  38. }
  39. // ICARUS_INFO functions - driver-icarus.h
  40. // device detection
  41. static
  42. bool zeusminer_detect_one(const char *devpath)
  43. {
  44. struct device_drv *drv = &zeusminer_drv;
  45. struct ICARUS_INFO *info = calloc(1, sizeof(struct ICARUS_INFO));
  46. if (unlikely(!info))
  47. quit(1, "Failed to malloc ICARUS_INFO");
  48. char scrypt_golden_ob[] =
  49. "55aa" //Freq is set to 0x55*1.5=85Mhz
  50. "0001" //We want to find a Nonce which result's diff is at least 32768
  51. "00038000" //Starting Nonce
  52. "063b0b1b" //Bits (target in compact form)
  53. "028f3253" //Timestamp
  54. "5e900609c15dc49a42b1d8492a6dd4f8f15295c989a1decf584a6aa93be26066" //Merkle root
  55. "d3185f55ef635b5865a7a79b7fa74121a6bb819da416328a9bd2f8cef72794bf" //Previous hash
  56. "02000000"; //Version
  57. const char scrypt_golden_nonce[] = "00038d26";
  58. *info = (struct ICARUS_INFO){
  59. .baud = ZEUSMINER_IO_SPEED,
  60. .timing_mode = MODE_DEFAULT,
  61. .do_icarus_timing = true,
  62. // 3 bits of 32 bits nonce are reserved for cores' combination
  63. // 10 bits of 32 bits nonce are reserved for chips' combination
  64. // the nonce range is split into 2^(10+3) parts
  65. .work_division = 8192,
  66. .probe_read_count = 5,
  67. .golden_nonce = scrypt_golden_nonce,
  68. .chips = ZEUSMINER_CHIPS_COUNT,
  69. .freq = ZEUSMINER_DEFAULT_CLOCK,
  70. };
  71. //pick up any user-defined settings passed in via --set
  72. drv_set_defaults(drv, zeusminer_set_device_funcs, info, devpath, detectone_meta_info.serial, 1);
  73. info->fpga_count = info->chips * ZEUSMINER_CHIP_CORES;
  74. //send the requested Chip Speed with the detect golden OB
  75. //we use the time this request takes in order to calc hashes
  76. //so we need to use the same Chip Speed used when hashing
  77. uint32_t clk_header = zeusminer_calc_clk_header(info->freq);
  78. char clk_header_str[10];
  79. sprintf(clk_header_str, "%08x", clk_header + 1);
  80. memcpy(scrypt_golden_ob, clk_header_str, 8);
  81. info->golden_ob = scrypt_golden_ob;
  82. if (!icarus_detect_custom(devpath, drv, info))
  83. {
  84. free(info);
  85. return false;
  86. }
  87. double duration_sec;
  88. const double hash_count = (double)0xd26;
  89. uint64_t default_hashes_per_core = (((info->freq * 2) / 3) * 1024) / ZEUSMINER_CHIP_CORES;
  90. if (info->ignore_golden_nonce)
  91. duration_sec = hash_count / default_hashes_per_core;
  92. else
  93. duration_sec = ((double)(info->golden_tv.tv_sec) + ((double)(info->golden_tv.tv_usec)) / ((double)1000000));
  94. //determines how the hash rate is calculated when no nonce is returned
  95. info->Hs = (double)(duration_sec / hash_count / info->chips / ZEUSMINER_CHIP_CORES);
  96. //set the read_count (how long to wait for a result) based on chips, cores, and time to find a nonce
  97. int chips_count_max = ZEUSMINER_CHIPS_COUNT_MAX;
  98. if (info->chips > chips_count_max)
  99. chips_count_max = upper_power_of_two_u32(info->chips);
  100. //golden_speed_per_core is the number of hashes / second / core
  101. uint64_t golden_speed_per_core = (uint64_t)(hash_count / duration_sec);
  102. //don't combine the following two lines - overflows leaving info->read_count at 0
  103. info->read_count = (uint32_t)((4294967296 * 10) / (ZEUSMINER_CHIP_CORES * chips_count_max * golden_speed_per_core * 2));
  104. info->read_count = info->read_count * 3 / 4;
  105. return true;
  106. }
  107. // support for --set-device
  108. // must be set before probing the device
  109. static
  110. const char *zeusminer_set_clock(struct cgpu_info * const device, const char * const option, const char * const setting, char * const replybuf, enum bfg_set_device_replytype * const success)
  111. {
  112. struct ICARUS_INFO * const info = device->device_data;
  113. int val = atoi(setting);
  114. if (val < ZEUSMINER_MIN_CLOCK || val > ZEUSMINER_MAX_CLOCK) {
  115. sprintf(replybuf, "invalid clock: '%s' valid range %d-%d",
  116. setting, ZEUSMINER_MIN_CLOCK, ZEUSMINER_MAX_CLOCK);
  117. return replybuf;
  118. }
  119. info->freq = val;
  120. return NULL;
  121. }
  122. static
  123. const char *zeusminer_set_chips(struct cgpu_info * const device, const char * const option, const char * const setting, char * const replybuf, enum bfg_set_device_replytype * const success)
  124. {
  125. struct ICARUS_INFO * const info = device->device_data;
  126. info->chips = atoi(setting);
  127. return NULL;
  128. }
  129. static
  130. const char *zeusminer_set_ignore_golden_nonce(struct cgpu_info * const device, const char * const option, const char * const setting, char * const replybuf, enum bfg_set_device_replytype * const success)
  131. {
  132. struct ICARUS_INFO * const info = device->device_data;
  133. info->ignore_golden_nonce = atoi(setting) == 1;
  134. return NULL;
  135. }
  136. static
  137. const struct bfg_set_device_definition zeusminer_set_device_funcs[] = {
  138. { "clock", zeusminer_set_clock, NULL },
  139. { "chips", zeusminer_set_chips, NULL },
  140. { "ignore_golden_nonce",zeusminer_set_ignore_golden_nonce, NULL },
  141. { NULL },
  142. };
  143. static
  144. bool zeusminer_lowl_probe(const struct lowlevel_device_info * const info)
  145. {
  146. return vcom_lowl_probe_wrapper(info, zeusminer_detect_one);
  147. }
  148. // device_drv functions - miner.h
  149. static
  150. bool zeusminer_thread_init(struct thr_info * const thr)
  151. {
  152. struct cgpu_info * const device = thr->cgpu;
  153. device->min_nonce_diff = 1./0x10000;
  154. return icarus_init(thr);
  155. }
  156. static
  157. bool zeusminer_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  158. {
  159. struct cgpu_info * const device = thr->cgpu;
  160. struct icarus_state * const state = thr->cgpu_data;
  161. struct ICARUS_INFO * const info = device->device_data;
  162. uint32_t clk_header = zeusminer_calc_clk_header(info->freq);
  163. uint32_t diff = work->nonce_diff * 0x10000;
  164. uint32_t target_me = 0xffff / diff;
  165. uint32_t header = clk_header + target_me;
  166. pk_u32be(state->ob_bin, 0, header);
  167. bswap_32mult(&state->ob_bin[4], work->data, 80/4);
  168. return true;
  169. }
  170. // device_drv definition - miner.h
  171. static
  172. void zeusminer_drv_init()
  173. {
  174. // based on Icarus
  175. zeusminer_drv = icarus_drv;
  176. // metadata
  177. zeusminer_drv.dname = "zeusminer";
  178. zeusminer_drv.name = "ZUS";
  179. zeusminer_drv.supported_algos = POW_SCRYPT;
  180. // detect device
  181. zeusminer_drv.lowl_probe = zeusminer_lowl_probe;
  182. // initialize thread
  183. zeusminer_drv.thread_init = zeusminer_thread_init;
  184. // Icarus scanhash mining hooks
  185. zeusminer_drv.job_prepare = zeusminer_job_prepare;
  186. // specify driver probe priority
  187. ++zeusminer_drv.probe_priority;
  188. }
  189. struct device_drv zeusminer_drv = {
  190. .drv_init = zeusminer_drv_init,
  191. };