WDC Classic C Libs
Wednesday, May 14th, 2008I recently got a comment through the comments of my latest post. Gato seems to have problems using C libs provided with the WDC compiler. What are really those C libs and what are the functionnality they provide? There is plenty of website providing more info on those libs. Don’t hesitate to use Google for more info about them.
I will now show only a part of the C standart librairy : stdlib.h and string.h.
Stdlib.h – Commonly used Library Functions. Some example of functions :
int abs(int _j); - absolute value of integer int atoi(const char *_nptr); - convert a string to an integer long int atol(const char *_nptr); - convert a string to a long integer void exit(int _status); - normal exit & close files void free(void *_ptr); - causes the space pointed to by ptr to be deallocated. void ftoa(double _val, char *_buf, int, int); - convert floating point to ascii void *malloc(size_t _size); - allocate with block size int rand(void); - integer random numbers (0 – 32565) void *realloc(void *_ptr, size_t _size); - expand memory block void srand(unsigned int _seed); - seed integer random number generator
String.h – String conversion Functions and Memory Functions. Some example of functions :
void *memchr(const void *_s, int _c, size_t _n); - search memory for character int memcmp(const void *_s1, const void *_s2, size_t _n); - compare memory void *memcpy(void *_dst, const void *_src, size_t _n); - copy memory, byte access, allows overlap void *memmove(void *_dst,const void *_src, size_t _n); - move memory, byte access, allows overlap void *memset(void *_s, int _c, size_t _n); - fill a block of memory with a character char *strcat(char *_dst, const char *_src); - string concatenate int strcmp(const char *_s1, const char *_s2); - compare strings char *strcpy(char *_dst, const char *_src); - copy string char *strstr(const char *_s1, const char *_s2); - search for one string in another char *strtok(char *_s1, const char *_s2); - split string into tokens
You should use those carefuylly since they may not be perfectely optimised and then take a large space into your final ROM. So now ? Maybe a small example to illustrate all this. 3 simples steps : code , compiling and finally linking.
The code – main.c
#include <stdlib.h> #include <string.h> void main(void) { } void preInit(void) { char string[10] = "snes"; char test[10]; // fill with space memset(test, 32, 10); // screen A // fill half with dots memset(test, 46, 5); // screen B // copy with string memcpy(test, string, 4); // screen C while(1 == 1) { } } void IRQHandler(void) { }
Compiling
Two lines for compiling, please refer to the doc for the purpose of teh differnet parameters. The -MS is for specifying the model, it is important to specify when later you need to specify the lib model.
wdc816cc -wl -wp -sop -MS main.c
Linking
wdcln -HB -M21 -V -T -Pff -C008000,0000 -U0000,0000 -Avectors=FFE4,7FE4 -Aregistration_data=FFB0,7FB0 -N startupSnes.obj main.obj -LC:\65xx_FreeSDK\lib\cs -O test.smc
The cs lib is for the Model Small that we specified while compiling.
Some function don’t seems to work properly. By example, the rand function comming from stdlib.h always return the same value. The tested string function are working perfect and are not too badly optimised. Here are some screenshot from my debugger during the 3 step of execution (Screen A, B and C), click to enlarge :
As usual you can get the full archive to download here.
See ya Lint,
PS : shoot your comments as usual !