driver-zeusminer.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 "config.h"
  12. #include <stdbool.h>
  13. #include <stdint.h>
  14. #include <string.h>
  15. #include <math.h>
  16. #include "miner.h"
  17. #include "driver-icarus.h"
  18. #include "lowl-vcom.h"
  19. #define ZEUSMINER_IO_SPEED 115200
  20. #define ZEUSMINER_CHIP_GEN1_CORES 8
  21. #define ZEUSMINER_CHIP_CORES ZEUSMINER_CHIP_GEN1_CORES
  22. #define ZEUSMINER_CHIPS_COUNT_MAX 1
  23. #define ZEUSMINER_CHIPS_COUNT 6
  24. #define ZEUSMINER_DEFAULT_CLOCK 328
  25. #define ZEUSMINER_MIN_CLOCK 200
  26. #define ZEUSMINER_MAX_CLOCK 383
  27. BFG_REGISTER_DRIVER(zeusminer_drv)
  28. static
  29. const struct bfg_set_device_definition zeusminer_set_device_funcs_probe[];
  30. static
  31. const struct bfg_set_device_definition zeusminer_set_device_funcs_live[];
  32. // device helper functions
  33. static
  34. uint32_t zeusminer_calc_clk_header(uint16_t freq)
  35. {
  36. //set clk_reg based on chip_clk
  37. uint32_t clk_reg = (uint32_t)freq * 2 / 3;
  38. //clock speed mask for header
  39. uint32_t clk_header = (clk_reg << 24) + ((0xff - clk_reg) << 16);
  40. return clk_header;
  41. }
  42. // ICARUS_INFO functions - driver-icarus.h
  43. // device detection
  44. static
  45. bool zeusminer_detect_one(const char *devpath)
  46. {
  47. struct device_drv *drv = &zeusminer_drv;
  48. struct ICARUS_INFO *info = calloc(1, sizeof(struct ICARUS_INFO));
  49. if (unlikely(!info))
  50. quit(1, "Failed to malloc ICARUS_INFO");
  51. char scrypt_golden_ob[] =
  52. "55aa" //Freq is set to 0x55*1.5=85Mhz
  53. "0001" //We want to find a Nonce which result's diff is at least 32768
  54. "00038000" //Starting Nonce
  55. "063b0b1b" //Bits (target in compact form)
  56. "028f3253" //Timestamp
  57. "5e900609c15dc49a42b1d8492a6dd4f8f15295c989a1decf584a6aa93be26066" //Merkle root
  58. "d3185f55ef635b5865a7a79b7fa74121a6bb819da416328a9bd2f8cef72794bf" //Previous hash
  59. "02000000"; //Version
  60. const char scrypt_golden_nonce[] = "00038d26";
  61. *info = (struct ICARUS_INFO){
  62. .baud = ZEUSMINER_IO_SPEED,
  63. .timing_mode = MODE_DEFAULT,
  64. // if do_icarus_timing is true, the timing adjustment may
  65. // result in a read_count that considers the device Idle
  66. .do_icarus_timing = false,
  67. .probe_read_count = 5,
  68. .golden_nonce = scrypt_golden_nonce,
  69. .chips = ZEUSMINER_CHIPS_COUNT,
  70. .freq = ZEUSMINER_DEFAULT_CLOCK,
  71. };
  72. //pick up any user-defined settings passed in via --set
  73. drv_set_defaults(drv, zeusminer_set_device_funcs_probe, info, devpath, detectone_meta_info.serial, 1);
  74. info->work_division = upper_power_of_two_u32(info->chips * ZEUSMINER_CHIP_CORES);
  75. info->fpga_count = info->chips * ZEUSMINER_CHIP_CORES;
  76. //send the requested Chip Speed with the detect golden OB
  77. //we use the time this request takes in order to calc hashes
  78. //so we need to use the same Chip Speed used when hashing
  79. uint32_t clk_header = zeusminer_calc_clk_header(info->freq);
  80. char clk_header_str[10];
  81. sprintf(clk_header_str, "%08x", clk_header + 1);
  82. memcpy(scrypt_golden_ob, clk_header_str, 8);
  83. info->golden_ob = scrypt_golden_ob;
  84. if (!icarus_detect_custom(devpath, drv, info) &&
  85. //ZM doesn't respond to detection 1 out of ~30 times
  86. !icarus_detect_custom(devpath, drv, info))
  87. {
  88. free(info);
  89. return false;
  90. }
  91. double duration_sec;
  92. const double hash_count = (double)0xd26;
  93. uint64_t default_hashes_per_core = (((info->freq * 2) / 3) * 1024) / ZEUSMINER_CHIP_CORES;
  94. if (info->ignore_golden_nonce)
  95. duration_sec = hash_count / default_hashes_per_core;
  96. else
  97. duration_sec = ((double)(info->golden_tv.tv_sec) + ((double)(info->golden_tv.tv_usec)) / ((double)1000000));
  98. //determines how the hash rate is calculated when no nonce is returned
  99. info->Hs = (double)(duration_sec / hash_count / info->chips / ZEUSMINER_CHIP_CORES);
  100. //set the read_count (how long to wait for a result) based on chips, cores, and time to find a nonce
  101. int chips_count_max = ZEUSMINER_CHIPS_COUNT_MAX;
  102. if (info->chips > chips_count_max)
  103. chips_count_max = upper_power_of_two_u32(info->chips);
  104. //golden_speed_per_core is the number of hashes / second / core
  105. uint64_t golden_speed_per_core = (uint64_t)(hash_count / duration_sec);
  106. //don't combine the following two lines - overflows leaving info->read_count at 0
  107. info->read_count = (uint32_t)((4294967296 * 10) / (ZEUSMINER_CHIP_CORES * chips_count_max * golden_speed_per_core * 2));
  108. info->read_count = info->read_count * 3 / 4;
  109. return true;
  110. }
  111. // support for --set-device
  112. // must be set before probing the device
  113. static
  114. bool zeusminer_set_clock_freq(struct cgpu_info * const device, int const freq)
  115. {
  116. struct ICARUS_INFO * const info = device->device_data;
  117. if (freq < ZEUSMINER_MIN_CLOCK || freq > ZEUSMINER_MAX_CLOCK)
  118. return false;
  119. info->freq = freq;
  120. return true;
  121. }
  122. static
  123. 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)
  124. {
  125. int val = atoi(setting);
  126. if (!zeusminer_set_clock_freq(device, val))
  127. {
  128. sprintf(replybuf, "invalid clock: '%s' valid range %d-%d",
  129. setting, ZEUSMINER_MIN_CLOCK, ZEUSMINER_MAX_CLOCK);
  130. return replybuf;
  131. }
  132. return NULL;
  133. }
  134. static
  135. 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)
  136. {
  137. struct ICARUS_INFO * const info = device->device_data;
  138. info->chips = atoi(setting);
  139. return NULL;
  140. }
  141. static
  142. 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)
  143. {
  144. struct ICARUS_INFO * const info = device->device_data;
  145. info->ignore_golden_nonce = atoi(setting) == 1;
  146. return NULL;
  147. }
  148. // for setting clock and chips during probe / detect
  149. static
  150. const struct bfg_set_device_definition zeusminer_set_device_funcs_probe[] = {
  151. { "clock", zeusminer_set_clock, NULL },
  152. { "chips", zeusminer_set_chips, NULL },
  153. { "ignore_golden_nonce",zeusminer_set_ignore_golden_nonce, NULL },
  154. { NULL },
  155. };
  156. // for setting clock while mining
  157. static
  158. const struct bfg_set_device_definition zeusminer_set_device_funcs_live[] = {
  159. { "clock", zeusminer_set_clock, NULL },
  160. { NULL },
  161. };
  162. static
  163. bool zeusminer_lowl_probe(const struct lowlevel_device_info * const info)
  164. {
  165. return vcom_lowl_probe_wrapper(info, zeusminer_detect_one);
  166. }
  167. // device_drv functions - miner.h
  168. static
  169. bool zeusminer_thread_init(struct thr_info * const thr)
  170. {
  171. struct cgpu_info * const device = thr->cgpu;
  172. device->min_nonce_diff = 1./0x10000;
  173. device->set_device_funcs = zeusminer_set_device_funcs_live;
  174. return icarus_init(thr);
  175. }
  176. static
  177. bool zeusminer_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  178. {
  179. struct cgpu_info * const device = thr->cgpu;
  180. struct icarus_state * const state = thr->cgpu_data;
  181. struct ICARUS_INFO * const info = device->device_data;
  182. uint32_t clk_header = zeusminer_calc_clk_header(info->freq);
  183. uint32_t diff = work->nonce_diff * 0x10000;
  184. uint32_t target_me = 0xffff / diff;
  185. uint32_t header = clk_header + target_me;
  186. pk_u32be(state->ob_bin, 0, header);
  187. bswap_32mult(&state->ob_bin[4], work->data, 80/4);
  188. return true;
  189. }
  190. // display the Chip # in the UI when viewing per-proc details
  191. static
  192. bool zeusminer_override_statline_temp2(char *buf, size_t bufsz, struct cgpu_info *device, __maybe_unused bool per_processor)
  193. {
  194. if (per_processor && ((device->proc_id % ZEUSMINER_CHIP_CORES) == 0))
  195. {
  196. tailsprintf(buf, bufsz, "C:%-3d", device->proc_id / ZEUSMINER_CHIP_CORES);
  197. return true;
  198. }
  199. return false;
  200. }
  201. // return the Chip # in via the API when procdetails is called
  202. static
  203. struct api_data *zeusminer_get_api_extra_device_detail(struct cgpu_info *device)
  204. {
  205. int chip = device->proc_id / ZEUSMINER_CHIP_CORES;
  206. return api_add_int(NULL, "Chip", &chip, true);
  207. }
  208. /*
  209. * specify settings / options via TUI
  210. */
  211. #ifdef HAVE_CURSES
  212. static
  213. void zeusminer_tui_wlogprint_choices(struct cgpu_info * const proc)
  214. {
  215. wlogprint("[C]lock speed ");
  216. }
  217. static
  218. const char *zeusminer_tui_handle_choice(struct cgpu_info * const proc, const int input)
  219. {
  220. static char buf[0x100]; // Static for replies
  221. switch (input)
  222. {
  223. case 'c': case 'C':
  224. {
  225. sprintf(buf, "Set clock speed");
  226. char * const setting = curses_input(buf);
  227. if (zeusminer_set_clock_freq(proc->device, atoi(setting)))
  228. {
  229. return "Clock speed changed\n";
  230. }
  231. else
  232. {
  233. sprintf(buf, "Invalid clock: '%s' valid range %d-%d",
  234. setting, ZEUSMINER_MIN_CLOCK, ZEUSMINER_MAX_CLOCK);
  235. return buf;
  236. }
  237. }
  238. }
  239. return NULL;
  240. }
  241. static
  242. void zeusminer_wlogprint_status(struct cgpu_info * const proc)
  243. {
  244. struct ICARUS_INFO * const info = proc->device->device_data;
  245. wlogprint("Clock speed: %d\n", info->freq);
  246. }
  247. #endif
  248. // device_drv definition - miner.h
  249. static
  250. void zeusminer_drv_init()
  251. {
  252. // based on Icarus
  253. zeusminer_drv = icarus_drv;
  254. // metadata
  255. zeusminer_drv.dname = "zeusminer";
  256. zeusminer_drv.name = "ZUS";
  257. zeusminer_drv.supported_algos = POW_SCRYPT;
  258. // detect device
  259. zeusminer_drv.lowl_probe = zeusminer_lowl_probe;
  260. // initialize thread
  261. zeusminer_drv.thread_init = zeusminer_thread_init;
  262. // Icarus scanhash mining hooks
  263. zeusminer_drv.job_prepare = zeusminer_job_prepare;
  264. // specify driver probe priority
  265. // currently setup specifically to probe before DualMiner
  266. zeusminer_drv.probe_priority = -100;
  267. // output the chip # when viewing per-proc stats
  268. // so we can easily ID chips vs cores
  269. zeusminer_drv.override_statline_temp2 = zeusminer_override_statline_temp2;
  270. // output the chip # via RPC API
  271. zeusminer_drv.get_api_extra_device_detail = zeusminer_get_api_extra_device_detail;
  272. // TUI support - e.g. setting clock via UI
  273. #ifdef HAVE_CURSES
  274. zeusminer_drv.proc_wlogprint_status = zeusminer_wlogprint_status;
  275. zeusminer_drv.proc_tui_wlogprint_choices = zeusminer_tui_wlogprint_choices;
  276. zeusminer_drv.proc_tui_handle_choice = zeusminer_tui_handle_choice;
  277. #endif
  278. }
  279. struct device_drv zeusminer_drv = {
  280. .drv_init = zeusminer_drv_init,
  281. };