* Fixed va_args for the z80
[fw/sdcc] / device / lib / gbz80 / printf.c
index 33d7050ea4a996c7d7144e526e57cf5b4bb4f62d..346eb3f1ad3ed600a49b36356c30785684b63bbd 100644 (file)
@@ -2,27 +2,13 @@
     Again a stub - will use the std one later...
 */
 
+#include <stdarg.h>
 #include <stdio.h>
 
-#define NULL 0
+/* PENDING */
+#define NULL   0
 
-/* A hack because I dont understand how va_arg works...
-   sdcc pushes right to left with the real sizes, not cast up
-   to an int.
-   so printf(int, char, long)
-   results in push long, push char, push int
-   On the z80 the stack grows down, so the things seem to be in
-   the correct order.
- */
-
-typedef char * va_list;
-#define va_start(list, last)   list = (char *)&last + sizeof(last)
-#define va_arg(list, type)     *(type *)list; list += sizeof(type);
-
-typedef void EMIT(char c, void *pData);
-
-
-static void _printn(unsigned u, unsigned base, char issigned, EMIT *emitter, void *pData)
+static void _printn(unsigned u, unsigned base, char issigned, void (*emitter)(), void *pData)
 {
     const char *_hex = "0123456789ABCDEF";
     if (issigned && ((int)u < 0)) {
@@ -34,7 +20,7 @@ static void _printn(unsigned u, unsigned base, char issigned, EMIT *emitter, voi
     (*emitter)(_hex[u%base], pData);
 }
 
-static void _printf(const char *format, EMIT *emitter, void *pData, va_list va)
+static void _printf(const char *format, void (*emitter)(), void *pData, va_list va)
 {
     while (*format) {
        if (*format == '%') {
@@ -57,7 +43,7 @@ static void _printf(const char *format, EMIT *emitter, void *pData, va_list va)
                    break;
                }
            case 'x':
-               {
+               {
                    unsigned u = va_arg(va, unsigned);
                    _printn(u, 16, 0, emitter, pData);
                    break;
@@ -79,15 +65,24 @@ static void _printf(const char *format, EMIT *emitter, void *pData, va_list va)
     }
 }
 
+void putchar(char c);
+
 static void _char_emitter(char c, void *pData)
 {
+    /* PENDING: Make the compiler happy. */
+    pData = 0;
+
     putchar(c);
 }
 
-void printf(const char *format, ...)
+int printf(const char *format, ...)
 {
     va_list va;
     va_start(va, format);
 
     _printf(format, _char_emitter, NULL, va);
+    _printf(format, _char_emitter, NULL, va);
+
+    /* PENDING: What to return? */
+    return 0;
 }