Making a computer game from scratch
What is a game from scratch? Is it something written in Unity? Is it writing your own game engine? I’d go one further and say it’s about building your own hardware! Yep! Lets start right at the bottom and work our way up. It’s a big project and no mistake but I’ve already made some headway.
What do we need?
Where do we even start with hardware for a game? Well, if we keep our expectations realistic, we are looking at a simple game, probably in 2D, with some simple user-interface and some bleepy bloopy sounds. Something like Tetris, Asteroids or a basic platformer. Andy Brown has a great example of what you can do with an FPGA and some other simple parts. This is where we might head.
Very roughly, we need the following:
- A display — either HDMI or VGA. This needs a framebuffer which leads us onto…
- …Memory. Something like SRAM or Hyper RAM. This will need a memory controller.
- An oscillator so we can time things.
- Power supply circuits for all the different voltages.
- A UART — some sort of communication with our system. This needs a USB socket and an FTDI chip.
- Some sort of external interface like a PMOD.
- A way to program the FPGA — a JTAG will do nicely. We also need…
- … a way to store our configuration. A small NOR Flash will do.
- LEDs to show some basic status. Maybe a couple of buttons to test that things are working.
- Some sort of storage? Proabably an SD-Card.
That should be enough to get the ball rolling. In my Previous post on FPGAs I’ve managed to get some basic VGA graphics going. So we are almost there with the first prototype.
The key to all of this is the Field Programmable Gate Array or FPGA. This amazing little chip is essentially, a set of building blocks that we can arrange to form whatever hardware we want. You can find them all over the place these days. For these of you into the retro gaming, FPGAs have become quite the thing, with projects like the MiSTer gaining a lot of traction. You can perfectly reproduce any chip you like, from the Z80 up to something like an ARM chip. The only limitation is the number of Lookup Tables (LUTS) and Pins on your FPGA and the speed at which it can be run.
There are many brands and types of FPGA. At the moment, we are working with Xilinx Spartan-7 FPGAs. The tool-chain is pretty good, the cost is low, and there are enough LUTs to get started with. There are other designs out there such as the range by Lattice, which have an Opensource toolchain for their ICE40 chips. Sadly, these tool-chains aren’t quite as evolved as the commercial stuff, but it’s only a matter of time, and once we’ve tested our stuff on the Xilinx chips, we can always move over.
At the moment, we are using System Verilog as our hardware description language. It has the benefits of Verilog (lots of existing stuff is written in it, simple syntax etc) whilst also having some very handy features such as more datatypes and namespaces. Very handy.
My good friend Will, over at Project-F has done an awful lot of work on the hardware design. All of his Verilog is out there for anyone to download and work with. So far we’ve gotten a large part of the graphics done.
For now, it’s likely we’ll be using a PMOD for sound, either designed ourselves or bought off-the-shelf. I know nothing when it comes to audio electronics so I’ll have to have a good think about that.
Interestingly, we are going back in time for this one. While we are using USB for the UART, what about controllers, mice, keyboards etc? Well, the PS2 port isn’t a bad shout. Using a single port we can plug in both a mouse and keyboard (a neat little hack that old laptops used to do). It’s a relatively simple protocol too, so that should be no bother.
Not so sure about game controllers, but the SNES controller has a relatively simple interface. If we keep a few pins free, we can plug one of these in.
But what of the CPU?
Indeed! The core, the brains of the operation! While it is possible to make a version of Tetris with no CPU I don’t think this is the way we’ll go. It’d be nice to use modern tools with our board. If we use something like RISC-V, existing compilers can target our board! That should work great! I’ve never gone down this road before — most of my verilog designs have been super simple. Nevertheless, I’m sure it’ll be a fun learning experience.
Fire up KiCAD
I’m still using KiCAD for my design work. It’s going rather well, though I do feel that KiCAD lacks support for reusing designs. If anyone knows a good way of importing circuit designs from previous projects I’d love to know! We are on our third board revision now, and I’m a little tired of having to copy a project, rename all the different parts, remove the bits I don’t need and change various paths, just to re-use the power circuits. I’m sure there is a better way. Still, it’s a great program aside from that.
The next design involves a lot of different parts, communication protocols and what not. Slow and steady prototyping seems like the only way. I’m pretty happy that our power rails, JTAG, UART, PMOD and VGA sections are working well. The next step is to get our NOR Flash, SD-Card and PS/2 interfaces going.
People take part in 48 hour game jams all the time, churning out game after game — many of which are excellent! There are easier ways to make games. Why would one do this?
Let me tell you about the PICO-8.
This fantasy console never existed, but Lexoloffle decided to make it. You can code little games for it and run them on the web or on your PC. Why on earth would someone program for this, using a PC that is a billion times more powerful?! At first, I found the existence of such a thing perverse and annoying. But that got me thinking — why is this popular?
I think it’s because simple systems, with easy ways into programming are super fun. Modern systems are super complex, with stacks that are quite tall. If you are using Unity, you are leveraging untold human hours of labour, sitting on top of a great many software and hardware layers. The PICO-8 goes someway to removing all the mess of visual-studio, direct-x, unity, shaders, SDLs, etc and provides just what is needed in an easy package. But it stops too short for my liking. It needs the hardware.
And that’s where we come in. Can we make a game from Scratch that looks and plays as well as the PICO-8 but exists in the real world and isn’t a fantasy?
I think we can!