/*
 * dec_print.c — compact decimal print primitives ported from solid-c
 * (third_party/solid-c/SRC/CLIB/STDLIB.ASM, modules dec8/dec16/dec32).
 *
 *   void dec8 (uint8_t  v) — no-leading-zero "0" .. "255"
 *   void dec16(uint16_t v) — no-leading-zero "0" .. "65535"
 *   void dec32(uint32_t v) — no-leading-zero "0" .. "4294967295"
 *
 * Algorithm: subtract-power-of-10 with running counter, then emit the
 * digit unless we are still on leading zeros.  32-bit values use a
 * BC/DE/HL pair plus the shadow set (exx) for the high half.
 *
 * Layout: dec32 is the master routine; dec8/dec16 are tiny wrappers
 * that prepare state and jump to internal entry points (__dec_entry3
 * for "last 3 digits", __dec_entry5 for "last 5") — same idea as
 * solid-c, sharing most of the per-digit code.
 *
 * ESTEX PUTCHAR ($5B) preserves IX (empirically verified) so no
 * push/pop ix around the RST.
 */

#include <stdio.h>
#include <stdint.h>

/* Leading-zero suppression flag — 0 means "still skipping zeros",
 * non-zero means "first non-zero digit seen, print everything from
 * here on (including subsequent zeros)". */
static uint8_t dec_flag  = 0;

void dec8(uint8_t v) __naked
{
    (void)v;
    __asm
        ;; A = v.
        ld    l, a
        ld    h, #0                 ; HL = value
        xor   a, a
        ld    (_dec_flag), a        ; reset suppress-leading-zero flag
        jp    __dec_entry3
    __endasm;
}

void dec16(uint16_t v) __naked
{
    (void)v;
    __asm
        ;; HL = v.
        exx
        ld    hl, #0                ; HL alt = 0 (high 16 of composite)
        exx
        xor   a, a
        ld    (_dec_flag), a
        jp    __dec_entry5
    __endasm;
}

void dec32(uint32_t v) __naked
{
    (void)v;
    __asm
        ;; HL = high16, DE = low16 on entry (SDCC HLDE).
        ;; Move high16 into HL alt (shadow set), low16 into HL.
        push  hl
        exx
        pop   hl                    ; HL alt = high16
        exx
        ex    de, hl                ; HL  = low16
        xor   a, a
        ld    (_dec_flag), a

        ;; ---- 5 most-significant decades (1e9..1e5) ----
        ld    de, #0xCA00
        exx
        ld    de, #0x3B9A           ; 0x3B9ACA00 = 1,000,000,000
        exx
        call  _dec_get_d32

        ld    de, #0xE100
        exx
        ld    de, #0x05F5           ; 0x05F5E100 = 100,000,000
        exx
        call  _dec_get_d32

        ld    de, #0x9680
        exx
        ld    de, #0x0098           ; 0x00989680 = 10,000,000
        exx
        call  _dec_get_d32

        ld    de, #0x4240
        exx
        ld    de, #0x000F           ; 0x000F4240 = 1,000,000
        exx
        call  _dec_get_d32

        ld    de, #0x86A0
        exx
        ld    de, #0x0001           ; 0x000186A0 = 100,000
        exx
        call  _dec_get_d32

    __dec_entry5::                  ; entered from dec16
        ld    de, #10000
        exx
        ld    de, #0
        exx
        call  _dec_get_d32

        ld    de, #1000
        call  _dec_get_d16

    __dec_entry3::                  ; entered from dec8
        ld    de, #100
        call  _dec_get_d16
        ld    de, #10
        call  _dec_get_d16

        ;; Units digit — always emitted (so dec*(0) prints "0").
        ld    a, l
        add   a, #0x30
        ld    c, #0x5B
        rst   #0x10
        ret

    ;; ---- 32-bit: how many times DE+DE_alt fits in HL+HL_alt ----
    _dec_get_d32:
        ld    a, #0x2F              ; 0x2F = '0' minus 1 (pre-decrement)
        and   a, a                  ; CF = 0
    _dec_get_d32_loop:
        inc   a
        sbc   hl, de                ; low half
        exx
        sbc   hl, de                ; high half (with chained borrow)
        exx
        jp    nc, _dec_get_d32_loop
        ;; Overshot — restore the last good value.
        add   hl, de
        exx
        adc   hl, de
        exx
        jr    _dec_emit_or_skip

    ;; ---- 16-bit: how many times DE fits in HL ----
    _dec_get_d16:
        ld    a, #0x2F
        and   a, a
    _dec_get_d16_loop:
        inc   a
        sbc   hl, de
        jp    nc, _dec_get_d16_loop
        add   hl, de
        ;; Fall through to emit/skip.

    _dec_emit_or_skip:
        ;; A = digit char in 0x30..0x39.  If non-'0', latch the flag.
        ;; Print only if flag is non-zero.
        ld    b, a                  ; save digit across the flag test
        cp    a, #0x30
        jr    z, _dec_check_flag
        ld    (_dec_flag), a        ; non-zero digit seen
    _dec_check_flag:
        ld    a, (_dec_flag)
        or    a, a
        ld    a, b                  ; restore digit (ld does not touch flags)
        ret   z                     ; leading zero — skip print

        ;; ESTEX PUTCHAR ($5B) preserves the main register set (BC, DE,
        ;; HL, IX) but CLOBBERS the shadow set (BC alt, DE alt, HL alt).
        ;; The 32-bit subtract-power-of-10 loop in _dec_get_d32 keeps
        ;; the high 16 bits of the running remainder in HL alt, so we
        ;; save/restore HL alt around the RST.  Main HL (= low 16 of
        ;; remainder) survives the call untouched, no save needed.
        ;; See memory/estex_putchar_abi.md.
        exx
        push  hl
        exx
        ld    c, #0x5B
        rst   #0x10
        exx
        pop   hl
        exx
        ret
    __endasm;
}
