Commit 2191ce0cb2cb56946823ad2171bbbb750c52cc27

Con Kolivas 2014-04-10T20:13:55

Merge spondoolies driver patch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
diff --git a/Makefile.am b/Makefile.am
index 658bfa2..4666e75 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -104,6 +104,10 @@ if HAS_COINTERRA
 cgminer_SOURCES += driver-cointerra.c driver-cointerra.h
 endif
 
+if HAS_SPONDOOLIES
+cgminer_SOURCES += driver-spondoolies.c mg_proto_parser.c
+endif
+
 if HAS_BAB
 cgminer_SOURCES += driver-bab.c
 endif
diff --git a/api.c b/api.c
index cf12762..ef63e95 100644
--- a/api.c
+++ b/api.c
@@ -30,7 +30,7 @@
 	defined(USE_HASHFAST) || defined(USE_BITFURY) || defined(USE_KLONDIKE) || \
 	defined(USE_KNC) || defined(USE_BAB) || defined(USE_DRILLBIT) || \
 	defined(USE_MINION) || defined(USE_COINTERRA) || defined(USE_BITMINE_A1) || \
-	defined(USE_ANT_S1)
+	defined(USE_ANT_S1) || defined(USE_SPONDOOLIES)
 #define HAVE_AN_ASIC 1
 #endif
 
@@ -196,6 +196,10 @@ static const char *DEVICECODE = ""
 #ifdef USE_COINTERRA
 			"CTA "
 #endif
+#ifdef USE_SPONDOOLIES
+			"SPN "
+#endif
+
 			"";
 
 static const char *OSINFO =
diff --git a/cgminer.c b/cgminer.c
index 9afee32..d88d2e3 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -76,6 +76,11 @@ char *curly = ":D";
 #include "driver-bflsc.h"
 #endif
 
+#ifdef USE_SPONDOOLIES
+#include "driver-spondoolies.h"
+#endif
+
+
 #ifdef USE_BITFURY
 #include "driver-bitfury.h"
 #endif
@@ -1638,6 +1643,9 @@ static char *opt_verusage_and_exit(const char *extra)
 #ifdef USE_BITMINE_A1
 		"Bitmine.A1 "
 #endif
+#ifdef USE_SPONDOOLIES
+		"spondoolies "
+#endif
 		"mining support.\n"
 		, packagename);
 	printf("%s", opt_usage(opt_argv0, extra));
diff --git a/configure.ac b/configure.ac
index 842d6bc..ae6ce53 100644
--- a/configure.ac
+++ b/configure.ac
@@ -304,6 +304,20 @@ if test "x$modminer" = xyes; then
 fi
 AM_CONDITIONAL([HAS_MODMINER], [test x$modminer = xyes])
 
+
+
+spondoolies="no"
+
+AC_ARG_ENABLE([spondoolies],
+	[AC_HELP_STRING([--enable-spondoolies],[Compile support for Spondoolies (default disabled)])],
+	[spondoolies=$enableval]
+	)
+if test "x$spondoolies" = xyes; then
+	AC_DEFINE([USE_SPONDOOLIES], [1], [Defined to 1 if Spondoolies support is wanted])
+fi
+AM_CONDITIONAL([HAS_SPONDOOLIES], [test x$spondoolies = xyes])
+
+
 curses="auto"
 
 AC_ARG_WITH([curses],
@@ -562,6 +576,12 @@ else
 	echo "  Cointerra.ASICs......: Disabled"
 fi
 
+if test "x$spondoolies" = xyes; then
+	echo "  Spondoolies.ASICs....: Enabled"
+else
+	echo "  Spondoolies.ASICs....: Disabled"
+fi
+
 if test "x$bitmine_A1" = xyes; then
 	echo "  Bitmine-A1.ASICs.....: Enabled"
 else
@@ -604,7 +624,7 @@ else
 	echo "  ModMiner.FPGAs.......: Disabled"
 fi
 
-if test "x$avalon$avalon2$bab$bflsc$bitforce$bitfury$hashfast$icarus$klondike$knc$modminer$drillbit$minion$cointerra$bitmine_A1$ants1" = xnononononononononononononononono; then
+if test "x$avalon$avalon2$bab$bflsc$bitforce$bitfury$hashfast$icarus$klondike$knc$modminer$drillbit$minion$cointerra$bitmine_A1$ants1$spondoolies" = xnonononononononononononononononono; then
 	AC_MSG_ERROR([No mining configured in])
 fi
 
diff --git a/driver-spondoolies.c b/driver-spondoolies.c
new file mode 100644
index 0000000..3518c5d
--- /dev/null
+++ b/driver-spondoolies.c
@@ -0,0 +1,439 @@
+/*
+ * Copyright 2014 Zvi (Zvisha) Shteingart - Spondoolies-tech.com
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.  See COPYING for more details.
+ */
+
+
+/*
+  This driver communicates the job requests via Unix socket to the minergate 
+  process, that is responsible for controlling the Spondoolies Dawson SP10 miner.
+
+  The jobs sent each with unique ID and returned asynchronously in one of the next 
+  transactions. REQUEST_PERIOD and REQUEST_SIZE define the communication rate with minergate.
+*/
+
+
+#include <float.h>
+#include <limits.h>
+#include <pthread.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <strings.h>
+#include <sys/time.h>
+#include <unistd.h>
+#include <assert.h>
+#include <time.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
+#include <string.h>
+#include <math.h>
+
+#include "config.h"
+
+#ifdef WIN32
+#include <windows.h>
+#endif
+
+#include "compat.h"
+#include "miner.h"
+#include "mg_proto_parser.h"
+#include "driver-spondoolies.h"
+
+extern int opt_g_threads;
+struct device_drv spondoolies_drv;
+
+
+#ifdef WORDS_BIGENDIAN
+#  define swap32tobe(out, in, sz)  ((out == in) ? (void)0 : memmove(out, in, sz))
+#  define LOCAL_swap32be(type, var, sz)  ;
+#  define swap32tole(out, in, sz)  swap32yes(out, in, sz)
+#  define LOCAL_swap32le(type, var, sz)  LOCAL_swap32(type, var, sz)
+#else
+#  define swap32tobe(out, in, sz)  swap32yes(out, in, sz)
+#  define LOCAL_swap32be(type, var, sz)  LOCAL_swap32(type, var, sz)
+#  define swap32tole(out, in, sz)  ((out == in) ? (void)0 : memmove(out, in, sz))
+#  define LOCAL_swap32le(type, var, sz)  ;
+#endif
+static inline void swap32yes(void*out, const void*in, size_t sz) {
+  size_t swapcounter = 0;
+  for (swapcounter = 0; swapcounter < sz; ++swapcounter)
+    (((uint32_t*)out)[swapcounter]) = swab32(((uint32_t*)in)[swapcounter]);
+}
+
+
+static void print_stats(struct spond_adapter *a) {
+  printf("%d wins %d good %d empty %d bad %d overflow %d\n", time(NULL),
+    a->wins,
+    a->good,
+    a->empty,
+    a->bad,
+    a->overflow);
+}
+
+
+void send_minergate_pkt(const minergate_req_packet* mp_req, 
+            minergate_rsp_packet* mp_rsp,
+            int  socket_fd) {
+  int   nbytes;    
+  write(socket_fd, (const void*)mp_req, sizeof(minergate_req_packet));
+  nbytes = read(socket_fd, (void*)mp_rsp, sizeof(minergate_rsp_packet));  
+  if(nbytes <= 0) {
+    _quit(-1);
+  }
+  passert(mp_rsp->magic == 0xcaf4);
+}
+
+
+
+static bool spondoolies_prepare(struct thr_info *thr)
+{
+  struct cgpu_info *spondoolies = thr->cgpu;
+  assert(spondoolies);
+  struct timeval now;
+
+  cgtime(&now);
+/* FIXME: Vladik */
+#if NEED_FIX
+  get_datestamp(spondoolies->init, &now);
+#endif
+  return true;
+}
+
+
+ 
+int init_socket() {
+  struct sockaddr_un address;
+    int socket_fd;
+  socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
+   if(socket_fd < 0)
+   {
+    printf("socket() failed\n");
+    perror("Err:");
+    return 0;
+   }
+   
+   /* start with a clean address structure */
+   memset(&address, 0, sizeof(struct sockaddr_un));
+   
+   address.sun_family = AF_UNIX;
+   sprintf(address.sun_path, MINERGATE_SOCKET_FILE);
+   
+   if(connect(socket_fd, 
+        (struct sockaddr *) &address, 
+        sizeof(struct sockaddr_un)) != 0)
+   {
+    printf("connect() failed\n");
+    perror("Err:");
+    return 0;
+   }
+
+  return socket_fd;
+}
+
+
+static bool spondoolies_flush_queue(struct cgpu_info *cgpu, 
+                                    struct spond_adapter* a,
+                                    bool flush_queue)
+{
+    if (!a->parse_resp) {
+      static int i = 0;
+      if (i++%10 == 0)
+        if (a->works_in_minergate_and_pending_tx + a->works_pending_tx != a->works_in_driver) {
+          printf("%d + %d != %d\n",a->works_in_minergate_and_pending_tx,a->works_pending_tx,a->works_in_driver);
+        }
+       assert(a->works_in_minergate_and_pending_tx + a->works_pending_tx == a->works_in_driver);   
+       send_minergate_pkt(a->mp_next_req,  a->mp_last_rsp, a->socket_fd);
+       if (flush_queue) {
+         a->mp_next_req->mask |= 0x02;     
+       } else {
+         a->mp_next_req->mask &= ~0x02;
+       }
+       
+       a->mp_next_req->req_count = 0;
+       a->parse_resp = 1;
+       a->works_in_minergate_and_pending_tx += a->works_pending_tx;
+       a->works_pending_tx = 0;
+    }
+    return true;
+}
+
+
+static void spondoolies_detect(__maybe_unused bool hotplug)
+{
+  int success;
+  struct spond_adapter *a;
+  struct device_drv *drv = &spondoolies_drv;
+  
+#if NEED_FIX
+  nDevs = 1;
+#endif
+
+  struct cgpu_info *cgpu = calloc(1, sizeof(*cgpu));
+  assert(cgpu);
+  cgpu->drv = drv;
+  cgpu->deven = DEV_ENABLED;
+  cgpu->threads = 1;
+  cgpu->device_data = calloc(sizeof(struct spond_adapter), 1);
+  memset(cgpu->device_data, 0, sizeof(struct spond_adapter));
+  if (unlikely(!(cgpu->device_data)))
+    quit(1, "Failed to calloc cgpu_info data");
+  a = cgpu->device_data;
+  a->cgpu = (void*)cgpu;
+  a->adapter_state = ADAPTER_STATE_OPERATIONAL;
+    a->mp_next_req = allocate_minergate_packet_req(
+        0xca, 0xfe);
+    a->mp_last_rsp = allocate_minergate_packet_rsp(
+        0xca, 0xfe);
+     
+  pthread_mutex_init(&a->lock, NULL);
+  a->socket_fd = init_socket();
+  if (a->socket_fd < 1) {
+    printf("Error connecting to minergate server!");
+    _quit(-1);
+  }
+
+  assert(add_cgpu(cgpu));
+  // Clean MG socket
+  spondoolies_flush_queue(cgpu, a, true);
+  spondoolies_flush_queue(cgpu, a, true);
+  spondoolies_flush_queue(cgpu, a, true);
+  applog(LOG_DEBUG, "SPOND spondoolies_detect done");
+}
+
+
+static struct api_data *spondoolies_api_stats(struct cgpu_info *cgpu)
+{
+  struct api_data *root = NULL;
+  applog(LOG_DEBUG, "SPOND spondoolies_api_stats");
+  applog(LOG_DEBUG, "SPOND spondoolies_api_stats done");
+  return root;
+}
+
+
+unsigned char get_leading_zeroes(const unsigned char *target) {
+  unsigned char leading = 0;
+  int first_non_zero_chr;
+  for (first_non_zero_chr = 31; first_non_zero_chr >= 0; first_non_zero_chr--) {
+    if (target[first_non_zero_chr] == 0) {
+      leading += 8;
+    } else { 
+      break;
+    }
+  }
+
+  // j = first non-zero
+  uint8_t m = target[first_non_zero_chr];
+  while ((m & 0x80) == 0) {
+    leading++;
+    m = m << 1;
+  }
+  return leading;
+}
+
+
+static void spondoolies_shutdown(__maybe_unused struct thr_info *thr)
+{
+}
+
+void fill_minergate_request(struct spond_adapter* a,
+                            minergate_do_job_req* work, 
+                            struct work *cg_work, 
+                            int ntime_offset) {
+  static int id = 1;
+  id++;
+  memset(work, 0, sizeof(minergate_do_job_req));
+  //work->
+  LOCAL_swap32le(unsigned char, cg_work->midstate, 32/4)
+  LOCAL_swap32le(unsigned char, cg_work->data+64, 64/4)
+  uint32_t x[64/4];
+  swap32yes(x, cg_work->data + 64, 64/4);
+  memcpy(work->midstate, cg_work->midstate, 32);
+  work->mrkle_root = ntohl(x[0]);
+  work->timestamp  = ntohl(x[1]);
+  work->difficulty = ntohl(x[2]);
+  //work->leading_zeroes = get_leading_zeroes(cg_work->target);
+  // Is there no better way to get leading zeroes?
+  work->leading_zeroes = 30;
+  int wd = (int)round(cg_work->work_difficulty);
+  while (wd) {
+    work->leading_zeroes++;
+    wd=wd>>1;
+  }
+  //printf("%d %d\n",work->leading_zeroes, (int)round(cg_work->work_difficulty));
+  work->work_id_in_sw = cg_work->subid;
+  work->ntime_limit = 0;
+  work->ntime_offset = ntime_offset;
+}
+
+
+
+// returns true if queue full.
+struct timeval last_force_queue = {0};
+static bool spondoolies_queue_full(struct cgpu_info *cgpu)
+{
+  // Only once every 1/10 second do work.
+  struct spond_adapter* a = cgpu->device_data;
+  int ret = false;
+  bool queue_full = false; // queue not full
+  mutex_lock(&a->lock);
+
+  passert(a->works_pending_tx <= REQUEST_SIZE);
+
+  struct timeval tv;
+  gettimeofday(&tv, NULL);
+  unsigned int usec;
+      
+  usec=(tv.tv_sec-last_force_queue.tv_sec)*1000000;
+  usec+=(tv.tv_usec-last_force_queue.tv_usec);
+
+  if ((usec >= REQUEST_PERIOD) || 
+      (a->reset_mg_queue == 2) ||
+      ((a->reset_mg_queue == 1) && (a->works_pending_tx == REQUEST_SIZE))) {
+    static int i =0; 
+    spondoolies_flush_queue(cgpu, a, (a->reset_mg_queue == 2));
+    if (a->reset_mg_queue) {
+      a->reset_mg_queue--;
+    }
+    last_force_queue = tv;
+  }
+
+  // see if we have enough jobs
+  if (a->works_pending_tx == REQUEST_SIZE) {
+    cgsleep_ms(40);
+    ret = true;
+    goto return_unlock;
+  }
+
+  // see if can take 1 more job.
+  int next_job_id =  (a->current_job_id+1)%MAX_JOBS_IN_MINERGATE;
+  if (a->my_jobs[next_job_id].cgminer_work) {
+    cgsleep_ms(40);
+    ret = true;
+    goto return_unlock;
+  }
+  struct work *work;
+  work = get_queued(cgpu);
+  if (!work) {
+     ret = true;
+     goto return_unlock;
+  }
+  
+  work->thr = cgpu->thr[0];  
+  work->thr_id = cgpu->thr[0]->id;
+  assert(work->thr);
+  int i;
+
+  // Create 5 works using ntime increment
+  a->current_job_id = next_job_id;
+  work->subid = a->current_job_id;
+  // Get pointer for the request
+  a->my_jobs[a->current_job_id].cgminer_work = work;
+  a->my_jobs[a->current_job_id].state = SPONDWORK_STATE_IN_BUSY;
+  a->my_jobs[a->current_job_id].ntime_clones = 0;
+  
+  int ntime_clones = (work->drv_rolllimit < MAX_NROLES)?work->drv_rolllimit:MAX_NROLES;
+  for (i = 0 ; (i < ntime_clones) && (a->works_pending_tx < REQUEST_SIZE) ; i++) {
+    minergate_do_job_req* pkt_job =  &a->mp_next_req->req[a->works_pending_tx];    
+    fill_minergate_request(a, pkt_job, work, i);
+    a->works_in_driver++;
+    a->works_pending_tx++;        
+    a->mp_next_req->req_count++;    
+    a->my_jobs[a->current_job_id].merkel_root = pkt_job->mrkle_root;  
+    a->my_jobs[a->current_job_id].ntime_clones++;
+  }
+
+ return_unlock:
+  mutex_unlock(&a->lock);
+  return ret;
+   
+}
+
+
+
+// Return completed work to submit_nonce() and work_completed() 
+// struct timeval last_force_queue = {0};  
+static int64_t spond_scanhash(struct thr_info *thr)
+{
+    int64_t ghashes=0;
+    struct cgpu_info *cgpu = thr->cgpu;
+    struct spond_adapter *a = cgpu->device_data;
+    if(a->parse_resp) {
+      mutex_lock(&a->lock);
+      ghashes = (a->mp_last_rsp->gh_div_10_rate);
+      ghashes=ghashes*10000*REQUEST_PERIOD;
+      int array_size = a->mp_last_rsp->rsp_count;
+      int i;
+      for (i = 0; i < array_size; i++) { // walk the jobs
+        minergate_do_job_rsp* work = a->mp_last_rsp->rsp + i;
+        int job_id =  work->work_id_in_sw;
+        if ((a->my_jobs[job_id].cgminer_work)) {
+          if (a->my_jobs[job_id].merkel_root == work->mrkle_root) {
+            assert(a->my_jobs[job_id].state == SPONDWORK_STATE_IN_BUSY);
+            a->works_in_minergate_and_pending_tx--;
+            a->works_in_driver--;
+            if (work->winner_nonce) {
+              struct work *cg_work = a->my_jobs[job_id].cgminer_work;
+#ifndef SP_NTIME             
+              bool r = submit_nonce(cg_work->thr, cg_work, work->winner_nonce);
+#else
+              bool r = submit_noffset_nonce(cg_work->thr, cg_work, work->winner_nonce, work->ntime_offset);
+#endif
+              a->wins++;
+            }
+            //printf("%d ntime_clones = %d\n",job_id,a->my_jobs[job_id].ntime_clones);
+            if ((--a->my_jobs[job_id].ntime_clones) == 0) {
+              //printf("Done with %d\n", job_id);
+              work_completed(a->cgpu, a->my_jobs[job_id].cgminer_work);
+              a->good++;              
+              a->my_jobs[job_id].cgminer_work = NULL;
+              a->my_jobs[job_id].state = SPONDWORK_STATE_EMPTY;  
+            }
+          } else {
+            a->bad++;
+            printf("Dropping minergate old job id=%d mrkl=%x my-mrkl=%x\n",job_id,
+              a->my_jobs[job_id].merkel_root, 
+              work->mrkle_root);  
+          }
+        } else {
+          a->empty++;
+          printf("No cgminer job (id:%d res:%d)!\n",job_id, work->res);
+        }
+      }
+    mutex_unlock(&a->lock);
+    a->parse_resp = 0;
+  }
+  return ghashes;
+}
+
+// Remove all work from queue
+static void spond_flush_work(struct cgpu_info *cgpu)
+{
+  struct spond_adapter *a = cgpu->device_data;
+  int i;
+  mutex_lock(&a->lock);
+  a->reset_mg_queue = 2;
+  mutex_unlock(&a->lock);
+}
+
+
+
+struct device_drv spondoolies_drv = {
+  .drv_id = DRIVER_spondoolies,
+  .dname = "Spondoolies",
+  .name = "SPN",
+  .drv_detect = spondoolies_detect,
+  .get_api_stats = spondoolies_api_stats,
+  .thread_prepare = spondoolies_prepare,
+  .thread_shutdown = spondoolies_shutdown,  
+  .hash_work = hash_queued_work,  
+  .queue_full = spondoolies_queue_full,  
+  .scanwork = spond_scanhash,
+  .flush_work = spond_flush_work,
+};
+
+
diff --git a/driver-spondoolies.h b/driver-spondoolies.h
new file mode 100644
index 0000000..7bd2357
--- /dev/null
+++ b/driver-spondoolies.h
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2014 Zvi Shteingart - Spondoolies-tech.com
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.  See COPYING for more details.
+ */
+
+#ifndef SPONDA_HFILE
+#define SPONDA_HFILE
+
+
+#include "miner.h"
+#include "mg_proto_parser.h"
+
+#define SP_NTIME
+
+typedef enum adapter_state {
+  ADAPTER_STATE_INIT,
+  ADAPTER_STATE_OPERATIONAL,
+} ADAPTER_STATE; 
+
+
+
+typedef enum spond_work_state {
+  SPONDWORK_STATE_EMPTY,
+  SPONDWORK_STATE_IN_BUSY,
+} SPONDWORK_STATE; 
+
+#define MAX_JOBS_IN_MINERGATE MINERGATE_TOTAL_QUEUE // 1.5 sec worth of jobs
+#define MAX_NROLES 50 
+typedef struct {
+    struct work      *cgminer_work;
+    SPONDWORK_STATE  state;
+    uint32_t         merkel_root;
+    time_t           start_time;
+    int              job_id[MAX_NROLES];
+    int              ntime_clones;
+} spond_driver_work;
+
+struct spond_adapter {
+  pthread_mutex_t lock;
+  ADAPTER_STATE adapter_state;
+  void* cgpu;
+  
+  // Statistics
+  int wins;
+  int good;
+  int empty;
+  int bad;
+  int overflow;
+  // state 
+  int works_in_driver;
+  int works_in_minergate_and_pending_tx;
+  int works_pending_tx;
+  int socket_fd;
+  int reset_mg_queue;  // 2=reset, 1=fast send, 0=nada
+  int current_job_id; 
+  int parse_resp;
+  minergate_req_packet* mp_next_req;
+  minergate_rsp_packet* mp_last_rsp;
+  spond_driver_work my_jobs[MAX_JOBS_IN_MINERGATE]; 
+};
+
+
+// returns non-zero if needs to change ASICs.
+int spond_one_sec_timer_scaling(struct spond_adapter *a, int t);
+int spond_do_scaling(struct spond_adapter *a);
+
+extern void one_sec_spondoolies_watchdog(int uptime);
+
+#define REQUEST_PERIOD (100000)  //  times per second - in usec
+#define REQUEST_SIZE   100      //  jobs per request
+
+
+
+#endif
+
diff --git a/mg_proto_parser.c b/mg_proto_parser.c
new file mode 100644
index 0000000..bdfbc39
--- /dev/null
+++ b/mg_proto_parser.c
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2014 Zvi (Zvisha) Shteingart - Spondoolies-tech.com
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.  See COPYING for more details.
+ *
+ * Note that changing this SW will void your miners guaranty
+ */
+
+/*
+	This file holds functions needed for minergate packet parsing/creation
+	by Zvisha Shteingart
+*/
+
+#include "mg_proto_parser.h"
+#include "assert.h"
+//#include "spond_debug.h"
+
+
+
+
+minergate_req_packet *allocate_minergate_packet_req(
+											uint8_t requester_id, 
+											uint8_t request_id) {
+	minergate_req_packet *p  = (minergate_req_packet*)malloc(sizeof(minergate_req_packet));
+	p->requester_id = requester_id;
+	p->req_count = 0;
+	p->protocol_version = MINERGATE_PROTOCOL_VERSION;
+	p->request_id = request_id;
+	p->magic = 0xcaf4;
+	p->mask |= 0x01; // first packet
+	return p;
+}
+
+
+minergate_rsp_packet *allocate_minergate_packet_rsp(
+											uint8_t requester_id, 
+											uint8_t request_id) {
+	
+	minergate_rsp_packet *p  = (minergate_rsp_packet*)malloc(sizeof(minergate_rsp_packet));
+	p->requester_id = requester_id;
+	p->rsp_count = 0;
+	p->protocol_version = MINERGATE_PROTOCOL_VERSION;
+	p->request_id = request_id;
+	p->magic = 0xcaf4;
+	p->gh_div_10_rate = 0;
+	return p;
+}
+
+
+
diff --git a/mg_proto_parser.h b/mg_proto_parser.h
new file mode 100644
index 0000000..42f3af4
--- /dev/null
+++ b/mg_proto_parser.h
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2014 Zvi (Zvisha) Shteingart - Spondoolies-tech.com
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.  See COPYING for more details.
+ *
+ * Note that changing this SW will void your miners guaranty
+ */
+
+#ifndef ____MINERGATE_LIB_H___
+#define ____MINERGATE_LIB_H___
+
+//#include "squid.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <getopt.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <linux/types.h>
+#include <linux/spi/spidev.h>
+#include <netinet/in.h>
+//#include "queue.h"
+//#include "spond_debug.h"
+
+#ifndef passert
+#define passert assert
+#endif
+
+
+
+#define MINERGATE_PROTOCOL_VERSION 5
+#define MINERGATE_SOCKET_FILE "/tmp/connection_pipe"
+
+
+typedef enum {
+	//MINERGATE_DATA_ID_CONNECT = 1,
+	MINERGATE_DATA_ID_DO_JOB_REQ = 2,
+	MINERGATE_DATA_ID_DO_JOB_RSP = 3, 
+
+} MINERGATE_DATA_ID;
+
+
+typedef struct {
+	uint32_t work_id_in_sw;
+	uint32_t difficulty;
+	uint32_t timestamp;
+	uint32_t mrkle_root;
+	uint32_t midstate[8];
+  uint8_t leading_zeroes;
+  uint8_t ntime_limit;
+  uint8_t ntime_offset;
+  uint8_t resr1; 
+} minergate_do_job_req;
+
+#define MAX_REQUESTS 100
+#define MAX_RESPONDS 300
+#define MINERGATE_TOTAL_QUEUE 300
+
+
+typedef struct {
+	uint32_t work_id_in_sw;
+	uint32_t mrkle_root;     // to validate
+	uint32_t winner_nonce;
+  uint8_t  ntime_offset;
+  uint8_t  res;            // 0 = done, 1 = overflow, 2 = dropped bist
+  uint8_t  resrv1;            
+  uint8_t  resrv2;            
+} minergate_do_job_rsp;
+
+
+typedef struct {
+	uint8_t requester_id;
+	uint8_t request_id;
+	uint8_t protocol_version;
+	uint8_t mask; // 0x01 = first request, 0x2 = drop old work
+	uint16_t magic; // 0xcafe
+	uint16_t req_count;
+	minergate_do_job_req req[MAX_REQUESTS]; // array of requests
+} minergate_req_packet;
+
+typedef struct {
+	uint8_t requester_id;
+	uint8_t request_id;
+	uint8_t protocol_version;
+	uint8_t gh_div_10_rate; // == 
+	uint16_t magic; // 0xcafe
+	uint16_t rsp_count;
+	minergate_do_job_rsp rsp[MAX_RESPONDS]; // array of responce
+} minergate_rsp_packet;
+
+
+minergate_req_packet *allocate_minergate_packet_req(
+											uint8_t requester_id, 
+											uint8_t request_id) ;
+
+
+
+minergate_rsp_packet *allocate_minergate_packet_rsp(
+											uint8_t requester_id, 
+											uint8_t request_id);
+
+
+#endif
diff --git a/miner.h b/miner.h
index b687ce3..276e765 100644
--- a/miner.h
+++ b/miner.h
@@ -248,7 +248,8 @@ static inline int fsync (int fd)
 	DRIVER_ADD_COMMAND(minion) \
 	DRIVER_ADD_COMMAND(ants1) \
 	DRIVER_ADD_COMMAND(avalon2) \
-	DRIVER_ADD_COMMAND(avalon)
+	DRIVER_ADD_COMMAND(avalon) \
+	DRIVER_ADD_COMMAND(spondoolies)
 
 #define DRIVER_PARSE_COMMANDS(DRIVER_ADD_COMMAND) \
 	FPGA_PARSE_COMMANDS(DRIVER_ADD_COMMAND) \