sdcc and time.h

ZX80, ZX 81, ZX Spectrum, TS2068 and other clones
Post Reply
berk
Member
Posts: 37
Joined: Wed Dec 24, 2014 9:46 am

sdcc and time.h

Post by berk »

Hello,

sdcc together with -create-app works well. But if I try to compile my project which contains srand(time(NULL) % 37); I get following error:

time.h: No such file or directory

What am I doing wrong?
User avatar
dom
Well known member
Posts: 1235
Joined: Sun Jul 15, 2007 10:01 pm

Post by dom »

The Spectrum port has never had a time() function available - there's no real time clock available.

With that in mind, I suspect Alvin never created time.h for the new library.

In classic, only CPM, z88, Sharp X1 and Sprinter have time() available.
berk
Member
Posts: 37
Joined: Wed Dec 24, 2014 9:46 am

Post by berk »

Ok, but it is available for sccz80 and so it makes existing projects for sccz80 incompatible with sdcc.
It is quite pity :-(
User avatar
dom
Well known member
Posts: 1235
Joined: Sun Jul 15, 2007 10:01 pm

Post by dom »

The project has obviously ended up in a confusing state.

There's two compilers (sccz80 and sdcc), and two libraries (classic and new).

Here's a broad current state of play:

* Both compilers work with both libraries (*)
* Only classic supports a broad-range of platforms, only new has very fine grained control of features.
* Only classic supports file IO.
* Only new supports floating point with sdcc. Only classic supports using native floating point libraries.
* Only sdcc supports 64 bit integers.

So you should be able take your code that worked with sccz80+classic tweak it so it's standards compliant and compile with sdcc+classic.

(*) The following classic library haven't been converted to work with sdcc (yet): tape loading routines (ZX +related), some graphics.h routines, sp1, Xircom Rex, OZ700 SDK, OSCA SDcard, qsort, bsearch, memopi, memopd, *scanf
berk
Member
Posts: 37
Joined: Wed Dec 24, 2014 9:46 am

Post by berk »

Thank you - now it is more clear to me.
User avatar
dom
Well known member
Posts: 1235
Joined: Sun Jul 15, 2007 10:01 pm

Post by dom »

No worries, I think we should work an announcement that has the same information.
alvin
Well known member
Posts: 1872
Joined: Mon Jul 16, 2007 7:39 pm

Post by alvin »

berk wrote:Ok, but it is available for sccz80 and so it makes existing projects for sccz80 incompatible with sdcc.
It is quite pity :-(
As dom mentioned there are two independent C libraries.

If the compile line has "-clib=new", "-clib=sdcc_ix" or "-clib=sdcc_iy" you are using the new c library.
The available headers can be seen here: http://z88dk.cvs.sourceforge.net/viewvc ... MENT/sdcc/
And there is a small amount of documentation for these headers here: http://www.z88dk.org/wiki/doku.php?id=t ... er_listing
Both sccz80 and sdcc have equal access to the new c library.

"time.h" has not been added yet because very few targets have realtime clocks available. For the zx, the classic lib is using the system variable FRAMES as a clock (it is incremented every 1/50s after power on). However because FRAMES starts at zero on power up, using time(0) as seed won't produce good results, especially in an emulation environment. Most often, people will double click on an executable (on the zx, a tap) and the emulator will instantly load it and run it. FRAMES will always be the same value and so will time(0) so using srand(time(0)) to set seed will always produce the same random number sequence on each run.

What I've been doing instead is gathering some entropy from the time it takes the user to start a game from a menu. A simple example might be:

Code: Select all

#include <input.h>
#include <stdlib.h>
#include <stdio.h>

int i;

printf("Any key to continue...\n\n");
   
in_wait_nokey();
for (i=0; !in_test_key(); ++i) ;
in_wait_nokey();

srand(i);
The functions "in_wait_nokey()" and "in_test_key()" come from input.h and are available across all platforms in the new c lib (keep in mind the new c lib only supports zx, cpm and embedded targets currently).

There are some things in the new c lib that might be interesting to your specific project (I think it was an adventure?), in particular it can support multiple terminal windows (maybe one for display and one for input), it has a proportional font driver and text editing is built into the driver. But, as mentioned, only three targets are supported as of now.


It is also possible to compile with sdcc using the classic c lib. This is being added now and a lot will probably work but no information has been given on how to do that yet.

Here's probably the first time it's been shown:

Classic compile with sccz80:
zcc +zx -vn -O3 test.c -o test -lndos

Classic compile with sdcc:
zcc +zx -vn -SO3 --max-allocs-per-node200000 --reserve-regs-iy -compiler=sdcc test.c -o test -lndos
berk
Member
Posts: 37
Joined: Wed Dec 24, 2014 9:46 am

Post by berk »

"time.h" has not been added yet because very few targets have realtime clocks available. For the zx, the classic lib is using the system variable FRAMES as a clock (it is incremented every 1/50s after power on). However because FRAMES starts at zero on power up, using time(0) as seed won't produce good results, especially in an emulation environment. Most often, people will double click on an executable (on the zx, a tap) and the emulator will instantly load it and run it. FRAMES will always be the same value and so will time(0) so using srand(time(0)) to set seed will always produce the same random number sequence on each run.
I understand but may be I would prefer portability in this case :-)
Classic compile with sdcc:
zcc +zx -vn -SO3 --max-allocs-per-node200000 --reserve-regs-iy -compiler=sdcc test.c -o test -lndos
Thank you for example - I could not find it. In wiki there is info that only new library is supported with sdcc at the moment.
stefano
Well known member
Posts: 1570
Joined: Mon Jul 16, 2007 7:39 pm

Post by stefano »

In 99% of the cases I met "time.h" was used to set the rand() seed which, not mentioning the "emulator" related issue (it is so frequent that in a Spectrum emulator forum I read about a hack to shift the clock randomly), costs a lot of space in libraries, code and structures for such a simple task.
The ancient BDS C had a special function printing a given text message and waiting for a keypress while garbling the random seed.
In the legacy z88dk libs I tried many times to catch the srand(time(...)) and similar calls and convert them to something smaller but every program has its way.
Probably it is worth to edit the code and to alter it manually.. which makes the old BDS C trick an interesting option !
Is it so ugly ?
alvin
Well known member
Posts: 1872
Joined: Mon Jul 16, 2007 7:39 pm

Post by alvin »

For source code compatibility in the larger world, time(0) should probably be present. I just haven't got around to it yet :)
Thank you for example - I could not find it. In wiki there is info that only new library is supported with sdcc at the moment.
Yes this is fairly recent-ish (the last commit for this was only a couple of days ago!) but a lot of things should compile even now.
berk
Member
Posts: 37
Joined: Wed Dec 24, 2014 9:46 am

Post by berk »

In 99% of the cases I met "time.h" was used to set the rand() seed which, not mentioning the "emulator" related issue (it is so frequent that in a Spectrum emulator forum I read about a hack to shift the clock randomly), costs a lot of space in libraries, code and structures for such a simple task.
You are absolutely right - 8bit computers represent special area with limited amount of ram and other resources and so it is hard or even impossible to implement all ISO C which consist about 24 header files.

But in my opinion it would be nice to have at least these (ctype.h, stdio.h, math.h, stdlib.h, string.h, time.h, stdarg.h).

I dont think solution you proposed is ugly - it is pretty efficient and correct - no discussion.

The question is how far is it still efficient to implement standard c functions and what functions are already out of scope in this area of 8-bit computers.
I dont know.
cborn
Member
Posts: 25
Joined: Tue Oct 06, 2020 7:45 pm

Re: sdcc and time.h

Post by cborn »

Hello
i once tried to calculate the correct real time for ZX Spectrum:
https://worldofspectrum.org/forums/disc ... ent_499765
i did not manage to make a good routine, but my theory itself was good enough i think.
if you follow the link you will find my ASM attempt down the thread
Post Reply