STANDARD LIBRARY (stdlib.h)

Header {z88dk}/include/stdlib.h
Source {z88dk}/libsrc/stdlib
Include #include <stdlib.h>
Linking n/a
Compile n/a
Comments These library functions are compiled as part of each target’s implicit library.

These are utility functions defined as part of the C standard. Some functions have been modified to better suit the z80 and some functions have been added that are useful specifically on z80 machines. The set of standard functions is nearly complete, with the float/double to string conversions missing.

General

Most text reproduced here from another source.1)

int atoi(char *s)

Converts s into an integer.

char *itoa(char *s, int n)

Writes the integer n into the ascii string s, terminating with a ‘\0’ character.

In 1.9 changes to int itoa(int n, char *s, unsigned char radix)

char *utoa(char *s, unsigned int n)

Writes the unsigned integer n into the ascii string s, terminating with a ‘\0’ character.

In 1.9 changes to int utoa(unsigned int n, char *s, unsigned char radix)

long atol(char *s)

Converts s into a long.

char *ltoa(char *s, long n)

Uses EXX

Writes the long n into the ascii string s, terminating with a ‘\0’ character.

In 1.9 changes to int ltoa(long n, char *s, unsigned char radix)

char *ultoa(char *s, unsigned long n)

Uses EXX

Writes the unsigned long n into the ascii string s, terminating with a ‘\0’ character.

In 1.9 changes to int ultoa(unsigned long, char *s, unsigned char radix)

long strtol(char *s, char **endp, int base)

Uses AF’, EXX, IX

strtol converts the prefix of s to long, ignoring leading white space; it stores a pointer to any uncoverted suffix in *endp unless endp is 0. If base is between 2 and 36, conversion is done assuming that the input is written in that base. If base is zero, the base is 8, 10, or 16; leading 0 implies octal and leading 0x or 0X hexadecimal. Letters in either case represent digits from 10 to base-1; a leading 0x or 0X is permitted in base 16. No overflow is detected.

unsigned long strtoul(char *s, char **endp, int base)

Uses AF’, EXX, IX

strtoul is the same as strtol except that the result is unsigned long. No overflow is detected.

int rand(void)

rand returns a pseudo-random integer in the range 0 to RAND_MAX, which is at least 32767. A seed variable is supplied by the startup code that can be replaced on some platforms by declaring an “int std_seed” (or equivalent) somewhere within your project and using the pragma #pragma output HAVESEED

void srand(unsigned int seed)

srand seeds the seed for a new sequence of pseudo-random numbers. The seed variable is transparently supplied by the platform’s startup code but can be replaced on some platforms as discussed under rand() above.

void *calloc(int nobj, int size)

calloc returns a pointer to space for an array of nobj objects, each of size size, or 0 if the request cannot be satisfied. The space is initialized to zero bytes. z88dk requires the program to initialize the heap prior to its use. See the malloc library description for more details. In addition some targets (currently the z88) can support a larger than 64k memory model using the far memory model.

void *malloc(int size)

malloc returns a pointer to space for an object of size size, or 0 if the request cannot be satisfied. The space is uninitialized. z88dk requires the program to initialize the heap prior to its use. See the malloc library description for more details. In addition some targets (currently the z88) can support a larger than 64k memory model using the far memory model.

void *realloc(void *p, int size)

realloc changes the size of the object pointed to by p; it does nothing if p is 0. p must be a pointer to space previously allocated by calloc, malloc or realloc. z88dk requires the program to initialize the heap prior to its use. See the malloc library description for more details. In addition some targets (currently the z88) can support a larger than 64k memory model using the far memory model.

void free(void *p)

free deallocates the space pointed to by p; it does nothing if p is 0. p must be a pointer to space previously allocated by calloc, malloc or realloc. See the malloc library description for more details. In addition some targets (currently the z88) can support a larger than 64k memory model using the far memory model.

void abort(void)

Equivalent to exit(15)

void exit(int status)

exit causes normal program termination. atexit functions are called in reverse order of registration, open files are flushed, open streams are closed, and control is returned to the environment. How status is returned to the environment is implementation dependent, but zero is taken as successful termination. The values EXIT_SUCCESS and EXIT_FAILURE may also be used.

int atexit(void (*fcn)(void))

atexit registers the function fcn to be called when the program terminates normally; it returns non-zero if the registration cannot be made.

void sleep(int)

Wait for a period of time measured in seconds.

int getopt(int nargc, char **nargv, const char *ostr)

Not ROMable.

void l_bsearch(void *key, void *base, unsigned int n, char (*cmp)(void *keyval, void *datum))

Uses IX, IY

l_bsearch performs a binary search on base[0]...base[n-1] for an item that matches *key. The function cmp must return negative if its first argument (the search key) is less than its second (a table entry), zero if equal, and positive if greater. Items in the array base must be in ascending order. bsearch returns a pointer to the matching item or 0 if none exists.

l_bsearch differs from the standard library’s bsearch in the assumption that all items in the base array are two bytes in size. This means you can search an array of integers, two-byte chars or pointers to objects. The general case specified by the library would negatively impact on z80 performance.

void l_qsort(void *base, size_t n, char (*cmp)(void *keyval, void *datum))

Uses AF’, IX, IY

l_qsort sorts into ascending order an array base[0]...base[n-1] of objects. The comparison function cmp is as in bsearch.

l_qsort differs from the standard library’s qsort in the assumption that all items in the base array are two bytes in size. This means you can sort an array of integers, two-byte chars or pointers to objects. The general case specified by the library would negatively impact on z80 performance.

Although this implementation of quicksort is the so-called iterative version, it can still require stack space proportional to the number of elements being sorted in degenerative cases. For this reason, on small 8-bit machines like the z80, it is usually preferable to use a heapsort which is similarly quick but does not require any additional memory to perform the sort. You can find a heapsort implementation in the abstract data types library described under the adt_HeapExtract() function.

int abs(int n)

Returns the absolute value of its integer argument.

long labs(long n)

Returns the absolute value of its long argument.

Non-Standard Additions

unsigned char inp(unsigned int port)

Reads a 16-bit i/o port and returns the result. The header includes a macro version M_INP(PORT) that will generate inline assembler for the same purpose. The PORT argument in the macro must resolve to a constant so the macro cannot act as a replacement for the function call in all circumstances.

void outp(unsigned int port, unsigned char byte)

Writes the byte to the 16-bit i/o port. The header includes a macro version M_OUTP(PORT,BYTE) that will generate inline assembler for the same purpose. Both arguments to the macro must resolve to constants so the macro cannot act as a replacement for the function call in all circumstances.

void delay(unsigned int tstates)

An accurate delay measured in z80 T-states (cycles). tstates must be at least 160 (141 in 1.9). The delay time includes the CALL to and RET from the function but does not include any time required by the compiler to collect the tstates parameter. If tstates is a constant this extra time would be 10 cycles.

void *swapendian(void *addr)

Returns the result of swapping the most significant and least significant bytes in the address passed as parameter.

void bpoke(void *addr, unsigned char byte)

Write the byte into the address specified. The header includes a macro version M_BPOKE(ADDR,BYTE) that will generate inline assembler for the same purpose. Both arguments to the macro must resolve to constants so the macro cannot act as a replacement for the function call in all circumstances.

void wpoke(void *addr, unsigned int word)

Write the word to the address specified. The word will be written in the usual z80 little endian way. The header includes a macro version M_WPOKE(ADDR,WORD) that will generate inline assembler for the same purpose. Both arguments to the macro must resolve to constants so the macro cannot act as a replacement for the function call in all circumstances.

unsigned char bpeek(void *addr)

Reads the byte stored at the address specified and returns the result. The header includes a macro version M_BPEEK(ADDR) that will generate inline assembler for the same purpose. The macro argument must resolve to a constant so the macro cannot act as a replacement for the function call in all circumstances.

unsigned int wpeek(void *addr)

Reads the little endian word stored at the address specified and returns the result. The header includes a macro version M_WPEEK(ADDR) that will generate inline assembler for the same purpose. The macro argument must resolve to a constant so the macro cannot act as a replacement for the function call in all circumstances.

unsigned int isqrt(unsigned int n)

Return the integer square root of n.

Z88 Only

int system(char *s)

Executes a CLI command, details of the format of the supplied string can be found at http://www.worldofspectrum.org/z88forever/dn327/stdiocli.htm

int csleep(int centiseconds)

Wait for a period of time measured in hundredths of seconds.

1) Descriptions for standard functions taken from The C Programming Language, 2nd Edition by Kernighan and Ritchie
 
library/stdlib.txt · Last modified: 2008/06/24 10:25 by aralbrec
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki