Writing a game from first principles in assembly language.

Introduction

This is a series where we’ll write a game from scratch on an 8-bit computer, that you will be able to play online, or with physical hardware. It will go from absolute first principles, explaining how computers work, to a working game written in assembly language. If you already know some parts, you’ll be able to skip ahead, but if you have no idea how to write a program, let alone a game, I’ll set out to explain each step of the way.

This is not a course on Unity, or any of the other modern high level game engines that work on PCs. We’re going to write our own (simple) game engine from first principles. That may sound daunting, but we’ll build up step by step until we have a game that has a main menu, levels, a ‘win’ screen and a Game Over screen. To make our lives easier, we’re going to recreate a classic game from the 80’s - Manic Miner - rather than writing our version of Breath of the Wild. Expect pixel graphics and platforms, not camera effects and landscapes.

Manic Miner

Written in 1983 by Matthew Smith, a bedroom coder from Liverpool who was just seventeen at the time, Manic Miner was a groundbreaking platformer. It was the best selling game on the Commodore 64 in 1984, won awards and was ported to many computers of the time. By modern standards, it is of course basic - but it has all the elements we need to have in a game and is a good challenge for a first project.

Manic Miner

You can play Manic Miner online here: https://www.simplyeighties.com/manic-miner.php

What Will We Write?

So we have an 8-bit game that was ported to nearly every 8-bit computer at the time and many others since. What machine are we going to target, and what will we be writing? Luckily for us, there is a new (old) kid on the block. Completed early in 2021, Cerberus is a 8-bit single board computer that plugs into a standard PS2 keyboard, and outputs video to any VGA monitor. It has two microprocessors - the 65c02 and Z80. We can choose to write software for either, but in this case we’ll go for the 65c02.

You won’t need to own a physical Cerberus computer to join in either. This site includes an accurate Cerberus emulator that runs in the browser, so not only can you write and run software online, you can also use the emulator to examine the detailed status of the processor, screen and every memory location as your code runs.

Cerberus has no ‘operating system’, no built in software to help us. So when I say we’re going to write the game from scratch, we really will be starting from zero. We’ll be using assembly language, which is the lowest level language a computer understands. That means instructions are very simple, which is both a blessing and a curse. On the one hand, there is not so much to learn to understand how a 6502 microprocessor works. On the other hand, even simple functions can take a little time to write - the 6502 doesn’t even know how to multiply two numbers together!

First Steps

To start off, we’re going to examine the game and try to understand what we need to do. What does a level consist of, what are the rules for the various things we see on screen, what do we need to animate, and what else does the game display to the user as they play? We’ll need to implement all of these things, detect collisions, handle the player completing a level, or dieing in the attempt and so on. Having a list of things to consider will help us as we progress.

Then we’re going to get to grips with Cerberus. How does the display work, and how can we (manually) draw parts of a level? We’ll create some graphics by hand and edit the video memory to show them and test them out.

Once we’ve got some simple graphics, the next job will be to start writing code to draw a level for us, rather than needing to do it by hand. This will be our introduction to writing assembly language. Our first program will clear the screen, then draw our platforms and walls, and populate our obstacles and items to collect.

After that, the next job will be to animate some enemy graphics. Hopefully then we can see our levels beginning to come to life.

That leaves us plenty to do - a player character, scoring, dieing and so on. We’ll begin to plan that out closer to the time.

Describing the Game

So let’s jump right in there and figure out some of the basics. Manic Miner has twenty levels that you play through - but what does a level consist of?

Firstly, we can see that each level has unique graphics. However, the elements of the levels are fairly consistent:

  • There are walls that you cannot pass through.
  • There are platforms that you can stand on, but you can also jump up through.
  • There are crumbling platforms that fade away the longer you stand on them, until they disappear and you fall.
  • There are conveyor belts that animate, and pull you in one direction if you stand on them
  • There are stationary obstacles that kill you if you touch them
  • There are items that you collect if you touch them.
  • There is an exit block that opens when you have collected all of the items

These are the building blocks of a level. In fact they actually are blocks. If you look carefully, you can see that the level is a grid, 32 blocks wide by 16 high. Walls and platforms are made up of repeating blocks, and items and obstacles occupy single blocks. In the next part we’ll work out how that maps to Cerberus’ screen.

We can also take an early look at the enemies in the game. In any level there appears to be two possible enemy types. One moves horizontally between two points. The other moves vertically. There are a couple of exceptions, and we will examine those later.

Finally, there is Miner Willy himself. Comparing him to the blocks that make up the level, we can see that he is exactly two blocks high, and slightly more than one block wide. He can walk left and right, jump on the spot or if he’s walking, jump left or right. His jump is slightly more than two blocks high.

During the game, there is an air meter that steadily decreases. If it reaches zero, you die and restart the level. You start with three lives and once they’re all gone, the game is over.

That seems like a fairly complete description of the game for now. Certainly enough to go on. In the next post, we’ll look at how we can display a level on Cerberus’ character mapped screen.