; ======================================================================
; XANDRIAN LABS  /  CS617 CHAPTER 02
; ======================================================================
; Program    : Chapter 02 Sorting
; Source     : app/numfmt.inc
; Revision   : x0.31a-dev / comment-harmonization pass
; Target     : Casio PB-2000C / HD61700
; Role       : Format small unsigned values as decimal and hex.
; Interface  : DEC_ST, HEX_ST, HEX_ASCII.
; Maintainer : Xandrian Labs
; ----------------------------------------------------------------------
; Formatting writes to caller-supplied buffers.
; ======================================================================

; PURPOSE : Write one decimal digit by repeated subtraction.
DEC_ST:
        LD      $12,0

DS_LOOP:
        SBCW    $8,$10
        JR      C,DS_OUT

        SBW     $8,$10
        AD      $12,$SY
        JR      DS_LOOP

DS_OUT:
        LD      $16,$12
        AD      $16,&H30
        STI     $16,(IX+$SX)
        RTN

; PURPOSE : Render a byte as two uppercase hexadecimal digits.
HEX_ST:
        LD      $11,0

HS_LOOP:
        SBC     $10,&H10
        JR      C,HS_HIGH

        SB      $10,&H10
        AD      $11,$SY
        JR      HS_LOOP

HS_HIGH:
        LD      $16,$11
        CAL     HEX_ASCII
        STI     $16,(IX+$SX)

        LD      $16,$10
        CAL     HEX_ASCII
        STI     $16,(IX+$SX)
        RTN

HEX_ASCII:
        AN      $16,&H0F
        AD      $16,&H30
        SBC     $16,&H3A
        RTN     C
        AD      $16,&H07
        RTN
; end of include