Browse Source

Merge branch 'master' into myfork

Luke Dashjr 14 years ago
parent
commit
b831eaa2b9
38 changed files with 4403 additions and 1637 deletions
  1. 1 1
      AUTHORS
  2. 10 3
      Makefile.am
  3. 126 0
      NEWS
  4. 66 27
      README
  5. 71 23
      adl.c
  6. 2 0
      adl.h
  7. 671 37
      api.c
  8. 24 0
      bitstreams/LICENSE.txt
  9. BIN
      bitstreams/ztex_ufm1_15b1.bit
  10. BIN
      bitstreams/ztex_ufm1_15d1.bit
  11. BIN
      bitstreams/ztex_ufm1_15d3.bit
  12. 226 62
      cgminer.c
  13. 37 10
      configure.ac
  14. 10 1
      diablo120328.cl
  15. 7 0
      driver-bitforce.c
  16. 1 1
      driver-cpu.c
  17. 80 29
      driver-icarus.c
  18. 41 18
      driver-opencl.c
  19. 322 0
      driver-ztex.c
  20. 477 0
      libztex.c
  21. 95 0
      libztex.h
  22. 51 2
      miner.h
  23. 463 92
      miner.php
  24. 26 23
      mkinstalldirs
  25. 1 0
      ocl.h
  26. 0 1288
      poclbm120222.cl
  27. 1353 0
      poclbm120327.cl
  28. 0 2
      sha256_4way.c
  29. 0 2
      sha256_altivec_4way.c
  30. 0 2
      sha256_cryptopp.c
  31. 0 2
      sha256_generic.c
  32. 0 2
      sha256_sse2_amd64.c
  33. 0 2
      sha256_sse2_i386.c
  34. 0 2
      sha256_sse4_amd64.c
  35. 0 2
      sha256_via.c
  36. 5 0
      todo_ztex.txt
  37. 13 4
      util.c
  38. 224 0
      windows-build.txt

+ 1 - 1
AUTHORS

@@ -1,4 +1,4 @@
 Original CPU mining software: Jeff Garzik <jgarzik@pobox.com>
 Original CPU mining software: Jeff Garzik <jgarzik@pobox.com>
 GPU mining and rewrite: Con Kolivas <kernel@kolivas.org> 15qSxP1SQcUX3o4nhkfdbgyoWEFMomJ4rZ
 GPU mining and rewrite: Con Kolivas <kernel@kolivas.org> 15qSxP1SQcUX3o4nhkfdbgyoWEFMomJ4rZ
 BitFORCE FPGA mining and refactor: Luke Dashjr <luke-jr+cgminer@utopios.org> 1NbRmS6a4dniwHHoSS9v3tEYUpP1Z5VVdL
 BitFORCE FPGA mining and refactor: Luke Dashjr <luke-jr+cgminer@utopios.org> 1NbRmS6a4dniwHHoSS9v3tEYUpP1Z5VVdL
-API+: Andrew Smith <kanoi@kano-kun.net> 1Jjk2LmktEQKnv8r2cZ9MvLiZwZ9gxabKm
+API+: Andrew Smith <kanoi2@kano-kun.net> 1Jjk2LmktEQKnv8r2cZ9MvLiZwZ9gxabKm

+ 10 - 3
Makefile.am

@@ -9,11 +9,12 @@ endif
 
 
 EXTRA_DIST	= example.conf m4/gnulib-cache.m4 linux-usb-cgminer \
 EXTRA_DIST	= example.conf m4/gnulib-cache.m4 linux-usb-cgminer \
 		  ADL_SDK/readme.txt api-example.php miner.php	\
 		  ADL_SDK/readme.txt api-example.php miner.php	\
-		  API.class API.java api-example.c
+		  API.class API.java api-example.c windows-build.txt \
+		  bitstreams/*
 
 
 SUBDIRS		= lib compat ccan
 SUBDIRS		= lib compat ccan
 
 
-INCLUDES	= $(PTHREAD_FLAGS) -fno-strict-aliasing $(JANSSON_INCLUDES)
+INCLUDES	= $(PTHREAD_FLAGS) -fno-strict-aliasing $(JANSSON_INCLUDES) $(USB_FLAGS)
 
 
 bin_PROGRAMS	= cgminer
 bin_PROGRAMS	= cgminer
 
 
@@ -22,7 +23,7 @@ bin_SCRIPTS	= *.cl
 cgminer_LDFLAGS	= $(PTHREAD_FLAGS)
 cgminer_LDFLAGS	= $(PTHREAD_FLAGS)
 cgminer_LDADD	= $(DLOPEN_FLAGS) @LIBCURL_LIBS@ @JANSSON_LIBS@ @PTHREAD_LIBS@ \
 cgminer_LDADD	= $(DLOPEN_FLAGS) @LIBCURL_LIBS@ @JANSSON_LIBS@ @PTHREAD_LIBS@ \
 		  @OPENCL_LIBS@ @NCURSES_LIBS@ @PDCURSES_LIBS@ @WS2_LIBS@ \
 		  @OPENCL_LIBS@ @NCURSES_LIBS@ @PDCURSES_LIBS@ @WS2_LIBS@ \
-		  @UDEV_LIBS@ \
+		  @UDEV_LIBS@ @USB_LIBS@ \
 		  @MATH_LIBS@ lib/libgnu.a ccan/libccan.a
 		  @MATH_LIBS@ lib/libgnu.a ccan/libccan.a
 cgminer_CPPFLAGS = -I$(top_builddir)/lib -I$(top_srcdir)/lib @OPENCL_FLAGS@
 cgminer_CPPFLAGS = -I$(top_builddir)/lib -I$(top_srcdir)/lib @OPENCL_FLAGS@
 
 
@@ -74,3 +75,9 @@ endif
 if HAS_ICARUS
 if HAS_ICARUS
 cgminer_SOURCES += driver-icarus.c
 cgminer_SOURCES += driver-icarus.c
 endif
 endif
+
+if HAS_ZTEX
+cgminer_SOURCES += driver-ztex.c libztex.c libztex.h
+bitstreamsdir = $(bindir)/bitstreams
+dist_bitstreams_DATA = bitstreams/*
+endif

+ 126 - 0
NEWS

@@ -1,3 +1,129 @@
+Version 2.3.3 - April 15, 2012
+
+- Don't even display that cpumining is disabled on ./configure to discourage
+people from enabling it.
+- Do a complete cgminer restart if the ATI Display Library fails, as it does on
+windows after running for some time, when fanspeed reporting fails.
+- Cache the initial arguments passed to cgminer and implement an attempted
+restart option from the settings menu.
+- Disable per-device status lines when there are more than 8 devices since
+screen output will be corrupted, enumerating them to the log output instead at
+startup.
+- Reuse Vals[] array more than W[] till they're re-initialised on the second
+sha256 cycle in poclbm kernel.
+- Minor variable alignment in poclbm kernel.
+- Make sure to disable devices with any status not being DEV_ENABLED to ensure
+that thermal cutoff code works as it was setting the status to DEV_RECOVER.
+- Re-initialising ADL simply made the driver fail since it is corruption over
+time within the windows driver that's responsible. Revert "Attempt to
+re-initialise ADL should a device that previously reported fanspeed stops
+reporting it."
+- Microoptimise poclbm kernel by ordering Val variables according to usage
+frequency.
+
+
+Version 2.3.2 - March 31, 2012
+
+- Damping small changes in hashrate so dramatically has the tendency to always
+make the hashrate underread so go back to gentle damping instead.
+- Revert the crossover of variables from Vals to W in poclbm kernel now that
+Vals are the first declared variables so they're used more frequently.
+- Vals variables appearing first in the array in poclbm is faster.
+- Change the preferred vector width to 1 for Tahiti only, not all poclbm
+kernels.
+- Use a time constant 0.63 for when large changes in hashrate are detected to
+damp change in case the large change is an aliasing artefact instead of a real
+chang
+- Only increment stale counter if the detected stales are discarded.
+- Attempt to re-initialise ADL should a device that previously reported fanspeed
+stops reporting it.
+- Move the ADL setup and clearing to separate functions and provide a reinit_adl
+function to be used when adl fails while running.
+- Use slightly more damping on the decay time function in the never-ending quest
+to smooth off the hashmeter.
+- Set the starting fanspeed to a safe and fairly neutral 50% when autofan is
+enabled.
+- Provide locking around updates of cgpu hashrates as well to prevent multiple
+threads accessing data fields on the same device.
+- Display the beginning of the new block in verbose mode in the logs.
+- Reinstate old diablo kernel variable ordering from 120222, adding only goffset
+and vector size hint. The massive variable ordering change only helped one SDK
+on
+- Change the version number on the correct kernels.
+- api.c devicecode/osinfo incorrectly swapped for json
+- Add extensive instructions on how to make a native windows build.
+- Update version numbers of poclbm and diablo kernels as their APIs have also
+changed.
+- Use global offset parameter to diablo and poclbm kernel ONLY for 1 vector
+kernels.
+- Use poclbm preferentially on Tahiti now regardless of SDK.
+- Remove unused constant passed to poclbm.
+- Clean up use of macros in poclbm and use bitselect everywhere possible.
+- Add vector type hint to diablo kernel.
+- Add worksize and vector attribute hints to the poclbm kernel.
+- Spaces for non-aligned variables in poclbm.
+- More tidying of poclbm.
+- Swap Vals and W variables where they can overlap in poclbm.
+- More tidying of poclbm.
+- Tidy up first half of poclbm.
+- Clean up use of any() by diablo and poclbm kernels.
+- Minor variable symmetry changes in poclbm.
+- Put additions on separate lines for consistency in poclbm.
+- Consolidate last use of W11 into Vals4 in poclbm.
+- Change email due to SPAM
+- api.c miner.php add a '*' to the front of all notify counters - simplifies
+future support of new counters
+- miner.php add display 'notify' command
+- Small change to help arch's without processor affinity
+- Fix bitforce compile error
+- api.c notify should report disabled devices also - of course
+- API returns the simple device history with the 'notify' command
+- code changes for supporting a simple device history
+- api.c Report an OS string in config to help with device issues
+- api.c fix Log Interval - integer in JSON
+- api.c config 'Device Code' to show list of compiled devices + README
+- api.c increase buffer size close to current code allowable limit
+- removed 8-component vector support from kernel, as this is not supported in
+CGMINER anyway
+- forgot to update kernel modification date, fixed ;)
+- reordered an addition in the kernel, which results in less instructions used
+in the GPU ISA code for GCN
+- miner.php: option for readonly or check privileged access
+- Ignore reduntant-with-build options --disable-gpu, --no-adl, and --no-restart
+- miner.php: ereg_replace is DEPRECATED so use preg_replace instead
+- Make curses TUI support optional at compile-time.
+- Bugfix: AC_ARG_WITH provides withval instead of enableval
+- miner.php split devs output for different devices
+- api.c: correct error messages
+- icarus.c modify (regular) timeout warning to only be debug
+- icarus.c set the windows TODO timeout
+- Allow specifying a specific driver for --scan-serial
+- optimized nonce-check and output code for -v 2 and -v 4
+- Bugfix: Check for libudev header (not just library) in configure, and document
+optional dependency
+- Add API support for Icarus and Bitforce
+- Next API version is 1.4 (1.3 is current)
+- README/api.c add "When" the request was processed to STATUS
+- Bugfix: ZLX to read BitFORCE temp, not ZKX -.-
+- Use libudev to autodetect BitFORCE GPUs, if available
+- Use the return value of fan_autotune to set fan_optimal instead of passing it
+as a pointer.
+- Pass the lasttemp from the device we're using to adjust fanspeed in twin
+devices.
+- fix the name to 3 chars, fix the multi-icarus support
+- Bugfix: "-S auto" is the default if no -S is specified, and there is no such
+delay in using it
+- README add information missing from --scan-serial
+- Update README RPC API Version comment
+- Bugfix: Allow enabling CPU even without OpenCL support
+- Change failed-to-mine number of requested shares messge to avoid segfault on
+recursive calling of quit().
+- Get rid of extra char which is just truncated in poclbm kernel.
+- only small code formating changes
+- removed vec_step() as this could lead to errors on older SDKs
+- unified code for generating nonce in kernel and moved addition of base to the
+end -> faster
+
 Version 2.3.1 - February 24, 2012
 Version 2.3.1 - February 24, 2012
 
 
 - Revert input and output code on diakgcn and phatk kernels to old style which
 - Revert input and output code on diakgcn and phatk kernels to old style which

+ 66 - 27
README

@@ -103,7 +103,7 @@ Basic WIN32 build instructions (LIKELY OUTDATED INFO. requires mingw32):
 	make
 	make
 	./mknsis.sh
 	./mknsis.sh
 	
 	
-Native WIN32 build instructions (on mingw32, on windows):
+Native WIN32 build instructions (outdated, see windows-build.txt)
 	Install the Microsoft platform SDK
 	Install the Microsoft platform SDK
 	Install AMD APP sdk, (if you want GPU mining)
 	Install AMD APP sdk, (if you want GPU mining)
 	Install AMD ADL sdk, (if you want GPU monitoring)
 	Install AMD ADL sdk, (if you want GPU monitoring)
@@ -205,13 +205,14 @@ FPGA mining boards(BitForce, Icarus) only options:
 --scan-serial|-S <arg> Serial port to probe for FPGA mining device
 --scan-serial|-S <arg> Serial port to probe for FPGA mining device
 
 
      By default, cgminer will scan for autodetected FPGAs unless at least one
      By default, cgminer will scan for autodetected FPGAs unless at least one
-     -S is specified. If you specify -S and still want cgminer to scan, you
-     must also use "-S auto". Note that presently, autodetection only works
-     on Linux, and might only detect one device depending on the version of
-     udev being used.
+     -S is specified for that driver. If you specify -S and still want cgminer
+     to scan, you must also use "-S auto". If you want to prevent cgminer from
+     scanning without specifying a device, you can use "-S noauto". Note that
+     presently, autodetection only works on Linux, and might only detect one
+     device depending on the version of udev being used.
 
 
      On linux <arg> is usually of the format /dev/ttyUSBn
      On linux <arg> is usually of the format /dev/ttyUSBn
-     On windows <arg> is usually of the format COMn
+     On windows <arg> is usually of the format \\.\COMn
        (where n = the correct device number for the FPGA device)
        (where n = the correct device number for the FPGA device)
 
 
 
 
@@ -596,7 +597,16 @@ An example request in both formats to set GPU 0 fan to 80%:
 The format of each reply (unless stated otherwise) is a STATUS section
 The format of each reply (unless stated otherwise) is a STATUS section
 followed by an optional detail section
 followed by an optional detail section
 
 
-For API version 1.4:
+From API verion 1.7 onwards, reply strings in JSON and Text have the
+necessary escaping as required to avoid ambiguity - they didn't before 1.7
+For JSON the 2 characters '"' and '\' are escaped with a '\' before them
+For Text the 4 characters '|' ',' '=' and '\' are escaped the same way
+
+Only user entered information will contain characters that require being
+escaped, such as Pool URL, User and Password or the Config save filename,
+when they are returned in messages or as their values by the API
+
+For API version 1.4 and later:
 
 
 The STATUS section is:
 The STATUS section is:
 
 
@@ -622,7 +632,7 @@ The STATUS section is:
    This defaults to the cgminer version but is the value of --api-description
    This defaults to the cgminer version but is the value of --api-description
    if it was specified at runtime.
    if it was specified at runtime.
 
 
-For API version 1.4:
+For API version 1.7:
 
 
 The list of requests - a (*) means it requires privileged access - and replies are:
 The list of requests - a (*) means it requires privileged access - and replies are:
 
 
@@ -633,12 +643,14 @@ The list of requests - a (*) means it requires privileged access - and replies a
 
 
  config        CONFIG         Some miner configuration information:
  config        CONFIG         Some miner configuration information:
                               GPU Count=N, <- the number of GPUs
                               GPU Count=N, <- the number of GPUs
+                              PGA Count=N, <- the number of PGAs
                               CPU Count=N, <- the number of CPUs
                               CPU Count=N, <- the number of CPUs
                               Pool Count=N, <- the number of Pools
                               Pool Count=N, <- the number of Pools
                               ADL=X, <- Y or N if ADL is compiled in the code
                               ADL=X, <- Y or N if ADL is compiled in the code
                               ADL in use=X, <- Y or N if any GPU has ADL
                               ADL in use=X, <- Y or N if any GPU has ADL
                               Strategy=Name, <- the current pool strategy
                               Strategy=Name, <- the current pool strategy
-                              Log Interval=N| <- log interval (--log N)
+                              Log Interval=N, <- log interval (--log N)
+                              Device Code=GPU ICA | <- spaced list of compiled devices
 
 
  summary       SUMMARY        The status summary of the miner
  summary       SUMMARY        The status summary of the miner
                               e.g. Elapsed=NNN,Found Blocks=N,Getworks=N,...|
                               e.g. Elapsed=NNN,Found Blocks=N,Getworks=N,...|
@@ -646,16 +658,22 @@ The list of requests - a (*) means it requires privileged access - and replies a
  pools         POOLS          The status of each pool
  pools         POOLS          The status of each pool
                               e.g. Pool=0,URL=http://pool.com:6311,Status=Alive,...|
                               e.g. Pool=0,URL=http://pool.com:6311,Status=Alive,...|
 
 
- devs          DEVS           Each available CPU and GPU with their details
+ devs          DEVS           Each available GPU, PGA and CPU with their details
                               e.g. GPU=0,Accepted=NN,MHS av=NNN,...,Intensity=D|
                               e.g. GPU=0,Accepted=NN,MHS av=NNN,...,Intensity=D|
                               Last Share Time=NNN, <- standand long time in seconds
                               Last Share Time=NNN, <- standand long time in seconds
                                (or 0 if none) of last accepted share
                                (or 0 if none) of last accepted share
                               Last Share Pool=N, <- pool number (or -1 if none)
                               Last Share Pool=N, <- pool number (or -1 if none)
+                              Will not report PGAs if PGA mining is disabled
                               Will not report CPUs if CPU mining is disabled
                               Will not report CPUs if CPU mining is disabled
 
 
  gpu|N         GPU            The details of a single GPU number N in the same
  gpu|N         GPU            The details of a single GPU number N in the same
                               format and details as for DEVS
                               format and details as for DEVS
 
 
+ pga|N         PGA            The details of a single PGA number N in the same
+                              format and details as for DEVS
+                              This is only available if PGA mining is enabled
+                              Use 'pgacount' or 'config' first to see if there are any
+
  cpu|N         CPU            The details of a single CPU number N in the same
  cpu|N         CPU            The details of a single CPU number N in the same
                               format and details as for DEVS
                               format and details as for DEVS
                               This is only available if CPU mining is enabled
                               This is only available if CPU mining is enabled
@@ -663,6 +681,9 @@ The list of requests - a (*) means it requires privileged access - and replies a
 
 
  gpucount      GPUS           Count=N| <- the number of GPUs
  gpucount      GPUS           Count=N| <- the number of GPUs
 
 
+ pgacount      PGAS           Count=N| <- the number of PGAs
+                              Always returns 0 if PGA mining is disabled
+
  cpucount      CPUS           Count=N| <- the number of CPUs
  cpucount      CPUS           Count=N| <- the number of CPUs
                               Always returns 0 if CPU mining is disabled
                               Always returns 0 if CPU mining is disabled
 
 
@@ -689,6 +710,12 @@ The list of requests - a (*) means it requires privileged access - and replies a
                               stating the results of disabling pool N
                               stating the results of disabling pool N
                               The Msg includes the pool URL
                               The Msg includes the pool URL
 
 
+ removepool|N (*)
+               none           There is no reply section just the STATUS section
+                              stating the results of removing pool N
+                              The Msg includes the pool URL
+                              N.B. all details for the pool will be lost
+
  gpuenable|N (*)
  gpuenable|N (*)
                none           There is no reply section just the STATUS section
                none           There is no reply section just the STATUS section
                               stating the results of the enable request
                               stating the results of the enable request
@@ -714,7 +741,7 @@ The list of requests - a (*) means it requires privileged access - and replies a
                               stating the results of setting GPU N clock to V MHz
                               stating the results of setting GPU N clock to V MHz
 
 
  gpufan|N,V (*)
  gpufan|N,V (*)
-                none           There is no reply section just the STATUS section
+               none           There is no reply section just the STATUS section
                               stating the results of setting GPU N fan speed to V%
                               stating the results of setting GPU N fan speed to V%
 
 
  gpuvddc|N,V (*)
  gpuvddc|N,V (*)
@@ -729,14 +756,28 @@ The list of requests - a (*) means it requires privileged access - and replies a
  quit (*)      none           There is no status section but just a single "BYE|"
  quit (*)      none           There is no status section but just a single "BYE|"
                               reply before cgminer quits
                               reply before cgminer quits
 
 
+ notify        NOTIFY         The last status and history count of each devices problem
+                              e.g. NOTIFY=0,Name=GPU,ID=0,Last Well=1332432290,...|
+
  privileged (*)
  privileged (*)
                none           There is no reply section just the STATUS section
                none           There is no reply section just the STATUS section
                               stating an error if you do not have privileged access
                               stating an error if you do not have privileged access
                               to the API and success if you do have privilege
                               to the API and success if you do have privilege
                               The command doesn't change anything in cgminer
                               The command doesn't change anything in cgminer
 
 
-When you enable, disable or restart a GPU, you will also get Thread messages in
-the cgminer status window
+ pgaenable|N (*)
+               none           There is no reply section just the STATUS section
+                              stating the results of the enable request
+                              You cannot enable a PGA if it's status is not WELL
+                              This is only available if PGA mining is enabled
+
+ pgadisable|N (*)
+               none           There is no reply section just the STATUS section
+                              stating the results of the disable request
+                              This is only available if PGA mining is enabled
+
+When you enable, disable or restart a GPU or PGA, you will also get Thread messages
+in the cgminer status window
 
 
 When you switch to a different pool to the current one, you will get a
 When you switch to a different pool to the current one, you will get a
 'Switching to URL' message in the cgminer status windows
 'Switching to URL' message in the cgminer status windows
@@ -769,9 +810,8 @@ api-example.c - a 'C' program to access the API (with source code)
 
 
 miner.php - an example web page to access the API
 miner.php - an example web page to access the API
  This includes buttons and inputs to attempt access to the privileged commands
  This includes buttons and inputs to attempt access to the privileged commands
- You must modify the 2 lines near the top to change where it looks for cgminer
-  $miner = '127.0.0.1'; # hostname or IP address
-  $port = 4028;
+ Read the top of the file (miner.php) for details of how to tune the display
+ and also to use the option to display a multi-rig summary
 
 
 ---
 ---
 
 
@@ -848,21 +888,14 @@ any further.
 
 
 Q: Can you change the autofan/autogpu to change speeds in a different manner?
 Q: Can you change the autofan/autogpu to change speeds in a different manner?
 A: The defaults are sane and safe. I'm not interested in changing them
 A: The defaults are sane and safe. I'm not interested in changing them
-further. The starting fan speed is set to 85% in auto-fan mode as a safety
-precaution, but if a specific fan speed has been set, it will use that first
-before adjusting automatically.
-
-Q: The fanspeed starts at 85% with --auto-fan. Can I set it lower?
-A: The initial fanspeed will always start at 85% unless you choose your own
-value with --gpu-fan. In this case it will use the value you give it with
---gpu-fan as the first fanspeed, but it will also use this as the maximum fan
-speed unless overheat is detected.
+further. The starting fan speed is set to 50% in auto-fan mode as a safety
+precaution.
 
 
 Q: Why is my efficiency above/below 100%?
 Q: Why is my efficiency above/below 100%?
 A: Efficiency simply means how many shares you return for the amount of work
 A: Efficiency simply means how many shares you return for the amount of work
 you request. It does not correlate with efficient use of your hardware, and is
 you request. It does not correlate with efficient use of your hardware, and is
 a measure of a combination of hardware speed, block luck, pool design and other
 a measure of a combination of hardware speed, block luck, pool design and other
-factors.
+factors
 
 
 Q: What are the best parameters to pass for X pool/hardware/device.
 Q: What are the best parameters to pass for X pool/hardware/device.
 A: Virtually always, the DEFAULT parameters give the best results. Most user
 A: Virtually always, the DEFAULT parameters give the best results. Most user
@@ -887,7 +920,7 @@ this time.
 
 
 Q: Which ATI SDK is the best for cgminer?
 Q: Which ATI SDK is the best for cgminer?
 A: At the moment, versions 2.4 and 2.5 work the best. If you are forced to use
 A: At the moment, versions 2.4 and 2.5 work the best. If you are forced to use
-the 2.6 SDK, -v 1 might help, along with not decreasing your memory clock speed.
+the 2.6 SDK.
 
 
 Q: I have multiple SDKs installed, can I choose which one it uses?
 Q: I have multiple SDKs installed, can I choose which one it uses?
 A: Run cgminer with the -n option and it will list all the platforms currently
 A: Run cgminer with the -n option and it will list all the platforms currently
@@ -923,6 +956,12 @@ it fail when php is installed properly but I only get errors about Sockets not
 working in the logs?
 working in the logs?
 A: http://us.php.net/manual/en/sockets.installation.php
 A: http://us.php.net/manual/en/sockets.installation.php
 
 
+Q: What is a PGA?
+A: At the moment, cgminer supports 2 FPGA's: Icarus and BitForce.
+They are Field-Programmable Gate Arrays that have been programmed to do Bitcoin
+mining. Since the acronym needs to be only 3 characters, the "Field-" part has
+been skipped.
+
 ---
 ---
 
 
 This code is provided entirely free of charge by the programmer in his spare
 This code is provided entirely free of charge by the programmer in his spare

+ 71 - 23
adl.c

@@ -124,11 +124,9 @@ static bool fanspeed_twin(struct gpu_adl *ga, struct gpu_adl *other_ga)
 	return true;
 	return true;
 }
 }
 
 
-void init_adl(int nDevs)
+static bool prepare_adl(void)
 {
 {
-	int result, i, j, devices = 0, last_adapter = -1, gpu = 0, dummy = 0;
-	struct gpu_adapters adapters[MAX_GPUDEVICES], vadapters[MAX_GPUDEVICES];
-	bool devs_match = true;
+	int result;
 
 
 #if defined (LINUX)
 #if defined (LINUX)
 	hDLL = dlopen( "libatiadlxx.so", RTLD_LAZY|RTLD_GLOBAL);
 	hDLL = dlopen( "libatiadlxx.so", RTLD_LAZY|RTLD_GLOBAL);
@@ -141,14 +139,8 @@ void init_adl(int nDevs)
 #endif
 #endif
 	if (hDLL == NULL) {
 	if (hDLL == NULL) {
 		applog(LOG_INFO, "Unable to load ati adl library");
 		applog(LOG_INFO, "Unable to load ati adl library");
-		return;
-	}
-
-	if (unlikely(pthread_mutex_init(&adl_lock, NULL))) {
-		applog(LOG_ERR, "Failed to init adl_lock in init_adl");
-		return;
+		return false;
 	}
 	}
-
 	ADL_Main_Control_Create = (ADL_MAIN_CONTROL_CREATE) GetProcAddress(hDLL,"ADL_Main_Control_Create");
 	ADL_Main_Control_Create = (ADL_MAIN_CONTROL_CREATE) GetProcAddress(hDLL,"ADL_Main_Control_Create");
 	ADL_Main_Control_Destroy = (ADL_MAIN_CONTROL_DESTROY) GetProcAddress(hDLL,"ADL_Main_Control_Destroy");
 	ADL_Main_Control_Destroy = (ADL_MAIN_CONTROL_DESTROY) GetProcAddress(hDLL,"ADL_Main_Control_Destroy");
 	ADL_Adapter_NumberOfAdapters_Get = (ADL_ADAPTER_NUMBEROFADAPTERS_GET) GetProcAddress(hDLL,"ADL_Adapter_NumberOfAdapters_Get");
 	ADL_Adapter_NumberOfAdapters_Get = (ADL_ADAPTER_NUMBEROFADAPTERS_GET) GetProcAddress(hDLL,"ADL_Adapter_NumberOfAdapters_Get");
@@ -177,7 +169,7 @@ void init_adl(int nDevs)
 		!ADL_Main_Control_Refresh || !ADL_Overdrive5_PowerControl_Get ||
 		!ADL_Main_Control_Refresh || !ADL_Overdrive5_PowerControl_Get ||
 		!ADL_Overdrive5_PowerControl_Set || !ADL_Overdrive5_FanSpeedToDefault_Set) {
 		!ADL_Overdrive5_PowerControl_Set || !ADL_Overdrive5_FanSpeedToDefault_Set) {
 			applog(LOG_WARNING, "ATI ADL's API is missing");
 			applog(LOG_WARNING, "ATI ADL's API is missing");
-		return;
+		return false;
 	}
 	}
 
 
 	// Initialise ADL. The second parameter is 1, which means:
 	// Initialise ADL. The second parameter is 1, which means:
@@ -185,15 +177,32 @@ void init_adl(int nDevs)
 	result = ADL_Main_Control_Create (ADL_Main_Memory_Alloc, 1);
 	result = ADL_Main_Control_Create (ADL_Main_Memory_Alloc, 1);
 	if (result != ADL_OK) {
 	if (result != ADL_OK) {
 		applog(LOG_INFO, "ADL Initialisation Error! Error %d!", result);
 		applog(LOG_INFO, "ADL Initialisation Error! Error %d!", result);
-		return ;
+		return false;
 	}
 	}
 
 
 	result = ADL_Main_Control_Refresh();
 	result = ADL_Main_Control_Refresh();
 	if (result != ADL_OK) {
 	if (result != ADL_OK) {
 		applog(LOG_INFO, "ADL Refresh Error! Error %d!", result);
 		applog(LOG_INFO, "ADL Refresh Error! Error %d!", result);
-		return ;
+		return false;
 	}
 	}
 
 
+	return true;
+}
+
+void init_adl(int nDevs)
+{
+	int result, i, j, devices = 0, last_adapter = -1, gpu = 0, dummy = 0;
+	struct gpu_adapters adapters[MAX_GPUDEVICES], vadapters[MAX_GPUDEVICES];
+	bool devs_match = true;
+
+	if (unlikely(pthread_mutex_init(&adl_lock, NULL))) {
+		applog(LOG_ERR, "Failed to init adl_lock in init_adl");
+		return;
+	}
+
+	if (!prepare_adl())
+		return;
+
 	// Obtain the number of adapters for the system
 	// Obtain the number of adapters for the system
 	result = ADL_Adapter_NumberOfAdapters_Get (&iNumberAdapters);
 	result = ADL_Adapter_NumberOfAdapters_Get (&iNumberAdapters);
 	if (result != ADL_OK) {
 	if (result != ADL_OK) {
@@ -466,7 +475,7 @@ void init_adl(int nDevs)
 		if (opt_autofan) {
 		if (opt_autofan) {
 			ga->autofan = true;
 			ga->autofan = true;
 			/* Set a safe starting default if we're automanaging fan speeds */
 			/* Set a safe starting default if we're automanaging fan speeds */
-			set_fanspeed(gpu, gpus[gpu].gpu_fan);
+			set_fanspeed(gpu, 50);
 		}
 		}
 		if (opt_autoengine) {
 		if (opt_autoengine) {
 			ga->autoengine = true;
 			ga->autoengine = true;
@@ -673,6 +682,16 @@ int gpu_fanpercent(int gpu)
 	lock_adl();
 	lock_adl();
 	ret = __gpu_fanpercent(ga);
 	ret = __gpu_fanpercent(ga);
 	unlock_adl();
 	unlock_adl();
+	if (unlikely(ga->has_fanspeed && ret == -1)) {
+		applog(LOG_WARNING, "GPU %d stopped reporting fanspeed due to driver corruption", gpu);
+		if (opt_restart) {
+			applog(LOG_WARNING, "Restart enabled, will restart cgminer");
+			applog(LOG_WARNING, "You can disable this with the --no-restart option");
+			app_restart();
+		}
+		applog(LOG_WARNING, "Disabling fanspeed monitoring on this device");
+		ga->has_fanspeed = false;
+	}
 	return ret;
 	return ret;
 }
 }
 
 
@@ -1000,6 +1019,10 @@ static bool fan_autotune(int gpu, int temp, int fanpercent, int lasttemp)
 	if (temp > ga->overtemp && fanpercent < iMax) {
 	if (temp > ga->overtemp && fanpercent < iMax) {
 		applog(LOG_WARNING, "Overheat detected on GPU %d, increasing fan to 100%", gpu);
 		applog(LOG_WARNING, "Overheat detected on GPU %d, increasing fan to 100%", gpu);
 		newpercent = iMax;
 		newpercent = iMax;
+
+		cgpu->device_last_not_well = time(NULL);
+		cgpu->device_not_well_reason = REASON_DEV_OVER_HEAT;
+		cgpu->dev_over_heat_count++;
 	} else if (temp > ga->targettemp && fanpercent < top && temp >= lasttemp) {
 	} else if (temp > ga->targettemp && fanpercent < top && temp >= lasttemp) {
 		applog(LOG_DEBUG, "Temperature over target, increasing fanspeed");
 		applog(LOG_DEBUG, "Temperature over target, increasing fanspeed");
 		if (temp > ga->targettemp + opt_hysteresis)
 		if (temp > ga->targettemp + opt_hysteresis)
@@ -1084,9 +1107,17 @@ void gpu_autotune(int gpu, enum dev_enable *denable)
 			applog(LOG_WARNING, "Hit thermal cutoff limit on GPU %d, disabling!", gpu);
 			applog(LOG_WARNING, "Hit thermal cutoff limit on GPU %d, disabling!", gpu);
 			*denable = DEV_RECOVER;
 			*denable = DEV_RECOVER;
 			newengine = ga->minspeed;
 			newengine = ga->minspeed;
+
+			cgpu->device_last_not_well = time(NULL);
+			cgpu->device_not_well_reason = REASON_DEV_THERMAL_CUTOFF;
+			cgpu->dev_thermal_cutoff_count++;
 		} else if (temp > ga->overtemp && engine > ga->minspeed) {
 		} else if (temp > ga->overtemp && engine > ga->minspeed) {
 			applog(LOG_WARNING, "Overheat detected, decreasing GPU %d clock speed", gpu);
 			applog(LOG_WARNING, "Overheat detected, decreasing GPU %d clock speed", gpu);
 			newengine = ga->minspeed;
 			newengine = ga->minspeed;
+
+			cgpu->device_last_not_well = time(NULL);
+			cgpu->device_not_well_reason = REASON_DEV_OVER_HEAT;
+			cgpu->dev_over_heat_count++;
 		} else if (temp > ga->targettemp + opt_hysteresis && engine > ga->minspeed && fan_optimal) {
 		} else if (temp > ga->targettemp + opt_hysteresis && engine > ga->minspeed && fan_optimal) {
 			applog(LOG_DEBUG, "Temperature %d degrees over target, decreasing clock speed", opt_hysteresis);
 			applog(LOG_DEBUG, "Temperature %d degrees over target, decreasing clock speed", opt_hysteresis);
 			newengine = engine - ga->lpOdParameters.sEngineClock.iStep;
 			newengine = engine - ga->lpOdParameters.sEngineClock.iStep;
@@ -1305,6 +1336,17 @@ updated:
 }
 }
 #endif
 #endif
 
 
+static void free_adl(void)
+{
+	ADL_Main_Memory_Free ((void **)&lpInfo);
+	ADL_Main_Control_Destroy ();
+#if defined (LINUX)
+	dlclose(hDLL);
+#else
+	FreeLibrary(hDLL);
+#endif
+}
+
 void clear_adl(int nDevs)
 void clear_adl(int nDevs)
 {
 {
 	struct gpu_adl *ga;
 	struct gpu_adl *ga;
@@ -1325,15 +1367,21 @@ void clear_adl(int nDevs)
 		ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
 		ADL_Overdrive5_FanSpeed_Set(ga->iAdapterIndex, 0, &ga->DefFanSpeedValue);
 		ADL_Overdrive5_FanSpeedToDefault_Set(ga->iAdapterIndex, 0);
 		ADL_Overdrive5_FanSpeedToDefault_Set(ga->iAdapterIndex, 0);
 	}
 	}
-
-	ADL_Main_Memory_Free ( (void **)&lpInfo );
-	ADL_Main_Control_Destroy ();
+	adl_active = false;
 	unlock_adl();
 	unlock_adl();
+	free_adl();
+}
 
 
-#if defined (LINUX)
-	dlclose(hDLL);
-#else
-	FreeLibrary(hDLL);
-#endif
+void reinit_adl(void)
+{
+	bool ret;
+	lock_adl();
+	free_adl();
+	ret = prepare_adl();
+	if (!ret) {
+		adl_active = false;
+		applog(LOG_WARNING, "Attempt to re-initialise ADL has failed, disabling");
+	}
+	unlock_adl();
 }
 }
 #endif /* HAVE_ADL */
 #endif /* HAVE_ADL */

+ 2 - 0
adl.h

@@ -19,10 +19,12 @@ bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vdd
 void change_gpusettings(int gpu);
 void change_gpusettings(int gpu);
 void gpu_autotune(int gpu, enum dev_enable *denable);
 void gpu_autotune(int gpu, enum dev_enable *denable);
 void clear_adl(int nDevs);
 void clear_adl(int nDevs);
+void reinit_adl(void);
 #else /* HAVE_ADL */
 #else /* HAVE_ADL */
 #define adl_active (0)
 #define adl_active (0)
 static inline void init_adl(int nDevs) {}
 static inline void init_adl(int nDevs) {}
 static inline void change_gpusettings(int gpu) { }
 static inline void change_gpusettings(int gpu) { }
 static inline void clear_adl(int nDevs) {}
 static inline void clear_adl(int nDevs) {}
+static inline void reinit_adl(void) {}
 #endif
 #endif
 #endif
 #endif

File diff suppressed because it is too large
+ 671 - 37
api.c


+ 24 - 0
bitstreams/LICENSE.txt

@@ -0,0 +1,24 @@
+All the bitstream files included in this directory that follow the name pattern ztex_*.bit are:
+
+----
+
+Copyright (C) 2009-2011 ZTEX GmbH.
+http://www.ztex.de
+
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License version 3 as
+published by the Free Software Foundation.
+
+This program is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, see http://www.gnu.org/licenses/.
+
+----
+
+You can find the original sources at the BTCMiner project home page: http://www.ztex.de/btcminer/
+

BIN
bitstreams/ztex_ufm1_15b1.bit


BIN
bitstreams/ztex_ufm1_15d1.bit


BIN
bitstreams/ztex_ufm1_15d3.bit


+ 226 - 62
cgminer.c

@@ -54,7 +54,6 @@
 	#include <sys/wait.h>
 	#include <sys/wait.h>
 #endif
 #endif
 
 
-
 enum workio_commands {
 enum workio_commands {
 	WC_GET_WORK,
 	WC_GET_WORK,
 	WC_SUBMIT_WORK,
 	WC_SUBMIT_WORK,
@@ -221,6 +220,7 @@ static int include_count = 0;
 
 
 #if defined(unix)
 #if defined(unix)
 	static char *opt_stderr_cmd = NULL;
 	static char *opt_stderr_cmd = NULL;
+	static int forkpid = 0;
 #endif // defined(unix)
 #endif // defined(unix)
 
 
 bool ping = true;
 bool ping = true;
@@ -887,12 +887,17 @@ static struct opt_table opt_config_table[] = {
 
 
 static char *load_config(const char *arg, void __maybe_unused *unused);
 static char *load_config(const char *arg, void __maybe_unused *unused);
 
 
+static int fileconf_load;
+
 static char *parse_config(json_t *config, bool fileconf)
 static char *parse_config(json_t *config, bool fileconf)
 {
 {
 	static char err_buf[200];
 	static char err_buf[200];
 	json_t *val;
 	json_t *val;
 	struct opt_table *opt;
 	struct opt_table *opt;
 
 
+	if (fileconf && !fileconf_load)
+		fileconf_load = 1;
+
 	for (opt = opt_config_table; opt->type != OPT_END; opt++) {
 	for (opt = opt_config_table; opt->type != OPT_END; opt++) {
 		char *p, *name;
 		char *p, *name;
 
 
@@ -916,24 +921,26 @@ static char *parse_config(json_t *config, bool fileconf)
 						  opt->u.arg);
 						  opt->u.arg);
 			} else if ((opt->type & OPT_HASARG) && json_is_array(val)) {
 			} else if ((opt->type & OPT_HASARG) && json_is_array(val)) {
 				int n, size = json_array_size(val);
 				int n, size = json_array_size(val);
-				for(n = 0; n < size && !err; n++) {
+
+				for (n = 0; n < size && !err; n++) {
 					if (json_is_string(json_array_get(val, n)))
 					if (json_is_string(json_array_get(val, n)))
 						err = opt->cb_arg(json_string_value(json_array_get(val, n)), opt->u.arg);
 						err = opt->cb_arg(json_string_value(json_array_get(val, n)), opt->u.arg);
 					else if (json_is_object(json_array_get(val, n)))
 					else if (json_is_object(json_array_get(val, n)))
 						err = parse_config(json_array_get(val, n), false);
 						err = parse_config(json_array_get(val, n), false);
 				}
 				}
-			} else if ((opt->type&OPT_NOARG) && json_is_true(val)) {
+			} else if ((opt->type & OPT_NOARG) && json_is_true(val))
 				err = opt->cb(opt->u.arg);
 				err = opt->cb(opt->u.arg);
-			} else {
+			else
 				err = "Invalid value";
 				err = "Invalid value";
-			}
+
 			if (err) {
 			if (err) {
 				/* Allow invalid values to be in configuration
 				/* Allow invalid values to be in configuration
 				 * file, just skipping over them provided the
 				 * file, just skipping over them provided the
 				 * JSON is still valid after that. */
 				 * JSON is still valid after that. */
-				if (fileconf)
+				if (fileconf) {
 					applog(LOG_ERR, "Invalid config option %s: %s", p, err);
 					applog(LOG_ERR, "Invalid config option %s: %s", p, err);
-				else {
+					fileconf_load = -1;
+				} else {
 					sprintf(err_buf, "Parsing JSON option %s: %s",
 					sprintf(err_buf, "Parsing JSON option %s: %s",
 						p, err);
 						p, err);
 					return err_buf;
 					return err_buf;
@@ -950,13 +957,18 @@ static char *parse_config(json_t *config, bool fileconf)
 	return NULL;
 	return NULL;
 }
 }
 
 
+char *cnfbuf = NULL;
+
 static char *load_config(const char *arg, void __maybe_unused *unused)
 static char *load_config(const char *arg, void __maybe_unused *unused)
 {
 {
 	json_error_t err;
 	json_error_t err;
 	json_t *config;
 	json_t *config;
 	char *json_error;
 	char *json_error;
 
 
-	if(++include_count > JSON_MAX_DEPTH)
+	if (!cnfbuf)
+		cnfbuf = strdup(arg);
+
+	if (++include_count > JSON_MAX_DEPTH)
 		return JSON_MAX_DEPTH_ERR;
 		return JSON_MAX_DEPTH_ERR;
 
 
 #if JANSSON_MAJOR_VERSION > 1
 #if JANSSON_MAJOR_VERSION > 1
@@ -974,6 +986,7 @@ static char *load_config(const char *arg, void __maybe_unused *unused)
 	}
 	}
 
 
 	config_loaded = true;
 	config_loaded = true;
+
 	/* Parse the config now, so we can override it.  That can keep pointers
 	/* Parse the config now, so we can override it.  That can keep pointers
 	 * so don't free config object. */
 	 * so don't free config object. */
 	return parse_config(config, true);
 	return parse_config(config, true);
@@ -981,22 +994,26 @@ static char *load_config(const char *arg, void __maybe_unused *unused)
 
 
 static void load_default_config(void)
 static void load_default_config(void)
 {
 {
-	char buf[PATH_MAX];
+	cnfbuf = malloc(PATH_MAX);
 
 
 #if defined(unix)
 #if defined(unix)
 	if (getenv("HOME") && *getenv("HOME")) {
 	if (getenv("HOME") && *getenv("HOME")) {
-	        strcpy(buf, getenv("HOME"));
-		strcat(buf, "/");
+	        strcpy(cnfbuf, getenv("HOME"));
+		strcat(cnfbuf, "/");
 	}
 	}
 	else
 	else
-		strcpy(buf, "");
-	strcat(buf, ".cgminer/");
+		strcpy(cnfbuf, "");
+	strcat(cnfbuf, ".cgminer/");
 #else
 #else
-	strcpy(buf, "");
+	strcpy(cnfbuf, "");
 #endif
 #endif
-	strcat(buf, def_conf);
-	if (!access(buf, R_OK))
-		load_config(buf, NULL);
+	strcat(cnfbuf, def_conf);
+	if (!access(cnfbuf, R_OK))
+		load_config(cnfbuf, NULL);
+	else {
+		free(cnfbuf);
+		cnfbuf = NULL;
+	}
 }
 }
 
 
 extern const char *opt_argv0;
 extern const char *opt_argv0;
@@ -1015,6 +1032,9 @@ static char *opt_verusage_and_exit(const char *extra)
 #endif
 #endif
 #ifdef USE_ICARUS
 #ifdef USE_ICARUS
 		"icarus "
 		"icarus "
+#endif
+#ifdef USE_ZTEX
+		"ztex "
 #endif
 #endif
 		"mining support.\n"
 		"mining support.\n"
 		, packagename);
 		, packagename);
@@ -1140,10 +1160,10 @@ void decay_time(double *f, double fadd)
 			ratio = 1 / ratio;
 			ratio = 1 / ratio;
 	}
 	}
 
 
-	if (ratio > 0.95)
-		*f = (fadd * 0.1 + *f) / 1.1;
+	if (ratio > 0.63)
+		*f = (fadd * 0.58 + *f) / 1.58;
 	else
 	else
-		*f = (fadd + *f * 0.1) / 1.1;
+		*f = (fadd + *f * 0.58) / 1.58;
 }
 }
 
 
 static int requests_staged(void)
 static int requests_staged(void)
@@ -1161,7 +1181,10 @@ WINDOW *mainwin, *statuswin, *logwin;
 #endif
 #endif
 double total_secs = 0.1;
 double total_secs = 0.1;
 static char statusline[256];
 static char statusline[256];
+/* logstart is where the log window should start */
 static int devcursor, logstart, logcursor;
 static int devcursor, logstart, logcursor;
+/* statusy is where the status window goes up to in cases where it won't fit at startup */
+static int statusy;
 struct cgpu_info gpus[MAX_GPUDEVICES]; /* Maximum number apparently possible */
 struct cgpu_info gpus[MAX_GPUDEVICES]; /* Maximum number apparently possible */
 struct cgpu_info *cpus;
 struct cgpu_info *cpus;
 
 
@@ -1256,7 +1279,7 @@ static void curses_print_status(void)
 	wclrtoeol(statuswin);
 	wclrtoeol(statuswin);
 	mvwprintw(statuswin, 5, 0, " Block: %s...  Started: %s", current_hash, blocktime);
 	mvwprintw(statuswin, 5, 0, " Block: %s...  Started: %s", current_hash, blocktime);
 	mvwhline(statuswin, 6, 0, '-', 80);
 	mvwhline(statuswin, 6, 0, '-', 80);
-	mvwhline(statuswin, logstart - 1, 0, '-', 80);
+	mvwhline(statuswin, statusy - 1, 0, '-', 80);
 	mvwprintw(statuswin, devcursor - 1, 1, "[P]ool management %s[S]ettings [D]isplay options [Q]uit",
 	mvwprintw(statuswin, devcursor - 1, 1, "[P]ool management %s[S]ettings [D]isplay options [Q]uit",
 		have_opencl ? "[G]PU management " : "");
 		have_opencl ? "[G]PU management " : "");
 }
 }
@@ -1273,9 +1296,12 @@ static void curses_print_devstatus(int thr_id)
 	struct cgpu_info *cgpu = thr_info[thr_id].cgpu;
 	struct cgpu_info *cgpu = thr_info[thr_id].cgpu;
 	char logline[255];
 	char logline[255];
 
 
-		cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
+	cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
 
 
-	mvwprintw(statuswin, devcursor + cgpu->cgminer_id, 0, " %s %d: ", cgpu->api->name, cgpu->device_id);
+	/* Check this isn't out of the window size */
+	if (wmove(statuswin,devcursor + cgpu->cgminer_id, 0) == ERR)
+		return;
+	wprintw(statuswin, " %s %d: ", cgpu->api->name, cgpu->device_id);
 	if (cgpu->api->get_statline_before) {
 	if (cgpu->api->get_statline_before) {
 		logline[0] = '\0';
 		logline[0] = '\0';
 		cgpu->api->get_statline_before(logline, cgpu);
 		cgpu->api->get_statline_before(logline, cgpu);
@@ -1284,20 +1310,20 @@ static void curses_print_devstatus(int thr_id)
 	else
 	else
 		wprintw(statuswin, "               | ");
 		wprintw(statuswin, "               | ");
 
 
-		if (cgpu->status == LIFE_DEAD)
-			wprintw(statuswin, "DEAD ");
-		else if (cgpu->status == LIFE_SICK)
-			wprintw(statuswin, "SICK ");
+	if (cgpu->status == LIFE_DEAD)
+		wprintw(statuswin, "DEAD ");
+	else if (cgpu->status == LIFE_SICK)
+		wprintw(statuswin, "SICK ");
 	else if (cgpu->deven == DEV_DISABLED)
 	else if (cgpu->deven == DEV_DISABLED)
 		wprintw(statuswin, "OFF  ");
 		wprintw(statuswin, "OFF  ");
 	else if (cgpu->deven == DEV_RECOVER)
 	else if (cgpu->deven == DEV_RECOVER)
 		wprintw(statuswin, "REST  ");
 		wprintw(statuswin, "REST  ");
 	else
 	else
 		wprintw(statuswin, "%5.1f", cgpu->rolling);
 		wprintw(statuswin, "%5.1f", cgpu->rolling);
-		adj_width(cgpu->accepted, &awidth);
-		adj_width(cgpu->rejected, &rwidth);
-		adj_width(cgpu->hw_errors, &hwwidth);
-		adj_width(cgpu->utility, &uwidth);
+	adj_width(cgpu->accepted, &awidth);
+	adj_width(cgpu->rejected, &rwidth);
+	adj_width(cgpu->hw_errors, &hwwidth);
+	adj_width(cgpu->utility, &uwidth);
 	wprintw(statuswin, "/%5.1fMh/s | A:%*d R:%*d HW:%*d U:%*.2f/m",
 	wprintw(statuswin, "/%5.1fMh/s | A:%*d R:%*d HW:%*d U:%*.2f/m",
 			cgpu->total_mhashes / total_secs,
 			cgpu->total_mhashes / total_secs,
 			awidth, cgpu->accepted,
 			awidth, cgpu->accepted,
@@ -1311,7 +1337,7 @@ static void curses_print_devstatus(int thr_id)
 		wprintw(statuswin, "%s", logline);
 		wprintw(statuswin, "%s", logline);
 	}
 	}
 
 
-		wclrtoeol(statuswin);
+	wclrtoeol(statuswin);
 }
 }
 #endif
 #endif
 
 
@@ -1326,16 +1352,31 @@ static void print_status(int thr_id)
 static inline bool change_logwinsize(void)
 static inline bool change_logwinsize(void)
 {
 {
 	int x, y, logx, logy;
 	int x, y, logx, logy;
+	bool ret = false;
 
 
 	getmaxyx(mainwin, y, x);
 	getmaxyx(mainwin, y, x);
-	getmaxyx(logwin, logy, logx);
+	if (x < 80 || y < 25)
+		return ret;
+
+	if (y > statusy + 2 && statusy < logstart) {
+		if (y - 2 < logstart)
+			statusy = y - 2;
+		else
+			statusy = logstart;
+		logcursor = statusy + 1;
+		mvwin(logwin, logcursor, 0);
+		wresize(statuswin, statusy, x);
+		ret = true;
+	}
+
 	y -= logcursor;
 	y -= logcursor;
+	getmaxyx(logwin, logy, logx);
 	/* Detect screen size change */
 	/* Detect screen size change */
-	if ((x != logx || y != logy) && x >= 80 && y >= 25) {
+	if (x != logx || y != logy) {
 		wresize(logwin, y, x);
 		wresize(logwin, y, x);
-		return true;
+		ret = true;
 	}
 	}
-	return false;
+	return ret;
 }
 }
 
 
 static void check_winsizes(void)
 static void check_winsizes(void)
@@ -1346,7 +1387,12 @@ static void check_winsizes(void)
 		int y, x;
 		int y, x;
 
 
 		x = getmaxx(statuswin);
 		x = getmaxx(statuswin);
-		wresize(statuswin, logstart, x);
+		if (logstart > LINES - 2)
+			statusy = LINES - 2;
+		else
+			statusy = logstart;
+		logcursor = statusy + 1;
+		wresize(statuswin, statusy, x);
 		getmaxyx(mainwin, y, x);
 		getmaxyx(mainwin, y, x);
 		y -= logcursor;
 		y -= logcursor;
 		wresize(logwin, y, x);
 		wresize(logwin, y, x);
@@ -1527,6 +1573,8 @@ static bool submit_upstream_work(const struct work *work)
 	res = json_object_get(val, "result");
 	res = json_object_get(val, "result");
 
 
 	if (!QUIET) {
 	if (!QUIET) {
+#ifndef MIPSEB
+// This one segfaults on my router for some reason
 		isblock = regeneratehash(work);
 		isblock = regeneratehash(work);
 		if (isblock)
 		if (isblock)
 			found_blocks++;
 			found_blocks++;
@@ -1534,6 +1582,7 @@ static bool submit_upstream_work(const struct work *work)
 		sprintf(hashshow, "%08lx.%08lx.%08lx%s",
 		sprintf(hashshow, "%08lx.%08lx.%08lx%s",
 			(unsigned long)(hash32[7]), (unsigned long)(hash32[6]), (unsigned long)(hash32[5]),
 			(unsigned long)(hash32[7]), (unsigned long)(hash32[6]), (unsigned long)(hash32[5]),
 			isblock ? " BLOCK!" : "");
 			isblock ? " BLOCK!" : "");
+#endif
 	}
 	}
 
 
 	/* Theoretically threads could race when modifying accepted and
 	/* Theoretically threads could race when modifying accepted and
@@ -1777,8 +1826,7 @@ static void disable_curses(void)
 
 
 static void print_summary(void);
 static void print_summary(void);
 
 
-/* This should be the common exit path */
-void kill_work(void)
+static void __kill_work(void)
 {
 {
 	struct thr_info *thr;
 	struct thr_info *thr;
 	int i;
 	int i;
@@ -1825,11 +1873,37 @@ void kill_work(void)
 	applog(LOG_DEBUG, "Killing off API thread");
 	applog(LOG_DEBUG, "Killing off API thread");
 	thr = &thr_info[api_thr_id];
 	thr = &thr_info[api_thr_id];
 	thr_info_cancel(thr);
 	thr_info_cancel(thr);
+}
+
+/* This should be the common exit path */
+void kill_work(void)
+{
+	__kill_work();
 
 
 	quit(0, "Shutdown signal received.");
 	quit(0, "Shutdown signal received.");
 }
 }
 
 
-void quit(int status, const char *format, ...);
+static char **initial_args;
+
+static void clean_up(void);
+
+void app_restart(void)
+{
+	applog(LOG_WARNING, "Attempting to restart %s", packagename);
+
+	__kill_work();
+	clean_up();
+
+#if defined(unix)
+	if (forkpid > 0) {
+		kill(forkpid, SIGTERM);
+		forkpid = 0;
+	}
+#endif
+
+	execv(initial_args[0], initial_args);
+	applog(LOG_WARNING, "Failed to restart application");
+}
 
 
 static void sighandler(int __maybe_unused sig)
 static void sighandler(int __maybe_unused sig)
 {
 {
@@ -1928,16 +2002,16 @@ static void *submit_work_thread(void *userdata)
 	pthread_detach(pthread_self());
 	pthread_detach(pthread_self());
 
 
 	if (stale_work(work, true)) {
 	if (stale_work(work, true)) {
-		total_stale++;
-		pool->stale_shares++;
-		if (!opt_submit_stale && !pool->submit_old) {
-			applog(LOG_NOTICE, "Stale share detected, discarding");
-			goto out;
-		}
 		if (opt_submit_stale)
 		if (opt_submit_stale)
 			applog(LOG_NOTICE, "Stale share detected, submitting as user requested");
 			applog(LOG_NOTICE, "Stale share detected, submitting as user requested");
 		else if (pool->submit_old)
 		else if (pool->submit_old)
 			applog(LOG_NOTICE, "Stale share detected, submitting as pool requested");
 			applog(LOG_NOTICE, "Stale share detected, submitting as pool requested");
+		else {
+			applog(LOG_NOTICE, "Stale share detected, discarding");
+			total_stale++;
+			pool->stale_shares++;
+			goto out;
+		}
 	}
 	}
 
 
 	/* submit solution to bitcoin via JSON-RPC */
 	/* submit solution to bitcoin via JSON-RPC */
@@ -2158,6 +2232,7 @@ static void set_curblock(char *hexstr, unsigned char *hash)
 	current_hash = bin2hex(hash_swap, 16);
 	current_hash = bin2hex(hash_swap, 16);
 	if (unlikely(!current_hash))
 	if (unlikely(!current_hash))
 		quit (1, "set_curblock OOM");
 		quit (1, "set_curblock OOM");
+	applog(LOG_INFO, "New block: %s...", current_hash);
 	if (old_hash)
 	if (old_hash)
 		free(old_hash);
 		free(old_hash);
 }
 }
@@ -2358,10 +2433,11 @@ static void display_pool_summary(struct pool *pool)
 		unlock_curses();
 		unlock_curses();
 	}
 	}
 }
 }
+#endif
 
 
 /* We can't remove the memory used for this struct pool because there may
 /* We can't remove the memory used for this struct pool because there may
  * still be work referencing it. We just remove it from the pools list */
  * still be work referencing it. We just remove it from the pools list */
-static void remove_pool(struct pool *pool)
+void remove_pool(struct pool *pool)
 {
 {
 	int i, last_pool = total_pools - 1;
 	int i, last_pool = total_pools - 1;
 	struct pool *other;
 	struct pool *other;
@@ -2382,7 +2458,6 @@ static void remove_pool(struct pool *pool)
 	pool->pool_no = total_pools;
 	pool->pool_no = total_pools;
 	total_pools--;
 	total_pools--;
 }
 }
-#endif
 
 
 void write_config(FILE *fcfg)
 void write_config(FILE *fcfg)
 {
 {
@@ -2754,7 +2829,8 @@ static void set_options(void)
 	clear_logwin();
 	clear_logwin();
 retry:
 retry:
 	wlogprint("\n[L]ongpoll: %s\n", want_longpoll ? "On" : "Off");
 	wlogprint("\n[L]ongpoll: %s\n", want_longpoll ? "On" : "Off");
-	wlogprint("[Q]ueue: %d\n[S]cantime: %d\n[E]xpiry: %d\n[R]etries: %d\n[P]ause: %d\n[W]rite config file\n",
+	wlogprint("[Q]ueue: %d\n[S]cantime: %d\n[E]xpiry: %d\n[R]etries: %d\n"
+		  "[P]ause: %d\n[W]rite config file\n[C]gminer restart\n",
 		opt_queue, opt_scantime, opt_expiry, opt_retries, opt_fail_pause);
 		opt_queue, opt_scantime, opt_expiry, opt_retries, opt_fail_pause);
 	wlogprint("Select an option or any other key to return\n");
 	wlogprint("Select an option or any other key to return\n");
 	input = getch();
 	input = getch();
@@ -2847,6 +2923,13 @@ retry:
 		fclose(fcfg);
 		fclose(fcfg);
 		goto retry;
 		goto retry;
 
 
+	} else if (!strncasecmp(&input, "c", 1)) {
+		wlogprint("Are you sure?\n");
+		input = getch();
+		if (!strncasecmp(&input, "y", 1))
+			app_restart();
+		else
+			clear_logwin();
 	} else
 	} else
 		clear_logwin();
 		clear_logwin();
 
 
@@ -2944,6 +3027,7 @@ void thread_reportin(struct thr_info *thr)
 	gettimeofday(&thr->last, NULL);
 	gettimeofday(&thr->last, NULL);
 	thr->cgpu->status = LIFE_WELL;
 	thr->cgpu->status = LIFE_WELL;
 	thr->getwork = false;
 	thr->getwork = false;
+	thr->cgpu->device_last_well = time(NULL);
 }
 }
 
 
 static inline void thread_reportout(struct thr_info *thr)
 static inline void thread_reportout(struct thr_info *thr)
@@ -2964,8 +3048,10 @@ static void hashmeter(int thr_id, struct timeval *diff,
 	bool showlog = false;
 	bool showlog = false;
 
 
 	/* Update the last time this thread reported in */
 	/* Update the last time this thread reported in */
-	if (thr_id >= 0)
+	if (thr_id >= 0) {
 		gettimeofday(&thr_info[thr_id].last, NULL);
 		gettimeofday(&thr_info[thr_id].last, NULL);
+		thr_info[thr_id].cgpu->device_last_well = time(NULL);
+	}
 
 
 	/* Don't bother calculating anything if we're not displaying it */
 	/* Don't bother calculating anything if we're not displaying it */
 	if (opt_realquiet || !opt_log_interval)
 	if (opt_realquiet || !opt_log_interval)
@@ -2991,8 +3077,10 @@ static void hashmeter(int thr_id, struct timeval *diff,
 			if (th->cgpu == cgpu)
 			if (th->cgpu == cgpu)
 				thread_rolling += th->rolling;
 				thread_rolling += th->rolling;
 		}
 		}
+		mutex_lock(&hash_lock);
 		decay_time(&cgpu->rolling, thread_rolling);
 		decay_time(&cgpu->rolling, thread_rolling);
 		cgpu->total_mhashes += local_mhashes;
 		cgpu->total_mhashes += local_mhashes;
+		mutex_unlock(&hash_lock);
 
 
 		// If needed, output detailed, per-device stats
 		// If needed, output detailed, per-device stats
 		if (want_per_device_stats) {
 		if (want_per_device_stats) {
@@ -3479,8 +3567,13 @@ void *miner_thread(void *userdata)
 	bool requested = false;
 	bool requested = false;
 	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
 	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
 
 
-	if (api->thread_init && !api->thread_init(mythr))
+	if (api->thread_init && !api->thread_init(mythr)) {
+		cgpu->device_last_not_well = time(NULL);
+		cgpu->device_not_well_reason = REASON_THREAD_FAIL_INIT;
+		cgpu->thread_fail_init_count++;
+
 		goto out;
 		goto out;
+	}
 
 
 	thread_reportout(mythr);
 	thread_reportout(mythr);
 	applog(LOG_DEBUG, "Popping ping in miner thread");
 	applog(LOG_DEBUG, "Popping ping in miner thread");
@@ -3529,8 +3622,14 @@ void *miner_thread(void *userdata)
 				break;
 				break;
 			}
 			}
 
 
-			if (unlikely(!hashes))
+			if (unlikely(!hashes)) {
+				cgpu->device_last_not_well = time(NULL);
+				cgpu->device_not_well_reason = REASON_THREAD_ZERO_HASH;
+				cgpu->thread_zero_hash_count++;
+
 				goto out;
 				goto out;
+			}
+
 			hashes_done += hashes;
 			hashes_done += hashes;
 			if (hashes > cgpu->max_hashes)
 			if (hashes > cgpu->max_hashes)
 				cgpu->max_hashes = hashes;
 				cgpu->max_hashes = hashes;
@@ -3550,6 +3649,11 @@ void *miner_thread(void *userdata)
 					thread_reportout(mythr);
 					thread_reportout(mythr);
 					if (unlikely(!queue_request(mythr, false))) {
 					if (unlikely(!queue_request(mythr, false))) {
 						applog(LOG_ERR, "Failed to queue_request in miner_thread %d", thr_id);
 						applog(LOG_ERR, "Failed to queue_request in miner_thread %d", thr_id);
+
+						cgpu->device_last_not_well = time(NULL);
+						cgpu->device_not_well_reason = REASON_THREAD_FAIL_QUEUE;
+						cgpu->thread_fail_queue_count++;
+
 						goto out;
 						goto out;
 					}
 					}
 					thread_reportin(mythr);
 					thread_reportin(mythr);
@@ -3582,7 +3686,7 @@ void *miner_thread(void *userdata)
 				tv_lastupdate = tv_end;
 				tv_lastupdate = tv_end;
 			}
 			}
 
 
-			if (unlikely(mythr->pause || cgpu->deven == DEV_DISABLED)) {
+			if (unlikely(mythr->pause || cgpu->deven != DEV_ENABLED)) {
 				applog(LOG_WARNING, "Thread %d being disabled", thr_id);
 				applog(LOG_WARNING, "Thread %d being disabled", thr_id);
 				mythr->rolling = mythr->cgpu->rolling = 0;
 				mythr->rolling = mythr->cgpu->rolling = 0;
 				applog(LOG_DEBUG, "Popping wakeup ping in miner thread");
 				applog(LOG_DEBUG, "Popping wakeup ping in miner thread");
@@ -3938,11 +4042,16 @@ static void *watchdog_thread(void __maybe_unused *userdata)
 			if (gpus[gpu].status != LIFE_WELL && now.tv_sec - thr->last.tv_sec < 60) {
 			if (gpus[gpu].status != LIFE_WELL && now.tv_sec - thr->last.tv_sec < 60) {
 				applog(LOG_ERR, "Device %d recovered, GPU %d declared WELL!", i, gpu);
 				applog(LOG_ERR, "Device %d recovered, GPU %d declared WELL!", i, gpu);
 				gpus[gpu].status = LIFE_WELL;
 				gpus[gpu].status = LIFE_WELL;
+				gpus[gpu].device_last_well = time(NULL);
 			} else if (now.tv_sec - thr->last.tv_sec > 60 && gpus[gpu].status == LIFE_WELL) {
 			} else if (now.tv_sec - thr->last.tv_sec > 60 && gpus[gpu].status == LIFE_WELL) {
 				thr->rolling = thr->cgpu->rolling = 0;
 				thr->rolling = thr->cgpu->rolling = 0;
 				gpus[gpu].status = LIFE_SICK;
 				gpus[gpu].status = LIFE_SICK;
 				applog(LOG_ERR, "Device %d idle for more than 60 seconds, GPU %d declared SICK!", i, gpu);
 				applog(LOG_ERR, "Device %d idle for more than 60 seconds, GPU %d declared SICK!", i, gpu);
 				gettimeofday(&thr->sick, NULL);
 				gettimeofday(&thr->sick, NULL);
+
+				gpus[gpu].device_last_not_well = time(NULL);
+				gpus[gpu].device_not_well_reason = REASON_DEV_SICK_IDLE_60;
+				gpus[gpu].dev_sick_idle_60_count++;
 #ifdef HAVE_ADL
 #ifdef HAVE_ADL
 				if (adl_active && gpus[gpu].has_adl && gpu_activity(gpu) > 50) {
 				if (adl_active && gpus[gpu].has_adl && gpu_activity(gpu) > 50) {
 					applog(LOG_ERR, "GPU still showing activity suggesting a hard hang.");
 					applog(LOG_ERR, "GPU still showing activity suggesting a hard hang.");
@@ -3957,6 +4066,10 @@ static void *watchdog_thread(void __maybe_unused *userdata)
 				gpus[gpu].status = LIFE_DEAD;
 				gpus[gpu].status = LIFE_DEAD;
 				applog(LOG_ERR, "Device %d not responding for more than 10 minutes, GPU %d declared DEAD!", i, gpu);
 				applog(LOG_ERR, "Device %d not responding for more than 10 minutes, GPU %d declared DEAD!", i, gpu);
 				gettimeofday(&thr->sick, NULL);
 				gettimeofday(&thr->sick, NULL);
+
+				gpus[gpu].device_last_not_well = time(NULL);
+				gpus[gpu].device_not_well_reason = REASON_DEV_DEAD_IDLE_600;
+				gpus[gpu].dev_dead_idle_600_count++;
 			} else if (now.tv_sec - thr->sick.tv_sec > 60 &&
 			} else if (now.tv_sec - thr->sick.tv_sec > 60 &&
 				   (gpus[i].status == LIFE_SICK || gpus[i].status == LIFE_DEAD)) {
 				   (gpus[i].status == LIFE_SICK || gpus[i].status == LIFE_DEAD)) {
 				/* Attempt to restart a GPU that's sick or dead once every minute */
 				/* Attempt to restart a GPU that's sick or dead once every minute */
@@ -3980,8 +4093,8 @@ static void log_print_status(struct cgpu_info *cgpu)
 {
 {
 	char logline[255];
 	char logline[255];
 
 
-		get_statline(logline, cgpu);
-		applog(LOG_WARNING, "%s", logline);
+	get_statline(logline, cgpu);
+	applog(LOG_WARNING, "%s", logline);
 }
 }
 
 
 static void print_summary(void)
 static void print_summary(void)
@@ -4067,6 +4180,9 @@ static void clean_up(void)
 #ifdef HAVE_OPENCL
 #ifdef HAVE_OPENCL
 	clear_adl(nDevs);
 	clear_adl(nDevs);
 #endif
 #endif
+#ifdef HAVE_LIBUSB
+        libusb_exit(NULL);
+#endif
 
 
 	gettimeofday(&total_tv_end, NULL);
 	gettimeofday(&total_tv_end, NULL);
 #ifdef HAVE_CURSES
 #ifdef HAVE_CURSES
@@ -4095,6 +4211,13 @@ void quit(int status, const char *format, ...)
 	fprintf(stderr, "\n");
 	fprintf(stderr, "\n");
 	fflush(stderr);
 	fflush(stderr);
 
 
+#if defined(unix)
+	if (forkpid > 0) {
+		kill(forkpid, SIGTERM);
+		forkpid = 0;
+	}
+#endif
+
 	exit(status);
 	exit(status);
 }
 }
 
 
@@ -4239,14 +4362,14 @@ out:
 		}
 		}
 
 
 		// Fork a child process
 		// Fork a child process
-		r = fork();
-		if (r<0) {
+		forkpid = fork();
+		if (forkpid<0) {
 			perror("fork - failed to fork child process for --monitor");
 			perror("fork - failed to fork child process for --monitor");
 			exit(1);
 			exit(1);
 		}
 		}
 
 
 		// Child: launch monitor command
 		// Child: launch monitor command
-		if (0==r) {
+		if (0==forkpid) {
 			// Make stdin read end of pipe
 			// Make stdin read end of pipe
 			r = dup2(pfd[0], 0);
 			r = dup2(pfd[0], 0);
 			if (r<0) {
 			if (r<0) {
@@ -4295,6 +4418,7 @@ void enable_curses(void) {
 	cbreak();
 	cbreak();
 	noecho();
 	noecho();
 	curses_active = true;
 	curses_active = true;
+	statusy = logstart;
 	unlock_curses();
 	unlock_curses();
 }
 }
 #endif
 #endif
@@ -4315,6 +4439,10 @@ extern struct device_api bitforce_api;
 extern struct device_api icarus_api;
 extern struct device_api icarus_api;
 #endif
 #endif
 
 
+#ifdef USE_ZTEX
+extern struct device_api ztex_api;
+#endif
+
 
 
 static int cgminer_id_count = 0;
 static int cgminer_id_count = 0;
 
 
@@ -4355,7 +4483,7 @@ bool add_cgpu(struct cgpu_info*cgpu)
 	return true;
 	return true;
 }
 }
 
 
-int main (int argc, char *argv[])
+int main(int argc, char *argv[])
 {
 {
 	struct block *block, *tmpblock;
 	struct block *block, *tmpblock;
 	struct work *work, *tmpwork;
 	struct work *work, *tmpwork;
@@ -4370,6 +4498,14 @@ int main (int argc, char *argv[])
 	if (unlikely(curl_global_init(CURL_GLOBAL_ALL)))
 	if (unlikely(curl_global_init(CURL_GLOBAL_ALL)))
 		quit(1, "Failed to curl_global_init");
 		quit(1, "Failed to curl_global_init");
 
 
+	initial_args = malloc(sizeof(char *) * (argc + 1));
+	for  (i = 0; i < argc; i++)
+		initial_args[i] = strdup(argv[i]);
+	initial_args[argc] = NULL;
+#ifdef HAVE_LIBUSB
+        libusb_init(NULL);
+#endif
+
 	mutex_init(&hash_lock);
 	mutex_init(&hash_lock);
 	mutex_init(&qd_lock);
 	mutex_init(&qd_lock);
 #ifdef HAVE_CURSES
 #ifdef HAVE_CURSES
@@ -4432,13 +4568,13 @@ int main (int argc, char *argv[])
 	opt_register_table(opt_cmdline_table,
 	opt_register_table(opt_cmdline_table,
 			   "Options for command line only");
 			   "Options for command line only");
 
 
-	if (!config_loaded)
-		load_default_config();
-
 	opt_parse(&argc, argv, applog_and_exit);
 	opt_parse(&argc, argv, applog_and_exit);
 	if (argc != 1)
 	if (argc != 1)
 		quit(1, "Unexpected extra commandline arguments");
 		quit(1, "Unexpected extra commandline arguments");
 
 
+	if (!config_loaded)
+		load_default_config();
+
 	if (opt_benchmark) {
 	if (opt_benchmark) {
 		struct pool *pool;
 		struct pool *pool;
 
 
@@ -4465,6 +4601,23 @@ int main (int argc, char *argv[])
 #endif
 #endif
 
 
 	applog(LOG_WARNING, "Started %s", packagename);
 	applog(LOG_WARNING, "Started %s", packagename);
+	if (cnfbuf) {
+		applog(LOG_NOTICE, "Loaded configuration file %s", cnfbuf);
+		switch (fileconf_load) {
+			case 0:
+				applog(LOG_WARNING, "Fatal JSON error in configuration file.");
+				applog(LOG_WARNING, "Configuration file could not be used.");
+				break;
+			case -1:
+				applog(LOG_WARNING, "Error in configuration file, partially loaded.");
+				applog(LOG_WARNING, "Start cgminer with -T to see what failed to load.");
+				break;
+			default:
+				break;
+		}
+		free(cnfbuf);
+		cnfbuf = NULL;
+	}
 
 
 	strcat(opt_kernel_path, "/");
 	strcat(opt_kernel_path, "/");
 
 
@@ -4522,6 +4675,10 @@ int main (int argc, char *argv[])
 	icarus_api.api_detect();
 	icarus_api.api_detect();
 #endif
 #endif
 
 
+#ifdef USE_ZTEX
+	ztex_api.api_detect();
+#endif
+
 #ifdef WANT_CPUMINE
 #ifdef WANT_CPUMINE
 	cpu_api.api_detect();
 	cpu_api.api_detect();
 #endif
 #endif
@@ -4842,5 +4999,12 @@ begin_bench:
 		free(block);
 		free(block);
 	}
 	}
 
 
+#if defined(unix)
+	if (forkpid > 0) {
+		kill(forkpid, SIGTERM);
+		forkpid = 0;
+	}
+#endif
+
 	return 0;
 	return 0;
 }
 }

+ 37 - 10
configure.ac

@@ -2,7 +2,7 @@
 ##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##
 ##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##
 m4_define([v_maj], [2])
 m4_define([v_maj], [2])
 m4_define([v_min], [3])
 m4_define([v_min], [3])
-m4_define([v_mic], [1])
+m4_define([v_mic], [3])
 ##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##
 ##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##--##
 m4_define([v_ver], [v_maj.v_min.v_mic])
 m4_define([v_ver], [v_maj.v_min.v_mic])
 m4_define([lt_rev], m4_eval(v_maj + v_min))
 m4_define([lt_rev], m4_eval(v_maj + v_min))
@@ -57,11 +57,14 @@ gl_INIT
 dnl Checks for header files.
 dnl Checks for header files.
 AC_HEADER_STDC
 AC_HEADER_STDC
 AC_CHECK_HEADERS(syslog.h)
 AC_CHECK_HEADERS(syslog.h)
+AC_CHECK_HEADERS([sys/epoll.h])
 
 
 AC_FUNC_ALLOCA
 AC_FUNC_ALLOCA
 
 
 have_win32=false
 have_win32=false
 PTHREAD_FLAGS="-lpthread"
 PTHREAD_FLAGS="-lpthread"
+USB_LIBS=""
+USB_FLAGS=""
 DLOPEN_FLAGS="-ldl"
 DLOPEN_FLAGS="-ldl"
 OPENCL_LIBS="-lOpenCL"
 OPENCL_LIBS="-lOpenCL"
 WS2_LIBS=""
 WS2_LIBS=""
@@ -207,6 +210,16 @@ if test "x$icarus" = xyes; then
 fi
 fi
 AM_CONDITIONAL([HAS_ICARUS], [test x$icarus = xyes])
 AM_CONDITIONAL([HAS_ICARUS], [test x$icarus = xyes])
 
 
+ztex="no"
+
+AC_ARG_ENABLE([ztex],
+	[AC_HELP_STRING([--enable-ztex],[Compile support for Ztex (default disabled)])],
+	[ztex=$enableval]
+	)
+if test "x$ztex" = xyes; then
+	AC_DEFINE([USE_ZTEX], [1], [Defined to 1 if Ztex support is wanted])
+fi
+AM_CONDITIONAL([HAS_ZTEX], [test x$ztex = xyes])
 
 
 curses="auto"
 curses="auto"
 
 
@@ -301,6 +314,14 @@ if test "x$bitforce" != xno; then
 fi
 fi
 AM_CONDITIONAL([HAVE_LIBUDEV], [test x$libudev != xno])
 AM_CONDITIONAL([HAVE_LIBUDEV], [test x$libudev != xno])
 
 
+if test "x$ztex" != xno; then
+  AC_CHECK_LIB(usb-1.0, libusb_init, ,
+          AC_MSG_ERROR([Could not find usb library - please install libusb]))
+  AC_DEFINE([HAVE_LIBUSB], [1], [Defined to 1 if libusb is wanted])
+  USB_LIBS="-lusb-1.0"
+  USB_FLAGS=""
+fi
+
 PKG_PROG_PKG_CONFIG()
 PKG_PROG_PKG_CONFIG()
 
 
 PKG_CHECK_MODULES([LIBCURL], [libcurl >= 7.15.6], [AC_DEFINE([CURL_HAS_SOCKOPT], [1], [Defined if version of curl supports sockopts.])],
 PKG_CHECK_MODULES([LIBCURL], [libcurl >= 7.15.6], [AC_DEFINE([CURL_HAS_SOCKOPT], [1], [Defined if version of curl supports sockopts.])],
@@ -346,9 +367,9 @@ fi
 AC_DEFINE_UNQUOTED([CGMINER_PREFIX], ["$prefix/bin"], [Path to cgminer install])
 AC_DEFINE_UNQUOTED([CGMINER_PREFIX], ["$prefix/bin"], [Path to cgminer install])
 
 
 AC_DEFINE_UNQUOTED([PHATK_KERNNAME], ["phatk120223"], [Filename for phatk kernel])
 AC_DEFINE_UNQUOTED([PHATK_KERNNAME], ["phatk120223"], [Filename for phatk kernel])
-AC_DEFINE_UNQUOTED([POCLBM_KERNNAME], ["poclbm120222"], [Filename for poclbm kernel])
+AC_DEFINE_UNQUOTED([POCLBM_KERNNAME], ["poclbm120327"], [Filename for poclbm kernel])
 AC_DEFINE_UNQUOTED([DIAKGCN_KERNNAME], ["diakgcn120223"], [Filename for diakgcn kernel])
 AC_DEFINE_UNQUOTED([DIAKGCN_KERNNAME], ["diakgcn120223"], [Filename for diakgcn kernel])
-AC_DEFINE_UNQUOTED([DIABLO_KERNNAME], ["diablo120222"], [Filename for diablo kernel])
+AC_DEFINE_UNQUOTED([DIABLO_KERNNAME], ["diablo120328"], [Filename for diablo kernel])
 
 
 
 
 AC_SUBST(OPENCL_LIBS)
 AC_SUBST(OPENCL_LIBS)
@@ -362,6 +383,8 @@ AC_SUBST(PDCURSES_LIBS)
 AC_SUBST(WS2_LIBS)
 AC_SUBST(WS2_LIBS)
 AC_SUBST(MATH_LIBS)
 AC_SUBST(MATH_LIBS)
 AC_SUBST(UDEV_LIBS)
 AC_SUBST(UDEV_LIBS)
+AC_SUBST(USB_LIBS)
+AC_SUBST(USB_FLAGS)
 
 
 AC_CONFIG_FILES([
 AC_CONFIG_FILES([
 	Makefile
 	Makefile
@@ -393,13 +416,13 @@ if test "x$opencl" != xno; then
 		echo "  OpenCL...............: FOUND. GPU mining support enabled"
 		echo "  OpenCL...............: FOUND. GPU mining support enabled"
 	else
 	else
 		echo "  OpenCL...............: NOT FOUND. GPU mining support DISABLED"
 		echo "  OpenCL...............: NOT FOUND. GPU mining support DISABLED"
-		if test "x$cpumining$bitforce$icarus" = xnonono; then
+		if test "x$cpumining$bitforce$icarus$ztex" = xnononono; then
 			AC_MSG_ERROR([No mining configured in])
 			AC_MSG_ERROR([No mining configured in])
 		fi
 		fi
 	fi
 	fi
 else
 else
 	echo "  OpenCL...............: Detection overrided. GPU mining support DISABLED"
 	echo "  OpenCL...............: Detection overrided. GPU mining support DISABLED"
-	if test "x$cpumining$bitforce$icarus" = xnonono; then
+	if test "x$cpumining$bitforce$icarus$ztex" = xnononono; then
 		AC_MSG_ERROR([No mining configured in])
 		AC_MSG_ERROR([No mining configured in])
 	fi
 	fi
 fi
 fi
@@ -427,24 +450,28 @@ else
 	echo "  Icarus.FPGAs.........: Disabled"
 	echo "  Icarus.FPGAs.........: Disabled"
 fi
 fi
 
 
+if test "x$ztex" = xyes; then
+	echo "  Ztex.FPGAs...........: Enabled"
+else
+	echo "  Ztex.FPGAs...........: Disabled"
+fi
+
 if test "x$bitforce" != xno; then
 if test "x$bitforce" != xno; then
 	echo "  libudev.detection....: $libudev"
 	echo "  libudev.detection....: $libudev"
 fi
 fi
 
 
-echo
 if test "x$cpumining" = xyes; then
 if test "x$cpumining" = xyes; then
+	echo
 	echo "  CPU Mining...........: Enabled"
 	echo "  CPU Mining...........: Enabled"
 	echo "  ASM.(for CPU mining).: $has_yasm"
 	echo "  ASM.(for CPU mining).: $has_yasm"
-else
-	echo "  CPU Mining...........: Disabled"
 fi
 fi
 
 
 echo
 echo
 echo "Compilation............: make (or gmake)"
 echo "Compilation............: make (or gmake)"
 echo "  CPPFLAGS.............: $CPPFLAGS"
 echo "  CPPFLAGS.............: $CPPFLAGS"
 echo "  CFLAGS...............: $CFLAGS"
 echo "  CFLAGS...............: $CFLAGS"
-echo "  LDFLAGS..............: $LDFLAGS $PTHREAD_FLAGS"
-echo "  LDADD................: $DLOPEN_FLAGS $LIBCURL_LIBS $JANSSON_LIBS $PTHREAD_LIBS $OPENCL_LIBS $NCURSES_LIBS $PDCURSES_LIBS $WS2_LIBS $MATH_LIBS $UDEV_LIBS"
+echo "  LDFLAGS..............: $LDFLAGS $PTHREAD_FLAGS $USB_FLAGS"
+echo "  LDADD................: $DLOPEN_FLAGS $LIBCURL_LIBS $JANSSON_LIBS $PTHREAD_LIBS $OPENCL_LIBS $NCURSES_LIBS $PDCURSES_LIBS $WS2_LIBS $MATH_LIBS $UDEV_LIBS $USB_LIBS"
 echo
 echo
 echo "Installation...........: make install (as root if needed, with 'su' or 'sudo')"
 echo "Installation...........: make install (as root if needed, with 'su' or 'sudo')"
 echo "  prefix...............: $prefix"
 echo "  prefix...............: $prefix"

+ 10 - 1
diablo120222.cl → diablo120328.cl

@@ -44,8 +44,13 @@
 #define ZR26(n) ((Zrotr((n), 26) ^ Zrotr((n), 21) ^ Zrotr((n), 7)))
 #define ZR26(n) ((Zrotr((n), 26) ^ Zrotr((n), 21) ^ Zrotr((n), 7)))
 #define ZR30(n) ((Zrotr((n), 30) ^ Zrotr((n), 19) ^ Zrotr((n), 10)))
 #define ZR30(n) ((Zrotr((n), 30) ^ Zrotr((n), 19) ^ Zrotr((n), 10)))
 
 
-__kernel __attribute__((reqd_work_group_size(WORKSIZE, 1, 1))) void search(
+__kernel
+__attribute__((vec_type_hint(z)))
+__attribute__((reqd_work_group_size(WORKSIZE, 1, 1)))
+void search(
+#ifndef GOFFSET
     const z base,
     const z base,
+#endif
     const uint PreVal4_state0, const uint PreVal4_state0_k7,
     const uint PreVal4_state0, const uint PreVal4_state0_k7,
     const uint PreVal4_T1,
     const uint PreVal4_T1,
     const uint W18, const uint W19,
     const uint W18, const uint W19,
@@ -62,7 +67,11 @@ __kernel __attribute__((reqd_work_group_size(WORKSIZE, 1, 1))) void search(
 
 
   z ZA[930];
   z ZA[930];
 
 
+#ifdef GOFFSET
+	const z Znonce = (uint)(get_global_id(0));
+#else
 	const z Znonce = base + (uint)(get_global_id(0));
 	const z Znonce = base + (uint)(get_global_id(0));
+#endif
 
 
     ZA[15] = Znonce + PreVal4_state0;
     ZA[15] = Znonce + PreVal4_state0;
     
     

+ 7 - 0
driver-bitforce.c

@@ -216,6 +216,9 @@ static void bitforce_detect()
 			s += 9;
 			s += 9;
 		if (!strcmp(s, "auto"))
 		if (!strcmp(s, "auto"))
 			autoscan = true;
 			autoscan = true;
+		else
+		if (!strcmp(s, "noauto"))
+			found = true;
 		else if (bitforce_detect_one(s)) {
 		else if (bitforce_detect_one(s)) {
 			string_elist_del(iter);
 			string_elist_del(iter);
 			found = true;
 			found = true;
@@ -312,6 +315,10 @@ static uint64_t bitforce_scanhash(struct thr_info *thr, struct work *work, uint6
 			if (temp > bitforce->cutofftemp) {
 			if (temp > bitforce->cutofftemp) {
 				applog(LOG_WARNING, "Hit thermal cutoff limit on %s %d, disabling!", bitforce->api->name, bitforce->device_id);
 				applog(LOG_WARNING, "Hit thermal cutoff limit on %s %d, disabling!", bitforce->api->name, bitforce->device_id);
 				bitforce->deven = DEV_RECOVER;
 				bitforce->deven = DEV_RECOVER;
+
+				bitforce->device_last_not_well = time(NULL);
+				bitforce->device_not_well_reason = REASON_DEV_THERMAL_CUTOFF;
+				bitforce->dev_thermal_cutoff_count++;
 			}
 			}
 		}
 		}
 	}
 	}

+ 1 - 1
driver-cpu.c

@@ -39,7 +39,7 @@
 	#include <fcntl.h>
 	#include <fcntl.h>
 #endif
 #endif
 
 
-#ifdef __linux /* Linux specific policy and affinity management */
+#if defined(__linux) && defined(cpu_set_t) /* Linux specific policy and affinity management */
 #include <sched.h>
 #include <sched.h>
 static inline void drop_policy(void)
 static inline void drop_policy(void)
 {
 {

+ 80 - 29
driver-icarus.c

@@ -46,11 +46,17 @@
   #include <windows.h>
   #include <windows.h>
   #include <io.h>
   #include <io.h>
 #endif
 #endif
+#ifdef HAVE_SYS_EPOLL_H
+  #include <sys/epoll.h>
+  #define HAVE_EPOLL
+#endif
 
 
 #include "elist.h"
 #include "elist.h"
 #include "miner.h"
 #include "miner.h"
 
 
-#define ICARUS_READ_FAULT_COUNT	(8)
+// 8 second timeout
+#define ICARUS_READ_FAULT_DECISECONDS (1)
+#define ICARUS_READ_FAULT_COUNT	(80)
 
 
 struct device_api icarus_api;
 struct device_api icarus_api;
 
 
@@ -87,7 +93,7 @@ static int icarus_open(const char *devpath)
 				ISTRIP | INLCR | IGNCR | ICRNL | IXON);
 				ISTRIP | INLCR | IGNCR | ICRNL | IXON);
 	my_termios.c_oflag &= ~OPOST;
 	my_termios.c_oflag &= ~OPOST;
 	my_termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
 	my_termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
-	my_termios.c_cc[VTIME] = 10; /* block 1 second */
+	my_termios.c_cc[VTIME] = ICARUS_READ_FAULT_DECISECONDS;
 	my_termios.c_cc[VMIN] = 0;
 	my_termios.c_cc[VMIN] = 0;
 	tcsetattr(serialfd, TCSANOW, &my_termios);
 	tcsetattr(serialfd, TCSANOW, &my_termios);
 
 
@@ -100,17 +106,39 @@ static int icarus_open(const char *devpath)
 				    NULL, OPEN_EXISTING, 0, NULL);
 				    NULL, OPEN_EXISTING, 0, NULL);
 	if (unlikely(hSerial == INVALID_HANDLE_VALUE))
 	if (unlikely(hSerial == INVALID_HANDLE_VALUE))
 		return -1;
 		return -1;
-	/* TODO: Needs setup read block time. just like VTIME = 10 */
+
+	COMMTIMEOUTS cto = {1000, 0, 1000, 0, 1000};
+	SetCommTimeouts(hSerial, &cto);
+
 	return _open_osfhandle((LONG)hSerial, 0);
 	return _open_osfhandle((LONG)hSerial, 0);
 #endif
 #endif
 }
 }
 
 
-static int icarus_gets(unsigned char *buf, size_t bufLen, int fd)
+static int icarus_gets(unsigned char *buf, size_t bufLen, int fd, volatile unsigned long *wr)
 {
 {
 	ssize_t ret = 0;
 	ssize_t ret = 0;
 	int rc = 0;
 	int rc = 0;
+	int epollfd = -1;
+
+#ifdef HAVE_EPOLL
+	struct epoll_event ev, evr;
+	epollfd = epoll_create(1);
+	if (epollfd != -1) {
+		ev.events = EPOLLIN;
+		ev.data.fd = fd;
+		if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev)) {
+			close(epollfd);
+			epollfd = -1;
+		}
+	}
+#endif
 
 
 	while (bufLen) {
 	while (bufLen) {
+#ifdef HAVE_EPOLL
+		if (epollfd != -1 && epoll_wait(epollfd, &evr, 1, ICARUS_READ_FAULT_DECISECONDS * 100) != 1)
+			ret = 0;
+		else
+#endif
 		ret = read(fd, buf, 1);
 		ret = read(fd, buf, 1);
 		if (ret == 1) {
 		if (ret == 1) {
 			bufLen--;
 			bufLen--;
@@ -119,13 +147,20 @@ static int icarus_gets(unsigned char *buf, size_t bufLen, int fd)
 		}
 		}
 
 
 		rc++;
 		rc++;
+		if (*wr)
+			return 1;
 		if (rc == ICARUS_READ_FAULT_COUNT) {
 		if (rc == ICARUS_READ_FAULT_COUNT) {
-			applog(LOG_WARNING,
-			       "Icarus Read: No data in %d seconds", rc);
+			if (epollfd != -1)
+				close(epollfd);
+			applog(LOG_DEBUG,
+			       "Icarus Read: No data in %d seconds", rc * ICARUS_READ_FAULT_DECISECONDS / 10);
 			return 1;
 			return 1;
 		}
 		}
 	}
 	}
 
 
+	if (epollfd != -1)
+		close(epollfd);
+
 	return 0;
 	return 0;
 }
 }
 
 
@@ -169,7 +204,8 @@ static bool icarus_detect_one(const char *devpath)
 	icarus_write(fd, ob_bin, sizeof(ob_bin));
 	icarus_write(fd, ob_bin, sizeof(ob_bin));
 
 
 	memset(nonce_bin, 0, sizeof(nonce_bin));
 	memset(nonce_bin, 0, sizeof(nonce_bin));
-	icarus_gets(nonce_bin, sizeof(nonce_bin), fd);
+	volatile unsigned long wr = 0;
+	icarus_gets(nonce_bin, sizeof(nonce_bin), fd, &wr);
 
 
 	icarus_close(fd);
 	icarus_close(fd);
 
 
@@ -210,6 +246,8 @@ static void icarus_detect()
 		s = iter->string;
 		s = iter->string;
 		if (!strncmp("icarus:", iter->string, 7))
 		if (!strncmp("icarus:", iter->string, 7))
 			s += 7;
 			s += 7;
+		if (!strcmp(s, "auto") || !strcmp(s, "noauto"))
+			continue;
 		if (icarus_detect_one(s))
 		if (icarus_detect_one(s))
 			string_elist_del(iter);
 			string_elist_del(iter);
 	}
 	}
@@ -221,15 +259,6 @@ static bool icarus_prepare(struct thr_info *thr)
 
 
 	struct timeval now;
 	struct timeval now;
 
 
-	int fd = icarus_open(icarus->device_path);
-	if (unlikely(-1 == fd)) {
-		applog(LOG_ERR, "Failed to open Icarus on %s",
-		       icarus->device_path);
-		return false;
-	}
-
-	icarus->device_fd = fd;
-
 	applog(LOG_INFO, "Opened Icarus on %s", icarus->device_path);
 	applog(LOG_INFO, "Opened Icarus on %s", icarus->device_path);
 	gettimeofday(&now, NULL);
 	gettimeofday(&now, NULL);
 	get_datestamp(icarus->init, &now);
 	get_datestamp(icarus->init, &now);
@@ -240,6 +269,8 @@ static bool icarus_prepare(struct thr_info *thr)
 static uint64_t icarus_scanhash(struct thr_info *thr, struct work *work,
 static uint64_t icarus_scanhash(struct thr_info *thr, struct work *work,
 				__maybe_unused uint64_t max_nonce)
 				__maybe_unused uint64_t max_nonce)
 {
 {
+	volatile unsigned long *wr = &work_restart[thr->id].restart;
+
 	struct cgpu_info *icarus;
 	struct cgpu_info *icarus;
 	int fd;
 	int fd;
 	int ret;
 	int ret;
@@ -248,10 +279,16 @@ static uint64_t icarus_scanhash(struct thr_info *thr, struct work *work,
 	char *ob_hex, *nonce_hex;
 	char *ob_hex, *nonce_hex;
 	uint32_t nonce;
 	uint32_t nonce;
 	uint32_t hash_count;
 	uint32_t hash_count;
-	time_t t = 0;
+	struct timeval tv_start, tv_end, diff;
 
 
 	icarus = thr->cgpu;
 	icarus = thr->cgpu;
-	fd = icarus->device_fd;
+
+	fd = icarus_open(icarus->device_path);
+	if (unlikely(-1 == fd)) {
+		applog(LOG_ERR, "Failed to open Icarus on %s",
+		       icarus->device_path);
+		return 0;
+	}
 
 
 	memset(ob_bin, 0, sizeof(ob_bin));
 	memset(ob_bin, 0, sizeof(ob_bin));
 	memcpy(ob_bin, work->midstate, 32);
 	memcpy(ob_bin, work->midstate, 32);
@@ -261,13 +298,17 @@ static uint64_t icarus_scanhash(struct thr_info *thr, struct work *work,
 #ifndef WIN32
 #ifndef WIN32
 	tcflush(fd, TCOFLUSH);
 	tcflush(fd, TCOFLUSH);
 #endif
 #endif
+
+	gettimeofday(&tv_start, NULL);
+
 	ret = icarus_write(fd, ob_bin, sizeof(ob_bin));
 	ret = icarus_write(fd, ob_bin, sizeof(ob_bin));
-	if (ret)
+	if (ret) {
+		icarus_close(fd);
 		return 0;	/* This should never happen */
 		return 0;	/* This should never happen */
+	}
 
 
 	ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
 	ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
 	if (ob_hex) {
 	if (ob_hex) {
-		t = time(NULL);
 		applog(LOG_DEBUG, "Icarus %s send: %s",
 		applog(LOG_DEBUG, "Icarus %s send: %s",
 		       icarus->device_id, ob_hex);
 		       icarus->device_id, ob_hex);
 		free(ob_hex);
 		free(ob_hex);
@@ -275,25 +316,35 @@ static uint64_t icarus_scanhash(struct thr_info *thr, struct work *work,
 
 
 	/* Icarus will return 8 bytes nonces or nothing */
 	/* Icarus will return 8 bytes nonces or nothing */
 	memset(nonce_bin, 0, sizeof(nonce_bin));
 	memset(nonce_bin, 0, sizeof(nonce_bin));
-	ret = icarus_gets(nonce_bin, sizeof(nonce_bin), fd);
+	ret = icarus_gets(nonce_bin, sizeof(nonce_bin), fd, wr);
+
+	gettimeofday(&tv_end, NULL);
+	timeval_subtract(&diff, &tv_end, &tv_start);
 
 
 	nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
 	nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
 	if (nonce_hex) {
 	if (nonce_hex) {
-		t = time(NULL) - t;
-		applog(LOG_DEBUG, "Icarus %d return (elapse %d seconds): %s",
-		       icarus->device_id, t, nonce_hex);
+		applog(LOG_DEBUG, "Icarus %d returned (in %d.%06d seconds): %s",
+		       icarus->device_id, diff.tv_sec, diff.tv_usec, nonce_hex);
 		free(nonce_hex);
 		free(nonce_hex);
 	}
 	}
 
 
 	memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
 	memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
 
 
-        if (nonce == 0 && ret)
-                return 0xffffffff;
+	work->blk.nonce = 0xffffffff;
+	icarus_close(fd);
+
+	if (nonce == 0 && ret) {
+		if (unlikely(diff.tv_sec > 12 || (diff.tv_sec == 11 && diff.tv_usec > 300067)))
+			return 0xffffffff;
+		// Approximately how much of the nonce Icarus scans in 1 second...
+		// 0x16a7a561 would be if it was exactly 380 MH/s
+		// 0x168b7b4b was the average over a 201-sample period based on time to find actual shares
+		return (0x168b7b4b * diff.tv_sec) + (0x17a * diff.tv_usec);
+	}
 
 
 #ifndef __BIG_ENDIAN__
 #ifndef __BIG_ENDIAN__
 	nonce = swab32(nonce);
 	nonce = swab32(nonce);
 #endif
 #endif
-	work->blk.nonce = 0xffffffff;
 	submit_nonce(thr, work, nonce);
 	submit_nonce(thr, work, nonce);
 
 
 	hash_count = (nonce & 0x7fffffff);
 	hash_count = (nonce & 0x7fffffff);
@@ -306,6 +357,8 @@ static uint64_t icarus_scanhash(struct thr_info *thr, struct work *work,
                         hash_count <<= 1;
                         hash_count <<= 1;
         }
         }
 
 
+	applog(LOG_DEBUG, "0x%x hashes in %d.%06d seconds", hash_count, diff.tv_sec, diff.tv_usec);
+
         return hash_count;
         return hash_count;
 }
 }
 
 
@@ -319,8 +372,6 @@ static void icarus_shutdown(struct thr_info *thr)
 		if (icarus->device_path)
 		if (icarus->device_path)
 			free(icarus->device_path);
 			free(icarus->device_path);
 
 
-		close(icarus->device_fd);
-
 		devices[icarus->device_id] = NULL;
 		devices[icarus->device_id] = NULL;
 		free(icarus);
 		free(icarus);
 
 

+ 41 - 18
driver-opencl.c

@@ -751,10 +751,8 @@ static _clState *clStates[MAX_GPUDEVICES];
 static cl_int queue_poclbm_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint threads)
 static cl_int queue_poclbm_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint threads)
 {
 {
 	cl_kernel *kernel = &clState->kernel;
 	cl_kernel *kernel = &clState->kernel;
-	cl_uint vwidth = clState->vwidth;
-	unsigned int i, num = 0;
+	unsigned int num = 0;
 	cl_int status = 0;
 	cl_int status = 0;
-	uint *nonces;
 
 
 	CL_SET_BLKARG(ctx_a);
 	CL_SET_BLKARG(ctx_a);
 	CL_SET_BLKARG(ctx_b);
 	CL_SET_BLKARG(ctx_b);
@@ -773,10 +771,15 @@ static cl_int queue_poclbm_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint t
 	CL_SET_BLKARG(cty_g);
 	CL_SET_BLKARG(cty_g);
 	CL_SET_BLKARG(cty_h);
 	CL_SET_BLKARG(cty_h);
 
 
-	nonces = alloca(sizeof(uint) * vwidth);
-	for (i = 0; i < vwidth; i++)
-		nonces[i] = blk->nonce + (i * threads);
-	CL_SET_VARG(vwidth, nonces);
+	if (!clState->goffset) {
+		cl_uint vwidth = clState->vwidth;
+		uint *nonces = alloca(sizeof(uint) * vwidth);
+		unsigned int i;
+
+		for (i = 0; i < vwidth; i++)
+			nonces[i] = blk->nonce + (i * threads);
+		CL_SET_VARG(vwidth, nonces);
+	}
 
 
 	CL_SET_BLKARG(fW0);
 	CL_SET_BLKARG(fW0);
 	CL_SET_BLKARG(fW1);
 	CL_SET_BLKARG(fW1);
@@ -785,7 +788,6 @@ static cl_int queue_poclbm_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint t
 	CL_SET_BLKARG(fW15);
 	CL_SET_BLKARG(fW15);
 	CL_SET_BLKARG(fW01r);
 	CL_SET_BLKARG(fW01r);
 
 
-	CL_SET_BLKARG(fcty_e2);
 	CL_SET_BLKARG(D1A);
 	CL_SET_BLKARG(D1A);
 	CL_SET_BLKARG(C1addK5);
 	CL_SET_BLKARG(C1addK5);
 	CL_SET_BLKARG(B1addK6);
 	CL_SET_BLKARG(B1addK6);
@@ -905,15 +907,19 @@ static cl_int queue_diakgcn_kernel(_clState *clState, dev_blk_ctx *blk,
 static cl_int queue_diablo_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint threads)
 static cl_int queue_diablo_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint threads)
 {
 {
 	cl_kernel *kernel = &clState->kernel;
 	cl_kernel *kernel = &clState->kernel;
-	cl_uint vwidth = clState->vwidth;
-	unsigned int i, num = 0;
+	unsigned int num = 0;
 	cl_int status = 0;
 	cl_int status = 0;
-	uint *nonces;
 
 
-	nonces = alloca(sizeof(uint) * vwidth);
-	for (i = 0; i < vwidth; i++)
-		nonces[i] = blk->nonce + (i * threads);
-	CL_SET_VARG(vwidth, nonces);
+	if (!clState->goffset) {
+		cl_uint vwidth = clState->vwidth;
+		uint *nonces = alloca(sizeof(uint) * vwidth);
+		unsigned int i;
+
+		for (i = 0; i < vwidth; i++)
+			nonces[i] = blk->nonce + (i * threads);
+		CL_SET_VARG(vwidth, nonces);
+	}
+
 
 
 	CL_SET_BLKARG(PreVal0);
 	CL_SET_BLKARG(PreVal0);
 	CL_SET_BLKARG(PreVal0addK7);
 	CL_SET_BLKARG(PreVal0addK7);
@@ -1174,6 +1180,7 @@ static bool opencl_thread_prepare(struct thr_info *thr)
 		return false;
 		return false;
 	}
 	}
 
 
+	strcpy(name, "");
 	applog(LOG_INFO, "Init GPU thread %i GPU %i virtual GPU %i", i, gpu, virtual_gpu);
 	applog(LOG_INFO, "Init GPU thread %i GPU %i virtual GPU %i", i, gpu, virtual_gpu);
 	clStates[i] = initCl(virtual_gpu, name, sizeof(name));
 	clStates[i] = initCl(virtual_gpu, name, sizeof(name));
 	if (!clStates[i]) {
 	if (!clStates[i]) {
@@ -1196,9 +1203,14 @@ static bool opencl_thread_prepare(struct thr_info *thr)
 		}
 		}
 		cgpu->deven = DEV_DISABLED;
 		cgpu->deven = DEV_DISABLED;
 		cgpu->status = LIFE_NOSTART;
 		cgpu->status = LIFE_NOSTART;
+
+		cgpu->device_last_not_well = time(NULL);
+		cgpu->device_not_well_reason = REASON_DEV_NOSTART;
+		cgpu->dev_nostart_count++;
+
 		return false;
 		return false;
 	}
 	}
-	if (name && !cgpu->name)
+	if (!cgpu->name)
 		cgpu->name = strdup(name);
 		cgpu->name = strdup(name);
 	if (!cgpu->kname)
 	if (!cgpu->kname)
 	{
 	{
@@ -1215,6 +1227,7 @@ static bool opencl_thread_prepare(struct thr_info *thr)
 		case KL_POCLBM:
 		case KL_POCLBM:
 			cgpu->kname = "poclbm";
 			cgpu->kname = "poclbm";
 		default:
 		default:
+			break;
 		}
 		}
 	}
 	}
 	applog(LOG_INFO, "initCl() finished. Found %s", name);
 	applog(LOG_INFO, "initCl() finished. Found %s", name);
@@ -1274,6 +1287,8 @@ static bool opencl_thread_init(struct thr_info *thr)
 
 
 	gpu->status = LIFE_WELL;
 	gpu->status = LIFE_WELL;
 
 
+	gpu->device_last_well = time(NULL);
+
 	return true;
 	return true;
 }
 }
 
 
@@ -1369,8 +1384,16 @@ static uint64_t opencl_scanhash(struct thr_info *thr, struct work *work,
 		memset(thrdata->res, 0, BUFFERSIZE);
 		memset(thrdata->res, 0, BUFFERSIZE);
 		clFinish(clState->commandQueue);
 		clFinish(clState->commandQueue);
 	}
 	}
-	status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
-			globalThreads, localThreads, 0,  NULL, NULL);
+
+	if (clState->goffset) {
+		size_t global_work_offset[1];
+
+		global_work_offset[0] = work->blk.nonce;
+		status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, global_work_offset,
+						globalThreads, localThreads, 0,  NULL, NULL);
+	} else
+		status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
+						globalThreads, localThreads, 0,  NULL, NULL);
 	if (unlikely(status != CL_SUCCESS)) {
 	if (unlikely(status != CL_SUCCESS)) {
 		applog(LOG_ERR, "Error: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)");
 		applog(LOG_ERR, "Error: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)");
 		return 0;
 		return 0;

+ 322 - 0
driver-ztex.c

@@ -0,0 +1,322 @@
+/**
+ *   ztex.c - cgminer worker for Ztex 1.15x fpga board
+ *
+ *   Copyright (c) 2012 nelisky.btc@gmail.com
+ *
+ *   This work is based upon the Java SDK provided by ztex which is
+ *   Copyright (C) 2009-2011 ZTEX GmbH.
+ *   http://www.ztex.de
+ *
+ *   This work is based upon the icarus.c worker which is
+ *   Copyright 2012 Luke Dashjr
+ *   Copyright 2012 Xiangfu <xiangfu@openmobilefree.com>
+ *
+ *   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License version 2 as
+ *   published by the Free Software Foundation.
+ *
+ *   This program is distributed in the hope that it will be useful, but
+ *   WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ *   General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, see http://www.gnu.org/licenses/.
+**/
+#include <unistd.h>
+#include <sha2.h>
+#include "miner.h"
+#include "libztex.h"
+
+#define GOLDEN_BACKLOG 5
+
+struct device_api ztex_api, ztex_hotplug_api;
+
+// Forward declarations
+static void ztex_disable(struct thr_info* thr);
+static bool ztex_prepare(struct thr_info *thr);
+
+static void ztex_detect(void)
+{
+	int cnt;
+	int i;
+	struct libztex_dev_list **ztex_devices;
+
+	cnt = libztex_scanDevices(&ztex_devices);
+	applog(LOG_WARNING, "Found %d ztex board(s)", cnt);
+
+	for (i = 0; i < cnt; i++) {
+		if (total_devices == MAX_DEVICES)
+			break;
+		struct cgpu_info *ztex;
+		ztex = calloc(1, sizeof(struct cgpu_info));
+		ztex->api = &ztex_api;
+		ztex->device_ztex = ztex_devices[i]->dev;
+		ztex->threads = 1;
+		add_cgpu(ztex);
+
+		applog(LOG_WARNING,"%s: Found Ztex, mark as %d", ztex->device_ztex->repr, ztex->device_id);
+	}
+
+	if (cnt > 0)
+		libztex_freeDevList(ztex_devices);
+}
+
+static bool ztex_updateFreq(struct libztex_device* ztex)
+{
+	int i, maxM, bestM;
+	double bestR, r;
+
+	for (i = 0; i < ztex->freqMaxM; i++)
+		if (ztex->maxErrorRate[i + 1] * i < ztex->maxErrorRate[i] * (i + 20))
+			ztex->maxErrorRate[i + 1] = ztex->maxErrorRate[i] * (1.0 + 20.0 / i);
+
+	maxM = 0;
+	while (maxM < ztex->freqMDefault && ztex->maxErrorRate[maxM + 1] < LIBZTEX_MAXMAXERRORRATE)
+		maxM++;
+	while (maxM < ztex->freqMaxM && ztex->errorWeight[maxM] > 150 && ztex->maxErrorRate[maxM + 1] < LIBZTEX_MAXMAXERRORRATE)
+		maxM++;
+
+	bestM = 0;
+	bestR = 0;
+	for (i = 0; i <= maxM; i++) {
+		r = (i + 1 + (i == ztex->freqM? LIBZTEX_ERRORHYSTERESIS: 0)) * (1 - ztex->maxErrorRate[i]);
+		if (r > bestR) {
+			bestM = i;
+			bestR = r;
+		}
+	}
+
+	if (bestM != ztex->freqM) 
+		libztex_setFreq(ztex, bestM);
+
+	maxM = ztex->freqMDefault;
+	while (maxM < ztex->freqMaxM && ztex->errorWeight[maxM + 1] > 100)
+		maxM++;
+	if ((bestM < (1.0 - LIBZTEX_OVERHEATTHRESHOLD) * maxM) && bestM < maxM - 1) {
+		libztex_resetFpga(ztex);
+		applog(LOG_ERR, "%s: frequency drop of %.1f%% detect. This may be caused by overheating. FPGA is shut down to prevent damage.",
+		       ztex->repr, (1.0 - 1.0 * bestM / maxM) * 100);
+		return false;
+	}
+	return true;
+}
+
+
+static bool ztex_checkNonce(struct libztex_device *ztex,
+                            struct work *work,
+                            struct libztex_hash_data *hdata)
+{
+	uint32_t *data32 = (uint32_t *)(work->data);
+	unsigned char swap[128];
+	uint32_t *swap32 = (uint32_t *)swap;
+	unsigned char hash1[32];
+	unsigned char hash2[32];
+	uint32_t *hash2_32 = (uint32_t *)hash2;
+	int i;
+
+#if defined(__BIGENDIAN__) || defined(MIPSEB)
+	hdata->nonce = swab32(hdata->nonce);
+	hdata->hash7 = swab32(hdata->hash7);
+#endif
+
+	work->data[64 + 12 + 0] = (hdata->nonce >> 0) & 0xff;
+	work->data[64 + 12 + 1] = (hdata->nonce >> 8) & 0xff;
+	work->data[64 + 12 + 2] = (hdata->nonce >> 16) & 0xff;
+	work->data[64 + 12 + 3] = (hdata->nonce >> 24) & 0xff;
+
+	for (i = 0; i < 80 / 4; i++)
+		swap32[i] = swab32(data32[i]);
+	
+	sha2(swap, 80, hash1, false);
+	sha2(hash1, 32, hash2, false);
+#if defined(__BIGENDIAN__) || defined(MIPSEB)
+	if (hash2_32[7] != ((hdata->hash7 + 0x5be0cd19) & 0xFFFFFFFF)) {
+#else
+	if (swab32(hash2_32[7]) != ((hdata->hash7 + 0x5be0cd19) & 0xFFFFFFFF)) {
+#endif
+		ztex->errorCount[ztex->freqM] += 1.0 / ztex->numNonces;
+		applog(LOG_DEBUG, "%s: checkNonce failed for %0.8X", ztex->repr, hdata->nonce);
+		return false;
+	}
+	return true;
+}
+
+static uint64_t ztex_scanhash(struct thr_info *thr, struct work *work,
+                              __maybe_unused uint64_t max_nonce)
+{
+	struct libztex_device *ztex;
+	unsigned char sendbuf[44];
+	int i, j;
+	uint32_t backlog[GOLDEN_BACKLOG];
+	int backlog_p = 0;
+	uint32_t lastnonce[GOLDEN_BACKLOG], nonce, noncecnt = 0;
+	bool overflow, found, rv;
+	struct libztex_hash_data hdata[GOLDEN_BACKLOG];
+
+	ztex = thr->cgpu->device_ztex;
+
+	memcpy(sendbuf, work->data + 64, 12);
+	memcpy(sendbuf + 12, work->midstate, 32);
+	memset(backlog, 0, sizeof(backlog));
+	i = libztex_sendHashData(ztex, sendbuf);
+	if (i < 0) {
+		// Something wrong happened in send
+		applog(LOG_ERR, "%s: Failed to send hash data with err %d, retrying", ztex->repr, i);
+		usleep(500000);
+		i = libztex_sendHashData(ztex, sendbuf);
+		if (i < 0) {
+			// And there's nothing we can do about it
+			ztex_disable(thr);
+			applog(LOG_ERR, "%s: Failed to send hash data with err %d, giving up", ztex->repr, i);
+			return 0;
+		}
+	}
+	
+	applog(LOG_DEBUG, "sent hashdata");
+
+	for (i = 0; i < ztex->numNonces; i++)
+		lastnonce[i] = 0;
+
+	overflow = false;
+
+	while (!(overflow || work_restart[thr->id].restart)) {
+		usleep(250000);
+		if (work_restart[thr->id].restart) {
+			applog(LOG_DEBUG, "%s: New work detected", ztex->repr);
+			break;
+		}
+		i = libztex_readHashData(ztex, &hdata[0]);
+		if (i < 0) {
+			// Something wrong happened in read
+			applog(LOG_ERR, "%s: Failed to read hash data with err %d, retrying", ztex->repr, i);
+			usleep(500000);
+			i = libztex_readHashData(ztex, &hdata[0]);
+			if (i < 0) {
+				// And there's nothing we can do about it
+				ztex_disable(thr);
+				applog(LOG_ERR, "%s: Failed to read hash data with err %d, giving up", ztex->repr, i);
+				return 0;
+			}
+		}
+
+		if (work_restart[thr->id].restart) {
+			applog(LOG_DEBUG, "%s: New work detected", ztex->repr);
+			break;
+		}
+
+		ztex->errorCount[ztex->freqM] *= 0.995;
+		ztex->errorWeight[ztex->freqM] = ztex->errorWeight[ztex->freqM] * 0.995 + 1.0;
+ 
+		for (i = 0; i < ztex->numNonces; i++) {
+			nonce = hdata[i].nonce;
+#if defined(__BIGENDIAN__) || defined(MIPSEB)
+			nonce = swab32(nonce);
+#endif
+			if (nonce > noncecnt)
+				noncecnt = nonce;
+			if (((nonce & 0x7fffffff) >> 4) < ((lastnonce[i] & 0x7fffffff) >> 4)) {
+				applog(LOG_DEBUG, "%s: overflow nonce=%0.8x lastnonce=%0.8x", ztex->repr, nonce, lastnonce[i]);
+				overflow = true;
+			} else
+				lastnonce[i] = nonce;
+#if !(defined(__BIGENDIAN__) || defined(MIPSEB))
+			nonce = swab32(nonce);
+#endif
+			if (!ztex_checkNonce(ztex, work, &hdata[i])) {
+				thr->cgpu->hw_errors++;
+				continue;
+			}
+			nonce = hdata[i].goldenNonce;
+			if (nonce > 0) {
+				found = false;
+				for (j = 0; j < GOLDEN_BACKLOG; j++) {
+					if (backlog[j] == nonce) {
+						found = true;
+						break;
+					}
+				}
+				if (!found) {
+					applog(LOG_DEBUG, "%s: Share found", ztex->repr);
+					backlog[backlog_p++] = nonce;
+					if (backlog_p >= GOLDEN_BACKLOG)
+						backlog_p = 0;
+#if defined(__BIGENDIAN__) || defined(MIPSEB)
+					nonce = swab32(nonce);
+#endif
+					work->blk.nonce = 0xffffffff;
+					rv = submit_nonce(thr, work, nonce);
+					applog(LOG_DEBUG, "%s: submitted %0.8x %d", ztex->repr, nonce, rv);
+				}
+			}
+
+		}
+
+	}
+
+	ztex->errorRate[ztex->freqM] = ztex->errorCount[ztex->freqM] /	ztex->errorWeight[ztex->freqM] * (ztex->errorWeight[ztex->freqM] < 100? ztex->errorWeight[ztex->freqM] * 0.01: 1.0);
+	if (ztex->errorRate[ztex->freqM] > ztex->maxErrorRate[ztex->freqM])
+		ztex->maxErrorRate[ztex->freqM] = ztex->errorRate[ztex->freqM];
+
+	if (!ztex_updateFreq(ztex))
+		// Something really serious happened, so mark this thread as dead!
+		return 0;
+
+	applog(LOG_DEBUG, "%s: exit %1.8X", ztex->repr, noncecnt);
+
+	work->blk.nonce = 0xffffffff;
+
+	return noncecnt > 0? noncecnt: 1;
+}
+
+static void ztex_statline_before(char *buf, struct cgpu_info *cgpu)
+{
+	if (cgpu->deven == DEV_ENABLED) {
+		tailsprintf(buf, "%s | ", cgpu->device_ztex->snString);
+		tailsprintf(buf, "%0.2fMhz | ", cgpu->device_ztex->freqM1 * (cgpu->device_ztex->freqM + 1));
+	}
+}
+
+static bool ztex_prepare(struct thr_info *thr)
+{
+	struct timeval now;
+	struct cgpu_info *ztex = thr->cgpu;
+
+	gettimeofday(&now, NULL);
+	get_datestamp(ztex->init, &now);
+
+	if (libztex_configureFpga(ztex->device_ztex) != 0)
+		return false;
+
+	ztex->device_ztex->freqM = -1;
+	ztex_updateFreq(ztex->device_ztex);
+
+	applog(LOG_DEBUG, "%s: prepare", ztex->device_ztex->repr);
+	return true;
+}
+
+static void ztex_shutdown(struct thr_info *thr)
+{
+	if (thr->cgpu->device_ztex != NULL) {
+		applog(LOG_DEBUG, "%s: shutdown", thr->cgpu->device_ztex->repr);
+		libztex_destroy_device(thr->cgpu->device_ztex);
+		thr->cgpu->device_ztex = NULL;
+	}
+}
+
+static void ztex_disable(struct thr_info *thr)
+{
+	applog(LOG_ERR, "%s: Disabling!", thr->cgpu->device_ztex->repr);
+	devices[thr->cgpu->device_id]->deven = DEV_DISABLED;
+	ztex_shutdown(thr);
+}
+
+struct device_api ztex_api = {
+	.dname = "ztex",
+	.name = "PGA",
+	.api_detect = ztex_detect,
+	.get_statline_before = ztex_statline_before,
+	.thread_prepare = ztex_prepare,
+	.scanhash = ztex_scanhash,
+	.thread_shutdown = ztex_shutdown,
+};

+ 477 - 0
libztex.c

@@ -0,0 +1,477 @@
+/**
+ *   libztex.c - Ztex 1.15x fpga board support library
+ *
+ *   Copyright (c) 2012 nelisky.btc@gmail.com
+ *
+ *   This work is based upon the Java SDK provided by ztex which is
+ *   Copyright (C) 2009-2011 ZTEX GmbH.
+ *   http://www.ztex.de
+ *
+ *   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License version 2 as
+ *   published by the Free Software Foundation.
+ *
+ *   This program is distributed in the hope that it will be useful, but
+ *   WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ *   General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, see http://www.gnu.org/licenses/.
+**/
+
+#include <stdio.h>
+#include <unistd.h>
+#include "miner.h"
+#include "libztex.h"
+
+#define BUFSIZE 256
+
+//* Capability index for EEPROM support.
+#define CAPABILITY_EEPROM 0,0
+//* Capability index for FPGA configuration support. 
+#define CAPABILITY_FPGA 0,1
+//* Capability index for FLASH memory support.
+#define CAPABILITY_FLASH 0,2
+//* Capability index for DEBUG helper support.
+#define CAPABILITY_DEBUG 0,3
+//* Capability index for AVR XMEGA support.
+#define CAPABILITY_XMEGA 0,4
+//* Capability index for AVR XMEGA support.
+#define CAPABILITY_HS_FPGA 0,5
+//* Capability index for AVR XMEGA support.
+#define CAPABILITY_MAC_EEPROM 0,6
+
+
+
+static bool libztex_checkDevice(struct libusb_device *dev)
+{
+	struct libusb_device_descriptor desc;
+	int err;
+
+	err = libusb_get_device_descriptor(dev, &desc);
+	if (unlikely(err != 0)) {
+		applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err);
+		return false;
+	}
+	if (!(desc.idVendor == LIBZTEX_IDVENDOR && desc.idProduct == LIBZTEX_IDPRODUCT)) {
+		applog(LOG_DEBUG, "Not a ZTEX device %0.4x:%0.4x", desc.idVendor, desc.idProduct);
+		return false;
+	}
+	return true;
+}
+
+static bool libztex_checkCapability(struct libztex_device *ztex, int i, int j)
+{
+	if (!((i >= 0) && (i <= 5) && (j >= 0) && (j < 8) &&
+	     (((ztex->interfaceCapabilities[i] & 255) & (1 << j)) != 0))) {
+		applog(LOG_ERR, "%s: capability missing: %d %d", ztex->repr, i, i);
+		return false;
+	}
+	return true;
+}
+
+static int libztex_detectBitstreamBitOrder(const unsigned char *buf, int size)
+{
+	int i;
+
+	for (i = 0; i < size - 4; i++) {
+		if (((buf[i] & 255) == 0xaa) && ((buf[i + 1] & 255) == 0x99) && ((buf[i + 2] & 255) == 0x55) && ((buf[i + 3] & 255) == 0x66))
+			return 1;
+		if (((buf[i] & 255) == 0x55) && ((buf[i + 1] & 255) == 0x99) && ((buf[i + 2] & 255) == 0xaa) && ((buf[i + 3] & 255) == 0x66))
+			return 0;
+	} 
+	applog(LOG_WARNING, "Unable to determine bitstream bit order: no signature found");
+	return 0;
+}
+
+static void libztex_swapBits(unsigned char *buf, int size)
+{
+	unsigned char c;
+	int i;
+
+	for (i = 0; i < size; i++) {
+		c = buf[i];
+		buf[i] = ((c & 128) >> 7) |
+		         ((c & 64) >> 5) |
+		         ((c & 32) >> 3) |
+		         ((c & 16) >> 1) |
+		         ((c & 8) << 1) |
+		         ((c & 4) << 3) |
+		         ((c & 2) << 5) |
+		         ((c & 1) << 7);
+	}
+}
+
+static int libztex_getFpgaState(struct libztex_device *ztex, struct libztex_fpgastate *state)
+{
+	unsigned char buf[9];
+	int cnt;
+
+	if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
+		return -1;
+	cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x30, 0, 0, buf, 9, 1000);
+	if (unlikely(cnt < 0)) {
+		applog(LOG_ERR, "%s: Failed getFpgaState with err %d", ztex->repr, cnt);
+		return cnt;
+	}
+	state->fpgaConfigured = (buf[0] == 0);
+	state->fpgaChecksum = buf[1] & 0xff;
+	state->fpgaBytes = ((buf[5] & 0xff) << 24) | ((buf[4] & 0xff) << 16) | ((buf[3] & 0xff) << 8) | (buf[2] & 0xff);
+	state->fpgaInitB = buf[6] & 0xff;
+	state->fpgaFlashResult = buf[7];
+	state->fpgaFlashBitSwap = (buf[8] != 0);
+	return 0;
+}
+
+static int libztex_configureFpgaLS(struct libztex_device *ztex, const char* firmware, bool force, char bs)
+{
+	struct libztex_fpgastate state;
+	const int transactionBytes = 2048;
+	unsigned char buf[transactionBytes], cs;
+	int tries, cnt, buf_p, i;
+	ssize_t pos = 0;
+	FILE *fp;
+
+	if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
+		return -1;
+
+	libztex_getFpgaState(ztex, &state);
+	if (!force && state.fpgaConfigured) {
+		applog(LOG_DEBUG, "Bitstream already configured");
+		return 1;
+	}
+
+	for (tries = 10; tries > 0; tries--) {
+		fp = fopen(firmware, "rb");
+		if (!fp) {
+			applog(LOG_ERR, "%s: failed to read firmware '%s'", ztex->repr, firmware);
+			return -2;
+		}
+
+		cs = 0;
+		while (pos < transactionBytes && !feof(fp)) {
+			buf[pos] = getc(fp);
+			cs += buf[pos++];
+		}
+
+		if (feof(fp))
+			pos--;
+
+		if (bs != 0 && bs != 1)
+			bs = libztex_detectBitstreamBitOrder(buf, transactionBytes < pos? transactionBytes: pos);
+
+		//* Reset fpga
+		cnt = libztex_resetFpga(ztex);
+		if (unlikely(cnt < 0)) {
+			applog(LOG_ERR, "%s: Failed reset fpga with err %d", ztex->repr, cnt);
+			continue;
+		}
+
+		if (bs == 1)
+			libztex_swapBits(buf, pos);
+	 
+		buf_p = pos;
+		while (1) {
+			i = 0;
+			while (i < buf_p) {
+				cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x32, 0, 0, &buf[i], buf_p - i, 5000);
+				if (unlikely(cnt < 0)) {
+					applog(LOG_ERR, "%s: Failed send fpga data with err %d", ztex->repr, cnt);
+					break;
+				}
+				i += cnt;
+			}
+			if (i < buf_p || buf_p < transactionBytes)
+				break;
+			buf_p = 0;
+			while (buf_p < transactionBytes && !feof(fp)) {
+				buf[buf_p] = getc(fp);
+				cs += buf[buf_p++];
+			}
+			if (feof(fp))
+				buf_p--;
+			pos += buf_p;
+			if (buf_p == 0)
+				break;
+			if (bs == 1)
+				libztex_swapBits(buf, buf_p);
+		}
+		if (cnt >= 0)
+			tries = 0;
+
+		fclose(fp);
+	}
+	libztex_getFpgaState(ztex, &state);
+	if (!state.fpgaConfigured) {
+		applog(LOG_ERR, "%s: FPGA configuration failed: DONE pin does not go high", ztex->repr);
+		return 3;
+	}
+	usleep(200000);
+	applog(LOG_INFO, "%s: FPGA configuration done", ztex->repr);
+	return 0;
+}
+
+int libztex_configureFpga(struct libztex_device *ztex)
+{
+	char buf[256] = "bitstreams/";
+
+	memset(&buf[11], 0, 245);
+	strcpy(&buf[11], ztex->bitFileName);
+	strcpy(&buf[strlen(buf)], ".bit");
+
+	return libztex_configureFpgaLS(ztex, buf, true, 2);
+}
+
+int libztex_setFreq(struct libztex_device *ztex, uint16_t freq)
+{
+	int cnt;
+
+	if (freq > ztex->freqMaxM)
+		freq = ztex->freqMaxM;
+
+	cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x83, freq, 0, NULL, 0, 500);
+	if (unlikely(cnt < 0)) {
+		applog(LOG_ERR, "Ztex check device: Failed to set frequency with err %d", cnt);
+		return cnt;
+	}
+	ztex->freqM = freq;
+	applog(LOG_WARNING, "%s: Frequency change to %0.2f Mhz", ztex->repr, ztex->freqM1 * (ztex->freqM + 1));
+
+	return 0;
+}
+
+int libztex_resetFpga(struct libztex_device *ztex)
+{
+	return libusb_control_transfer(ztex->hndl, 0x40, 0x31, 0, 0, NULL, 0, 1000);
+}
+
+int libztex_prepare_device(struct libusb_device *dev, struct libztex_device** ztex)
+{
+	struct libztex_device *newdev;
+	unsigned char buf[64];
+	int cnt, err;
+
+	newdev = malloc(sizeof(struct libztex_device));
+	newdev->bitFileName = NULL;
+	newdev->valid = false;
+	newdev->hndl = NULL;
+	*ztex = newdev;
+
+	err = libusb_get_device_descriptor(dev, &newdev->descriptor);
+	if (unlikely(err != 0)) {
+		applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err);
+		return err;
+	}
+
+	// Check vendorId and productId
+	if (!(newdev->descriptor.idVendor == LIBZTEX_IDVENDOR &&
+				newdev->descriptor.idProduct == LIBZTEX_IDPRODUCT)) {
+		applog(LOG_ERR, "Not a ztex device? %0.4X, %0.4X", newdev->descriptor.idVendor, newdev->descriptor.idProduct);
+		return 1;
+	}
+
+	err = libusb_open(dev, &newdev->hndl);
+	if (unlikely(err != 0)) {
+		applog(LOG_ERR, "Ztex check device: Failed to open handle with error %d", err);
+		return err;
+	}
+
+	cnt = libusb_get_string_descriptor_ascii (newdev->hndl, newdev->descriptor.iSerialNumber, newdev->snString,
+	                                          LIBZTEX_SNSTRING_LEN + 1);
+	if (unlikely(cnt < 0)) {
+		applog(LOG_ERR, "Ztex check device: Failed to read device snString with err %d", cnt);
+		return cnt;
+	}
+
+	cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x22, 0, 0, buf, 40, 500);
+	if (unlikely(cnt < 0)) {
+		applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
+		return cnt;
+	}
+	
+	if ( buf[0] != 40 || buf[1] != 1 || buf[2] != 'Z' || buf[3] != 'T' || buf[4] != 'E' || buf[5] != 'X' ) {
+		applog(LOG_ERR, "Ztex check device: Error reading ztex descriptor");
+		return 2;
+	}
+
+	newdev->productId[0] = buf[6];
+	newdev->productId[1] = buf[7];
+	newdev->productId[2] = buf[8];
+	newdev->productId[3] = buf[9];
+	newdev->fwVersion = buf[10];
+	newdev->interfaceVersion = buf[11];
+	newdev->interfaceCapabilities[0] = buf[12];
+	newdev->interfaceCapabilities[1] = buf[13];
+	newdev->interfaceCapabilities[2] = buf[14];
+	newdev->interfaceCapabilities[3] = buf[15];
+	newdev->interfaceCapabilities[4] = buf[16];
+	newdev->interfaceCapabilities[5] = buf[17];
+	newdev->moduleReserved[0] = buf[18];
+	newdev->moduleReserved[1] = buf[19];
+	newdev->moduleReserved[2] = buf[20];
+	newdev->moduleReserved[3] = buf[21];
+	newdev->moduleReserved[4] = buf[22];
+	newdev->moduleReserved[5] = buf[23];
+	newdev->moduleReserved[6] = buf[24];
+	newdev->moduleReserved[7] = buf[25];
+	newdev->moduleReserved[8] = buf[26];
+	newdev->moduleReserved[9] = buf[27];
+	newdev->moduleReserved[10] = buf[28];
+	newdev->moduleReserved[11] = buf[29];
+
+
+	cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x82, 0, 0, buf, 64, 500);
+	if (unlikely(cnt < 0)) {
+		applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
+		return cnt;
+	}
+
+	if (unlikely(buf[0] != 4)) {
+		if (unlikely(buf[0] != 2)) {
+			applog(LOG_ERR, "Invalid BTCMiner descriptor version. Firmware must be updated (%d).", buf[0]);
+			return 3;
+		}
+		applog(LOG_WARNING, "Firmware out of date");
+	}
+
+	newdev->numNonces = buf[1] + 1;
+	newdev->offsNonces = ((buf[2] & 255) | ((buf[3] & 255) << 8)) - 10000;
+	newdev->freqM1 = ((buf[4] & 255) | ((buf[5] & 255) << 8) ) * 0.01;
+	newdev->freqMaxM = (buf[7] & 255);
+	newdev->freqM = (buf[6] & 255);
+	newdev->freqMDefault = newdev->freqM;
+
+	for (cnt=0; cnt < 255; cnt++) {
+		newdev->errorCount[cnt] = 0;
+		newdev->errorWeight[cnt] = 0;
+		newdev->errorRate[cnt] = 0;
+		newdev->maxErrorRate[cnt] = 0;
+	}
+
+	cnt = strlen((char *)&buf[buf[0] == 4? 10: 8]);
+	newdev->bitFileName = malloc(sizeof(char) * (cnt + 1));
+	memcpy(newdev->bitFileName, &buf[buf[0] == 4? 10: 8], cnt + 1);
+
+	newdev->usbbus = libusb_get_bus_number(dev);
+	newdev->usbaddress = libusb_get_device_address(dev);
+	sprintf(newdev->repr, "ZTEX %.3d:%.3d-%s", newdev->usbbus, newdev->usbaddress, newdev->snString);
+	newdev->valid = true;
+	return 0;
+}
+
+void libztex_destroy_device(struct libztex_device* ztex)
+{
+	if (ztex->hndl != NULL) {
+		libusb_close(ztex->hndl);
+		ztex->hndl = NULL;
+	}
+	if (ztex->bitFileName != NULL) {
+		free(ztex->bitFileName);
+		ztex->bitFileName = NULL;
+	}
+	free(ztex);
+}
+
+int libztex_scanDevices(struct libztex_dev_list*** devs_p)
+{
+	int usbdevices[LIBZTEX_MAX_DESCRIPTORS];
+	struct libztex_dev_list **devs;
+	struct libztex_device *ztex;
+	int found = 0, pos = 0, err;
+	libusb_device **list;
+	ssize_t cnt, i = 0;
+
+	cnt = libusb_get_device_list(NULL, &list);
+	if (unlikely(cnt < 0)) {
+		applog(LOG_ERR, "Ztex scan devices: Failed to list usb devices with err %d", cnt);
+		return 0;
+	}
+
+	for (i = 0; i < cnt; i++) {
+		if (libztex_checkDevice(list[i])) {
+			// Got one!
+			usbdevices[found] = i;
+			found++;
+		}
+	}
+
+	devs = malloc(sizeof(struct libztex_dev_list *) * found);
+	if (devs == NULL) {
+		applog(LOG_ERR, "Ztex scan devices: Failed to allocate memory");
+		return 0;
+	}
+
+	for (i = 0; i < found; i++) {
+		err = libztex_prepare_device(list[usbdevices[i]], &ztex);
+		if (unlikely(err != 0))
+			applog(LOG_ERR, "prepare device: %d", err);
+		// check if valid
+		if (!ztex->valid) {
+			libztex_destroy_device(ztex);
+			continue;
+		}
+		devs[pos] = malloc(sizeof(struct libztex_dev_list));
+		devs[pos]->dev = ztex;
+		devs[pos]->next = NULL;
+		if (pos > 0)
+			devs[pos - 1]->next = devs[pos];
+		pos++;
+	}
+
+	libusb_free_device_list(list, 1);
+	*devs_p = devs;
+	return pos;
+}
+
+int libztex_sendHashData(struct libztex_device *ztex, unsigned char *sendbuf)
+{
+	int cnt;
+
+	if (ztex == NULL || ztex->hndl == NULL)
+		return 0;
+	cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x80, 0, 0, sendbuf, 44, 1000);
+	if (unlikely(cnt < 0))
+		applog(LOG_ERR, "%s: Failed sendHashData with err %d", ztex->repr, cnt);
+	
+	return cnt;
+}
+
+int libztex_readHashData(struct libztex_device *ztex, struct libztex_hash_data nonces[])
+{
+	// length of buf must be 8 * (numNonces + 1)
+	unsigned char rbuf[12 * 8];
+	int cnt, i;
+
+	if (ztex->hndl == NULL)
+		return 0;
+	
+	cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x81, 0, 0, rbuf, 12 * ztex->numNonces, 1000);
+	if (unlikely(cnt < 0)) {
+		applog(LOG_ERR, "%s: Failed readHashData with err %d", ztex->repr, cnt);
+		return cnt;
+	}
+
+	for (i = 0; i < ztex->numNonces; i++) {
+		memcpy((char*)&nonces[i].goldenNonce, &rbuf[i * 12], 4);
+		nonces[i].goldenNonce -= ztex->offsNonces;
+		memcpy((char*)&nonces[i].nonce, &rbuf[(i * 12) + 4], 4);
+		nonces[i].nonce -= ztex->offsNonces;
+		memcpy((char*)&nonces[i].hash7, &rbuf[(i * 12) + 8], 4);
+	}
+	
+	return cnt;
+}
+
+void libztex_freeDevList(struct libztex_dev_list **devs)
+{
+	bool done = false;
+	ssize_t cnt = 0;
+
+	while (!done) {
+		if (devs[cnt]->next == NULL)
+			done = true;
+		free(devs[cnt++]);
+	}
+	free(devs);
+}

+ 95 - 0
libztex.h

@@ -0,0 +1,95 @@
+/**
+ *   libztex.h - headers for Ztex 1.15x fpga board support library
+ *
+ *   Copyright (c) 2012 nelisky.btc@gmail.com
+ *
+ *   This work is based upon the Java SDK provided by ztex which is
+ *   Copyright (C) 2009-2011 ZTEX GmbH.
+ *   http://www.ztex.de
+ *
+ *   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License version 2 as
+ *   published by the Free Software Foundation.
+ *
+ *   This program is distributed in the hope that it will be useful, but
+ *   WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ *   General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, see http://www.gnu.org/licenses/.
+**/
+#ifndef __LIBZTEX_H__
+#define __LIBZTEX_H__
+
+#include <libusb-1.0/libusb.h>
+
+#define LIBZTEX_MAX_DESCRIPTORS 512
+#define LIBZTEX_SNSTRING_LEN 10
+
+#define LIBZTEX_IDVENDOR 0x221A
+#define LIBZTEX_IDPRODUCT 0x0100
+
+#define LIBZTEX_MAXMAXERRORRATE 0.05
+#define LIBZTEX_ERRORHYSTERESIS 0.1
+#define LIBZTEX_OVERHEATTHRESHOLD 0.5
+
+struct libztex_fpgastate {
+	bool fpgaConfigured;
+	unsigned char fpgaChecksum;
+	uint16_t fpgaBytes;
+	unsigned char fpgaInitB;
+	unsigned char fpgaFlashResult;
+	bool fpgaFlashBitSwap;
+};
+
+struct libztex_device {
+	bool valid;
+	struct libusb_device_descriptor descriptor;
+	libusb_device_handle *hndl; 
+	unsigned char usbbus;
+	unsigned char usbaddress;
+	unsigned char snString[LIBZTEX_SNSTRING_LEN+1];
+	unsigned char productId[4];
+	unsigned char fwVersion;
+	unsigned char interfaceVersion;
+	unsigned char interfaceCapabilities[6];
+	unsigned char moduleReserved[12];
+	uint8_t numNonces;
+	uint16_t offsNonces;
+	double freqM1;	
+	uint8_t freqM;
+	uint8_t freqMaxM;
+	uint8_t freqMDefault;
+	char* bitFileName;
+
+	double errorCount[256];
+	double errorWeight[256];
+	double errorRate[256];
+	double maxErrorRate[256];
+
+	char repr[64];
+};
+
+struct libztex_dev_list { 
+	struct libztex_device *dev;
+	struct libztex_dev_list *next;
+};
+
+struct libztex_hash_data {
+	uint32_t goldenNonce;
+	uint32_t nonce;
+	uint32_t hash7;
+};
+
+extern int libztex_scanDevices (struct libztex_dev_list ***devs);
+extern void libztex_freeDevList (struct libztex_dev_list **devs);
+extern int libztex_prepare_device (struct libusb_device *dev, struct libztex_device** ztex);
+extern void libztex_destroy_device (struct libztex_device* ztex);
+extern int libztex_configureFpga (struct libztex_device *dev);
+extern int libztex_setFreq (struct libztex_device *ztex, uint16_t freq);
+extern int libztex_sendHashData (struct libztex_device *ztex, unsigned char *sendbuf);
+extern int libztex_readHashData (struct libztex_device *ztex, struct libztex_hash_data nonces[]);
+extern int libztex_resetFpga (struct libztex_device *ztex);
+
+#endif /* __LIBZTEX_H__ */

+ 51 - 2
miner.h

@@ -61,6 +61,14 @@ void *alloca (size_t);
  #include "ADL_SDK/adl_sdk.h"
  #include "ADL_SDK/adl_sdk.h"
 #endif
 #endif
 
 
+#ifdef HAVE_LIBUSB
+  #include <libusb-1.0/libusb.h>
+#endif
+
+#ifdef USE_ZTEX
+  #include "libztex.h"
+#endif
+
 #if !defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
 #if !defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
 #define bswap_16 __builtin_bswap16
 #define bswap_16 __builtin_bswap16
 #define bswap_32 __builtin_bswap32
 #define bswap_32 __builtin_bswap32
@@ -222,6 +230,28 @@ enum cl_kernels {
 	KL_DIABLO,
 	KL_DIABLO,
 };
 };
 
 
+enum dev_reason {
+	REASON_THREAD_FAIL_INIT,
+	REASON_THREAD_ZERO_HASH,
+	REASON_THREAD_FAIL_QUEUE,
+	REASON_DEV_SICK_IDLE_60,
+	REASON_DEV_DEAD_IDLE_600,
+	REASON_DEV_NOSTART,
+	REASON_DEV_OVER_HEAT,
+	REASON_DEV_THERMAL_CUTOFF,
+};
+
+#define REASON_NONE			"None"
+#define REASON_THREAD_FAIL_INIT_STR	"Thread failed to init"
+#define REASON_THREAD_ZERO_HASH_STR	"Thread got zero hashes"
+#define REASON_THREAD_FAIL_QUEUE_STR	"Thread failed to queue work"
+#define REASON_DEV_SICK_IDLE_60_STR	"Device idle for 60s"
+#define REASON_DEV_DEAD_IDLE_600_STR	"Device dead - idle for 600s"
+#define REASON_DEV_NOSTART_STR		"Device failed to start"
+#define REASON_DEV_OVER_HEAT_STR	"Device over heated"
+#define REASON_DEV_THERMAL_CUTOFF_STR	"Device reached thermal cutoff"
+#define REASON_UNKNOWN_STR		"Unknown reason - code bug"
+
 struct cgpu_info {
 struct cgpu_info {
 	int cgminer_id;
 	int cgminer_id;
 	struct device_api *api;
 	struct device_api *api;
@@ -229,7 +259,12 @@ struct cgpu_info {
 	char *name;
 	char *name;
 	char *device_path;
 	char *device_path;
 	FILE *device_file;
 	FILE *device_file;
-	int device_fd;
+	union {
+#ifdef USE_ZTEX
+		struct libztex_device *device_ztex;
+#endif
+		int device_fd;
+	};
 
 
 	enum dev_enable deven;
 	enum dev_enable deven;
 	int accepted;
 	int accepted;
@@ -275,6 +310,18 @@ struct cgpu_info {
 #endif
 #endif
 	int last_share_pool;
 	int last_share_pool;
 	time_t last_share_pool_time;
 	time_t last_share_pool_time;
+
+	time_t device_last_well;
+	time_t device_last_not_well;
+	enum dev_reason device_not_well_reason;
+	int thread_fail_init_count;
+	int thread_zero_hash_count;
+	int thread_fail_queue_count;
+	int dev_sick_idle_60_count;
+	int dev_dead_idle_600_count;
+	int dev_nostart_count;
+	int dev_over_heat_count;	// It's a warning but worth knowing
+	int dev_thermal_cutoff_count;
 };
 };
 
 
 extern bool add_cgpu(struct cgpu_info*);
 extern bool add_cgpu(struct cgpu_info*);
@@ -478,7 +525,7 @@ extern int add_pool_details(bool live, char *url, char *user, char *pass);
 #define ADD_POOL_OK 0
 #define ADD_POOL_OK 0
 
 
 #define MAX_GPUDEVICES 16
 #define MAX_GPUDEVICES 16
-#define MAX_DEVICES 32
+#define MAX_DEVICES 64
 #define MAX_POOLS (32)
 #define MAX_POOLS (32)
 
 
 #define MIN_INTENSITY -10
 #define MIN_INTENSITY -10
@@ -616,6 +663,7 @@ extern int curses_int(const char *query);
 extern char *curses_input(const char *query);
 extern char *curses_input(const char *query);
 extern void kill_work(void);
 extern void kill_work(void);
 extern void switch_pools(struct pool *selected);
 extern void switch_pools(struct pool *selected);
+extern void remove_pool(struct pool *pool);
 extern void write_config(FILE *fcfg);
 extern void write_config(FILE *fcfg);
 extern void log_curses(int prio, const char *f, va_list ap);
 extern void log_curses(int prio, const char *f, va_list ap);
 extern void clear_logwin(void);
 extern void clear_logwin(void);
@@ -628,5 +676,6 @@ extern void tq_freeze(struct thread_q *tq);
 extern void tq_thaw(struct thread_q *tq);
 extern void tq_thaw(struct thread_q *tq);
 extern bool successful_connect;
 extern bool successful_connect;
 extern void adl(void);
 extern void adl(void);
+extern void app_restart(void);
 
 
 #endif /* __MINER_H__ */
 #endif /* __MINER_H__ */

+ 463 - 92
miner.php

@@ -1,30 +1,79 @@
 <?php
 <?php
 session_start();
 session_start();
 #
 #
-global $miner, $port;
-$miner = '127.0.0.1'; # hostname or IP address
-$port = 4028;
+global $miner, $port, $readonly, $notify, $rigs;
+#
+# Don't touch these 2 - see $rigs below
+$miner = null;
+$port = null;
+#
+# Set $readonly to true to force miner.php to be readonly
+# Set $readonly to false then it will check cgminer 'privileged'
+$readonly = false;
+#
+# Set $notify to false to NOT attempt to display the notify command
+# Set $notify to true to attempt to display the notify command
+# If your older version of cgminer returns an 'Invalid command'
+#  coz it doesn't have notify - it just shows the error status table
+$notify = true;
+#
+# Set $rigs to an array of your cgminer rigs that are running
+#  format: 'IP:Port' or 'Host:Port'
+# If you only have one rig, it will just show the detail of that rig
+# If you have more than one rig it will show a summary of all the rigs
+#  with buttons to show the details of each rig
+# e.g. $rigs = array('127.0.0.1:4028','myrig.com:4028');
+$rigs = array('127.0.0.1:4028');
 #
 #
 $here = $_SERVER['PHP_SELF'];
 $here = $_SERVER['PHP_SELF'];
 #
 #
+global $tablebegin, $tableend, $warnfont, $warnoff;
+$tablebegin = '<tr><td><table border=1 cellpadding=5 cellspacing=0>';
+$tableend = '</table></td></tr>';
+$warnfont = '<font color=red><b>';
+$warnoff = '</b></font>';
+
+#
+function htmlhead($checkapi)
+{
+ global $error, $readonly, $here;
+ if ($readonly === false && $checkapi === true)
+ {
+	$access = api('privileged');
+	if ($error != null
+	||  !isset($access['STATUS']['STATUS'])
+	||  $access['STATUS']['STATUS'] != 'S')
+		$readonly = true;
+ }
 ?>
 ?>
 <html><head><title>Mine</title>
 <html><head><title>Mine</title>
 <style type='text/css'>
 <style type='text/css'>
 td { color:blue; font-family:verdana,arial,sans; font-size:13pt; }
 td { color:blue; font-family:verdana,arial,sans; font-size:13pt; }
 td.h { color:blue; font-family:verdana,arial,sans; font-size:13pt; background:#d0ffff }
 td.h { color:blue; font-family:verdana,arial,sans; font-size:13pt; background:#d0ffff }
+td.err { color:black; font-family:verdana,arial,sans; font-size:13pt; background:#ff3050 }
+td.warn { color:black; font-family:verdana,arial,sans; font-size:13pt; background:#ffb050 }
 td.sta { color:green; font-family:verdana,arial,sans; font-size:13pt; }
 td.sta { color:green; font-family:verdana,arial,sans; font-size:13pt; }
+td.tot { color:blue; font-family:verdana,arial,sans; font-size:13pt; background:#fff8f2 }
 </style>
 </style>
 </head><body bgcolor=#ecffff>
 </head><body bgcolor=#ecffff>
 <script type='text/javascript'>
 <script type='text/javascript'>
 function pr(a,m){if(m!=null){if(!confirm(m+'?'))return}window.location="<?php echo $here ?>"+a}
 function pr(a,m){if(m!=null){if(!confirm(m+'?'))return}window.location="<?php echo $here ?>"+a}
+<?php
+ if ($readonly === false && $checkapi === true)
+ {
+?>
 function prc(a,m){pr('?arg='+a,m)}
 function prc(a,m){pr('?arg='+a,m)}
-function prs(a){var c=a.substr(3);var z=c.split('|',2);var m=z[0].substr(0,1).toUpperCase()+z[0].substr(1)+' GPU '+z[1];prc(a,m)}
-function prs2(a,n){var v=document.getElementById('gi'+n).value;var c=a.substr(3);var z=c.split('|',2);var m='Set GPU '+z[1]+' '+z[0].substr(0,1).toUpperCase()+z[0].substr(1)+' to '+v;prc(a+','+v,m)}
+function prs(a,r){var c=a.substr(3);var z=c.split('|',2);var m=z[0].substr(0,1).toUpperCase()+z[0].substr(1)+' GPU '+z[1];prc(a+'&rig='+r,m)}
+function prs2(a,n,r){var v=document.getElementById('gi'+n).value;var c=a.substr(3);var z=c.split('|',2);var m='Set GPU '+z[1]+' '+z[0].substr(0,1).toUpperCase()+z[0].substr(1)+' to '+v;prc(a+','+v+'&rig='+r,m)}
+<?php
+ }
+?>
 </script>
 </script>
 <table width=100% height=100% border=0 cellpadding=0 cellspacing=0 summary='Mine'>
 <table width=100% height=100% border=0 cellpadding=0 cellspacing=0 summary='Mine'>
 <tr><td align=center valign=top>
 <tr><td align=center valign=top>
 <table border=0 cellpadding=4 cellspacing=0 summary='Mine'>
 <table border=0 cellpadding=4 cellspacing=0 summary='Mine'>
 <?php
 <?php
+}
 #
 #
 global $error;
 global $error;
 $error = null;
 $error = null;
@@ -154,121 +203,214 @@ function getparam($name, $both = false)
 #
 #
 function fmt($section, $name, $value)
 function fmt($section, $name, $value)
 {
 {
+ $errorclass = ' class=err';
+ $warnclass = ' class=warn';
  $b = '&nbsp;';
  $b = '&nbsp;';
 
 
+ $ret = $value;
+ $class = '';
+
  switch ($section.'.'.$name)
  switch ($section.'.'.$name)
  {
  {
- case 'GPU0.Last Share Time':
-	return date('H:i:s', $value);
+ case 'GPU.Last Share Time':
+ case 'PGA.Last Share Time':
+	$ret = date('H:i:s', $value);
 	break;
 	break;
  case 'SUMMARY.Elapsed':
  case 'SUMMARY.Elapsed':
 	$s = $value % 60;
 	$s = $value % 60;
 	$value -= $s;
 	$value -= $s;
 	$value /= 60;
 	$value /= 60;
 	if ($value == 0)
 	if ($value == 0)
-	{
-		return $s.'s';
-	}
+		$ret = $s.'s';
 	else
 	else
 	{
 	{
 		$m = $value % 60;
 		$m = $value % 60;
 		$value -= $m;
 		$value -= $m;
 		$value /= 60;
 		$value /= 60;
 		if ($value == 0)
 		if ($value == 0)
-		{
-			return sprintf("%dm$b%02ds", $m, $s);
-		}
+			$ret = sprintf("%dm$b%02ds", $m, $s);
 		else
 		else
 		{
 		{
 			$h = $value % 24;
 			$h = $value % 24;
 			$value -= $h;
 			$value -= $h;
 			$value /= 24;
 			$value /= 24;
 			if ($value == 0)
 			if ($value == 0)
-				return sprintf("%dh$b%02dm$b%02ds", $h, $m, $s);
+				$ret = sprintf("%dh$b%02dm$b%02ds", $h, $m, $s);
 			else
 			else
-				return sprintf("%ddays$b%02dh$b%02dm$b%02ds", $value, $h, $m, $s);
+			{
+				if ($value == 1)
+					$days = '';
+				else
+					$days = 's';
+
+				$ret = sprintf("%dday$days$b%02dh$b%02dm$b%02ds", $value, $h, $m, $s);
+			}
 		}
 		}
 	}
 	}
 	break;
 	break;
- case 'GPU0.Utility':
+ case 'NOTIFY.Last Well':
+	if ($value == '0')
+	{
+		$ret = 'Never';
+		$class = $warnclass;
+	}
+	else
+		$ret = date('H:i:s', $value);
+	break;
+ case 'NOTIFY.Last Not Well':
+	if ($value == '0')
+		$ret = 'Never';
+	else
+	{
+		$ret = date('H:i:s', $value);
+		$class = $errorclass;
+	}
+	break;
+ case 'NOTIFY.Reason Not Well':
+	if ($value != 'None')
+		$class = $errorclass;
+	break;
+ case 'GPU.Utility':
+ case 'PGA.Utility':
  case 'SUMMARY.Utility':
  case 'SUMMARY.Utility':
-	return $value.'/m';
+	$ret = $value.'/m';
+	break;
+ case 'PGA.Temperature':
+	$ret = $value.'&deg;C';
+	break;
+ case 'GPU.Temperature':
+	$ret = $value.'&deg;C';
+ case 'GPU.Fan Speed':
+ case 'GPU.Fan Percent':
+ case 'GPU.GPU Clock':
+ case 'GPU.Memory Clock':
+ case 'GPU.GPU Voltage':
+ case 'GPU.GPU Activity':
+	if ($value == 0)
+		$class = $warnclass;
 	break;
 	break;
- case 'GPU0.Temperature':
-	return $value.'&deg;C';
+ case 'GPU.MHS av':
+ case 'PGA.MHS av':
+ case 'SUMMARY.MHS av':
+ case 'GPU.Total MH':
+ case 'PGA.Total MH':
+ case 'SUMMARY.Total MH':
+ case 'SUMMARY.Getworks':
+ case 'GPU.Accepted':
+ case 'PGA.Accepted':
+ case 'SUMMARY.Accepted':
+ case 'GPU.Rejected':
+ case 'PGA.Rejected':
+ case 'SUMMARY.Rejected':
+ case 'SUMMARY.Local Work':
+ case 'POOL.Getworks':
+ case 'POOL.Accepted':
+ case 'POOL.Rejected':
+ case 'POOL.Discarded':
+	$parts = explode('.', $value, 2);
+	if (count($parts) == 1)
+		$dec = '';
+	else
+		$dec = '.'.$parts[1];
+	$ret = number_format($parts[0]).$dec;
+	break;
+ case 'GPU.Status':
+ case 'PGA.Status':
+ case 'POOL.Status':
+	if ($value != 'Alive')
+		$class = $errorclass;
+	break;
+ case 'GPU.Enabled':
+ case 'PGA.Enabled':
+	if ($value != 'Y')
+		$class = $warnclass;
 	break;
 	break;
  }
  }
 
 
- return $value;
+ if ($section == 'NOTIFY' && substr($name, 0, 1) == '*' && $value != '0')
+	$class = $errorclass;
+
+ return array($ret, $class);
 }
 }
 #
 #
-function details($cmd, $list)
+global $poolcmd;
+$poolcmd = array(	'Switch to'	=> 'switchpool',
+			'Enable'	=> 'enablepool',
+			'Disable'	=> 'disablepool' );
+#
+function showhead($cmd, $item, $values)
 {
 {
- $stas = array('S' => 'Success', 'W' => 'Warning', 'I' => 'Informational', 'E' => 'Error', 'F' => 'Fatal');
+ global $poolcmd, $readonly;
 
 
- $tb = '<tr><td><table border=1 cellpadding=5 cellspacing=0>';
- $te = '</table></td></tr>';
+ echo '<tr>';
 
 
- echo $tb;
+ foreach ($values as $name => $value)
+ {
+	if ($name == '0')
+		$name = '&nbsp;';
+	echo "<td valign=bottom class=h>$name</td>";
+ }
 
 
- echo '<tr><td class=sta>Date: '.date('H:i:s j-M-Y \U\T\CP').'</td></tr>';
+ if ($cmd == 'pools' && $readonly === false)
+	foreach ($poolcmd as $name => $pcmd)
+		echo "<td valign=bottom class=h>$name</td>";
 
 
- echo $te.$tb;
+ echo '</tr>';
+}
+#
+function details($cmd, $list, $rig)
+{
+ global $tablebegin, $tableend;
+ global $poolcmd, $readonly;
+
+ $dfmt = 'H:i:s j-M-Y \U\T\CP';
+
+ $stas = array('S' => 'Success', 'W' => 'Warning', 'I' => 'Informational', 'E' => 'Error', 'F' => 'Fatal');
+
+ echo $tablebegin;
+
+ echo '<tr><td class=sta>Date: '.date($dfmt).'</td></tr>';
+
+ echo $tableend.$tablebegin;
 
 
  if (isset($list['STATUS']))
  if (isset($list['STATUS']))
  {
  {
 	echo '<tr>';
 	echo '<tr>';
 	echo '<td>Computer: '.$list['STATUS']['Description'].'</td>';
 	echo '<td>Computer: '.$list['STATUS']['Description'].'</td>';
+	if (isset($list['STATUS']['When']))
+		echo '<td>When: '.date($dfmt, $list['STATUS']['When']).'</td>';
 	$sta = $list['STATUS']['STATUS'];
 	$sta = $list['STATUS']['STATUS'];
 	echo '<td>Status: '.$stas[$sta].'</td>';
 	echo '<td>Status: '.$stas[$sta].'</td>';
 	echo '<td>Message: '.$list['STATUS']['Msg'].'</td>';
 	echo '<td>Message: '.$list['STATUS']['Msg'].'</td>';
 	echo '</tr>';
 	echo '</tr>';
  }
  }
 
 
- echo $te.$tb;
 
 
  $section = '';
  $section = '';
 
 
- $poolcmd = array(	'Switch to'	=> 'switchpool',
-			'Enable'	=> 'enablepool',
-			'Disable'	=> 'disablepool' );
-
  foreach ($list as $item => $values)
  foreach ($list as $item => $values)
  {
  {
-	if ($item != 'STATUS')
-	{
-		$section = $item;
-
-		echo '<tr>';
-
-		foreach ($values as $name => $value)
-		{
-			if ($name == '0')
-				$name = '&nbsp;';
-			echo "<td valign=bottom class=h>$name</td>";
-		}
-
-		if ($cmd == 'pools')
-			foreach ($poolcmd as $name => $pcmd)
-				echo "<td valign=bottom class=h>$name</td>";
+	if ($item == 'STATUS')
+		continue;
 
 
-		echo '</tr>';
+	$sectionname = preg_replace('/\d/', '', $item);
 
 
-		break;
+	if ($sectionname != $section)
+	{
+		echo $tableend.$tablebegin;
+		showhead($cmd, $item, $values);
+		$section = $sectionname;
 	}
 	}
- }
-
- foreach ($list as $item => $values)
- {
-	if ($item == 'STATUS')
-		continue;
 
 
 	echo '<tr>';
 	echo '<tr>';
 
 
 	foreach ($values as $name => $value)
 	foreach ($values as $name => $value)
-		echo '<td>'.fmt($section, $name, $value).'</td>';
+	{
+		list($showvalue, $class) = fmt($section, $name, $value);
+		echo "<td$class>$showvalue</td>";
+	}
 
 
-	if ($cmd == 'pools')
+	if ($cmd == 'pools' && $readonly === false)
 	{
 	{
 		reset($values);
 		reset($values);
 		$pool = current($values);
 		$pool = current($values);
@@ -280,7 +422,7 @@ function details($cmd, $list)
 			else
 			else
 			{
 			{
 				echo "<input type=button value='Pool $pool'";
 				echo "<input type=button value='Pool $pool'";
-				echo " onclick='prc(\"$pcmd|$pool\",\"$name Pool $pool\")'>";
+				echo " onclick='prc(\"$pcmd|$pool&rig=$rig\",\"$name Pool $pool\")'>";
 			}
 			}
 			echo '</td>';
 			echo '</td>';
 		}
 		}
@@ -288,14 +430,16 @@ function details($cmd, $list)
 
 
 	echo '</tr>';
 	echo '</tr>';
  }
  }
- echo $te;
+
+ echo $tableend;
 }
 }
 #
 #
 global $devs;
 global $devs;
 $devs = null;
 $devs = null;
 #
 #
-function gpubuttons($count, $info)
+function gpubuttons($count, $rig)
 {
 {
+ global $tablebegin, $tableend;
  global $devs;
  global $devs;
 
 
  $basic = array( 'GPU', 'Enable', 'Disable', 'Restart' );
  $basic = array( 'GPU', 'Enable', 'Disable', 'Restart' );
@@ -306,10 +450,7 @@ function gpubuttons($count, $info)
 			'mem' => 'Memory Clock',
 			'mem' => 'Memory Clock',
 			'vddc' => 'GPU Voltage' );
 			'vddc' => 'GPU Voltage' );
 
 
- $tb = '<tr><td><table border=1 cellpadding=5 cellspacing=0>';
- $te = '</table></td></tr>';
-
- echo $tb.'<tr>';
+ echo $tablebegin.'<tr>';
 
 
  foreach ($basic as $head)
  foreach ($basic as $head)
 	echo "<td>$head</td>";
 	echo "<td>$head</td>";
@@ -332,7 +473,7 @@ function gpubuttons($count, $info)
 		{
 		{
 			echo "<input type=button value='$name $c' onclick='prs(\"gpu";
 			echo "<input type=button value='$name $c' onclick='prs(\"gpu";
 			echo strtolower($name);
 			echo strtolower($name);
-			echo "|$c\")'>";
+			echo "|$c\",$rig)'>";
 		}
 		}
 
 
 		echo '</td>';
 		echo '</td>';
@@ -346,7 +487,7 @@ function gpubuttons($count, $info)
 		else
 		else
 		{
 		{
 			$value = $devs["GPU$c"][$des];
 			$value = $devs["GPU$c"][$des];
-			echo "<input type=button value='Set $c:' onclick='prs2(\"gpu$name|$c\",$n)'>";
+			echo "<input type=button value='Set $c:' onclick='prs2(\"gpu$name|$c\",$n,$rig)'>";
 			echo "<input size=7 type=text name=gi$n value='$value' id=gi$n>";
 			echo "<input size=7 type=text name=gi$n value='$value' id=gi$n>";
 			$n++;
 			$n++;
 		}
 		}
@@ -356,35 +497,37 @@ function gpubuttons($count, $info)
 
 
  }
  }
 
 
- echo '</tr>'.$te;
+ echo '</tr>'.$tableend;
 }
 }
 #
 #
-function processgpus($rd, $ro)
+function processgpus($rig)
 {
 {
  global $error;
  global $error;
+ global $warnfont, $warnoff;
 
 
  $gpus = api('gpucount');
  $gpus = api('gpucount');
 
 
  if ($error != null)
  if ($error != null)
-	echo '<tr><td>Error getting GPU count: '.$rd.$error.$ro.'</td></tr>';
+	echo '<tr><td>Error getting GPU count: '.$warnfont.$error.$warnoff.'</td></tr>';
  else
  else
  {
  {
 	if (!isset($gpus['GPUS']['Count']))
 	if (!isset($gpus['GPUS']['Count']))
-		echo '<tr><td>No GPU count returned: '.$rd.$gpus['STATUS']['STATUS'].' '.$gpus['STATUS']['Msg'].$ro.'</td></tr>';
+		echo '<tr><td>No GPU count returned: '.$warnfont.$gpus['STATUS']['STATUS'].' '.$gpus['STATUS']['Msg'].$ro.'</td></tr>';
 	else
 	else
 	{
 	{
 		$count = $gpus['GPUS']['Count'];
 		$count = $gpus['GPUS']['Count'];
 		if ($count == 0)
 		if ($count == 0)
 			echo '<tr><td>No GPUs</td></tr>';
 			echo '<tr><td>No GPUs</td></tr>';
 		else
 		else
-			gpubuttons($count);
+			gpubuttons($count, $rig);
 	}
 	}
  }
  }
 }
 }
 #
 #
-function process($cmds, $rd, $ro)
+function process($cmds, $rig)
 {
 {
  global $error, $devs;
  global $error, $devs;
+ global $warnfont, $warnoff;
 
 
  foreach ($cmds as $cmd => $des)
  foreach ($cmds as $cmd => $des)
  {
  {
@@ -392,13 +535,13 @@ function process($cmds, $rd, $ro)
 
 
 	if ($error != null)
 	if ($error != null)
 	{
 	{
-		echo "<tr><td>Error getting $des: ";
-		echo $rd.$error.$ro.'</td></tr>';
+		echo "<tr><td colspan=100>Error getting $des: ";
+		echo $warnfont.$error.$warnoff.'</td></tr>';
 		break;
 		break;
 	}
 	}
 	else
 	else
 	{
 	{
-		details($cmd, $process);
+		details($cmd, $process, $rig);
 		echo '<tr><td><br><br></td></tr>';
 		echo '<tr><td><br><br></td></tr>';
 		if ($cmd == 'devs')
 		if ($cmd == 'devs')
 			$devs = $process;
 			$devs = $process;
@@ -406,34 +549,262 @@ function process($cmds, $rd, $ro)
  }
  }
 }
 }
 #
 #
-function display()
+# $head is a hack but this is just a demo anyway :)
+function doforeach($cmd, $des, $sum, $head)
 {
 {
- global $error;
+ global $miner, $port;
+ global $error, $readonly, $notify, $rigs;
+ global $tablebegin, $tableend, $warnfont, $warnoff;
 
 
- $error = null;
+ $header = $head;
+ $anss = array();
 
 
- $rd = '<font color=red><b>';
- $ro = '</b></font>';
+ $count = 0;
+ foreach ($rigs as $rig)
+ {
+	$parts = explode(':', $rig, 2);
+	if (count($parts) == 2)
+	{
+		$miner = $parts[0];
+		$port = $parts[1];
+
+		$ans = api($cmd);
+
+		if ($error != null)
+		{
+			echo "<tr><td colspan=100>Error on rig $count getting $des: ";
+			echo $warnfont.$error.$warnoff.'</td></tr>';
+			$error = null;
+		}
+		else
+			$anss[$count] = $ans;
+	}
+	$count++;
+ }
+
+ if (count($anss) == 0)
+ {
+	echo "<tr><td>Failed to access any rigs successfully</td></tr>";
+	return;
+ }
+
+ $total = array();
+
+ foreach ($anss as $rig => $ans)
+ {
+	foreach ($ans as $item => $row)
+	{
+		if ($item == 'STATUS')
+			continue;
+
+		if (count($row) > count($header))
+		{
+			$header = $head;
+			foreach ($row as $name => $value)
+				if (!isset($header[$name]))
+					$header[$name] = '';
+		}
+
+		if ($sum != null)
+			foreach ($sum as $name)
+			{
+				if (isset($row[$name]))
+				{
+					if (isset($total[$name]))
+						$total[$name] += $row[$name];
+					else
+						$total[$name] = $row[$name];
+				}
+			}
+	}
+ }
+
+ if ($sum != null)
+	$anss['total']['total'] = $total;
+
+ showhead('', null, $header);
+
+ $section = '';
+
+ foreach ($anss as $rig => $ans)
+ {
+	foreach ($ans as $item => $row)
+	{
+		if ($item == 'STATUS')
+			continue;
+
+		echo '<tr>';
+
+		$newsection = preg_replace('/\d/', '', $item);
+		if ($newsection != 'total')
+			$section = $newsection;
+
+		foreach ($header as $name => $x)
+		{
+			if ($name == '')
+			{
+				if ($rig === 'total')
+					echo "<td align=right class=tot>Total:</td>";
+				else
+					echo "<td align=right><input type=button value='Rig $rig' onclick='pr(\"?rig=$rig\",null)'></td>";
+			}
+			else
+			{
+				if (isset($row[$name]))
+					list($showvalue, $class) = fmt($section, $name, $row[$name]);
+				else
+				{
+					$class = '';
+					$showvalue = '&nbsp;';
+				}
+
+				if ($rig === 'total' and $class == '')
+					$class = ' class=tot';
+
+				echo "<td$class align=right>$showvalue</td>";
+			}
+		}
+
+		echo '</tr>';
+	}
+ }
+}
+#
+function doOne($rig, $preprocess)
+{
+ global $error, $readonly, $notify;
+ global $rigs;
+
+ htmlhead(true);
+
+ $error = null;
 
 
  echo "<tr><td><table cellpadding=0 cellspacing=0 border=0><tr><td>";
  echo "<tr><td><table cellpadding=0 cellspacing=0 border=0><tr><td>";
- echo "<input type=button value='Refresh' onclick='pr(\"\",null)'>";
- echo "</td><td width=100%>&nbsp;</td><td>";
- echo "<input type=button value='Quit' onclick='prc(\"quit\",\"Quit CGMiner\")'>";
+ echo "<input type=button value='Refresh' onclick='pr(\"?rig=$rig\",null)'></td>";
+ if (count($rigs) > 1)
+	echo "<td><input type=button value='Summary' onclick='pr(\"\",null)'></td>";
+ echo "<td width=100%>&nbsp;</td><td>";
+ if ($readonly === false)
+ {
+	$msg = 'Quit CGMiner';
+	if (count($rigs) > 1)
+		$msg .= " Rig $rig";
+	echo "<input type=button value='Quit' onclick='prc(\"quit&rig=$rig\",\"$msg\")'>";
+ }
  echo "</td></tr></table></td></tr>";
  echo "</td></tr></table></td></tr>";
 
 
- $arg = trim(getparam('arg', true));
- if ($arg != null and $arg != '')
-	process(array($arg => $arg), $rd, $ro);
+ if ($preprocess != null)
+	process(array($preprocess => $preprocess), $rig);
 
 
  $cmds = array(	'devs'    => 'device list',
  $cmds = array(	'devs'    => 'device list',
 		'summary' => 'summary information',
 		'summary' => 'summary information',
-		'pools'   => 'pool list',
-		'config'  => 'cgminer config');
+		'pools'   => 'pool list');
+
+ if ($notify)
+	$cmds['notify'] = 'device status';
+
+ $cmds['config'] = 'cgminer config';
+
+ process($cmds, $rig);
+
+ if ($error == null && $readonly === false)
+	processgpus($rig);
+}
+#
+function display()
+{
+ global $tablebegin, $tableend;
+ global $miner, $port;
+ global $error, $readonly, $notify, $rigs;
+
+ $rig = trim(getparam('rig', true));
+
+ $arg = trim(getparam('arg', true));
+ $preprocess = null;
+ if ($arg != null and $arg != '')
+ {
+	$num = null;
+	if ($rig != null and $rig != '')
+	{
+		if ($rig >= 0 and $rig < count($rigs))
+			$num = $rig;
+	}
+	else
+		if (count($rigs) == 0)
+			$num = 0;
+
+	if ($num != null)
+	{
+		$parts = explode(':', $rigs[$num], 2);
+		if (count($parts) == 2)
+		{
+			$miner = $parts[0];
+			$port = $parts[1];
+
+			$preprocess = $arg;
+		}
+	}
+ }
+
+ if ($rigs == null or count($rigs) == 0)
+ {
+	echo "<tr><td>No rigs defined</td></tr>";
+	return;
+ }
+
+ if (count($rigs) == 1)
+ {
+	$parts = explode(':', $rigs[0], 2);
+	if (count($parts) == 2)
+	{
+		$miner = $parts[0];
+		$port = $parts[1];
+
+		doOne(0, $preprocess);
+	}
+	else
+		echo '<tr><td>Invalid "$rigs" array</td></tr>';
+
+	return;
+ }
 
 
- process($cmds, $rd, $ro);
+ if ($rig != null and $rig != '' and $rig >= 0 and $rig < count($rigs))
+ {
+	$parts = explode(':', $rigs[$rig], 2);
+	if (count($parts) == 2)
+	{
+		$miner = $parts[0];
+		$port = $parts[1];
+
+		doOne($rig, $preprocess);
+	}
+	else
+		echo '<tr><td>Invalid "$rigs" array</td></tr>';
+
+	return;
+ }
+
+ htmlhead(false);
+
+ echo "<tr><td><table cellpadding=0 cellspacing=0 border=0><tr><td>";
+ echo "<input type=button value='Refresh' onclick='pr(\"\",null)'>";
+ echo "</td></tr></table></td></tr>";
 
 
- if ($error == null)
-	processgpus($rd, $ro);
+ if ($preprocess != null)
+	process(array($preprocess => $preprocess), $rig);
+
+ echo $tablebegin;
+ $sum = array('MHS av', 'Getworks', 'Found Blocks', 'Accepted', 'Rejected', 'Discarded', 'Stale', 'Utility', 'Local Work', 'Total MH');
+ doforeach('summary', 'summary information', $sum, array());
+ echo $tableend;
+ echo '<tr><td><br><br></td></tr>';
+ echo $tablebegin;
+ doforeach('devs', 'device list', $sum, array(''=>'','ID'=>'','Name'=>''));
+ echo $tableend;
+ echo '<tr><td><br><br></td></tr>';
+ echo $tablebegin;
+ doforeach('pools', 'pool list', $sum, array(''=>''));
+ echo $tableend;
 }
 }
 #
 #
 display();
 display();

+ 26 - 23
mkinstalldirs

@@ -335,51 +335,46 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize)
 	/* Create binary filename based on parameters passed to opencl
 	/* Create binary filename based on parameters passed to opencl
 	 * compiler to ensure we only load a binary that matches what would
 	 * compiler to ensure we only load a binary that matches what would
 	 * have otherwise created. The filename is:
 	 * have otherwise created. The filename is:
-	 * name + kernelname + v + vectors + w + work_size + l + sizeof(long) + .bin
+	 * name + kernelname +/- g(offset) + v + vectors + w + work_size + l + sizeof(long) + .bin
 	 */
 	 */
 	char binaryfilename[255];
 	char binaryfilename[255];
 	char filename[255];
 	char filename[255];
 	char numbuf[10];
 	char numbuf[10];
 
 
 	if (gpus[gpu].kernel == KL_NONE) {
 	if (gpus[gpu].kernel == KL_NONE) {
-		if (strstr(vbuff, "844.4") || // Linux 64 bit ATI 2.6 SDK
-		    strstr(vbuff, "851.4") || // Windows 64 bit ""
-		    strstr(vbuff, "831.4")) { // Windows & Linux 32 bit ""
-			if (strstr(name, "Tahiti")) {
-				applog(LOG_INFO, "Selecting poclbm kernel");
-				clState->chosen_kernel = KL_POCLBM;
-			} else {
+		/* Detect all 2.6 SDKs not with Tahiti and use diablo kernel */
+		if (!strstr(name, "Tahiti") &&
+			(strstr(vbuff, "844.4") ||  // Linux 64 bit ATI 2.6 SDK
+			 strstr(vbuff, "851.4") ||  // Windows 64 bit ""
+			 strstr(vbuff, "831.4") ||
+			 strstr(vbuff, "898.1"))) { // 12.2 driver SDK
 				applog(LOG_INFO, "Selecting diablo kernel");
 				applog(LOG_INFO, "Selecting diablo kernel");
 				clState->chosen_kernel = KL_DIABLO;
 				clState->chosen_kernel = KL_DIABLO;
-			}
-		} else if (strstr(vbuff, "898.1") || // Windows 64 bit 12.2 driver
-			   strstr(name, "Tahiti")) { // All non SDK 2.6 79x0
-				applog(LOG_INFO, "Selecting diablo kernel");
-				clState->chosen_kernel = KL_DIABLO;
-		} else if (clState->hasBitAlign) {
-			applog(LOG_INFO, "Selecting phatk kernel");
-			clState->chosen_kernel = KL_PHATK;
-		} else {
+		/* Detect all 7970s, older ATI and NVIDIA and use poclbm */
+		} else if (strstr(name, "Tahiti") || !clState->hasBitAlign) {
 			applog(LOG_INFO, "Selecting poclbm kernel");
 			applog(LOG_INFO, "Selecting poclbm kernel");
 			clState->chosen_kernel = KL_POCLBM;
 			clState->chosen_kernel = KL_POCLBM;
+		/* Use phatk for the rest R5xxx R6xxx */
+		} else {
+			applog(LOG_INFO, "Selecting phatk kernel");
+			clState->chosen_kernel = KL_PHATK;
 		}
 		}
-
 		gpus[gpu].kernel = clState->chosen_kernel;
 		gpus[gpu].kernel = clState->chosen_kernel;
 	} else
 	} else
 		clState->chosen_kernel = gpus[gpu].kernel;
 		clState->chosen_kernel = gpus[gpu].kernel;
 
 
 	/* For some reason 2 vectors is still better even if the card says
 	/* For some reason 2 vectors is still better even if the card says
 	 * otherwise, and many cards lie about their max so use 256 as max
 	 * otherwise, and many cards lie about their max so use 256 as max
-	 * unless explicitly set on the command line. */
-	if (preferred_vwidth > 2)
+	 * unless explicitly set on the command line. Tahiti prefers 1 */
+	if (strstr(name, "Tahiti"))
+		preferred_vwidth = 1;
+	else if (preferred_vwidth > 2)
 		preferred_vwidth = 2;
 		preferred_vwidth = 2;
 
 
 	switch (clState->chosen_kernel) {
 	switch (clState->chosen_kernel) {
 		case KL_POCLBM:
 		case KL_POCLBM:
 			strcpy(filename, POCLBM_KERNNAME".cl");
 			strcpy(filename, POCLBM_KERNNAME".cl");
 			strcpy(binaryfilename, POCLBM_KERNNAME);
 			strcpy(binaryfilename, POCLBM_KERNNAME);
-			/* This kernel prefers to not use vectors */
-			preferred_vwidth = 1;
 			break;
 			break;
 		case KL_PHATK:
 		case KL_PHATK:
 			strcpy(filename, PHATK_KERNNAME".cl");
 			strcpy(filename, PHATK_KERNNAME".cl");
@@ -403,6 +398,10 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize)
 		gpus[gpu].vwidth = preferred_vwidth;
 		gpus[gpu].vwidth = preferred_vwidth;
 	}
 	}
 
 
+	if ((clState->chosen_kernel == KL_POCLBM || clState->chosen_kernel == KL_DIABLO) &&
+		clState->vwidth == 1 && clState->hasOpenCL11plus)
+			clState->goffset = true;
+
 	if (gpus[gpu].work_size && gpus[gpu].work_size <= clState->max_work_size)
 	if (gpus[gpu].work_size && gpus[gpu].work_size <= clState->max_work_size)
 		clState->wsize = gpus[gpu].work_size;
 		clState->wsize = gpus[gpu].work_size;
 	else if (strstr(name, "Tahiti"))
 	else if (strstr(name, "Tahiti"))
@@ -436,7 +435,8 @@ _clState *initCl(unsigned int gpu, char *name, size_t nameSize)
 	}
 	}
 
 
 	strcat(binaryfilename, name);
 	strcat(binaryfilename, name);
-
+	if (clState->goffset)
+		strcat(binaryfilename, "g");
 	strcat(binaryfilename, "v");
 	strcat(binaryfilename, "v");
 	sprintf(numbuf, "%d", clState->vwidth);
 	sprintf(numbuf, "%d", clState->vwidth);
 	strcat(binaryfilename, numbuf);
 	strcat(binaryfilename, numbuf);
@@ -538,6 +538,9 @@ build:
 	} else
 	} else
 		applog(LOG_DEBUG, "BFI_INT patch requiring device not found, will not BFI_INT patch");
 		applog(LOG_DEBUG, "BFI_INT patch requiring device not found, will not BFI_INT patch");
 
 
+	if (clState->goffset)
+		strcat(CompilerOptions, " -D GOFFSET");
+
 	applog(LOG_DEBUG, "CompilerOptions: %s", CompilerOptions);
 	applog(LOG_DEBUG, "CompilerOptions: %s", CompilerOptions);
 	status = clBuildProgram(clState->program, 1, &devices[gpu], CompilerOptions , NULL, NULL);
 	status = clBuildProgram(clState->program, 1, &devices[gpu], CompilerOptions , NULL, NULL);
 	free(CompilerOptions);
 	free(CompilerOptions);

+ 1 - 0
ocl.h

@@ -21,6 +21,7 @@ typedef struct {
 	cl_mem outputBuffer;
 	cl_mem outputBuffer;
 	bool hasBitAlign;
 	bool hasBitAlign;
 	bool hasOpenCL11plus;
 	bool hasOpenCL11plus;
+	bool goffset;
 	cl_uint vwidth;
 	cl_uint vwidth;
 	size_t max_work_size;
 	size_t max_work_size;
 	size_t wsize;
 	size_t wsize;

+ 0 - 1288
poclbm120222.cl

@@ -1,1288 +0,0 @@
-// -ck modified kernel taken from Phoenix taken from poclbm, with aspects of
-// phatk and others.
-// Modified version copyright 2011-2012 Con Kolivas
-
-// This file is taken and modified from the public-domain poclbm project, and
-// we have therefore decided to keep it public-domain in Phoenix.
-
-#ifdef VECTORS4
-	typedef uint4 u;
-#elif defined VECTORS2
-	typedef uint2 u;
-#else
-	typedef uint u;
-#endif
-
-__constant uint K[64] = { 
-    0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
-    0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
-    0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
-    0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
-    0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
-    0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
-    0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
-    0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
-};
-
-
-// This part is not from the stock poclbm kernel. It's part of an optimization
-// added in the Phoenix Miner.
-
-// Some AMD devices have a BFI_INT opcode, which behaves exactly like the
-// SHA-256 ch function, but provides it in exactly one instruction. If
-// detected, use it for ch. Otherwise, construct ch out of simpler logical
-// primitives.
-
-#ifdef BITALIGN
-	#pragma OPENCL EXTENSION cl_amd_media_ops : enable
-	#define rotr(x, y) amd_bitalign((u)x, (u)x, (u)y)
- #ifdef BFI_INT
-	// Well, slight problem... It turns out BFI_INT isn't actually exposed to
-	// OpenCL (or CAL IL for that matter) in any way. However, there is 
-	// a similar instruction, BYTE_ALIGN_INT, which is exposed to OpenCL via
-	// amd_bytealign, takes the same inputs, and provides the same output. 
-	// We can use that as a placeholder for BFI_INT and have the application 
-	// patch it after compilation.
-	
-	// This is the BFI_INT function
-	#define ch(x, y, z) amd_bytealign(x, y, z)
-	
-	// Ma can also be implemented in terms of BFI_INT...
-	#define Ma(x, y, z) amd_bytealign( (z^x), (y), (x) )
- #else // BFI_INT
-	// Later SDKs optimise this to BFI INT without patching and GCN
-	// actually fails if manually patched with BFI_INT
-
-	#define ch(x, y, z) bitselect((u)z, (u)y, (u)x)
-	#define Ma(x, y, z) bitselect((u)x, (u)y, (u)z ^ (u)x)
-#endif
-#else // BITALIGN
-	#define ch(x, y, z) (z ^ (x & (y ^ z)))
-	#define Ma(x, y, z) ((x & z) | (y & (x | z)))
-	#define rotr(x, y) rotate((u)x, (u)(32 - y))
-#endif
-
-// AMD's KernelAnalyzer throws errors compiling the kernel if we use 
-// amd_bytealign on constants with vectors enabled, so we use this to avoid 
-// problems. (this is used 4 times, and likely optimized out by the compiler.)
-#define Ma2(x, y, z) ((y & z) | (x & (y | z)))
-
-__kernel void search(const uint state0, const uint state1, const uint state2, const uint state3,
-						const uint state4, const uint state5, const uint state6, const uint state7,
-						const uint b1, const uint c1,
-						const uint f1, const uint g1, const uint h1,
-						const u base,
-						const uint fw0, const uint fw1, const uint fw2, const uint fw3, const uint fw15, const uint fw01r,
-						const uint fcty_e2,
-						const uint D1A, const uint C1addK5, const uint B1addK6,
-						const uint W16addK16, const uint W17addK17,
-						const uint PreVal4addT1, const uint Preval0,
-						__global uint * output)
-{
-	u W[24];
-	u *Vals = &W[16]; // Now put at W[16] to be in same array
-
-	const u nonce = base + (uint)(get_global_id(0));
-
-
-Vals[0]=Preval0+nonce;
-
-Vals[3]=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
-Vals[3]+=ch(Vals[0],b1,c1);
-Vals[3]+=D1A;
-
-Vals[7]=Vals[3];
-Vals[7]+=h1;
-Vals[4]=PreVal4addT1+nonce;
-Vals[3]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
-
-Vals[2]=C1addK5;
-Vals[2]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
-Vals[2]+=ch(Vals[7],Vals[0],b1);
-
-Vals[6]=Vals[2];
-Vals[6]+=g1;
-Vals[3]+=Ma2(g1,Vals[4],f1);
-Vals[2]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
-
-Vals[1]=B1addK6;
-Vals[1]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
-Vals[1]+=ch(Vals[6],Vals[7],Vals[0]);
-
-Vals[5]=Vals[1];
-Vals[5]+=f1;
-Vals[2]+=Ma2(f1,Vals[3],Vals[4]);
-Vals[1]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
-Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
-Vals[0]+=ch(Vals[5],Vals[6],Vals[7]);
-Vals[0]+=K[7];
-Vals[1]+=Ma(Vals[4],Vals[2],Vals[3]);
-Vals[4]+=Vals[0];
-Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
-Vals[0]+=Ma(Vals[3],Vals[1],Vals[2]);
-Vals[7]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
-Vals[7]+=ch(Vals[4],Vals[5],Vals[6]);
-Vals[7]+=K[8];
-Vals[3]+=Vals[7];
-Vals[7]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
-Vals[7]+=Ma(Vals[2],Vals[0],Vals[1]);
-Vals[6]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
-Vals[6]+=ch(Vals[3],Vals[4],Vals[5]);
-Vals[6]+=K[9];
-Vals[2]+=Vals[6];
-Vals[6]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
-Vals[6]+=Ma(Vals[1],Vals[7],Vals[0]);
-Vals[5]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
-Vals[5]+=ch(Vals[2],Vals[3],Vals[4]);
-Vals[5]+=K[10];
-Vals[1]+=Vals[5];
-Vals[5]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
-Vals[5]+=Ma(Vals[0],Vals[6],Vals[7]);
-Vals[4]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
-Vals[4]+=ch(Vals[1],Vals[2],Vals[3]);
-Vals[4]+=K[11];
-Vals[0]+=Vals[4];
-Vals[4]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
-Vals[4]+=Ma(Vals[7],Vals[5],Vals[6]);
-Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
-Vals[3]+=ch(Vals[0],Vals[1],Vals[2]);
-Vals[3]+=K[12];
-Vals[7]+=Vals[3];
-Vals[3]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
-Vals[3]+=Ma(Vals[6],Vals[4],Vals[5]);
-Vals[2]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
-Vals[2]+=ch(Vals[7],Vals[0],Vals[1]);
-Vals[2]+=K[13];
-Vals[6]+=Vals[2];
-Vals[2]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
-Vals[2]+=Ma(Vals[5],Vals[3],Vals[4]);
-Vals[1]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
-Vals[1]+=ch(Vals[6],Vals[7],Vals[0]);
-Vals[1]+=K[14];
-Vals[5]+=Vals[1];
-Vals[1]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
-Vals[1]+=Ma(Vals[4],Vals[2],Vals[3]);
-Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
-Vals[0]+=ch(Vals[5],Vals[6],Vals[7]);
-Vals[0]+=0xC19BF3F4U;
-Vals[4]+=Vals[0];
-Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
-Vals[7]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
-Vals[7]+=ch(Vals[4],Vals[5],Vals[6]);
-Vals[7]+=W16addK16;
-Vals[0]+=Ma(Vals[3],Vals[1],Vals[2]);
-Vals[3]+=Vals[7];
-Vals[7]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
-Vals[7]+=Ma(Vals[2],Vals[0],Vals[1]);
-Vals[6]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
-Vals[6]+=ch(Vals[3],Vals[4],Vals[5]);
-Vals[6]+=W17addK17;
-Vals[2]+=Vals[6];
-Vals[6]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
-
-W[2]=(rotr(nonce,7)^rotr(nonce,18)^(nonce>>3U));
-W[2]+=fw2;
-Vals[5]+=W[2];
-Vals[5]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
-Vals[5]+=ch(Vals[2],Vals[3],Vals[4]);
-Vals[5]+=K[18];
-Vals[6]+=Ma(Vals[1],Vals[7],Vals[0]);
-Vals[1]+=Vals[5];
-Vals[5]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
-Vals[5]+=Ma(Vals[0],Vals[6],Vals[7]);
-
-W[3]=nonce;
-W[3]+=fw3;
-Vals[4]+=W[3];
-Vals[4]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
-Vals[4]+=ch(Vals[1],Vals[2],Vals[3]);
-Vals[4]+=K[19];
-Vals[0]+=Vals[4];
-Vals[4]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
-
-W[4]=(rotr(W[2],17)^rotr(W[2],19)^(W[2]>>10U));
-W[4]+=0x80000000U;
-Vals[3]+=W[4];
-Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
-Vals[3]+=ch(Vals[0],Vals[1],Vals[2]);
-Vals[3]+=K[20];
-Vals[4]+=Ma(Vals[7],Vals[5],Vals[6]);
-Vals[7]+=Vals[3];
-Vals[3]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
-Vals[3]+=Ma(Vals[6],Vals[4],Vals[5]);
-
-W[5]=(rotr(W[3],17)^rotr(W[3],19)^(W[3]>>10U));
-Vals[2]+=W[5];
-Vals[2]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
-Vals[2]+=ch(Vals[7],Vals[0],Vals[1]);
-Vals[2]+=K[21];
-Vals[6]+=Vals[2];
-Vals[2]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
-
-W[6]=(rotr(W[4],17)^rotr(W[4],19)^(W[4]>>10U));
-W[6]+=0x00000280U;
-Vals[1]+=W[6];
-Vals[1]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
-Vals[1]+=ch(Vals[6],Vals[7],Vals[0]);
-Vals[1]+=K[22];
-Vals[2]+=Ma(Vals[5],Vals[3],Vals[4]);
-Vals[5]+=Vals[1];
-Vals[1]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
-Vals[1]+=Ma(Vals[4],Vals[2],Vals[3]);
-
-W[7]=(rotr(W[5],17)^rotr(W[5],19)^(W[5]>>10U));
-W[7]+=fw0;
-Vals[0]+=W[7];
-Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
-Vals[0]+=ch(Vals[5],Vals[6],Vals[7]);
-Vals[0]+=K[23];
-
-Vals[4]+=Vals[0];
-Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
-
-W[8]=(rotr(W[6],17)^rotr(W[6],19)^(W[6]>>10U));
-W[8]+=fw1;
-Vals[7]+=W[8];
-Vals[7]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
-Vals[7]+=ch(Vals[4],Vals[5],Vals[6]);
-Vals[7]+=K[24];
-Vals[0]+=Ma(Vals[3],Vals[1],Vals[2]);
-Vals[3]+=Vals[7];
-Vals[7]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
-Vals[7]+=Ma(Vals[2],Vals[0],Vals[1]);
-
-W[9]=W[2];
-W[9]+=(rotr(W[7],17)^rotr(W[7],19)^(W[7]>>10U));
-Vals[6]+=W[9];
-Vals[6]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
-Vals[6]+=ch(Vals[3],Vals[4],Vals[5]);
-Vals[6]+=K[25];
-
-Vals[2]+=Vals[6];
-Vals[6]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
-
-W[10]=W[3];
-W[10]+=(rotr(W[8],17)^rotr(W[8],19)^(W[8]>>10U));
-Vals[5]+=W[10];
-Vals[5]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
-Vals[5]+=ch(Vals[2],Vals[3],Vals[4]);
-Vals[5]+=K[26];
-Vals[6]+=Ma(Vals[1],Vals[7],Vals[0]);
-Vals[1]+=Vals[5];
-Vals[5]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
-Vals[5]+=Ma(Vals[0],Vals[6],Vals[7]);
-
-W[11]=W[4];
-W[11]+=(rotr(W[9],17)^rotr(W[9],19)^(W[9]>>10U));
-Vals[4]+=W[11];
-Vals[4]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
-Vals[4]+=ch(Vals[1],Vals[2],Vals[3]);
-Vals[4]+=K[27];
-Vals[0]+=Vals[4];
-Vals[4]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
-
-W[12]=W[5];
-W[12]+=(rotr(W[10],17)^rotr(W[10],19)^(W[10]>>10U));
-Vals[3]+=W[12];
-Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
-Vals[3]+=ch(Vals[0],Vals[1],Vals[2]);
-Vals[3]+=K[28];
-Vals[4]+=Ma(Vals[7],Vals[5],Vals[6]);
-Vals[7]+=Vals[3];
-Vals[3]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
-Vals[3]+=Ma(Vals[6],Vals[4],Vals[5]);
-
-W[13]=W[6];
-W[13]+=(rotr(W[11],17)^rotr(W[11],19)^(W[11]>>10U));
-Vals[2]+=W[13];
-Vals[2]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
-Vals[2]+=ch(Vals[7],Vals[0],Vals[1]);
-Vals[2]+=K[29];
-Vals[6]+=Vals[2];
-Vals[2]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
-
-W[14]=0x00a00055U;
-W[14]+=W[7];
-W[14]+=(rotr(W[12],17)^rotr(W[12],19)^(W[12]>>10U));
-Vals[1]+=W[14];
-Vals[1]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
-Vals[1]+=ch(Vals[6],Vals[7],Vals[0]);
-Vals[1]+=K[30];
-Vals[2]+=Ma(Vals[5],Vals[3],Vals[4]);
-Vals[5]+=Vals[1];
-Vals[1]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
-Vals[1]+=Ma(Vals[4],Vals[2],Vals[3]);
-
-W[15]=fw15;
-W[15]+=W[8];
-W[15]+=(rotr(W[13],17)^rotr(W[13],19)^(W[13]>>10U));
-Vals[0]+=W[15];
-Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
-Vals[0]+=ch(Vals[5],Vals[6],Vals[7]);
-Vals[0]+=K[31];
-Vals[4]+=Vals[0];
-Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
-
-W[0]=fw01r;
-W[0]+=W[9];
-W[0]+=(rotr(W[14],17)^rotr(W[14],19)^(W[14]>>10U));
-Vals[7]+=W[0];
-Vals[7]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
-Vals[7]+=ch(Vals[4],Vals[5],Vals[6]);
-Vals[7]+=K[32];
-Vals[0]+=Ma(Vals[3],Vals[1],Vals[2]);
-Vals[3]+=Vals[7];
-Vals[7]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
-Vals[7]+=Ma(Vals[2],Vals[0],Vals[1]);
-
-W[1]=fw1;
-W[1]+=(rotr(W[2],7)^rotr(W[2],18)^(W[2]>>3U));
-W[1]+=W[10];
-W[1]+=(rotr(W[15],17)^rotr(W[15],19)^(W[15]>>10U));
-Vals[6]+=W[1];
-Vals[6]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
-Vals[6]+=ch(Vals[3],Vals[4],Vals[5]);
-Vals[6]+=K[33];
-Vals[2]+=Vals[6];
-Vals[6]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
-W[2]+=(rotr(W[3],7)^rotr(W[3],18)^(W[3]>>3U));
-W[2]+=W[11];
-Vals[5]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
-Vals[5]+=ch(Vals[2],Vals[3],Vals[4]);
-W[2]+=(rotr(W[0],17)^rotr(W[0],19)^(W[0]>>10U));
-Vals[5]+=K[34];
-Vals[5]+=W[2];
-Vals[6]+=Ma(Vals[1],Vals[7],Vals[0]);
-Vals[1]+=Vals[5];
-Vals[5]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
-Vals[5]+=Ma(Vals[0],Vals[6],Vals[7]);
-W[3]+=(rotr(W[4],7)^rotr(W[4],18)^(W[4]>>3U));
-W[3]+=W[12];
-Vals[4]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
-Vals[4]+=ch(Vals[1],Vals[2],Vals[3]);
-Vals[4]+=K[35];
-W[3]+=(rotr(W[1],17)^rotr(W[1],19)^(W[1]>>10U));
-Vals[4]+=W[3];
-Vals[0]+=Vals[4];
-Vals[4]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
-W[4]+=(rotr(W[5],7)^rotr(W[5],18)^(W[5]>>3U));
-W[4]+=W[13];
-Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
-Vals[3]+=ch(Vals[0],Vals[1],Vals[2]);
-W[4]+=(rotr(W[2],17)^rotr(W[2],19)^(W[2]>>10U));
-Vals[3]+=K[36];
-Vals[3]+=W[4];
-Vals[4]+=Ma(Vals[7],Vals[5],Vals[6]);
-Vals[7]+=Vals[3];
-Vals[3]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
-Vals[3]+=Ma(Vals[6],Vals[4],Vals[5]);
-W[5]+=(rotr(W[6],7)^rotr(W[6],18)^(W[6]>>3U));
-W[5]+=W[14];
-Vals[2]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
-Vals[2]+=ch(Vals[7],Vals[0],Vals[1]);
-Vals[2]+=K[37];
-W[5]+=(rotr(W[3],17)^rotr(W[3],19)^(W[3]>>10U));
-Vals[2]+=W[5];
-Vals[6]+=Vals[2];
-Vals[2]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
-W[6]+=(rotr(W[7],7)^rotr(W[7],18)^(W[7]>>3U));
-W[6]+=W[15];
-Vals[1]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
-Vals[1]+=ch(Vals[6],Vals[7],Vals[0]);
-W[6]+=(rotr(W[4],17)^rotr(W[4],19)^(W[4]>>10U));
-Vals[1]+=K[38];
-Vals[1]+=W[6];
-Vals[2]+=Ma(Vals[5],Vals[3],Vals[4]);
-Vals[5]+=Vals[1];
-Vals[1]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
-Vals[1]+=Ma(Vals[4],Vals[2],Vals[3]);
-W[7]+=(rotr(W[8],7)^rotr(W[8],18)^(W[8]>>3U));
-W[7]+=W[0];
-Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
-Vals[0]+=ch(Vals[5],Vals[6],Vals[7]);
-Vals[0]+=K[39];
-W[7]+=(rotr(W[5],17)^rotr(W[5],19)^(W[5]>>10U));
-Vals[0]+=W[7];
-Vals[4]+=Vals[0];
-Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
-W[8]+=(rotr(W[9],7)^rotr(W[9],18)^(W[9]>>3U));
-W[8]+=W[1];
-Vals[7]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
-Vals[7]+=ch(Vals[4],Vals[5],Vals[6]);
-W[8]+=(rotr(W[6],17)^rotr(W[6],19)^(W[6]>>10U));
-Vals[7]+=K[40];
-Vals[7]+=W[8];
-Vals[0]+=Ma(Vals[3],Vals[1],Vals[2]);
-Vals[3]+=Vals[7];
-Vals[7]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
-Vals[7]+=Ma(Vals[2],Vals[0],Vals[1]);
-W[9]+=(rotr(W[10],7)^rotr(W[10],18)^(W[10]>>3U));
-W[9]+=W[2];
-Vals[6]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
-Vals[6]+=ch(Vals[3],Vals[4],Vals[5]);
-Vals[6]+=K[41];
-W[9]+=(rotr(W[7],17)^rotr(W[7],19)^(W[7]>>10U));
-Vals[6]+=W[9];
-Vals[2]+=Vals[6];
-Vals[6]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
-W[10]+=(rotr(W[11],7)^rotr(W[11],18)^(W[11]>>3U));
-W[10]+=W[3];
-Vals[5]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
-Vals[5]+=ch(Vals[2],Vals[3],Vals[4]);
-W[10]+=(rotr(W[8],17)^rotr(W[8],19)^(W[8]>>10U));
-Vals[5]+=K[42];
-Vals[5]+=W[10];
-Vals[6]+=Ma(Vals[1],Vals[7],Vals[0]);
-Vals[1]+=Vals[5];
-Vals[5]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
-Vals[5]+=Ma(Vals[0],Vals[6],Vals[7]);
-W[11]+=(rotr(W[12],7)^rotr(W[12],18)^(W[12]>>3U));
-W[11]+=W[4];
-Vals[4]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
-Vals[4]+=ch(Vals[1],Vals[2],Vals[3]);
-Vals[4]+=K[43];
-W[11]+=(rotr(W[9],17)^rotr(W[9],19)^(W[9]>>10U));
-Vals[4]+=W[11];
-Vals[0]+=Vals[4];
-Vals[4]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
-W[12]+=(rotr(W[13],7)^rotr(W[13],18)^(W[13]>>3U));
-W[12]+=W[5];
-Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
-Vals[3]+=ch(Vals[0],Vals[1],Vals[2]);
-W[12]+=(rotr(W[10],17)^rotr(W[10],19)^(W[10]>>10U));
-Vals[3]+=K[44];
-Vals[3]+=W[12];
-Vals[4]+=Ma(Vals[7],Vals[5],Vals[6]);
-Vals[7]+=Vals[3];
-Vals[3]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
-Vals[3]+=Ma(Vals[6],Vals[4],Vals[5]);
-W[13]+=(rotr(W[14],7)^rotr(W[14],18)^(W[14]>>3U));
-W[13]+=W[6];
-Vals[2]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
-Vals[2]+=ch(Vals[7],Vals[0],Vals[1]);
-Vals[2]+=K[45];
-W[13]+=(rotr(W[11],17)^rotr(W[11],19)^(W[11]>>10U));
-Vals[2]+=W[13];
-Vals[6]+=Vals[2];
-Vals[2]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
-W[14]+=(rotr(W[15],7)^rotr(W[15],18)^(W[15]>>3U));
-W[14]+=W[7];
-Vals[1]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
-Vals[1]+=ch(Vals[6],Vals[7],Vals[0]);
-W[14]+=(rotr(W[12],17)^rotr(W[12],19)^(W[12]>>10U));
-Vals[1]+=K[46];
-Vals[1]+=W[14];
-Vals[2]+=Ma(Vals[5],Vals[3],Vals[4]);
-Vals[5]+=Vals[1];
-Vals[1]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
-Vals[1]+=Ma(Vals[4],Vals[2],Vals[3]);
-W[15]+=(rotr(W[0],7)^rotr(W[0],18)^(W[0]>>3U));
-W[15]+=W[8];
-Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
-Vals[0]+=ch(Vals[5],Vals[6],Vals[7]);
-Vals[0]+=K[47];
-W[15]+=(rotr(W[13],17)^rotr(W[13],19)^(W[13]>>10U));
-Vals[0]+=W[15];
-Vals[4]+=Vals[0];
-Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
-W[0]+=(rotr(W[1],7)^rotr(W[1],18)^(W[1]>>3U));
-W[0]+=W[9];
-Vals[7]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
-Vals[7]+=ch(Vals[4],Vals[5],Vals[6]);
-W[0]+=(rotr(W[14],17)^rotr(W[14],19)^(W[14]>>10U));
-Vals[7]+=K[48];
-Vals[7]+=W[0];
-Vals[0]+=Ma(Vals[3],Vals[1],Vals[2]);
-Vals[3]+=Vals[7];
-Vals[7]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
-Vals[7]+=Ma(Vals[2],Vals[0],Vals[1]);
-W[1]+=(rotr(W[2],7)^rotr(W[2],18)^(W[2]>>3U));
-W[1]+=W[10];
-Vals[6]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
-Vals[6]+=ch(Vals[3],Vals[4],Vals[5]);
-Vals[6]+=K[49];
-W[1]+=(rotr(W[15],17)^rotr(W[15],19)^(W[15]>>10U));
-Vals[6]+=W[1];
-Vals[2]+=Vals[6];
-Vals[6]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
-W[2]+=(rotr(W[3],7)^rotr(W[3],18)^(W[3]>>3U));
-W[2]+=W[11];
-Vals[5]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
-Vals[5]+=ch(Vals[2],Vals[3],Vals[4]);
-W[2]+=(rotr(W[0],17)^rotr(W[0],19)^(W[0]>>10U));
-Vals[5]+=K[50];
-Vals[5]+=W[2];
-Vals[6]+=Ma(Vals[1],Vals[7],Vals[0]);
-Vals[1]+=Vals[5];
-Vals[5]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
-Vals[5]+=Ma(Vals[0],Vals[6],Vals[7]);
-W[3]+=(rotr(W[4],7)^rotr(W[4],18)^(W[4]>>3U));
-W[3]+=W[12];
-Vals[4]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
-Vals[4]+=ch(Vals[1],Vals[2],Vals[3]);
-Vals[4]+=K[51];
-W[3]+=(rotr(W[1],17)^rotr(W[1],19)^(W[1]>>10U));
-Vals[4]+=W[3];
-Vals[0]+=Vals[4];
-Vals[4]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
-W[4]+=(rotr(W[5],7)^rotr(W[5],18)^(W[5]>>3U));
-W[4]+=W[13];
-Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
-Vals[3]+=ch(Vals[0],Vals[1],Vals[2]);
-W[4]+=(rotr(W[2],17)^rotr(W[2],19)^(W[2]>>10U));
-Vals[3]+=K[52];
-Vals[3]+=W[4];
-Vals[4]+=Ma(Vals[7],Vals[5],Vals[6]);
-Vals[7]+=Vals[3];
-Vals[3]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
-Vals[3]+=Ma(Vals[6],Vals[4],Vals[5]);
-W[5]+=(rotr(W[6],7)^rotr(W[6],18)^(W[6]>>3U));
-W[5]+=W[14];
-Vals[2]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
-Vals[2]+=ch(Vals[7],Vals[0],Vals[1]);
-Vals[2]+=K[53];
-W[5]+=(rotr(W[3],17)^rotr(W[3],19)^(W[3]>>10U));
-Vals[2]+=W[5];
-Vals[6]+=Vals[2];
-Vals[2]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
-W[6]+=(rotr(W[7],7)^rotr(W[7],18)^(W[7]>>3U));
-W[6]+=W[15];
-Vals[1]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
-Vals[1]+=ch(Vals[6],Vals[7],Vals[0]);
-W[6]+=(rotr(W[4],17)^rotr(W[4],19)^(W[4]>>10U));
-Vals[1]+=K[54];
-Vals[1]+=W[6];
-Vals[2]+=Ma(Vals[5],Vals[3],Vals[4]);
-Vals[5]+=Vals[1];
-Vals[1]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
-Vals[1]+=Ma(Vals[4],Vals[2],Vals[3]);
-W[7]+=(rotr(W[8],7)^rotr(W[8],18)^(W[8]>>3U));
-W[7]+=W[0];
-Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
-Vals[0]+=ch(Vals[5],Vals[6],Vals[7]);
-Vals[0]+=K[55];
-W[7]+=(rotr(W[5],17)^rotr(W[5],19)^(W[5]>>10U));
-Vals[0]+=W[7];
-Vals[4]+=Vals[0];
-Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
-W[8]+=(rotr(W[9],7)^rotr(W[9],18)^(W[9]>>3U));
-W[8]+=W[1];
-Vals[7]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
-Vals[7]+=ch(Vals[4],Vals[5],Vals[6]);
-W[8]+=(rotr(W[6],17)^rotr(W[6],19)^(W[6]>>10U));
-Vals[7]+=K[56];
-Vals[7]+=W[8];
-Vals[0]+=Ma(Vals[3],Vals[1],Vals[2]);
-Vals[3]+=Vals[7];
-Vals[7]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
-Vals[7]+=Ma(Vals[2],Vals[0],Vals[1]);
-W[9]+=(rotr(W[10],7)^rotr(W[10],18)^(W[10]>>3U));
-W[9]+=W[2];
-Vals[6]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
-Vals[6]+=ch(Vals[3],Vals[4],Vals[5]);
-Vals[6]+=K[57];
-W[9]+=(rotr(W[7],17)^rotr(W[7],19)^(W[7]>>10U));
-Vals[6]+=W[9];
-Vals[2]+=Vals[6];
-Vals[6]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
-W[10]+=(rotr(W[11],7)^rotr(W[11],18)^(W[11]>>3U));
-W[10]+=W[3];
-Vals[5]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
-Vals[5]+=ch(Vals[2],Vals[3],Vals[4]);
-W[10]+=(rotr(W[8],17)^rotr(W[8],19)^(W[8]>>10U));
-Vals[5]+=K[58];
-Vals[5]+=W[10];
-Vals[6]+=Ma(Vals[1],Vals[7],Vals[0]);
-Vals[1]+=Vals[5];
-Vals[5]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
-Vals[5]+=Ma(Vals[0],Vals[6],Vals[7]);
-W[11]+=(rotr(W[12],7)^rotr(W[12],18)^(W[12]>>3U));
-W[11]+=W[4];
-Vals[4]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
-Vals[4]+=ch(Vals[1],Vals[2],Vals[3]);
-Vals[4]+=K[59];
-W[11]+=(rotr(W[9],17)^rotr(W[9],19)^(W[9]>>10U));
-Vals[4]+=W[11];
-Vals[0]+=Vals[4];
-Vals[4]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
-W[12]+=(rotr(W[13],7)^rotr(W[13],18)^(W[13]>>3U));
-W[12]+=W[5];
-Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
-Vals[3]+=ch(Vals[0],Vals[1],Vals[2]);
-W[12]+=(rotr(W[10],17)^rotr(W[10],19)^(W[10]>>10U));
-Vals[3]+=K[60];
-Vals[3]+=W[12];
-Vals[4]+=Ma(Vals[7],Vals[5],Vals[6]);
-Vals[7]+=Vals[3];
-Vals[3]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
-Vals[3]+=Ma(Vals[6],Vals[4],Vals[5]);
-W[13]+=(rotr(W[14],7)^rotr(W[14],18)^(W[14]>>3U));
-W[13]+=W[6];
-Vals[2]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
-Vals[2]+=ch(Vals[7],Vals[0],Vals[1]);
-Vals[2]+=K[61];
-W[13]+=(rotr(W[11],17)^rotr(W[11],19)^(W[11]>>10U));
-Vals[2]+=W[13];
-Vals[6]+=Vals[2];
-Vals[2]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
-W[14]+=(rotr(W[15],7)^rotr(W[15],18)^(W[15]>>3U));
-W[14]+=W[7];
-Vals[1]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
-Vals[1]+=ch(Vals[6],Vals[7],Vals[0]);
-W[14]+=(rotr(W[12],17)^rotr(W[12],19)^(W[12]>>10U));
-Vals[1]+=K[62];
-Vals[1]+=W[14];
-Vals[2]+=Ma(Vals[5],Vals[3],Vals[4]);
-Vals[5]+=Vals[1];
-Vals[1]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
-Vals[1]+=Ma(Vals[4],Vals[2],Vals[3]);
-W[15]+=(rotr(W[0],7)^rotr(W[0],18)^(W[0]>>3U));
-W[15]+=W[8];
-Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
-Vals[0]+=ch(Vals[5],Vals[6],Vals[7]);
-Vals[0]+=K[63];
-W[15]+=(rotr(W[13],17)^rotr(W[13],19)^(W[13]>>10U));
-Vals[0]+=W[15];
-Vals[4]+=Vals[0];
-Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
-Vals[0]+=Ma(Vals[3],Vals[1],Vals[2]);
-
-W[0]=Vals[0];
-
-W[7]=state7;
-W[7]+=Vals[7];
-
-Vals[7]=0xF377ED68U;
-W[0]+=state0;
-Vals[7]+=W[0];
-
-W[3]=state3;
-W[3]+=Vals[3];
-
-Vals[3]=0xa54ff53aU;
-Vals[3]+=Vals[7];
-
-W[1]=Vals[1];
-W[1]+=state1;
-
-W[6]=state6;
-W[6]+=Vals[6];
-
-Vals[6]=0x90BB1E3CU;
-Vals[6]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
-Vals[6]+=(0x9b05688cU^(Vals[3]&0xca0b3af3U));
-
-W[2]=state2;
-W[2]+=Vals[2];
-
-Vals[2]=0x3c6ef372U;
-Vals[6]+=W[1];
-Vals[2]+=Vals[6];
-Vals[7]+=0x08909ae5U;
-Vals[6]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
-
-W[5]=state5;
-W[5]+=Vals[5];
-
-Vals[5]=0x50C6645BU;
-Vals[5]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
-Vals[5]+=ch(Vals[2],Vals[3],0x510e527fU);
-Vals[5]+=W[2];
-
-Vals[1]=0xbb67ae85U;
-Vals[1]+=Vals[5];
-Vals[6]+=Ma2(0xbb67ae85U,Vals[7],0x6a09e667U);
-Vals[5]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
-
-W[4]=state4;
-W[4]+=Vals[4];
-
-Vals[4]=0x3AC42E24U;
-Vals[4]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
-Vals[4]+=ch(Vals[1],Vals[2],Vals[3]);
-Vals[4]+=W[3];
-
-Vals[0]=Vals[4];
-Vals[0]+=0x6a09e667U;
-Vals[5]+=Ma2(0x6a09e667U,Vals[6],Vals[7]);
-Vals[4]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
-Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
-Vals[3]+=ch(Vals[0],Vals[1],Vals[2]);
-Vals[3]+=K[4];
-Vals[3]+=W[4];
-Vals[4]+=Ma(Vals[7],Vals[5],Vals[6]);
-Vals[7]+=Vals[3];
-Vals[3]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
-Vals[3]+=Ma(Vals[6],Vals[4],Vals[5]);
-Vals[2]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
-Vals[2]+=ch(Vals[7],Vals[0],Vals[1]);
-Vals[2]+=K[5];
-Vals[2]+=W[5];
-Vals[6]+=Vals[2];
-Vals[2]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
-Vals[1]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
-Vals[1]+=ch(Vals[6],Vals[7],Vals[0]);
-Vals[1]+=K[6];
-Vals[1]+=W[6];
-Vals[2]+=Ma(Vals[5],Vals[3],Vals[4]);
-Vals[5]+=Vals[1];
-Vals[1]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
-Vals[1]+=Ma(Vals[4],Vals[2],Vals[3]);
-Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
-Vals[0]+=ch(Vals[5],Vals[6],Vals[7]);
-Vals[0]+=K[7];
-Vals[0]+=W[7];
-Vals[4]+=Vals[0];
-Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
-Vals[7]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
-Vals[7]+=ch(Vals[4],Vals[5],Vals[6]);
-Vals[7]+=0x5807AA98U;
-Vals[0]+=Ma(Vals[3],Vals[1],Vals[2]);
-Vals[3]+=Vals[7];
-Vals[7]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
-Vals[7]+=Ma(Vals[2],Vals[0],Vals[1]);
-Vals[6]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
-Vals[6]+=ch(Vals[3],Vals[4],Vals[5]);
-Vals[6]+=K[9];
-Vals[2]+=Vals[6];
-Vals[6]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
-Vals[6]+=Ma(Vals[1],Vals[7],Vals[0]);
-Vals[5]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
-Vals[5]+=ch(Vals[2],Vals[3],Vals[4]);
-Vals[5]+=K[10];
-Vals[1]+=Vals[5];
-Vals[5]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
-Vals[5]+=Ma(Vals[0],Vals[6],Vals[7]);
-Vals[4]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
-Vals[4]+=ch(Vals[1],Vals[2],Vals[3]);
-Vals[4]+=K[11];
-Vals[0]+=Vals[4];
-Vals[4]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
-Vals[4]+=Ma(Vals[7],Vals[5],Vals[6]);
-Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
-Vals[3]+=ch(Vals[0],Vals[1],Vals[2]);
-Vals[3]+=K[12];
-Vals[7]+=Vals[3];
-Vals[3]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
-Vals[3]+=Ma(Vals[6],Vals[4],Vals[5]);
-Vals[2]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
-Vals[2]+=ch(Vals[7],Vals[0],Vals[1]);
-Vals[2]+=K[13];
-Vals[6]+=Vals[2];
-Vals[2]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
-Vals[2]+=Ma(Vals[5],Vals[3],Vals[4]);
-Vals[1]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
-Vals[1]+=ch(Vals[6],Vals[7],Vals[0]);
-Vals[1]+=K[14];
-Vals[5]+=Vals[1];
-Vals[1]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
-Vals[1]+=Ma(Vals[4],Vals[2],Vals[3]);
-Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
-Vals[0]+=ch(Vals[5],Vals[6],Vals[7]);
-Vals[0]+=0xC19BF274U;
-Vals[4]+=Vals[0];
-Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
-Vals[7]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
-Vals[7]+=ch(Vals[4],Vals[5],Vals[6]);
-W[0]+=(rotr(W[1],7)^rotr(W[1],18)^(W[1]>>3U));
-Vals[7]+=K[16];
-Vals[7]+=W[0];
-Vals[0]+=Ma(Vals[3],Vals[1],Vals[2]);
-Vals[3]+=Vals[7];
-Vals[7]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
-Vals[7]+=Ma(Vals[2],Vals[0],Vals[1]);
-W[1]+=(rotr(W[2],7)^rotr(W[2],18)^(W[2]>>3U));
-W[1]+=0x00a00000U;
-Vals[6]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
-Vals[6]+=ch(Vals[3],Vals[4],Vals[5]);
-Vals[6]+=K[17];
-Vals[6]+=W[1];
-Vals[2]+=Vals[6];
-Vals[6]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
-W[2]+=(rotr(W[3],7)^rotr(W[3],18)^(W[3]>>3U));
-W[2]+=(rotr(W[0],17)^rotr(W[0],19)^(W[0]>>10U));
-Vals[5]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
-Vals[5]+=ch(Vals[2],Vals[3],Vals[4]);
-Vals[5]+=K[18];
-Vals[5]+=W[2];
-Vals[6]+=Ma(Vals[1],Vals[7],Vals[0]);
-Vals[1]+=Vals[5];
-Vals[5]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
-Vals[5]+=Ma(Vals[0],Vals[6],Vals[7]);
-W[3]+=(rotr(W[4],7)^rotr(W[4],18)^(W[4]>>3U));
-W[3]+=(rotr(W[1],17)^rotr(W[1],19)^(W[1]>>10U));
-Vals[4]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
-Vals[4]+=ch(Vals[1],Vals[2],Vals[3]);
-Vals[4]+=K[19];
-Vals[4]+=W[3];
-Vals[0]+=Vals[4];
-Vals[4]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
-W[4]+=(rotr(W[5],7)^rotr(W[5],18)^(W[5]>>3U));
-W[4]+=(rotr(W[2],17)^rotr(W[2],19)^(W[2]>>10U));
-Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
-Vals[3]+=ch(Vals[0],Vals[1],Vals[2]);
-Vals[3]+=K[20];
-Vals[3]+=W[4];
-Vals[4]+=Ma(Vals[7],Vals[5],Vals[6]);
-Vals[7]+=Vals[3];
-Vals[3]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
-Vals[3]+=Ma(Vals[6],Vals[4],Vals[5]);
-W[5]+=(rotr(W[6],7)^rotr(W[6],18)^(W[6]>>3U));
-W[5]+=(rotr(W[3],17)^rotr(W[3],19)^(W[3]>>10U));
-Vals[2]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
-Vals[2]+=ch(Vals[7],Vals[0],Vals[1]);
-Vals[2]+=K[21];
-Vals[2]+=W[5];
-Vals[6]+=Vals[2];
-Vals[2]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
-W[6]+=(rotr(W[7],7)^rotr(W[7],18)^(W[7]>>3U));
-W[6]+=0x00000100U;
-Vals[1]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
-Vals[1]+=ch(Vals[6],Vals[7],Vals[0]);
-W[6]+=(rotr(W[4],17)^rotr(W[4],19)^(W[4]>>10U));
-Vals[1]+=K[22];
-Vals[1]+=W[6];
-Vals[2]+=Ma(Vals[5],Vals[3],Vals[4]);
-Vals[5]+=Vals[1];
-Vals[1]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
-Vals[1]+=Ma(Vals[4],Vals[2],Vals[3]);
-W[7]+=0x11002000U;
-W[7]+=W[0];
-Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
-Vals[0]+=ch(Vals[5],Vals[6],Vals[7]);
-Vals[0]+=K[23];
-W[7]+=(rotr(W[5],17)^rotr(W[5],19)^(W[5]>>10U));
-Vals[0]+=W[7];
-Vals[4]+=Vals[0];
-Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
-
-W[8]=0x80000000U;
-W[8]+=W[1];
-W[8]+=(rotr(W[6],17)^rotr(W[6],19)^(W[6]>>10U));
-Vals[7]+=W[8];
-Vals[7]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
-Vals[7]+=ch(Vals[4],Vals[5],Vals[6]);
-Vals[7]+=K[24];
-Vals[0]+=Ma(Vals[3],Vals[1],Vals[2]);
-Vals[3]+=Vals[7];
-Vals[7]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
-Vals[7]+=Ma(Vals[2],Vals[0],Vals[1]);
-
-W[9]=W[2];
-W[9]+=(rotr(W[7],17)^rotr(W[7],19)^(W[7]>>10U));
-Vals[6]+=W[9];
-Vals[6]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
-Vals[6]+=ch(Vals[3],Vals[4],Vals[5]);
-Vals[6]+=K[25];
-Vals[2]+=Vals[6];
-Vals[6]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
-
-W[10]=W[3];
-W[10]+=(rotr(W[8],17)^rotr(W[8],19)^(W[8]>>10U));
-Vals[5]+=W[10];
-Vals[5]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
-Vals[5]+=ch(Vals[2],Vals[3],Vals[4]);
-Vals[5]+=K[26];
-Vals[6]+=Ma(Vals[1],Vals[7],Vals[0]);
-Vals[1]+=Vals[5];
-Vals[5]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
-Vals[5]+=Ma(Vals[0],Vals[6],Vals[7]);
-
-W[11]=W[4];
-W[11]+=(rotr(W[9],17)^rotr(W[9],19)^(W[9]>>10U));
-Vals[4]+=W[11];
-Vals[4]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
-Vals[4]+=ch(Vals[1],Vals[2],Vals[3]);
-Vals[4]+=K[27];
-Vals[0]+=Vals[4];
-Vals[4]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
-
-W[12]=W[5];
-W[12]+=(rotr(W[10],17)^rotr(W[10],19)^(W[10]>>10U));
-Vals[3]+=W[12];
-Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
-Vals[3]+=ch(Vals[0],Vals[1],Vals[2]);
-Vals[3]+=K[28];
-Vals[4]+=Ma(Vals[7],Vals[5],Vals[6]);
-Vals[7]+=Vals[3];
-Vals[3]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
-Vals[3]+=Ma(Vals[6],Vals[4],Vals[5]);
-
-W[13]=W[6];
-W[13]+=(rotr(W[11],17)^rotr(W[11],19)^(W[11]>>10U));
-Vals[2]+=W[13];
-Vals[2]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
-Vals[2]+=ch(Vals[7],Vals[0],Vals[1]);
-Vals[2]+=K[29];
-Vals[6]+=Vals[2];
-Vals[2]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
-
-W[14]=0x00400022U;
-W[14]+=W[7];
-W[14]+=(rotr(W[12],17)^rotr(W[12],19)^(W[12]>>10U));
-Vals[1]+=W[14];
-Vals[1]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
-Vals[1]+=ch(Vals[6],Vals[7],Vals[0]);
-Vals[1]+=K[30];
-Vals[2]+=Ma(Vals[5],Vals[3],Vals[4]);
-Vals[5]+=Vals[1];
-Vals[1]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
-Vals[1]+=Ma(Vals[4],Vals[2],Vals[3]);
-
-W[15]=0x00000100U;
-W[15]+=(rotr(W[0],7)^rotr(W[0],18)^(W[0]>>3U));
-W[15]+=W[8];
-W[15]+=(rotr(W[13],17)^rotr(W[13],19)^(W[13]>>10U));
-Vals[0]+=W[15];
-Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
-Vals[0]+=ch(Vals[5],Vals[6],Vals[7]);
-Vals[0]+=K[31];
-Vals[4]+=Vals[0];
-Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
-
-W[0]+=(rotr(W[1],7)^rotr(W[1],18)^(W[1]>>3U));
-W[0]+=W[9];
-W[0]+=(rotr(W[14],17)^rotr(W[14],19)^(W[14]>>10U));
-Vals[7]+=W[0];
-Vals[7]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
-Vals[7]+=ch(Vals[4],Vals[5],Vals[6]);
-Vals[7]+=K[32];
-Vals[0]+=Ma(Vals[3],Vals[1],Vals[2]);
-Vals[3]+=Vals[7];
-Vals[7]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
-Vals[7]+=Ma(Vals[2],Vals[0],Vals[1]);
-
-W[1]+=(rotr(W[2],7)^rotr(W[2],18)^(W[2]>>3U));
-W[1]+=W[10];
-W[1]+=(rotr(W[15],17)^rotr(W[15],19)^(W[15]>>10U));
-Vals[6]+=W[1];
-Vals[6]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
-Vals[6]+=ch(Vals[3],Vals[4],Vals[5]);
-Vals[6]+=K[33];
-Vals[2]+=Vals[6];
-Vals[6]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
-
-W[2]+=(rotr(W[3],7)^rotr(W[3],18)^(W[3]>>3U));
-W[2]+=W[11];
-W[2]+=(rotr(W[0],17)^rotr(W[0],19)^(W[0]>>10U));
-Vals[5]+=W[2];
-Vals[5]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
-Vals[5]+=ch(Vals[2],Vals[3],Vals[4]);
-Vals[5]+=K[34];
-Vals[6]+=Ma(Vals[1],Vals[7],Vals[0]);
-Vals[1]+=Vals[5];
-Vals[5]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
-Vals[5]+=Ma(Vals[0],Vals[6],Vals[7]);
-
-W[3]+=(rotr(W[4],7)^rotr(W[4],18)^(W[4]>>3U));
-W[3]+=W[12];
-W[3]+=(rotr(W[1],17)^rotr(W[1],19)^(W[1]>>10U));
-Vals[4]+=W[3];
-Vals[4]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
-Vals[4]+=ch(Vals[1],Vals[2],Vals[3]);
-Vals[4]+=K[35];
-Vals[0]+=Vals[4];
-Vals[4]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
-
-W[4]+=(rotr(W[5],7)^rotr(W[5],18)^(W[5]>>3U));
-W[4]+=W[13];
-W[4]+=(rotr(W[2],17)^rotr(W[2],19)^(W[2]>>10U));
-Vals[3]+=W[4];
-Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
-Vals[3]+=ch(Vals[0],Vals[1],Vals[2]);
-Vals[3]+=K[36];
-Vals[4]+=Ma(Vals[7],Vals[5],Vals[6]);
-Vals[7]+=Vals[3];
-Vals[3]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
-Vals[3]+=Ma(Vals[6],Vals[4],Vals[5]);
-
-W[5]+=(rotr(W[6],7)^rotr(W[6],18)^(W[6]>>3U));
-W[5]+=W[14];
-W[5]+=(rotr(W[3],17)^rotr(W[3],19)^(W[3]>>10U));
-Vals[2]+=W[5];
-Vals[2]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
-Vals[2]+=ch(Vals[7],Vals[0],Vals[1]);
-Vals[2]+=K[37];
-Vals[6]+=Vals[2];
-Vals[2]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
-
-W[6]+=(rotr(W[7],7)^rotr(W[7],18)^(W[7]>>3U));
-W[6]+=W[15];
-W[6]+=(rotr(W[4],17)^rotr(W[4],19)^(W[4]>>10U));
-Vals[1]+=W[6];
-Vals[1]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
-Vals[1]+=ch(Vals[6],Vals[7],Vals[0]);
-Vals[1]+=K[38];
-Vals[2]+=Ma(Vals[5],Vals[3],Vals[4]);
-Vals[5]+=Vals[1];
-Vals[1]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
-Vals[1]+=Ma(Vals[4],Vals[2],Vals[3]);
-
-W[7]+=(rotr(W[8],7)^rotr(W[8],18)^(W[8]>>3U));
-W[7]+=W[0];
-W[7]+=(rotr(W[5],17)^rotr(W[5],19)^(W[5]>>10U));
-Vals[0]+=W[7];
-Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
-Vals[0]+=ch(Vals[5],Vals[6],Vals[7]);
-Vals[0]+=K[39];
-Vals[4]+=Vals[0];
-Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
-
-W[8]+=(rotr(W[9],7)^rotr(W[9],18)^(W[9]>>3U));
-W[8]+=W[1];
-W[8]+=(rotr(W[6],17)^rotr(W[6],19)^(W[6]>>10U));
-Vals[7]+=W[8];
-Vals[7]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
-Vals[7]+=ch(Vals[4],Vals[5],Vals[6]);
-Vals[7]+=K[40];
-Vals[0]+=Ma(Vals[3],Vals[1],Vals[2]);
-Vals[3]+=Vals[7];
-Vals[7]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
-Vals[7]+=Ma(Vals[2],Vals[0],Vals[1]);
-
-W[9]+=(rotr(W[10],7)^rotr(W[10],18)^(W[10]>>3U));
-W[9]+=W[2];
-W[9]+=(rotr(W[7],17)^rotr(W[7],19)^(W[7]>>10U));
-Vals[6]+=W[9];
-Vals[6]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
-Vals[6]+=ch(Vals[3],Vals[4],Vals[5]);
-Vals[6]+=K[41];
-Vals[2]+=Vals[6];
-Vals[6]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
-
-W[10]+=(rotr(W[11],7)^rotr(W[11],18)^(W[11]>>3U));
-W[10]+=W[3];
-W[10]+=(rotr(W[8],17)^rotr(W[8],19)^(W[8]>>10U));
-Vals[5]+=W[10];
-Vals[5]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
-Vals[5]+=ch(Vals[2],Vals[3],Vals[4]);
-Vals[5]+=K[42];
-Vals[6]+=Ma(Vals[1],Vals[7],Vals[0]);
-Vals[1]+=Vals[5];
-Vals[5]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
-Vals[5]+=Ma(Vals[0],Vals[6],Vals[7]);
-
-W[11]+=(rotr(W[12],7)^rotr(W[12],18)^(W[12]>>3U));
-W[11]+=W[4];
-W[11]+=(rotr(W[9],17)^rotr(W[9],19)^(W[9]>>10U));
-Vals[4]+=W[11];
-Vals[4]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
-Vals[4]+=ch(Vals[1],Vals[2],Vals[3]);
-Vals[4]+=K[43];
-Vals[0]+=Vals[4];
-Vals[4]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
-
-W[12]+=(rotr(W[13],7)^rotr(W[13],18)^(W[13]>>3U));
-W[12]+=W[5];
-W[12]+=(rotr(W[10],17)^rotr(W[10],19)^(W[10]>>10U));
-Vals[3]+=W[12];
-Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
-Vals[3]+=ch(Vals[0],Vals[1],Vals[2]);
-Vals[3]+=K[44];
-Vals[4]+=Ma(Vals[7],Vals[5],Vals[6]);
-Vals[7]+=Vals[3];
-Vals[3]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
-Vals[3]+=Ma(Vals[6],Vals[4],Vals[5]);
-
-W[13]+=(rotr(W[14],7)^rotr(W[14],18)^(W[14]>>3U));
-W[13]+=W[6];
-W[13]+=(rotr(W[11],17)^rotr(W[11],19)^(W[11]>>10U));
-Vals[2]+=W[13];
-Vals[2]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
-Vals[2]+=ch(Vals[7],Vals[0],Vals[1]);
-Vals[2]+=K[45];
-Vals[6]+=Vals[2];
-Vals[2]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
-
-W[14]+=(rotr(W[15],7)^rotr(W[15],18)^(W[15]>>3U));
-W[14]+=W[7];
-W[14]+=(rotr(W[12],17)^rotr(W[12],19)^(W[12]>>10U));
-Vals[1]+=W[14];
-Vals[1]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
-Vals[1]+=ch(Vals[6],Vals[7],Vals[0]);
-Vals[1]+=K[46];
-Vals[2]+=Ma(Vals[5],Vals[3],Vals[4]);
-Vals[5]+=Vals[1];
-Vals[1]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
-Vals[1]+=Ma(Vals[4],Vals[2],Vals[3]);
-
-W[15]+=(rotr(W[0],7)^rotr(W[0],18)^(W[0]>>3U));
-W[15]+=W[8];
-W[15]+=(rotr(W[13],17)^rotr(W[13],19)^(W[13]>>10U));
-Vals[0]+=W[15];
-Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
-Vals[0]+=ch(Vals[5],Vals[6],Vals[7]);
-Vals[0]+=K[47];
-Vals[4]+=Vals[0];
-Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
-
-W[0]+=(rotr(W[1],7)^rotr(W[1],18)^(W[1]>>3U));
-W[0]+=W[9];
-W[0]+=(rotr(W[14],17)^rotr(W[14],19)^(W[14]>>10U));
-Vals[7]+=W[0];
-Vals[7]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
-Vals[7]+=ch(Vals[4],Vals[5],Vals[6]);
-Vals[7]+=K[48];
-Vals[0]+=Ma(Vals[3],Vals[1],Vals[2]);
-Vals[3]+=Vals[7];
-Vals[7]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
-Vals[7]+=Ma(Vals[2],Vals[0],Vals[1]);
-
-W[1]+=(rotr(W[2],7)^rotr(W[2],18)^(W[2]>>3U));
-W[1]+=W[10];
-W[1]+=(rotr(W[15],17)^rotr(W[15],19)^(W[15]>>10U));
-Vals[6]+=W[1];
-Vals[6]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
-Vals[6]+=ch(Vals[3],Vals[4],Vals[5]);
-Vals[6]+=K[49];
-Vals[2]+=Vals[6];
-Vals[6]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
-
-W[2]+=(rotr(W[3],7)^rotr(W[3],18)^(W[3]>>3U));
-W[2]+=W[11];
-W[2]+=(rotr(W[0],17)^rotr(W[0],19)^(W[0]>>10U));
-Vals[5]+=W[2];
-Vals[5]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
-Vals[5]+=ch(Vals[2],Vals[3],Vals[4]);
-Vals[5]+=K[50];
-Vals[6]+=Ma(Vals[1],Vals[7],Vals[0]);
-Vals[1]+=Vals[5];
-Vals[5]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
-Vals[5]+=Ma(Vals[0],Vals[6],Vals[7]);
-
-W[3]+=(rotr(W[4],7)^rotr(W[4],18)^(W[4]>>3U));
-W[3]+=W[12];
-W[3]+=(rotr(W[1],17)^rotr(W[1],19)^(W[1]>>10U));
-Vals[4]+=W[3];
-Vals[4]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
-Vals[4]+=ch(Vals[1],Vals[2],Vals[3]);
-Vals[4]+=K[51];
-Vals[0]+=Vals[4];
-Vals[4]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
-
-W[4]+=(rotr(W[5],7)^rotr(W[5],18)^(W[5]>>3U));
-W[4]+=W[13];
-W[4]+=(rotr(W[2],17)^rotr(W[2],19)^(W[2]>>10U));
-Vals[3]+=W[4];
-Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
-Vals[3]+=ch(Vals[0],Vals[1],Vals[2]);
-Vals[3]+=K[52];
-Vals[4]+=Ma(Vals[7],Vals[5],Vals[6]);
-Vals[7]+=Vals[3];
-Vals[3]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
-Vals[3]+=Ma(Vals[6],Vals[4],Vals[5]);
-
-W[5]+=(rotr(W[6],7)^rotr(W[6],18)^(W[6]>>3U));
-W[5]+=W[14];
-W[5]+=(rotr(W[3],17)^rotr(W[3],19)^(W[3]>>10U));
-Vals[2]+=W[5];
-Vals[2]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
-Vals[2]+=ch(Vals[7],Vals[0],Vals[1]);
-Vals[2]+=K[53];
-Vals[6]+=Vals[2];
-Vals[2]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
-
-W[6]+=(rotr(W[7],7)^rotr(W[7],18)^(W[7]>>3U));
-W[6]+=W[15];
-W[6]+=(rotr(W[4],17)^rotr(W[4],19)^(W[4]>>10U));
-Vals[1]+=W[6];
-Vals[1]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
-Vals[1]+=ch(Vals[6],Vals[7],Vals[0]);
-Vals[1]+=K[54];
-Vals[2]+=Ma(Vals[5],Vals[3],Vals[4]);
-Vals[5]+=Vals[1];
-Vals[1]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
-Vals[1]+=Ma(Vals[4],Vals[2],Vals[3]);
-
-W[7]+=(rotr(W[8],7)^rotr(W[8],18)^(W[8]>>3U));
-W[7]+=W[0];
-W[7]+=(rotr(W[5],17)^rotr(W[5],19)^(W[5]>>10U));
-Vals[0]+=W[7];
-Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
-Vals[0]+=ch(Vals[5],Vals[6],Vals[7]);
-Vals[0]+=K[55];
-Vals[4]+=Vals[0];
-Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
-
-W[8]+=(rotr(W[9],7)^rotr(W[9],18)^(W[9]>>3U));
-W[8]+=W[1];
-W[8]+=(rotr(W[6],17)^rotr(W[6],19)^(W[6]>>10U));
-Vals[7]+=W[8];
-Vals[7]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
-Vals[7]+=ch(Vals[4],Vals[5],Vals[6]);
-Vals[7]+=K[56];
-Vals[0]+=Ma(Vals[3],Vals[1],Vals[2]);
-Vals[3]+=Vals[7];
-Vals[7]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
-Vals[7]+=Ma(Vals[2],Vals[0],Vals[1]);
-
-W[9]+=(rotr(W[10],7)^rotr(W[10],18)^(W[10]>>3U));
-W[9]+=W[2];
-W[9]+=(rotr(W[7],17)^rotr(W[7],19)^(W[7]>>10U));
-Vals[6]+=W[9];
-Vals[6]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
-Vals[6]+=ch(Vals[3],Vals[4],Vals[5]);
-Vals[6]+=K[57];
-
-W[10]+=(rotr(W[11],7)^rotr(W[11],18)^(W[11]>>3U));
-W[10]+=W[3];
-W[10]+=(rotr(W[8],17)^rotr(W[8],19)^(W[8]>>10U));
-Vals[5]+=W[10];
-Vals[2]+=Vals[6];
-Vals[5]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
-Vals[5]+=ch(Vals[2],Vals[3],Vals[4]);
-Vals[5]+=K[58];
-
-W[11]+=(rotr(W[12],7)^rotr(W[12],18)^(W[12]>>3U));
-W[11]+=W[4];
-W[11]+=(rotr(W[9],17)^rotr(W[9],19)^(W[9]>>10U));
-Vals[4]+=W[11];
-Vals[1]+=Vals[5];
-Vals[4]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
-Vals[4]+=ch(Vals[1],Vals[2],Vals[3]);
-Vals[4]+=K[59];
-
-W[12]+=(rotr(W[13],7)^rotr(W[13],18)^(W[13]>>3U));
-W[12]+=W[5];
-W[12]+=(rotr(W[10],17)^rotr(W[10],19)^(W[10]>>10U));
-Vals[7]+=W[12];
-Vals[0]+=Vals[4];
-Vals[7]+=Vals[3];
-Vals[7]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
-Vals[7]+=ch(Vals[0],Vals[1],Vals[2]);
-//Vals[7]+=K[60]; diffed from 0xA41F32E7
-
-#define FOUND (0x80)
-#define NFLAG (0x7F)
-
-#if defined(VECTORS4)
-	Vals[7] ^= 0x136032edU;
-
-	bool result = Vals[7].x & Vals[7].y & Vals[7].z & Vals[7].w;
-
-	if (!result) {
-		if (!Vals[7].x)
-			output[FOUND] = output[NFLAG & nonce.x] = nonce.x;
-		if (!Vals[7].y)
-			output[FOUND] = output[NFLAG & nonce.y] = nonce.y;
-		if (!Vals[7].z)
-			output[FOUND] = output[NFLAG & nonce.z] = nonce.z;
-		if (!Vals[7].w)
-			output[FOUND] = output[NFLAG & nonce.w] = nonce.w;
-	}
-#elif defined VECTORS2
-	Vals[7] ^= 0x136032edU;
-
-	bool result = Vals[7].x & Vals[7].y;
-
-	if (!result) {
-		if (!Vals[7].x)
-			output[FOUND] = output[FOUND] = output[NFLAG & nonce.x] = nonce.x;
-		if (!Vals[7].y)
-			output[FOUND] = output[FOUND] = output[NFLAG & nonce.y] = nonce.y;
-	}
-#else
-	if (Vals[7] == 0x136032edU)
-		output[FOUND] = output[NFLAG & nonce] =  nonce;
-#endif
-}

+ 1353 - 0
poclbm120327.cl

@@ -0,0 +1,1353 @@
+// -ck modified kernel taken from Phoenix taken from poclbm, with aspects of
+// phatk and others.
+// Modified version copyright 2011-2012 Con Kolivas
+
+// This file is taken and modified from the public-domain poclbm project, and
+// we have therefore decided to keep it public-domain in Phoenix.
+
+#ifdef VECTORS4
+	typedef uint4 u;
+#elif defined VECTORS2
+	typedef uint2 u;
+#else
+	typedef uint u;
+#endif
+
+__constant uint K[64] = { 
+    0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
+    0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
+    0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
+    0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
+    0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
+    0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
+    0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
+    0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
+};
+
+
+// This part is not from the stock poclbm kernel. It's part of an optimization
+// added in the Phoenix Miner.
+
+// Some AMD devices have a BFI_INT opcode, which behaves exactly like the
+// SHA-256 ch function, but provides it in exactly one instruction. If
+// detected, use it for ch. Otherwise, construct ch out of simpler logical
+// primitives.
+
+#ifdef BITALIGN
+	#pragma OPENCL EXTENSION cl_amd_media_ops : enable
+	#define rotr(x, y) amd_bitalign((u)x, (u)x, (u)y)
+#else
+	#define rotr(x, y) rotate((u)x, (u)(32 - y))
+#endif
+#ifdef BFI_INT
+	// Well, slight problem... It turns out BFI_INT isn't actually exposed to
+	// OpenCL (or CAL IL for that matter) in any way. However, there is 
+	// a similar instruction, BYTE_ALIGN_INT, which is exposed to OpenCL via
+	// amd_bytealign, takes the same inputs, and provides the same output. 
+	// We can use that as a placeholder for BFI_INT and have the application 
+	// patch it after compilation.
+	
+	// This is the BFI_INT function
+	#define ch(x, y, z) amd_bytealign(x, y, z)
+	
+	// Ma can also be implemented in terms of BFI_INT...
+	#define Ma(x, y, z) amd_bytealign( (z^x), (y), (x) )
+
+	// AMD's KernelAnalyzer throws errors compiling the kernel if we use
+	// amd_bytealign on constants with vectors enabled, so we use this to avoid
+	// problems. (this is used 4 times, and likely optimized out by the compiler.)
+	#define Ma2(x, y, z) bitselect((u)x, (u)y, (u)z ^ (u)x)
+#else // BFI_INT
+	//GCN actually fails if manually patched with BFI_INT
+
+	#define ch(x, y, z) bitselect((u)z, (u)y, (u)x)
+	#define Ma(x, y, z) bitselect((u)x, (u)y, (u)z ^ (u)x)
+	#define Ma2(x, y, z) Ma(x, y, z)
+#endif
+
+
+__kernel
+__attribute__((vec_type_hint(u)))
+__attribute__((reqd_work_group_size(WORKSIZE, 1, 1)))
+void search(const uint state0, const uint state1, const uint state2, const uint state3,
+	const uint state4, const uint state5, const uint state6, const uint state7,
+	const uint b1, const uint c1,
+	const uint f1, const uint g1, const uint h1,
+#ifndef GOFFSET
+	const u base,
+#endif
+	const uint fw0, const uint fw1, const uint fw2, const uint fw3, const uint fw15, const uint fw01r,
+	const uint D1A, const uint C1addK5, const uint B1addK6,
+	const uint W16addK16, const uint W17addK17,
+	const uint PreVal4addT1, const uint Preval0,
+	__global uint * output)
+{
+	u Vals[24];
+	u *W = &Vals[8];
+
+#ifdef GOFFSET
+	const u nonce = (uint)(get_global_id(0));
+#else
+	const u nonce = base + (uint)(get_global_id(0));
+#endif
+
+Vals[5]=Preval0;
+Vals[5]+=nonce;
+
+Vals[0]=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
+Vals[0]+=ch(Vals[5],b1,c1);
+Vals[0]+=D1A;
+
+Vals[2]=Vals[0];
+Vals[2]+=h1;
+
+Vals[1]=PreVal4addT1;
+Vals[1]+=nonce;
+Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
+
+Vals[6]=C1addK5;
+Vals[6]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
+Vals[6]+=ch(Vals[2],Vals[5],b1);
+
+Vals[3]=Vals[6];
+Vals[3]+=g1;
+Vals[0]+=Ma2(g1,Vals[1],f1);
+Vals[6]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
+Vals[6]+=Ma2(f1,Vals[0],Vals[1]);
+
+Vals[7]=B1addK6;
+Vals[7]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
+Vals[7]+=ch(Vals[3],Vals[2],Vals[5]);
+
+Vals[4]=Vals[7];
+Vals[4]+=f1;
+
+Vals[7]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
+Vals[7]+=Ma(Vals[1],Vals[6],Vals[0]);
+
+Vals[5]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
+Vals[5]+=ch(Vals[4],Vals[3],Vals[2]);
+Vals[5]+=K[7];
+Vals[1]+=Vals[5];
+Vals[5]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
+Vals[5]+=Ma(Vals[0],Vals[7],Vals[6]);
+
+Vals[2]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
+Vals[2]+=ch(Vals[1],Vals[4],Vals[3]);
+Vals[2]+=K[8];
+Vals[0]+=Vals[2];
+Vals[2]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
+Vals[2]+=Ma(Vals[6],Vals[5],Vals[7]);
+
+Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
+Vals[3]+=ch(Vals[0],Vals[1],Vals[4]);
+Vals[3]+=K[9];
+Vals[6]+=Vals[3];
+Vals[3]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
+Vals[3]+=Ma(Vals[7],Vals[2],Vals[5]);
+
+Vals[4]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
+Vals[4]+=ch(Vals[6],Vals[0],Vals[1]);
+Vals[4]+=K[10];
+Vals[7]+=Vals[4];
+Vals[4]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
+Vals[4]+=Ma(Vals[5],Vals[3],Vals[2]);
+
+Vals[1]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
+Vals[1]+=ch(Vals[7],Vals[6],Vals[0]);
+Vals[1]+=K[11];
+Vals[5]+=Vals[1];
+Vals[1]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
+Vals[1]+=Ma(Vals[2],Vals[4],Vals[3]);
+
+Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
+Vals[0]+=ch(Vals[5],Vals[7],Vals[6]);
+Vals[0]+=K[12];
+Vals[2]+=Vals[0];
+Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
+Vals[0]+=Ma(Vals[3],Vals[1],Vals[4]);
+
+Vals[6]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
+Vals[6]+=ch(Vals[2],Vals[5],Vals[7]);
+Vals[6]+=K[13];
+Vals[3]+=Vals[6];
+Vals[6]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
+Vals[6]+=Ma(Vals[4],Vals[0],Vals[1]);
+
+Vals[7]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
+Vals[7]+=ch(Vals[3],Vals[2],Vals[5]);
+Vals[7]+=K[14];
+Vals[4]+=Vals[7];
+Vals[7]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
+Vals[7]+=Ma(Vals[1],Vals[6],Vals[0]);
+
+Vals[5]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
+Vals[5]+=ch(Vals[4],Vals[3],Vals[2]);
+Vals[5]+=0xC19BF3F4U;
+Vals[1]+=Vals[5];
+Vals[5]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
+Vals[5]+=Ma(Vals[0],Vals[7],Vals[6]);
+
+Vals[2]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
+Vals[2]+=ch(Vals[1],Vals[4],Vals[3]);
+Vals[2]+=W16addK16;
+Vals[0]+=Vals[2];
+Vals[2]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
+Vals[2]+=Ma(Vals[6],Vals[5],Vals[7]);
+
+Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
+Vals[3]+=ch(Vals[0],Vals[1],Vals[4]);
+Vals[3]+=W17addK17;
+Vals[6]+=Vals[3];
+Vals[3]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
+Vals[3]+=Ma(Vals[7],Vals[2],Vals[5]);
+
+W[2]=(rotr(nonce,7)^rotr(nonce,18)^(nonce>>3U));
+W[2]+=fw2;
+Vals[4]+=W[2];
+Vals[4]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
+Vals[4]+=ch(Vals[6],Vals[0],Vals[1]);
+Vals[4]+=K[18];
+Vals[7]+=Vals[4];
+Vals[4]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
+Vals[4]+=Ma(Vals[5],Vals[3],Vals[2]);
+
+W[3]=nonce;
+W[3]+=fw3;
+Vals[1]+=W[3];
+Vals[1]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
+Vals[1]+=ch(Vals[7],Vals[6],Vals[0]);
+Vals[1]+=K[19];
+Vals[5]+=Vals[1];
+Vals[1]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
+Vals[1]+=Ma(Vals[2],Vals[4],Vals[3]);
+
+W[4]=(rotr(W[2],17)^rotr(W[2],19)^(W[2]>>10U));
+W[4]+=0x80000000U;
+Vals[0]+=W[4];
+Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
+Vals[0]+=ch(Vals[5],Vals[7],Vals[6]);
+Vals[0]+=K[20];
+Vals[2]+=Vals[0];
+Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
+Vals[0]+=Ma(Vals[3],Vals[1],Vals[4]);
+
+W[5]=(rotr(W[3],17)^rotr(W[3],19)^(W[3]>>10U));
+Vals[6]+=W[5];
+Vals[6]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
+Vals[6]+=ch(Vals[2],Vals[5],Vals[7]);
+Vals[6]+=K[21];
+Vals[3]+=Vals[6];
+Vals[6]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
+Vals[6]+=Ma(Vals[4],Vals[0],Vals[1]);
+
+W[6]=(rotr(W[4],17)^rotr(W[4],19)^(W[4]>>10U));
+W[6]+=0x00000280U;
+Vals[7]+=W[6];
+Vals[7]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
+Vals[7]+=ch(Vals[3],Vals[2],Vals[5]);
+Vals[7]+=K[22];
+Vals[4]+=Vals[7];
+Vals[7]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
+Vals[7]+=Ma(Vals[1],Vals[6],Vals[0]);
+
+W[7]=(rotr(W[5],17)^rotr(W[5],19)^(W[5]>>10U));
+W[7]+=fw0;
+Vals[5]+=W[7];
+Vals[5]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
+Vals[5]+=ch(Vals[4],Vals[3],Vals[2]);
+Vals[5]+=K[23];
+Vals[1]+=Vals[5];
+Vals[5]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
+Vals[5]+=Ma(Vals[0],Vals[7],Vals[6]);
+
+W[8]=(rotr(W[6],17)^rotr(W[6],19)^(W[6]>>10U));
+W[8]+=fw1;
+Vals[2]+=W[8];
+Vals[2]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
+Vals[2]+=ch(Vals[1],Vals[4],Vals[3]);
+Vals[2]+=K[24];
+Vals[0]+=Vals[2];
+Vals[2]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
+Vals[2]+=Ma(Vals[6],Vals[5],Vals[7]);
+
+W[9]=W[2];
+W[9]+=(rotr(W[7],17)^rotr(W[7],19)^(W[7]>>10U));
+Vals[3]+=W[9];
+Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
+Vals[3]+=ch(Vals[0],Vals[1],Vals[4]);
+Vals[3]+=K[25];
+Vals[6]+=Vals[3];
+Vals[3]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
+Vals[3]+=Ma(Vals[7],Vals[2],Vals[5]);
+
+W[10]=W[3];
+W[10]+=(rotr(W[8],17)^rotr(W[8],19)^(W[8]>>10U));
+Vals[4]+=W[10];
+Vals[4]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
+Vals[4]+=ch(Vals[6],Vals[0],Vals[1]);
+Vals[4]+=K[26];
+Vals[7]+=Vals[4];
+Vals[4]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
+Vals[4]+=Ma(Vals[5],Vals[3],Vals[2]);
+
+W[11]=W[4];
+W[11]+=(rotr(W[9],17)^rotr(W[9],19)^(W[9]>>10U));
+Vals[1]+=W[11];
+Vals[1]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
+Vals[1]+=ch(Vals[7],Vals[6],Vals[0]);
+Vals[1]+=K[27];
+Vals[5]+=Vals[1];
+Vals[1]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
+Vals[1]+=Ma(Vals[2],Vals[4],Vals[3]);
+
+W[12]=W[5];
+W[12]+=(rotr(W[10],17)^rotr(W[10],19)^(W[10]>>10U));
+Vals[0]+=W[12];
+Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
+Vals[0]+=ch(Vals[5],Vals[7],Vals[6]);
+Vals[0]+=K[28];
+Vals[2]+=Vals[0];
+Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
+Vals[0]+=Ma(Vals[3],Vals[1],Vals[4]);
+
+W[13]=W[6];
+W[13]+=(rotr(W[11],17)^rotr(W[11],19)^(W[11]>>10U));
+Vals[6]+=W[13];
+Vals[6]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
+Vals[6]+=ch(Vals[2],Vals[5],Vals[7]);
+Vals[6]+=K[29];
+Vals[3]+=Vals[6];
+Vals[6]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
+Vals[6]+=Ma(Vals[4],Vals[0],Vals[1]);
+
+W[14]=0x00a00055U;
+W[14]+=W[7];
+W[14]+=(rotr(W[12],17)^rotr(W[12],19)^(W[12]>>10U));
+Vals[7]+=W[14];
+Vals[7]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
+Vals[7]+=ch(Vals[3],Vals[2],Vals[5]);
+Vals[7]+=K[30];
+Vals[4]+=Vals[7];
+Vals[7]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
+Vals[7]+=Ma(Vals[1],Vals[6],Vals[0]);
+
+W[15]=fw15;
+W[15]+=W[8];
+W[15]+=(rotr(W[13],17)^rotr(W[13],19)^(W[13]>>10U));
+Vals[5]+=W[15];
+Vals[5]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
+Vals[5]+=ch(Vals[4],Vals[3],Vals[2]);
+Vals[5]+=K[31];
+Vals[1]+=Vals[5];
+Vals[5]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
+Vals[5]+=Ma(Vals[0],Vals[7],Vals[6]);
+
+W[0]=fw01r;
+W[0]+=W[9];
+W[0]+=(rotr(W[14],17)^rotr(W[14],19)^(W[14]>>10U));
+Vals[2]+=W[0];
+Vals[2]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
+Vals[2]+=ch(Vals[1],Vals[4],Vals[3]);
+Vals[2]+=K[32];
+Vals[0]+=Vals[2];
+Vals[2]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
+Vals[2]+=Ma(Vals[6],Vals[5],Vals[7]);
+
+W[1]=fw1;
+W[1]+=(rotr(W[2],7)^rotr(W[2],18)^(W[2]>>3U));
+W[1]+=W[10];
+W[1]+=(rotr(W[15],17)^rotr(W[15],19)^(W[15]>>10U));
+Vals[3]+=W[1];
+Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
+Vals[3]+=ch(Vals[0],Vals[1],Vals[4]);
+Vals[3]+=K[33];
+Vals[6]+=Vals[3];
+Vals[3]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
+Vals[3]+=Ma(Vals[7],Vals[2],Vals[5]);
+
+W[2]+=(rotr(W[3],7)^rotr(W[3],18)^(W[3]>>3U));
+W[2]+=W[11];
+W[2]+=(rotr(W[0],17)^rotr(W[0],19)^(W[0]>>10U));
+Vals[4]+=W[2];
+Vals[4]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
+Vals[4]+=ch(Vals[6],Vals[0],Vals[1]);
+Vals[4]+=K[34];
+Vals[7]+=Vals[4];
+Vals[4]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
+Vals[4]+=Ma(Vals[5],Vals[3],Vals[2]);
+
+W[3]+=(rotr(W[4],7)^rotr(W[4],18)^(W[4]>>3U));
+W[3]+=W[12];
+W[3]+=(rotr(W[1],17)^rotr(W[1],19)^(W[1]>>10U));
+Vals[1]+=W[3];
+Vals[1]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
+Vals[1]+=ch(Vals[7],Vals[6],Vals[0]);
+Vals[1]+=K[35];
+Vals[5]+=Vals[1];
+Vals[1]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
+Vals[1]+=Ma(Vals[2],Vals[4],Vals[3]);
+
+W[4]+=(rotr(W[5],7)^rotr(W[5],18)^(W[5]>>3U));
+W[4]+=W[13];
+W[4]+=(rotr(W[2],17)^rotr(W[2],19)^(W[2]>>10U));
+Vals[0]+=W[4];
+Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
+Vals[0]+=ch(Vals[5],Vals[7],Vals[6]);
+Vals[0]+=K[36];
+Vals[2]+=Vals[0];
+Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
+Vals[0]+=Ma(Vals[3],Vals[1],Vals[4]);
+
+W[5]+=(rotr(W[6],7)^rotr(W[6],18)^(W[6]>>3U));
+W[5]+=W[14];
+W[5]+=(rotr(W[3],17)^rotr(W[3],19)^(W[3]>>10U));
+Vals[6]+=W[5];
+Vals[6]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
+Vals[6]+=ch(Vals[2],Vals[5],Vals[7]);
+Vals[6]+=K[37];
+Vals[3]+=Vals[6];
+Vals[6]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
+Vals[6]+=Ma(Vals[4],Vals[0],Vals[1]);
+
+W[6]+=(rotr(W[7],7)^rotr(W[7],18)^(W[7]>>3U));
+W[6]+=W[15];
+W[6]+=(rotr(W[4],17)^rotr(W[4],19)^(W[4]>>10U));
+Vals[7]+=W[6];
+Vals[7]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
+Vals[7]+=ch(Vals[3],Vals[2],Vals[5]);
+Vals[7]+=K[38];
+Vals[4]+=Vals[7];
+Vals[7]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
+Vals[7]+=Ma(Vals[1],Vals[6],Vals[0]);
+
+W[7]+=(rotr(W[8],7)^rotr(W[8],18)^(W[8]>>3U));
+W[7]+=W[0];
+W[7]+=(rotr(W[5],17)^rotr(W[5],19)^(W[5]>>10U));
+Vals[5]+=W[7];
+Vals[5]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
+Vals[5]+=ch(Vals[4],Vals[3],Vals[2]);
+Vals[5]+=K[39];
+Vals[1]+=Vals[5];
+Vals[5]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
+Vals[5]+=Ma(Vals[0],Vals[7],Vals[6]);
+
+W[8]+=(rotr(W[9],7)^rotr(W[9],18)^(W[9]>>3U));
+W[8]+=W[1];
+W[8]+=(rotr(W[6],17)^rotr(W[6],19)^(W[6]>>10U));
+Vals[2]+=W[8];
+Vals[2]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
+Vals[2]+=ch(Vals[1],Vals[4],Vals[3]);
+Vals[2]+=K[40];
+Vals[0]+=Vals[2];
+Vals[2]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
+Vals[2]+=Ma(Vals[6],Vals[5],Vals[7]);
+
+W[9]+=(rotr(W[10],7)^rotr(W[10],18)^(W[10]>>3U));
+W[9]+=W[2];
+W[9]+=(rotr(W[7],17)^rotr(W[7],19)^(W[7]>>10U));
+Vals[3]+=W[9];
+Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
+Vals[3]+=ch(Vals[0],Vals[1],Vals[4]);
+Vals[3]+=K[41];
+Vals[6]+=Vals[3];
+Vals[3]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
+Vals[3]+=Ma(Vals[7],Vals[2],Vals[5]);
+
+W[10]+=(rotr(W[11],7)^rotr(W[11],18)^(W[11]>>3U));
+W[10]+=W[3];
+W[10]+=(rotr(W[8],17)^rotr(W[8],19)^(W[8]>>10U));
+Vals[4]+=W[10];
+Vals[4]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
+Vals[4]+=ch(Vals[6],Vals[0],Vals[1]);
+Vals[4]+=K[42];
+Vals[7]+=Vals[4];
+Vals[4]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
+Vals[4]+=Ma(Vals[5],Vals[3],Vals[2]);
+
+W[11]+=(rotr(W[12],7)^rotr(W[12],18)^(W[12]>>3U));
+W[11]+=W[4];
+W[11]+=(rotr(W[9],17)^rotr(W[9],19)^(W[9]>>10U));
+Vals[1]+=W[11];
+Vals[1]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
+Vals[1]+=ch(Vals[7],Vals[6],Vals[0]);
+Vals[1]+=K[43];
+Vals[5]+=Vals[1];
+Vals[1]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
+Vals[1]+=Ma(Vals[2],Vals[4],Vals[3]);
+
+W[12]+=(rotr(W[13],7)^rotr(W[13],18)^(W[13]>>3U));
+W[12]+=W[5];
+W[12]+=(rotr(W[10],17)^rotr(W[10],19)^(W[10]>>10U));
+Vals[0]+=W[12];
+Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
+Vals[0]+=ch(Vals[5],Vals[7],Vals[6]);
+Vals[0]+=K[44];
+Vals[2]+=Vals[0];
+Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
+Vals[0]+=Ma(Vals[3],Vals[1],Vals[4]);
+
+W[13]+=(rotr(W[14],7)^rotr(W[14],18)^(W[14]>>3U));
+W[13]+=W[6];
+W[13]+=(rotr(W[11],17)^rotr(W[11],19)^(W[11]>>10U));
+Vals[6]+=W[13];
+Vals[6]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
+Vals[6]+=ch(Vals[2],Vals[5],Vals[7]);
+Vals[6]+=K[45];
+Vals[3]+=Vals[6];
+Vals[6]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
+Vals[6]+=Ma(Vals[4],Vals[0],Vals[1]);
+
+W[14]+=(rotr(W[15],7)^rotr(W[15],18)^(W[15]>>3U));
+W[14]+=W[7];
+W[14]+=(rotr(W[12],17)^rotr(W[12],19)^(W[12]>>10U));
+Vals[7]+=W[14];
+Vals[7]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
+Vals[7]+=ch(Vals[3],Vals[2],Vals[5]);
+Vals[7]+=K[46];
+Vals[4]+=Vals[7];
+Vals[7]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
+Vals[7]+=Ma(Vals[1],Vals[6],Vals[0]);
+
+W[15]+=(rotr(W[0],7)^rotr(W[0],18)^(W[0]>>3U));
+W[15]+=W[8];
+W[15]+=(rotr(W[13],17)^rotr(W[13],19)^(W[13]>>10U));
+Vals[5]+=W[15];
+Vals[5]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
+Vals[5]+=ch(Vals[4],Vals[3],Vals[2]);
+Vals[5]+=K[47];
+Vals[1]+=Vals[5];
+Vals[5]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
+Vals[5]+=Ma(Vals[0],Vals[7],Vals[6]);
+
+W[0]+=(rotr(W[1],7)^rotr(W[1],18)^(W[1]>>3U));
+W[0]+=W[9];
+W[0]+=(rotr(W[14],17)^rotr(W[14],19)^(W[14]>>10U));
+Vals[2]+=W[0];
+Vals[2]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
+Vals[2]+=ch(Vals[1],Vals[4],Vals[3]);
+Vals[2]+=K[48];
+Vals[0]+=Vals[2];
+Vals[2]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
+Vals[2]+=Ma(Vals[6],Vals[5],Vals[7]);
+
+W[1]+=(rotr(W[2],7)^rotr(W[2],18)^(W[2]>>3U));
+W[1]+=W[10];
+W[1]+=(rotr(W[15],17)^rotr(W[15],19)^(W[15]>>10U));
+Vals[3]+=W[1];
+Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
+Vals[3]+=ch(Vals[0],Vals[1],Vals[4]);
+Vals[3]+=K[49];
+Vals[6]+=Vals[3];
+Vals[3]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
+Vals[3]+=Ma(Vals[7],Vals[2],Vals[5]);
+
+W[2]+=(rotr(W[3],7)^rotr(W[3],18)^(W[3]>>3U));
+W[2]+=W[11];
+W[2]+=(rotr(W[0],17)^rotr(W[0],19)^(W[0]>>10U));
+Vals[4]+=W[2];
+Vals[4]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
+Vals[4]+=ch(Vals[6],Vals[0],Vals[1]);
+Vals[4]+=K[50];
+Vals[7]+=Vals[4];
+Vals[4]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
+Vals[4]+=Ma(Vals[5],Vals[3],Vals[2]);
+
+W[3]+=(rotr(W[4],7)^rotr(W[4],18)^(W[4]>>3U));
+W[3]+=W[12];
+W[3]+=(rotr(W[1],17)^rotr(W[1],19)^(W[1]>>10U));
+Vals[1]+=W[3];
+Vals[1]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
+Vals[1]+=ch(Vals[7],Vals[6],Vals[0]);
+Vals[1]+=K[51];
+Vals[5]+=Vals[1];
+Vals[1]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
+Vals[1]+=Ma(Vals[2],Vals[4],Vals[3]);
+
+W[4]+=(rotr(W[5],7)^rotr(W[5],18)^(W[5]>>3U));
+W[4]+=W[13];
+W[4]+=(rotr(W[2],17)^rotr(W[2],19)^(W[2]>>10U));
+Vals[0]+=W[4];
+Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
+Vals[0]+=ch(Vals[5],Vals[7],Vals[6]);
+Vals[0]+=K[52];
+Vals[2]+=Vals[0];
+Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
+Vals[0]+=Ma(Vals[3],Vals[1],Vals[4]);
+
+W[5]+=(rotr(W[6],7)^rotr(W[6],18)^(W[6]>>3U));
+W[5]+=W[14];
+W[5]+=(rotr(W[3],17)^rotr(W[3],19)^(W[3]>>10U));
+Vals[6]+=W[5];
+Vals[6]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
+Vals[6]+=ch(Vals[2],Vals[5],Vals[7]);
+Vals[6]+=K[53];
+Vals[3]+=Vals[6];
+Vals[6]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
+Vals[6]+=Ma(Vals[4],Vals[0],Vals[1]);
+
+W[6]+=(rotr(W[7],7)^rotr(W[7],18)^(W[7]>>3U));
+W[6]+=W[15];
+W[6]+=(rotr(W[4],17)^rotr(W[4],19)^(W[4]>>10U));
+Vals[7]+=W[6];
+Vals[7]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
+Vals[7]+=ch(Vals[3],Vals[2],Vals[5]);
+Vals[7]+=K[54];
+Vals[4]+=Vals[7];
+Vals[7]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
+Vals[7]+=Ma(Vals[1],Vals[6],Vals[0]);
+
+W[7]+=(rotr(W[8],7)^rotr(W[8],18)^(W[8]>>3U));
+W[7]+=W[0];
+W[7]+=(rotr(W[5],17)^rotr(W[5],19)^(W[5]>>10U));
+Vals[5]+=W[7];
+Vals[5]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
+Vals[5]+=ch(Vals[4],Vals[3],Vals[2]);
+Vals[5]+=K[55];
+Vals[1]+=Vals[5];
+Vals[5]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
+Vals[5]+=Ma(Vals[0],Vals[7],Vals[6]);
+
+W[8]+=(rotr(W[9],7)^rotr(W[9],18)^(W[9]>>3U));
+W[8]+=W[1];
+W[8]+=(rotr(W[6],17)^rotr(W[6],19)^(W[6]>>10U));
+Vals[2]+=W[8];
+Vals[2]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
+Vals[2]+=ch(Vals[1],Vals[4],Vals[3]);
+Vals[2]+=K[56];
+Vals[0]+=Vals[2];
+Vals[2]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
+Vals[2]+=Ma(Vals[6],Vals[5],Vals[7]);
+
+W[9]+=(rotr(W[10],7)^rotr(W[10],18)^(W[10]>>3U));
+W[9]+=W[2];
+W[9]+=(rotr(W[7],17)^rotr(W[7],19)^(W[7]>>10U));
+Vals[3]+=W[9];
+Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
+Vals[3]+=ch(Vals[0],Vals[1],Vals[4]);
+Vals[3]+=K[57];
+Vals[6]+=Vals[3];
+Vals[3]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
+Vals[3]+=Ma(Vals[7],Vals[2],Vals[5]);
+
+W[10]+=(rotr(W[11],7)^rotr(W[11],18)^(W[11]>>3U));
+W[10]+=W[3];
+W[10]+=(rotr(W[8],17)^rotr(W[8],19)^(W[8]>>10U));
+Vals[4]+=W[10];
+Vals[4]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
+Vals[4]+=ch(Vals[6],Vals[0],Vals[1]);
+Vals[4]+=K[58];
+Vals[7]+=Vals[4];
+Vals[4]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
+Vals[4]+=Ma(Vals[5],Vals[3],Vals[2]);
+
+W[11]+=(rotr(W[12],7)^rotr(W[12],18)^(W[12]>>3U));
+W[11]+=W[4];
+W[11]+=(rotr(W[9],17)^rotr(W[9],19)^(W[9]>>10U));
+Vals[1]+=W[11];
+Vals[1]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
+Vals[1]+=ch(Vals[7],Vals[6],Vals[0]);
+Vals[1]+=K[59];
+Vals[5]+=Vals[1];
+Vals[1]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
+Vals[1]+=Ma(Vals[2],Vals[4],Vals[3]);
+
+W[12]+=(rotr(W[13],7)^rotr(W[13],18)^(W[13]>>3U));
+W[12]+=W[5];
+W[12]+=(rotr(W[10],17)^rotr(W[10],19)^(W[10]>>10U));
+Vals[0]+=W[12];
+Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
+Vals[0]+=ch(Vals[5],Vals[7],Vals[6]);
+Vals[0]+=K[60];
+Vals[2]+=Vals[0];
+Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
+Vals[0]+=Ma(Vals[3],Vals[1],Vals[4]);
+
+W[13]+=(rotr(W[14],7)^rotr(W[14],18)^(W[14]>>3U));
+W[13]+=W[6];
+W[13]+=(rotr(W[11],17)^rotr(W[11],19)^(W[11]>>10U));
+Vals[6]+=W[13];
+Vals[6]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
+Vals[6]+=ch(Vals[2],Vals[5],Vals[7]);
+Vals[6]+=K[61];
+Vals[3]+=Vals[6];
+Vals[6]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
+Vals[6]+=Ma(Vals[4],Vals[0],Vals[1]);
+
+Vals[7]+=W[14];
+Vals[7]+=(rotr(W[15],7)^rotr(W[15],18)^(W[15]>>3U));
+Vals[7]+=W[7];
+Vals[7]+=(rotr(W[12],17)^rotr(W[12],19)^(W[12]>>10U));
+Vals[7]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
+Vals[7]+=ch(Vals[3],Vals[2],Vals[5]);
+Vals[7]+=K[62];
+Vals[4]+=Vals[7];
+Vals[7]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
+Vals[7]+=Ma(Vals[1],Vals[6],Vals[0]);
+
+Vals[5]+=W[15];
+Vals[5]+=(rotr(W[0],7)^rotr(W[0],18)^(W[0]>>3U));
+Vals[5]+=W[8];
+Vals[5]+=(rotr(W[13],17)^rotr(W[13],19)^(W[13]>>10U));
+Vals[5]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
+Vals[5]+=ch(Vals[4],Vals[3],Vals[2]);
+Vals[5]+=K[63];
+Vals[1]+=Vals[5];
+Vals[5]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
+Vals[5]+=Ma(Vals[0],Vals[7],Vals[6]);
+
+Vals[5]+=state0;
+
+W[7]=state7;
+W[7]+=Vals[2];
+
+Vals[2]=0xF377ED68U;
+Vals[2]+=Vals[5];
+
+W[3]=state3;
+W[3]+=Vals[0];
+
+Vals[0]=0xa54ff53aU;
+Vals[0]+=Vals[2];
+Vals[2]+=0x08909ae5U;
+
+W[6]=state6;
+W[6]+=Vals[3];
+
+Vals[3]=0x90BB1E3CU;
+Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
+Vals[3]+=(0x9b05688cU^(Vals[0]&0xca0b3af3U));
+
+Vals[7]+=state1;
+Vals[3]+=Vals[7];
+
+W[2]=state2;
+W[2]+=Vals[6];
+
+Vals[6]=0x3c6ef372U;
+Vals[6]+=Vals[3];
+Vals[3]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
+Vals[3]+=Ma2(0xbb67ae85U,Vals[2],0x6a09e667U);
+
+W[5]=state5;
+W[5]+=Vals[4];
+
+Vals[4]=0x50C6645BU;
+Vals[4]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
+Vals[4]+=ch(Vals[6],Vals[0],0x510e527fU);
+Vals[4]+=W[2];
+
+W[1]=Vals[7];
+Vals[7]=0xbb67ae85U;
+Vals[7]+=Vals[4];
+Vals[4]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
+Vals[4]+=Ma2(0x6a09e667U,Vals[3],Vals[2]);
+
+W[4]=state4;
+W[4]+=Vals[1];
+
+Vals[1]=0x3AC42E24U;
+Vals[1]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
+Vals[1]+=ch(Vals[7],Vals[6],Vals[0]);
+Vals[1]+=W[3];
+
+W[0]=Vals[5];
+
+Vals[5]=Vals[1];
+Vals[5]+=0x6a09e667U;
+
+Vals[1]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
+Vals[1]+=Ma(Vals[2],Vals[4],Vals[3]);
+
+Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
+Vals[0]+=ch(Vals[5],Vals[7],Vals[6]);
+Vals[0]+=K[4];
+Vals[0]+=W[4];
+Vals[2]+=Vals[0];
+Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
+Vals[0]+=Ma(Vals[3],Vals[1],Vals[4]);
+
+Vals[6]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
+Vals[6]+=ch(Vals[2],Vals[5],Vals[7]);
+Vals[6]+=K[5];
+Vals[6]+=W[5];
+Vals[3]+=Vals[6];
+Vals[6]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
+Vals[6]+=Ma(Vals[4],Vals[0],Vals[1]);
+
+Vals[7]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
+Vals[7]+=ch(Vals[3],Vals[2],Vals[5]);
+Vals[7]+=K[6];
+Vals[7]+=W[6];
+Vals[4]+=Vals[7];
+Vals[7]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
+Vals[7]+=Ma(Vals[1],Vals[6],Vals[0]);
+
+Vals[5]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
+Vals[5]+=ch(Vals[4],Vals[3],Vals[2]);
+Vals[5]+=K[7];
+Vals[5]+=W[7];
+Vals[1]+=Vals[5];
+Vals[5]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
+Vals[5]+=Ma(Vals[0],Vals[7],Vals[6]);
+
+Vals[2]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
+Vals[2]+=ch(Vals[1],Vals[4],Vals[3]);
+Vals[2]+=0x5807AA98U;
+Vals[0]+=Vals[2];
+Vals[2]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
+Vals[2]+=Ma(Vals[6],Vals[5],Vals[7]);
+
+Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
+Vals[3]+=ch(Vals[0],Vals[1],Vals[4]);
+Vals[3]+=K[9];
+Vals[6]+=Vals[3];
+Vals[3]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
+Vals[3]+=Ma(Vals[7],Vals[2],Vals[5]);
+
+Vals[4]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
+Vals[4]+=ch(Vals[6],Vals[0],Vals[1]);
+Vals[4]+=K[10];
+Vals[7]+=Vals[4];
+Vals[4]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
+Vals[4]+=Ma(Vals[5],Vals[3],Vals[2]);
+
+Vals[1]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
+Vals[1]+=ch(Vals[7],Vals[6],Vals[0]);
+Vals[1]+=K[11];
+Vals[5]+=Vals[1];
+Vals[1]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
+Vals[1]+=Ma(Vals[2],Vals[4],Vals[3]);
+
+Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
+Vals[0]+=ch(Vals[5],Vals[7],Vals[6]);
+Vals[0]+=K[12];
+Vals[2]+=Vals[0];
+Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
+Vals[0]+=Ma(Vals[3],Vals[1],Vals[4]);
+
+Vals[6]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
+Vals[6]+=ch(Vals[2],Vals[5],Vals[7]);
+Vals[6]+=K[13];
+Vals[3]+=Vals[6];
+Vals[6]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
+Vals[6]+=Ma(Vals[4],Vals[0],Vals[1]);
+
+Vals[7]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
+Vals[7]+=ch(Vals[3],Vals[2],Vals[5]);
+Vals[7]+=K[14];
+Vals[4]+=Vals[7];
+Vals[7]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
+Vals[7]+=Ma(Vals[1],Vals[6],Vals[0]);
+
+Vals[5]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
+Vals[5]+=ch(Vals[4],Vals[3],Vals[2]);
+Vals[5]+=0xC19BF274U;
+Vals[1]+=Vals[5];
+Vals[5]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
+Vals[5]+=Ma(Vals[0],Vals[7],Vals[6]);
+
+W[0]+=(rotr(W[1],7)^rotr(W[1],18)^(W[1]>>3U));
+Vals[2]+=W[0];
+Vals[2]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
+Vals[2]+=ch(Vals[1],Vals[4],Vals[3]);
+Vals[2]+=K[16];
+Vals[0]+=Vals[2];
+Vals[2]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
+Vals[2]+=Ma(Vals[6],Vals[5],Vals[7]);
+
+W[1]+=(rotr(W[2],7)^rotr(W[2],18)^(W[2]>>3U));
+W[1]+=0x00a00000U;
+Vals[3]+=W[1];
+Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
+Vals[3]+=ch(Vals[0],Vals[1],Vals[4]);
+Vals[3]+=K[17];
+Vals[6]+=Vals[3];
+Vals[3]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
+Vals[3]+=Ma(Vals[7],Vals[2],Vals[5]);
+
+W[2]+=(rotr(W[3],7)^rotr(W[3],18)^(W[3]>>3U));
+W[2]+=(rotr(W[0],17)^rotr(W[0],19)^(W[0]>>10U));
+Vals[4]+=W[2];
+Vals[4]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
+Vals[4]+=ch(Vals[6],Vals[0],Vals[1]);
+Vals[4]+=K[18];
+Vals[7]+=Vals[4];
+Vals[4]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
+Vals[4]+=Ma(Vals[5],Vals[3],Vals[2]);
+
+W[3]+=(rotr(W[4],7)^rotr(W[4],18)^(W[4]>>3U));
+W[3]+=(rotr(W[1],17)^rotr(W[1],19)^(W[1]>>10U));
+Vals[1]+=W[3];
+Vals[1]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
+Vals[1]+=ch(Vals[7],Vals[6],Vals[0]);
+Vals[1]+=K[19];
+Vals[5]+=Vals[1];
+Vals[1]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
+Vals[1]+=Ma(Vals[2],Vals[4],Vals[3]);
+
+W[4]+=(rotr(W[5],7)^rotr(W[5],18)^(W[5]>>3U));
+W[4]+=(rotr(W[2],17)^rotr(W[2],19)^(W[2]>>10U));
+Vals[0]+=W[4];
+Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
+Vals[0]+=ch(Vals[5],Vals[7],Vals[6]);
+Vals[0]+=K[20];
+Vals[2]+=Vals[0];
+Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
+Vals[0]+=Ma(Vals[3],Vals[1],Vals[4]);
+
+W[5]+=(rotr(W[6],7)^rotr(W[6],18)^(W[6]>>3U));
+W[5]+=(rotr(W[3],17)^rotr(W[3],19)^(W[3]>>10U));
+Vals[6]+=W[5];
+Vals[6]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
+Vals[6]+=ch(Vals[2],Vals[5],Vals[7]);
+Vals[6]+=K[21];
+Vals[3]+=Vals[6];
+Vals[6]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
+Vals[6]+=Ma(Vals[4],Vals[0],Vals[1]);
+
+W[6]+=(rotr(W[7],7)^rotr(W[7],18)^(W[7]>>3U));
+W[6]+=0x00000100U;
+W[6]+=(rotr(W[4],17)^rotr(W[4],19)^(W[4]>>10U));
+Vals[7]+=W[6];
+Vals[7]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
+Vals[7]+=ch(Vals[3],Vals[2],Vals[5]);
+Vals[7]+=K[22];
+Vals[4]+=Vals[7];
+Vals[7]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
+Vals[7]+=Ma(Vals[1],Vals[6],Vals[0]);
+
+W[7]+=0x11002000U;
+W[7]+=W[0];
+W[7]+=(rotr(W[5],17)^rotr(W[5],19)^(W[5]>>10U));
+Vals[5]+=W[7];
+Vals[5]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
+Vals[5]+=ch(Vals[4],Vals[3],Vals[2]);
+Vals[5]+=K[23];
+Vals[1]+=Vals[5];
+Vals[5]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
+Vals[5]+=Ma(Vals[0],Vals[7],Vals[6]);
+
+W[8]=0x80000000U;
+W[8]+=W[1];
+W[8]+=(rotr(W[6],17)^rotr(W[6],19)^(W[6]>>10U));
+Vals[2]+=W[8];
+Vals[2]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
+Vals[2]+=ch(Vals[1],Vals[4],Vals[3]);
+Vals[2]+=K[24];
+Vals[0]+=Vals[2];
+Vals[2]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
+Vals[2]+=Ma(Vals[6],Vals[5],Vals[7]);
+
+W[9]=W[2];
+W[9]+=(rotr(W[7],17)^rotr(W[7],19)^(W[7]>>10U));
+Vals[3]+=W[9];
+Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
+Vals[3]+=ch(Vals[0],Vals[1],Vals[4]);
+Vals[3]+=K[25];
+Vals[6]+=Vals[3];
+Vals[3]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
+Vals[3]+=Ma(Vals[7],Vals[2],Vals[5]);
+
+W[10]=W[3];
+W[10]+=(rotr(W[8],17)^rotr(W[8],19)^(W[8]>>10U));
+Vals[4]+=W[10];
+Vals[4]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
+Vals[4]+=ch(Vals[6],Vals[0],Vals[1]);
+Vals[4]+=K[26];
+Vals[7]+=Vals[4];
+Vals[4]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
+Vals[4]+=Ma(Vals[5],Vals[3],Vals[2]);
+
+W[11]=W[4];
+W[11]+=(rotr(W[9],17)^rotr(W[9],19)^(W[9]>>10U));
+Vals[1]+=W[11];
+Vals[1]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
+Vals[1]+=ch(Vals[7],Vals[6],Vals[0]);
+Vals[1]+=K[27];
+Vals[5]+=Vals[1];
+Vals[1]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
+Vals[1]+=Ma(Vals[2],Vals[4],Vals[3]);
+
+W[12]=W[5];
+W[12]+=(rotr(W[10],17)^rotr(W[10],19)^(W[10]>>10U));
+Vals[0]+=W[12];
+Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
+Vals[0]+=ch(Vals[5],Vals[7],Vals[6]);
+Vals[0]+=K[28];
+Vals[2]+=Vals[0];
+Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
+Vals[0]+=Ma(Vals[3],Vals[1],Vals[4]);
+
+W[13]=W[6];
+W[13]+=(rotr(W[11],17)^rotr(W[11],19)^(W[11]>>10U));
+Vals[6]+=W[13];
+Vals[6]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
+Vals[6]+=ch(Vals[2],Vals[5],Vals[7]);
+Vals[6]+=K[29];
+Vals[3]+=Vals[6];
+Vals[6]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
+Vals[6]+=Ma(Vals[4],Vals[0],Vals[1]);
+
+W[14]=0x00400022U;
+W[14]+=W[7];
+W[14]+=(rotr(W[12],17)^rotr(W[12],19)^(W[12]>>10U));
+Vals[7]+=W[14];
+Vals[7]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
+Vals[7]+=ch(Vals[3],Vals[2],Vals[5]);
+Vals[7]+=K[30];
+Vals[4]+=Vals[7];
+Vals[7]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
+Vals[7]+=Ma(Vals[1],Vals[6],Vals[0]);
+
+W[15]=0x00000100U;
+W[15]+=(rotr(W[0],7)^rotr(W[0],18)^(W[0]>>3U));
+W[15]+=W[8];
+W[15]+=(rotr(W[13],17)^rotr(W[13],19)^(W[13]>>10U));
+Vals[5]+=W[15];
+Vals[5]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
+Vals[5]+=ch(Vals[4],Vals[3],Vals[2]);
+Vals[5]+=K[31];
+Vals[1]+=Vals[5];
+Vals[5]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
+Vals[5]+=Ma(Vals[0],Vals[7],Vals[6]);
+
+W[0]+=(rotr(W[1],7)^rotr(W[1],18)^(W[1]>>3U));
+W[0]+=W[9];
+W[0]+=(rotr(W[14],17)^rotr(W[14],19)^(W[14]>>10U));
+Vals[2]+=W[0];
+Vals[2]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
+Vals[2]+=ch(Vals[1],Vals[4],Vals[3]);
+Vals[2]+=K[32];
+Vals[0]+=Vals[2];
+Vals[2]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
+Vals[2]+=Ma(Vals[6],Vals[5],Vals[7]);
+
+W[1]+=(rotr(W[2],7)^rotr(W[2],18)^(W[2]>>3U));
+W[1]+=W[10];
+W[1]+=(rotr(W[15],17)^rotr(W[15],19)^(W[15]>>10U));
+Vals[3]+=W[1];
+Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
+Vals[3]+=ch(Vals[0],Vals[1],Vals[4]);
+Vals[3]+=K[33];
+Vals[6]+=Vals[3];
+Vals[3]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
+Vals[3]+=Ma(Vals[7],Vals[2],Vals[5]);
+
+W[2]+=(rotr(W[3],7)^rotr(W[3],18)^(W[3]>>3U));
+W[2]+=W[11];
+W[2]+=(rotr(W[0],17)^rotr(W[0],19)^(W[0]>>10U));
+Vals[4]+=W[2];
+Vals[4]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
+Vals[4]+=ch(Vals[6],Vals[0],Vals[1]);
+Vals[4]+=K[34];
+Vals[7]+=Vals[4];
+Vals[4]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
+Vals[4]+=Ma(Vals[5],Vals[3],Vals[2]);
+
+W[3]+=(rotr(W[4],7)^rotr(W[4],18)^(W[4]>>3U));
+W[3]+=W[12];
+W[3]+=(rotr(W[1],17)^rotr(W[1],19)^(W[1]>>10U));
+Vals[1]+=W[3];
+Vals[1]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
+Vals[1]+=ch(Vals[7],Vals[6],Vals[0]);
+Vals[1]+=K[35];
+Vals[5]+=Vals[1];
+Vals[1]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
+Vals[1]+=Ma(Vals[2],Vals[4],Vals[3]);
+
+W[4]+=(rotr(W[5],7)^rotr(W[5],18)^(W[5]>>3U));
+W[4]+=W[13];
+W[4]+=(rotr(W[2],17)^rotr(W[2],19)^(W[2]>>10U));
+Vals[0]+=W[4];
+Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
+Vals[0]+=ch(Vals[5],Vals[7],Vals[6]);
+Vals[0]+=K[36];
+Vals[2]+=Vals[0];
+Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
+Vals[0]+=Ma(Vals[3],Vals[1],Vals[4]);
+
+W[5]+=(rotr(W[6],7)^rotr(W[6],18)^(W[6]>>3U));
+W[5]+=W[14];
+W[5]+=(rotr(W[3],17)^rotr(W[3],19)^(W[3]>>10U));
+Vals[6]+=W[5];
+Vals[6]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
+Vals[6]+=ch(Vals[2],Vals[5],Vals[7]);
+Vals[6]+=K[37];
+Vals[3]+=Vals[6];
+Vals[6]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
+Vals[6]+=Ma(Vals[4],Vals[0],Vals[1]);
+
+W[6]+=(rotr(W[7],7)^rotr(W[7],18)^(W[7]>>3U));
+W[6]+=W[15];
+W[6]+=(rotr(W[4],17)^rotr(W[4],19)^(W[4]>>10U));
+Vals[7]+=W[6];
+Vals[7]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
+Vals[7]+=ch(Vals[3],Vals[2],Vals[5]);
+Vals[7]+=K[38];
+Vals[4]+=Vals[7];
+Vals[7]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
+Vals[7]+=Ma(Vals[1],Vals[6],Vals[0]);
+
+W[7]+=(rotr(W[8],7)^rotr(W[8],18)^(W[8]>>3U));
+W[7]+=W[0];
+W[7]+=(rotr(W[5],17)^rotr(W[5],19)^(W[5]>>10U));
+Vals[5]+=W[7];
+Vals[5]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
+Vals[5]+=ch(Vals[4],Vals[3],Vals[2]);
+Vals[5]+=K[39];
+Vals[1]+=Vals[5];
+Vals[5]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
+Vals[5]+=Ma(Vals[0],Vals[7],Vals[6]);
+
+W[8]+=(rotr(W[9],7)^rotr(W[9],18)^(W[9]>>3U));
+W[8]+=W[1];
+W[8]+=(rotr(W[6],17)^rotr(W[6],19)^(W[6]>>10U));
+Vals[2]+=W[8];
+Vals[2]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
+Vals[2]+=ch(Vals[1],Vals[4],Vals[3]);
+Vals[2]+=K[40];
+Vals[0]+=Vals[2];
+Vals[2]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
+Vals[2]+=Ma(Vals[6],Vals[5],Vals[7]);
+
+W[9]+=(rotr(W[10],7)^rotr(W[10],18)^(W[10]>>3U));
+W[9]+=W[2];
+W[9]+=(rotr(W[7],17)^rotr(W[7],19)^(W[7]>>10U));
+Vals[3]+=W[9];
+Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
+Vals[3]+=ch(Vals[0],Vals[1],Vals[4]);
+Vals[3]+=K[41];
+Vals[6]+=Vals[3];
+Vals[3]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
+Vals[3]+=Ma(Vals[7],Vals[2],Vals[5]);
+
+W[10]+=(rotr(W[11],7)^rotr(W[11],18)^(W[11]>>3U));
+W[10]+=W[3];
+W[10]+=(rotr(W[8],17)^rotr(W[8],19)^(W[8]>>10U));
+Vals[4]+=W[10];
+Vals[4]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
+Vals[4]+=ch(Vals[6],Vals[0],Vals[1]);
+Vals[4]+=K[42];
+Vals[7]+=Vals[4];
+Vals[4]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
+Vals[4]+=Ma(Vals[5],Vals[3],Vals[2]);
+
+W[11]+=(rotr(W[12],7)^rotr(W[12],18)^(W[12]>>3U));
+W[11]+=W[4];
+W[11]+=(rotr(W[9],17)^rotr(W[9],19)^(W[9]>>10U));
+Vals[1]+=W[11];
+Vals[1]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
+Vals[1]+=ch(Vals[7],Vals[6],Vals[0]);
+Vals[1]+=K[43];
+Vals[5]+=Vals[1];
+Vals[1]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
+Vals[1]+=Ma(Vals[2],Vals[4],Vals[3]);
+
+W[12]+=(rotr(W[13],7)^rotr(W[13],18)^(W[13]>>3U));
+W[12]+=W[5];
+W[12]+=(rotr(W[10],17)^rotr(W[10],19)^(W[10]>>10U));
+Vals[0]+=W[12];
+Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
+Vals[0]+=ch(Vals[5],Vals[7],Vals[6]);
+Vals[0]+=K[44];
+Vals[2]+=Vals[0];
+Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
+Vals[0]+=Ma(Vals[3],Vals[1],Vals[4]);
+
+W[13]+=(rotr(W[14],7)^rotr(W[14],18)^(W[14]>>3U));
+W[13]+=W[6];
+W[13]+=(rotr(W[11],17)^rotr(W[11],19)^(W[11]>>10U));
+Vals[6]+=W[13];
+Vals[6]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
+Vals[6]+=ch(Vals[2],Vals[5],Vals[7]);
+Vals[6]+=K[45];
+Vals[3]+=Vals[6];
+Vals[6]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
+Vals[6]+=Ma(Vals[4],Vals[0],Vals[1]);
+
+W[14]+=(rotr(W[15],7)^rotr(W[15],18)^(W[15]>>3U));
+W[14]+=W[7];
+W[14]+=(rotr(W[12],17)^rotr(W[12],19)^(W[12]>>10U));
+Vals[7]+=W[14];
+Vals[7]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
+Vals[7]+=ch(Vals[3],Vals[2],Vals[5]);
+Vals[7]+=K[46];
+Vals[4]+=Vals[7];
+Vals[7]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
+Vals[7]+=Ma(Vals[1],Vals[6],Vals[0]);
+
+W[15]+=(rotr(W[0],7)^rotr(W[0],18)^(W[0]>>3U));
+W[15]+=W[8];
+W[15]+=(rotr(W[13],17)^rotr(W[13],19)^(W[13]>>10U));
+Vals[5]+=W[15];
+Vals[5]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
+Vals[5]+=ch(Vals[4],Vals[3],Vals[2]);
+Vals[5]+=K[47];
+Vals[1]+=Vals[5];
+Vals[5]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
+Vals[5]+=Ma(Vals[0],Vals[7],Vals[6]);
+
+W[0]+=(rotr(W[1],7)^rotr(W[1],18)^(W[1]>>3U));
+W[0]+=W[9];
+W[0]+=(rotr(W[14],17)^rotr(W[14],19)^(W[14]>>10U));
+Vals[2]+=W[0];
+Vals[2]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
+Vals[2]+=ch(Vals[1],Vals[4],Vals[3]);
+Vals[2]+=K[48];
+Vals[0]+=Vals[2];
+Vals[2]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
+Vals[2]+=Ma(Vals[6],Vals[5],Vals[7]);
+
+W[1]+=(rotr(W[2],7)^rotr(W[2],18)^(W[2]>>3U));
+W[1]+=W[10];
+W[1]+=(rotr(W[15],17)^rotr(W[15],19)^(W[15]>>10U));
+Vals[3]+=W[1];
+Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
+Vals[3]+=ch(Vals[0],Vals[1],Vals[4]);
+Vals[3]+=K[49];
+Vals[6]+=Vals[3];
+Vals[3]+=(rotr(Vals[2],2)^rotr(Vals[2],13)^rotr(Vals[2],22));
+Vals[3]+=Ma(Vals[7],Vals[2],Vals[5]);
+
+W[2]+=(rotr(W[3],7)^rotr(W[3],18)^(W[3]>>3U));
+W[2]+=W[11];
+W[2]+=(rotr(W[0],17)^rotr(W[0],19)^(W[0]>>10U));
+Vals[4]+=W[2];
+Vals[4]+=(rotr(Vals[6],6)^rotr(Vals[6],11)^rotr(Vals[6],25));
+Vals[4]+=ch(Vals[6],Vals[0],Vals[1]);
+Vals[4]+=K[50];
+Vals[7]+=Vals[4];
+Vals[4]+=(rotr(Vals[3],2)^rotr(Vals[3],13)^rotr(Vals[3],22));
+Vals[4]+=Ma(Vals[5],Vals[3],Vals[2]);
+
+W[3]+=(rotr(W[4],7)^rotr(W[4],18)^(W[4]>>3U));
+W[3]+=W[12];
+W[3]+=(rotr(W[1],17)^rotr(W[1],19)^(W[1]>>10U));
+Vals[1]+=W[3];
+Vals[1]+=(rotr(Vals[7],6)^rotr(Vals[7],11)^rotr(Vals[7],25));
+Vals[1]+=ch(Vals[7],Vals[6],Vals[0]);
+Vals[1]+=K[51];
+Vals[5]+=Vals[1];
+Vals[1]+=(rotr(Vals[4],2)^rotr(Vals[4],13)^rotr(Vals[4],22));
+Vals[1]+=Ma(Vals[2],Vals[4],Vals[3]);
+
+W[4]+=(rotr(W[5],7)^rotr(W[5],18)^(W[5]>>3U));
+W[4]+=W[13];
+W[4]+=(rotr(W[2],17)^rotr(W[2],19)^(W[2]>>10U));
+Vals[0]+=W[4];
+Vals[0]+=(rotr(Vals[5],6)^rotr(Vals[5],11)^rotr(Vals[5],25));
+Vals[0]+=ch(Vals[5],Vals[7],Vals[6]);
+Vals[0]+=K[52];
+Vals[2]+=Vals[0];
+Vals[0]+=(rotr(Vals[1],2)^rotr(Vals[1],13)^rotr(Vals[1],22));
+Vals[0]+=Ma(Vals[3],Vals[1],Vals[4]);
+
+W[5]+=(rotr(W[6],7)^rotr(W[6],18)^(W[6]>>3U));
+W[5]+=W[14];
+W[5]+=(rotr(W[3],17)^rotr(W[3],19)^(W[3]>>10U));
+Vals[6]+=W[5];
+Vals[6]+=(rotr(Vals[2],6)^rotr(Vals[2],11)^rotr(Vals[2],25));
+Vals[6]+=ch(Vals[2],Vals[5],Vals[7]);
+Vals[6]+=K[53];
+Vals[3]+=Vals[6];
+Vals[6]+=(rotr(Vals[0],2)^rotr(Vals[0],13)^rotr(Vals[0],22));
+Vals[6]+=Ma(Vals[4],Vals[0],Vals[1]);
+
+W[6]+=(rotr(W[7],7)^rotr(W[7],18)^(W[7]>>3U));
+W[6]+=W[15];
+W[6]+=(rotr(W[4],17)^rotr(W[4],19)^(W[4]>>10U));
+Vals[7]+=W[6];
+Vals[7]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
+Vals[7]+=ch(Vals[3],Vals[2],Vals[5]);
+Vals[7]+=K[54];
+Vals[4]+=Vals[7];
+Vals[7]+=(rotr(Vals[6],2)^rotr(Vals[6],13)^rotr(Vals[6],22));
+Vals[7]+=Ma(Vals[1],Vals[6],Vals[0]);
+
+W[7]+=(rotr(W[8],7)^rotr(W[8],18)^(W[8]>>3U));
+W[7]+=W[0];
+W[7]+=(rotr(W[5],17)^rotr(W[5],19)^(W[5]>>10U));
+Vals[5]+=W[7];
+Vals[5]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
+Vals[5]+=ch(Vals[4],Vals[3],Vals[2]);
+Vals[5]+=K[55];
+Vals[1]+=Vals[5];
+Vals[5]+=(rotr(Vals[7],2)^rotr(Vals[7],13)^rotr(Vals[7],22));
+Vals[5]+=Ma(Vals[0],Vals[7],Vals[6]);
+
+W[8]+=(rotr(W[9],7)^rotr(W[9],18)^(W[9]>>3U));
+W[8]+=W[1];
+W[8]+=(rotr(W[6],17)^rotr(W[6],19)^(W[6]>>10U));
+Vals[2]+=W[8];
+Vals[2]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
+Vals[2]+=ch(Vals[1],Vals[4],Vals[3]);
+Vals[2]+=K[56];
+Vals[0]+=Vals[2];
+
+W[9]+=(rotr(W[10],7)^rotr(W[10],18)^(W[10]>>3U));
+W[9]+=W[2];
+W[9]+=(rotr(W[7],17)^rotr(W[7],19)^(W[7]>>10U));
+Vals[3]+=W[9];
+Vals[3]+=(rotr(Vals[0],6)^rotr(Vals[0],11)^rotr(Vals[0],25));
+Vals[3]+=ch(Vals[0],Vals[1],Vals[4]);
+Vals[3]+=K[57];
+Vals[3]+=Vals[6];
+
+W[10]+=(rotr(W[11],7)^rotr(W[11],18)^(W[11]>>3U));
+W[10]+=W[3];
+W[10]+=(rotr(W[8],17)^rotr(W[8],19)^(W[8]>>10U));
+Vals[4]+=W[10];
+Vals[4]+=(rotr(Vals[3],6)^rotr(Vals[3],11)^rotr(Vals[3],25));
+Vals[4]+=ch(Vals[3],Vals[0],Vals[1]);
+Vals[4]+=K[58];
+Vals[4]+=Vals[7];
+Vals[1]+=(rotr(Vals[4],6)^rotr(Vals[4],11)^rotr(Vals[4],25));
+Vals[1]+=ch(Vals[4],Vals[3],Vals[0]);
+Vals[1]+=W[11];
+Vals[1]+=(rotr(W[12],7)^rotr(W[12],18)^(W[12]>>3U));
+Vals[1]+=W[4];
+Vals[1]+=(rotr(W[9],17)^rotr(W[9],19)^(W[9]>>10U));
+Vals[1]+=K[59];
+Vals[1]+=Vals[5];
+
+#define FOUND (0x80)
+#define NFLAG (0x7F)
+
+#if defined(VECTORS2) || defined(VECTORS4)
+	Vals[2]+=Ma(Vals[6],Vals[5],Vals[7]);
+	Vals[2]+=(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22));
+	Vals[2]+=W[12];
+	Vals[2]+=(rotr(W[13],7)^rotr(W[13],18)^(W[13]>>3U));
+	Vals[2]+=W[5];
+	Vals[2]+=(rotr(W[10],17)^rotr(W[10],19)^(W[10]>>10U));
+	Vals[2]+=Vals[0];
+	Vals[2]+=(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25));
+	Vals[2]+=ch(Vals[1],Vals[4],Vals[3]);
+
+	if (any(Vals[2] == 0x136032edU)) {
+		if (Vals[2].x == 0x136032edU)
+			output[FOUND] = output[NFLAG & nonce.x] = nonce.x;
+		if (Vals[2].y == 0x136032edU)
+			output[FOUND] = output[NFLAG & nonce.y] = nonce.y;
+#if defined(VECTORS4)
+		if (Vals[2].z == 0x136032edU)
+			output[FOUND] = output[NFLAG & nonce.z] = nonce.z;
+		if (Vals[2].w == 0x136032edU)
+			output[FOUND] = output[NFLAG & nonce.w] = nonce.w;
+#endif
+	}
+#else
+	if ((Vals[2]+
+		Ma(Vals[6],Vals[5],Vals[7])+
+		(rotr(Vals[5],2)^rotr(Vals[5],13)^rotr(Vals[5],22))+
+		W[12]+
+		(rotr(W[13],7)^rotr(W[13],18)^(W[13]>>3U))+
+		W[5]+
+		(rotr(W[10],17)^rotr(W[10],19)^(W[10]>>10U))+
+		Vals[0]+
+		(rotr(Vals[1],6)^rotr(Vals[1],11)^rotr(Vals[1],25))+
+		ch(Vals[1],Vals[4],Vals[3])) == 0x136032edU)
+			output[FOUND] = output[NFLAG & nonce] =  nonce;
+#endif
+}

+ 0 - 2
sha256_4way.c

@@ -111,8 +111,6 @@ bool ScanHash_4WaySSE2(int thr_id, const unsigned char *pmidstate,
 
 
 	pdata += 64;
 	pdata += 64;
 
 
-    work_restart[thr_id].restart = 0;
-
     for (;;)
     for (;;)
     {
     {
         unsigned int thash[9][NPAR] __attribute__((aligned(128)));
         unsigned int thash[9][NPAR] __attribute__((aligned(128)));

+ 0 - 2
sha256_altivec_4way.c

@@ -84,8 +84,6 @@ bool ScanHash_altivec_4way(int thr_id, const unsigned char *pmidstate,
 
 
 	pdata += 64;
 	pdata += 64;
 
 
-    work_restart[thr_id].restart = 0;
-
     for (;;)
     for (;;)
     {
     {
         unsigned int thash[9][NPAR] __attribute__((aligned(128)));
         unsigned int thash[9][NPAR] __attribute__((aligned(128)));

+ 0 - 2
sha256_cryptopp.c

@@ -589,8 +589,6 @@ bool scanhash_asm32(int thr_id, const unsigned char *midstate,
 
 
 	data += 64;
 	data += 64;
 
 
-	work_restart[thr_id].restart = 0;
-
 	while (1) {
 	while (1) {
 		n++;
 		n++;
 		*nonce = n;
 		*nonce = n;

+ 0 - 2
sha256_generic.c

@@ -251,8 +251,6 @@ bool scanhash_c(int thr_id, const unsigned char *midstate, unsigned char *data,
 
 
 	data += 64;
 	data += 64;
 
 
-	work_restart[thr_id].restart = 0;
-
 	while (1) {
 	while (1) {
 		n++;
 		n++;
 		*nonce = n;
 		*nonce = n;

+ 0 - 2
sha256_sse2_amd64.c

@@ -65,8 +65,6 @@ bool scanhash_sse2_64(int thr_id, const unsigned char *pmidstate,
 
 
 	pdata += 64;
 	pdata += 64;
 
 
-    work_restart[thr_id].restart = 0;
-
     /* For debugging */
     /* For debugging */
     union {
     union {
         __m128i m;
         __m128i m;

+ 0 - 2
sha256_sse2_i386.c

@@ -65,8 +65,6 @@ bool scanhash_sse2_32(int thr_id, const unsigned char *pmidstate,
 
 
 	pdata += 64;
 	pdata += 64;
 
 
-    work_restart[thr_id].restart = 0;
-
     /* Message expansion */
     /* Message expansion */
     memcpy(m_midstate, pmidstate, sizeof(m_midstate));
     memcpy(m_midstate, pmidstate, sizeof(m_midstate));
     memcpy(m_w, pdata, sizeof(m_w)); /* The 2nd half of the data */
     memcpy(m_w, pdata, sizeof(m_w)); /* The 2nd half of the data */

+ 0 - 2
sha256_sse4_amd64.c

@@ -62,8 +62,6 @@ bool scanhash_sse4_64(int thr_id, const unsigned char *pmidstate,
 
 
 	pdata += 64;
 	pdata += 64;
 
 
-    work_restart[thr_id].restart = 0;
-
     /* For debugging */
     /* For debugging */
     union {
     union {
         __m128i m;
         __m128i m;

+ 0 - 2
sha256_via.c

@@ -35,8 +35,6 @@ bool scanhash_via(int thr_id, const unsigned char *pmidstate,
 	unsigned long stat_ctr = 0;
 	unsigned long stat_ctr = 0;
 	int i;
 	int i;
 
 
-	work_restart[thr_id].restart = 0;
-
 	/* bitcoin gives us big endian input, but via wants LE,
 	/* bitcoin gives us big endian input, but via wants LE,
 	 * so we reverse the swapping bitcoin has already done (extra work)
 	 * so we reverse the swapping bitcoin has already done (extra work)
 	 * in order to permit the hardware to swap everything
 	 * in order to permit the hardware to swap everything

+ 5 - 0
todo_ztex.txt

@@ -0,0 +1,5 @@
+- verify setting cgpu.status=DEAD does in fact stop the thread
+- allow configuring bitstream directory
+- HS fpga config
+- allow configuring LIBZTEX_OVERHEATTHRESHOLD
+- hotplug support?

+ 13 - 4
util.c

@@ -30,6 +30,7 @@
 # include <winsock2.h>
 # include <winsock2.h>
 # include <mstcpip.h>
 # include <mstcpip.h>
 #endif
 #endif
+
 #include "miner.h"
 #include "miner.h"
 #include "elist.h"
 #include "elist.h"
 #include "compat.h"
 #include "compat.h"
@@ -364,10 +365,16 @@ json_t *json_rpc_call(CURL *curl, const char *url,
 	if (probing) {
 	if (probing) {
 		pool->probed = true;
 		pool->probed = true;
 		/* If X-Long-Polling was found, activate long polling */
 		/* If X-Long-Polling was found, activate long polling */
-		if (hi.lp_path)
+		if (hi.lp_path) {
+			if (pool->hdr_path != NULL)
+				free(pool->hdr_path);
 			pool->hdr_path = hi.lp_path;
 			pool->hdr_path = hi.lp_path;
-		else
+		} else {
 			pool->hdr_path = NULL;
 			pool->hdr_path = NULL;
+		}
+	} else if (hi.lp_path) {
+		free(hi.lp_path);
+		hi.lp_path = NULL;
 	}
 	}
 
 
 	*rolltime = hi.has_rolltime;
 	*rolltime = hi.has_rolltime;
@@ -410,9 +417,11 @@ json_t *json_rpc_call(CURL *curl, const char *url,
 		goto err_out;
 		goto err_out;
 	}
 	}
 
 
-	if (hi.reason)
+	if (hi.reason) {
 		json_object_set_new(val, "reject-reason", json_string(hi.reason));
 		json_object_set_new(val, "reject-reason", json_string(hi.reason));
-
+		free(hi.reason);
+		hi.reason = NULL;
+	}
 	successful_connect = true;
 	successful_connect = true;
 	databuf_free(&all_data);
 	databuf_free(&all_data);
 	curl_slist_free_all(headers);
 	curl_slist_free_all(headers);

+ 224 - 0
windows-build.txt

@@ -0,0 +1,224 @@
+######################################################################################
+#                                                                                    #
+#          Native WIN32 setup and build instructions (on mingw32/Windows):           #
+#                                                                                    #
+######################################################################################
+
+**************************************************************************************
+* Introduction                                                                       *
+**************************************************************************************
+The following instructions have been tested on both Windows 7 and Windows XP.
+Most of what is described below (copying files, downloading files, etc.) can be done
+directly in the MinGW MSYS shell; these instructions do not do so because package
+versions and links change over time. The best way is to use your browser, go to the
+links directly, and see for yourself which versions you want to install.
+
+If you think that this documentation was helpful and you wish to donate, you can 
+do so at the following address. 12KaKtrK52iQjPdtsJq7fJ7smC32tXWbWr
+
+**************************************************************************************
+* A tip that might help you along the way                                            *
+**************************************************************************************
+Enable "QuickEdit Mode" in your Command Prompt Window or MinGW Command Prompt
+Window (No need to go into the context menu to choose edit-mark/copy/paste):
+Right-click on the title bar and click Properties. Under the Options tab, check
+the box for "QuickEdit Mode". Alternately, if you want this change to be
+permanent on all of your Command Prompt Windows; you can click Defaults instead
+of Properties as described above. Now you can drag and select text you want to
+copy, right-click to copy the text to the clipboard and right-click once again to
+paste it at the desired location. You could for example, copy some text from this
+document to the clipboard and right click in your Command Prompt Window to paste
+what you copied.
+
+**************************************************************************************
+* Install mingw32                                                                    *
+**************************************************************************************
+Go to this url ==> http://www.mingw.org/wiki/Getting_Started
+Click the link that says "Download and run the latest mingw-get-inst version."
+Download and run the latest file. Install MinGW in the default directory.
+(I downloaded the one labeled "mingw-get-inst-20111118" - note that this could 
+be a different version later.)
+Make sure to check the option for "Download latest repository catalogs".
+I just selected all the check boxes (excluding "Fortran Compiler") so that everything
+was installed.
+
+**************************************************************************************
+* Create mstcpip.h                                                                   *
+**************************************************************************************
+Open notepad and copy the following into it. Save it as "\MinGW\include\mstcpip.h".
+Make sure it does not have the ".txt" extension (If it does then rename it).
+
+struct tcp_keepalive
+{
+    u_long onoff;
+    u_long keepalivetime;
+    u_long keepaliveinterval;
+};
+
+#ifndef USE_WS_PREFIX
+
+#define SIO_KEEPALIVE_VALS    _WSAIOW(IOC_VENDOR, 4)
+
+#else
+
+#define WS_SIO_KEEPALIVE_VALS    _WSAIOW(WS_IOC_VENDOR, 4)
+
+#endif
+
+**************************************************************************************
+* Run the MSYS shell for the first time to create your user directory                *
+************************************************************************************** 
+(Start Icon/keyboard key ==> All Programs ==> MinGW ==> MinGW Shell).
+This will create your user directory for you.
+
+**************************************************************************************
+* Install libpdcurses                                                                *
+**************************************************************************************
+Type the lines below to install libpdcurses.
+mingw-get install mingw32-libpdcurses
+mingw-get install mingw32-pdcurses
+Ctrl-D or typing "logout" and pressing the enter key should get you out of the
+window.
+
+**************************************************************************************
+* Copy CGMiner source to your MSYS working directory                                 *
+**************************************************************************************
+Copy CGMiner source code directory into: 
+\MinGW\msys\1.0\home\(folder with your user name)
+
+**************************************************************************************
+* Install AMD APP SDK, latest version (only if you want GPU mining)                  *
+**************************************************************************************
+Note: You do not need to install the AMD APP SDK if you are only using Nvidia GPU's
+Go to this url for the latest AMD APP SDK: 
+ http://developer.amd.com/sdks/AMDAPPSDK/downloads/Pages/default.aspx
+Go to this url for legacy AMD APP SDK's:
+ http://developer.amd.com/sdks/AMDAPPSDK/downloads/pages/AMDAPPSDKDownloadArchive.aspx
+Download and install whichever version you like best.
+Copy the folders in \Program Files (x86)\AMD APP\include to \MinGW\include 
+Copy \Program Files (x86)\AMD APP\lib\x86\libOpenCL.a to \MinGW\lib
+Note: If you are on a 32 bit version of windows "Program Files (x86)" will be 
+"Program Files".
+Note2: If you update your APP SDK later you might want to recopy the above files 
+
+**************************************************************************************
+* Install AMD ADL SDK, latest version (only if you want GPU monitoring)              *
+**************************************************************************************
+Note: You do not need to install the AMD ADL SDK if you are only using Nvidia GPU's	
+Go to this url ==> http://developer.amd.com/sdks/ADLSDK/Pages/default.aspx
+Download and unzip the file you downloaded.
+Pull adl_defines.h, adl_sdk.h, and adl_structures.h out of the include folder 
+Put those files into the ADL_SDK folder in your source tree as shown below.
+\MinGW\msys\1.0\home\(folder with your user name)\cgminer-x.x.x\ADL_SDK
+
+**************************************************************************************
+* Install GTK-WIN, required for Pkg-config in the next step                          *
+**************************************************************************************
+Go to this url ==> http://sourceforge.net/projects/gtk-win/ 
+Download the file.
+After you have downloaded the file Double click/run it and this will install GTK+
+I chose all the selection boxes when I installed.
+Copy libglib-2.0-0.dll and intl.dll from \Program Files (x86)\gtk2-runtime\bin to 
+\MinGW\bin
+Note: If you are on a 32 bit version of windows "Program Files (x86)" will be 
+"Program Files".
+
+**************************************************************************************
+* Install pkg-config                                                                 *
+**************************************************************************************
+Go to this url ==> http://www.gtk.org/download/win32.php
+Scroll down to where it shows pkg-cfg.
+Download the file from the tool link. Extract "pkg-config.exe" from bin and place in
+your  \MinGW\bin directory.
+Download the file from the "Dev" link. Extract "pkg.m4" from share\aclocal and place
+in your \MingW\share\aclocal directory.
+		
+**************************************************************************************
+* Install libcurl                                                                    *
+**************************************************************************************
+Go to this url ==> http://curl.haxx.se/download.html#Win32
+At the section where it says "Win32 - Generic", Click on the link that indicates
+Win32 2000.XP 7.24.0 libcurl SSL and download it.
+The one I downloaded may not be current for you. Choose the latest.
+Extract the files that are in the zip (bin, include, and lib) to their respective
+locations in MinGW (\MinGW\bin, \MinGW\include, and \MinGW\lib).
+Edit the file \MinGW\lib\pkgconfig\libcurl.pc and change "-lcurl" to 
+"-lcurl -lcurldll".
+Ref. http://old.nabble.com/gcc-working-with-libcurl-td20506927.html
+
+**************************************************************************************
+* Build cgminer.exe                                                                  *
+**************************************************************************************
+Run the MinGW MSYS shell 
+(Start Icon/keyboard key ==> All Programs ==> MinGW ==> MinGW Shell).	
+Change the working directory to your CGMiner project folder.
+Example: cd cgminer-2.1.2 [Enter Key] if you are unsure then type "ls -la"
+Another way is to type "cd cg" and then press the tab key; It will auto fill.		
+Type the lines below one at a time. Look for problems after each one before going on
+to the next.
+
+      adl.sh (optional - see below)
+      autoreconf -fvi
+      CFLAGS="-O2 -msse2" ./configure (additional config options, see below)
+      make
+
+**************************************************************************************
+* Copy files to a build directory/folder                                             *
+**************************************************************************************
+Make a directory and copy the following files into it. This will be your CGMiner
+Folder that you use for mining. Remember the .cl filenames could change on later
+releases. If you installed a different version of libcurl then some of those dll's
+may be different as well.
+  cgminer.exe     from \MinGW\msys\1.0\home\(username)\cgminer-x.x.x 
+  *.cl            from \MinGW\msys\1.0\home\(username)\cgminer-x.x.x
+  README          from \MinGW\msys\1.0\home\(username)\cgminer-x.x.x
+  libcurl.dll     from \MinGW\bin
+  libeay32.dll    from \MinGW\bin
+  libidn-11.dll   from \MinGW\bin
+  libssl32.dll    from \MinGW\bin
+  libpdcurses.dll from \MinGW\bin
+  pthreadGC2.dll  from \MinGW\bin
+  
+**************************************************************************************
+* Optional - Install Git into MinGW/MSYS                                             *
+**************************************************************************************
+Go to this url ==> http://code.google.com/p/msysgit/
+Click on the Downloads tab.
+Download the latest "Portable" git archive.
+Extract the git*.exe files from the bin folder and put them into \MinGW\bin.
+Extract the share\git-core folder and place it into \MinGW\share.
+To test if it is working, open a MinGW shell and type the following:
+  git config -–global core.autocrlf false (note: one time run only)
+  git clone git://github.com/ckolivas/cgminer.git
+  
+If you simply just want to update the source after you have already cloned, type:
+  git pull git://github.com/ckolivas/cgminer.git
+
+Now you can get the latest source directly from github.
+
+**************************************************************************************
+* Optional - Make a .sh file to automate copying over ADL files                      *
+**************************************************************************************
+Make a folder/directory in your home folder and name it ADL_SDK.
+ (ref:  \MinGW\msys\1.0\home\(folder with your user name)\ADL_SDK)
+Copy the ADL .h files into that folder/directory.
+Open your favorite text editor and type the following into it.
+ cp -av ../ADL_SDK/*.h ADL_SDK
+Save the file as "adl.sh" and then place the file into "\MinGW\msys\1.0\bin".
+From now on when your current working directory is the cgminer source directory
+You can simply type "adl.sh" and it will place the ADL header files into place
+For you. Make sure you never remove the ADL_SDK folder from your home folder.
+
+**************************************************************************************
+* Some ./configure options                                                           *
+**************************************************************************************
+--disable-opencl        Override detection and disable building with opencl
+--disable-adl           Override detection and disable building with adl
+--enable-bitforce       Compile support for BitForce FPGAs(default disabled)
+--enable-icarus         Compile support for Icarus Board(default disabled)
+
+######################################################################################
+#                                                                                    #
+#       Native WIN32 setup and build instructions (on mingw32/Windows) complete      #
+#                                                                                    #
+######################################################################################

Some files were not shown because too many files changed in this diff