;SNES String Functions ;(C) 1998, 1999, 2000 Realtime Simulations and Roleplaying Games ;By Grog ;String Functions RAM locations ;I arbitrarily close $00FD, but move it if you like StdoutHandler EQU $FD ;Holds 24-bit pointer to Stdout handler ;SetStdout -- Set the location of the Stdout handler subroutine ;In: X -- Handler address's offset ; A -- Handler address's Bank SetStdout: ;Assumes 16b index STX StdoutHandler STA StdoutHandler+2 RTS ;SimplePrint -- Stdout handler that simply prints ASCII to VRAM at the ; current VRAM pointer. Of course, an 8x8 ASCII tile set must ; be loaded and the VRAM pointer set ahead of time ; Also note that for proper operation, $2115 bit 7 should be ; cleared ahead of time. Doesn't process control characters. ;In: A -- ASCII code to print ;Out: none ;Modifies: P SimplePrint: STA $802118 ;Output character RTL ;Print -- Print an ASCII character to Stdout ;In: A -- ASCII code to print ;Out: none ;Modifies: none Print: JML (StdoutHandler) ;PrintF -- Print a Formatted, NULL terminated string to Stdout ;In: X -- points to format string ; Y -- points to parameter buffer ;Out: none ;Modifies: P ;Notes: ; Supported Format characters: ; %s -- sub-string (reads 16-bit pointer from Y data) ; %d -- 16-bit Integer (reads 16-bit data from Y data) ; %c -- 8-bit Integer (reads 8-bit data from Y data) ; %% -- normal % ; \n -- Newline character ($0D) ; \t -- Tab character ($09) ; \b -- Page break character ($0C) ; \\ -- normal slash ; String pointers all refer to current Data Bank (DB) PrintF: ;Assumes 8b mem, 16b index .mem 8 .index 16 PHA PHX PHY PrintFLoop: LDA !$0000,X ;Read next format string character BEQ PrintFDone ;Check for NULL terminator INX ;Increment input pointer CMP #'%' BEQ PrintFFormat ;Handle Format character CMP #'\' BEQ PrintFControl ;Handle Control character NormalPrint: JSL Print ;Print the character normally BRA PrintFLoop PrintFDone: PLY PLX PLA RTS PrintFControl: LDA !$0000,X ;Read control character BEQ PrintFDone ;Check for NULL terminator INX ;Increment input pointer CMP #'n' BNE + LDA #$0D ;Newline BRA NormalPrint + CMP #'t' BNE + LDA #$09 ;Tab BRA NormalPrint + CMP #'b' BNE + LDA #$0C ;Page break BRA NormalPrint LDA #'\' ;Normal \ BRA NormalPrint PrintFFormat: LDA !$0000,X ;Read format character BEQ PrintFDone ;Check for NULL terminator INX ;Increment input pointer CMP #'s' BNE + PHX ;Preserve current format string pointer LDX 0,Y ;Load sub-string pointer INY INY JSR PrintF ;Call PrintF recursively with sub-string BRA PrintFLoop + CMP #'d' BNE + JSR PrintInt16 ;Print 16-bit Integer BRA PrintFLoop + CMP #'c' BNE + JSR PrintInt8 ;Print 8-bit Integer BRA PrintFLoop + LDA #'%' BRA NormalPrint ;PrintInt16 -- Read a 16-bit value pointed to by Y and print it to stdout ;In: Y -- Points to integer in current data bank ;Out: Y=Y+2 ;Modifies: P ;Notes: Uses Print to output ASCII to stdout PrintInt16: ;Assume 8b mem, 16b index LDA #$00 PHA LDA !$0000,Y STA $804204 LDA !$0001,Y STA $804205 INY INY DivLoop: LDA #$0A STA $804206 LDA #'0' CLC ADC $804216 PHA LDA $804214 STA $804204 BEQ + LDA $804215 STA $804205 BRA DivLoop + LDA $804215 STA $804205 BEQ IntPrintLoop BRA DivLoop IntPrintLoop: PLA BEQ + JSL Print BRA IntPrintLoop + RTS ;PrintInt8 -- Read an 8-bit value pointed to by Y and print it to stdout ;In: Y -- Points to integer in current data bank ;Out: Y=Y+1 ;Modifies: P ;Notes: Uses Print to output ASCII to stdout PrintInt8: ;Assume 8b mem, 16b index LDA #$00 PHA LDA !$0000,Y STA $804204 LDA #$00 STA $804205 INY BRA DivLoop