f256/f256lib/f_bitmap.c
2024-04-11 16:10:11 -05:00

261 lines
6 KiB
C

/*
* Copyright (c) 2024 Scott Duensing, scott@kangaroopunch.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef WITHOUT_BITMAP
#ifndef F256LIB_AMALGAMATED_BUILD
#include "f_bitmap.h"
#include "f_dma.h"
#endif
static uint16_t _MAX_X;
static uint16_t _MAX_Y;
static uint32_t _PAGE_SIZE;
static uint32_t _BITMAP_BASE[3]; // Maximum of 3 pages possible.
static byte _BITMAP_CLUT[3];
static byte _color;
static byte _active; // Current drawing page.
#define bitmapPutPixelIOSet(x, y) ({ \
uint32_t address = _BITMAP_BASE[_active] + mathUnsignedAddition(mathUnsignedMultiply(y, _MAX_X), (int32_t)x); \
byte block = address / EIGHTK; \
address &= 0x1FFF; \
POKE(SWAP_SLOT, block); \
POKE(SWAP_ADDR + address, _color); \
})
void bitmapClear(void) {
#ifdef BOOM
dmaFill(_BITMAP_BASE[_active], _PAGE_SIZE, _color);
//dma2dFill(_BITMAP_BASE[_active], _MAX_X, _MAX_Y, _MAX_X, _color);
#else
byte block = _BITMAP_BASE[_active] / EIGHTK;
byte x;
uint16_t c;
volatile byte *mem = (byte *)SWAP_ADDR;
SWAP_IO_SETUP();
// Clear full 8k blocks.
for (x=0; x<9; x++) {
POKE(SWAP_SLOT, block++);
for (c=0; c<EIGHTK; c++) mem[c] = _color;
}
// Clear last partial block.
POKE(SWAP_SLOT, block);
for (c=0; c<5120; c++) mem[c] = _color;
SWAP_RESTORE_SLOT();
SWAP_IO_SHUTDOWN();
#endif
}
void bitmapGetResolution(uint16_t *x, uint16_t *y) {
*x = _MAX_X;
*y = _MAX_Y;
}
void bitmapLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
uint16_t x;
uint16_t y;
int16_t dx;
int16_t dy;
int16_t incX;
int16_t incY;
int16_t balance;
SWAP_IO_SETUP();
if (x2 >= x1) {
dx = x2 - x1;
incX = 1;
} else {
dx = x1 - x2;
incX = -1;
}
if (y2 >= y1) {
dy = y2 - y1;
incY = 1;
} else {
dy = y1 - y2;
incY = -1;
}
x = x1;
y = y1;
if (dx >= dy) {
dy <<= 1;
balance = dy - dx;
dx <<= 1;
while (x != x2) {
bitmapPutPixelIOSet(x, y);
if (balance >= 0) {
y += incY;
balance -= dx;
}
balance += dy;
x += incX;
}
bitmapPutPixelIOSet(x, y);
} else {
dx <<= 1;
balance = dx - dy;
dy <<= 1;
while (y != y2) {
bitmapPutPixelIOSet(x, y);
if (balance >= 0) {
x += incX;
balance -= dy;
}
balance += dx;
y += incY;
}
bitmapPutPixelIOSet(x, y);
}
SWAP_RESTORE_SLOT();
SWAP_IO_SHUTDOWN();
}
void bitmapPutPixel(uint16_t x, uint16_t y) {
SWAP_IO_SETUP();
bitmapPutPixelIOSet(x, y);
SWAP_RESTORE_SLOT();
SWAP_IO_SHUTDOWN();
}
void bitmapReset(void) {
uint32_t realSize;
uint32_t pageBlocks;
_MAX_X = 320;
_MAX_Y = 240;
_PAGE_SIZE = mathUnsignedMultiply(_MAX_X, _MAX_Y);
_active = 0;
_color = 255;
// I could hardcode this, but this preserves the math so I don't forget later.
pageBlocks = _PAGE_SIZE / EIGHTK;
if (mathUnsignedMultiply(pageBlocks, EIGHTK) != _PAGE_SIZE) {
// Fractional pageBlock. Round up.
pageBlocks++;
}
realSize = mathUnsignedMultiply(pageBlocks, EIGHTK); // Works out to 80k or 0x14000.
// Bitmaps start at the top of far memory and work downwards for each additional page.
_BITMAP_BASE[0] = 0x080000 - realSize; // Page 1 = 0x6c000
_BITMAP_BASE[1] = _BITMAP_BASE[0] - realSize; // Page 2 = 0x58000
_BITMAP_BASE[2] = _BITMAP_BASE[1] - realSize; // Page 3 = 0x44000
/*
textPrint("\nbase0 = "); textPrintInt(_BITMAP_BASE[0]);
textPrint("\nbase1 = "); textPrintInt(_BITMAP_BASE[1]);
textPrint("\nbase2 = "); textPrintInt(_BITMAP_BASE[2]);
*/
_BITMAP_CLUT[0] = 0;
_BITMAP_CLUT[1] = 0;
_BITMAP_CLUT[2] = 0;
// Set up default bitmap memory.
POKEA(VKY_BM0_ADDR_L, _BITMAP_BASE[0]); // Location of bitmap data.
POKEA(VKY_BM1_ADDR_L, _BITMAP_BASE[1]); // Location of bitmap data.
POKEA(VKY_BM2_ADDR_L, _BITMAP_BASE[2]); // Location of bitmap data.
// Hide everything.
bitmapSetVisible(0, false);
bitmapSetVisible(1, false);
bitmapSetVisible(2, false);
}
void bitmapSetActive(byte p) {
_active = p;
}
void bitmapSetAddress(byte p, uint32_t a) {
_BITMAP_BASE[p] = a;
switch (p) {
case 0:
POKEA(VKY_BM0_ADDR_L, a); // Location of bitmap data.
break;
case 1:
POKEA(VKY_BM1_ADDR_L, a); // Location of bitmap data.
break;
case 2:
POKEA(VKY_BM2_ADDR_L, a); // Location of bitmap data.
break;
}
}
void bitmapSetCLUT(byte clut) {
// Convert CLUT address to bits for bitmap control registers.
_BITMAP_CLUT[_active] = clut << 1;
switch (_active) {
case 0:
POKE(VKY_BM0_CTRL, (PEEK(VKY_BM0_CTRL) & 0xf9) | _BITMAP_CLUT[_active]);
break;
case 1:
POKE(VKY_BM1_CTRL, (PEEK(VKY_BM1_CTRL) & 0xf9) | _BITMAP_CLUT[_active]);
break;
case 2:
POKE(VKY_BM2_CTRL, (PEEK(VKY_BM2_CTRL) & 0xf9) | _BITMAP_CLUT[_active]);
break;
}
}
void bitmapSetColor(byte c) {
_color = c;
}
void bitmapSetVisible(byte p, bool v) {
switch (p) {
case 0:
POKE(VKY_BM0_CTRL, v ? 1 | _BITMAP_CLUT[p] : 0); // Enable bitmap 0.
break;
case 1:
POKE(VKY_BM1_CTRL, v ? 1 | _BITMAP_CLUT[p] : 0); // Enable bitmap 1.
break;
case 2:
POKE(VKY_BM2_CTRL, v ? 1 | _BITMAP_CLUT[p] : 0); // Enable bitmap 2.
break;
}
}
#endif