*Communication with the Bitcoin network
This part is pretty simple. I’m using bitcoind to do the network communication. This is pretty standard for bitcoin mining, the mining software focuses on doing the hashing and lets bitcoind do the p2p network stuff. There’s a few standard protocols for those two pieces to communicate with varying levels of efficiency, but I’m using the most basic ‘getwork’ protocol because, heh, this isn’t going to be the bottleneck in this operation.
For the portions of computing that do not happen on the NES, I’ve got a raspberry pi housed in a Makerbot Replicator2 3D printed case. I believe I am now fully 2013 Hack Project compliant.
Raspian’s repos were serving some crusty version of arm bitcoind, so I compiled my own from the latest source. This is not hard, except that my rpi has 256MB of RAM and g++ just gives up when its all filled up. I’m sure there’s a more elegant cross-compilation environment available, but adding a 2GB swap and letting it crank all night worked for me.
*Data in
There are a couple pieces involved in getting data into the NES, many of which I’m just pasting together from other people’s code. They deserve the credit for their respective projects.
First, I’m using a python implementation of the mining protocol by jgarzik (https://github.com/jgarzik/pyminer) to do the jsonrpc communication out to bitcoind and basic structure.
Next, I’m using a NES that I modified with a USB CopyNES (http://www.retrousb.com/product_inf…&products_id=36). Inserting this board inbetween the NES’s mainboard and processor adds a USB port to an NES that affords for lots of development capability, including the ability to write to RAM carts. The RAM cart I’m using is a PowerPak Lite, also from retrousb.com (http://www.retrousb.com/product_info.php?products_id=35)
For the USB serial communications to the NES I’ve gutted and hacked up mstrand’s script (https://github.com/mstrand/copynes)
So the rpi getworks a chunk of data, wraps it up into a ROM (detailed below), and sends it to the console using via USB CopyNES.
*Doing the hashing
SHA256 hashing uses many 32-bit operations, and the 6502 in the NES is an 8-bit CPU. Initially I thought this would be a significant challenge, but with some modifications, I got an open implementation of SHA256 to compile to a 6502 target using the cc65 compiler (cc65.org)
The rpi getworks a chunk of data, compiles it into a ROM that includes the SHA256 algo and current target data, and sends it to the console via USB CopyNES.
Each ROM computes and tests a single hash.
*Data Out
After the NES computes and tests the hash against the target value, the NES knows if this iteration was a success. But we need to pass successes back out to the bitcoin network.
If the generated hash is less than the target value, the background color of the screen will be green, otherwise it will be red. A Playstation Eye camera pointed at the screen takes a picture and, using OpenCV, checks for the predominant color in the image.
If there’s more green than red in the picture that the webcam snaps then it gets reported out to the bitcoin network as a success. If not, then we start the process over again with a freshly grabbed chunk of getwork data.