Skip to content

Classic game of Snake that runs from a boot sector

License

Notifications You must be signed in to change notification settings

adam10603/mbr_snake

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MBR Snake 🐍

Version License

Intro 🖊️

A classic game of Snake that fits into an x86 boot sector, written in C++17.

The main goal of this project is to show how it's possible to use modern tools such as STL algorithms, lambdas, templates and more, to target even the most low-level environments without any runtime overhead. This CppCon talk by Jason Turner with a similar theme was a big inspiration behind making this.

Gameplay

An x86 boot sector has 510 bytes of usable program space. The entire game including logic, graphics, input handling etc. has to fit in there. Since there's no underlying OS to provide further runtime functionality (like memory allocation) either, the binary has to be fully self-sufficient as well.

Originally I developed a version of this for a university assignment in 2017, but I had to cut features at the time due to the size limit. Recently I came across that original project and decided to give it another go and share it here for the snake of it.

In the code you might see some odd choices in the way I did things, but the explanation is almost always the executable size. There were a few places where writing something in a weird way resulted in a decreased binary size, so if you see anything odd, that might be why.

Usage 🖥️

  1. To run the game, you can download a bootable image from Releases, or build one yourself (see Building).

  2. After you have an image, you can use a virtual machine to boot it (such as VirtualBox or QEMU), or even a real PC by flashing the image onto a USB drive for example.

    • This browser-based VM is very convenient. Open the site, select mbr_snake.img as "Floppy disk image", and click "Start emulation". I suggest increasing the "Scale" setting to see things better.
  3. Once you've booted the game, you can play using the arrow keys ⬆ ➡ ⬇ ⬅ .

Building 🛠️

Building this project specifically requires GCC on Linux. Your mileage may vary depending on your GCC version though, since a different version may produce a slightly oversized binary. For reference, I used GCC 9.3.

  1. Make sure libc6-dev-i386 or your distro's equivalent 32-bit dev library is installed. See this post for more info.

  2. Use make for the initial build. This creates one file in the bin directory:

    • mbr_snake.bin - This is the raw program binary and nothing else. It's useful for checking the final size of the program (cannot be over 510 bytes).
  3. Use make image which uses the file from step 2 to make two more files (these are what you get in a Release):

    • mbr_snake_bs.bin - Same as the file from step 2, but it's been padded to 512 bytes and given the 2-byte MBR signature 55 AA at the end. This is an exact image of a boot sector.

    • mbr_snake.img - This is the final product you'll probably want, as this is a bootable floppy disk image. It's a 1.44MB zero-filled file with the contents of mbr_snake_bs.bin copied to its first 512 bytes, making for an empty floppy image with the game in its boot sector.

Known Issues 🛑

  • A newly spawned food item can overlap with the snake's body. This isn't game-breaking though, as you can just move over a bit, then come back to grab it. Sadly, I can't insert a check for this due to the binary size limit.

  • Pressing End or PageUp will temporarily break the game until you press an arrow key. Once again, the reason is the lack of space for doing better input checking. As to why those two keys in particular are the problem, that comes down to keyboard scan code values.

Version History 📃

  • v1.0
    • Initial release

MIT Logo Licensed under MIT License.