I Made a 32-bit Computer Inside Terraria

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
This is the single greatest achievement of my  life to date. As Gutenberg with his press or   Eiffel with his Tower I have brought forth  my genius upon the world. But you shouldn't   be impressed because I spent 600 hours and  five months making a Pong clone. You should   be impressed because it's running on a computer  that is itself running inside of the game Terraria [Classical music] First off, some background. Terraria is a sandbox  adventure game where you explore the world and   fight bosses to progress. You can basically think  of it as a 2D Minecraft clone, although for some   reason people get very mad on Reddit if you ever  say so. Inside the game there's a mechanic called   wiring, which lets you trigger certain tiles such  as opening doors or activating traps. When I say   I've created a computer inside Terraria what I  mean is that I've completely simulated the inner   workings of a regular computer, except instead of  electronics I did it inside of a video game world.   But let's start at the beginning. Around five  months ago I was playing Terraria for... a   few hours and I was getting a bit bored, so I  decided to try out the wiring system on a whim.   At the beginning I knew pretty much nothing about  it, but I quickly realized it's super powerful   and you can do some really awesome things with  it. Here's a simple example. Whenever you flip   the switch a signal is passed through the  red wire that opens and closes the door.   It's the same for turning on and off the candle.  Each different color of wire acts independently,   so if we cross the red and blue  wire they just ignore each other. The most basic building block of how computers  are implemented is the logic gate. These are tiny   individual units that take in several inputs and  emit an output based on some very simple logic.   Computers are made by extensive combinations of  these gates. Terraria has some traditional logic   gates such as AND and OR, but for complicated and  frustrating timing reasons these are ridiculously   hard to work with. Instead, for my computer I  exclusively use a different type of gate that   looks like this. There are three parts in each of  these gates: the input, the state, and the output.   Whenever a switch is used to trigger the  input, if the state is on then the output   emits a signal. Otherwise, nothing happens.  For instance, you can see that the door only   opens and closes if the state wire is on. In real  world circuits when a circuit is on it stays on,   but in Terraria everything is just sent in  pulses. This means it behaves completely   differently and a lot of traditional circuits  have to be redesigned in this new paradigm. You might think that we're just getting started  but that's it. The entirety of the in-game CPU   exclusively uses these gates in various different  convoluted configurations and absolutely nothing   else. The complexity comes from the different  ways that these logic gates can interact with   each other. To give you an idea of how we can  make interesting mechanisms from these seemingly   simple building blocks imagine chaining two of  these Gates together. When the input triggers   it checks the first gate. If that's activated  then it triggers the next gate which checks the   other state. Finally if both were on then the  second gate emits a signal and the door opens. If you think about it, we've just  created the world's least secure   combination lock. All we have to do is  change the direction of the switches and   hide the gates from the user and now only  a single combination will allow entrances.   The length of the code can be extended by  just chaining together more of these. [Music] Back to when I was trying these gates for the  first time, as I was playing around with them I   started getting really excited. Just by tinkering  around with these things I was able to get many   basic real world circuits working that are in  every computer like adders and multiplexers.   Making an actual computer with  them was the obvious next step,   but that's a massive jump in complexity from  what I had. So I decided to spend the next   two weeks putting in an unhealthy  amount of hours making a prototype. [Music]   This culminated in a functional,  albeit simple prototype computer.   Here you can see it calculating the Fibonacci  numbers. The A register shows the current value   in the sequence and B holds the next, both in  binary. I've displayed the decimal versions below. To understand how it works, let's talk a  bit about how computers work on the inside.   Computers, at least in high level  functionality, are surprisingly simple.   Imagine a computer as a combination of a button,  a huge list of instructions, a calculator,   and some scratch paper. Every time the button  is pressed the current instruction in the list   is fed into the calculator, then the result is  written onto the scratch paper in case it might   be used again later. The next time the button  is pressed the next instruction is executed.   At a very high level this is all that computers  do. Here we see that the computer just loops   endlessly, incrementing by two each time. That's  quite simple, but even programs as complicated as   the app or browser that you're watching this video  on are composed of millions of simple instructions   just like this. Of course, computer engineers  have fancy names for all of these. Instead of   a button that you have to manually press to  execute the next instruction, computers have   extremely fast clocks that automatically press  the button up to billions of times a second.   The list of instructions is the program  you're executing which is stored in memory.   The calculator is called the arithmetic logic  unit, or ALU. The scratch paper corresponds   to registers, which are like temporary buckets  you can store numbers in between calculations.   Finally all the data being passed between  these components has been written here in   a human readable form, but because computers  can only interpret ones and zeros everything   is represented in some kind of binary format. How  exactly each of these instructions is encoded in   ones and zeros is called the instruction set.  There's one other component to add to this:   to actually organize the flow of data between  each of these modules something called the   control unit is in charge of telling each  of the other components what to do and when.   It's kind of analogous to me, interfacing all  these components together in the demonstration This all seems pretty abstract but you can  see exactly this layout in my prototype.   There's enough memory for 32 instructions,  each eight bits long. There's an ALU which   executes arithmetic instructions such  as addition, and it reads and writes to   a pair of registers labeled A and B.  Most other stuff is the control unit Of course it's not obvious how breaking it  down this way actually helps anything. All   we've done is take one hard problem, and  converted it into five other also hard   problems. It feels like trying to cure yourself  of syphilis by infecting yourself with malaria;   it seems crazy like we're getting nowhere, but it  actually somehow works. A lot of computer science   is basically that though: breaking hard problems  into more manageable chunks. Unlike the open-ended   task of building a computer, each of these units  have very well defined inputs and outputs. For   example, to make a circuit that adds two numbers  we know the exact rules that govern an addition,   and we just have to get clever in how we hook up  the gates to match these rules, just like how we   did for the combination lock earlier. This is  a compact circuit to add to one bit numbers. There were a lot of problems with this first  prototype, as expected. Remember how I mentioned   that only the specific kind of logic gate should  be used? Making this prototype was where I   realized why the other ones don't work, but not  before some... extensive cranial agitation on   my behalf for a few days. The instructions set  that I used was custom made and very limited:   I was literally adding new operation codes  on a spreadsheet when I implemented them.   For my full-scale computer I wanted to implement  a real world instruction set. I don't have time   to go into exactly what this means, but it's  a massive difference that enables most of   what I've done here, but it involves far more work  ensuring complete compliance to a strict standard.   Finally performance-wise my prototype was  pathetic. My target for the full computer   was 100 kilobytes of memory  and far faster running speeds.   At this point I had a plan for the full scale  project, now I just needed to implement it. Building the full computer took a  long time. Like, a really long time.   There are mods and editors that enable some copy  and paste functionality, but any kind of circuitry   that isn't exactly copied has to be redone by  hand. All in all this took around three months   of active development, and along the way I learned  a huge amount about digital logic and CPU design.   Surprisingly, spending three months playing  Terraria has improved my career prospects. After all that though, I had finally done it.   I had designed and built a complete  computer from scratch in Terraria. To give you a sense of scale, here's the prototype  I talked about earlier. Here's how big it is when   compared to the real size of the final version.  Looking back at projects like this in hindsight   always gives the sense that things just work  perfectly in the first try, but they really,   really don't. I wanted to give you a small taste  of the pain I experienced debugging this behemoth   so let's look at this circuit for example. You can  see the problem, right? No? Well you can at least   tell the difference between the right and wrong  versions, right? Still no? Okay fine, let's zoom   in a bit. I'm sure now you can see the difference,  right? I'll even give you a few seconds. The problem turned out to be that this green wire  was the wrong shade of green, which meant that the   whole thing worked unpredictably, and I didn't  even know where in the world it was. It's like   trying to find a needle in a haystack except  for the needle is hay shaped and amber, rather   than the golden yellow that the hay is. Here  comes the antagonist in our story though: lag.   Imagine you spent months building this monstrous  creation, and you excitedly turn it on, and then Well, that's what would happen if you tried to run  it. It turns out for reasons unbeknownst to me,   Terraria, an indie game from over a decade ago,  is not particularly well optimized for massive   wiring projects the size of an entire world. It's  actually really intuitive why it's so slow. When   the switch is pressed, just as before, the door  should open. Every time the switch is pressed,   the game one by one looks at each wire along the  way, checks each of its neighbors, and if it has   any tries to go down those as well. It repeats  this process all the way through the entire   length of the wire. This wire is pretty short,  but when your computer is the size of a world   going back and forth across thousands of tiles  many times can grind the game engine to a halt.   However if the game just instead remembered every  time that this switch corresponded to this door   without manually checking each wire in between,  the game could run at much more reasonable   speeds. There are many other more complicated  optimizations you can also do. All of this logic   is set by the developers of Terraria inside the  game though, so there's nothing we can do about   it. A Terraria computer is just not realistically  possible. Or is it? My solution to this problem   was just to take it head-on and re-implement the  game's wiring system. My mod, called WireHead,   takes over and overrides the entire wiring system  in Terraria with its own custom version while   keeping the in-game logic identical. The game  plays exactly the same but it is way, way faster. For reference, when running without the  accelerator mod the computer runs at around   0.1 instructions per second. When using the  mod I get around 5000 instructions per second.   To give you an idea of how much faster this is  that's like the difference in speed between a   95 year old absolutely booking their way across  the street and a supersonic Concord airliner.   That's not a metaphorical comparison either:   the ratio between their speeds is literally  how much my mod speeds up the game. But that's not all! If you order now I'll throw  in absolutely free an extensive and extremely   over-engineered software tool chain supporting  development in Computerraria. I've written   WireHead to enable programmatic control of the  entire Terraria world, so you can use it to write   arbitrary binary programs into the computer's  memory. I've also written a high level and   easy to use rust application driver, set up the  official RISC-V instruction set compliance tests   to run in-game, and even published a Docker image  with continuous integration to automatically run   regression tests whenever the world has changed.  There's a ton of cool stuff here, but I won't get   into it in this video as I think more people are  interested in the Terraria part of the Terraria   computer, so just know that there's a lot of work  in the background to keep things running smoothly. Enough wait, let's take a look at what's  possible with this thing. I already showed pong,   so let's start there. What's cool about pong is  that it runs in real time and takes user input.   When I recorded this, I was in game controlling  the left paddle as it moved. Movement of your   Terraria character is converted into wiring  signals and then interpreted as up or down,   which is why my character is jerking around  like that. It's unbelievably satisfying to   play a video game live in a general purpose  computer you made inside another video game. Another program I've written for my computer  is Conway's Game of Life. The Game of Life   is a set of extremely simple rules that produce  fascinating patterns when applied repeatedly to a   grid of cells that are either alive or dead. Here  you can see it running in-game. Unfortunately,   because it's quite computationally  expensive it doesn't run very fast,   even after I optimize the program for speed quite  a bit. This recording is sped up by 240 times. I've saved the best for last. So far everything  I've showed has been pretty simple 2D programs,   but what if you could venture  into the third dimension?   Amazingly, even on a CPU as  resource limited as this you can!   Note that this isn't just playing back a  pre-recorded pixel video it's actually rendering   a full 3d World from the perspective of a certain  XYZ coordinate and displaying it on the screen.   As with the game of life it's currently too slow  to run in real time since it takes 45 seconds just   to render a single frame. I can't take full credit  here though. A friend of mine wrote this super   efficient program, and all I did was optimize  it for this computer. While not quite a simple   Doom clone yet, we're far closer to it than I was  expecting to be at the beginning of the project. So what's next? A simple version of Terraria  running inside the game Terraria is definitely   something I want to do, and I'm pretty close. It  might take a bit more work in the accelerator,   but that's the obvious next milestone.  Everything I've done so far is open   source and available on public GitHub  repositories linked In the description.   If you're curious about this project take a  look and check them out. The rust interface   to write programs is quite easy to use if you  have even a bit of software experience. It's   a lot like writing a simpler Arduino program, and  I'd be thrilled to see some more programs for it. [Music] If you've made it this far, thank you   for watching. Please like, subscribe, comment  and, I don't know, star my GitHub? If you're   interested in a detailed video going over the  technical aspects of the project more thoroughly,   please let me know in the comments. I'd be  happy to go into it if there is an audience. [Music]
Info
Channel: From Scratch
Views: 3,469,096
Rating: undefined out of 5
Keywords: Terraria, Terraria Mods, Terraria Wiring, Computer, CPU, RISC-V, Terraria Gameplay, Terraria Builds, Terraria In Game Creation, Raycaster, Terraria Logic Gates
Id: zXPiqk0-zDY
Channel Id: undefined
Length: 15min 25sec (925 seconds)
Published: Sun Jun 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.