libbitfury.c 21 KB

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