* device/lib/mcs51/crtxclear.asm: pdata segment of 256 byte would not be cleared...
[fw/sdcc] / device / lib / printf_fast.c
index d05457eee6b53b16880c33e5d9c86e92480aa5ea..63a4308e2aee918ab0bf31a24609bc4f75668e09 100644 (file)
 /******************************************************************/
 /**                                                              **/
 /**    Major features.  These determine what capabilities your   **/
-/**    comiled printf_fast will have.                            **/
+/**    compiled printf_fast will have.                           **/
 /**                                                              **/
 /******************************************************************/
 
 // Include support for 32 bit base 10 integers (%ld and %lu).  Without
 // this, you won't be able to print 32 bit integers as base 10.  They
-// will appear in hexidecimal.
+// will appear in hexadecimal.
 #define LONG
 
 // Include support for floating point numbers (%f).  Don't forget to
 
 /* extern void putchar(char ); */
 
+// Warning: using static/global variables makes these functions NON-reentrant!
+// reentrant keyword is only used for parameter passing method
 
 static bit long_flag, short_flag, print_zero_flag, negative_flag;
 
 #ifdef FIELD_WIDTH
 static bit field_width_flag;
+static bit leading_zero_flag;
 static data unsigned char field_width;
 #endif
 
@@ -115,9 +118,25 @@ static data unsigned int i2bcd_tmp;  // slow 32 int conversion needs temp space
 
 
 
-void printf_fast(code char *fmt, ...) reentrant
+#ifndef PRINTF_FAST
+#define PRINTF_FAST printf_fast
+#endif
+
+
+#if !defined(SDCC_mcs51) || defined(SDCC_USE_XSTACK) || defined(_SDCC_NO_ASM_LIB_FUNCS)
+// Does printf_fast really work on ds390 and ds400?
+// If it does, enable them in the line above
+#if defined(SDCC_USE_XSTACK)
+#warning "printf_fast not built, does not support --xstack"
+#elif defined(_SDCC_NO_ASM_LIB_FUNCS)
+#warning "printf_fast not built, _SDCC_NO_ASM_LIB_FUNCS defined"
+#endif
+#else // defines are compatible with printf_fast
+
+
+void PRINTF_FAST(code char *fmt, ...) reentrant
 {
-       fmt;    /* supress unreferenced variable warning */
+       fmt;    /* suppress unreferenced variable warning */
 
        _asm
 
@@ -145,6 +164,7 @@ printf_format:
        clr     _negative_flag
 #ifdef FIELD_WIDTH
        clr     _field_width_flag
+       clr     _leading_zero_flag
        mov     r1, #_field_width
        mov     @r1, #0
 #endif
@@ -165,6 +185,10 @@ printf_format_loop:
        jnc     printf_nondigit2
 #ifdef FIELD_WIDTH
 printf_digit:
+       jnz     printf_digit_2
+       cjne    a, _field_width, printf_digit_2
+       setb    _leading_zero_flag
+printf_digit_2:
        setb    _field_width_flag
        mov     r2, a
        mov     a, @r1
@@ -212,6 +236,7 @@ printf_format_u:
 printf_format_c:
        //cjne  a, #'c', printf_format_x
        cjne    a, #99, printf_format_x
+       dec     r0
        mov     a, @r0          // Acc has the character to print
        dec     r0
        sjmp    printf_char
@@ -270,6 +295,7 @@ printf_string:
 
 #ifdef FIELD_WIDTH
        jnb     _field_width_flag, printf_str_loop
+       clr     _leading_zero_flag      // never leading zeros for strings
        push    dpl
        push    dph
 printf_str_fw_loop:
@@ -911,6 +937,7 @@ print_float_int:
        jnc     print_float_size_ok
 printf_float_too_big:
        // TODO: should print some sort of overflow error??
+       pop     ar0
        ljmp    printf_format_loop
 print_float_size_ok:
        push    dpl
@@ -1393,6 +1420,10 @@ printf_i2bcd_add_skip:
 printf_space_loop:
        //mov   a, #' '
        mov     a, #32
+       jnb     _leading_zero_flag, printf_space_output
+       //mov   a, #'0'
+       mov     a, #48
+printf_space_output:
        lcall   printf_putchar
        dec     _field_width
 printf_space:
@@ -1473,24 +1504,24 @@ printf_end:
  */
 
 #if 0
-code unsigned char int2bcd_0[] = {
+static code unsigned char int2bcd_0[] = {
 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15};
 
-code unsigned char int2bcd_1[] = {
+static code unsigned char int2bcd_1[] = {
 0x00, 0x16, 0x32, 0x48, 0x64, 0x80, 0x96, 0x12,
 0x28, 0x44, 0x60, 0x76, 0x92, 0x08, 0x24, 0x40,
 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02};
 #endif
 
-code unsigned char int2bcd_2[] = {
+static code unsigned char int2bcd_2[] = {
 0x00, 0x56, 0x12, 0x68, 0x24, 0x80, 0x36, 0x92,
 0x48, 0x04, 0x60, 0x16, 0x72, 0x28, 0x84, 0x40,
 0x00, 0x02, 0x05, 0x07, 0x10, 0x12, 0x15, 0x17,
 0x20, 0x23, 0x25, 0x28, 0x30, 0x33, 0x35, 0x38};
 
-code unsigned char int2bcd_3[] = {
+static code unsigned char int2bcd_3[] = {
 0x00, 0x96, 0x92, 0x88, 0x84, 0x80, 0x76, 0x72,
 0x68, 0x64, 0x60, 0x56, 0x52, 0x48, 0x44, 0x40,
 0x00, 0x40, 0x81, 0x22, 0x63, 0x04, 0x45, 0x86,
@@ -1499,7 +1530,7 @@ code unsigned char int2bcd_3[] = {
 0x03, 0x03, 0x04, 0x04, 0x04, 0x05, 0x05, 0x06};
 
 #ifdef LONG
-code unsigned char int2bcd_4[] = {
+static code unsigned char int2bcd_4[] = {
 0x00, 0x36, 0x72, 0x08, 0x44, 0x80, 0x16, 0x52,
 0x88, 0x24, 0x60, 0x96, 0x32, 0x68, 0x04, 0x40,
 0x00, 0x55, 0x10, 0x66, 0x21, 0x76, 0x32, 0x87,
@@ -1507,7 +1538,7 @@ code unsigned char int2bcd_4[] = {
 0x00, 0x06, 0x13, 0x19, 0x26, 0x32, 0x39, 0x45,
 0x52, 0x58, 0x65, 0x72, 0x78, 0x85, 0x91, 0x98};
 
-code unsigned char int2bcd_5[] = {
+static code unsigned char int2bcd_5[] = {
 0x00, 0x76, 0x52, 0x28, 0x04, 0x80, 0x56, 0x32,
 0x08, 0x84, 0x60, 0x36, 0x12, 0x88, 0x64, 0x40,
 0x00, 0x85, 0x71, 0x57, 0x43, 0x28, 0x14, 0x00,
@@ -1517,7 +1548,7 @@ code unsigned char int2bcd_5[] = {
 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15};
 
-code unsigned char int2bcd_6[] = {
+static code unsigned char int2bcd_6[] = {
 0x00, 0x16, 0x32, 0x48, 0x64, 0x80, 0x96, 0x12,
 0x28, 0x44, 0x60, 0x76, 0x92, 0x08, 0x24, 0x40,
 0x00, 0x72, 0x44, 0x16, 0x88, 0x60, 0x32, 0x05,
@@ -1529,7 +1560,7 @@ code unsigned char int2bcd_6[] = {
 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02};
 
-code unsigned char int2bcd_7[] = {
+static code unsigned char int2bcd_7[] = {
 0x00, 0x56, 0x12, 0x68, 0x24, 0x80, 0x36, 0x92,
 0x48, 0x04, 0x60, 0x16, 0x72, 0x28, 0x84, 0x40,
 0x00, 0x54, 0x09, 0x63, 0x18, 0x72, 0x27, 0x81,
@@ -1566,7 +1597,7 @@ code unsigned char int2bcd_7[] = {
 */
 
 #ifdef LONG
-code unsigned char int2bcd[] = {
+static code unsigned char int2bcd[] = {
 0x01, 0x00, 0x00, 0x00, 0x00,          //          1
 0x02, 0x00, 0x00, 0x00, 0x00,          //          2
 0x04, 0x00, 0x00, 0x00, 0x00,          //          4
@@ -1601,7 +1632,7 @@ code unsigned char int2bcd[] = {
 0x48, 0x36, 0x48, 0x47, 0x21           // 2147483648
 };
 #else // not LONG
-code unsigned char int2bcd[] = {
+static code unsigned char int2bcd[] = {
 0x01, 0x00, 0x00,              //          1
 0x02, 0x00, 0x00,              //          2
 0x04, 0x00, 0x00,              //          4
@@ -1642,7 +1673,7 @@ code unsigned char int2bcd[] = {
  * }
  */
 
-code unsigned char frac2bcd[] = {
+static code unsigned char frac2bcd[] = {
 0x00, 0x00, 0x00, 0x50,                // 0.500000000000000  0.50000000
 0x00, 0x00, 0x00, 0x25,                // 0.250000000000000  0.75000000
 0x00, 0x00, 0x50, 0x12,                // 0.125000000000000  0.87500000
@@ -1684,7 +1715,7 @@ code unsigned char frac2bcd[] = {
 // 100.0 - 999.99      2       100.0 = 0x42C80000  42C8
 // 1000 - 9999.9       1       1000  = 0x447A0000  447A
 // 10000+              0       10000 = 0x461C4000  461C
-code unsigned int float_range_table[] = {
+static code unsigned int float_range_table[] = {
 65536 - 0x3A83,
 65536 - 0x3C23,
 65536 - 0x3DCC,
@@ -1710,7 +1741,7 @@ code unsigned int float_range_table[] = {
 * }
 */
 
-code unsigned char frac2bcd[] = {
+static code unsigned char frac2bcd[] = {
 0x00, 0x50,             // 0.500000000000000  0.5000
 0x00, 0x25,             // 0.250000000000000  0.7500
 0x50, 0x12,             // 0.125000000000000  0.8750
@@ -1731,5 +1762,5 @@ code unsigned char frac2bcd[] = {
 #endif // FLOAT
 
 
-
+#endif // defines compatible with printf_fast