libbitfury.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  2. * Copyright 2013 bitfury
  3. * Copyright 2013 legkodymov
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include "config.h"
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <string.h>
  27. #include "miner.h"
  28. #include "libbitfury.h"
  29. #include "spidevc.h"
  30. #include "sha2.h"
  31. #include <time.h>
  32. #define BITFURY_REFRESH_DELAY 100
  33. #define BITFURY_DETECT_TRIES 3000 / BITFURY_REFRESH_DELAY
  34. // 0 .... 31 bit
  35. // 1000 0011 0101 0110 1001 1010 1100 0111
  36. // 1100 0001 0110 1010 0101 1001 1110 0011
  37. // C16A59E3
  38. unsigned char enaconf[4] = { 0xc1, 0x6a, 0x59, 0xe3 };
  39. unsigned char disconf[4] = { 0, 0, 0, 0 };
  40. unsigned decnonce(unsigned in);
  41. /* Configuration registers - control oscillators and such stuff. PROGRAMMED when magic number is matches, UNPROGRAMMED (default) otherwise */
  42. void config_reg(struct spi_port *port, int cfgreg, int ena)
  43. {
  44. if (ena) spi_emit_data(port, 0x7000+cfgreg*32, (void*)enaconf, 4);
  45. else spi_emit_data(port, 0x7000+cfgreg*32, (void*)disconf, 4);
  46. }
  47. #define FIRST_BASE 61
  48. #define SECOND_BASE 4
  49. char counters[16] = { 64, 64,
  50. SECOND_BASE, SECOND_BASE+4, SECOND_BASE+2, SECOND_BASE+2+16, SECOND_BASE, SECOND_BASE+1,
  51. (FIRST_BASE)%65, (FIRST_BASE+1)%65, (FIRST_BASE+3)%65, (FIRST_BASE+3+16)%65, (FIRST_BASE+4)%65, (FIRST_BASE+4+4)%65, (FIRST_BASE+3+3)%65, (FIRST_BASE+3+1+3)%65};
  52. //char counters[16] = { 64, 64,
  53. // SECOND_BASE, SECOND_BASE+4, SECOND_BASE+2, SECOND_BASE+2+16, SECOND_BASE, SECOND_BASE+1,
  54. // (FIRST_BASE)%65, (FIRST_BASE+1)%65, (FIRST_BASE+3)%65, (FIRST_BASE+3+16)%65, (FIRST_BASE+4)%65, (FIRST_BASE+4+4)%65, (FIRST_BASE+3+3)%65, (FIRST_BASE+3+1+3)%65};
  55. char *buf = "Hello, World!\x55\xaa";
  56. char outbuf[16];
  57. /* Oscillator setup variants (maybe more), values inside of chip ANDed to not allow by programming errors work it at higher speeds */
  58. /* WARNING! no chip temperature control limits, etc. It may self-fry and make fried chips with great ease :-) So if trying to overclock */
  59. /* Do not place chip near flammable objects, provide adequate power protection and better wear eye protection ! */
  60. /* Thermal runaway in this case could produce nice flames of chippy fries */
  61. // Thermometer code from left to right - more ones ==> faster clock!
  62. /* Test vectors to calculate (using address-translated loads) */
  63. unsigned atrvec[] = {
  64. 0xb0e72d8e, 0x1dc5b862, 0xe9e7c4a6, 0x3050f1f5, 0x8a1a6b7e, 0x7ec384e8, 0x42c1c3fc, 0x8ed158a1, /* MIDSTATE */
  65. 0,0,0,0,0,0,0,0,
  66. 0x8a0bb7b7, 0x33af304f, 0x0b290c1a, 0xf0c4e61f, /* WDATA: hashMerleRoot[7], nTime, nBits, nNonce */
  67. 0x9c4dfdc0, 0xf055c9e1, 0xe60f079d, 0xeeada6da, 0xd459883d, 0xd8049a9d, 0xd49f9a96, 0x15972fed, /* MIDSTATE */
  68. 0,0,0,0,0,0,0,0,
  69. 0x048b2528, 0x7acb2d4f, 0x0b290c1a, 0xbe00084a, /* WDATA: hashMerleRoot[7], nTime, nBits, nNonce */
  70. 0x0317b3ea, 0x1d227d06, 0x3cca281e, 0xa6d0b9da, 0x1a359fe2, 0xa7287e27, 0x8b79c296, 0xc4d88274, /* MIDSTATE */
  71. 0,0,0,0,0,0,0,0,
  72. 0x328bcd4f, 0x75462d4f, 0x0b290c1a, 0x002c6dbc, /* WDATA: hashMerleRoot[7], nTime, nBits, nNonce */
  73. 0xac4e38b6, 0xba0e3b3b, 0x649ad6f8, 0xf72e4c02, 0x93be06fb, 0x366d1126, 0xf4aae554, 0x4ff19c5b, /* MIDSTATE */
  74. 0,0,0,0,0,0,0,0,
  75. 0x72698140, 0x3bd62b4f, 0x3fd40c1a, 0x801e43e9, /* WDATA: hashMerleRoot[7], nTime, nBits, nNonce */
  76. 0x9dbf91c9, 0x12e5066c, 0xf4184b87, 0x8060bc4d, 0x18f9c115, 0xf589d551, 0x0f7f18ae, 0x885aca59, /* MIDSTATE */
  77. 0,0,0,0,0,0,0,0,
  78. 0x6f3806c3, 0x41f82a4f, 0x3fd40c1a, 0x00334b39, /* WDATA: hashMerleRoot[7], nTime, nBits, nNonce */
  79. };
  80. #define rotrFixed(x,y) (((x) >> (y)) | ((x) << (32-(y))))
  81. #define s0(x) (rotrFixed(x,7)^rotrFixed(x,18)^(x>>3))
  82. #define s1(x) (rotrFixed(x,17)^rotrFixed(x,19)^(x>>10))
  83. #define Ch(x,y,z) (z^(x&(y^z)))
  84. #define Maj(x,y,z) (y^((x^y)&(y^z)))
  85. #define S0(x) (rotrFixed(x,2)^rotrFixed(x,13)^rotrFixed(x,22))
  86. #define S1(x) (rotrFixed(x,6)^rotrFixed(x,11)^rotrFixed(x,25))
  87. /* SHA256 CONSTANTS */
  88. static const unsigned SHA_K[64] = {
  89. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  90. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  91. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  92. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  93. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  94. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  95. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  96. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  97. };
  98. void t_print(struct timespec d_time) {
  99. printf(" %ds %.2fms\n", (int)d_time.tv_sec, (double)d_time.tv_nsec / 1000000.0);
  100. }
  101. struct timespec t_add(struct timespec time1, struct timespec time2) {
  102. struct timespec result ;
  103. result.tv_sec = time1.tv_sec + time2.tv_sec ;
  104. result.tv_nsec = time1.tv_nsec + time2.tv_nsec ;
  105. if (result.tv_nsec >= 1000000000L) {
  106. result.tv_sec++ ; result.tv_nsec = result.tv_nsec - 1000000000L ;
  107. }
  108. return (result) ;
  109. }
  110. struct timespec t_diff(struct timespec start, struct timespec end)
  111. {
  112. struct timespec temp;
  113. if (end.tv_nsec < start.tv_nsec) {
  114. temp.tv_sec = end.tv_sec-start.tv_sec-1;
  115. temp.tv_nsec = 1000000000LU;
  116. temp.tv_nsec -= start.tv_nsec;
  117. temp.tv_nsec += end.tv_nsec;
  118. } else {
  119. temp.tv_sec = end.tv_sec-start.tv_sec;
  120. temp.tv_nsec = end.tv_nsec-start.tv_nsec;
  121. }
  122. return temp;
  123. }
  124. void ms3_compute(unsigned *p)
  125. {
  126. unsigned a,b,c,d,e,f,g,h, ne, na, i;
  127. a = p[0]; b = p[1]; c = p[2]; d = p[3]; e = p[4]; f = p[5]; g = p[6]; h = p[7];
  128. for (i = 0; i < 3; i++) {
  129. ne = p[i+16] + SHA_K[i] + h + Ch(e,f,g) + S1(e) + d;
  130. na = p[i+16] + SHA_K[i] + h + Ch(e,f,g) + S1(e) + S0(a) + Maj(a,b,c);
  131. d = c; c = b; b = a; a = na;
  132. h = g; g = f; f = e; e = ne;
  133. }
  134. p[15] = a; p[14] = b; p[13] = c; p[12] = d; p[11] = e; p[10] = f; p[9] = g; p[8] = h;
  135. }
  136. void send_conf(struct spi_port *port) {
  137. int i;
  138. for (i = 7; i <= 11; ++i)
  139. config_reg(port, i, 0);
  140. config_reg(port, 6, 0); /* disable OUTSLK */
  141. config_reg(port, 4, 1); /* Enable slow oscillator */
  142. for (i = 1; i <= 3; ++i)
  143. config_reg(port, i, 0);
  144. spi_emit_data(port, 0x0100, counters, 16); /* Program counters correctly for rounds processing, here baby should start consuming power */
  145. }
  146. void send_init(struct spi_port *port) {
  147. /* Prepare internal buffers */
  148. /* PREPARE BUFFERS (INITIAL PROGRAMMING) */
  149. unsigned w[16];
  150. unsigned atrvec[] = {
  151. 0xb0e72d8e, 0x1dc5b862, 0xe9e7c4a6, 0x3050f1f5, 0x8a1a6b7e, 0x7ec384e8, 0x42c1c3fc, 0x8ed158a1, /* MIDSTATE */
  152. 0,0,0,0,0,0,0,0,
  153. 0x8a0bb7b7, 0x33af304f, 0x0b290c1a, 0xf0c4e61f, /* WDATA: hashMerleRoot[7], nTime, nBits, nNonce */
  154. };
  155. ms3_compute(&atrvec[0]);
  156. memset(&w, 0, sizeof(w)); w[3] = 0xffffffff; w[4] = 0x80000000; w[15] = 0x00000280;
  157. spi_emit_data(port, 0x1000, w, 16*4);
  158. spi_emit_data(port, 0x1400, w, 8*4);
  159. memset(w, 0, sizeof(w)); w[0] = 0x80000000; w[7] = 0x100;
  160. spi_emit_data(port, 0x1900, &w[0],8*4); /* Prepare MS and W buffers! */
  161. spi_emit_data(port, 0x3000, &atrvec[0], 19*4);
  162. }
  163. void set_freq(struct spi_port *port, int bits) {
  164. uint64_t freq;
  165. unsigned char *osc6;
  166. osc6 = (unsigned char *)&freq;
  167. freq = (1ULL << bits) - 1ULL;
  168. spi_emit_data(port, 0x6000, osc6, 8); /* Program internal on-die slow oscillator frequency */
  169. config_reg(port, 4, 1); /* Enable slow oscillator */
  170. }
  171. void send_reinit(struct spi_port *port, int slot, int chip_n, int n) {
  172. spi_clear_buf(port);
  173. spi_emit_break(port);
  174. spi_emit_fasync(port, chip_n);
  175. set_freq(port, n);
  176. send_conf(port);
  177. send_init(port);
  178. spi_txrx(port);
  179. }
  180. void send_shutdown(struct spi_port *port, int slot, int chip_n) {
  181. spi_clear_buf(port);
  182. spi_emit_break(port);
  183. spi_emit_fasync(port, chip_n);
  184. config_reg(port, 4, 0); /* Disable slow oscillator */
  185. spi_txrx(port);
  186. }
  187. void send_freq(struct spi_port *port, int slot, int chip_n, int bits) {
  188. spi_clear_buf(port);
  189. spi_emit_break(port);
  190. spi_emit_fasync(port, chip_n);
  191. set_freq(port, bits);
  192. spi_txrx(port);
  193. }
  194. unsigned int c_diff(unsigned ocounter, unsigned counter) {
  195. return counter > ocounter ? counter - ocounter : (0x003FFFFF - ocounter) + counter;
  196. }
  197. int get_counter(unsigned int *newbuf, unsigned int *oldbuf) {
  198. int j;
  199. for(j = 0; j < 16; j++) {
  200. if (newbuf[j] != oldbuf[j]) {
  201. unsigned counter = decnonce(newbuf[j]);
  202. if ((counter & 0xFFC00000) == 0xdf800000) {
  203. counter -= 0xdf800000;
  204. return counter;
  205. }
  206. }
  207. }
  208. return 0;
  209. }
  210. int detect_chip(struct spi_port *port, int chip_n) {
  211. int i;
  212. unsigned newbuf[17], oldbuf[17];
  213. unsigned ocounter;
  214. int odiff;
  215. struct timespec t1;
  216. memset(newbuf, 0, 17 * 4);
  217. memset(oldbuf, 0, 17 * 4);
  218. ms3_compute(&atrvec[0]);
  219. ms3_compute(&atrvec[20]);
  220. ms3_compute(&atrvec[40]);
  221. spi_clear_buf(port);
  222. spi_emit_break(port); /* First we want to break chain! Otherwise we'll get all of traffic bounced to output */
  223. spi_emit_fasync(port, chip_n);
  224. set_freq(port, 52); //54 - 3F, 53 - 1F
  225. send_conf(port);
  226. send_init(port);
  227. spi_txrx(port);
  228. ocounter = 0;
  229. for (i = 0; i < BITFURY_DETECT_TRIES; i++) {
  230. int counter;
  231. spi_clear_buf(port);
  232. spi_emit_break(port);
  233. spi_emit_fasync(port, chip_n);
  234. spi_emit_data(port, 0x3000, &atrvec[0], 19*4);
  235. spi_txrx(port);
  236. memcpy(newbuf, spi_getrxbuf(port) + 4 + chip_n, 17*4);
  237. clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
  238. counter = get_counter(newbuf, oldbuf);
  239. if (ocounter) {
  240. unsigned int cdiff = c_diff(ocounter, counter);
  241. if (cdiff > 5000 && cdiff < 100000 && odiff > 5000 && odiff < 100000)
  242. return 1;
  243. odiff = cdiff;
  244. }
  245. ocounter = counter;
  246. if (newbuf[16] != 0 && newbuf[16] != 0xFFFFFFFF) {
  247. return 0;
  248. }
  249. cgsleep_ms(BITFURY_REFRESH_DELAY / 10);
  250. memcpy(oldbuf, newbuf, 17 * 4);
  251. }
  252. return 0;
  253. }
  254. int libbitfury_detectChips1(struct spi_port *port) {
  255. int n;
  256. for (n = 0; detect_chip(port, n); ++n)
  257. {}
  258. return n;
  259. }
  260. unsigned decnonce(unsigned in)
  261. {
  262. unsigned out;
  263. /* First part load */
  264. out = (in & 0xFF) << 24; in >>= 8;
  265. /* Byte reversal */
  266. in = (((in & 0xaaaaaaaa) >> 1) | ((in & 0x55555555) << 1));
  267. in = (((in & 0xcccccccc) >> 2) | ((in & 0x33333333) << 2));
  268. in = (((in & 0xf0f0f0f0) >> 4) | ((in & 0x0f0f0f0f) << 4));
  269. out |= (in >> 2)&0x3FFFFF;
  270. /* Extraction */
  271. if (in & 1) out |= (1 << 23);
  272. if (in & 2) out |= (1 << 22);
  273. out -= 0x800004;
  274. return out;
  275. }
  276. int rehash(unsigned char *midstate, unsigned m7,
  277. unsigned ntime, unsigned nbits, unsigned nnonce) {
  278. unsigned char in[16];
  279. unsigned int *in32 = (unsigned int *)in;
  280. unsigned int *mid32 = (unsigned int *)midstate;
  281. unsigned out32[8];
  282. unsigned char *out = (unsigned char *) out32;
  283. #ifdef BITFURY_REHASH_DEBUG
  284. static unsigned history[512];
  285. static unsigned history_p;
  286. #endif
  287. sha256_ctx ctx;
  288. memset( &ctx, 0, sizeof( sha256_ctx ) );
  289. memcpy(ctx.h, mid32, 8*4);
  290. ctx.tot_len = 64;
  291. ctx.len = 0;
  292. nnonce = bswap_32(nnonce);
  293. in32[0] = bswap_32(m7);
  294. in32[1] = bswap_32(ntime);
  295. in32[2] = bswap_32(nbits);
  296. in32[3] = nnonce;
  297. sha256_update(&ctx, in, 16);
  298. sha256_final(&ctx, out);
  299. sha256(out, 32, out);
  300. if (out32[7] == 0) {
  301. #ifdef BITFURY_REHASH_DEBUG
  302. char hex[65];
  303. bin2hex(hex, out, 32);
  304. applog(LOG_INFO, "! MS0: %08x, m7: %08x, ntime: %08x, nbits: %08x, nnonce: %08x\n\t\t\t out: %s\n", mid32[0], m7, ntime, nbits, nnonce, hex);
  305. history[history_p] = nnonce;
  306. history_p++; history_p &= 512 - 1;
  307. #endif
  308. return 1;
  309. }
  310. return 0;
  311. }
  312. void work_to_payload(struct bitfury_payload *p, struct work *w) {
  313. unsigned char flipped_data[80];
  314. memset(p, 0, sizeof(struct bitfury_payload));
  315. swap32yes(flipped_data, w->data, 80 / 4);
  316. memcpy(p->midstate, w->midstate, 32);
  317. p->m7 = bswap_32(*(unsigned *)(flipped_data + 64));
  318. p->ntime = bswap_32(*(unsigned *)(flipped_data + 68));
  319. p->nbits = bswap_32(*(unsigned *)(flipped_data + 72));
  320. }
  321. void libbitfury_sendHashData1(int chip_id, struct bitfury_device *d, bool second_run)
  322. {
  323. struct spi_port *port = d->spi;
  324. unsigned *newbuf = d->newbuf;
  325. unsigned *oldbuf = d->oldbuf;
  326. struct bitfury_payload *p = &(d->payload);
  327. struct bitfury_payload *op = &(d->opayload);
  328. struct bitfury_payload *o2p = &(d->o2payload);
  329. struct timespec d_time;
  330. struct timespec time;
  331. int smart = 0;
  332. int chip = d->fasync;
  333. memcpy(atrvec, p, 20*4);
  334. ms3_compute(atrvec);
  335. clock_gettime(CLOCK_REALTIME, &(time));
  336. if (!second_run) {
  337. d->predict2 = d->predict1 = time;
  338. d->counter1 = d->counter2 = 0;
  339. d->req2_done = 0;
  340. };
  341. d_time = t_diff(time, d->predict1);
  342. if (d_time.tv_sec < 0 && (d->req2_done || !smart)) {
  343. d->otimer1 = d->timer1;
  344. d->timer1 = time;
  345. /* Programming next value */
  346. spi_clear_buf(port);
  347. spi_emit_break(port);
  348. spi_emit_fasync(port, chip);
  349. spi_emit_data(port, 0x3000, &atrvec[0], 19*4);
  350. if (smart) {
  351. config_reg(port, 3, 0);
  352. }
  353. clock_gettime(CLOCK_REALTIME, &(time));
  354. d_time = t_diff(time, d->predict1);
  355. spi_txrx(port);
  356. memcpy(newbuf, spi_getrxbuf(port)+4 + chip, 17*4);
  357. d->job_switched = newbuf[16] != oldbuf[16];
  358. int i;
  359. int results_num = 0;
  360. int found = 0;
  361. unsigned * results = d->results;
  362. d->old_nonce = 0;
  363. d->future_nonce = 0;
  364. for (i = 0; i < 16; i++) {
  365. if (oldbuf[i] != newbuf[i] && op && o2p) {
  366. unsigned pn; //possible nonce
  367. unsigned int s = 0; //TODO zero may be solution
  368. if ((newbuf[i] & 0xFF) == 0xE0)
  369. continue;
  370. pn = decnonce(newbuf[i]);
  371. s |= rehash(op->midstate, op->m7, op->ntime, op->nbits, pn) ? pn : 0;
  372. s |= rehash(op->midstate, op->m7, op->ntime, op->nbits, pn-0x00400000) ? pn - 0x00400000 : 0;
  373. s |= rehash(op->midstate, op->m7, op->ntime, op->nbits, pn-0x00800000) ? pn - 0x00800000 : 0;
  374. s |= rehash(op->midstate, op->m7, op->ntime, op->nbits, pn+0x02800000) ? pn + 0x02800000 : 0;
  375. s |= rehash(op->midstate, op->m7, op->ntime, op->nbits, pn+0x02C00000) ? pn + 0x02C00000 : 0;
  376. s |= rehash(op->midstate, op->m7, op->ntime, op->nbits, pn+0x00400000) ? pn + 0x00400000 : 0;
  377. if (s) {
  378. int k;
  379. int dup = 0;
  380. for (k = 0; k < results_num; k++) {
  381. if (results[k] == bswap_32(s)) {
  382. dup = 1;
  383. }
  384. }
  385. if (!dup) {
  386. results[results_num++] = bswap_32(s);
  387. found++;
  388. }
  389. }
  390. s = 0;
  391. pn = decnonce(newbuf[i]);
  392. s |= rehash(o2p->midstate, o2p->m7, o2p->ntime, o2p->nbits, pn) ? pn : 0;
  393. s |= rehash(o2p->midstate, o2p->m7, o2p->ntime, o2p->nbits, pn-0x400000) ? pn - 0x400000 : 0;
  394. s |= rehash(o2p->midstate, o2p->m7, o2p->ntime, o2p->nbits, pn-0x800000) ? pn - 0x800000 : 0;
  395. s |= rehash(o2p->midstate, o2p->m7, o2p->ntime, o2p->nbits, pn+0x2800000)? pn + 0x2800000 : 0;
  396. s |= rehash(o2p->midstate, o2p->m7, o2p->ntime, o2p->nbits, pn+0x2C00000)? pn + 0x2C00000 : 0;
  397. s |= rehash(o2p->midstate, o2p->m7, o2p->ntime, o2p->nbits, pn+0x400000) ? pn + 0x400000 : 0;
  398. if (s) {
  399. d->old_nonce = bswap_32(s);
  400. found++;
  401. }
  402. s = 0;
  403. pn = decnonce(newbuf[i]);
  404. s |= rehash(p->midstate, p->m7, p->ntime, p->nbits, pn) ? pn : 0;
  405. s |= rehash(p->midstate, p->m7, p->ntime, p->nbits, pn-0x400000) ? pn - 0x400000 : 0;
  406. s |= rehash(p->midstate, p->m7, p->ntime, p->nbits, pn-0x800000) ? pn - 0x800000 : 0;
  407. s |= rehash(p->midstate, p->m7, p->ntime, p->nbits, pn+0x2800000)? pn + 0x2800000 : 0;
  408. s |= rehash(p->midstate, p->m7, p->ntime, p->nbits, pn+0x2C00000)? pn + 0x2C00000 : 0;
  409. s |= rehash(p->midstate, p->m7, p->ntime, p->nbits, pn+0x400000) ? pn + 0x400000 : 0;
  410. if (s) {
  411. d->future_nonce = bswap_32(s);
  412. found++;
  413. }
  414. if (!found) {
  415. printf("AAA Strange: %08x, chip_id: %d\n", pn, chip_id);
  416. }
  417. }
  418. }
  419. d->results_n = results_num;
  420. if (smart) {
  421. d_time = t_diff(d->timer2, d->timer1);
  422. } else {
  423. d_time = t_diff(d->otimer1, d->timer1);
  424. }
  425. d->ocounter1 = d->counter1;
  426. d->counter1 = get_counter(newbuf, oldbuf);
  427. if (d->counter2 || !smart) {
  428. int shift;
  429. int cycles;
  430. int req1_cycles;
  431. long long unsigned int period;
  432. double ns;
  433. unsigned full_cycles, half_cycles;
  434. double full_delay, half_delay;
  435. long long unsigned delta;
  436. struct timespec t_delta;
  437. double mhz;
  438. int ccase;
  439. shift = 800000;
  440. if (smart) {
  441. cycles = d->counter1 < d->counter2 ? 0x00400000 - d->counter2 + d->counter1 : d->counter1 - d->counter2; // + 0x003FFFFF;
  442. } else {
  443. if (d->counter1 > (0x00400000 - shift * 2) && d->ocounter1 > (0x00400000 - shift)) {
  444. cycles = 0x00400000 - d->ocounter1 + d->counter1; // + 0x003FFFFF;
  445. ccase = 1;
  446. } else {
  447. cycles = d->counter1 - d->ocounter1;
  448. ccase = 2;
  449. }
  450. }
  451. req1_cycles = 0x003FFFFF - d->counter1;
  452. period = (long long unsigned int)d_time.tv_sec * 1000000000ULL + (long long unsigned int)d_time.tv_nsec;
  453. ns = (double)period / (double)(cycles);
  454. mhz = 1.0 / ns * 65.0 * 1000.0;
  455. if (d->counter1 > 0 && d->counter1 < 0x001FFFFF)
  456. printf("AAA chip_id %2d: %llu ms, req1_cycles: %08u, counter1: %08d, ocounter1: %08d, counter2: %08d, cycles: %08d, ns: %.2f, mhz: %.2f \n", chip_id, period / 1000000ULL, req1_cycles, d->counter1, d->ocounter1, d->counter2, cycles, ns, mhz);
  457. if (ns > 2000.0 || ns < 20) {
  458. printf("AAA %d!Stupid ns chip_id %2d: %llu ms, req1_cycles: %08u, counter1: %08d, ocounter1: %08d, counter2: %08d, cycles: %08d, ns: %.2f, mhz: %.2f \n", ccase, chip_id, period / 1000000ULL, req1_cycles, d->counter1, d->ocounter1, d->counter2, cycles, ns, mhz);
  459. ns = 200.0;
  460. } else {
  461. d->ns = ns;
  462. d->mhz = mhz;
  463. }
  464. if (smart) {
  465. half_cycles = req1_cycles + shift;
  466. full_cycles = 0x003FFFFF - 2 * shift;
  467. } else {
  468. half_cycles = 0;
  469. full_cycles = req1_cycles > shift ? req1_cycles - shift : req1_cycles + 0x00400000 - shift;
  470. }
  471. half_delay = (double)half_cycles * ns * (1 +0.92);
  472. full_delay = (double)full_cycles * ns;
  473. delta = (long long unsigned)(full_delay + half_delay);
  474. t_delta.tv_sec = delta / 1000000000ULL;
  475. t_delta.tv_nsec = delta - t_delta.tv_sec * 1000000000ULL;
  476. d->predict1 = t_add(time, t_delta);
  477. if (smart) {
  478. half_cycles = req1_cycles + shift;
  479. full_cycles = 0;
  480. } else {
  481. full_cycles = req1_cycles + shift;
  482. }
  483. half_delay = (double)half_cycles * ns * (1 + 0.92);
  484. full_delay = (double)full_cycles * ns;
  485. delta = (long long unsigned)(full_delay + half_delay);
  486. t_delta.tv_sec = delta / 1000000000ULL;
  487. t_delta.tv_nsec = delta - t_delta.tv_sec * 1000000000ULL;
  488. d->predict2 = t_add(time, t_delta);
  489. d->req2_done = 0; d->req1_done = 0;
  490. }
  491. if (d->job_switched) {
  492. memcpy(o2p, op, sizeof(struct bitfury_payload));
  493. memcpy(op, p, sizeof(struct bitfury_payload));
  494. memcpy(oldbuf, newbuf, 17 * 4);
  495. }
  496. }
  497. clock_gettime(CLOCK_REALTIME, &(time));
  498. d_time = t_diff(time, d->predict2);
  499. if (d_time.tv_sec < 0 && !d->req2_done) {
  500. if(smart) {
  501. d->otimer2 = d->timer2;
  502. d->timer2 = time;
  503. spi_clear_buf(port);
  504. spi_emit_break(port);
  505. spi_emit_fasync(port, chip);
  506. spi_emit_data(port, 0x3000, &atrvec[0], 19*4);
  507. if (smart) {
  508. config_reg(port, 3, 1);
  509. }
  510. spi_txrx(port);
  511. memcpy(newbuf, spi_getrxbuf(port)+4 + chip, 17*4);
  512. d->counter2 = get_counter(newbuf, oldbuf);
  513. d->req2_done = 1;
  514. } else {
  515. d->req2_done = 1;
  516. }
  517. }
  518. }
  519. void libbitfury_sendHashData(struct bitfury_device *bf, int chip_n) {
  520. int chip_id;
  521. static bool second_run = false;
  522. for (chip_id = 0; chip_id < chip_n; chip_id++) {
  523. struct bitfury_device *d = bf + chip_id;
  524. libbitfury_sendHashData1(chip_id, d, second_run);
  525. }
  526. second_run = true;
  527. return;
  528. }
  529. int libbitfury_readHashData(unsigned int *res) {
  530. return 0;
  531. }