/*
 * puts — C99 fputs(s, stdout) + '\n'.
 *
 * Turbo-C convention: stdio's `puts` is the FAST path with NO attribute
 * control — backed by ESTEX PCHARS ($5C).  Cursor cell attributes are
 * whatever ESTEX has cached (usually the shell's default).
 *
 * For coloured output use cputs() / cprintf() from <conio.h>.
 *
 * Implementation notes:
 *   - PCHARS does NOT translate '\n' to CR LF, so we copy the string
 *     into a static buffer expanding each '\n' to CR LF, then append
 *     the trailing CR LF before the NUL.
 *   - Avoid trailing PUTCHAR after PCHARS — empirically that sometimes
 *     drops the next char.  Embed the line ending inside the PCHARS
 *     buffer instead.
 */

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

static void pchars(const char *s) __naked
{
    (void)s;
    __asm
        push  ix
        ld    c, #0x5C
        rst   #0x10
        pop   ix
        ret
    __endasm;
}

char puts(const char *s) __naked
{
    (void)s;    
    __asm
    puts_:
        ld    a, (hl)
        or    a
        jr    z, fin_
        push  hl
        ld    l, a
        ld    h, #0
        call  _putchar
        pop   hl
        inc   hl
        jp    puts_
        ;
    fin_:	
        ld    l, #0x0A
        ld    h, #0
        call  _putchar
        ret
    __endasm;
}
