C on the Spectrum (z88dk)

The place for codemasters or beginners to talk about programming any language for the Spectrum.
FFoulkes
Berk
Posts: 30
Joined: Thu Feb 07, 2019 2:42 pm

C on the Spectrum (z88dk)

Post by FFoulkes » Thu Feb 07, 2019 3:29 pm

Hi,

I'm new to the forum. But not to the Spectrum. :) When I was young, I always wanted to go beyond Basic, but Assembler seemed a bit too harsh for me. Much later, I messed with C on Linux, but got better results with Perl and Python. Using the "fuse"-emulator, I wondered, if I could program the Spectrum in C.
Someone pointed me to the compiler "z88dk".

Yesterday, I could do this:

Code: Select all

#include <stdio.h>

/* toscreen.c */

int main() {
    int i;
    for (i=16384; i<=18384; i++) {
        /* printf("%d\n", i); */
        bpoke(i, 25);
    }
}
And compile with:

Code: Select all

zcc +zx -clib=ansi -lndos -create-app toscreen.c
Compile-line from: https://www.z88dk.org/wiki/doku.php?id=platform:zx
This creates a file "a.tap", you can run with "fuse a.tap".
It pokes a pattern into the screen-buffer, just like:

Code: Select all

10 FOR i = 16384 TO 18384
20 POKE i, 25
30 NEXT i
But in C ! I was really excited to see that.

Then I thought about writing a PLOT-function in C. Seems, it's quite tricky to poke in the correct value at the correct address, to set a single point at a certain pixel (you know, how the Spectrum-screen builds up).
Seems, there's a library for z88dk for that. So you can plot like this:

Code: Select all

#include <arch/zx.h>
#include <stdlib.h>

/* plot.c */

void plot(unsigned char x, unsigned char y) {
    *zx_pxy2saddr(x,y) |= zx_px2bitmask(x);
}

int main(void) {
    unsigned char i;
    zx_cls(PAPER_WHITE);
    for(i=0; i<15; i++) {
       plot(rand() % 256, rand() % 192);      
    }
    return 0;
}
Compile with:

Code: Select all

zcc +zx -vn -clib=new -create-app -zorg=32768 plot.c
Doc: https://github.com/z88dk/z88dk/blob/mas ... raphics.md

That's pretty cool. I can write my code in vim on Linux, and execute it on a (virtual for now) Spectrum.
Of course the .tap would work on a hardware-Spectrum too.
Let's see, what else I can do ...
1 x

User avatar
PeterJ
Site Admin
Posts: 1248
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: C on the Spectrum (z88dk)

Post by PeterJ » Thu Feb 07, 2019 3:58 pm

Nice @FFoulkes! I'm looking forward to see how you get on. How did you find getting Z88dk running under Linux?

Take a look at this from @dfzx:

https://github.com/z88dk/z88dk/blob/mas ... edGuide.md
0 x

FFoulkes
Berk
Posts: 30
Joined: Thu Feb 07, 2019 2:42 pm

Re: C on the Spectrum (z88dk)

Post by FFoulkes » Thu Feb 07, 2019 4:15 pm

PeterJ wrote:
Thu Feb 07, 2019 3:58 pm
Nice @Ffoulkes! I'm looking forward to see how you get on. How did you find getting Z88dk running under Linux?
Well, it comes with scripts "build.sh" and "config.sh". These have to be executable ("chmod +x ./build.sh", "chmod +x ./config.sh"). It compiles ok then, when you run "./build.sh" (OpenSuSE 13.1), no severe errors. Installation goes into "/usr/local/share/z88dk" then. Sometimes it wants something in "/usr/local/lib/z88dk" instead. I copy stuff by hand there then. I guess, you need a bit of experience with Linux, but most C-programmers will have that, I think. Usually, I build myself a rpm-package with "checkinstall", but that didn't work this time. So it was "make install" - without the option to remove the files cleanly later. The build-directory with the compiler-libraries was 171 Mb in size. Quite big for a compiler targeting a simple 8 bit-platform, I would say.
PeterJ wrote:
Thu Feb 07, 2019 3:58 pm
Take a look at this from @dfzx
https://github.com/z88dk/z88dk/blob/mas ... edGuide.md
Wow, thanks, that's just what I needed! I will read that soon.
1 x

dfzx
Microbot
Posts: 167
Joined: Mon Nov 13, 2017 6:55 pm
Location: New Forest, UK
Contact:

Re: C on the Spectrum (z88dk)

Post by dfzx » Thu Feb 07, 2019 5:37 pm

Good to have you onboard @FFoulkes!

C remains a very much underused language for Spectrum development, which is a shame because in many ways it offers the best of all worlds: program development is much faster than assembly language yet final program execution is close enough to assembly language speed as makes no difference in most cases.

z88dk is excellent, with good tools, standard C libraries and IO facilities, and Spectrum specific libraries with over 1,000 subroutines all hand coded in assembly language. It carries support for all Spectrum models and all the facilities you might need, including sound, sprites, multicolour, etc.

You've clearly done the hard bit of getting the thing installed and working. It's thankfully easier than it used to be - just run a single script on Linux as you've discovered. My recommendation now would be to focus on the ZSDCC compiler and new library, which is altogether much more modern and standards compliant than the older sccz80 compiler with the classic library.
0 x

FFoulkes
Berk
Posts: 30
Joined: Thu Feb 07, 2019 2:42 pm

Re: C on the Spectrum (z88dk)

Post by FFoulkes » Fri Feb 08, 2019 4:22 pm

Thanks for the kind words, dfzx!

In my first approach (which worked out), I used z88dk from Sourceforge, but it's the older version of 2017.
To install the recent version, which uses "zsdcc", like suggested in the tutorial, seems a bit tougher.
Here's what I did now:

The tutorial wants two additional files, "zsdcc" and "zsdcpp". These are executables from a different compiler called "sdcc". They are used with z88dk though.
It seems, z88dk uses two different compilers and even two completely different sets of C-libraries. That makes it all a bit complicated.

For now, how to get the two files. It seems, the directories in the sdcc-source have changed a bit, since the page with instructions was written.

1. Getting the recent source of sdcc:

Code: Select all

svn checkout svn://svn.code.sf.net/p/sdcc/code/trunk sdcc-code
2. Get the code of "sdcc-z88dk.patch" from "https://github.com/z88dk/z88dk/tree/master/src/zsdcc". It's also in z88dk's source files.

3. "cd sdcc-code/sdcc" and copy "sdcc-z88dk.patch" in there. Execute:

Code: Select all

patch -p0 < sdcc-z88dk.patch
It should tell, that four files have been patched in the "src"-directory below.

4. Configure with:

Code: Select all

./configure --disable-mcs51-port --disable-avr-port --disable-ds390-port --disable-hc08-port --disable-pic-port --disable-pic16-port --disable-xa51-port --disable-stm8-port --disable-tlcs90-port --disable-s08-port --disable-pic14-port --disable-ds390-port --disable-ds400-port --disable-ucsim --disable-device-lib --disable-packihx
This disables most targets for sdcc, but the zx-target. Otherwise compilation would take almost forever.

5. Go (with cd) to "sdcc-code/sdcc/src". Run "make" here. (Important step. Otherwise main compilation won't work).

6. Go (with cd) to "sdcc-code/sdcc" again. Run "make" here. It will probably fail, but go on to next step.

7. Check, if there's the "sdcc"-binary in "sdcc-code/sdcc/bin". If it is, rename it to "xsdcc".

8. Go (cd) to "sdcc-code/sdcc/support/cpp". Run "./configure" and "make" here.

9. Now, there should be the "zsdcpp"-binary in "sdcc-code/sdcc/bin". If it is, rename it to "xzsdcpp". Done.

It took me quite a while, to figure that out. (Maybe that information could be useful for someone else some time.)

I still have problems, running the recent z88dk-compiler though. I'm getting this error-message:

Code: Select all

m4:/usr/local/share/z88dk/lib/config/../..//libsrc/_DEVELOPMENT/target/zx/startup/zx_crt_0.asm.m4:1: cannot open `z88dk.m4': No such file or directory
Env had been set (and I tried to copy the file 'z88dk.m4' - which I have - about everywhere):

Code: Select all

export ZCCCFG=/usr/local/share/z88dk/lib/config
I can run the older version of z88dk from Sourceforge though.

In general, you should probably "write in C". ;)
I'm just not sure, if 48K memory (or even less) is enough to hold interesting programs. Maybe it has to be Assembly on such small systems to get decent results. We'll see. :)

From this point of view, it was really amazing, how much content they pushed into such little memory, when you think of "The Hobbit", for example.
0 x

dfzx
Microbot
Posts: 167
Joined: Mon Nov 13, 2017 6:55 pm
Location: New Forest, UK
Contact:

Re: C on the Spectrum (z88dk)

Post by dfzx » Fri Feb 08, 2019 4:47 pm

I'm impressed you worked your way through that lot @FFoulkes! That's the old way. Unfortunately z88dk suffers from a whole pile of out of date information, and it's easy to stumble across bits of it.

The current correct way of building z88dk is described here:

https://github.com/z88dk/z88dk/wiki/ins ... inux--unix

In fact, that's now out of date too because it tells you to go and get SDCC. That's not necessary because the script goes and gets the correct version of SDCC from Sourceforge, builds and and patches it itself. I tried that script yesterday to create a fresh z88dk installation and it did the job from start to finish. Set the ZCCCFG envvar and it's ready to go. I suggest you try again and hopefully it'll fix your m4 problem. I've no clue why you're seeing that.

If you have a z88dk installation as part of your Linux distribution, uninstall that first. It just complicates things. Then do the z88dk build somewhere in your home directory and run it from there. No need to install system wide into /usr/local unless you really need to for some reason.
0 x

FFoulkes
Berk
Posts: 30
Joined: Thu Feb 07, 2019 2:42 pm

Re: C on the Spectrum (z88dk)

Post by FFoulkes » Fri Feb 08, 2019 6:09 pm

Hooray, I got it working now! Even got my rpm. Just wanted to let you know.
I'll start with the tutorial tomorrow. Thanks!
0 x

User avatar
utz
Dizzy
Posts: 53
Joined: Wed Nov 15, 2017 9:04 am
Contact:

Re: C on the Spectrum (z88dk)

Post by utz » Sat Feb 09, 2019 2:07 pm

dfzx wrote:
Fri Feb 08, 2019 4:47 pm
The current correct way of building z88dk is described here:

https://github.com/z88dk/z88dk/wiki/ins ... inux--unix
Doesn't quite work for me with latest nightly. On step 'Verify the Install':

Code: Select all

$ zcc +zx -vn test.c -o test -lndos           
warning: unknown option '-iquote.'
warning: unknown option '-isystem/home/myuser/path/to/z88dk/lib/config/../..//include'
spurious duplicate filename '/tmp/tmpXXkntuNK.i2' - vs. 'test.c' 
Usage: ucpp [options] [file]
...
0 x

FFoulkes
Berk
Posts: 30
Joined: Thu Feb 07, 2019 2:42 pm

Re: C on the Spectrum (z88dk)

Post by FFoulkes » Sat Feb 09, 2019 3:31 pm

@utz: If you really want to set it up, we can go through the installation step by step. What do you think?

------

After deleting some files in the working directory, I got that error about not finding "z88dk.m4" again. It turns out, that file has to be in my working directory (where I compile), then it works.
Maybe that's just my system.

Then something nice: I often use a little example-program, originally found in a good old book about Perl (by Laura Lemay). My C-version of it (intended for gcc) also works just fine with z88dk on the Spectrum, without having to change much. Made me quite happy:

Code: Select all

#include <arch/zx.h>
#include <stdio.h>
#include <string.h>

int main() {
    zx_cls(PAPER_WHITE);
    puts("Cookie Monster\n");
    char cookies[10];
    strcpy(cookies, "");
    while (strcmp(cookies, "COOKIES") != 0) {
        printf("I want COOKIES: ");
        scanf("%s", &cookies);
    }
    puts("\nMmmm. COOKIES.");
    return 0;
}
Compile-line:

Code: Select all

zcc +zx -vn -clib=sdcc_iy -startup=0 -zorg=32768 -create-app cookie_monster.c
Edit: Rather fast and simple in Spectrum's BASIC:

Code: Select all

10 INPUT "I want COOKIES: ";a$
20 IF a$ <> "COOKIES" THEN GO TO 10
30 PRINT "Mmmm. COOKIES."
"bas2tap" seems to be a nice program, too.
0 x

dom
Berk
Posts: 3
Joined: Sun Feb 10, 2019 11:08 am

Re: C on the Spectrum (z88dk)

Post by dom » Sun Feb 10, 2019 11:15 am

utz wrote:
Sat Feb 09, 2019 2:07 pm
Doesn't quite work for me with latest nightly. On step 'Verify the Install':

Code: Select all

$ zcc +zx -vn test.c -o test -lndos           
warning: unknown option '-iquote.'
warning: unknown option '-isystem/home/myuser/path/to/z88dk/lib/config/../..//include'
spurious duplicate filename '/tmp/tmpXXkntuNK.i2' - vs. 'test.c' 
Usage: ucpp [options] [file]
...
I think that means there's another ucpp on your path somewhere. Setup the path so that z88dk/bin is first. There is a compilation option to prefix all the commands with something unique to avoid this problem for global installations but I'm not sure it works properly for the zsdcc component.
0 x

Post Reply