Mining Bitcoin on the Game Boy - YouTube

Channel: stacksmashing

[0]
Hey!
[1]
So a couple of days ago, Elon Musk announced that you can now buy a Tesla using Bitcoins.
[5]
Given that news, one thing was clear to me: I needed to get into the Bitcoin mining game.
[10]
But upon checking the online stores, it seemed like all decently fast GPUs were out of stock!
[15]
But then I realized: I have a lot of very high-end gaming hardware just lying around,
[19]
why not use that for mining bitcoin? And so I grabbed my Game Boy and got started.
[24]
Now my goal was to use an origina, unmodified game boy: I want to plugin a cartridge, and
[28]
start mining. But to mine, we need to somehow communicate
[31]
with a Bitcoin node that communicates with the bitcoin network and will give us the data
[36]
required for mining. We also need it to announce our block in case we manage to mine one. But
[40]
the Game Boy doesn鈥檛 have Wifi or anything, so how can we get it communicate with the
[44]
bitcoin node?
[45]
Well luckily the Game Boy has a link port, which is normally used to perform pokemon
[49]
trading and other highly important things, but for mining, we can use it to communicate
[54]
with a computer. And to build an adapter from link cable to USB, I decided to use the Raspberry
[59]
Pi Pico, as it makes it really easy to implement all kinds of IO protocols and I was really
[64]
looking forward to making use of that. If you are interested in learning more about
[67]
the programmable IO features, check out the in-depth video I did on it.
[73]
Now I won鈥檛 go into too much detail on how mining itself works, but the basic, simplified
[78]
idea is: For each block we take some magic blockchain
[81]
data, we sha256 it - twice, and then we treat the resulting hash as a 256 bit number.
[87]
We then compare it against something called the target:
[90]
This target changes automatically with the mining power of the bitcoin network, and ensures
[94]
that a block is mined roughly every 10 minutes,even if the global mining power increases.
[99]
If our hash is smaller or equal to the target, we just successfully mined a block, yielding
[103]
us 12.5 bitcoin! As you can probably imagine, the smaller the target, the harder it gets
[109]
to generate a hash that鈥檚 smaller than it, and it鈥檚 already very very hard to successfully
[113]
mine a bitcoin block.
[114]
The data that we hash is a 76 bytelong, to which we append a 4 byte nonce: After each
[120]
double-hash we check if the hash is smaller than the target, if not, we increase the nonce,
[124]
and try again. And again. And again.
[127]
And that鈥檚 exactly what we need to do on the Game Boy: Receive the 76 byte large data
[131]
and the target value from the mining software running on a computer, and start hashing.
[136]
If our hash is smaller than the target, we need to report that to the computer, and announce
[140]
our block on the bitcoin network to get our well earned bitcoins.
[144]
If not, we increase the nonce and go back to hashing again.
[147]
So, lets get started with the hardware side of the setup:
[150]
First we need a way to run software on the Game Boy: I鈥檓 using a simple USB flash card,
[154]
which lets me quickly load my compiled ROMs onto the Game Boy.
[157]
I also modified an old Game Boy Link Cable, so that I can simply plug it into a bread
[161]
board.
[162]
Next, to connect the link cable to our PC, I鈥檓 using a Raspberry Pi Pico. Now a problem
[167]
is that the Pico runs at 3.3V, but the Link Cable uses 5V logic levels. To solve this
[173]
I鈥檓 using a simple 4 channel, bi-directional logic shifter. We don鈥檛 need the bi-directional
[178]
feature in this case, but these were the only ones I had laying around and they work pretty
[182]
well.
[183]
After everything is set up, we have this sweet setup: The Game Boy connected to the Raspberry
[187]
Pi Pico via the level shifter, and the Pico connected to the PC.
[191]
Now it鈥檚 time to program the Game Boy ROM: If you are interested in getting started with
[195]
Game Boy development, Modern Vintage Gamer released a great video on how setup & use
[199]
GBDK - the open-source Game Boy development kit. The ROM is written in C, and as mentioned
[205]
for mining bitcoin, we need a SHA256 implementation. There are a lot of different ones floating
[210]
around, I decided to use the one that鈥檚 also used by the Trezor hardware wallet, as
[214]
I thought it鈥檇 nicely fit in.
[215]
The Game Boy mining code itself is really simple, and you can find it linked in the
[220]
description:
[221]
We have an endless loop that first receives both the block data and the target value that
[225]
we need for mining from the computer, and then it starts mining:
[229]
We Copy the nonce into the block data, hash the whole thing the first time, the second
[234]
time, and then we check if the resulting hash is smaller than or equal to the target value!
[238]
If it is we successfully mined a block - earning us 12.5 bitcoin!
[242]
All we need to do now is send the successful nonce back to the computer, where the mining
[246]
software will fill in the rest of the data and send it to our bitcoin node!
[250]
After adding some more checks and also a nice bitcoin sprite that moves across the screen
[254]
as we鈥檙e mining, we have the mining software done and nicely running on the Game Boy!
[258]
Next lets look at the firmware for the Raspberry Pi Pico, which acts as our USB to link cable
[263]
adapter. Before we can write the firmware, we need to understand how the link cable protocol
[268]
works:
[269]
The link cable has 3 signals that we are interested in:
[272]
Clock Serial in
[273]
And Serial out
[275]
If we connect two devices via link cable, the clocks will be connected to each other,
[279]
and the serial out of one side will be connected to the serial IN of the other side and vice
[283]
versa.
[284]
In our case, we can simply - via the level shifter - connect all pins to any of the Rasperry鈥檚
[290]
GPIOs.
[291]
Lets also take a quick look at how the data transfer works:
[294]
Each data transfer is initiated by one side of the communication, lets call it the initiator.
[300]
The initiator will send a clock signal on the clock line, and on each falling edge,
[304]
will encode the bit it wants to send on the serial out line. The receiving end, which
[308]
will receive the data on its serial-in line, will wait for the rising edge of the clock
[312]
and check which bit it currently receives on the serial in, and shift it into an 8 byte
[317]
register. This is important to remember: Each transfer is 8 bits - aka 1 byte long. Also
[322]
you might notice: We can send and receive at the same time: The counterpart of our transfer
[327]
can send us data while we are transferring data to it!
[330]
The default clock speed is roughly 8 khz, so we get roughly one kilobyte per second
[334]
transfer speed.
[335]
Now you might already recognize that basically the link cable protocol is SPI: the Serial
[340]
Peripheral Bus. And the Pico already comes with SPI examples, and all we have to do is
[345]
adjust the clock speed for the SPI PIO state machine, and then we are ready to talk to
[350]
the Game Boy! And that鈥檚 all this tiny piece of firmware here does: It initializes SPI
[355]
and USB serial, and then sends each byte that it receives on the USB serial interface out
[360]
via SPI, and sends back to the computer whatever was received. By the way, in our case the
[366]
Raspberry Pi will always be the initiator of a transfer.
[370]
Lets go to the computer side: First of, to mine bitcoin, we need to run
[374]
a bitcoin node. To initialize our node, it needs to download a couple of hundred gigabytes
[378]
of data first, so if you try to do this at home, I recommend that you start with this.
[383]
Next, we need a mining software. Now a lot of the mining examples you can find online
[388]
are outdated and don鈥檛 work with a modern bitcoin node, but after a while I found ntgbtminer
[393]
by Vanya Sergeev, a basic, python based CPU miner.
[397]
This miner makes it really easy to replace the main mining function with a bit of code
[401]
that communicates with our Game Boy. It basically sends the target value and the block data
[405]
to our game boy, and checks in regularly with it to see whether it was able to successfully
[410]
mine a block. It also also notify the game boy if someone else successfully mined a block
[414]
and provide it with the new block hash and target value.
[417]
Again, you can find the code for the miner linked in the description.
[420]
And now it was time for the big moment: It was time to start mining and finally earn
[424]
some well deserved bitcoins! So lets boot the Game Boy, and wait for the
[428]
mining ROM to load. Now lets start the miner on the computer, wait for it to transfer the
[432]
data, And yes, it鈥檚 working!
[434]
The game boy received all required data, and is now happily chugging along! Unexpectedly,
[439]
you can actually HEAR it working, there鈥檚 a certain signature coilwhine when it鈥檚
[443]
running that鈥檚 hard to capture on camera!
[445]
On the screen you can see the nonce increasing, and the hash rate is pretty impressive: roughly
[449]
0.8 hashes per second! If you compare that to a modern ASIC miner, which comes in at
[454]
around 100 Terahashes per second, you can see that we are almost as fast, only off by
[459]
a factor of roughly 125 trillion. At this rate, it should only take us a couple of quadrillion
[464]
years to mine a bitcoin. And remember that the ASIC miners use a ton
[468]
of power, while our Game Boy simply runs on 4 AA batteries. Big success,
[473]
Now to see whether the Game Boy miner is really able to mine blocks, I鈥檝e also set up a
[477]
new, empty bitcoin blockchain with a very low difficulty, and the Game Boy indeed is
[481]
able to successfully mine blocks for it! Pretty cool, blockchain on game boy!
[486]
Obviously, mining bitcoin on a game boy is everything but profitable, but I learned a
[489]
lot of things while building this, and definitely had a ton of fun!
[492]
By the way, if you want to experiment with the Game Boy hardware, you can now order the
[497]
Breakout and ROM cartridge that liveoverflow and me showed in our game boy series a while
[501]
ago, you can find the link in the description.
[504]
I hope you enjoyed this video, and to see you on this channel again soon! Thank you!