6502-OS/util/string.h65

99 lines
2.9 KiB
Plaintext

;;********************************************************************************
;; @module string
;; @type utility
;; @details
;; String utility
;;********************************************************************************
.ifndef INCLUDE_STRING
INCLUDE_STRING = 1
.include "system/system.h65"
;;********************************************************************************
;; @brief String utility
;;********************************************************************************
.scope str
Import str, strf, printf_buffer
Import str, hex_char_to_uint8, hex_str_to_uint
Import str, uint8_to_hex_str, uint_to_hex_str
;;********************************************************************************
;; @macro Helper for str::Strf macro
;; @details
;; Store arg in ARG4-ARG... and increment counter variable @N_ARGS one.
;; Arg can be x, y, immediate or an address
;;********************************************************************************
.macro _StrfStoreArg arg
.if (.not .blank(arg))
.if .match(arg, x)
stx ARG4 + @N_ARGS
.elseif .match(arg, y)
sty ARG4 + @N_ARGS
.else
lda arg
sta ARG4 + @N_ARGS
.endif
@N_ARGS .set (@N_ARGS + 1)
.endif
.endmacro
;;********************************************************************************
;; @macro Macro for passing arguments to strf
;; @param fmt: Format string address or string literal
;; @param out: Output string address
;; @param x0-x9: Additional parameters
;; @warning Addresses as additional paramters must be passed like this `#<addr,#>addr`
;; @modifies: A, X, Y, ARG4, ARG5
;; @see str::strf
;;********************************************************************************
.macro Strf fmt,out,x0,x1,x2,x3,x4,x5,x6,x7,x8,x9
@N_ARGS .set 0 ; @ so that it doesnt break cheap labels
.if .match(fmt, "") ; fmt is a string literal -> store in rodata
.pushseg
.rodata
: .asciiz fmt
.popseg
lda #<:-
sta ARG0
lda #>:-
sta ARG1
.else ; message is not a string
lda #<fmt
sta ARG0
lda #>fmt
sta ARG1
.endif
lda #<out
sta ARG2
lda #>out
sta ARG3
_StrfStoreArg x0
_StrfStoreArg x1
_StrfStoreArg x2
_StrfStoreArg x3
_StrfStoreArg x4
_StrfStoreArg x5
_StrfStoreArg x6
_StrfStoreArg x7
_StrfStoreArg x8
_StrfStoreArg x9
jsr str::strf
; .out .sprintf("info: Strf: called with %d arguments", @N_ARGS)
.endmacro
;;********************************************************************************
;; @function Macro for formated printing
;; @details
;; Pass all arguments to str::Strf macro, then call lcd::PrintNC macro
;; @see str::strf
;;********************************************************************************
.macro Printf fmt,x0,x1,x2,x3,x4,x5,x6,x7,x8,x9
Strf fmt,str::printf_buffer,x0,x1,x2,x3,x4,x5,x6,x7,x8,x9
PrintNC str::printf_buffer
.endmacro
.endscope
.endif ; guard