Homebrew z80 C Tutorial

Homebrew z80 C Tutorial

This page has a tutorial that will walk you through creating a C program to run on your NABU computer. The program will display Hello World bouncing on the screen with some music.

Back to NABU Homebrew · NABU Developers Quiver

What You Need

Before you follow the tutorial below, there are a few items you will need for the adventure. These include specification documents, and a compiler.

z88dk

This is a popular C compiler for Z80 consoles and computers, with a built-in NABU target (zcc +nabu). Find it and the other tools in Programming Tools.

NABU Tech Manual

This manual was created by a NABU enthusiast who goes by MJP. The manual includes port addresses for sound, video, keyboard, and communication.

NABU API Manual

The NABU API is a set of subroutines embedded in the downloadable operating system (DOS) that makes the hardware more accessible to programmers. Currently, we do not have a development environment configured to use the subroutines, but we’re working on it. In the meantime, this document shares the list of available subroutines.

Sample Hello World C

DJ Sures has provided a sample Hello World bouncing demo source that can be compiled and loaded on your NABU PC. This is part of the tutorial below, so you will need this file to continue.

Step 1 — z88dk

Download the z88dk release for your operating system using the link above in What You Need. These instructions will tailor to Windows users, but Linux users will be experienced enough to interpret the differences.

  1. Download z88dk from the link above in What You Need.
  2. Uncompress all files from z88dk into C:\z88dk.

Step 2 — Download Hello World C Example

Download the Hello World C from the above resource above. You will need to extract these files in a folder. Within the folder should be the following files:

  • build.bat
  • HelloWorld.c
  • NABU.c
  • NABU.h
  • patterns.h
  • tms9918.c
  • tms9918.h

Step 3 — View HelloWorld.c File

We can now open the main file (HelloWorld.c) to view the contents. Visual Studio Code is a sound editor for viewing/editing assembly source code files; others may prefer Notepad++. However, any text editor will do.

We do not need to make any changes to this file. However, you can scroll through to see the specifics that make this a NABU application. Specifically, the ORG address of $140D, and notice there is 3 NOP at the very beginning. The NABU BIOS loads the application at $1410, but it calls $140D the entry point. If the 3 NOPs and ORG are not at the beginning of the file, your program will not work.

Step 4 — Build Batch File

Look at the build.bat batch file included with the Hello World C example program. The batch file contains simple DOS commands that compile the Hello World C source code into a .nabu binary. You need to be in the z88dk command prompt environment when executing this batch file. To view the batch file, right-click on the Hello world 1.bat file and select EDIT.

Step 5 — Compile Program

Compiling the HelloWorld.C file to a Nabu program is as easy. We will execute a shell command prompt for the z88dk environment, navigate into the folder containing the project, and run the build batch file.

  1. Navigate to the z88dk folder.
  2. Double-click on the z88dk_prompt.bat file (for windows users). Or, Linux users can run the set_environment.sh file.
  3. In the new command shell, navigate to the Hello World C example files folder. Example: cd \nabu\helloworld
  4. Type BUILD to launch the build process.

Step 6 — Upload to NABU

Now comes the best part! We get to upload the file to the NABU PC using the NABU Internet Adapter. At this stage, we have assumed you have already successfully used the internet adapter to use the default NABU Network software on the NABU PC and, therefore, have a working RS422 to USB cable.

  1. Load the NABU Internet Adapter application.
  2. View the Settings menu.
  3. Navigate to the SOURCE tab and select the checkbox next to Use Local Folder.
  4. Press the Select Segment Folder button and navigate to the folder working folder with the Hello World C and the 000001.Nabu files.
  5. Save the configuration and return to the main screen.
  6. Under the File menu, select the Clear PAK Cache option. This will clear any PAK files that have been cached because we will load our Hello World program instead of the NABU Network Main Menu.
  7. Power on the NABU and see your program run!

What Have We Just Done?

The ROM in the NABU contains a small program that communicates with the NABU Network Adapter and requests a program called The Main Menu by its PAK identifier 000001.

As the ROM downloads the program, it writes it to memory starting at address $140D. Once the entire program has been downloaded, the ROM jumps to $140F, two bytes into the program, and the program starts running. That is why the program is built with ORG $140D and starts with NOPs: the first bytes are skipped, so no real code may live there. See the NABU PC Boot Process for every step.

What we are doing in this tutorial is replacing the main menu (000001) with our program. The ROM does not know that we did that, nor does it care. All the ROM does it transfer the program and CALL the memory location to execute it. Using this method, we can load any homebrew software onto the NABU PC.

What’s Next?

Now that your first C program runs, explore the complete example games in NABU-LIB on GitHub, such as Brick Battle and Game Man Yeah! They show sprites, sound, joysticks and RetroNET file access. To let people play against each other online, read the Multiplayer Developer Guide.

When your program is ready, publish it as a NABU channel so every NABU Internet Adapter can load it and anyone can try it with Play in browser. Find more tools in the NABU Developers Quiver.

An unhandled error has occurred. Reload 🗙

Rejoining the server...

Rejoin failed... trying again in seconds.

Failed to rejoin.
Please retry or reload the page.

The session has been paused by the server.

Failed to resume the session.
Please retry or reload the page.