/* * 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. */ #include "bitmap.h" #include "dma.h" #define EIGHTK 0x2000 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 _page; // Current drawing page. static byte _pages; // Number of active pages. void bitmapClear(void) { #ifdef BOOM dmaFill(_BITMAP_BASE[_page], _PAGE_SIZE, _color); //dma2dFill(_BITMAP_BASE[_page], _MAX_X, _MAX_Y, _MAX_X, _color); #else byte block = _BITMAP_BASE[_page] / EIGHTK; byte x; uint16_t c; volatile byte *mem = (byte *)SWAP_ADDR; // Clear full 8k blocks. for (x=0; x<9; x++) { POKE(SWAP_SLOT, block++); for (c=0; c= 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) { bitmapPutPixel(x, y); if (balance >= 0) { y += incY; balance -= dx; } balance += dy; x += incX; } bitmapPutPixel(x, y); } else { dx <<= 1; balance = dx - dy; dy <<= 1; while (y != y2) { bitmapPutPixel(x, y); if (balance >= 0) { x += incX; balance -= dy; } balance += dx; y += incY; } bitmapPutPixel(x, y); } } void bitmapPutPixel(uint16_t x, uint16_t y) { uint32_t pixelRAM; byte block; pixelRAM = _BITMAP_BASE[_page] + mathUnsignedAddition(mathUnsignedMultiply(y, _MAX_X), (int32_t)x); block = pixelRAM / EIGHTK; pixelRAM &= 0x1FFF; // Find offset into this block. POKE(SWAP_SLOT, block); POKE(SWAP_ADDR + pixelRAM, _color); } void bitmapReset(void) { uint32_t realSize; uint32_t pageBlocks; _MAX_X = 320; _MAX_Y = 240; _PAGE_SIZE = mathUnsignedMultiply(_MAX_X, _MAX_Y); _pages = 1; _page = 0; _color = 255; // I could hardcode this, but this preserves the math so I don't forget later. pageBlocks = _PAGE_SIZE / EIGHTK; if ((pageBlocks * EIGHTK) != _PAGE_SIZE) { // Fractional pageBlock. Round up. pageBlocks++; } realSize = mathUnsignedMultiply(pageBlocks, EIGHTK); _BITMAP_BASE[0] = 0x10000; _BITMAP_BASE[1] = mathUnsignedAddition(_BITMAP_BASE[0], realSize); // Page 2 = 0x24000 _BITMAP_BASE[2] = mathUnsignedAddition(_BITMAP_BASE[1], realSize); // Page 3 = 0x38000 /* 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. bitmapShowNone(); } 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(uint16_t clut) { // Convert CLUT address to bits for bitmap control registers. switch (clut) { case GRAPHICS_CLUT_0: _BITMAP_CLUT[_page] = 0; break; case GRAPHICS_CLUT_1: _BITMAP_CLUT[_page] = 2; break; case GRAPHICS_CLUT_2: _BITMAP_CLUT[_page] = 4; break; case GRAPHICS_CLUT_3: _BITMAP_CLUT[_page] = 6; break; } switch (_page) { case 0: POKE(VKY_BM0_CTRL, (PEEK(VKY_BM0_CTRL) & 0xf9) | _BITMAP_CLUT[_page]); break; case 1: POKE(VKY_BM1_CTRL, (PEEK(VKY_BM1_CTRL) & 0xf9) | _BITMAP_CLUT[_page]); break; case 2: POKE(VKY_BM2_CTRL, (PEEK(VKY_BM2_CTRL) & 0xf9) | _BITMAP_CLUT[_page]); break; } } void bitmapSetColor(byte c) { _color = c; } void bitmapSetPage(byte p) { _page = p; } void bitmapShowNone(void) { POKE(VKY_BM0_CTRL, 0); POKE(VKY_BM1_CTRL, 0); POKE(VKY_BM2_CTRL, 0); } void bitmapShowPage(byte p) { POKE(VKY_BM0_CTRL, p == 0 ? 1 | _BITMAP_CLUT[_page] : 0); // Enable bitmap 0, GLUT 0. POKE(VKY_BM1_CTRL, p == 1 ? 1 | _BITMAP_CLUT[_page] : 0); POKE(VKY_BM2_CTRL, p == 2 ? 1 | _BITMAP_CLUT[_page] : 0); }