2013-11-03

2013-11-03 Doom Continued, and printf upgrade.

With Doom reaching the main() function, it's time to start working through it making sure everything is implemented and working.

Bearing in mine that sound and video are a way off yet, the short term goal is getting Doom to display its opening text, loading what files it wants, then "running", then exiting.

So the next thing it wants is a WAD file.  Doom searches to see which .wad files are available, but for now I'll just hardcode the path to the .wad file.

I seem to getting a strange problem with printf().  It's printing text to the screen.  That may not sound that odd, except for the fact that I haven't written it yet.  It appears that GCC is optimising calls to printf() with a constant string to be calls to puts() instead (which is written) so that why some of them were working.  (I thought -O0 turned off optimisations?)

V_Init: allocate screens.
M_LoadDefaults: Load system defaults.
Z_Init: Init zone memory allocation daemon.
W_Init: Init WADfiles.

The next thing that Doom needs is a sprintf() function which I don't have.  Originally, my printf() routine was a simple routine written by someone else (one of the few pieces of code my OS has used that I didn't write).  It was designed for an embedded system and didn't support all the features and options of printf().

Not long ago I sat down and wrote my own version, adding a lot more support for the various tokens, but it would still only output to the screen.  This version of the function is still used in the kernel as kstdio_printf() which gets called with debug messages all over the place.

So today I've taken that function and extended it once more.  After researching variadic functions and how they are handled in C, I've written implementations for the eight different version of printf():

  • fprintf() - variadic arguments, outputting to a stream
  • printf() - variadic arguments, outputting to stdout (a special case of above).
  • sprintf() - variadic arguments, outputting to a buffer.
  • snprintf() - variadic arguments, outputting to a buffer with a limit.
  • vfprintf() - va_list arguments, outputting to a stream.
  • vprintf() - va_list arguments, outputting to stdout (a special case of above).
  • vsprintf() - va_list arguments, outputting to a buffer.
  • vsnprintf() - va_list arguments, outputting to a buffer with a limit.

All of these are actually serviced by a single internal printf function which is capable of performing them all which reduces code duplication.

With all of these in place, I think that's good place to leave it for today.


No comments:

Post a Comment