C on the Spectrum (z88dk)

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

C on the Spectrum (z88dk)

Post by FFoulkes »

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 ...
User avatar
PeterJ
Site Admin
Posts: 6853
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: C on the Spectrum (z88dk)

Post by PeterJ »

Nice [mention]FFoulkes[/mention]! 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 [mention]dfzx[/mention]:

https://github.com/z88dk/z88dk/blob/mas ... edGuide.md
FFoulkes
Microbot
Posts: 161
Joined: Thu Feb 07, 2019 2:42 pm

Re: C on the Spectrum (z88dk)

Post by FFoulkes »

PeterJ wrote: Thu Feb 07, 2019 3:58 pmNice @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 pmTake 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.
dfzx
Manic Miner
Posts: 673
Joined: Mon Nov 13, 2017 6:55 pm
Location: New Forest, UK
Contact:

Re: C on the Spectrum (z88dk)

Post by dfzx »

Good to have you onboard [mention]FFoulkes[/mention]!

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.
Derek Fountain, author of the ZX Spectrum C Programmer's Getting Started Guide and various open source games, hardware and other projects, including an IF1 and ZX Microdrive emulator.
FFoulkes
Microbot
Posts: 161
Joined: Thu Feb 07, 2019 2:42 pm

Re: C on the Spectrum (z88dk)

Post by FFoulkes »

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.
dfzx
Manic Miner
Posts: 673
Joined: Mon Nov 13, 2017 6:55 pm
Location: New Forest, UK
Contact:

Re: C on the Spectrum (z88dk)

Post by dfzx »

I'm impressed you worked your way through that lot [mention]FFoulkes[/mention]! 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.
Derek Fountain, author of the ZX Spectrum C Programmer's Getting Started Guide and various open source games, hardware and other projects, including an IF1 and ZX Microdrive emulator.
FFoulkes
Microbot
Posts: 161
Joined: Thu Feb 07, 2019 2:42 pm

Re: C on the Spectrum (z88dk)

Post by FFoulkes »

Hooray, I got it working now! Even got my rpm. Just wanted to let you know.
I'll start with the tutorial tomorrow. Thanks!
User avatar
utz
Microbot
Posts: 113
Joined: Wed Nov 15, 2017 9:04 am
Contact:

Re: C on the Spectrum (z88dk)

Post by utz »

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]
...
FFoulkes
Microbot
Posts: 161
Joined: Thu Feb 07, 2019 2:42 pm

Re: C on the Spectrum (z88dk)

Post by FFoulkes »

@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.
dom
Drutt
Posts: 11
Joined: Sun Feb 10, 2019 11:08 am

Re: C on the Spectrum (z88dk)

Post by dom »

utz wrote: Sat Feb 09, 2019 2:07 pmDoesn'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.
dom
Drutt
Posts: 11
Joined: Sun Feb 10, 2019 11:08 am

Re: C on the Spectrum (z88dk)

Post by dom »

FFoulkes wrote: Thu Feb 07, 2019 4:15 pm 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.
That's not intended - can you let me know which ones?
FFoulkes wrote: Thu Feb 07, 2019 4:15 pm 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.
Well...each target (machine) library is roughly a megabyte, and there's about 80 of them so the disc space mounts up quickly!
FFoulkes
Microbot
Posts: 161
Joined: Thu Feb 07, 2019 2:42 pm

Re: C on the Spectrum (z88dk)

Post by FFoulkes »

dom wrote: Sun Feb 10, 2019 11:27 amThat's not intended - can you let me know which ones?
Ah, never mind. I think, it's ok. I was a bit confused (by the installation), when I wrote the posting.
User avatar
utz
Microbot
Posts: 113
Joined: Wed Nov 15, 2017 9:04 am
Contact:

Re: C on the Spectrum (z88dk)

Post by utz »

dom wrote: Sun Feb 10, 2019 11:15 am
utz wrote: Sat Feb 09, 2019 2:07 pmDoesn'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.
Confirmed, I have ucpp as part of my OS (Gentoo), where it is required to build libreoffice. I'm no expert on these matters but I think that implies that changing the path is not an option. How do I apply the prefix option for a local installation? I tried setting it in the Makefile but that didn't solve the problem.

EDIT: Upon second thought, I think I can get away with overriding the path just for the user session, as I probably won't be needing the system ucpp for anything else. At least for now it works fine. So, thanks for your help :) Might still be a good idea to have those prefixes available for local installs somehow.
FFoulkes
Microbot
Posts: 161
Joined: Thu Feb 07, 2019 2:42 pm

Re: C on the Spectrum (z88dk)

Post by FFoulkes »

Well, I'm running into a problem now. I'd like to print a UDG at a certain place on the screen.
Printing an "A" works with "-startup=1", but this has no UDG-support, meaning "printf("%c", 144);" doesn't do anything (although the UDG is defined (at address 65368)).
"-startup=30" has a certain UDG-support (printing in general does work then). But then the "PRINT AT" doesn't work:

Code: Select all

#include <stdio.h>
int main() {
    puts("\x16\x0A\x0D" "A");
    return 0;
}

Code: Select all

zcc +zx -vn -clib=sdcc_iy -startup=30 -zorg=32768 -create-app test.c
Any ideas how to get both working (UDG plus "PRINT AT")? Seems to be quite important for games, I guess.

There was a discussion about it here, but there wasn't a solution yet. Maybe we have to wait for further compiler development ...

Oh, and there is an error "Integer out of range" then, which looks just like in Basic. If the ordinary ROM-routines are called anyway, C may not be much faster than Basic, I think.
dfzx
Manic Miner
Posts: 673
Joined: Mon Nov 13, 2017 6:55 pm
Location: New Forest, UK
Contact:

Re: C on the Spectrum (z88dk)

Post by dfzx »

FFoulkes wrote: Mon Feb 11, 2019 1:16 am Well, I'm running into a problem now. I'd like to print a UDG at a certain place on the screen.
I think there's a bit of a context shift required for people coming to C on the Spectrum. UDGs are largely a concept from BASIC which don't make a lot of sense in the C world. C is much closer to assembly language, so you need to think at a lower level. OTOH, the stdio stuff is a high level concept from UNIX, and although it's a useful starting point I personally have never been convinced of its value on the Spectrum. Once you get past "hello world," unless you're doing something which requires a lot of text manipulation, you'll probably just turn it off. Mixing these disparate concepts together is, as you've found, a bit of a headache. It's possible to make it work, as per Alvin's post which you found in the z88dk forum, but it doesn't really happen naturally.

So, at the risk of telling you you're asking the wrong question then answering my own, let's take a step back. :)

As soon as you start thinking about UDGs, you're thinking about graphics, and graphics, once you step outside the world of BASIC, means you need to start thinking about how to directly change the Spectrum's display. The display is memory mapped, so you change it by POKEing (to use the BASIC terminology) values into it. If you want an 8x8 pixel graphic to appear in the middle of the display you can do it like this:

Code: Select all

#include <arch/zx.h>

unsigned char hash_udg[] = { 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA };

void printc(unsigned char x, unsigned char y, unsigned char* udg)
{
   unsigned char *p;
   unsigned char i;

   p = zx_cxy2saddr(x, y);

   for (i = 0; i < 8; ++i)
   {
      *p = *udg++;
      p += 256;
   }
}

int main( void )
{
  printc(15, 12, hash_udg);
  return 0;
}
which you can compile with:

Code: Select all

zcc +zx -vn -clib=sdcc_iy -startup=31 printc.c -o printc -create-app
The zx_cxy2saddr() function is described along with all its friends here, but in essence it takes an x,y character coordinates pair and returns you the address in the Spectrum's display of the top byte of that character cell. Adding 256 to it returns the address of the byte directly below it, hence the loop POKEs the 8 UDG byte values directly into the display at the place required.

That's the simple and fast way. At the other extreme you can use the SP1 library. You're not at that level yet, and I haven't written the tutorial article which tells you how to do it, but as a peek ahead you can see the control characters of the SP1 library's very sophisticated print facility here. SP1 isn't to everyone's taste, nor the answer to all problems, but if you're after a powerful library to experiment with for graphics it's worth looking into when you're ready.

Quite where this all leaves you I'm not sure. :lol: I suppose it depends on what you're trying to learn or what problem you're trying to solve.
Derek Fountain, author of the ZX Spectrum C Programmer's Getting Started Guide and various open source games, hardware and other projects, including an IF1 and ZX Microdrive emulator.
dom
Drutt
Posts: 11
Joined: Sun Feb 10, 2019 11:08 am

Re: C on the Spectrum (z88dk)

Post by dom »

dfzx wrote: Mon Feb 11, 2019 3:52 pmI think there's a bit of a context shift required for people coming to C on the Spectrum. UDGs are largely a concept from BASIC which don't make a lot of sense in the C world. C is much closer to assembly language, so you need to think at a lower level. OTOH, the stdio stuff is a high level concept from UNIX, and although it's a useful starting point I personally have never been convinced of its value on the Spectrum. Once you get past "hello world," unless you're doing something which requires a lot of text manipulation, you'll probably just turn it off. Mixing these disparate concepts together is, as you've found, a bit of a headache. It's possible to make it work, as per Alvin's post which you found in the z88dk forum, but it doesn't really happen naturally.
I have to admit that I've never fully got to grips with the way the newlib stdio works, it's powerful but has quite a bit of overhead.

In fact, stdio one of two areas where classic and newlib really diverge: most of the rest of the library code is shared. However, if you're looking to create a text based application that works across many platforms then classic may be the answer.

The gencon is a terrible name for the VT52+ZX Code screen driver that's supported on "most" of the classic targets. It provides a consistent way to print text (and yes, UDGs) and handle colours across many platforms. It even provides a SCREEN$ equivalent for collision detection. Cross Chase uses gencon for many of the z80 builds.
Last edited by dom on Mon Feb 11, 2019 8:18 pm, edited 1 time in total.
FFoulkes
Microbot
Posts: 161
Joined: Thu Feb 07, 2019 2:42 pm

Re: C on the Spectrum (z88dk)

Post by FFoulkes »

Thank you, dfzx. I think, this is a very good answer!
Because probably more people starting with C on the Spectrum will come to the point, where they need to think beyond Basic.
At the moment, I wasn't sure about going for C or assembly directly, so I watched a tutorial about Spectrum's assembly. Quite a good one, actually. There was some homework, so I wondered, if I could do that in C. That was, what got me there.
Next step would be to get that colour (white on blue). The control codes won't be the answer, I guess. :)
FFoulkes
Microbot
Posts: 161
Joined: Thu Feb 07, 2019 2:42 pm

Re: C on the Spectrum (z88dk)

Post by FFoulkes »

Consider this. "PRINT AT" again ... :) :

Code: Select all

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

void cls() {
    zx_cls(PAPER_WHITE);
}

void printCharAt(unsigned char x, unsigned char y, unsigned char c) {
   unsigned char *p;
   unsigned char *dat;
   long int temp;
   unsigned char i;
   temp = 15360 + c * 8;
   dat = (unsigned char *) temp;
   p = zx_cxy2saddr(x, y);
   for (i = 0; i < 8; ++i) {
      *p = *dat;
      dat++;
      p += 256;
   }
}

void printUdgAt(unsigned char x, unsigned char y, unsigned char *udg) {
   unsigned char *p;
   unsigned char i;
   p = zx_cxy2saddr(x, y);
   for (i = 0; i < 8; ++i) {
      *p = *udg;
      udg++;
      p += 256;
   }
}

unsigned char printAt(unsigned char x, unsigned char y, unsigned char *mystring) {
    unsigned char slen = strlen(mystring);
    if (x + slen > 32) {
        puts("Warning: Integer out of range. Nothing printed.");
        printf("x: %d  string: %s\n", x, mystring);
        return 1;
    }
    if (y > 21) {
        puts("Warning: Integer out of range. Nothing printed.");
        printf("y: %d\n", y);
        return 2;
    }
    unsigned char i;
    for (i=0; i < slen; i++) {
        printCharAt(x, y, mystring[i]);
        x++;
    }
    return 0;
}

int main() {
    cls();
    unsigned char a[] = "CONNECT 4";
    unsigned char udg1[] = {0, 24, 60, 126, 126, 60, 24, 0};
    printAt(12, 4, a);
    unsigned char i;
    unsigned char u;
    unsigned char fieldstart_x = 13;
    unsigned char fieldstart_y = 10;
    for (i=0; i<6; i++) {
        for (u=0; u<7; u++) {
            printUdgAt(fieldstart_x + u, fieldstart_y + i, udg1);
        } 
    }
    return 0;
}
FFoulkes
Microbot
Posts: 161
Joined: Thu Feb 07, 2019 2:42 pm

Re: C on the Spectrum (z88dk)

Post by FFoulkes »

Ok, I got colours now, too:

Code: Select all

#define BLACK   0
#define BLUE    1
#define RED     2
#define MAGENTA 3
#define GREEN   4
#define CYAN    5
#define YELLOW  6
#define WHITE   7

void setColour(unsigned char x, unsigned char y, unsigned char paper, unsigned char ink) {
    unsigned char *p = zx_cxy2aaddr(x, y); 
    *p = 8 * paper + ink;
}
dfzx
Manic Miner
Posts: 673
Joined: Mon Nov 13, 2017 6:55 pm
Location: New Forest, UK
Contact:

Re: C on the Spectrum (z88dk)

Post by dfzx »

FFoulkes wrote: Mon Feb 11, 2019 10:33 pm Ok, I got colours now, too:

Code: Select all

#define BLACK   0
#define BLUE    1
#define RED     2
#define MAGENTA 3
#define GREEN   4
#define CYAN    5
#define YELLOW  6
#define WHITE   7

void setColour(unsigned char x, unsigned char y, unsigned char paper, unsigned char ink) {
    unsigned char *p = zx_cxy2aaddr(x, y); 
    *p = 8 * paper + ink;
}
Yes, that's the idea! When do we get to play the final game? :)

Have a look in arch/zx.h though - all the colour macros are in there, plus other stuff you're likely to need.
Derek Fountain, author of the ZX Spectrum C Programmer's Getting Started Guide and various open source games, hardware and other projects, including an IF1 and ZX Microdrive emulator.
FFoulkes
Microbot
Posts: 161
Joined: Thu Feb 07, 2019 2:42 pm

Re: C on the Spectrum (z88dk)

Post by FFoulkes »

dfzx wrote: Tue Feb 12, 2019 9:46 amYes, that's the idea! When do we get to play the final game? :)
Ha ha. :) As I still can't get really comfortable with C (just as it always was), I have another idea now.
Please give me some time. I think, it will be really nice. I'll post about it, when it's ready.
FFoulkes
Microbot
Posts: 161
Joined: Thu Feb 07, 2019 2:42 pm

Re: C on the Spectrum (z88dk)

Post by FFoulkes »

This is where I went the last days:

viewtopic.php?f=9&t=1315
FFoulkes
Microbot
Posts: 161
Joined: Thu Feb 07, 2019 2:42 pm

Re: C on the Spectrum (z88dk)

Post by FFoulkes »

Hi, back again. ;) I'm trying to rewrite the Connect4-example with Spectrum's Basic now. It would be fast enough to animate the falling tile.
But it's rather slow (too slow), when checking the grid, if anybody has won.

So would it be possible to write a Basic/C-hybrid program? I thought, maybe I could poke the grid to defined memory addresses with Basic, then call a (compiled) C program, also in memory. Write the result of the check by the C program to another memory address, then fall back to Basic. Read the result with Peek there. Is that something, that could be done? Or is it a bad idea?
User avatar
Einar Saukas
Bugaboo
Posts: 3070
Joined: Wed Nov 15, 2017 2:48 pm

Re: C on the Spectrum (z88dk)

Post by Einar Saukas »

FFoulkes wrote: Wed Mar 13, 2019 11:48 amHi, back again. ;) I'm trying to rewrite the Connect4-example with Spectrum's Basic now. It would be fast enough to animate the falling tile.
But it's rather slow (too slow), when checking the grid, if anybody has won.
It can be done in Sinclair BASIC at a reasonable speed. Take a look:

http://reptonix.awardspace.co.uk/sincla ... t4-pvp.htm

It's available for download here:

https://spectrumcomputing.co.uk/index.p ... 6&id=25224

Notice it should be possible to make it run even faster, since this implementation above was optimized for size instead of speed...
FFoulkes
Microbot
Posts: 161
Joined: Thu Feb 07, 2019 2:42 pm

Re: C on the Spectrum (z88dk)

Post by FFoulkes »

Thanks. Interesting. Don't get how you do it. So you put the grid-data in a one-dimensional array. Does this help by finding diagonal tiles? I'd say, it can't work. But obviously it does. And it's even fast. So it must be ... magic.
Post Reply