Files
Sprinter-SDCC/libc/include/sprinter_mem.h
T
snark13 527d4a6a18 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>
2026-06-04 09:26:10 +03:00

104 lines
4.7 KiB
C

/*
* sprinter_mem.h — banking-aware page allocator and bank-data accessors.
*
* For data that doesn't fit in window 2's heap, allocate physical pages
* directly through ESTEX EMM and access them via bank_read / bank_write
* (which swap a CPU window internally).
*
* mem_alloc_pages(N) — reserve N contiguous 16 KB pages, return block id.
* mem_free_block(id) — release a previously-allocated block.
* mem_get_page(id, i) — translate (block, page-index) to physical page.
* mem_info(&total, &free) — query EMM about total/free 16 KB pages.
*
* bank_load_byte / bank_store_byte — single-byte access through W3.
* bank_read / bank_write — bulk copy through W3.
* bank_*_w1 — same family but through W1 (for
* memory modes where code lives in
* W2 and W1 is free for data).
*
* Each helper saves the previous window-mapping, swaps to the target
* physical page, does the access, restores the saved page.
*
* SAFETY by memory mode (-memory FLAG to sprinter-cc):
*
* `tiny` : both bank_* and bank_*_w1 are safe.
* `small`/`huge`: only bank_* (W3) is safe; bank_*_w1 CRASHES
* because code lives in W1.
* `big` : only bank_* (W3) is safe; bank_*_w1 would clobber
* the banked-code segment in W1.
* `huge` (data path through W3 banked code): bank_* (W3) is unsafe;
* avoid banking via W3 in huge programs.
*
* In short: `_w1` accessors are a `tiny`-mode optimisation, useful when
* the program is small enough to fit in W2 and wants both W1 and W3 as
* independent banked-data windows.
*/
#ifndef SPRINTER_MEM_H
#define SPRINTER_MEM_H
#include <stdint.h>
/* ===================================================================
* ESTEX EMM allocator wrappers
* =================================================================== */
/* Allocate `n` contiguous 16-KB physical pages from the EMM pool.
* n : 1..255
* ret : blk_id (1..255) on success; 0 on failure with errno set.
* The id is opaque — pass it to mem_get_page() and mem_free_block(). */
uint8_t mem_alloc_pages(uint8_t n);
/* Release a block previously returned by mem_alloc_pages().
* On error errno is set (e.g. EINVAL for unknown id). Double-free is
* NOT idempotent: the second call sets errno. */
void mem_free_block(uint8_t blk_id);
/* Translate (block, page-index) into a physical page number suitable
* for sprinter_page_w1/w2/w3() or the bank_*() helpers below.
* blk_id: from mem_alloc_pages()
* idx : 0..(n-1)
* ret : physical page (1..255) on success; 0 on failure (errno set). */
uint8_t mem_get_page(uint8_t blk_id, uint8_t idx);
/* Query the EMM allocator state. Both pointers must be non-NULL.
* Cannot fail (no error path). */
void mem_info(uint16_t *total, uint16_t *free_pages);
/* ===================================================================
* Far-page accessors via window 3 (base 0xC000, port 0xE2)
*
* Each call saves the current W3 mapping, sets W3 to `phys_page`, does
* the access at (base + off_in_window), then restores the saved page.
* Safe in `tiny`, `small`, `big` memory modes (W3 is free for data).
* NOT safe in `huge` — banked code lives in W3 there.
* =================================================================== */
/* Read one byte from phys_page at offset off_in_window (0..0x3FFF). */
uint8_t bank_load_byte(uint8_t phys_page, uint16_t off_in_window);
/* Write byte `v` into phys_page at offset off_in_window. */
void bank_store_byte(uint8_t phys_page, uint16_t off_in_window, uint8_t v);
/* Copy `n` bytes from phys_page[off..off+n-1] into the near buffer `dst`. */
void bank_read(uint8_t phys_page, uint16_t off, void *dst, uint16_t n);
/* Copy `n` bytes from near buffer `src` into phys_page[off..off+n-1]. */
void bank_write(uint8_t phys_page, uint16_t off, const void *src, uint16_t n);
/* ===================================================================
* Far-page accessors via window 1 (base 0x4000, port 0xA2)
*
* Same semantics as the W3 family but swap W1 instead. SAFE ONLY in
* `--memory tiny` builds — in any other mode code lives in (or uses) W1
* and swapping it mid-call will crash. See header notes above for the
* full safety matrix.
* =================================================================== */
uint8_t bank_load_byte_w1(uint8_t phys_page, uint16_t off_in_window);
void bank_store_byte_w1(uint8_t phys_page, uint16_t off_in_window, uint8_t v);
void bank_read_w1(uint8_t phys_page, uint16_t off, void *dst, uint16_t n);
void bank_write_w1(uint8_t phys_page, uint16_t off, const void *src, uint16_t n);
#endif