; ======================================================================
; CS617 NUMERIC - U32 DECIMAL FORMAT DIAGNOSTIC - C6 D1
;
; Hypothesis:
;   Unsigned u32 values can be formatted as ordinary decimal ASCII by
;   composing accepted UDIV3216NZ / UDIV32NZ and a small reversal layer.
;
; Required gates:
;   1. Rev. 0.45 assembly success.
;   2. relocated UDIV32NZ remains byte-identical to accepted EAC5 donor.
;   3. accepted u32/u16 wrapper source remains unchanged.
;   4. reduced representative smoke corpus reports PASS 12/12.
;
; New primitive:
;   UFMT32DEC
;       input  $15-$18 = u32
;       input  IX      = writable 11-byte buffer
;       output buffer  = ASCII decimal + NUL
;       output $14     = length 1..10
;       input u32 and IX are preserved
; ======================================================================

        ORG     0
        LEVEL   0

OFFCHK: EQU     &H04EF
FMTBUF: EQU     &H2000

        DB      &H43,&H4D
        DB      &HFF,&HFF,&HFF,&HFF
        DB      &H01,&H04,&H10,&H10
        DB      &HFF,&HFF,&HFF,&HFF
        DB      &HFF,&HFF,&HFF,&HFF
        DB      &HFF,&HFF,&HFF,&HFF
        DB      &HFF,&HFF,&HFF,&HFF
        DB      &HFF
        DB      &H03
        DB      &HC0
        DB      &HFF,&HFF,&HFF

        ORG     &H0D80

        JP      MAIN
        JP      TRAP
        JP      &H01AA
        JP      &H01BC
        JP      TRAP
        JP      TRAP
        JP      TRAP
        JP      &H00E3

DEFRT:  RTN
        DB      &HFF,&HFF
        RTN
        DB      &HFF,&HFF
        JP      DEFRT
        JP      POFF
        JP      DEFRT
        JP      DEFRT
        JP      MAIN
        JP      DEFRT
        JP      DEFRT
        JP      DEFRT
        JP      DEFRT
        DB      &H11
        DB      &H10

TRAP:   RTNI

POFF:   OFF
        OFF
        OFF
        JR      POFF

        ORG     &H0E00

MAIN:
; Required literal specific-register environment.
        LD      $30,1
        LD      $31,0
        PSR     SZ,0
        PSR     SY,30
        PSR     SX,31

; UA=54: PC remains in ROM bank 0; IX and stacks use bank 1 RAM.
        PST     UA,&H54
        PRE     SS,&H1298
        JP      RUN_TESTS

; Accepted dependency layout:
;   UDIV32NZ  0E16..0E76  97 bytes
#INCLUDE (../lib/divmod_u32_u32.inc)
#LIST
#INCLUDE (../lib/divmod_u32_u16.inc)
#LIST
#INCLUDE (../lib/format_u32_dec.inc)
#LIST

RUN_TESTS:
#INCLUDE (c6_smoke_vectors.inc)
#LIST
        JP      TEST_PASS

TEST_PASS:
; Firmware text helpers expect the normal standalone ROM data environment.
        PST     UA,&H14
        CAL     SCREEN_INIT
        LD      $6,&H40
        PRE     IZ,TXT_PASS
        CAL     PRINTZ
        LD      $6,&H60
        PRE     IZ,TXT_READY
        CAL     PRINTZ
        JP      HALT

TEST_FAIL:
; $27 = test number, $28 = failure component.
; Screen initialization is expected to use low registers; keep diagnostics
; in the high-register pair used by the test harness.
        PST     UA,&H14
        CAL     SCREEN_INIT

        LD      $6,&H40
        PRE     IZ,TXT_FAIL
        CAL     PRINTZ
        LD      $12,$27
        CAL     SHOWHEX

        LD      $6,&H60
        PRE     IZ,TXT_COMP
        CAL     PRINTZ
        LD      $12,$28
        CAL     SHOWHEX
        JP      HALT

HALT:
        JR      HALT

SCREEN_INIT:
        PST     UA,&H14
        CAL     OFFCHK
        PPO     &HC0

        LDW     $0,&H12B5
INIT_DELAY:
        SBW     $0,$SY
        JR      NZ,INIT_DELAY

        CAL     &H02D4

        PPO     &HC3
        LD      $0,&H0C
        LDW     $1,&H3000
        STLM    $0,3

        LD      $6,$SX
        PRE     IZ,TXT_TITLE
        CAL     PRINTZ
        LD      $8,115
        CAL     &H018D

        LD      $6,&H20
        PRE     IZ,TXT_OP
        CAL     PRINTZ

        CAL     &H0304
        CAL     &H0306
        RTN

SHOWHEX:
        LD      $16,$12
        DID     $16
        CAL     HEXASC
        CAL     PUTC
        LD      $16,$12
        CAL     HEXASC
        CAL     PUTC
        RTN

HEXASC:
        AN      $16,&H0F
        AD      $16,&H30
        SBC     $16,&H3A
        RTN     C
        AD      $16,&H07
        RTN

PUTC:
        LD      $7,$16
        LD      $0,$7
        DIU     $7
        DID     $0
        OR      $7,$SZ
        CAL     &H0197
        RTN

PRINTZ:
        LDI     $7,(IZ+$SX)
        LD      $0,$7
        DIU     $7
        DID     $0
        OR      $7,$SZ
        RTN     Z
        CAL     &H0197
        JR      PRINTZ

TXT_TITLE:
        DB      "CS617 NUMERIC",0
TXT_OP:
        DB      "U32 -> DECIMAL",0
TXT_PASS:
        DB      "PASS 12/12",0
TXT_FAIL:
        DB      "FAIL TEST ",0
TXT_COMP:
        DB      "COMPONENT ",0
TXT_READY:
        DB      "C6 D1 SMOKE",0

        ORG     &HFFFF
        DB      0
