WEBVTT
Kind: captions
Language: en

00:00:13.920 --> 00:00:20.240
This is an emulator for the Nintendo Entertainment&nbsp;
System running entirely within Portal 2. Now,&nbsp;&nbsp;

00:00:20.240 --> 00:00:23.600
I’ll state the obvious, it’s barely&nbsp;
managing two frames per second,&nbsp;&nbsp;

00:00:23.600 --> 00:00:28.640
and at this quality and performance, most&nbsp;
sensible people would consider this unplayable.

00:00:28.640 --> 00:00:31.920
Nonetheless, what’s important&nbsp;
is that it is running,&nbsp;&nbsp;

00:00:31.920 --> 00:00:36.400
and it’s programmed entirely with the&nbsp;
scripting language of Portal 2. But,&nbsp;&nbsp;

00:00:36.400 --> 00:00:43.200
before we talk about what this is and how it&nbsp;
came to be, let’s quickly cover what this isn’t.

00:00:43.200 --> 00:00:47.280
If you frequent the Portal 2 workshop, you&nbsp;
may have come across chambers where people&nbsp;&nbsp;

00:00:47.280 --> 00:00:52.400
attempt to create a simple computer using&nbsp;
logic gates built out of lasers. Indeed,&nbsp;&nbsp;

00:00:52.400 --> 00:00:56.160
Portal 2’s testing elements are&nbsp;
Turing-complete, which means that,&nbsp;&nbsp;

00:00:56.160 --> 00:01:02.240
in theory, they can be used to compute anything.&nbsp;
But just like real computers are limited by&nbsp;&nbsp;

00:01:02.240 --> 00:01:07.760
their physical hardware, in reality you&nbsp;
can’t construct an NES with this alone.

00:01:07.760 --> 00:01:13.440
Put simply, the Source engine has a certain budget&nbsp;
for the maximum entity count. No, not a monetary&nbsp;&nbsp;

00:01:13.440 --> 00:01:18.640
budget, though- hold on, money might actually&nbsp;
be a decent analogy for this. Say we start with&nbsp;&nbsp;

00:01:18.640 --> 00:01:24.000
2048 dollars. This isn’t an arbitrary number&nbsp;
by the way, it’s the networked entity limit&nbsp;&nbsp;

00:01:24.000 --> 00:01:28.400
in most Source games. Exceeding this number&nbsp;
instantly crashes the game, so you can think&nbsp;&nbsp;

00:01:28.400 --> 00:01:33.280
of this as a finite budget for the construction&nbsp;
of each test chamber. Every element you place&nbsp;&nbsp;

00:01:33.280 --> 00:01:38.400
and every connection you make costs you some&nbsp;
of that budget. While a simple logic gate is&nbsp;&nbsp;

00:01:38.400 --> 00:01:44.080
perfectly reasonable, a system on the scale&nbsp;
of the NES quickly gets way too expensive.

00:01:44.080 --> 00:01:48.560
Even if we use “cheaper parts” so to speak,&nbsp;
by building our own testing elements,&nbsp;&nbsp;

00:01:48.560 --> 00:01:54.240
we’re still not even close to the right ballpark.&nbsp;
The cost of a single element cannot be a fraction,&nbsp;&nbsp;

00:01:54.240 --> 00:01:59.680
as remember – under the hood, this budget is&nbsp;
an entity count. That's a discrete indivisible&nbsp;&nbsp;

00:01:59.680 --> 00:02:05.520
natural number. In our analogy, this means that&nbsp;
the cheapest possible element costs 1 dollar.

00:02:05.520 --> 00:02:12.000
Anything we build has to consist of less than 2&nbsp;
thousand discrete parts. The NES has 8 kilobytes&nbsp;&nbsp;

00:02:12.000 --> 00:02:18.960
of program memory. That’s over 8’000 individual&nbsp;
bytes, or about 64’000 bits. So even just the&nbsp;&nbsp;

00:02:18.960 --> 00:02:25.280
memory alone takes up 32 times our budget, and&nbsp;
that’s before we even get to the processor.

00:02:25.280 --> 00:02:29.840
The 6502 was generally considered&nbsp;
to be a pretty well optimized CPU,&nbsp;&nbsp;

00:02:29.840 --> 00:02:34.320
and by today’s standards it’s considered&nbsp;
ancient, but even that thing has about&nbsp;&nbsp;

00:02:34.320 --> 00:02:39.200
3.5 thousand transistors. That's&nbsp;
already almost double our budget,&nbsp;&nbsp;

00:02:39.200 --> 00:02:44.880
but it gets worse once you realize that one&nbsp;
transistor requires several puzzle elements.

00:02:44.880 --> 00:02:48.880
The actual complexity of a chip isn’t&nbsp;
this cut-and-dry. You might be able&nbsp;&nbsp;

00:02:48.880 --> 00:02:53.600
to represent whole logic gates or circuits&nbsp;
with less Portal 2 elements than transistors,&nbsp;&nbsp;

00:02:53.600 --> 00:02:58.000
but unless you’re up for the task&nbsp;
of re-engineering the 6502 with no&nbsp;&nbsp;

00:02:58.000 --> 00:03:01.120
breathing room, that’s not&nbsp;
happening any time soon.

00:03:01.120 --> 00:03:07.280
Even if you somehow got a one-to-one copy&nbsp;
of the physical CPU running in Portal 2,&nbsp;&nbsp;

00:03:07.280 --> 00:03:13.440
your next immediate issue would be performance.&nbsp;
Portal 2 runs at 60 ticks per second, which means&nbsp;&nbsp;

00:03:13.440 --> 00:03:19.520
that you can only simulate 60 connections per&nbsp;
second. That’s like if electricity moved in&nbsp;&nbsp;

00:03:19.520 --> 00:03:26.000
slow-motion. We’re no longer talking in frames&nbsp;
per second, we’re talking CPU cycles per minute.

00:03:26.000 --> 00:03:30.880
So, okay, now that that’s out of the&nbsp;
way, how does this work? Well, luckily,&nbsp;&nbsp;

00:03:30.880 --> 00:03:35.760
Portal 2 has a very powerful built-in scripting&nbsp;
engine that lets you write code to automate&nbsp;&nbsp;

00:03:35.760 --> 00:03:40.400
many scripted events. In the past I’ve used&nbsp;
VScript to make whole entire gameplay mods,&nbsp;&nbsp;

00:03:40.400 --> 00:03:44.080
but this time we’re looking to port&nbsp;
an emulator to it. There are tons of&nbsp;&nbsp;

00:03:44.080 --> 00:03:48.480
great emulators out there, but we just&nbsp;
need one that’s really easy to port.

00:03:48.480 --> 00:03:52.640
So I chose… smolnes.

00:03:52.640 --> 00:03:56.880
Now, okay, okay- hear me out. Some of&nbsp;
you might call me an idiot for this,&nbsp;&nbsp;

00:03:56.880 --> 00:04:00.400
and others might see this as&nbsp;
even more impressive. Personally,&nbsp;&nbsp;

00:04:00.400 --> 00:04:05.120
I just think I shot myself in the foot, but&nbsp;
managed to hit a backflip while doing so.

00:04:05.120 --> 00:04:10.560
See, smolnes is an example of code golfing - it’s&nbsp;
an attempt to achieve the shortest possible source&nbsp;&nbsp;

00:04:10.560 --> 00:04:16.320
code while still having a functioning program. The&nbsp;
final minified version of smolnes is under 5000&nbsp;&nbsp;

00:04:16.320 --> 00:04:22.080
bytes of C code. Of course, I won’t be working&nbsp;
with this version - it’s completely unreadable.&nbsp;&nbsp;

00:04:22.080 --> 00:04:27.040
I’ll be working with the deobfuscated&nbsp;
code, which is… slightly better. Even so,&nbsp;&nbsp;

00:04:27.040 --> 00:04:31.920
various nasty hacks are employed to make&nbsp;
the code as small as physically possible.&nbsp;&nbsp;

00:04:31.920 --> 00:04:36.640
If you're not a programmer, this is kind of&nbsp;
like reading a poem written in emoji – you&nbsp;&nbsp;

00:04:36.640 --> 00:04:42.240
have to really sit down and process every&nbsp;
line to understand what it even means.

00:04:42.240 --> 00:04:47.360
This is why it might seem like a bad choice&nbsp;
for a port. The code isn’t easy to read,&nbsp;&nbsp;

00:04:47.360 --> 00:04:52.880
it compromises efficiency for a shorter&nbsp;
program, and I can see it being a pain to debug.

00:04:52.880 --> 00:04:58.640
That said, it being tiny does mean that&nbsp;
there’s just less code overall to port,&nbsp;&nbsp;

00:04:58.640 --> 00:05:02.800
and since I understand C better than&nbsp;
the NES, I’ll probably have an easier&nbsp;&nbsp;

00:05:02.800 --> 00:05:07.680
time porting a bit of gibberish than trying to&nbsp;
understand the code base of a larger emulator.

00:05:07.680 --> 00:05:12.240
In other words, if given the choice between&nbsp;
reading that emoji-poem and dissecting the&nbsp;&nbsp;

00:05:12.240 --> 00:05:16.800
author's life to extract the story that&nbsp;
way, unless you're a trained detective,&nbsp;&nbsp;

00:05:16.800 --> 00:05:20.720
you're probably much better off&nbsp;
just translating the damn poem.

00:05:20.720 --> 00:05:26.560
That now brings up a question - how different&nbsp;
is Squirrel, the Portal 2 scripting language,&nbsp;&nbsp;

00:05:26.560 --> 00:05:32.480
from C, the language that smolnes is written&nbsp;
in? Can we map emoji directly to words,&nbsp;&nbsp;

00:05:32.480 --> 00:05:36.080
or is there a unique structural difference?

00:05:36.080 --> 00:05:41.680
Smolnes uses the C preprocessor #define&nbsp;
directive for convenience and smaller code size,&nbsp;&nbsp;

00:05:41.680 --> 00:05:46.480
which Squirrel doesn't have. This basically&nbsp;
just tells the compiler to find-and-replace&nbsp;&nbsp;

00:05:46.480 --> 00:05:52.880
all instances of a given token with some other&nbsp;
tokens. So, in our Squirrel code, we can replicate&nbsp;&nbsp;

00:05:52.880 --> 00:05:58.080
this by just performing that find-and-replace&nbsp;
chore manually. That part is simple enough.

00:05:58.080 --> 00:06:04.320
Next, there's the data types. The NES is an&nbsp;
8-bit system housing an 8-bit processor, so of&nbsp;&nbsp;

00:06:04.320 --> 00:06:11.200
course the emulator uses 8-bit unsigned integers&nbsp;
extensively to imitate that behavior. But Squirrel&nbsp;&nbsp;

00:06:11.200 --> 00:06:17.760
is a much higher-level scripting language,&nbsp;
and its only integer type is 32 bits wide.

00:06:17.760 --> 00:06:22.240
If you're not super familiar with these concepts,&nbsp;
it might not be immediately obvious why this is&nbsp;&nbsp;

00:06:22.240 --> 00:06:27.680
a problem. After all, we literally have more&nbsp;
bits, how could that be a limitation? Well,&nbsp;&nbsp;

00:06:27.680 --> 00:06:33.680
it all has to do with what happens if you exceed&nbsp;
those 8 bits. On a true 8-bit system, the largest&nbsp;&nbsp;

00:06:33.680 --> 00:06:39.920
whole number you can physically represent is&nbsp;
255. If you try to add 1 to that, you literally&nbsp;&nbsp;

00:06:39.920 --> 00:06:44.880
run out of bits to flip, so the value "rolls&nbsp;
over" back to zero. On a more modern computer,&nbsp;&nbsp;

00:06:44.880 --> 00:06:51.200
we can easily represent values in the billions.&nbsp;
So if you add 1 to 255, instead of becoming 0,&nbsp;&nbsp;

00:06:51.200 --> 00:06:57.040
it'll just increment to 256. This is just one&nbsp;
example of how an 8-bit value differs from a&nbsp;&nbsp;

00:06:57.040 --> 00:07:03.600
32-bit value, but yes, at the end of the day, this&nbsp;
isn't a dealbreaker. If we just tell our code to&nbsp;&nbsp;

00:07:03.600 --> 00:07:09.040
ignore everything after the first 8 bits of&nbsp;
an integer, it starts to behave pretty much&nbsp;&nbsp;

00:07:09.040 --> 00:07:17.200
like an 8-bit value. Adding one to 255 will still&nbsp;
flip the 9th bit, making it 256, but notice that&nbsp;&nbsp;

00:07:17.200 --> 00:07:22.960
if we just mask out all the other bits and&nbsp;
leave only the first 8, the value is zero.

00:07:22.960 --> 00:07:26.800
It's a bit easier said than done, of course&nbsp;
– this isn't something you can just magically&nbsp;&nbsp;

00:07:26.800 --> 00:07:31.760
tell the program to do. You have to carefully&nbsp;
find every piece of code where a value could&nbsp;&nbsp;

00:07:31.760 --> 00:07:37.360
conceivably overflow past 8 bits, and wrap it&nbsp;
in a function that prevents it from doing so.

00:07:37.360 --> 00:07:44.560
Then come the pointers. To minimize code size,&nbsp;
smolnes uses various raw memory pointers that,&nbsp;&nbsp;

00:07:44.560 --> 00:07:48.720
well, point to a place in the computer's&nbsp;
memory, to make it easier to address&nbsp;&nbsp;

00:07:48.720 --> 00:07:52.720
specific array elements with a smaller&nbsp;
code footprint. This is a feature pretty&nbsp;&nbsp;

00:07:52.720 --> 00:07:57.360
much exclusive to lower-level languages like&nbsp;
C, and while it's obviously very powerful,&nbsp;&nbsp;

00:07:57.360 --> 00:08:01.680
its use is generally frowned upon in larger&nbsp;
systems due to how dangerous it can be to&nbsp;&nbsp;

00:08:01.680 --> 00:08:06.800
handle memory this way. As such, Squirrel&nbsp;
doesn't have any support for raw pointers,&nbsp;&nbsp;

00:08:06.800 --> 00:08:11.120
so that logic has to be rewritten to&nbsp;
use regular old array indexes instead.

00:08:11.120 --> 00:08:16.880
Lastly, smolnes employs an even nastier C&nbsp;
language feature called the "goto" statement.&nbsp;&nbsp;

00:08:16.880 --> 00:08:22.400
As the name implies, it lets you go to a&nbsp;
specific part of the code unconditionally,&nbsp;&nbsp;

00:08:22.400 --> 00:08:26.320
completely ignoring any proceeding&nbsp;
control flow. Letting the developer&nbsp;&nbsp;

00:08:26.320 --> 00:08:31.120
jump around the code like this is a&nbsp;
terrible idea for 99% of scenarios,&nbsp;&nbsp;

00:08:31.120 --> 00:08:36.480
which is why Squirrel (and most sensible&nbsp;
languages, really) obviously don't support it.

00:08:36.480 --> 00:08:40.720
To imitate that behavior in a more rational&nbsp;
way, I'll have to change up the control flow in&nbsp;&nbsp;

00:08:40.720 --> 00:08:46.560
some parts, and wrap a bunch of other code in if&nbsp;
statements that check whether goto is being used.

00:08:46.560 --> 00:08:50.400
Okay, so that's the big picture. There's&nbsp;
a lot that has to be changed already,&nbsp;&nbsp;

00:08:50.400 --> 00:08:55.680
and the odds of introducing a new bug double&nbsp;
with every next step. After monotonously skimming&nbsp;&nbsp;

00:08:55.680 --> 00:09:00.320
through the code to apply all of these changes,&nbsp;
I unsurprisingly ran into a few compiler errors,&nbsp;&nbsp;

00:09:00.320 --> 00:09:05.600
but after all of those got resolved, I was&nbsp;
genuinely shocked to see that the game didn't&nbsp;&nbsp;

00:09:05.600 --> 00:09:10.960
spontaneously explode or something. In fact,&nbsp;
it even claimed to be rendering frames! But&nbsp;&nbsp;

00:09:10.960 --> 00:09:15.520
taking a peek at the framebuffer, it quickly&nbsp;
became clear that all of those frames were&nbsp;&nbsp;

00:09:15.520 --> 00:09:20.160
pitch black. I traced through the logic of the&nbsp;
emulator a bit, and after a bit more debugging&nbsp;&nbsp;

00:09:20.160 --> 00:09:24.640
I found that the flags for the picture&nbsp;
processing unit never even got touched.

00:09:24.640 --> 00:09:29.360
To debug a port like this, your best bet is&nbsp;
to print some output from your program and&nbsp;&nbsp;

00:09:29.360 --> 00:09:33.440
compare it to the original. What you're&nbsp;
seeing here is a log of the bytes being&nbsp;&nbsp;

00:09:33.440 --> 00:09:37.520
read from the Super Mario Bros. ROM, and&nbsp;
the address that each one was read from.&nbsp;&nbsp;

00:09:37.520 --> 00:09:41.680
Comparing it side-by-side with the original&nbsp;
emulator like this helps me find exactly&nbsp;&nbsp;

00:09:41.680 --> 00:09:45.920
where the emulation takes a wrong step. In this&nbsp;
case, we can see that the address from which&nbsp;&nbsp;

00:09:45.920 --> 00:09:51.440
we're reading the opcode jumps to the wrong&nbsp;
value, and- oh, what's an opcode, you ask?

00:09:51.440 --> 00:09:57.040
Put simply, the CPU inside of the NES,&nbsp;
the legendary 6502, works kind of like&nbsp;&nbsp;

00:09:57.040 --> 00:10:01.600
a tape reader. It reads one byte at a time,&nbsp;
processes the instruction associated with it,&nbsp;&nbsp;

00:10:01.600 --> 00:10:06.080
then moves the program counter forward to either&nbsp;
get some value for the current instruction,&nbsp;&nbsp;

00:10:06.080 --> 00:10:10.320
or to get to the next instruction. Of&nbsp;
course, I'm definitely oversimplifying this,&nbsp;&nbsp;

00:10:10.320 --> 00:10:14.320
and I'm not exactly qualified to talk&nbsp;
about the intricacies of the 6502,&nbsp;&nbsp;

00:10:14.320 --> 00:10:17.920
but this should at least help you&nbsp;
understand what's going wrong:&nbsp;&nbsp;

00:10:17.920 --> 00:10:23.760
some instruction is telling the NES to change the&nbsp;
address that it's reading from, but our emulated&nbsp;&nbsp;

00:10:23.760 --> 00:10:29.840
CPU messes this up and lands on the wrong address,&nbsp;
which usually gets it stuck in some infinite loop.

00:10:29.840 --> 00:10:34.240
Now, there were several such bugs, so I'll&nbsp;
skip the details for brevity. Most of them&nbsp;&nbsp;

00:10:34.240 --> 00:10:39.200
came from, who would've guessed it, bugs I&nbsp;
introduced while porting from C to Squirrel.

00:10:39.200 --> 00:10:44.240
While resolving these, I ran into a bit of good&nbsp;
news and some bad news. The good news was that&nbsp;&nbsp;

00:10:44.240 --> 00:10:49.360
the emulator was getting further and further into&nbsp;
the Super Mario Bros. program before deviating,&nbsp;&nbsp;

00:10:49.360 --> 00:10:53.680
but the bad news is that the log was&nbsp;
now too long to look through manually.

00:10:53.680 --> 00:10:58.400
This is where diff-ing becomes useful. If you have&nbsp;
two text files that are supposed to be identical,&nbsp;&nbsp;

00:10:58.400 --> 00:11:02.160
like the logs of the same NES emulator&nbsp;
written in two different languages,&nbsp;&nbsp;

00:11:02.160 --> 00:11:06.320
you can use a tool such as the UNIX&nbsp;
"diff" to tell you exactly where the&nbsp;&nbsp;

00:11:06.320 --> 00:11:10.560
two logs start to desync. Even then,&nbsp;
the discrepancies aren't trivial to&nbsp;&nbsp;

00:11:10.560 --> 00:11:14.320
debug. Even if I know in roughly which&nbsp;
part of the code the issue is happening,&nbsp;&nbsp;

00:11:14.320 --> 00:11:19.920
and have the original side-by-side with my&nbsp;
port, it's still never immediately obvious.

00:11:19.920 --> 00:11:23.920
Eventually, I got to the point where all of&nbsp;
the instructions were being handled correctly,&nbsp;&nbsp;

00:11:23.920 --> 00:11:28.080
and I even got it to draw something to&nbsp;
the screen, but it's not exactly looking&nbsp;&nbsp;

00:11:28.080 --> 00:11:32.960
too much like Mario. The problem was that&nbsp;
the rendering of each frame was... offset&nbsp;&nbsp;

00:11:32.960 --> 00:11:39.120
by a few cycles. The picture processing unit&nbsp;
of the NES runs 3 times faster than the CPU,&nbsp;&nbsp;

00:11:39.120 --> 00:11:42.960
and as soon as it's done processing the&nbsp;
whole framebuffer, it updates some values&nbsp;&nbsp;

00:11:42.960 --> 00:11:48.160
that may be used in further calculations. If&nbsp;
the rendering routine isn't perfectly in-sync,&nbsp;&nbsp;

00:11:48.160 --> 00:11:52.560
the processor falls out of sync too. This&nbsp;
offset would then accumulate over time,&nbsp;&nbsp;

00:11:52.560 --> 00:11:57.920
eventually driving the emulator off-course. I&nbsp;
assumed that it must have been some silly typo,&nbsp;&nbsp;

00:11:57.920 --> 00:12:03.200
like a +3 to the cycle count instead of +2 by&nbsp;
sheer accident. But, either way, seeing as how&nbsp;&nbsp;

00:12:03.200 --> 00:12:10.880
the emulator worked fine otherwise, surely it&nbsp;
couldn't have been a fundamental flaw, right?

00:12:10.880 --> 00:12:17.200
When I wrote the functions that would mask all of&nbsp;
the 32-bit integers down to 8 or 16 bits, I had to&nbsp;&nbsp;

00:12:17.200 --> 00:12:21.920
skim through the code to add these functions&nbsp;
to each assignment operation. An assignment&nbsp;&nbsp;

00:12:21.920 --> 00:12:29.040
like this would become this, an addition like&nbsp;
this would become this, and most importantly,&nbsp;&nbsp;

00:12:29.040 --> 00:12:34.800
increment and decrement operations such as&nbsp;
these ones would end up looking like these.

00:12:34.800 --> 00:12:38.800
Here's some homework for those of you who write&nbsp;
code – what's the difference between these two&nbsp;&nbsp;

00:12:38.800 --> 00:12:44.240
operations? Got your answers ready? Okay. So,&nbsp;
unless you've only ever written Python, you&nbsp;&nbsp;

00:12:44.240 --> 00:12:49.680
might know that this is called the post-increment&nbsp;
operator. The one major difference that sets it&nbsp;&nbsp;

00:12:49.680 --> 00:12:55.760
apart from similar operations is that it only&nbsp;
increments the variable after returning it. So,&nbsp;&nbsp;

00:12:55.760 --> 00:13:02.640
say x is equal to 2 – after a post-increment,&nbsp;
its value will be 3, but here's the catch - if&nbsp;&nbsp;

00:13:02.640 --> 00:13:08.800
you try to use the value of "x++", it'll&nbsp;
still be 2. This is a subtle difference,&nbsp;&nbsp;

00:13:08.800 --> 00:13:14.240
and most experienced programmers will keep this in&nbsp;
mind while writing code. But here's the thing – I&nbsp;&nbsp;

00:13:14.240 --> 00:13:19.760
wasn't writing code, I was translating it.&nbsp;
So while mindlessly wrapping these values,&nbsp;&nbsp;

00:13:19.760 --> 00:13:25.040
I accidentally swapped all post-increment&nbsp;
operators with a regular increment. This meant&nbsp;&nbsp;

00:13:25.040 --> 00:13:29.440
that I would exit some loops one iteration&nbsp;
early, simply because the value would get&nbsp;&nbsp;

00:13:29.440 --> 00:13:36.480
checked after it was decremented. So that's how I&nbsp;
ended up desynced by a few PPU cycles each tick.

00:13:36.480 --> 00:13:40.480
I also ran into some similar bugs&nbsp;
related to operator precedence. See,&nbsp;&nbsp;

00:13:40.480 --> 00:13:44.240
Squirrel doesn't have shorthands for&nbsp;
bitwise assignments. In other words,&nbsp;&nbsp;

00:13:44.240 --> 00:13:50.320
I can't do "x &amp;= 8", for example. I have to&nbsp;
do "x = x &amp; 8", which in this case is still&nbsp;&nbsp;

00:13:50.320 --> 00:13:54.560
identical. But if the expression was&nbsp;
more complicated, just transforming&nbsp;&nbsp;

00:13:54.560 --> 00:14:01.760
it to this is no longer identical, as&nbsp;
"x &amp; y" bind tighter than "y ^ 12".

00:14:01.760 --> 00:14:06.000
Anyway, with all that out of the way,&nbsp;
the emulator starts rendering its first&nbsp;&nbsp;

00:14:06.000 --> 00:14:10.400
recognizable frame! Well, "recognizable"&nbsp;
is about as far as it goes currently.&nbsp;&nbsp;

00:14:10.400 --> 00:14:13.920
It might be hard to tell what's going on&nbsp;
from the low resolution in-game display,&nbsp;&nbsp;

00:14:13.920 --> 00:14:20.480
so here's a direct dump of the framebuffer. The&nbsp;
sprites are all there, but the colors are... off.&nbsp;&nbsp;

00:14:20.480 --> 00:14:27.280
Mario seems to be looking fine, but everything&nbsp;
else looks... green? Or maybe blue? And, actually,&nbsp;&nbsp;

00:14:27.280 --> 00:14:31.840
that'd be a pretty good guess at what's really&nbsp;
happening. Since Mario is fine, the issue is&nbsp;&nbsp;

00:14:31.840 --> 00:14:36.560
likely isolated to just the background sprites.&nbsp;
And since everything seems to be tinted green or&nbsp;&nbsp;

00:14:36.560 --> 00:14:42.000
blue, we can start to suspect that the background&nbsp;
is just missing the red color channel entirely.&nbsp;&nbsp;

00:14:42.000 --> 00:14:46.640
If we grab the hex color values for the title&nbsp;
box and compare them to the original emulator,&nbsp;&nbsp;

00:14:46.640 --> 00:14:54.240
we see that- nope, never mind. It's close, but&nbsp;
not quite. I really thought I was onto something.

00:14:54.240 --> 00:14:58.640
Once again, to debug this, I had both emulators&nbsp;
spit out a bunch of debug information,&nbsp;&nbsp;

00:14:58.640 --> 00:15:01.520
this time from the background&nbsp;
rendering procedure of the PPU,&nbsp;&nbsp;

00:15:01.520 --> 00:15:06.480
and diff-ed it all to isolate the issue. It&nbsp;
turns out, there was exactly one variable in&nbsp;&nbsp;

00:15:06.480 --> 00:15:11.040
the smolnes source code that wasn't&nbsp;
an 8- or 16-bit unsigned integer,&nbsp;&nbsp;

00:15:11.040 --> 00:15:18.000
but rather a good ol' classic 32-bit signed int. I&nbsp;
was unnecessarily masking it to 16 bits, and after&nbsp;&nbsp;

00:15:18.000 --> 00:15:24.640
changing that one line of code, I had finally&nbsp;
achieved full parity with the smolnes emulator.

00:15:24.640 --> 00:15:29.280
Alright, let's take a step back for a bit and look&nbsp;
at the Portal 2 side of things. Other than porting&nbsp;&nbsp;

00:15:29.280 --> 00:15:35.280
an emulator from C to Squirrel, what do you&nbsp;
have to do to make it actually show up in-game?

00:15:35.280 --> 00:15:39.680
The obvious first concern is the display.&nbsp;
With just Squirrel alone, we can't create&nbsp;&nbsp;

00:15:39.680 --> 00:15:43.840
a custom screen with our own framebuffer.&nbsp;
This is a scripting language after all,&nbsp;&nbsp;

00:15:43.840 --> 00:15:49.280
so I can only really use it to script the behavior&nbsp;
of in-game entities. In this case, I'm creating a&nbsp;&nbsp;

00:15:49.280 --> 00:15:54.240
large grid of self-illuminated cubes, and updating&nbsp;
their color every time that a new frame is ready.&nbsp;&nbsp;

00:15:54.240 --> 00:16:00.080
Because of the 2048 entity limit, I can't actually&nbsp;
represent the entire NES screen – that's why the&nbsp;&nbsp;

00:16:00.080 --> 00:16:05.920
resolution is so bad. I'm only able to render&nbsp;
every 7th pixel without crashing the game.

00:16:05.920 --> 00:16:10.000
But the display is only half of the&nbsp;
battle – running the emulator itself&nbsp;&nbsp;

00:16:10.000 --> 00:16:15.040
isn't as straightforward as you think. When&nbsp;
running a script, the whole engine pauses to&nbsp;&nbsp;

00:16:15.040 --> 00:16:19.600
wait for that script to finish executing.&nbsp;
If we just ran the emulator indefinitely,&nbsp;&nbsp;

00:16:19.600 --> 00:16:24.800
we'd lock up the game. Valve foresaw something&nbsp;
like this, which is why they implemented an&nbsp;&nbsp;

00:16:24.800 --> 00:16:29.280
exception which prevents any script query&nbsp;
from running longer than 30 milliseconds.

00:16:29.280 --> 00:16:34.320
Thanks Valve, that's great and all, but this&nbsp;
quickly becomes a pain to work around. Sure,&nbsp;&nbsp;

00:16:34.320 --> 00:16:38.880
either way, we'd have to segment the main program&nbsp;
loop across multiple game ticks. This way,&nbsp;&nbsp;

00:16:38.880 --> 00:16:44.000
we can emulate just a few CPU cycles of the&nbsp;
NES each tick, and return control to Portal 2&nbsp;&nbsp;

00:16:44.000 --> 00:16:50.240
for the rest of the frame. But unless we know how&nbsp;
long any given iteration will take ahead of time,&nbsp;&nbsp;

00:16:50.240 --> 00:16:55.280
we always run the risk of going over those&nbsp;
30 milliseconds, which abruptly halts the&nbsp;&nbsp;

00:16:55.280 --> 00:17:00.880
emulator and desyncs it. Too many iterations,&nbsp;
and the risk is too high. Too few iterations,&nbsp;&nbsp;

00:17:00.880 --> 00:17:05.440
and we're wasting potential emulation&nbsp;
time during less demanding cycles.

00:17:05.440 --> 00:17:11.120
The best way I've found to work around this is&nbsp;
to spin up a new script VM for each CPU cycle.&nbsp;&nbsp;

00:17:11.120 --> 00:17:15.520
Yes, this introduces a ton of overhead,&nbsp;
but the 30 millisecond limit only applies&nbsp;&nbsp;

00:17:15.520 --> 00:17:20.160
to individual queries. So really, I can&nbsp;
keep the game busy for however long I like,&nbsp;&nbsp;

00:17:20.160 --> 00:17:23.040
as long as I'm actively starting new script VMs.

00:17:23.040 --> 00:17:28.080
And with this comes our final technical&nbsp;
problem – the emulator currently runs&nbsp;&nbsp;

00:17:28.080 --> 00:17:34.320
at about 5 seconds per frame. That's a fifth&nbsp;
of one FPS. Sorry for the colors by the way,&nbsp;&nbsp;

00:17:34.320 --> 00:17:38.400
I recorded this demo before I found the fix&nbsp;
to that, and I didn't bother re-recording,&nbsp;&nbsp;

00:17:38.400 --> 00:17:43.440
because this little sped-up clip took me half&nbsp;
an hour to record, and it doesn't even look that&nbsp;&nbsp;

00:17:43.440 --> 00:17:49.440
good with the low resolution. Although... hold&nbsp;
on, what if we just skip processing the pixels&nbsp;&nbsp;

00:17:49.440 --> 00:17:55.360
that we're not displaying? Since the entity limit&nbsp;
only allows us to display every 7th pixel anyway,&nbsp;&nbsp;

00:17:55.360 --> 00:17:59.760
why waste the time calculating all of&nbsp;
the pixels that we don't get to see?

00:17:59.760 --> 00:18:05.440
With this optimization, we're now looking at&nbsp;
right about 1.3 FPS. If you're keeping track,&nbsp;&nbsp;

00:18:05.440 --> 00:18:11.760
that's over 6x faster than before! But since we're&nbsp;
brute-forcing way past that 30 millisecond script&nbsp;&nbsp;

00:18:11.760 --> 00:18:17.520
timeout, Portal 2 isn't feeling so good anymore.&nbsp;
The game that all of this is running on is itself&nbsp;&nbsp;

00:18:17.520 --> 00:18:23.200
running at barely two frames per second. The more&nbsp;
time we give to the emulator, the less time Portal&nbsp;&nbsp;

00:18:23.200 --> 00:18:30.640
2 has to render its frames. If only there was a&nbsp;
way to separate the game server from the client...

00:18:30.640 --> 00:18:32.800
Oh, right. Co-op exists.

00:18:32.800 --> 00:18:37.920
By hosting a Portal 2 co-op game on one&nbsp;
computer and connecting to it from another,&nbsp;&nbsp;

00:18:37.920 --> 00:18:41.280
we can have the host bear the cost of&nbsp;
running the emulator, while the other&nbsp;&nbsp;

00:18:41.280 --> 00:18:47.120
player can enjoy a smooth framerate alongside&nbsp;
an almost recognizable Super Mario Bros.

00:18:47.120 --> 00:18:51.280
But, at this point, I think I really&nbsp;
have played all of my cards. I tried&nbsp;&nbsp;

00:18:51.280 --> 00:18:54.800
building a standalone Squirrel runtime,&nbsp;
the same version that Portal 2 uses,&nbsp;&nbsp;

00:18:54.800 --> 00:18:59.840
to test how well the emulator would run without&nbsp;
any overhead, but even that clocks in at only&nbsp;&nbsp;

00:18:59.840 --> 00:19:05.200
about 3.3 frames per second on the title&nbsp;
screen. Squirrel just isn't comparable to&nbsp;&nbsp;

00:19:05.200 --> 00:19:10.000
C in terms of raw performance. Not even close.&nbsp;
It's a language made for lightweight scripting,&nbsp;&nbsp;

00:19:10.000 --> 00:19:13.360
and I don't think anyone ever imagined&nbsp;
it would be running an emulator.

00:19:13.360 --> 00:19:16.960
That said, I would've loved to find a&nbsp;
more practical application for this,&nbsp;&nbsp;

00:19:16.960 --> 00:19:22.320
but given the quality and performance of emulating&nbsp;
just the original Super Mario Bros., I don't think&nbsp;&nbsp;

00:19:22.320 --> 00:19:26.720
this is worth much more than a simple tech demo.&nbsp;
Feel free to prove me wrong in the comments.

00:19:26.720 --> 00:19:29.360
Anyway, if you subscribe, I might&nbsp;
tell you about the time I turned&nbsp;&nbsp;

00:19:29.360 --> 00:19:33.680
Portal 2 into a Minecraft client.&nbsp;
Otherwise, that's all from me today.

00:19:33.680 --> 00:19:35.840
See ya.

