; *************************************************************************** ; *************************************************************************** ; ; hucc-printf.asm ; ; A small and simple printf() and sprintf() implementation for HuCC. ; ; Copyright John Brandwood 2019-2025. ; ; Distributed under the Boost Software License, Version 1.0. ; (See accompanying file LICENSE_1_0.txt or copy at ; http://www.boost.org/LICENSE_1_0.txt) ; ; *************************************************************************** ; *************************************************************************** ; ; WARNING: This only supports a subset of POSIX printf()/sprintf() behavior! ; WARNING: , and numbers in ESC-sequences must be <= 127! ; WARNING: This only supports a maximum of 4 data arguments! ; WARNING: sprintf() must output less than 256 characters! ; ; *************************************************************************** ; *************************************************************************** ; ; A format specifier follows this prototype: ; ; '%' [] [] ['.' ] [] ; ; ; ; ('-' | '0' | '+' | ' ') = one or more of the following ... ; ; '-' to left-justify output within the ; '0' to pad to with zero instead of space ; '+' to prepend positive integers with a '+' when "%d" or "%i" ; ' ' to prepend positive integers with a ' ' when "%d", "%i" or "%u" ; ; ; ; ( | '*') = "1..127" or read value from an argument ; ; ; ; ( | '*') = "1..127" or read value from an argument ; ; ; ; ('l') = only "%x" and "%X" : read two 16-bit arguments as a 32-bit value ; ; ; ; '%' = just print a '%' ; 'c' = print an ASCII character times ; 'd' = print a signed integer variable in decimal ; 'i' = print a signed integer variable in decimal ; 's' = print string, stop at characters ; 'u' = print an unsigned integer variable in decimal ; 'x' = print an unsigned binary (or BCD) variable in hex ; 'X' = print an unsigned binary (or BCD) variable in hex ; ; Note: ; ; HuCC follows GCC's behavior, except for "%u" with a '+' ... ; ; If both '+' and ' ' flags are used, then '+' overrides the ' ' flag. ; "%u" is unsigned-by-definition and treats '+' as ' ' (GCC ignores '+'). ; "%x", "%X", "%c" and "%s" ignore the '+' and ' ' flags. ; ; *************************************************************************** ; ; printf() and puts() output accept virtual-terminal control codes: ; ; ; ; '\n' = move cursor-X to XL, cursor-Y down 1 line. ; '\r' = move cursor-X to XL. ; '\f' = clear-screen + home cursor-X and cursor-Y to (XL, YT) ; ; *************************************************************************** ; ; printf() and puts() output accept virtual-terminal escape sequences: ; ; ('\e' | '\x1B') ; ; ; ; 'X' = set current X coordinate (0..127) ; 'Y' = set current Y coordinate (0..63) ; 'L' = set minimum X coordinate (0..127) ; 'T' = set minimum Y coordinate (0..63) ; 'P' = set current palette (0..15) ; ; *************************************************************************** ; *************************************************************************** ; ; Include dependancies ... ; include "common.asm" ; Common helpers. ; ; Variables that are remembered between invocations. ; .if 0 ; ************** ; 16-bytes of VDC TTY information (moved to hucc-gfx.asm to save .bss memory). ; ; N.B. MUST be 16-bytes before the SGX versions to use PCE_VDC_OFFSET. vdc_tty_x_lhs: ds 1 ; TTY minimum X position. vdc_tty_y_top: ds 1 ; TTY minimum Y position. vdc_tty_x: ds 1 ; TTY current X position. vdc_tty_y: ds 1 ; TTY current Y position. .if SUPPORT_SGX ; ************** ; 16-bytes of SGX TTY information (moved to hucc-gfx.asm to save .bss memory). ; ; N.B. MUST be 16-bytes after the VDC versions to use SGX_VDC_OFFSET. ds 12 ; Padding to ensure the 16-byte delta. sgx_tty_x_lhs: ds 1 ; TTY minimum X position. sgx_tty_y_top: ds 1 ; TTY minimum Y position. sgx_tty_x: ds 1 ; TTY current X position. sgx_tty_y: ds 1 ; TTY current Y position. .endif .endif ; ; Temporary variables for each invocation using common zero-page locations. ; ; N.B. TTY form-feed output uses _ax and _bl! ; tty_out1st = _al ; Is there a +/- sign? tty_outpad = _ah ; Pad with zero, not space. tty_outlhs = _bl ; Left justify, not right. tty_outmin = _bh ; Minimum output width. tty_outmax = _cl ; Maximum output width. tty_outstk = _ch ; SP of the end of a converted string. tty_vararg = _dl ; Index of next vararg parameter. tty_outcnt = _dh ; Number of characters written. tty_vdc = __ptr+0 ; SGX_VDC_OFFSET or zero. tty_xyok = __ptr+1 ; Is the VDC_MAWR register correct? tty_vector = __fptr ; Output vector for writing. ; *************************************************************************** ; *************************************************************************** ; ; int __fastcall sprintf( unsigned char *string<_di>, unsigned char *format<_bp>, unsigned int vararg1<__vararg1>, unsigned int vararg2<__vararg2>, unsigned int vararg3<__vararg3>, unsigned int vararg4<__vararg3> ); ; int __fastcall printf( unsigned char *format<_bp>, unsigned int vararg1<__vararg1>, unsigned int vararg2<__vararg2>, unsigned int vararg3<__vararg3>, unsigned int vararg4<__vararg3> ); ; int __fastcall sgx_printf( unsigned char *format<_bp>, unsigned int vararg1<__vararg1>, unsigned int vararg2<__vararg2>, unsigned int vararg3<__vararg3>, unsigned int vararg4<__vararg3> ); ; ; Also uses: _si, _di, _ax, _bx, _cx, _dx, __ptr ; ; This is a reasonably-compliant C "printf", but don't expect wonders! ; ; Each number that is written is converted onto the stack before the output ; so that it can be justified and/or padded. ; printf_group .procgroup ; These routines share code! _sprintf.2 .proc ; HuC entry point. lda.l #write_to_str ; Output to a string. sta.l tty_vector lda.h #write_to_str sta.h tty_vector ldx #-1 ; Set -ve VDC to signal output bra !printf+ ; to a string. .ref _printf.1 ; Need _printf.1 .endp .if SUPPORT_SGX _sgx_printf.1 .proc ldx #SGX_VDC_OFFSET ; Offset to SGX VDC. db $F0 ; Turn "clx" into a "beq". .ref _printf.1 ; Need _printf.1 .endp .endif _printf.1 .proc ; HuC entry point. clx ; Offset to PCE VDC. lda.l #write_to_tty ; Output to one of the VDCs. sta.l tty_vector lda.h #write_to_tty sta.h tty_vector !printf: stx C. rol.h Remainder. sbc #10 ; Subtract Divisor. .divide_less: rol.l Dividend LSB. rol.h C. dex bne .divide_loop pha ; Remainder onto the stack. lda.l ); ; void __fastcall __macro sgx_puts( unsigned char *string<_bp> ); .macro _sgx_puts.1 ldx #SGX_VDC_OFFSET call puts_x .endm .macro _puts.1 clx call puts_x .endm puts_x .proc ; HuC entry point. stx ); ; void __fastcall __macro sgx_putchar( unsigned char *string<_bp> ); .macro _sgx_putchar.1 tay ldx #SGX_VDC_OFFSET call putchar_x .endm .macro _putchar.1 tay clx call putchar_x .endm putchar_x .proc ; HuC entry point. stx