►
Description
The magical thing about Rust is its ability to remove the fear of learning new things. My journey with Rust has led me to the world of hardware emulators - specifically old gaming machines from the 80s and 90s. Join me as we explore what it takes to implement a fully working Game Boy emulator. We'll take a look at the ins and outs of this 30 year old gaming wonder, learning a lot along the way about how computers and Rust work! By the end we'll have our emulator running in a web browser, a window on our Desktop and even a mobile phone!
https://rome.rustfest.eu/sessions/gameboy-emulator
https://media.ccc.de/v/rustfest-rome-3-gameboy-emulator
A
A
Cool,
it's
back
alright,
so
the
first
thing
we
have
to
talk
about
is
what
is
emulation.
Hopefully
everybody
is
familiar
with
this
thing.
This
is
the
original
Gameboy
from
like
1989,
and
the
goal
of
emulation
is
to
basically
create
this
piece
of
hardware
in
software,
but
before
we
get
into
the
details
of
how
to
do
that,
I'd
like
to
talk
a
little
bit
about
my
story,
so
I'm
a
web
programmer
originally
mostly
working
on
the
backend
I've,
also
done
a
lot
of
front-end
development.
I
studied
marketing,
so
I
don't
have
a
computer
science
background.
A
So
basically,
what
I
want
to
tell
you
with
all
of
this
is
I'm,
not
any
kind
of
genius.
It's
when
I
first
started
working
on
emulation,
I
thought
this
is
probably
a
very
hard
problem.
It's
not
really
so
I
encourage
all
of
you
to
take
a
look
at
it
because
it's
a
lot
of
fun
and
at
the
end
you
get
to
play
games
which
is
pretty
cool.
A
So
the
first
thing
that,
when
I
started
on
my
journey,
was
kind
of
looking
for
what
can
I
emulate
I
want
to
create
an
emulator,
because
it
sounds
like
a
cool
project.
What
should
I
start
with
when
I
thought?
Maybe
I
start
with
a
Nintendo
64,
because
I
played
that
as
a
kid
or
a
Playstation?
Well,
those
were
kind
of
hard.
A
So,
let's
start
a
little
bit
simple
and
I
started
with
the
chip
8,
and
if
anybody
is
not
familiar
with
the
chip
8,
it's
a
computer
from
the
1970s,
very
simple
kind
of
looked
like
this.
When
you
were
playing
with
it,
can
play
things
like
pong
and
breakout
and
stuff
like
that
and
two
very
simple
architectures.
So
if
you
want
to
start
with
something
and
the
game
boy
seems
like
a
little
bit,
a
big
leap
for
you,
I
would
suggest,
starting
with
the
chip8
first.
A
So
now
that
we
have
that
covered,
what
do
we
actually
have
to
do
to
emulate
the
Gameboy?
And
there
are
a
couple
of
major
parts
to
emulation.
For
instance,
we
have
the
CPU,
which
is
kind
of
like
the
brains
of
the
of
the
computer.
We
have
memory,
we
have
graphics
and
we
have
a
whole
bunch
more.
That
I
won't
even
touch
on
like
sound
input
stuff
like
that,
and
unfortunately,
this
is
only
30
minutes,
long
and
I'm,
not
that
good.
So
so
this
is
going
to
be.
Our
goal
for
today
is
to
get
this
working.
A
A
So,
let's
start
kind
of
at
the
top
of
the
stack,
the
thing
that
you
as
the
player
get
to
see
will
start
with
graphics,
and
the
first
thing
that
we
have
to
talk
about
is
the
tiling
system.
So
when
you
look
at
the
background
of
the
game,
what
you'll
actually
be
looking
at
is
a
grouping
of
tiles,
so
the
game
boy
does
not
manipulate
pixel
by
pixel,
but
rather
creates
these
8
by
8
pixel
tiles,
and
then
you
arrange
those
8
by
8,
pixel
tiles
on
the
screen,
and
so
here
on
the
Left.
A
We
can
see
the
tiles
set,
which
is
kind
of
your,
your
8
by
8
tiles
that
you
can
use
as
building
blocks,
to
build
your
graphics
and
then
on.
The
right
is
the
background
and
you
basically
just
map
tile
to
some
place
on
the
background.
So
let's
take
a
little
bit
closer
and
look
at
that
we'll
kind
of
zoom
in
on
one
part
of
the
tile
set
and
some
corresponding
pieces
interesting
pieces
of
the
background.
A
The
first
tile
in
this
tile
set
happens
to
be
all
white
pixels,
then,
after
that
it's
the
kind
of
top
left
of
the
end
and
so
on,
and
then
what
we're
going
to
do
is,
in
our
background,
we're
going
to
map
from
a
tile
to
a
place
in
the
background,
so
the
first
square
there
is
going
to
be
tile
one
to
the
very
right.
You
see
the
little
like
are
on
the
top
there.
That's
tile
25
below
that
is
tile
zero,
so
we're
mapping
from
tiles
and
art
and
our
tile
set
to
the
actual
background.
A
So
if
you
take
a
look
at
something
like
this,
this
is
Tetris
right
here
and
a
very
poorly
played
game
of
Tetris.
That's
my
fault,
sorry
about
that.
You
can
see
we
have
a
tile
set,
which
is
much
more
complicated
than
intend
to
logo,
one
that
has
all
kinds
of
things
in
it,
and
the
background
is
just
kind
of
composed
of
all
of
those
tiles,
and
we
just
need
to
know
this
and
be
able
to
emulate
this.
It's
not
our
job.
A
To
actually
do
the
composition,
that's
the
programmers
job
and
you
know
we're
not
the
game
programmer,
we're
just
allowing
the
game
to
actually
do
that.
You
might
be
asking,
though,
wait
a
second.
When
we
look
at
the
tetris
thing,
we've
got
all
this
white
space
next
to
the
actual
game.
What's
going
on
with
that,
and
the
answer
is:
is
that
the
background
is
actually
much
larger
than
the
display
sri
and
on
the
device
itself.
A
So
what
we
have
to
do
is
also
emulate
a
viewport
on
top
of
that
window,
and
you
can
look
what
that
looks
like
when
you're
actually
scrolling
the
Nintendo
logo.
So
the
I
was
amazed
when
I
first
saw
this.
It
turns
out
that
the
Nintendo
logo
isn't
actually
scrolling
down,
but
the
viewport
is
scrolling
up,
which.
A
So,
let's,
let's
dive
into
a
little
bit
of
code
and
again
this
is
gonna,
be
high
level,
because
unfortunately
we
only
have
30
minutes,
but
it
should
give
you
a
rough
idea
of
how
you
could
model
this
and
rust.
So
here
we
have
a
GPU
struct
and
we're
gonna
have
something
like
a
tile
set
and
it's
going
to
be
again.
384
tiles
and
these
tiles
are
just
8
by
8
pixels.
A
So
it's
kind
of
you
can
think
of
it
as
a
you
know,
a
an
array,
for
instance,
and
then
we
have
video
RAM,
and
that
is
that
includes
things
like
our
background.
I'm
gonna
also
include
things
like
sprites
and
stuff
like
that,
but
for
now
it's
just
the
background
and
then
the
last
thing
is
our
canvas
buffer
and
what
that
is
going
to
be
is
just
what
we're
going
to
take
from
the
tile
set
and
the
video
RAM
and
compose
them
together
to
actually
display
it
onto
the
screen.
A
So
the
tile
set
is
something
that
the
game
codes,
that
your
your
emulator
is
running.
Woman,
if
you
like
the
video
Ram,
is
also
something
the
game
code
will
manipulate,
but
the
canvas
buffer
is
something
that
we,
in
the
background,
are
manipulating,
so
that
we
display
something
actually
to
the
user
of
our
emulator.
A
So
now
that
we
have
kind
of
a
rough
idea
of
how
you
might
model
graphics
and
only
a
high-level
one,
let's
talk
a
little
bit
about
memory.
So
it's
important
to
remember
that
memory
is
just
one
very,
very
long
array
and
it's
full
of
bytes.
That's
all
it
is
so
if
you're
thinking
about
how
this
is
very,
very
complicated.
A
It's
not
it's
really
just
an
array
and
it
has
unsigned
8-bit
integers
in
it
and
that's
it,
and
it's
really
the
clever
use
of
game
code,
people
much
smarter
than
myself
in
the
80s
and
the
90s
who
created
these
games
to
manipulate
that
long
array
of
bytes
that
are
actually
doing
the
cool
stuff
that
you
see
on
the
screen.
So,
let's
take
a
little
bit
closer
of
a
look
at
the
memory
map
that
we
have.
What
is
this
array
of
bytes
actually
contain?
A
Well,
the
first
half
of
it
is
ROM
code,
that's
kind
of
the
code
that
gets
run
automatically
by
the
game
boy,
it's
a
boot
up.
It
also
includes
your
game
cartridge
code.
So
when
you
stick
a
cartridge
into
the
game
boy
that
move
the
game
boy
automatically
moves
the
game
cartridge
code
into
this
first
half
of
your
long
ray,
and
we
also
have
video
RAM
here
and
you
can
think
of
we're
kind
of
already
emulating
and
that
inside
of
our
with
our
GPU
struct.
So
that's
taken
care
of
there.
A
A
So
you
can
imagine
that
we
would
have
a
struct
called
memory
bus
and
that
memory
bus
contains
that
super
long
array
and
it's
actually
again
just
an
array
of
unsigned
8-bit
integers,
that's
0
fffff
long
and
that's
how
much
memory
the
Gameboy
had
it's,
actually
not
how
much
memory
the
Gameboy
had
is
how
much
the
Gameboy
could
address
in
memory.
So
an
address
space
of
16
bits.
A
If
it's
this
part
of
memory
go
to
that
subsystem
and
and
that's
how
it
works.
And
lastly,
what
we're
going
to
have,
then
is
the
CPU,
so
the
kind
of
overarching
brains
of
the
computer
and
that
holds
on
to
the
bus.
So
the
CPU
can
talked
through
the
bus
to
the
GPU,
but
the
CPU
has
one
more
thing
along
with
it.
A
It
has
something
called
a
program
counter
and
what
the
program
counter
does
is
it's
basically
just
a
number
that
indicates
where
in
memory
you're
currently
executing,
so
we
can
take
a
look
at
the
program
counter
here.
Currently,
our
program
counter
is
set
to
0.
If
we
go
ahead
and
execute
that
instruction
at
0
and
move
forward,
it
will
jump
to
instruction
at
address
2.
Then
it
jumps
to
address
4.
A
Maybe
it
then
you
know
that
instruction
is
a
jump
and
it
jumps
over
to
address
9
and
maybe
then
it
jumps
to
address
19
and
that's
all
you're
doing
is
keeping
track
of
what
is
the
currently
execute
an
instruction
and
we'll
look
at
what
executing
instructions
actually
looks
like
in
a
second?
So
now
we
know.
Ok,
our
struct,
our
CPU
struct
is
pretty
simple.
It's
got
a
program
counter
and
got
a
memory
bus
and
that's
about
it.
A
So
that's
great.
Let's
take
a
little
bit
closer.
Look
at
the
CPU
itself,
and
the
first
thing
that
we
need
to
look
at
is
instruction,
so
we
have
our
program
counter
we're
reading
a
byte
and
from
memory
of
an
instruction
that
we
want
to
execute.
But
what
we
probably
want
to
do
is
interpret
that
by
as
an
actual
instruction.
A
So
we
have
this
in
this
function
called
from
byte
on
our
on
an
instruction
struct
or
an
instruction
enum,
and
what
from
guide
does
is
take
that
the
unsigned
8-bit,
integer
and
decodes
it
as
an
instruction
so,
for
instance,
0
X
0,
2
decodes
as
an
increment
on
the
BC
target.
So
that's
just
what
what
it
is?
It's
just
an
instruction
we'll
look
at
it
specifically
some
instructions
that
we
have
so
that
now
that
we
have
instructions,
what
we
need
to
do
is
go
ahead
and
execute
them.
A
So
we're
going
to
look
at
this
step
function
on
the
CPU
that
goes
ahead
and
execute
instructions
and
the
way
that
an
emulator
works.
The
way
that
a
CPU
works.
Is
it
basically
just
loops
infinitely
and
call
step
executing
one
instruction
at
a
time
going
to
the
next
and
going
going
going
going
going
forever
and
ever
and
ever
unless
the
program
calls
halt
or
the
Gameboy
explodes
or
whatever
so
the
first
thing
and
our
step
instruction
is
just
it's
going
to
our
step
function.
Is
it's
going
to
take
beautiful,
beautiful
reference
itself?
A
A
Okay.
So
now
that
we
understand
how
how
things
get
executed,
let's
talk
a
little
bit
about
registers
and
what
registers
are
are
the
kind
of
smallest
pieces
of
memory
that
we
have
inside
of
of
the
computer
they're
little
tiny,
unsigned,
8-bit
integers.
That's
it
right
next
right
on
the
CPU
and
that's
what
the
CPU
actually
ends
up
manipulating.
So
the
CPU
can
fetch
things
so
memory
I
can
do
something
to
the
contents
of
a
register,
and
then
it
can
write
back
out
to
memory
and
really
that's
about
all
it
can
do
so.
A
Our
CPU
instruction
execution
is
actually
pretty
simple.
It's
going
to
be
just
some
simple
math
operations,
and
so
these
things
might
look
like
this
and
we
might.
We
might
go
ahead
and
model
it
like
this.
This
is
pretty
straightforward.
We
have
a
register
struct,
it's
got
an
unsigned,
8-bit
integer
for
each
of
our
our
registers
that
happen
to
be
called
ABCDE,
F,
H
and
L.
That's
just
the
way
that
they
were
called.
It's
not
because
I
can't
name
things,
and
so
our
CPU
looks
a
little
bit
different.
Now
right.
A
It
just
has
registers
on
it
and,
let's
take
a
look
specifically
at
some
instructions
that
might
we
might
want
to
execute
so
we're
going
to
look
at
one
instruction.
It's
the
add
instruction,
it's
pretty
straightforward
and
it
has
a
couple
of
targets
and
what
the
add
instruction
will
do
is
it
looks
at
the
target.
So
if
the
target,
let's
say,
is
D
register
D,
it
will
take
the
contents
of
register
D.
A
It
will
add
it
to
the
contents
of
register
a
and
then
it
will
store
it
back
into
register
a
and
so
no
matter
what
the
target
is
it
just
takes.
It
puts
it
adds
it
with
the
register
a
and
then
writes
it
back
into
register
a
and,
if
you're
thinking
that
sounds
pretty
simple.
It
is
it's
like
one
line
of
code,
so
what
we
have
to
do
now
is
actually
execute
our
instruction.
A
So
what
we're
going
to
look
at
here
is
execute
on
the
CPU
and
it's
going
to
take
a
mutable
reference
itself
and
an
instruction
that's
going
to
actually
execute
the
instruction,
and
so
first
it
needs
to
figure
out
what
instruction
it's
dealing
with
right.
So
we
match
on
the
instruction
and
in
this
we're
gonna
if
it
happens
to
be
an
add
instruction
and
the
target
is
the
C
register.
What
we're
going
to
do,
then,
is
take
the
value
and
I
just
notice.
Now
we
don't
have
the
value
we
get
the
value
from
the
C
register.
A
We
write
it,
we
wrap
wrapping,
add
to
register
a
and
what
wrapping
ad
does.
Is
it
just
make
sure
that
the
if
it
goes
above
255,
which
is
the
maximum
that
unsigned
8-bit
integer,
can
represent
it
just
wraps
around
to
0
again
and
goes
from
there
and
we're
going
to
then
write
that
back
into
register
a
pretty
simple,
and
so
we
have
a
whole
bunch
of
instructions.
A
Here,
we've
got
add
sub,
bitwise
and
bitwise
or
but
what
is
X
or
maybe,
if
you're
familiar
with
bitwise
and
struct
like
bitwise
operations,
you
know
what
these
are,
if
not
they're,
pretty
easy
to
learn.
You
have
Inc,
which
adds
1
to
something
deck
which
takes
one
away
and
some
more-
and
this
is
by
no
means
exhaustive.
So
when
you're
writing
an
emulator,
this
is
probably
the
most
boring
part
of
the
entire
thing,
because
you're
just
going
to
be
doing
this
for
like
200
instructions,
but
it
gets
much
more
fun
after
that.
A
So
the
last
thing
we're
gonna
do
today
is
make
it
run,
and
in
order
to
do
that,
I'm
gonna
use
a
crate
called
Russ
mini
FB,
and
what
this
allows
you
to
do
is
it
gives
you
a
window,
and
it
gives
you
something
called
a
frame
buffer,
which
again
is
just
a
big
array
of
bytes
that
you
can
write
to
and
it
will
display
those
bytes
on
the
screen.
So
you
can
write
to
it.
If
you
write,
you
know
12
to
the
first
byte
there
or
the
first
3
bytes.
A
It
displays
that
pixel
to
the
screen,
and
so
it
allows
us
to
easily
get
up
and
running
and
write
something
write
an
actual
window.
So
this
is
a
big
chunk
of
code
that
we've
got
to
get
working.
The
first
line
here
is
it
takes
a
CPU
and
it
takes
a
window
and
we're
gonna
get
some
stuff
set
up
here.
So
we
have
our
window
buffer.
That
we're
going
to
write
to
we
have
some
housekeeping
for
how
many
cycles
of
the
CPU
how
many
times
we've
run
the
the
run
function
and
we
have
some
timing
stuff.
A
How
much
time
has
passed
since
we
last
we're
here
and
then
we
kind
of
right
now
back
into
the
now
function,
so
we've
just
trying
to
keep
track
of
how
much
time
has
passed
since
the
last
time,
then
we
go
ahead
and
call
run
on
the
CPU
when
we
pass
in
this
time.
Delta
and
the
CPU
just
knows
how
many
instructions,
how
many
times
to
call
step
for
that
amount
of
time
and
the
game
boy
has
a
very
well
specified
amount
of
time
that
it's
allowed
to
run
and
we
get
back.
A
How
many
cycles
have
actually
passed?
How
many
cycles
have
been
run
and
we
add
that
to
our
cycles,
elapsed
and
frame
and
what
we
do
there
is
we
just
check?
Okay,
have
we
run
more
cycles,
then,
should
happen
and
one
frame
and
by
frame
I
mean
showing
something
to
the
user,
and
that
happens
30
seconds
30
times
per
second,
and
if
we,
if
we
have,
then
we
go
ahead
and
copy
our
pixel
buffer
from
our
GPU
that
we
had
and
we
put
it
into
our
window
buffer
and
then
we
write
it
to
our
window.
A
And
then
we
reset
the
cycles,
elapse
and
frame,
and
this
will
just
display
the
thing
to
the
user,
pretty
straightforward,
and
otherwise
we
sleep
for
a
little
while,
because
rust
is
so
fast
and
your
computer
is
so
much
faster
than
then
a
gameboy
was
that
if
you
don't
do
that,
it
will
just
explode
because
it
can't
handle
it.
And
if
we
do
that,
we
did
it.
We
have
accomplished
something
so
give
ourselves
a
round
of
applause.
A
So
we
should
all
feel
pretty
proud
of
ourselves
today,
but
admittedly,
I
might
have
skipped
over
a
couple
of
things.
It
wasn't
exactly
an
exhaustive
review
of
how
to
do
this,
so
you
might
be
wondering
okay,
I'm
going
to
go
home
and
I
want
to
try
this
out
for
myself.
What
are
some
resources
that
I
can
do
to
go
ahead
and
get
better
at
this?
One
of
the
first
ones
here
is
a
talk
by
Mickey
eyelet,
which
is
a
talk
called
the
ultimate
game,
boy
talk
and
it
really
goes
into
super
technical
depth
very
fast.
A
Much
faster
than
I
talked
about
the
game
boy,
and
so,
if
you
want
to
just
get
a
whirlwind
tour
of
the
technical
aspects
of
a
game
boy
go
for
this.
The
next
one
is
something
called
the
pan
docks.
These
are
really
thorough
technical
documentation
on
on
the
game
boy,
and
it's
really
great.
If
you
have
a
question
more
than
likely,
if
you
can't
find
it
here,
you
probably
won't
find
it
unless
you'll
find
a
random
blog
post
somewhere
online.
So
this
is
a
really
good.
A
First,
stop
the
next
one
is
what
how
I
got
started
was
reading
this
blog
post
series
called
game
boy
emulation
in
JavaScript
and
its
really
great
it's
not
in
rust.
Obviously,
but
if
you're
familiar
with
JavaScript,
it's
good
to
kind
of
read
through
this
and
get
an
idea
of
how
you
might
model
it
in
JavaScript
at
least
then,
if
you're
looking
for
something
more
rusty,
we
have
a
repo
here
that
implements
a
game
boy
in
rust,
and
the
great
thing
about
this
is
that
it's
meant
to
be
extremely
accurate.
A
So
if
you
have
any
questions
about
how
you
should
be
emulating
a
game
boy,
you
should
look
here
because
the
the
awesome
thing
about
this
project
is,
they
have
literally
taken
a
physical
game.
Boy
created
test
drums
for
it
stuck
them
into
the
game.
Boy
tried
it
out
doing
weird
things
that
no
one
would
ever
consider
to
do
in
an
actual
game
and
see
what
happens,
and
then
they
emulate
that
inside
of
it.
A
A
So
really
what
it
comes
down
to
is.
This
was
all
kind
of
a
pain
for
me.
There's
a
lot
of
I
kind
of
gave
you
the
shortcut
on
resources
here
of
where
to
go.
If
you,
if
you
need
to
find
things,
but
it
took
me
a
long
time
to
find
those
sites,
those
are
by
no
means
everything
that
you
will
need
to
implement
this,
but
just
because
it
was
a
pain
for
me
doesn't
mean
it
has
to
be
a
pain
for
you.
What's
exciting
about
emulating
a
Game
Boy
is
not
trying
to
find
documentation
online.
A
Is
that
you
can
compile
it
to
web
assembly,
and
so
I
did
that
and
I
built
myself
a
nice
little
Gameboy
and
thing
here.
It's
got
some
really
great
debugging
tools
and
there's
more
of
this
to
come
as
well.
So
if
you're
interested,
if
this
looks
really
cool
to
you
and
you
want
to
help
out
to
make
something
very
accessible
so
that
other
people
can
build
game
boys
and
have
fun
with
them,
then
I
say
let's
learn
together.
B
A
I
do
so
it's
github.com,
/r,
I'll,
EV,
ry
le
v,
/
dmg,
0,
1,
dmg,
0
1
was
the
code
name
for
the
game
boy.
So,
if
you
forget
it
just
go
to
the
Wikipedia
page
and
look
up
what
the
code
name
for
the
game.
Boy
was,
while
they
were
developing
it,
you
you'll
be
able
to
remember
it,
but
yeah
I
have
the
code
there.
The
emulator
is.
This
is
actually
my
second
attempt
at
writing.
The
emulator
the
first
attempt
I
did
was
in
typescript
and
that
one
is
a
bit
further
along.
A
So
the
one
currently
can
only
run
test
roms.
It
can't
yet
run
tetris,
but
I'm
like
very
close
to
being
able
to
do
that
and
since
I've
already
done
it,
if
you
want
to
do
it
yourself
and
need
my
help,
I
would
be
more
than
happy
to
take
you
through
and
we
can
get
it
running
tetris,
which
is
quite
a
lot
of
fun.
Thank.
B
A
Have
not
tried
to
optimize
it,
but
I
have
I
made
before
when
I
first
ran,
I
wrote
that
run
code,
I
didn't
have
the
sleep
in
there
and
it
it
goes
really
fast.
So
you
will
see
things
like
the
the
screen
just
kind
of
going
and
then
and
then
eventually
it
just
kind
of
gets
out
of
sync
and
stuff
like
that
and
yeah
you
need
to.
You
need
to
slow
it
down
for
it
to
be
usable
in
any
way.