libc review: mem/, stdio/, fixes in sprinter-cc and FILE shim
libc/mem/:
• Split bank_io.c into bank_io_w3.c (existing W3 helpers, base 0xC000,
port 0xE2) and bank_io_w1.c (new mirror through W1, base 0x4000,
port 0xA2). Two .rel files so DCE picks only the needed group:
a W3-only user pulls ~70 bytes instead of all 134. W1 variants are
`--memory tiny`-only (any other mode runs code from W1 or uses W1
for the banked-code segment, swapping it crashes).
• mem_alloc.c: add CF=err checks for mem_free_block and mem_get_page
(were silently ignored), per-function docstrings on alloc/free/
get_page/info, drop the confused "wait wrong order" comment in
mem_info. Header sprinter_mem.h gets matching per-function doc.
libc/stdio/:
• Add hex_print.c (hex8/hex16/hex32, ~26 bytes) and dec_print.c
(dec8/dec16/dec32, ~170 bytes) ported from solid-c STDLIB.ASM.
Replaces the printf("%u"/"%X") wrappers in solid_helpers.c that
dragged in the 3-5 KB printf machinery.
- hex* use the classic cp 10 / sbc 0x69 / daa nibble→ASCII trick;
hex8 self-calls for the high nibble, hex16/hex32 tail-call hex8.
- dec32 is the master routine; dec8/dec16 jump into shared entry
points (__dec_entry3 / __dec_entry5). 32-bit subtract-power-of-10
keeps the high 16 bits in HL alt (shadow set).
- DISCOVERY: ESTEX PUTCHAR ($5B) on our Sprinter build preserves
the main register set + IX but CLOBBERS the shadow set
(BC'/DE'/HL'). solid-c's original code assumed otherwise and
garbled output for values ≥ 6 digits. Fix: save/restore HL alt
around the RST 10 in _dec_emit_or_skip. Documented in
memory/estex_putchar_abi.md.
• file.c: drop stdaux/stdprn (no Sprinter printer API), change
stdin/stdout/stderr fd markers to 0/-1/-2 (positive fds clash with
ESTEX OPEN return values), add TODO header pointing at v2 buffered
FILE rewrite (see docs/TODO.md for the Solid-C reference struct).
bin/sprinter-cc:
• --memory big and --memory huge now always use crt0_banked.s (was:
only with --bank flags), matching docs/memory_modes_implemented.md.
When the user has no --bank flags, generate a tiny stub with
`const uint8_t n_banks = 0;` and assemble bank.s for _bank_pages.
Without this fix, openenv with --memory big could not see the
estex_file_handle symbol exported by crt0_banked.
examples/openenv:
• Add usage of estex_file_handle to confirm the crt0_banked startup-
info is reachable. Local extern decl — keeps the symbol out of
sprinter.h since it only exists in big/huge builds.
examples/dec_test:
• New regression test covering hex8/16/32 and dec8/16/32 across the
interesting boundary values.
.gitignore: add .kilo/ (editor session cache).
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
+37
-15
@@ -1,14 +1,40 @@
|
||||
/*
|
||||
* file.c — minimal unbuffered FILE * implementation on top of the
|
||||
* POSIX-style fd I/O (open/read/write/lseek/close).
|
||||
* file.c — *** PROVISIONAL *** minimal unbuffered FILE * implementation
|
||||
* on top of POSIX-style fd I/O
|
||||
* (open/read/write/lseek/close).
|
||||
*
|
||||
* No buffering: each fputc/fgetc maps to one read/write syscall. For
|
||||
* heavy-throughput code, prefer fread/fwrite with a sizable buffer or
|
||||
* the raw fd I/O directly.
|
||||
* ============================================================
|
||||
* TODO (v2): replace with a proper BUFFERED implementation.
|
||||
* See docs/TODO.md / Solid-C reference layout:
|
||||
*
|
||||
* stdin/stdout/stderr are STATIC sentinel FILEs with fd=-1 and the
|
||||
* _F_CONIN/_F_CONOUT flags set; fputc/fgetc detect them and call
|
||||
* putchar()/getchar() (which already do CR/LF mapping and ESTEX calls).
|
||||
* typedef struct {
|
||||
* uint flags; // +0..1 file status flags
|
||||
* int level; // +2..3 empty/fill level of buffer
|
||||
* char *curp; // +4..5 current active pointer
|
||||
* int fd; // +6..7 underlying low-level fd
|
||||
* char *buffer; // +8..9 data transfer buffer
|
||||
* char hold; // +10 ungetc byte if no buffer
|
||||
* short token; // +11..12 reserved
|
||||
* char dummy; // +13 reserved
|
||||
* } FILE;
|
||||
*
|
||||
* The current implementation maps each fputc/fgetc to one read/write
|
||||
* syscall — fine for correctness checks, awful for throughput. Issues
|
||||
* 3/4/5 from the stdio-review (fwrite short-write flag, fgets n=1,
|
||||
* mode_to_flags break) are deferred until that rewrite.
|
||||
* ============================================================
|
||||
*
|
||||
* stdin/stdout/stderr are STATIC sentinel FILEs flagged with
|
||||
* _F_CONIN/_F_CONOUT; fputc/fgetc detect them and call putchar() /
|
||||
* getchar() which already handle CR/LF translation and ESTEX calls.
|
||||
*
|
||||
* Their fd fields are 0 / -1 / -2. Negative values were chosen because
|
||||
* ESTEX OPEN can return small positive fds (1, 2, …) for ordinary
|
||||
* files — if we marked stdout/stderr with fd=1/2 a real file could
|
||||
* collide with their identifier. fd=0 for stdin is kept (POSIX-style)
|
||||
* because ESTEX does not return 0. Even so, none of these fd fields
|
||||
* is ever passed to a syscall — the _F_CONIN/_F_CONOUT flags drive
|
||||
* the dispatch.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
@@ -19,17 +45,13 @@
|
||||
#include <errno.h>
|
||||
|
||||
/* ---- console pseudo-streams ----------------------------------------*/
|
||||
static FILE _stdin = { -1, _F_READ | _F_CONIN };
|
||||
static FILE _stdout = { -2, _F_WRITE | _F_CONOUT };
|
||||
static FILE _stderr = { -3, _F_WRITE | _F_CONOUT };
|
||||
static FILE _stdaux = { -4, _F_WRITE | _F_CONOUT };
|
||||
static FILE _stdprn = { -5, _F_WRITE | _F_CONOUT };
|
||||
static FILE _stdin = { 0, _F_READ | _F_CONIN };
|
||||
static FILE _stdout = { -1, _F_WRITE | _F_CONOUT };
|
||||
static FILE _stderr = { -2, _F_WRITE | _F_CONOUT };
|
||||
|
||||
FILE *const stdin = &_stdin;
|
||||
FILE *const stdout = &_stdout;
|
||||
FILE *const stderr = &_stderr;
|
||||
FILE *const stdaux = &_stdaux;
|
||||
FILE *const stdprn = &_stdprn;
|
||||
|
||||
/* ---- fopen / fclose -------------------------------------------------*/
|
||||
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* hex_print.c — compact hex print primitives ported from solid-c
|
||||
* (third_party/solid-c/SRC/CLIB/STDLIB.ASM, modules hex8/hex16/hex32).
|
||||
*
|
||||
* void hex8 (uint8_t v) — always-2-digit "00" .. "FF"
|
||||
* void hex16(uint16_t v) — always-4-digit "0000" .. "FFFF"
|
||||
* void hex32(uint32_t v) — always-8-digit "00000000" .. "FFFFFFFF"
|
||||
*
|
||||
* Each nibble is emitted via the classic Z80 `cp 10 / sbc 0x69 / daa`
|
||||
* trick — 5 bytes per nibble. hex8 self-calls for the high nibble
|
||||
* then falls through for the low nibble. hex16/hex32 split into two
|
||||
* hex8/hex16 calls.
|
||||
*
|
||||
* ESTEX PUTCHAR ($5B) preserves IX (empirically verified) so we skip
|
||||
* the usual push/pop ix around the RST.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
void hex8(uint8_t v) __naked
|
||||
{
|
||||
(void)v;
|
||||
__asm
|
||||
;; A = v on entry.
|
||||
push af
|
||||
rra
|
||||
rra
|
||||
rra
|
||||
rra
|
||||
call _hex8_digit
|
||||
pop af
|
||||
_hex8_digit:
|
||||
and a, #0x0F
|
||||
cp a, #10
|
||||
sbc a, #0x69
|
||||
daa
|
||||
ld c, #0x5B
|
||||
rst #0x10
|
||||
ret
|
||||
__endasm;
|
||||
}
|
||||
|
||||
void hex16(uint16_t v) __naked
|
||||
{
|
||||
(void)v;
|
||||
__asm
|
||||
;; HL = v on entry.
|
||||
ld a, h
|
||||
push hl
|
||||
call _hex8
|
||||
pop hl
|
||||
ld a, l
|
||||
jp _hex8 ; tail-call
|
||||
__endasm;
|
||||
}
|
||||
|
||||
void hex32(uint32_t v) __naked
|
||||
{
|
||||
(void)v;
|
||||
__asm
|
||||
;; HL = high16, DE = low16 on entry (SDCC HLDE).
|
||||
push de
|
||||
call _hex16
|
||||
pop hl
|
||||
jp _hex16 ; tail-call
|
||||
__endasm;
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
/*
|
||||
* solid_helpers.c — small Solid-C compatibility helpers.
|
||||
*
|
||||
* Each function maps to the standard printf/sprintf machinery already
|
||||
* available from SDCC's z80.lib + our overrides. No new syscalls.
|
||||
* dec8 / dec16 / dec32 / hex8 / hex16 / hex32 are in dec_hex.c (compact
|
||||
* asm port from solid-c's STDLIB.ASM, ~150 bytes total — vs ~3-5 KB if
|
||||
* routed through printf). This file now only holds gets().
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/* ---- gets — dangerous but Solid-C provides it ---------------------- */
|
||||
char *gets(char *buf)
|
||||
@@ -25,37 +25,3 @@ char *gets(char *buf)
|
||||
buf[i] = 0;
|
||||
return buf;
|
||||
}
|
||||
|
||||
/* ---- decimal output: use printf %u ---------------------------------- */
|
||||
|
||||
void dec8(uint8_t v)
|
||||
{
|
||||
printf("%u", (unsigned)v);
|
||||
}
|
||||
|
||||
void dec16(uint16_t v)
|
||||
{
|
||||
printf("%u", (unsigned)v);
|
||||
}
|
||||
|
||||
void dec32(uint32_t v)
|
||||
{
|
||||
printf("%lu", (unsigned long)v);
|
||||
}
|
||||
|
||||
/* ---- hex output: zero-padded ---------------------------------------- */
|
||||
|
||||
void hex8(uint8_t v)
|
||||
{
|
||||
printf("%02X", (unsigned)v);
|
||||
}
|
||||
|
||||
void hex16(uint16_t v)
|
||||
{
|
||||
printf("%04X", (unsigned)v);
|
||||
}
|
||||
|
||||
void hex32(uint32_t v)
|
||||
{
|
||||
printf("%08lX", (unsigned long)v);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user