libc: add Sprinter palette API (graphics + text planes)

palette.h/video/palette.c wrap BIOS $A4 PIC_SET_PAL/PIC_GET_PAL and $A6
SET_PAL_INIT for the 8 palette pages (0-3 graphics, 4-7 text planes).
gfx_palette.c and conio/text_palette.c are thin per-domain wrappers;
text_palette.c maps the text "plane" 0..3 (paper/ink/blink-paper/
blink-ink) onto BIOS pages 4..7. Covered by tests/text_palette.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-23 17:07:55 +03:00
parent 5e5e70d0c8
commit 49a87b4d02
6 changed files with 501 additions and 0 deletions
+42
View File
@@ -0,0 +1,42 @@
/*
* text_palette.c — text-mode palette wrappers.
*
* Thin layer over libc/video/palette.c. Translates the text "plane
* number" 0..3 (paper/ink/blink-paper/blink-ink) into the underlying
* BIOS palette page 4..7 used by $A4.
*
* For an introduction to the four-plane text colour model see
* <palette.h> (top-of-file doc-block).
*/
#include <stdint.h>
#include <conio.h>
#include <palette.h>
void text_pal_load(uint8_t plane, uint8_t start, uint8_t count,
const uint8_t *data)
{
pal_load((uint8_t)(plane + 4u), start, count, data);
}
void text_pal_set_color(uint8_t plane, uint8_t attr,
uint8_t r, uint8_t g, uint8_t b)
{
pal_set_color((uint8_t)(plane + 4u), attr, r, g, b);
}
void text_pal_get(uint8_t plane, uint8_t start, uint8_t count, uint8_t *data)
{
pal_get((uint8_t)(plane + 4u), start, count, data);
}
void text_pal_get_color(uint8_t plane, uint8_t attr,
uint8_t *r, uint8_t *g, uint8_t *b)
{
pal_get_color((uint8_t)(plane + 4u), attr, r, g, b);
}
void text_pal_reset(void)
{
pal_reset(PAL_CGA);
}
+74
View File
@@ -0,0 +1,74 @@
/*
* gfx_palette.c — graphics-side palette wrappers.
*
* The "legacy" entry points (gfx_pal_load / gfx_pal_set) keep their own
* inline-asm implementation rather than thunking through pal_*; this
* avoids depending on SDCC's __sdcccall(1) stack-shuffling for nested
* calls, which empirically misbehaves here (the trampoline corrupts the
* text palette so the next text-mode print hangs). Functionally these
* are identical to the old gfx_core.c versions.
*
* The newer get / reset helpers are simple wrappers — they're either
* not on hot paths or are first introduced here, so the thunk overhead
* doesn't matter and we get to share the asm with libc/video/palette.c.
*/
#include <stdint.h>
#include <gfx.h>
#include <palette.h>
static uint8_t gpl_num_;
static uint8_t gpl_start_;
static uint8_t gpl_count_;
static uint16_t gpl_data_;
void gfx_pal_load(uint8_t pal_num, uint8_t start, uint8_t count,
const uint8_t *data)
{
gpl_num_ = pal_num;
gpl_start_ = start;
gpl_count_ = count;
gpl_data_ = (uint16_t)(uintptr_t)data;
__asm
push ix
ld a, (_gpl_start_)
ld e, a ; E = start
ld a, (_gpl_count_)
ld d, a ; D = count (0 256)
ld hl, (_gpl_data_) ; HL = data
ld b, #0xFF ; mask
ld a, (_gpl_num_) ; A = palette number
ld c, #0xA4 ; BIOS PIC_SET_PAL
rst #0x08
pop ix
__endasm;
}
void gfx_pal_set(uint8_t pal_num, uint8_t idx,
uint8_t r, uint8_t g, uint8_t b)
{
uint8_t entry[4];
entry[0] = b;
entry[1] = g;
entry[2] = r;
entry[3] = 0;
gfx_pal_load(pal_num, idx, 1, entry);
}
void gfx_pal_get(uint8_t pal_num, uint8_t start, uint8_t count,
uint8_t *data)
{
pal_get(pal_num, start, count, data);
}
void gfx_pal_get_color(uint8_t pal_num, uint8_t idx,
uint8_t *r, uint8_t *g, uint8_t *b)
{
pal_get_color(pal_num, idx, r, g, b);
}
void gfx_pal_reset(void)
{
pal_reset(PAL_GRAPH);
}
+89
View File
@@ -0,0 +1,89 @@
/*
* palette.h — Sprinter palette (low-level).
*
* The Sprinter has eight 256-colour palette pages, shared by graphics and
* text modes:
*
* pal_num 0..3 — graphics palettes. Each character cell selects which
* of the four it uses through bits 7..6 of its mode byte.
* Used by graphics modes 0x81 / 0x82.
* pal_num 4..7 — text-mode planes — together they form the colour table
* of the text-mode attribute byte:
* 4 = paper (background, non-blink phase)
* 5 = ink (foreground, non-blink phase)
* 6 = paper-blink (background during blink half-cycle)
* 7 = ink-blink (foreground during blink half-cycle)
* Each plane holds 256 BGR colours indexed directly by
* the 8-bit attribute byte of the cell.
*
* Entry format — 4 bytes per colour, in Blue-Green-Red-pad order; the pad
* byte is reserved and must be 0.
*
* Notes on the underlying BIOS ($A4):
* - `count = 0` means 256 entries (full plane).
* - On write, data is AND-masked against `pal_mask` before reaching
* VRAM. pal_load/pal_set_color hard-code mask = 0xFF (no masking).
*
* Blink semantics (text planes):
* The hardware constantly alternates between planes 4↔6 (paper) and
* 5↔7 (ink). To DISABLE blink everywhere in IBM-CGA style, copy plane
* 4 → 6 and 5 → 7 (so both phases show the same colour). To ENABLE
* ZX-Spectrum style flash, swap entries 6 ↔ 7 for the desired attrs.
* The system default (PAL_CGA) leaves flash visible only for attribute
* bytes with bit 7 set.
*
* Backed by BIOS PIC_SET_PAL/PIC_GET_PAL ($A4) and SET_PAL_INIT ($A6).
* Higher-level wrappers live in <conio.h> (text_pal_*) and <gfx.h>
* (gfx_pal_*); use those directly unless you need raw plane control.
*/
#ifndef PALETTE_H
#define PALETTE_H
#include <stdint.h>
/* Default-palette types for pal_reset(). Values match BIOS $A6 B-register. */
#define PAL_GRAPH 1 /* graphics palette (planes 0..3) */
#define PAL_SINCLAIR 2 /* Spectrum palette (text planes 4..7, ZX colours) */
#define PAL_CGA 3 /* CGA text palette (text planes 4..7, IBM CGA) */
/* Load a contiguous block of palette entries.
* pal_num: 0..7 (0..3 graphics, 4..7 text)
* start: first slot (0..255)
* count: number of slots (0 means 256)
* bgr0: pointer to count entries of 4 bytes each: Blue, Green, Red, 0 */
void pal_load(uint8_t pal_num, uint8_t start, uint8_t count,
const uint8_t *bgr0);
/* Read a contiguous block of palette entries back into RAM.
* Same parameter shape as pal_load — bgr0 here is a write buffer of
* count*4 bytes that receives B,G,R,0 quadruples. */
void pal_get (uint8_t pal_num, uint8_t start, uint8_t count,
uint8_t *bgr0);
/* Convenience: set one entry from an RGB triple. */
void pal_set_color(uint8_t pal_num, uint8_t slot,
uint8_t r, uint8_t g, uint8_t b);
/* Convenience: read one entry into R,G,B pointers (any may be NULL). */
void pal_get_color(uint8_t pal_num, uint8_t slot,
uint8_t *r, uint8_t *g, uint8_t *b);
/* Restore a built-in default palette (BIOS $A6 SET_PAL_INIT).
* type: PAL_GRAPH / PAL_SINCLAIR / PAL_CGA.
* Internally:
* PAL_GRAPH → A=0, E=0, B=1 (resets graphics palette 0)
* PAL_SINCLAIR → A=0, E=0, B=2
* PAL_CGA → A=0, E=0, B=3 (resets all text planes 4..7)
*
* Use pal_reset_at() if you need a non-zero page or a non-zero graphics
* palette index. */
void pal_reset(uint8_t type);
/* Full-control variant of pal_reset.
* pal_page: BIOS A register — "palette page" hardware index.
* graph_pal: BIOS E register — for PAL_GRAPH, target palette 0..3.
* type: BIOS B register — PAL_GRAPH / PAL_SINCLAIR / PAL_CGA. */
void pal_reset_at(uint8_t type, uint8_t pal_page, uint8_t graph_pal);
#endif
+131
View File
@@ -0,0 +1,131 @@
/*
* palette.c — Sprinter palette (BIOS $A4 PIC_SET_PAL / PIC_GET_PAL,
* BIOS $A6 SET_PAL_INIT).
*
* Direction in $A4 is selected by bit 7 of A:
* A = pal_num → write entries from RAM to VRAM
* A = 0x80 | pal_num → read entries from VRAM into RAM
*
* Lives under libc/video/ because palette control is shared between
* graphics (mode 0x81/0x82) and text (mode 0x03) — pal_num 0..3 for
* graphics, 4..7 for the four text-mode planes. The conio and gfx
* subsystems each ship thin domain-specific wrappers on top.
*/
#include <stdint.h>
#include <palette.h>
/* Statics used to pass parameters to the inline asm — SDCC __sdcccall(1)
* gives us only HL natively, so the rest go through memory. */
static uint8_t pal_num_;
static uint8_t pal_start_;
static uint8_t pal_count_;
static uint16_t pal_data_;
/* ---- $A4 PIC_SET_PAL — write entries to VRAM --------------------- */
void pal_load(uint8_t pal_num, uint8_t start, uint8_t count,
const uint8_t *bgr0)
{
pal_num_ = pal_num;
pal_start_ = start;
pal_count_ = count;
pal_data_ = (uint16_t)(uintptr_t)bgr0;
__asm
push ix
ld a, (_pal_start_)
ld e, a ; E = start
ld a, (_pal_count_)
ld d, a ; D = count (0 256)
ld hl, (_pal_data_) ; HL = data
ld b, #0xFF ; B = mask (no AND)
ld a, (_pal_num_) ; A = palette number (bit7 = 0 write)
ld c, #0xA4 ; BIOS PIC_SET_PAL
rst #0x08
pop ix
__endasm;
}
/* ---- $A4 PIC_GET_PAL — read entries from VRAM -------------------- *
* Same function id; bit 7 of A flips it to read mode. */
void pal_get(uint8_t pal_num, uint8_t start, uint8_t count, uint8_t *bgr0)
{
pal_num_ = (uint8_t)(pal_num | 0x80); /* bit 7 = read */
pal_start_ = start;
pal_count_ = count;
pal_data_ = (uint16_t)(uintptr_t)bgr0;
__asm
push ix
ld a, (_pal_start_)
ld e, a ; E = start
ld a, (_pal_count_)
ld d, a ; D = count (0 256)
ld hl, (_pal_data_) ; HL = buffer
ld b, #0xFF ; B = mask
ld a, (_pal_num_) ; A = 0x80 | pal_num read
ld c, #0xA4 ; BIOS PIC_GET_PAL (= $A4)
rst #0x08
pop ix
__endasm;
}
/* ---- one-colour helpers ------------------------------------------ */
void pal_set_color(uint8_t pal_num, uint8_t slot,
uint8_t r, uint8_t g, uint8_t b)
{
uint8_t entry[4];
entry[0] = b;
entry[1] = g;
entry[2] = r;
entry[3] = 0;
pal_load(pal_num, slot, 1, entry);
}
void pal_get_color(uint8_t pal_num, uint8_t slot,
uint8_t *r, uint8_t *g, uint8_t *b)
{
uint8_t entry[4];
pal_get(pal_num, slot, 1, entry);
if (b) *b = entry[0];
if (g) *g = entry[1];
if (r) *r = entry[2];
}
/* ---- $A6 SET_PAL_INIT — restore a built-in default palette ------- *
* Signature: A = pal_page, E = graphics palette index (0..3), B = type.
* Type is one of PAL_GRAPH (1) / PAL_SINCLAIR (2) / PAL_CGA (3). */
static uint8_t reset_page_;
static uint8_t reset_graph_;
static uint8_t reset_type_;
void pal_reset_at(uint8_t type, uint8_t pal_page, uint8_t graph_pal)
{
reset_type_ = type;
reset_page_ = pal_page;
reset_graph_ = graph_pal;
__asm
push ix
ld a, (_reset_graph_)
ld e, a ; E = graphics palette index (0..3)
ld a, (_reset_type_)
ld b, a ; B = type (1=GRAPH, 2=SINCLAIR, 3=CGA)
ld a, (_reset_page_) ; A = palette page (last A is needed)
ld c, #0xA6 ; BIOS SET_PAL_INIT
rst #0x08
pop ix
__endasm;
}
void pal_reset(uint8_t type)
{
pal_reset_at(type, 4, 0);
pal_reset_at(type, 5, 0);
pal_reset_at(type, 6, 0);
pal_reset_at(type, 7, 0);
}