* device/include/pic16/pic18f*.h: add bit aliases in INTCONbits_t
[fw/sdcc] / device / lib / printf_fast.c
index e1f0aa032fde1e6058564a3a4152dd4745e19f61..3e20d343b9da77cf404da4f968f0561285dfca48 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;
+static __bit long_flag, short_flag, print_zero_flag, negative_flag;
 
 #ifdef FIELD_WIDTH
-static bit field_width_flag;
-static data unsigned char field_width;
+static __bit field_width_flag;
+static __bit leading_zero_flag;
+static __data unsigned char field_width;
 #endif
 
 #ifdef FLOAT
+#define SDCC_FLOAT_LIB
 #include <float.h>
-static bit continue_float;
+static __bit continue_float;
 #ifndef FLOAT_FIXED4
-static data unsigned char frac_field_width;
-static data unsigned char float_frac_bcd[4];
+static __data unsigned char frac_field_width;
+static __data unsigned char float_frac_bcd[4];
 // TODO: can float_frac_bcd be overlaid with temps used by trig functions
 #endif
 #endif
 
 #ifndef FAST_INTEGER
 #ifdef LONG
-static data unsigned int i2bcd_tmp;  // slow 32 int conversion needs temp space
+static __data unsigned int i2bcd_tmp;  // slow 32 int conversion needs temp space
 #endif
 #endif
 
 
+#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
+
+void PRINTF_FAST(__code char *fmt, ...) __reentrant
 {
-       fmt;    /* supress unreferenced variable warning */
+       fmt;    /* suppress unreferenced variable warning */
 
-       _asm
+       __asm
 
 printf_begin:
        mov     a, _bp          // r0 will point to va_args (stack)
@@ -129,7 +148,6 @@ printf_begin:
        mov     dpl, @r0        // dptr has address of fmt
        dec     r0
 
-
 printf_main_loop:
        clr     a
        movc    a, @a+dptr      // get next byte of fmt string
@@ -144,6 +162,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
@@ -164,6 +183,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
@@ -178,7 +201,6 @@ printf_nondigit1:
 printf_nondigit2:
        add     a, #48
 
-
 printf_format_l:
        //cjne  a, #'l', printf_format_h
        cjne    a, #108, printf_format_h
@@ -211,6 +233,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
@@ -250,10 +273,6 @@ printf_eot:
        ljmp    printf_end
 
 
-
-
-
-
        /* print a string... just grab each byte with __gptrget */
        /* the user much pass a 24 bit generic pointer */
 
@@ -269,6 +288,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:
@@ -296,12 +316,6 @@ printf_str_done:
        ljmp    printf_main_loop
 
 
-
-
-
-
-
-
        /* printing in hex is easy because sdcc pushes the LSB first */
 
 printf_hex:
@@ -400,6 +414,11 @@ printf_uint_ck8:
        /* print the digit zero and skip all the hard work! */
        mov     a, r1
        jnz     printf_uint_begin
+#ifdef FLOAT
+       /* never use the "just print zero" shortcut if we're printing */
+       /* the integer part of a float  (fixes bug 1255403)  */
+       jb      _continue_float, printf_uint_begin
+#endif
 #ifdef FIELD_WIDTH
        jnb     _field_width_flag, printf_uint_zero
        mov     a, _field_width
@@ -411,15 +430,8 @@ printf_uint_zero:
        //mov   a, #'0'
        mov     a, #48
        lcall   printf_putchar
-#ifdef FLOAT
-       jnb     _continue_float, 0001$
-       ret
-0001$:
-#endif
        ljmp    printf_main_loop
 
-
-
 printf_uint_begin:
        push    dpl
        push    dph
@@ -473,7 +485,7 @@ printf_uifw_8:
        jnz     printf_uifw_sub
        dec     r1
 printf_uifw_sub:
-       ;r1 has the number of digits for the number
+       //r1 has the number of digits for the number
        mov     a, _field_width
        mov     c, _negative_flag
        subb    a, r1
@@ -589,12 +601,6 @@ printf_uint8a:
        ljmp    printf_main_loop
 
 
-
-
-
-
-
-
 #ifdef FLOAT
 #ifdef FLOAT_FIXED4
        // Print a float the easy way.  First, extract the integer part and
@@ -612,17 +618,20 @@ print_float:
        mov     _field_width, #0
 #endif
 print_float_begin:
-       mov     exp_b, r0               // keep r0 safe, will need it again
+       push    ar0             // keep r0 safe, will need it again
        lcall   printf_get_float
        clr     c
        mov     a, #158                 // check for large float we can't print
-       subb    a, exp_a
+       subb    a, r7
        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:
-       lcall   __fs_rshift_a
+       push    dpl
+       lcall   fs_rshift_a
+       pop     dpl
        setb    _continue_float
 #ifndef LONG
        mov     a, r3
@@ -635,10 +644,12 @@ print_float_size_ok:
        lcall   printf_putchar
        // now that the integer part is printed, we need to refetch the
        // float from the va_args and extract the fractional part
-       mov     r0, exp_b
+       pop     ar0
        lcall   printf_get_float
        push    ar0
-       mov     a, exp_a
+       push    dpl
+       push    dph
+       mov     a, r7
        cjne    a, #126, print_float_frac_lshift
        sjmp    print_float_frac // input between 0.5 to 0.9999
 print_float_frac_lshift:
@@ -663,7 +674,7 @@ print_float_frac_rshift:
        //Acc (exponent) is less than 126 (input < 0.5)
        cpl     a
        add     a, #127
-       lcall   __fs_rshift_a
+       lcall   fs_rshift_a
 print_float_frac:
        // now we've got the fractional part, so now is the time to
        // convert to BCD... just convert each bit to BCD using a
@@ -672,8 +683,6 @@ print_float_frac:
        clr     a
        mov     r6, a
        mov     r5, a
-       push    dpl
-       push    dph
        mov     dptr, #_frac2bcd        // FLOAT_FIXED4 version (14 entries)
 print_float_frac_loop:
        mov     a, r3
@@ -720,8 +729,6 @@ print_float_frac_skip:
 #else // not FLOAT_FIXED4
 
 
-
-
 print_float:
        // Print a float the not-as-easy way, with a configurable number of
        // fractional digits (up to 8) and proper round-off (up to 7 digits).
@@ -758,8 +765,12 @@ print_float_default_width:
        // default fractional field width (between 0 to 7)
        // attempt to scale the default number of fractional digits
        // based on the magnitude of the float
-       mov     ar1, r0         // r0 points to first byte of float
-       dec     r1              // r1 points to second byte of float
+       mov     a, @r0
+       anl     a, #0x7F        // ignore sign bit
+       mov     r2, a           // r2 is first byte of float
+       dec     r0
+       mov     ar3, @r0        // r3 is second byte of float
+       inc     r0
        mov     r6, dpl
        mov     r7, dph
        mov     dptr, #_float_range_table
@@ -767,11 +778,11 @@ print_float_default_width:
 print_float_default_loop:
        clr     a
        movc    a, @a+dptr
-       add     a, @r1
+       add     a, r3
        inc     dptr
        clr     a
        movc    a, @a+dptr
-       addc    a, @r0
+       addc    a, r2
        jnc     print_float_default_done
        inc     dptr
        djnz    r5, print_float_default_loop
@@ -784,7 +795,9 @@ print_float_default_done:
 print_float_begin:
        push    ar0                     // keep r0 safe, will need it again
        lcall   printf_get_float
-       mov     a, exp_a
+       push    dpl
+       push    dph
+       mov     a, r7
        cjne    a, #126, print_float_frac_lshift
        sjmp    print_float_frac        // input between 0.5 to 0.9999
 
@@ -810,17 +823,15 @@ print_float_frac_rshift:
        //Acc (exponent) is less than 126 (input < 0.5)
        cpl     a
        add     a, #127
-       lcall   __fs_rshift_a
+       lcall   fs_rshift_a
 print_float_frac:
-       // Convert the fraction in r4/r3/r2/r1 into 8 BCD digits in exb_b/r7/r6/r5
+       // Convert the fraction in r4/r3/r2/r1 into 8 BCD digits in r0/r7/r6/r5
        mov     b, #27
        clr     a
-       mov     exp_b, a
+       mov     r0, a
        mov     r7, a
        mov     r6, a
        mov     r5, a
-       push    dpl
-       push    dph
        mov     dptr, #_frac2bcd        // FLOAT version (27 entries)
 print_float_frac_loop:
        mov     a, r1
@@ -853,17 +864,15 @@ print_float_frac_loop:
        mov     r7, a
        mov     a, #3
        movc    a, @a+dptr
-       addc    a, exp_b
+       addc    a, r0
        da      a
-       mov     exp_b, a
+       mov     r0, a
 print_float_frac_skip:
        inc     dptr
        inc     dptr
        inc     dptr
        inc     dptr
        djnz    b, print_float_frac_loop
-       pop     dph
-       pop     dpl
 print_float_frac_roundoff:
        // Now it's time to round-off the BCD digits to the desired precision.
        clr     a
@@ -875,7 +884,8 @@ print_float_frac_roundoff:
        rl      a
        rl      a
        anl     a, #0xFC
-       lcall   __fs_rshift_a           // divide r4/r3/r2/r1 by 10^frac_field_width
+       mov     dph, r0                 // fs_rshift_a will overwrite r0 & dpl
+       lcall   fs_rshift_a             // divide r4/r3/r2/r1 by 10^frac_field_width
        mov     a, r5
        add     a, r1                   // add rounding to fractional part
        da      a
@@ -888,11 +898,13 @@ print_float_frac_roundoff:
        addc    a, r3
        da      a
        mov     _float_frac_bcd+1, a
-       mov     a, exp_b
+       mov     a, dph
        addc    a, r4
        da      a
        mov     _float_frac_bcd+0, a
        mov     sign_b, c               // keep fractional carry in sign_b
+       pop     dph
+       pop     dpl
 print_float_int:
        // Time to work on the integer portion... fetch the float again, check
        // size (exponent), scale to integer, add the fraction's carry, and
@@ -902,13 +914,16 @@ print_float_int:
        push    ar0
        clr     c
        mov     a, #158                 // check for large float we can't print
-       subb    a, exp_a
+       subb    a, r7
        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:
-       lcall   __fs_rshift_a
+       push    dpl
+       lcall   fs_rshift_a
+       pop     dpl
        jnb     sign_b, print_float_do_int
        // if we get here, the fractional round off caused the
        // integer part to increment.  Add 1 for a proper result
@@ -975,7 +990,6 @@ print_float_done:
        ljmp    printf_main_loop
 
 
-
        // acc=1 for tenths, acc=2 for hundredths, etc
 get_float_frac_digit:
        dec     a
@@ -991,9 +1005,6 @@ get_float_frac_digit_done:
        anl     a, #15
        ret
 
-
-
-
 #endif // end of normal FLOAT code (not FLOAT_FIXED4)
 
 
@@ -1002,7 +1013,7 @@ get_float_frac_digit_done:
 
 #if 0
 pm2_print_float:
-        mov    a, exp_a
+        mov    a, r7
         lcall  pm2_entry_phex
         mov    a, #0x20
         lcall  pm2_entry_cout
@@ -1013,7 +1024,7 @@ pm2_print_float:
 #endif
 
        // Fetch a float from the va_args and put it into
-       // exp_a/r4/r3/r2 and also clear r1 and preset
+       // r7(exp) r4/r3/r2(mant) and also clear r1 and preset
        // the flags
 printf_get_float:
        mov     a, @r0
@@ -1026,7 +1037,7 @@ printf_get_float:
        mov     a, r1
        rlc     a
        mov     _negative_flag, c
-       mov     exp_a, a
+       mov     r7, a
        jz      printf_get_float_2
        orl     ar4, #0x80
 printf_get_float_2:
@@ -1043,17 +1054,6 @@ printf_get_float_2:
 #endif // FLOAT
 
 
-
-
-
-
-
-
-
-
-
-
-
        /* read an integer into r1/r2/r3/r4, and msb into r5 */
 printf_get_int:
        mov     a, @r0
@@ -1078,9 +1078,6 @@ printf_get_done:
        ret
 
 
-
-
-
 #ifdef FAST_INTEGER
 
        /* convert binary number in r4/r3/r2/r1 into bcd packed number
@@ -1371,21 +1368,14 @@ printf_i2bcd_add_skip:
 #endif // not FAST_INTEGER
 
 
-
-
-
-
-
-
-
-
-
-
-
 #ifdef FIELD_WIDTH
 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:
@@ -1394,10 +1384,6 @@ printf_space:
        ret
 #endif
 
-
-
-
-
        /* print a hex digit, either upper 4 bit (msn) or lower 4 bits (lsn) */
 
 printf_phex_msn:
@@ -1443,7 +1429,7 @@ printf_zero:
         ljmp   printf_putchar
   
 printf_end:
-       _endasm;
+       __endasm;
 }
 
 
@@ -1466,24 +1452,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,
@@ -1492,7 +1478,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,
@@ -1500,7 +1486,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,
@@ -1510,7 +1496,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,
@@ -1522,7 +1508,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,
@@ -1539,7 +1525,7 @@ code unsigned char int2bcd_7[] = {
 
 /*
  * #! /usr/bin/perl
- * print "code unsigned char int2bcd[] = {\n";
+ * print "__code unsigned char int2bcd[] = {\n";
  * for ($i=0, $n=1; $i<32; $i++, $n*=2) {
  *     $r = sprintf "%010u", $n;
  *     $r =~ /([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])/;
@@ -1547,7 +1533,7 @@ code unsigned char int2bcd_7[] = {
  *     print ',' if $i < 31;
  *     printf "\t\t// %10u\n", $n;
  * }
- * print "}\ncode unsigned char int2bcd[] = {\n";
+ * print "}\n__code unsigned char int2bcd[] = {\n";
  * for ($i=0, $n=1; $i<16; $i++, $n*=2) {
  *     $r = sprintf "%06u", $n;
  *     $r =~ /([0-9][0-9])([0-9][0-9])([0-9][0-9])/;
@@ -1559,7 +1545,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
@@ -1594,7 +1580,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
@@ -1617,8 +1603,6 @@ code unsigned char int2bcd[] = {
 #endif // not FAST_INTEGER
 
 
-
-
 #ifdef FLOAT
 #ifndef FLOAT_FIXED4
 
@@ -1635,7 +1619,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
@@ -1677,7 +1661,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,
@@ -1703,7 +1687,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
@@ -1724,5 +1708,4 @@ code unsigned char frac2bcd[] = {
 #endif // FLOAT
 
 
-
-
+#endif // defines compatible with printf_fast