Hey!
So a couple of days ago, Elon Musk announced that you can now buy a Tesla using Bitcoins.
Given that news, one thing was clear to me: I needed to get into the Bitcoin mining game.
But upon checking the online stores, it seemed like all decently fast GPUs were out of stock!
But then I realized: I have a lot of very high-end gaming hardware just lying around,
why not use that for mining bitcoin? And so I grabbed my Game Boy and got started.
Now my goal was to use an origina, unmodified game boy: I want to plugin a cartridge, and
start mining. But to mine, we need to somehow communicate
with a Bitcoin node that communicates with the bitcoin network and will give us the data
required for mining. We also need it to announce our block in case we manage to mine one. But
the Game Boy doesn’t have Wifi or anything, so how can we get it communicate with the
bitcoin node?
Well luckily the Game Boy has a link port, which is normally used to perform pokemon
trading and other highly important things, but for mining, we can use it to communicate
with a computer. And to build an adapter from link cable to USB, I decided to use the Raspberry
Pi Pico, as it makes it really easy to implement all kinds of IO protocols and I was really
looking forward to making use of that. If you are interested in learning more about
the programmable IO features, check out the in-depth video I did on it.
Now I won’t go into too much detail on how mining itself works, but the basic, simplified
idea is: For each block we take some magic blockchain
data, we sha256 it - twice, and then we treat the resulting hash as a 256 bit number.
We then compare it against something called the target:
This target changes automatically with the mining power of the bitcoin network, and ensures
that a block is mined roughly every 10 minutes,even if the global mining power increases.
If our hash is smaller or equal to the target, we just successfully mined a block, yielding
us 12.5 bitcoin! As you can probably imagine, the smaller the target, the harder it gets
to generate a hash that’s smaller than it, and it’s already very very hard to successfully
mine a bitcoin block.
The data that we hash is a 76 bytelong, to which we append a 4 byte nonce: After each
double-hash we check if the hash is smaller than the target, if not, we increase the nonce,
and try again. And again. And again.
And that’s exactly what we need to do on the Game Boy: Receive the 76 byte large data
and the target value from the mining software running on a computer, and start hashing.
If our hash is smaller than the target, we need to report that to the computer, and announce
our block on the bitcoin network to get our well earned bitcoins.
If not, we increase the nonce and go back to hashing again.
So, lets get started with the hardware side of the setup:
First we need a way to run software on the Game Boy: I’m using a simple USB flash card,
which lets me quickly load my compiled ROMs onto the Game Boy.
I also modified an old Game Boy Link Cable, so that I can simply plug it into a bread
board.
Next, to connect the link cable to our PC, I’m using a Raspberry Pi Pico. Now a problem
is that the Pico runs at 3.3V, but the Link Cable uses 5V logic levels. To solve this
I’m using a simple 4 channel, bi-directional logic shifter. We don’t need the bi-directional
feature in this case, but these were the only ones I had laying around and they work pretty
well.
After everything is set up, we have this sweet setup: The Game Boy connected to the Raspberry
Pi Pico via the level shifter, and the Pico connected to the PC.
Now it’s time to program the Game Boy ROM: If you are interested in getting started with
Game Boy development, Modern Vintage Gamer released a great video on how setup & use
GBDK - the open-source Game Boy development kit. The ROM is written in C, and as mentioned
for mining bitcoin, we need a SHA256 implementation. There are a lot of different ones floating
around, I decided to use the one that’s also used by the Trezor hardware wallet, as
I thought it’d nicely fit in.
The Game Boy mining code itself is really simple, and you can find it linked in the
description:
We have an endless loop that first receives both the block data and the target value that
we need for mining from the computer, and then it starts mining:
We Copy the nonce into the block data, hash the whole thing the first time, the second
time, and then we check if the resulting hash is smaller than or equal to the target value!
If it is we successfully mined a block - earning us 12.5 bitcoin!
All we need to do now is send the successful nonce back to the computer, where the mining
software will fill in the rest of the data and send it to our bitcoin node!
After adding some more checks and also a nice bitcoin sprite that moves across the screen
as we’re mining, we have the mining software done and nicely running on the Game Boy!
Next lets look at the firmware for the Raspberry Pi Pico, which acts as our USB to link cable
adapter. Before we can write the firmware, we need to understand how the link cable protocol
works:
The link cable has 3 signals that we are interested in:
Clock Serial in
And Serial out
If we connect two devices via link cable, the clocks will be connected to each other,
and the serial out of one side will be connected to the serial IN of the other side and vice
versa.
In our case, we can simply - via the level shifter - connect all pins to any of the Rasperry’s
GPIOs.
Lets also take a quick look at how the data transfer works:
Each data transfer is initiated by one side of the communication, lets call it the initiator.
The initiator will send a clock signal on the clock line, and on each falling edge,
will encode the bit it wants to send on the serial out line. The receiving end, which
will receive the data on its serial-in line, will wait for the rising edge of the clock
and check which bit it currently receives on the serial in, and shift it into an 8 byte
register. This is important to remember: Each transfer is 8 bits - aka 1 byte long. Also
you might notice: We can send and receive at the same time: The counterpart of our transfer
can send us data while we are transferring data to it!
The default clock speed is roughly 8 khz, so we get roughly one kilobyte per second
transfer speed.
Now you might already recognize that basically the link cable protocol is SPI: the Serial
Peripheral Bus. And the Pico already comes with SPI examples, and all we have to do is
adjust the clock speed for the SPI PIO state machine, and then we are ready to talk to
the Game Boy! And that’s all this tiny piece of firmware here does: It initializes SPI
and USB serial, and then sends each byte that it receives on the USB serial interface out
via SPI, and sends back to the computer whatever was received. By the way, in our case the
Raspberry Pi will always be the initiator of a transfer.
Lets go to the computer side: First of, to mine bitcoin, we need to run
a bitcoin node. To initialize our node, it needs to download a couple of hundred gigabytes
of data first, so if you try to do this at home, I recommend that you start with this.
Next, we need a mining software. Now a lot of the mining examples you can find online
are outdated and don’t work with a modern bitcoin node, but after a while I found ntgbtminer
by Vanya Sergeev, a basic, python based CPU miner.
This miner makes it really easy to replace the main mining function with a bit of code
that communicates with our Game Boy. It basically sends the target value and the block data
to our game boy, and checks in regularly with it to see whether it was able to successfully
mine a block. It also also notify the game boy if someone else successfully mined a block
and provide it with the new block hash and target value.
Again, you can find the code for the miner linked in the description.
And now it was time for the big moment: It was time to start mining and finally earn
some well deserved bitcoins! So lets boot the Game Boy, and wait for the
mining ROM to load. Now lets start the miner on the computer, wait for it to transfer the
data, And yes, it’s working!
The game boy received all required data, and is now happily chugging along! Unexpectedly,
you can actually HEAR it working, there’s a certain signature coilwhine when it’s
running that’s hard to capture on camera!
On the screen you can see the nonce increasing, and the hash rate is pretty impressive: roughly
0.8 hashes per second! If you compare that to a modern ASIC miner, which comes in at
around 100 Terahashes per second, you can see that we are almost as fast, only off by
a factor of roughly 125 trillion. At this rate, it should only take us a couple of quadrillion
years to mine a bitcoin. And remember that the ASIC miners use a ton
of power, while our Game Boy simply runs on 4 AA batteries. Big success,
Now to see whether the Game Boy miner is really able to mine blocks, I’ve also set up a
new, empty bitcoin blockchain with a very low difficulty, and the Game Boy indeed is
able to successfully mine blocks for it! Pretty cool, blockchain on game boy!
Obviously, mining bitcoin on a game boy is everything but profitable, but I learned a
lot of things while building this, and definitely had a ton of fun!
By the way, if you want to experiment with the Game Boy hardware, you can now order the
Breakout and ROM cartridge that liveoverflow and me showed in our game boy series a while
ago, you can find the link in the description.
I hope you enjoyed this video, and to see you on this channel again soon! Thank you!