News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu

programming in c using cpcrslib and z88dk

Started by arnoldemu, 15:29, 19 February 10

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

arnoldemu

First I program C for a living so I thought it was going to be easy to write a program for the cpc using z88dk and cpcrslib.

No. I must have done something wrong because it has been a real battle so far. I am finally getting there and making a game.. more news when it is done.

1. Make sure you install z88dk to c:\z88dk because everything assumes it to be there. Install it to "Documents and Settings" and you'll end up with no fun.

2. z88dk is not full C. So don't expect everything to work as you expect.

3. In z88dk v1.9 if you want to have opt files then you can't have a final executable.
e.g. using -a to generate opt files, stops -zorg from working. At least i can't get it to work.

If you want to use -zorg, then you have to use zcc for compiling AND linking.
You can't use zcc for building c files and then use zasm to link without using your own crt0 file and/or hand defining zorg yourself.

So if you get "error in MODULE ... at line " type errors, then you have to generate .opt files to see the final asm, and then you can work out what the error really refers to in your original code.

3. If you want to use asm's "binary" to include a file directly into the source e.g.


#asm
_Tiles:
binary "bin\tiles.bin"
#endasm


Expect problems. I thought it was going to be easy, but instead I got an error at link time, to a line which only exist in the .opt file.

In the end it was far easier to create a .asm file wrapper which is built into a module.

e.g.
font.asm:

XDEF first_char
XLIB cpc_Chars

_cpc_chars:
BINARY "bin\font.bin"

This works nicely.

4. You want to use cpcrslib? Ok, make sure you always build it and send the cpcrslib.lib to c:\z88dk\lib and the cpcrslib.h to c:\z88dk\include. It is much easier. The current build of the lib doesn't seem to be up to date. Putting the files into the lib and include dir makes things much easier. You could try using the new "-I" and "-L" to try and point it to a different directory, but z88dk doesn't like it much especially if the directory path has spaces in it.

5. Compiling stuff for cpcrslib looks like this (most download "sources" don't have makefiles or similar so you have to guess how they made their files):

zcc +cpc -zorg=8192 -O3 -create-app -make-app -Wall -unsigned -lcpcrslib -o bin\game font.asm tiles.asm cpcgame.c

6. You can use -o to specify an output file but it actually ends up as a raw binary without amsdos header. zcc actually also creates a .cpc file which is probably the one you really want especially if you're using cpcxfs to put your files into a dsk.

7. I build my code using a batch file but z88dk doesn't seem to set error codes correctly so:

IF errorlevel 0 goto
does not seem to work....

My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

mr_lou

:) I recognize some of the problems from when we developed Sort'em, but I don't remember we had that many problems. Of course, Sort'em is only a simple game, so maybe that's why we never encountered that many problems.

But great to hear you're working on a game. :)

I hope it'll run on a standard CPC464 (with 128k if it must be).

Cholo

Just some usefull links.

Better get the latest version of cpcrlib from:
http://www.amstrad.es/programacion/c/index.php

Also its worth downloading the Mariano game as it contains full sourcecode+files. I did manage to compile that game myself using the included files so it works allright.

http://www.z88dk.org/forum/
.. didnt seem to include anything amstrad-usefull back when i looked at it, so better get the latest version of cpcrlib manually.

arnoldemu

Quote from: mr_lou on 21:56, 19 February 10
:) I recognize some of the problems from when we developed Sort'em, but I don't remember we had that many problems. Of course, Sort'em is only a simple game, so maybe that's why we never encountered that many problems.

But great to hear you're working on a game. :)

I hope it'll run on a standard CPC464 (with 128k if it must be).
:( I think I am just unlucky.

But anyway, when the game is done full sources and build files will be released. I hope it will fit into 64k, but already it will not be easy.

I am making slow progress.
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

arnoldemu

Quote from: Cholo on 13:33, 21 February 10
Just some usefull links.

Better get the latest version of cpcrlib from:
http://www.amstrad.es/programacion/c/index.php

Also its worth downloading the Mariano game as it contains full sourcecode+files. I did manage to compile that game myself using the included files so it works allright.

http://www.z88dk.org/forum/
.. didnt seem to include anything amstrad-usefull back when i looked at it, so better get the latest version of cpcrlib manually.
Thanks. I've got latest lib, so that is good. Ahh I don't think I downloaded Mariano. I did download some others.

Well now I have the frontend UI working good, and all the graphics converted too.
It crashes when it goes into game but I am sure I can fix that soon.
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

arnoldemu

Quote from: arnoldemu on 11:01, 22 February 10
Thanks. I've got latest lib, so that is good. Ahh I don't think I downloaded Mariano. I did download some others.

Well now I have the frontend UI working good, and all the graphics converted too.
It crashes when it goes into game but I am sure I can fix that soon.
Question: Why are the parameters for cpc_PutSpr for width and height defined as char *, should they not be defined as int? Or is it down to the actual size of int? Should it not be 16-bit on z80? or should I use "unsigned short"?

Anyways I fell foul of something that works in standard C but not in z88dk and it probably gave me a warning which I didn't read ;)

if you want to have external asm data:

You need this:


XLIB Tiles
XDEF _Tiles

._Tiles
  BINARY "Tiles.bin"


and in your code you need this:

extern unsigned char Tiles[];

if you do:

extern unsigned char *Tiles;

this doesn't produce the correct code although this works fine in C.

So something else to watch out for.



Ok, I fell foul of not reading the warnigns
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

arnoldemu

#6
OK I fell foul again of compiler switches:

1. -make-app . What do this really do?

Effectively they are used for creating rom based software, or software where you want a shared area for variables.
You need to use vardefs here. If you don't then any variables which are assigned a value will be created, given a valid address based on zorg, and will appear in the map.
Any vars which do not have values set will be located relative to the value defined by vardefs which defaults to 0. In addition they don't end up in the map if vardefs is not defined. So you end up getting variables stomping over the rst vectors. Not good.

And the compiler doesn't give you any warning or errors either.

So unless you are planning to create rom based software, avoid these switches.

2. Note: The cpcrslib uses self modifying code, so is not suitable for rom based games.

3. If you want to create a normal CPC executable with header and you want to have your code and variables in ram and all based off zorg, this is the command line you need to use:

zcc +cpc -zorg=8192 -O3 -create-app -Wall -unsigned -lcpcrslib -o bin\game font.asm tiles.asm cpcgame.c

+cpc is important because it also tells the compiler to generate the binary with ".cpc" extension which has the amsdos header you need.
-create-app is needed to force generation of ".cpc" file.
-zorg defines start address of code (note crt0 comes first)
others define programs and libraries

4. z88dk doesn't seem to recognise const keyword sometimes. i tried this: static const unsigned char to indicate some data that was constant (e.g. read only) but it didn't like it.

My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

redbox

Quote from: arnoldemu on 10:29, 24 February 10
2. Note: The cpcrslib uses self modifying code, so is not suitable for rom based games.

Poo.  I have been thinking about using cpcrslib and reading your posts, but now don't think I will use it because I will cry when my game doesn't run on the first homebrew cartridge  8)

arnoldemu

Quote from: redbox on 14:53, 24 February 10
Poo.  I have been thinking about using cpcrslib and reading your posts, but now don't think I will use it because I will cry when my game doesn't run on the first homebrew cartridge  8)
Well it may be better to consider using crocolib.

http://crocolib.sourceforge.net

I don't know much about this, other than it supports cpc+ features (cpcrslib is cpc only).

I think older versions used z88dk, but newer ones may use sdcc?

http://sdcc.sourceforge.net

Now that I have overcome my problems, I am now able to develop the game at a much faster rate.
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

mr_lou

I think someone should make a cpc development lib where all methods are similar to those of Java ME (used to code mobile games). Because then CPC devs could easily port their games to mobile phones, meaning it would actually be possible to earn a little pocket money with them.
And devs that do mobile games could have their games ported to the CPC, e.g. by some of us. Would be a win/win situation.  8)

Another option could be Qt (http://qt.nokia.com). Code some support for the CPC for Qt. :) Then when you've coded your game for the CPC using Qt, you can also release for a ton of other platforms.

Octoate

Quote from: arnoldemu on 15:16, 24 February 10
Well it may be better to consider using crocolib.
http://crocolib.sourceforge.net

I don't know much about this, other than it supports cpc+ features (cpcrslib is cpc only).
I think older versions used z88dk, but newer ones may use sdcc?

http://sdcc.sourceforge.net

Now that I have overcome my problems, I am now able to develop the game at a much faster rate.
You are right. The older version of the Crocolib used Z88dk and the newer one uses SDCC. When I tested the library I had the feeling that it is to specialized for my purposes.
I also played a bit around with the SDCC compiler (without any lib) and ported the UnExo and Pucrunch example of the cpcrslib to the SDCC compiler. Well... I don't like the assembler syntax of SDCC. It is soooooooooo unfamiliar. The cool thing about SDCC is that it is a full ANSI C compatible compiler.
If anyone is interested in this both examples I can upload it to the Wiki share :).

@mr_lou: I remember that you already mentioned such a library before somewhere here in the forums and I really like the idea. It would be great to e.g. use Netbeans with its integrated map editor for such a cross-platform development.
--

arnoldemu

Quote from: Octoate on 17:58, 24 February 10
You are right. The older version of the Crocolib used Z88dk and the newer one uses SDCC. When I tested the library I had the feeling that it is to specialized for my purposes.
I also played a bit around with the SDCC compiler (without any lib) and ported the UnExo and Pucrunch example of the cpcrslib to the SDCC compiler. Well... I don't like the assembler syntax of SDCC. It is soooooooooo unfamiliar. The cool thing about SDCC is that it is a full ANSI C compatible compiler.
If anyone is interested in this both examples I can upload it to the Wiki share :).

@mr_lou: I remember that you already mentioned such a library before somewhere here in the forums and I really like the idea. It would be great to e.g. use Netbeans with its integrated map editor for such a cross-platform development.
I think these are useful especially in a area related to C programming on the wiki. Please upload them.
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

arnoldemu

Quote from: mr_lou on 16:24, 24 February 10
I think someone should make a cpc development lib where all methods are similar to those of Java ME (used to code mobile games). Because then CPC devs could easily port their games to mobile phones, meaning it would actually be possible to earn a little pocket money with them.
And devs that do mobile games could have their games ported to the CPC, e.g. by some of us. Would be a win/win situation.  8)

Another option could be Qt (http://qt.nokia.com). Code some support for the CPC for Qt. :) Then when you've coded your game for the CPC using Qt, you can also release for a ton of other platforms.
I am not familiar with these environments. I have done very little programming with Java ME.
Please would you explain more how it works?

For example how does Qt work? I know I could read the docs from the link, but a brief summary will help.
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

Octoate

Quote from: arnoldemu on 10:35, 25 February 10
I think these are useful especially in a area related to C programming on the wiki. Please upload them.
I saw that I already uploaded it to the Wiki share, because another user was intersted some time before (because SDCC was to complex for him, so he switched to the CCZ80 compiler). You can get my simple test here. In this ZIP you will finde to folders: one for the creation of the 'sdcclib.lib" library with the two uncompress routines of the cpcrslib and the other directory with the UnExo example of the cpcrslib. You can create a new version of the 'sdcclib.lib' with the 'makelib.bat' in the directory. You have to copy the previously created 'sdcclib.lib' to this directory and can execute the 'make.bat' to compile the file. You will then get a binary file 'code.bin' without AMSDOS header and code location 0x2800.

The data transformation for the image is not included in this example. I couldn't manage to include a binary file, so I created a simple file 'tonteria.z80' which includes the byte definitions. Not an elegant method, but maybe some of you know how to include this.
--

mr_lou

Quote from: arnoldemu on 10:36, 25 February 10
I am not familiar with these environments. I have done very little programming with Java ME.
Please would you explain more how it works?

For example how does Qt work? I know I could read the docs from the link, but a brief summary will help.

I don't know much about Qt. I only heard about it recently, although it's 15 years old. But from what I understand, you use the Qt framework to code your stuff, and then you can compile that same code to executables on several platforms.

arnoldemu

Correction:

"I build my code using a batch file but z88dk doesn't seem to set error codes correctly so:

IF errorlevel 0 goto
does not seem to work....
"
It does work:

This is what you need, works both for compile and link stages:


zcc +cpc -zorg=8192 -create-app -O3 -Wall -unsigned -lcpcrslib -o bin\game font.asm tiles.asm cpcgame.c -m
if %ERRORLEVEL% NEQ 0 goto :error

goto end

:error
pause
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

Octoate

Quote from: mr_lou on 12:18, 25 February 10
I don't know much about Qt. I only heard about it recently, although it's 15 years old. But from what I understand, you use the Qt framework to code your stuff, and then you can compile that same code to executables on several platforms.
QT is a very well designed C++ framework. I already used it for threading (only a console program) in one of my projects, where I implemented pattern recognition on an Opteron cluster system. However, I doubt that we can port a C++ framework to the CPC.
--

norecess

Hi guys,

I registered on CPC Wiki forum just to answer this thread.  :D

I'm NoRecess, and created 2 demos for the CPC made in C : Phat and Pheelone.

Phat was my first experience in C targeting the Z80. I used z88dk. I had to make many hacks to get it working for the CPC, especially because z88dk is made by people initially targeting the ZX Spectrum platform, and that CPC implementation of stdio etc. is only based on firmware routines (which was incompatible with the scope of programming demos).
Then for the Pheelone demo, I switched to SDCC. The toolkit is cleaner, but most importantly, it produces smaller Z80 code (for the same C counter-part).
None of these toolchain perfectly suits for the CPC, it's hard to get them working as expected at first. You often need to deal with some updates or third-party requirements (hex2bin, batch files, patch crt files, etc).

My advices & general notes :
- use SDCC over z88dk
- for the CPC, use ONLY C language for its syntax, not the libs which comes with the compiler, even if they look interesting at first.. You want to use C because it's more practical to design your program that way
- use C for all your inits, your "overall" big loop, your loading process, etc.
- don't use C for the "inner loops". Use inlined assembly instead
- C packs very well ;) think about splitting/packing your game/demo in several distinct parts if you can
- SDCC inlined asm syntax sucks ;) but it remains Z80, it takes few hours only to get used to the syntax
- make sure to know and understand where your memory allocation go.. there is a difference making a global variable static or not. Always look back the generated asm code
- read the doc coming with the compiler, the SDCC Wiki is excellent
- make your OWN experience and practice by yourself. Many people around simply do not know anything about C programming applied to embedded constraints
- you still have to use asm code, even if you are using C language

These are the SVN repository of Phat and Pheelone :
https://svn.sourceforge.net/svnroot/crocolib/trunk/phat
https://svn.sourceforge.net/svnroot/crocolib/trunk/pheelone

Finally, forget about the "general framework" for all purposes.. I simply can't work this way for the CPC. I completely failed 2 times, my "uber framework" called crocolib finally ended being shared code between parts of the Phat demo. I found myself more productive in Pheelone by writing code dedicated for it, and not having in mind that the code will be reused somewhere else..

Qt for the Cpc, Aahahha.... you guys are kidding?
The only thing that would eventually work is a SDL port for the CPC (reuse the H, implementation done on CPC, with many parts unavailable) - but once again, in the end you will obtain something very slow..

That was my thoughts about the topic!
Good luck! :)

PS. I don't give support to my previous productions..

redbox

This is all really interesting, but really has only demonstrated one thing... I should stick with assembler for the CPC  :(

norecess

Actually, I gained so much time thanks to the C usage.. For Phat and Pheelone, I prototyped most of the effects with Win32+SDL (with CPC constraints in mind), then when I was satisfied with the result I simply ported it to the CPC...
Basically, for an FX, it was 2 hours for the FX, 2 hours for the CPC port, and 2 more hours for the ASM optimization when needed..
Once you have your setup working as expected, it's really nice.

arnoldemu

Z88dk doesn't support chars properly:

e.g.
*ptr = '-';

This wants to use the maths library to convert to a double? basically if you don't link against maths library you get errors about DADD and IFIX etc. So to get around this problem you need to use decimal/hex number for char.

My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

norecess

Actually, I hate z88dk because of too many hacks in their implementation.
I would advise to use SDCC instead (which also produce smaller code).

Or wait for something better coming sooner or later..... (no, it's not teasing! or is it? :P)

arnoldemu

Quote from: norecess on 15:06, 10 March 10
Actually, I hate z88dk because of too many hacks in their implementation.
I would advise to use SDCC instead (which also produce smaller code).

Or wait for something better coming sooner or later..... (no, it's not teasing! or is it? :P)
I started with z88dk because I also wanted to use cpcrslib,but now I am beginning to think I should have chosen SDCC.

Something comming? I know what you are planning ;)
Yes, I looked on crocolib svn recently ;)
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

norecess

BTW, I moved the repository, crocolib is no more..

arnoldemu

Quote from: norecess on 15:44, 10 March 10
BTW, I moved the repository, crocolib is no more..
Would you p.m. the new location?
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

Powered by SMFPacks Menu Editor Mod