A voxel game/Minecraft clone for the iCE40 UP5K FPGA

Related tags

Game fpga_craft
Overview

FPGA craft

A voxel game/Minecraft clone for the iCE40 UP5K FPGA (for the iCEBreaker board).

Disclaimer

Use this project at your own risk. This project has had no real testing, and unlike typical software, it may have bugs that damage your hardware (board, N64 controller, etc.). Only use it if you're familiar with FPGA/HW dev and you understand the risks.

Screenshot

Note: this repo does not contain any assets/textures, you need to provide these yourself (more details below).

image

Features

  • 3D voxel rendering on tiny FPGA!
  • 256x192 resolution (256x128 3D area), 30 fps, 12 bit color
  • Input via N64 controller
  • Real-time terrain streaming from flash memory
  • Placing and mining blocks
  • Terrain changes by player are saved back to flash
  • Max overworld size: 512x512x32 blocks (wraps at edges)
  • Offline terrain generator (run on PC, then upload terrain to flash) with multiple biomes/features (grass, forest, desert, snow, caves)
  • Low memory usage (15kb BRAM, 128kb SPRAM, 16MB flash)
  • Custom ray tracing GPU that handles ~1 million rays/second
  • Custom 16-bit CPU with its own instruction set, running at 32.625Mhz
  • Compiled firmware is ~5kb, written in custom Forth like assembly langage, compiled using custom assembler ("kasm")
  • Hardware design written in Wyre, a custom hardware design language
  • Fully dynamic hardware based lighting system
  • Day/night cycle (each full day is ~18 minutes)
  • Up to 128 textures (using palette based texture compression to save memory)
  • Dithering to emulate higher color bit depth
  • Animated textures (lava, portals)
  • Portals and multiple dimensions (though terrain gen for these is not implemented in this version, see note below)
  • Transparent water rendering (slightly hacky)
  • Underwater rendering & swim physics
  • Fly around using rockets (item)
  • Falling sand/gravel physics
  • Water/lava physics (limited to modified terrain chunks)
  • Noclip mode & debug info overlay (including player XYZ coords)
  • Inventory system

Note: you may have seen (videos of) this project on my twitter. This open source repo is a "light version" of the project, without the Minecraft specific terrain gen/biomes/structures.

Requirements

  • iCEBreaker board
  • VGA PMOD
  • VGA display
  • N64 controller (You don't need an original "vintage" controller. For example, I'm just using a cheap third party replica.)
  • Bring your own textures (see Setting up textures below).

It should be possible to port the project to other boards/FPGAs, displays (e.g. using HDMI PMOD) and input devices (e.g. PS2 mouse/keyboard or GameCube controller). If you do, let me know and I'd be happy to add a link to your project.

Build steps

Preparation

  1. Disconnect any currently connected peripherals from board.
  2. Ensure yosys, nextpnr and icestorm are installed (e.g. from here) and are available on your path.

Getting the code

  1. git clone https://github.com/nickmqb/fpga_craft (this repo)
  2. git clone https://github.com/nickmqb/muon and follow the installation instructions.
  3. If not already done, move the Muon compiler binary mu to the muon directory (i.e. file path of the binary, including filename should be: muon/mu).
  4. git clone https://github.com/nickmqb/wyre and follow the installation instructions.
  5. If not already done, move the Wyre compiler binary wyre to the wyre/compiler directory (i.e. file path of the binary, including filename should be: wyre/compiler/wyre).

Setting up textures

As mentioned above, this repo does not include any textures (except for a few custom textures specifically made for this project). You need to provide your own. You have several options:

  • If you own Minecraft, find your Minecraft .jar file (e.g. .minecraft/versions/{latest_version}/{version.jar}). From the .jar file, extract all .png files from assets/minecraft/textures/block and copy them to fpga_craft/textures.
  • Download a Minecraft texture pack (e.g. from here). This will be a .zip file with the same directory structure as the Minecraft .jar file. See the previous bullet point for which files to copy. Note: not all texture packs contain all required textures, so you may need to combine multiple packs. (Based on some quick experimentation, the "Unity" pack works. The screenshot near the top of this doc was made with this pack.)
  • Create your own textures or use a different source (e.g. a Minetest texture pack). Because the naming scheme of the texture filenames will likely be different, you will need to manually tweak fpga_craft/block_info_gen/block_info_generator.mu (namespace Ids) and provide the right filenames.

Building tools

  1. cd fpga_craft
  2. cd bram_swap && ./build.sh && cd ..
  3. cd kasm && ./build.sh && cd ..

Data gen

  1. cd block_info_gen && ./run.sh && cd ..
  2. cd data_gen && ./run.sh && cd .. (generates fpga_craft/ram_a.bin and fpga_craft/textures.bin)
  3. iceprog -o 2048k ram_a.bin
  4. iceprog -o 2112k textures.bin

Terrain gen

  1. Go to dir fpga_craft/terrain_gen
  2. ./build.sh
  3. ./terrain_gen --dimension 1 --output overworld.bin (Note: alternatively, you can use the OpenGL terrain_viewer for a preview of the generated terrain, see section below: OpenGL terrain viewer: generating terrain)
  4. ./terrain_gen --dimension 2 --output dim2.bin ("Stub" generator, also see Multiple dimensions)
  5. ./terrain_gen --dimension 3 --output dim3.bin ("Stub" generator)
  6. iceprog -o 8192k overworld.bin (file is 8MB, so this will take several minutes)
  7. iceprog -o 4096k dim2.bin (file is 0.5MB)
  8. iceprog -o 6144k dim3.bin (file is 2MB)

Building the hardware design

  1. Go to dir fpga_craft/hw
  2. By default, the code to save terrain changes to flash is commented out. You can choose to enable it.
    • To enable saving, overwrite src/firmware.ka with src/firmware_save.ka, i.e.: cp src/firmware_save.ka src/firmware.ka. Terrain changes are buffered in memory. The game automatically saves changes to flash, as needed, to keep the buffer from getting too full. Before quitting, you must manually save any remaining changes (see gameplay tips below). When the game is saving, a progress bar (consisting out of floppy disk icons) is shown in the top right. Saving is done when all floppy disks have vanished.
    • Keep an eye out for rapid flash usage bugs (though I didn't encounter any bugs like that so far). If the progress bar gets stuck and doesn't update at all for a long time (e.g. >10 seconds), the game may be stuck in a "save loop", you should disconnect power if that happens. Additionally: there are hardware counters that track the number of flash sector erase and page program operations, if you enable 'DEBUG INFO' in the game menu, these are shown on the 3rd line from the top (first 2 numbers). If these counters start going crazy, or if the game freezes, you should also disconnect power.
    • If you don't enable saving, the game buffers up to 32 changed terrain chunks (2x2) in memory. After that, subsequent placed blocks/changes will disappear as you move through the terrain. Note: if the game freezes, it's still a good idea to disconnect power; flash bugs are less likely if you don't enable saving but are still possible.
  3. ./build.sh (builds FPGA bitstream; pay close attention to the result, may fail timing; if that happens, just re-run the command until you get a result that meets timing; may need to run a few times).
  4. When the design meets timing: ./prog.sh (upload bitstream to FPGA)

Final steps

  1. Disconnect board from power
  2. Connect VGA and N64 controller according to pinout spec in fpga_craft/hw/icebreaker_spi.pcf
  3. Reconnect power
  4. Ready to play! (See below for controls and some useful gameplay tips)

Controls

  • Look around: stick
  • Move/strafe: C buttons
  • Jump: R
  • Place block: Z
  • Mine block: B (hold)
  • Select hotbar slot: DPAD left/right
  • Open door: A
  • Move down (noclip mode): A
  • Open/close inventory: L
  • Inventory, move cursor: C buttons
  • Inventory, select: A (or R)
  • Open/close game menu: Start
  • Game menu, move cursor: DPAD up/down
  • Game menu, select: A
  • Flying/boost: select rockets in hotbar, then: Z
  • When flying: brake: B

Gameplay tips

  • There is a day/night cycle. If it gets dark and you want to skip the night, hit Start, then select 'TIME' and hit A a bunch of times (TIME 0x10-0x80 = day, TIME 0x90-0x00 = night).
  • If you enabled saving (see instructions above), before quitting, hit Start on the controller, then select 'SAVE XY CHUNKS' to save remaining changes to flash. A progress bar (consisting out of floppy disk icons) is shown in the top right; saving is done when all floppy disks have vanished.

Optional steps

Changing the firmware

  1. Go to dir fpga_craft/hw
  2. Modify src/firmware.ka, e.g. to change controls or game logic.
  3. ./bfw.sh (compiles the firmware to src/firmware.w, then modifies the bitstream directly; this avoids synthesis & routing stages to allow quick iteration on the firmware)
  4. ./prog.sh to upload bitstream to FPGA.

Changing the hardware design (.w)

The HW design is written in Wyre.

Important: to avoid errors, set your tab size to 4 when editing .w files. (Wyre uses significant whitespace, but the feature still needs more work; this is the easiest workaround for now.)

Changing other tools (.mu)

The other tools in this repo are written in Muon.

Important: to avoid errors, set your tab size to 4 when editing .mu files. (Muon uses significant whitespace, but the feature still needs more work; this is the easiest workaround for now.)

OpenGL terrain viewer: set up

  1. Go back to base directory (i.e. directory with fpga_craft, muon, etc.)
  2. git clone https://github.com/nickmqb/muon_gfx and follow the setup instructions for sdl_bindings. Note: the SDL2 directory should be a subdirectory of the sdl_bindings directory.
  3. cd fpga_craft/terrain_viewer
  4. ./build.sh

OpenGL terrain viewer: generating terrain

  1. Ensure previous section is done.
  2. ./terrain_viewer --dimension 1 --output overworld.bin
  3. Fly around with WASD/space/C. Look around with arrow keys. Move slowly by holding shift.
  4. To generate a new random world, press F3.
  5. To save the world to the output path, press F12 (you must do this before exiting, otherwise output file will not be written).
  6. The tool never frees memory, and will crash if you generate a lot of random worlds -- just restart in that case ("good enough, ship it!" ;)).

OpenGL terrain viewer: viewing terrain

  1. After playing, download modified world from flash. E.g.: iceprog -o 8192k -R 8192k overworld_device.bin
  2. ./terrain_viewer --input overworld_device.bin
  3. Fly around with WASD/space/C. Look around with arrow keys. Move slowly by holding shift.

Adding new blocks/textures

There currently is room for ~75 additional textures. To add new blocks/textures:

  1. Modify block_info_gen/block_info_generator.mu. Add the textures under Ids, then add the block definitions in function BlockInfoGenerator.generate.
  2. Run block_info_gen.
  3. Add the new blocks to the inventory in data_gen/data_generator.mu (function buildInventory).
  4. Run data_gen, reupload ram_a.bin and textures.bin, and rebuild and reupload the firmware.

Multiple dimensions

The game supports multiple dimensions, though terrain gen is only implemented for the overworld.

  • Dimension 1: 512x512x32 (overworld)
  • Dimension 2: 128x128x32 (access by building an obsidian portal (like Nether portal in Minecraft))
  • Dimension 3: 256x256x32 (access only through portal_3 block; can be enabled by adding the block to the inventory in data_gen/data_generator.mu (function buildInventory), or modifying terrain gen).

Map file format

Each map consists of a series of 128 byte chunks. There is no header.

A chunk consists of 128 blocks. It has a base of 2x2 blocks (x, z) and a height of 32 blocks (y). 1 block = 1 byte. After running the block_info_generator, block_info_generator/block_info.txt contains the mapping from byte value to block type.

The offset (in bytes) of a block within a chunk has the bit pattern: yyyyyzx (rightmost bit is LSB, e.g. x=0, z=1, y=2 -> offset=10)

The offset (in whole chunks) of a chunk within a map file has the bit pattern: zxzx xxxz zzzz zxxx (rightmost bit is LSB, this pattern is used to ensure good data locality).

License & acknowledgements

See LICENSE.

stb_image.h: https://github.com/nothings/stb

textures/custom_cracked.png: from "Pixel pack" texture pack for Minetest by isaiah658 (modified)

A custom launcher for Minecraft that allows you to easily manage multiple installations of Minecraft at once (Fork of MultiMC)

PolyMC is a custom launcher for Minecraft that focuses on predictability, long term stability and simplicity. This is a fork of the MultiMC Launcher a

null 1.4k Jun 22, 2022
A Minecraft-clone written in C++/Vulkan

Minecraft A Minecraft-clone written in C++/Vulkan Current state It is currently very bare-bones. Planned features Textures Procedural generation Colli

null 4 Feb 27, 2022
A minecraft clone built in c++ opengl for the purpose of prefecting graphics programming skills.

LearnOpenGL - CLion This is a project template for OpenGL development with JetBrains CLion IDE. It was created mainly for LearnOpenGL tutorials. Inclu

Jeremy Dellock 1 Dec 28, 2021
StreamMinecraftClone - A Minecraft Clone developed live on stream at twitch.tv/gameswthgabe

Minecraft Clone This is a Minecraft clone that will be used for an education YouTube series. I will link the YouTube series here once I begin creating

null 86 Jun 22, 2022
Minecraft Classic Clone in C

Minecraft Classic Clone in C This was my first large-scale OpenGL project. I've personally never used OpenGL, and so I apologize for any bizzare/slopp

null 2 Jan 25, 2022
Minetest is an open source voxel game engine with easy modding and game creation

Minetest is an open source voxel game engine with easy modding and game creation

Minetest 7.3k Jul 3, 2022
A simplified version of the famous game Minecraft.

This program uses the concept of the famous game Minecraft but with better graphics. It procedurally generates an infinite world, the player has an animated character and can break blocks, there are also different biomes and mobs.

Angel Uriot 52 Jun 14, 2022
A Minecraft like game with basic rendering, movement, block placement.. All made from scratch

Voxel-Game-Demo A Minecraft like game with basic rendering, movement, block placement.. All made from scratch Downlod Pre-compiled binaries (executabl

null 1 Dec 30, 2021
Multiplayer Voxel Survival in C99, includes a badly written implementation of LISP

Imagine a mix between Minecraft, Quake ]I[ and Emacs. Apart from that there is no clear plan for this game, just a bunch of ideas that hopefully will turn out to be fun. Some of these are stored here in this repo, others have been talked about on Twitch during dev streams.

Ben 72 Jun 20, 2022
Battle City Game Clone

BattleCity Battle City Game Clone 4 Level 3 Level 2 Level 1 Level Menu You can also add your own level as txt-extention file: -in first row you need t

Sergey Semionkin 2 Feb 19, 2022
Minecraft Classic 0.0.30a reimplemented in C.

MinecraftC - A 0.0.30a implementation in C Features True to the original version Implemented fully in C using SDL2 and OpenGL 1.1 Two different binari

John Payne 174 Jun 19, 2022
Replace Minecraft entity with MMD model.

KAIMyEntity Replace Minecraft entity with MMD model. KAIMyEntitySaba 将Github项目 benikabocha/saba (https://github.com/benikabocha/saba) 魔改成一个以JNI形式Expor

null 26 Jun 27, 2022
Minecraft 4k: decompiled, translated to C using SDL for graphics and input, and improved upon

M4KC Minecraft 4K - C Rewrite For those who don't know, Minecraft 4K was a stripped down version of Minecraft submitted by Notch to the Java 4K Game P

Sasha Koshka 30 Jun 15, 2022
A fully-featured Minecraft server startup script

A fully-featured Minecraft server startup script suite that offers a friendly user interface, blazing fast speeds, and wide compatibility.

null 75 Jun 21, 2022
Randomizes Minecraft's loot tables.

Minecraft Loot Randomizer Randomizes Minecraft's loot tables. Use Instructions To use this program, which you can either do via the provided exe file

bryceio 1 Oct 20, 2021
MotorMC is a blazing fast, multi threaded, asynchronous Minecraft server software

MotorMC is a blazing fast, multi threaded, asynchronous Minecraft server software that aims to handle many players (1000+) on a single world while still providing an experience as close to vanilla Minecraft as possible.

Garet Halliday 59 Jun 6, 2022
📦 A familiar Minecraft Launcher with native support for macOS arm64 (M1)

ManyMC A familiar Minecraft Launcher with native support for macOS arm64 (M1) ⚠️ This is an UNOFFICIAL project. DO NOT report any issues to the MultiM

Minecraft Machina 425 Jun 26, 2022
A third party program to change Minecraft RTX's settings externally, directly in-memory.

RenderBender A third party program to change Minecraft RTX's settings externally, directly in-memory. Get the latest release here. About RenderBender

Jesse Daems 9 May 14, 2022
A clone of the classic QBasic Gorillas written in the Zig programming language

⚡ Zig Gorillas ?? A clone of the classic QBasic Gorillas written in the Zig programming language. Take turns in throwing an exploding banana at each o

Fabio Arnold 44 Jun 18, 2022