Getting something simple to compile with z88dk

If you like transforming your statements into code, this is the place for you

Moderator: Programming Moderators

Post Reply
Bagpuss
Posts: 27
Joined: Tue Jun 06, 2017 9:13 pm

Getting something simple to compile with z88dk

Post by Bagpuss » Wed Apr 25, 2018 10:09 pm

OK, having a lot of issues with crashing code now.

As a test case I wrote a program "testit.c"

/* C source start */

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

int main()
{
zx_border(PAPER_WHITE);
puts ("Hello World");
return 0;
}

/* C source end */


Then compile it with
zcc +zxn -vn -clib=sdcc_iy -startup=0 -create-app -subtype=sna testit.c -o testit

On running it in cspect or zesarux I get Hello world. Some pixels centre of screen and a crash.

Its odd becuause trying C3D example and adding in a case '3' to exit returns me to command prompt OK but that is a vanilla speccy prog.

I'm wondering if its a +zxn type thing and I'm compiling next stuff incorrectly?

Alcoholics Anonymous
Posts: 511
Joined: Mon May 29, 2017 7:00 pm

Re: Getting something simple to compile with z88dk

Post by Alcoholics Anonymous » Thu Apr 26, 2018 2:27 am

Check that your includes are consistent:

You have "#include <arch/zx.h>" in there but are compiling for zx next (+zxn) so it should be "#include <arch/zxn.h>". This won't make a difference for the zx subset of zxn because both share a subset of the codebase but you won't have access to the entire zx next library.

The compile line is fine although I would add -SO3 (more aggressive peephole optimization to clean up the code a bit) and I like it rearranged a little :) but that won't make a difference:

zcc +zxn -vn -startup=0 -SO3 -clib=sdcc_iy testit.c -o testit -subtype=sna -Cz"--clean" -create-app

The optional extra -Cz"--clean" tells appmake to remove binary files it uses when making the sna. You may not want to do this if you want to see how big the binary is (the size of the *_CODE.bin file).

The problem with crashing is a problem with the sna format itself. z88dk tries to set up an sna that can return to basic but the sna is a 128k snapshot (or a 48k one) and contains system variables set up for a spectrum in 48k mode or 128k mode. So these will return to basic properly if run on a 48k/128k emulator or the Next in 48k/128k mode. However NextOS has more state in bank 8 that is not in the sna, has system variables set up differently and these can depend on the system itself (do you have two sd cards plugged in? how are they formatted? what sectors are currently buffered in divmmc memory?) So this state may be inconsistent with the snapshot and the system will crash when trying to return to basic. I was planning to have a NextOS-specific sna that tried to return to basic too but Garry reminded me of these issues so it's just not possible.

If running on NextOS it's better to have an infinite loop at the end of main or have the crt (the launch code that calls main) do an infinite loop on program exit. The latter is the best solution because this will also work with exit(), abort(), quickexit(), etc. You can control what the crt does with the CRT_ON_EXIT pragma (see https://www.z88dk.org/wiki/doku.php?id= ... figuration - the defaults mentioned are for the +z80 target not the +zx or +zxn which will have different defaults). This last bit could be saved for the future when you want to dig a bit deeper into the technical details of things.

Pretty soon a proper tap format for the Next will be in z88dk. This one will automatically generate a tap file that will load all the memory banks in the project from basic. Then returning to basic at program exit will be easy.

You can almost do that now if you compile with:

zcc +zxn -vn -startup=0 -SO3 -clib=sdcc_iy testit.c -o testit -create-app

Notice I removed "-subtype=sna" to do the default build which is a tap file. The tap maker is set up for the spectrum right now so it will only do the main binary and not load other memory banks if you put anything there but it can be used for quick experimentation. This one you will be able to return to Next basic on exit.

To load a tap file in NextOS you must:

.tapein -c foo.tap
LOAD "t:"
LOAD ""

At least I think that's how it goes! Maybe selecting it from the file browser works just as well. I don't have real hardware to try these things out unfortunately.

Of course, cspect can only do sna snapshots so this is not an option for cspect. But cspect does not emulate NextOS either and runs as a 128k machine in usr 0 mode so the snas z88dk generates should return to basic fine under cspect emulation.
Last edited by Alcoholics Anonymous on Thu Apr 26, 2018 2:52 am, edited 4 times in total.

Alcoholics Anonymous
Posts: 511
Joined: Mon May 29, 2017 7:00 pm

Re: Getting something simple to compile with z88dk

Post by Alcoholics Anonymous » Thu Apr 26, 2018 2:35 am

I just tried on cspect to verify the sna successfully returns to basic:
https://drive.google.com/file/d/1diSCUV ... sp=sharing

The compile line was:
zcc +zxn -vn -startup=0 -SO3 -clib=sdcc_iy testit.c -o testit -subtype=sna -Cz"--clean" -create-app

cspect was started with:
cspect -zxnext -mmc=.\

F2 was used to find and load "testit.sna"

The screenshot shows a 0 in the top left corner. This is because the snapshot effectively contains a "PRINT USR nnnn" command to start the program. The value printed will be the return value from the C program. Maybe it shouldn't be printed but that's how it is right now.

I should note that this ability to return to basic from snas was added 8 days ago so if you have something earlier than that you may want to update.

User avatar
varmfskii
Posts: 186
Joined: Fri Jun 23, 2017 1:13 pm
Location: Albuquerque, NM USA

Re: Getting something simple to compile with z88dk

Post by varmfskii » Thu Apr 26, 2018 2:54 am

Why are you using -clib=sdcc_iy? If you are using +zxn zcc defaults to using the zsdcc backend for the compiler.
Backer #2741 - TS2068, Byte, ZX Evolution

Alcoholics Anonymous
Posts: 511
Joined: Mon May 29, 2017 7:00 pm

Re: Getting something simple to compile with z88dk

Post by Alcoholics Anonymous » Thu Apr 26, 2018 3:04 am

varmfskii wrote:
Thu Apr 26, 2018 2:54 am
Why are you using -clib=sdcc_iy? If you are using +zxn zcc defaults to using the zsdcc backend for the compiler.
It does for +zxn:
https://github.com/z88dk/z88dk/blob/mas ... xn.cfg#L15

but this is not typical. Most targets, including +zx, default to the classic library and sccz80. So I prefer to see it in the compile line just to keep things straight when I switch to other targets.

The same compile line using sccz80 would look like this btw:

zcc +zxn -vn -startup=0 -clib=new testit.c -o testit -subtype=sna -Cz"--clean" -create-app

(the only difference is in the -clib=new and -SO3 is removed since that only applies to zsdcc and would be ignored if present)

and optimization turned up on the zsdcc compile:

zcc +zxn -vn -startup=0 -SO3 -clib=sdcc_iy --max-allocs-per-node200000 testit.c -o testit -subtype=sna -Cz"--clean" -create-app

(--max-allocs-per-node is increased to 200000 from the default 3000. --opt-code-size could be added too if size is important). This optimization setting will get the best code out but it is slow. sccz80 is very fast so it is being used by several people to get quick compiles during development and then zsdcc is used for optimized ones in large projects. If using both you should check in with the other compiler occasionally to make sure things are still working.

You can add "--list" to the compile to see the asm generated for the .c files (*.c.lis will be created) when a binary is made or you can stop at translation to asm with "-a" which will create *.c.asm. These latter files are valid *.asm files so could be hand edited and used to compile the project instead of the .c but this is only for masochists :)

Alcoholics Anonymous
Posts: 511
Joined: Mon May 29, 2017 7:00 pm

Re: Getting something simple to compile with z88dk

Post by Alcoholics Anonymous » Thu Apr 26, 2018 2:59 pm

I had a chance to try the sna under NextOS emulation using zesarux this morning and it does seem to return to basic fine, to my surprise. It looks like NextOS places the system in a kind of 48k or 128k mode when running the snapshot. So I think you may just have an older version of z88dk that does not set up the sna to return to basic.

I updated the download link ( https://drive.google.com/file/d/1diSCUV ... sp=sharing ) by adding a single file "testit.asm" which can be used to force the creation of a 128k snapshot instead of a 48k one.

z88dk will generate a 48k snapshot if no 128k memory is used. So to force a 128k snapshot, I've added "testit.asm" so that a single byte is placed in BANK_6.

48k snapshot (without "testit.asm"):
zcc +zxn -vn -startup=0 -clib=new testit.c -o testit -subtype=sna -Cz"--clean" -create-app

128k snapshot (with "testit.asm"):
zcc +zxn -vn -startup=0 -clib=new testit.c testit.asm -o testit -subtype=sna -Cz"--clean" -create-app

Both are returning to basic in cspect and zesarux.

There's a small fix to your program too. "zx_border(PAPER_WHITE)" should be "zx_border(INK_WHITE)" since the border colour is determined by the bottom 3 bits in port 0xfe (ie 0-7 like ink values).

User avatar
SevenFFF
Posts: 221
Joined: Mon Jun 05, 2017 5:30 pm
Location: USA

Re: Getting something simple to compile with z88dk

Post by SevenFFF » Thu Apr 26, 2018 3:33 pm

Would you be able to link your two testit.sna files please, Alvin?
Robin Verhagen-Guest
SevenFFF / Threetwosevensixseven / colonel32
NXtel Spectron 2084blog

Alcoholics Anonymous
Posts: 511
Joined: Mon May 29, 2017 7:00 pm

Re: Getting something simple to compile with z88dk

Post by Alcoholics Anonymous » Thu Apr 26, 2018 3:53 pm

Both 48k and 128k snapshots are in the link :)

User avatar
SevenFFF
Posts: 221
Joined: Mon Jun 05, 2017 5:30 pm
Location: USA

Re: Getting something simple to compile with z88dk

Post by SevenFFF » Thu Apr 26, 2018 4:28 pm

Aha, thanks!
Robin Verhagen-Guest
SevenFFF / Threetwosevensixseven / colonel32
NXtel Spectron 2084blog

Bagpuss
Posts: 27
Joined: Tue Jun 06, 2017 9:13 pm

Re: Getting something simple to compile with z88dk

Post by Bagpuss » Thu Apr 26, 2018 5:06 pm

Hi,
Thanks for all the help. Trying the google docs example, it runs fine in cspect. Trying to compile myself results in a crash. I used the nightly build from the 17th so I'll get a new nightly build.

Post Reply