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:
+38
-18
@@ -186,9 +186,15 @@ ENTRY_ADDR="${ENTRY_ADDR:-$LOAD_ADDR}"
|
||||
|
||||
# Pick crt0 source. Memory mode drives default crt0 selection; explicit
|
||||
# --crt0=TYPE still wins (e.g. --crt0=minimal for tiny w/o argv).
|
||||
#
|
||||
# big/huge ALWAYS use crt0_banked, even without explicit --bank flags:
|
||||
# big — banks in W1 (BANK_W1=1 prepended) — code stays in W2 = tiny layout
|
||||
# huge — banks in W3 — code in W1 = small layout, crt0_banked auto-detects W2
|
||||
# crt0_banked also exports startup-info symbols (estex_file_handle etc.).
|
||||
case "$MEMORY_MODE" in
|
||||
small|huge) DEFAULT_CRT0="small";;
|
||||
*) DEFAULT_CRT0="default";;
|
||||
small) DEFAULT_CRT0="small";;
|
||||
big|huge) DEFAULT_CRT0="banked";;
|
||||
*) DEFAULT_CRT0="default";; # tiny, manual
|
||||
esac
|
||||
if [[ "$CRT0_TYPE" == "default" && "$DEFAULT_CRT0" != "default" ]]; then
|
||||
CRT0_TYPE="$DEFAULT_CRT0"
|
||||
@@ -272,32 +278,46 @@ for src in "${SOURCES[@]}"; do
|
||||
USER_RELS+=("$rel")
|
||||
done
|
||||
|
||||
# 3. bank sources + trampoline (bank.s). In BIG mode banks live in W1 at
|
||||
# low16 = 0x4000; in HUGE / default mode they live in W3 at low16 = 0xC000.
|
||||
# 3. bank infrastructure — required whenever crt0_banked is in play.
|
||||
# Always assemble bank.s for _bank_pages + the bcall/bjump trampolines.
|
||||
# If the user did not pass any --bank, generate a tiny stub providing
|
||||
# n_banks = 0 (crt0_banked.s imports this symbol unconditionally).
|
||||
# In BIG mode banks live in W1 at low16 = 0x4000; in HUGE / default
|
||||
# mode they live in W3 at low16 = 0xC000.
|
||||
BANK_RELS=()
|
||||
BANK_LD_FLAGS=()
|
||||
if [[ ${#BANK_SPECS[@]} -gt 0 ]]; then
|
||||
if [[ "$CRT0_TYPE" == "banked" ]]; then
|
||||
if [[ $BANK_W1 -eq 1 ]]; then
|
||||
BANK_LOW16=0x4000
|
||||
else
|
||||
BANK_LOW16=0xC000
|
||||
fi
|
||||
# Trampoline — depends on BANK_W1 so we assemble it here, not from the lib.
|
||||
# Trampoline + _bank_pages table — depend on BANK_W1, so assemble per build.
|
||||
BANK_TRAMP_REL="$WORK/bank_trampoline.rel"
|
||||
asm_runtime "$RUNTIME/bank.s" "$BANK_TRAMP_REL"
|
||||
BANK_RELS+=("$BANK_TRAMP_REL")
|
||||
for spec in "${BANK_SPECS[@]}"; do
|
||||
bank_n="${spec%%=*}"
|
||||
bank_src="${spec#*=}"
|
||||
rel="$WORK/bank${bank_n}_$(basename "$bank_src" .c).rel"
|
||||
run "$SDCC" "${CC_FLAGS[@]}" \
|
||||
--codeseg "BANK${bank_n}" --constseg "BANK${bank_n}" --dataseg "BANK${bank_n}" \
|
||||
-c -o "$rel" "$bank_src"
|
||||
BANK_RELS+=("$rel")
|
||||
# Virtual address: bank_n in upper byte, BANK_LOW16 in low half.
|
||||
addr=$(printf "0x%X" $(( (bank_n << 16) | BANK_LOW16 )))
|
||||
BANK_LD_FLAGS+=("-Wl-b_BANK${bank_n}=${addr}")
|
||||
done
|
||||
|
||||
if [[ ${#BANK_SPECS[@]} -eq 0 ]]; then
|
||||
# User has no banks — supply n_banks=0 so crt0_banked links.
|
||||
STUB_C="$WORK/_n_banks_stub.c"
|
||||
STUB_REL="$WORK/_n_banks_stub.rel"
|
||||
echo "const unsigned char n_banks = 0;" > "$STUB_C"
|
||||
run "$SDCC" "${CC_FLAGS[@]}" -c -o "$STUB_REL" "$STUB_C"
|
||||
BANK_RELS+=("$STUB_REL")
|
||||
else
|
||||
for spec in "${BANK_SPECS[@]}"; do
|
||||
bank_n="${spec%%=*}"
|
||||
bank_src="${spec#*=}"
|
||||
rel="$WORK/bank${bank_n}_$(basename "$bank_src" .c).rel"
|
||||
run "$SDCC" "${CC_FLAGS[@]}" \
|
||||
--codeseg "BANK${bank_n}" --constseg "BANK${bank_n}" --dataseg "BANK${bank_n}" \
|
||||
-c -o "$rel" "$bank_src"
|
||||
BANK_RELS+=("$rel")
|
||||
# Virtual address: bank_n in upper byte, BANK_LOW16 in low half.
|
||||
addr=$(printf "0x%X" $(( (bank_n << 16) | BANK_LOW16 )))
|
||||
BANK_LD_FLAGS+=("-Wl-b_BANK${bank_n}=${addr}")
|
||||
done
|
||||
fi
|
||||
fi
|
||||
|
||||
# 4. link → .ihx
|
||||
|
||||
Reference in New Issue
Block a user