It took nearly 30 years but the de facto standard programming language of the software industry has finally made it to the humble Timex/Sinclair. It has been a long time since I have been this excited about anything in the T/S world and actually being able to write programs in C for the TS-2068 over the past few months has brought a warm grin to my face. What makes it possible is the Z88DK Small C Cross Compiler (“http://z88dk.sourceforge.net”). Z88DK supports a nearly ANSI-C implementation of the language and comes with many of the standard C libraries built-in for a variety of Z80 machines, including the Spectrum and ZX81. The level of support for each platform varies depending on the interest paid by the platform’s users. The Spectrum has seen some of the most support and boasts a fairly complete set of standard libraries.
This is not the first time a popular high-level language has been made available for Sinclair machines. David Solly has previously written articles about programming the TS-2068 using HiSoft Pascal. Indeed, HiSoft also sold a C compiler for the Spectrum (HiSoft C). The problem with these implementations is that both the compiler and source code had to fit into 48K of memory. This led to compromises, the most important one being that the compiler only implemented subsets of the actual language. Having to split up large programs into many small pieces that could be compiled in memory and then hand linking all the pieces together also made compiling any serious program a serious hassle. Z88DK is a cross compiler, meaning it actually runs on a third computer (like a PC) and generates programs for the TS-2068. Since the source code is entered on the PC and compiled there, Z88DK has the necessary available memory to be as fully featured as desired and can compile source programs of any size with little hassle. There is still the matter of transferring the finished product to the TS-2068 itself but that is also straightforward with the right tools.
For the uninitiated, C was originally developed by Dennis Ritchie in the early ’70s to write the first Unix operating system on the DEC PDP-11. It was intentionally made a small language and, as far as high-level languages go, it is as low-level as they get (Forth would be the only example of a lower level language that I can think of). This last point does not mean C is a hard language to pick up like assembly language, but that it deals with the kinds of objects that the bare CPU deals with: bits, characters, numbers and addresses. For these reasons C inherits many of the advantages of both high level and low level languages: it is easy to learn, C programs can be written quickly and the compiled result is fast and compact. The fact that C is such a small language manifests itself when it is realized that nothing much useful can be done with it without additional libraries. Libraries are vendor or user supplied collections of subroutines. Without these libraries there would be no way to print a character, read the keyboard or draw a line. The C language provides the structure of the program and the libraries provide the essence.
C took off like wildfire in the mid ’70s and like anything that grows out of control, pretty soon many variations of C not quite compatible with each other arose. The solution was a standardization that occurred in 1983 with the adoption of ANSI-C. ANSI-C formalized various language constructs and clarified many (but not all) implementation issues for compiler writers. Also part of ANSI-C is a standardized collection of libraries (the “standard i/o library”) that grants C programs basic i/o functionality (character output, character input, etc.) and access to useful utility subroutines (such as conversion from strings to integers, allocating memory, etc.). With compilers written to adhere to the standard made available for all computer platforms, significant C programs could be written with cross platform compatibility.
Since ANSI-C was introduced the language has evolved into C++, now the standard among desktop and larger computer software. C++ adds language constructs that support object-oriented, generic and functional programming styles. C, like Basic, is an imperative or procedural language where programs are written as step by step instructions. Unlike C, C++ is a large language and takes many months (some would say years!) to master. Because of its size and complexity it is an inappropriate language to program with on small computer systems.
Z88DK supports a nearly complete subset of ANSI-C and supplies most of the standard library functions expected by C programmers. I have felt little restriction in writing C programs with Z88DK, retaining the typical style I use when writing programs for modern systems. The small nature of the underlying machine means you do have to be more aware of memory constraints, speed issues (i.e. efficient algorithms are important!) and data type sizes (ints are only 16 bits). It is also helpful to know a little about how the C is compiled into assembly language in order to write efficient C on the T/S. Z88DK also supports embedding Z80 assembler into C programs.
What are the advantages of using C on the TS-2068? The biggest advantage is speed. There is simply no way to write anything faster without resorting to assembly language. With C you will also have access to many structured programming constructs that are lacking in Sinclair Basic, like while loops and functions with local variables, that make the programming experience easier and more expressive. Z88DK’s libraries also supply many functions that have no Basic equivalent. For example there are functions for printing formatted output, reading formatted input, searching strings and classifying characters as numbers / letters / punctuation. Non-standard library additions allow drawing of lines, filling screen areas and manipulating graphic sprites.
C is not only a significant step up for Basic programmers, it is also a significant tool for assembly language programmers. Complete programs can be written very quickly with excellent performance. If the performance needs to be improved, critical sections of the code can be rewritten seamlessly in assembler. Typically 90% of the time a program executes 10% of the code. Only having to rewrite that 10% of code in assembler to squeeze out the last vestiges of speed can reduce development time from months to weeks. There is also another secret: most of the library routines are written directly in assembler, meaning the C program is mainly used to hold together calls to already optimized assembly language subroutines. This is why C programs on the TS-2068 can be so quick.
I have played cheerleader long enough now; how does one get started? The best reference for learning C is The C Programming Language by Kernighan and Ritchie. I have the second edition, which is refreshingly compact having just 200 pages full of examples explaining all the features of C. Most computer books I own near 1000 pages of text and have little content; the reverse is true of this one. The authors do stress that the book is not meant as an introduction to programming but as a presentation of the C language itself. You do not need to buy a book to learn C — your local library will have many books to choose from and the internet is full of introductory tutorials on C. If ZQA manages to survive past Abed’s service to the community I would likely volunteer to write an introduction to C in this newsletter.
With some sort of C reference in hand, the next step is to get hold of the Z88DK compiler. Z88DK is distributed in both binary and source code form. Binary distributions include an EXE for Win32 console with other distributions for the Amiga, Linux and Unix systems. Visit “http://sourceforge.net/project/showfiles.php?group_id=29 17” to reach the Z88DK download page. Grab the latest version (1.5) by clicking on the appropriate link. Windows users should grab the Win32 version. The following discussion pertains to installation for Windows users.
With the zip file download, extract all the files in the archive to some directory (I suggest “C:\z88dk”). Create a file “init.bat” containing the following text and save it in “C:\z88dk”:
SET PATH=c:\z88dk\bin\;%PATH%
SET Z80_OZFILES=c:\z88dk\LIB\
SET ZCCCFG=c:\z88dk\LIB\CONFIG\
This batch file will set up the environment for Z88DK when you are ready to compile your C programs.
I also suggest creating a “work” directory in “C:\z88dk” to contain your C programs. You can write your C programs using any text editor such as Notepad in Windows. When you are ready to compile, open up a dosbox and change directories to “C:\z88dk”. Run “init.bat” to set up the environment, then change to your work directory where your C program is saved. Compile it with the following:
zcc +zx -vn myprog.c -o myprog.bin -lndos -lm -lsplib2
Where:
+zx – tells z88dk to use the Spectrum’s standard libraries
-vn – tells z88dk we are not interested in verbose messages
myprog.c is the name of the C program(s) to compile. This can be a list of files if your program is split across many files.
-o myprog.bin – specifies that the output machine code program should be named “myprog.bin”
-lndos – links in the dummy i/o stubs for the Spectrum (essential). The Spectrum does not support all the i/o facilities expected by C.
-lm – links in the floating point math library (optional)
-splib2 – links in the Sprite Pack library (optional)
Z88DK will create a machine code program assembled to run from address 32768 by default. Z88DK will automatically attach machine code subroutines from the libraries listed in the compile command as they are needed. The math library (“-lm”) is needed if your program manipulates floating point numbers. Integer operations do not require the math library to be linked. The Sprite Pack library (“-lsplib2”) is a non-standard library that contains a collection of machine code routines that I have written over the years. It does not come with Z88DK — you must download it from my web page at “http://justme895.tripod.com/main.htm”.
The Sprite Pack zip file contains the complete source of the library and can be customized for the various video modes of the TS-2068, among other things, by editing the “SPconfig.def” file prior to compilation. The header file “spritepack.h” contains the function prototypes for all the functions available from Sprite Pack. Compilation of the library creates the “splib2.lib” file. The zip file comes with a precompiled version of the library using the default settings in “SPconfig.def”. Copy “splib2.lib” to “C:\z88dk\lib\clibs” and “spritepack.h” to “C:\z88dk\include” to get access to the library. Sprite Pack contains many library functions that support sprites, dynamic memory allocation, IM2 interrupts, keyboard / joystick / mouse scanning and a host of other functionality which makes writing exciting programs for the TS-2068 much easier. The best way to learn how to use it is to go through the examples in the “examples” directory of Sprite Pack. The web page will also have a tutorial in the near future.
We have assumed that you made no mistakes with your C program in order to get the resulting binary file. If you did make some mistakes you will learn about them through error messages. Z88DK compiles C programs in a two step process: first the C is translated into assembler and then the assembler is compiled into machine code. If you’ve made a mistake you are likely to hear about it twice. To save some headache and deal with the C errors only, compile your programs with “zcc -a -vn myprog.c” at first. This command asks to translate the C to assembler and then stop there. Then only the C errors will be reported.
Once you have a binary file (“myprog.bin”) the next step is to run it. The most convenient means to test your programs is to run it on an emulator on your development machine. I typically use the “vbSpec” emulator for Windows available from: “http://www.worldofspectrum.org” because it supports loading binaries directly into memory and it has a Timex TC2048 mode that allows programs that use the advanced video modes of the TS-2068 to run properly (recall Sprite Pack will generate programs for all the TS-2068’s video modes). VbSpec’s load binaries option asks for a filename and a memory address to load the binary (enter 32768). With the file in memory it can be run with “RANDOMIZE USR 32768”.
To transfer the program to a real TS-2068 it must first be converted to tap format, which is an electronic representation of the contents of a tape. Z88DK has a tool that will do this for you. Run “\z88dk\support\zx\bin2tap myprog.bin 32768” to create the “myprog.tap” tape file. This file can be played out of the PC’s soundcard and recorder on tape with the “Taper” utility (again available from World of Spectrum). Load this program from tape into your TS-2068 as you would any other program and then “RANDOMIZE USR 32768” to execute the program.
The one gotcha to all this is that the Z88DK libraries target the Spectrum and not the TS-2068. This almost doesn’t matter, except for the few cases where the Z88DK libraries make calls into the Spectrum’s ROM. For those few cases, the compiled program may need a Spectrum emulator to run properly on the TS-2068. Most programs generated will not require the emulator, fortunately. The Z88DK folks are just waiting for one of us to create a TS-2068 specific library to add to their collection.
This has been a brief introduction to the Z88DK compiler; there is still much to know but you have enough information to get going. It is important to know that Z88DK is being actively developed and as such has a few bugs (though very few!) and is being expanded to be more ANSI-C compliant. Between official releases, the latest bits of the compiler can be retrieved from the CVS repository at source forge and from:
http://www.algonet.se/%7Edennisgr/z88dk.htm
You should do the necessary update to fix a few bugs that have been corrected since version 1.5. If you don’t know what CVS is, don’t worry — that’s the subject of another article for another time.
Experienced C programmers: With the latest updates to Z88DK there is only three noticeable deficiencies that I am aware of. One is that function pointers cannot be prototyped. I solve this problem by declaring all function pointers as void* type. The second is that complicated types cannot by typedef’d. E.g.: it is okay to “typedef unsigned char uchar;” but it is not okay to “typedef struct {…} mystruct;” This also means pointers cannot be typedef’d as in “typedef void* pvoid;”. The third is the lack of support for multidimensional arrays.
For more info, be sure to read the z88dk documentation in “C:\z88dk\doc”, particularly the “zcc.htm” file.