/*
 * bank_io_w1.c — far-page accessors that swap window 1 (port 0xA2,
 * base 0x4000).  Mirror of bank_io.c but through W1.
 *
 * SAFE ONLY in `--memory tiny` builds.  In tiny mode all code and data
 * fit in W2, so both W1 and W3 are free for arbitrary banked data.
 *
 *   small / huge: code lives in W1 (HOME) → swapping W1 unmaps the
 *                 function mid-call and crashes.
 *   big         : banked code segment lives in W1 → swap clobbers it.
 *
 * Split into its own .rel so that programs using only the W3 variants
 * do NOT pull these functions in (and vice versa).
 */

#include <stdint.h>
#include <string.h>
#include <sprinter.h>
#include <sprinter_mem.h>

uint8_t bank_load_byte_w1(uint8_t phys_page, uint16_t off_in_window)
{
    uint8_t saved = _io_page_w1;
    sprinter_page_w1(phys_page);
    uint8_t v = *((volatile uint8_t *)(0x4000u + off_in_window));
    sprinter_page_w1(saved);
    return v;
}

void bank_store_byte_w1(uint8_t phys_page, uint16_t off_in_window, uint8_t v)
{
    uint8_t saved = _io_page_w1;
    sprinter_page_w1(phys_page);
    *((volatile uint8_t *)(0x4000u + off_in_window)) = v;
    sprinter_page_w1(saved);
}

void bank_read_w1(uint8_t phys_page, uint16_t off, void *dst, uint16_t n)
{
    uint8_t saved = _io_page_w1;
    sprinter_page_w1(phys_page);
    memcpy(dst, (const void *)(0x4000u + off), n);
    sprinter_page_w1(saved);
}

void bank_write_w1(uint8_t phys_page, uint16_t off, const void *src, uint16_t n)
{
    uint8_t saved = _io_page_w1;
    sprinter_page_w1(phys_page);
    memcpy((void *)(0x4000u + off), src, n);
    sprinter_page_w1(saved);
}
