Files
VCCPlusOSS/sdk/cmoc/bank/bank.c
T

220 lines
4.9 KiB
C

// SPDX-License-Identifier: GPL-3.0-or-later
#include <vcc/bank.h>
typedef unsigned char byte;
#define WINDOW 0x8000u
#define TASK0 ((byte *)0xFFA0)
#define CHUNK 32
static byte active;
static byte marker;
static asm byte lowProgramAndStack(void)
{
asm {
program_end import
program_start import
s_rodata import
l_rodata import
s_rwdata import
ldx #s_rodata
cmpx #program_start
blo @no
ldx #s_rwdata
cmpx #s_rodata+l_rodata
blo @no
ldx #program_end
cmpx #s_rwdata
blo @no
cmpx #$8000
bhi @no
cmps #$8000
bhi @no
ldb #1
rts
@no clrb
}
}
static asm byte irqEnabled(void)
{
asm {
tfr cc,b
bitb #$10
bne @no
ldb #1
rts
@no clrb
}
}
static asm unsigned programStart(void)
{
asm {
program_start import
ldd #program_start
}
}
static asm unsigned writableStart(void)
{
asm {
s_rwdata import
ldd #s_rwdata
}
}
// Read back the real MMU register. A NOP ($12) supplies zero high bus bits
// on a 128/512 KiB machine, whose register drives only the bottom six bits.
static asm byte readPage(const byte *entry)
{
asm {
ldx 2,s
ldb ,x
nop
}
}
// Prove that task 0 bank 4 can actually select the requested banks. First
// read our own low-memory marker through a second address, twice. Then map
// the candidate bank and check if changing the marker is visible there.
// On 128 KiB, $28 aliases $38 (and $29 aliases $39, etc.). All writes in
// this probe go to our own byte, never through the borrowed window.
static byte independent(byte ordinary, byte candidate, byte *window, byte *local)
{
byte ok;
asm {
pshs cc,y
orcc #$50
lda >$ffa4
nop
pshs a
ldx :local
ldy :window
lda :ordinary
sta >$ffa4
lda #$5a
sta ,x
cmpa ,y
bne @reject
coma
sta ,x
cmpa ,y
bne @reject
lda :candidate
sta >$ffa4
lda ,y
coma
sta ,x
cmpa ,y
beq @reject
ldb #1
bra @restore
@reject clrb
@restore
clr ,x
puls a
sta >$ffa4
puls cc,y
stb :ok
}
return ok;
}
static byte usableMap(void)
{
unsigned address = (unsigned)&marker;
byte slot = (byte)(address >> 13);
byte i, page;
if (!lowProgramAndStack() || slot >= 4 || *(unsigned *)0xfff8 != 0xfef7)
return 0;
for (i = 0; i < 16; ++i) {
page = readPage(TASK0 + i);
if (i < 8 && page != 0x38 + i)
return 0;
if (page >= BANK_FIRST_PAGE && page < BANK_FIRST_PAGE + 4)
return 0;
}
return independent(0x38 + slot, BANK_FIRST_PAGE + slot,
(byte *)(WINDOW + (address & (BANK_PAGE_SIZE - 1))), &marker);
}
byte bank_open(void)
{
if (active || !irqEnabled() || !usableMap())
return 0;
active = 1;
return 1;
}
void bank_close(void)
{
active = 0;
}
// All addresses and the byte count are prepared before remapping. Keep a
// 32-byte bound on the masked interval so BASIC can service interrupts even
// when the caller transfers a full asset in one request.
static void copyChunk(byte page, const byte *source, byte *destination, byte count)
{
asm {
pshs cc,y
orcc #$50
lda >$ffa4
nop
pshs a
ldx :source
ldy :destination
ldb :count
lda :page
sta >$ffa4
@copy lda ,x+
sta ,y+
decb
bne @copy
puls a
sta >$ffa4
puls cc,y
}
}
static byte transfer(unsigned offset, byte *buffer, unsigned length, byte writing)
{
unsigned address = (unsigned)buffer;
if (!active || offset > BANK_CAPACITY || length > BANK_CAPACITY - offset)
return 0;
if (!length)
return 1;
if (address < (writing ? programStart() : writableStart()) ||
address >= WINDOW || length > WINDOW - address || !usableMap())
return 0;
while (length) {
unsigned inPage = offset & (BANK_PAGE_SIZE - 1);
unsigned count = BANK_PAGE_SIZE - inPage;
byte page = (byte)(BANK_FIRST_PAGE + (offset >> 13));
byte *window = (byte *)(WINDOW + inPage);
if (count > CHUNK) count = CHUNK;
if (count > length) count = length;
if (writing)
copyChunk(page, buffer, window, (byte) count);
else
copyChunk(page, window, buffer, (byte) count);
buffer += count;
offset += count;
length -= count;
}
return 1;
}
byte bank_read(unsigned offset, void *destination, unsigned length)
{
return transfer(offset, (byte *)destination, length, 0);
}
byte bank_write(unsigned offset, const void *source, unsigned length)
{
return transfer(offset, (byte *)source, length, 1);
}