Hello World C program for the ZX Spectrum Next using the z88dk compiler.
Z88DK
Z88DK is a collection of software development tools that targets the
8080 and z80 family of machines. It allows development of programs in
C, assembly language or any mixture of the two. What makes z88dk unique
is its ease of use, built-in support for many z80 machines, including ZX Spectrum Next, and its
extensive set of assembly language library subroutines implementing the C
standard and extensions.
The Z88DK offers several C Run Times (CRT) that can be selected using -startup at compile time. These CRTs provide the initialisation code that is called before main(). Some of them provide a simple tty device which supports controls codes such as \x16nn move cursor to X,Y.
NB when installing z88dk on Windows you need to set up the environment variables
- create a environment variable to point to the z88dk root folder e.g. called z88dk
- include z88dk\bin in your PATH
- set Z80_OZFILES to point to \lib e.g. $Env:Z80_OZFILES=$Env:z88dk+"\Lib\"
- ZCCCFG to point to the config folder e.g. $Env:ZCCCFG=$Env:z88dk+"\Lib\Config"
Hello World C program
The following program prints "Hello World" near the middle of the
screen and then loops forever changing the border between red and
yellow.
The resultant .nex file can be loaded by a ZX Spectrum emulator such as
CSpect or copied over and run on real ZX Spectrum Next hardware.
/* =========================================================================
ZX Spectrum Next Hello World C program
Compile using z88dk
zcc +zxn -vn -SO3 -clib=sdcc_iy -startup=1 --max-allocs-per-node200000 .\helloworldSpectumNext.c -create-app -subtype=nex
+zxn target ZX Spectrum Next
-vn no verbose
-SO3 set optimisation level 3
-clib use sdcc_iy library (and use the recommended zsdcc compiler which produces smaller code)
-start set the C Run Time. 1 is standard 32 column display tty_z88dk terminal
--max-allocs-per-node controls how deeply zsdcc looks at code alternatives
-create-app run the program to create an executable that can be run by an emulator
-subtype=nex Create a nex file executable
*/
#include <arch/zxn.h> // ZX Spectrum Next architecture specfic functions
#include <stdio.h>
// Define some macros to make use of tty_z88dk control codes
// Program must be compiled with a CRT that supports tty_z88dk e.g. -startup=1
#define printInk(k) printf("\x10%c", '0'+(k))
#define printPaper(k) printf("\x11%c", '0'+(k))
#define printAt(row, col) printf("\x16%c%c", (col)+1, (row)+1)
int main()
{
printAt(10,10); // move cursor
puts("Hello World!");
while(1) { // loop for ever
zx_border(INK_RED); // set border red
zx_border(INK_YELLOW); // set border yellow
};
return 0;
}