WEBVTT
Kind: captions
Language: en-US

00:00:00.320 --> 00:00:06.880
Recently I got myself a M5PaperS3.
It's an ESP32-S3 dev kit&nbsp;&nbsp;

00:00:06.880 --> 00:00:14.800
shaped like a pocketable e-reader,
featuring a 4.7" 960x540 Eink touchscreen.&nbsp;

00:00:14.800 --> 00:00:18.400
What makes it special compared to&nbsp;
other small Eink devkits is that&nbsp;

00:00:18.400 --> 00:00:23.360
it has got a screen with raw&nbsp;
row/column driver interface&nbsp;

00:00:23.360 --> 00:00:27.760
also commonly referred to as parallel inteface
But that's a different parallel&nbsp;&nbsp;

00:00:27.760 --> 00:00:32.480
interface compared to LCD
This enabled me to drive this&nbsp;&nbsp;

00:00:32.480 --> 00:00:38.640
screen while bypassing the normal waveform method
This would in turn, making high refresh rate&nbsp;&nbsp;

00:00:38.640 --> 00:00:43.920
possible, up to 60fps
More on that later.&nbsp;

00:00:44.480 --> 00:00:48.320
That got me thinking, what kind&nbsp;
of fun stuff I cab do with it&nbsp;

00:00:48.320 --> 00:00:52.240
other than writing an e-reader application?
Several years ago,&nbsp;&nbsp;

00:00:52.240 --> 00:00:58.320
I made a GameBoy FPGA re-implementation
so it's fair to say that I have at least some&nbsp;&nbsp;

00:00:58.320 --> 00:01:03.120
understanding of the GameBoy architecture
How about combining both?&nbsp;

00:01:03.120 --> 00:01:07.920
I searched online, and found no-one have&nbsp;
ever attempted building such as thing.&nbsp;

00:01:07.920 --> 00:01:10.880
As a result
I am really tempted to create the&nbsp;&nbsp;

00:01:10.880 --> 00:01:18.080
first GameBoy emulator on the PaperS3
and make it actually playable!&nbsp;

00:01:18.080 --> 00:01:22.320
The first thing I started working on&nbsp;
is the high-refresh-rate Eink driver&nbsp;

00:01:22.320 --> 00:01:27.680
Because this is the component that decides&nbsp;
if this project would ever work or not&nbsp;

00:01:27.680 --> 00:01:33.120
Eink are generally perceived as slow:&nbsp;
low refresh rate and slow response&nbsp;

00:01:33.120 --> 00:01:38.000
However it's possible to improve&nbsp;
it drastically given the hardware.&nbsp;

00:01:38.000 --> 00:01:42.400
I have a video dedicated about how&nbsp;
to do 60Hz and low latency on Eink&nbsp;

00:01:42.400 --> 00:01:47.200
and how I turned it into an Eink monitor
You can view it here&nbsp;

00:01:47.200 --> 00:01:50.720
But basically, if you treat&nbsp;
each pixel individually&nbsp;

00:01:50.720 --> 00:01:56.240
doing pipelined updates, and pay the&nbsp;
cost to use an FPGA to run all these&nbsp;

00:01:56.240 --> 00:02:01.200
you can get really smooth image on Eink
Now I just need to see if&nbsp;&nbsp;

00:02:01.200 --> 00:02:06.480
I can do the same on ESP32
Luckily for this project I don't really&nbsp;&nbsp;

00:02:06.480 --> 00:02:13.040
need to process all pixels on the screen
GameBoy only has 160x144 pixels&nbsp;

00:02:13.040 --> 00:02:20.160
I need 3 pixels to represent 4 shades of grey
duplicating dithered image by 3 times I get 3X&nbsp;&nbsp;

00:02:20.160 --> 00:02:25.120
pixel perfect scaling + dithered image
This means I only need to&nbsp;&nbsp;

00:02:25.120 --> 00:02:34.800
process 160x3x144 pixels at 60Hz
a much lower target than 960x540 at 60Hz&nbsp;

00:02:34.800 --> 00:02:39.600
I am storing 4 bits states per pixel
It gets compared with the incoming&nbsp;&nbsp;

00:02:39.600 --> 00:02:45.920
buffer to decide what to send to the screen
The LCDIF + DMA is used to actually pumping&nbsp;&nbsp;

00:02:45.920 --> 00:02:49.280
out the pixels to the screen
so the core can calculate&nbsp;&nbsp;

00:02:49.280 --> 00:02:55.360
next batch of pixels to display
The whole thing runs on the 2nd core on the ESP32&nbsp;

00:02:55.360 --> 00:03:02.240
each iteration is set to run for around&nbsp;
1/60s, providing a 60Hz refresh rate&nbsp;

00:03:02.240 --> 00:03:09.440
Double buffering is also implemented, allowing&nbsp;
the emulator to render tear free image&nbsp;

00:03:09.440 --> 00:03:15.680
While I have implemented a GameBoy&nbsp;
emulator in the past, that was also on FPGA&nbsp;

00:03:15.680 --> 00:03:20.000
GB emulator on microcontroller&nbsp;
has been done many many times over&nbsp;

00:03:20.000 --> 00:03:25.520
so I decided to use existing emulator&nbsp;
instead of reinventing the wheel myself&nbsp;

00:03:25.520 --> 00:03:33.440
I tried 3 existing emulators as the core: the&nbsp;
Peanut GB, the Walnut CGB, and the CrankBoy&nbsp;

00:03:33.440 --> 00:03:37.040
Peanut GB seems to be the&nbsp;
most popular emulator option,&nbsp;&nbsp;

00:03:37.040 --> 00:03:43.520
with decent performance on microcontrollers
Walnut CGB is a fork of the Peanut GB&nbsp;

00:03:43.520 --> 00:03:49.440
It modified the emulator to do 16-bit&nbsp;
instruction fetch instead of 8-bit fetch&nbsp;

00:03:49.440 --> 00:03:54.560
potentially saving time on memory access
mostly saving time on the expensive&nbsp;&nbsp;

00:03:54.560 --> 00:03:59.760
emulated memory mapping path&nbsp;
by issuing less memory accesses&nbsp;

00:03:59.760 --> 00:04:05.840
CrankBoy is a fork of the Peanut&nbsp;
GB + frontend for Playdate project&nbsp;

00:04:05.840 --> 00:04:10.320
offering a complete optimized&nbsp;
GB emulator for Playdate&nbsp;

00:04:10.320 --> 00:04:16.160
They added many optimizations for the port
especially around the memory mapping path&nbsp;

00:04:16.160 --> 00:04:20.880
allowing much better performance&nbsp;
without the rather unusual dual-fetch&nbsp;&nbsp;

00:04:20.880 --> 00:04:27.280
architecture from the Walnut CGB
I tried them out on the ESP32-S3&nbsp;

00:04:27.280 --> 00:04:32.400
I tested the emulation speed in various titles
Here are the results:&nbsp;

00:04:32.400 --> 00:04:37.680
Performance measured as relative to GameBoy
1 meaning it's running at roughly the&nbsp;&nbsp;

00:04:37.680 --> 00:04:41.360
same speed as the gameboy
I used the most performant&nbsp;&nbsp;

00:04:41.360 --> 00:04:45.148
CrankBoy as the emulation core
With dynamic frameskip (which only&nbsp;&nbsp;

00:04:45.148 --> 00:04:45.920
does the emulation but not rendering to&nbsp;
the screen, saving a few ms to catch up)&nbsp;

00:04:45.920 --> 00:04:52.000
it can hit 100% speed in many&nbsp;
titles, and rendering at 30-60fps&nbsp;

00:04:52.000 --> 00:04:55.680
The actual frame rate depends&nbsp;
a lot on runtime situations&nbsp;

00:04:55.680 --> 00:04:59.840
With the current performance,&nbsp;
it's rather unrealistic to enable&nbsp;&nbsp;

00:04:59.840 --> 00:05:03.920
GameBoy Color support for now though
as the GameBoy Color would allow the&nbsp;&nbsp;

00:05:03.920 --> 00:05:08.960
CPU to run at double speed
We are not even close&nbsp;

00:05:08.960 --> 00:05:14.000
All three emulators uses minigb-apu&nbsp;
as the sound emulation engine&nbsp;

00:05:14.000 --> 00:05:18.080
The GameBoy integrates a 4-channel&nbsp;
programable sound generator&nbsp;

00:05:18.080 --> 00:05:23.360
2 channels of square wave, 1 channel&nbsp;
of sample, 1 channel of noise&nbsp;

00:05:23.360 --> 00:05:27.440
4 channels are then mixed together&nbsp;
to produce the sound output&nbsp;

00:05:27.440 --> 00:05:31.680
In a real GB, some part of the&nbsp;
mixing happens in the analog domain&nbsp;

00:05:31.680 --> 00:05:37.200
but generally for emulators it's easiest&nbsp;
to just do everything as PCM samples,&nbsp;&nbsp;

00:05:37.200 --> 00:05:42.560
at 32.768kHz sample rate
All modern sound cards&nbsp;&nbsp;

00:05:42.560 --> 00:05:47.440
playback PCM samples anyways
This is however not the case on the PaperS3&nbsp;

00:05:48.240 --> 00:05:55.200
The PaperS3 only has a buzzer, capable of&nbsp;
generating a single tone when driven by the PWM&nbsp;

00:05:55.200 --> 00:06:00.560
One well known technique is to use&nbsp;
the PWM DAC to output PCM samples&nbsp;

00:06:00.560 --> 00:06:07.680
Basically just send DC-biased PCM samples&nbsp;
right into the PWM module as the duty cycle&nbsp;

00:06:07.680 --> 00:06:14.560
However, on ESP32-S3, PWM module&nbsp;
couldn't be fed by the DMA&nbsp;

00:06:14.560 --> 00:06:18.240
It needs to be either polled, or&nbsp;
generating an interrupt at the&nbsp;&nbsp;

00:06:18.240 --> 00:06:24.720
sample rate to update the duty cycle register
Both would require me to reserve some CPU cycles&nbsp;&nbsp;

00:06:24.720 --> 00:06:28.800
just for outputting the sound
Instead I used the PDM output&nbsp;&nbsp;

00:06:28.800 --> 00:06:34.000
capability of the I2S peripheral
It was intended for connecting to&nbsp;&nbsp;

00:06:34.000 --> 00:06:38.400
DAC/ amplifiers accepting PDM,
but you could also just add a&nbsp;&nbsp;

00:06:38.400 --> 00:06:47.920
RC filter to it and itself becomes a DAC
I implemented this first, and it worked right away&nbsp;

00:07:00.880 --> 00:07:05.600
But the issue is..
it's just way too quiet&nbsp;

00:07:05.600 --> 00:07:12.560
I thought it might be because the modulation&nbsp;
frequency was too high, I also tried just 32kHz&nbsp;&nbsp;

00:07:12.560 --> 00:07:18.400
PWM, but it doesn't make any meaningful&nbsp;
difference, so I am sticking with the PDM&nbsp;

00:07:18.400 --> 00:07:25.120
But I felt like that was not enough
Yes it does have sound, but if it's not&nbsp;&nbsp;

00:07:25.120 --> 00:07:29.280
audible in many situations..
it's not that useful&nbsp;

00:07:29.280 --> 00:07:34.240
To make loud enough buzz, it pretty&nbsp;
much has to be a single tone,&nbsp;&nbsp;

00:07:34.240 --> 00:07:37.328
something it was actually designed to do

00:07:37.328 --> 00:07:41.085
So, how about actually just use a single tone?

00:07:41.085 --> 00:07:46.716
You see, on GameBoy's PSG, first 2&nbsp;
channels are straight square wave

00:07:46.814 --> 00:07:50.817
The sample channel could often be&nbsp;
approximated with a square wave

00:07:50.817 --> 00:08:00.338
While being a bit far fetched, I could&nbsp;
use square wave in place of noise Then it's just 4 channels of square waves

00:08:00.338 --> 00:08:10.400
While the buzzer can only do one tone, it's possible to switch between multiple sound channels&nbsp;rapidly to create a pseudo polyphony effect&nbsp;

00:08:10.400 --> 00:08:19.451
This is a well known technique on platforms&nbsp;that also only has a single buzzer, such as&nbsp;&nbsp;an IBM compatible PC without any soundcard

00:08:19.529 --> 00:08:20.925
Regarding the results,

00:08:26.767 --> 00:08:34.262
I think it worked surprising well and bad at the same time
The good part, it's loud and clear, 

00:08:34.262 --> 00:08:40.160
game music are still very recognizable,&nbsp;and it's overall interesting to listen to&nbsp;

00:08:40.160 --> 00:08:44.480
The bad part, well the GameBoy is&nbsp;
never supposed to be sound like that.&nbsp;

00:08:44.480 --> 00:08:49.600
To ensure all these don't eat into the&nbsp;
limited CPU resources for emulation,&nbsp;&nbsp;

00:08:49.600 --> 00:08:55.840
I put the entire PSG emulation on the 2nd core
Based on profiling on selected games,&nbsp;&nbsp;

00:08:55.840 --> 00:09:02.320
PSG emulation generally runs for up to 2ms&nbsp;per frame depending on the sound complexity.&nbsp;

00:09:03.040 --> 00:09:07.760
The input is rather straight-forward
The M5PaperS3 has a touch screen,&nbsp;&nbsp;

00:09:07.760 --> 00:09:12.800
so it's natural to just show a&nbsp;
touchscreen joypad on the screen.&nbsp;

00:09:12.800 --> 00:09:21.280
For serious gamers, I have also added&nbsp;
experimental Bluetooth LE joypad support&nbsp;

00:09:21.280 --> 00:09:27.840
Though it doesn't actually fully implement the HID&nbsp;
protocol, it only guesses about the report format&nbsp;

00:09:27.840 --> 00:09:33.520
As a result, the compatibility is quite poor
I think as a demo this is fine,&nbsp;&nbsp;

00:09:33.520 --> 00:09:37.468
but this definitely can see some improvements.

00:09:37.468 --> 00:09:41.553
I added another feature: quick save and quick load

00:09:41.670 --> 00:09:50.499
 This basically stores and loads the state of the&nbsp;entire emulated GameBoy, allowing quickly creating checkpoint and return to the checkpoint&nbsp;

00:09:50.596 --> 00:09:58.212
So the idea is that when next time you pick up the PaperS3,&nbsp;&nbsp;after shutting it down and put away

00:09:58.480 --> 00:10:03.280
you can just use quick load to&nbsp;get back to the game immediately.&nbsp;

00:10:03.280 --> 00:10:11.200
In conclusion, I think the end result is a pretty&nbsp;decent/ usable GameBoy emulator for the M5PaperS3&nbsp;

00:10:11.200 --> 00:10:18.000
It's sad that PaperS3 has already reached EOL,&nbsp;hopefully we get a proper replacement for it&nbsp;&nbsp;

00:10:18.000 --> 00:10:23.760
soon! If you want to try this firmware&nbsp;out, it's available on the M5Burner&nbsp;

00:10:23.760 --> 00:10:26.640
Thank you for watching, and&nbsp;
I will see you next time

