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?
sdcc and time.h
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
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
As dom mentioned there are two independent C libraries.berk wrote:Ok, but it is available for sccz80 and so it makes existing projects for sccz80 incompatible with sdcc.
It is quite pity :-(
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);
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
I understand but may be I would prefer portability in this case :-)"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.
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.Classic compile with sdcc:
zcc +zx -vn -SO3 --max-allocs-per-node200000 --reserve-regs-iy -compiler=sdcc test.c -o test -lndos
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 ?
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 ?
For source code compatibility in the larger world, time(0) should probably be present. I just haven't got around to it yet
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.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.
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.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.
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.
Re: sdcc and time.h
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
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