Lots of refactoring to get tiles and sprites supported.

This commit is contained in:
Scott Duensing 2024-01-09 21:46:37 -06:00
parent 93c28f3430
commit a558f1a472
18 changed files with 579 additions and 139 deletions

View file

@ -27,15 +27,13 @@
#include "f256.h"
int16_t _remainder;
#define FIX_PREC 9
#define TO_FIX(x) ((int32_t)((x)*(1<<FIX_PREC))) // This is only used for initialization and needs to be constant.
#define TO_LONG(x) (x>>FIX_PREC) // ((x)/(1<<FIX_PREC))
#define fix_mul(am,bm) (mathSignedMultiply(am, bm) >> FIX_PREC)
#define fix_sqr(am) (mathSignedMultiply(am, am) >> FIX_PREC)
#define fix_div(num,den) (mathSignedMultiply(mathUnSignedDivision(0x8000, (den >> 1), (uint16_t *)&_remainder), num) >> 7) // ((a << FIX_PREC) / b)
#define fix_div(num,den) (mathSignedMultiply(mathUnsignedDivision(0x8000, (den >> 1)), num) >> 7) // ((a << FIX_PREC) / b)
#define SIN_SIZE 512
#define COS_OFF 128
@ -103,8 +101,8 @@ typedef struct lineS {
} lineT;
int16_t *COS = SIN + COS_OFF;
uint16_t width;
uint16_t height;
uint16_t widthOffset;
uint16_t heightOffset;
int32_t a = TO_FIX(4);
int32_t scale = TO_FIX(40);
lineT past[12][2];
@ -158,8 +156,8 @@ void draw_cube(byte p, int16_t t) {
// Projection to screen space
cubeProjX[i] = fix_div(fix_mul(a, cubeRotX[i]), cubeRotZ[i]);
cubeProjY[i] = fix_div(fix_mul(a, cubeRotY[i]), cubeRotZ[i]);
cubeProjX[i] = TO_LONG(fix_mul(cubeProjX[i], scale)) + width;
cubeProjY[i] = TO_LONG(fix_mul(cubeProjY[i], scale)) + height;
cubeProjX[i] = TO_LONG(fix_mul(cubeProjX[i], scale)) + widthOffset;
cubeProjY[i] = TO_LONG(fix_mul(cubeProjY[i], scale)) + heightOffset;
}
l = 0;
@ -183,9 +181,16 @@ int main(void) {
textSetCursor(0); // No cursor.
bitmapGetResolution(&width, &height);
width = width >> 1;
height = height >> 1;
// Clear two graphics pages.
bitmapSetColor(0);
bitmapSetPage(0);
bitmapClear();
bitmapSetPage(1);
bitmapClear();
bitmapGetResolution(&widthOffset, &heightOffset);
widthOffset = widthOffset >> 1;
heightOffset = heightOffset >> 1;
while(1) {
if (p) {

View file

@ -97,6 +97,5 @@ int main(void) {
f256Init();
text();
bitmap();
//bitmapReset(); // BOOM!
return 0;
}

View file

@ -32,8 +32,10 @@ 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;
static byte _page; // Current drawing page.
static byte _pages; // Number of active pages.
void bitmapClear(void) {
@ -44,36 +46,20 @@ void bitmapClear(void) {
byte block = _BITMAP_BASE[_page] / EIGHTK;
byte x;
uint16_t c;
volatile byte *mem = (byte *)0xa000;
volatile byte *mem = (byte *)SWAP_ADDR;
// Clear full 8k blocks.
for (x=0; x<9; x++) {
POKE(MMU_MEM_BANK_5, block++);
POKE(SWAP_SLOT, block++);
for (c=0; c<EIGHTK; c++) mem[c] = _color;
}
// Clear last partial block.
POKE(MMU_MEM_BANK_5, block);
POKE(SWAP_SLOT, block);
for (c=0; c<5120; c++) mem[c] = _color;
#endif
}
void bitmapDefineColor(byte slot, byte r, byte g, byte b) {
byte mmu = PEEK(MMU_IO_CTRL); // Get current MMU state.
byte *write;
POKE(MMU_IO_CTRL, MMU_IO_PAGE_1); // Swap I/O page 1 into bank 6.
write = (byte *)VKY_GR_CLUT_0 + slot * 4;
*write++ = b;
*write++ = g;
*write++ = r;
*write++ = 0xff;
POKE(MMU_IO_CTRL, mmu); // Restore MMU state.
}
void bitmapGetResolution(uint16_t *x, uint16_t *y) {
*x = _MAX_X;
*y = _MAX_Y;
@ -144,25 +130,24 @@ void bitmapPutPixel(uint16_t x, uint16_t y) {
uint32_t pixelRAM;
byte block;
// We only map 8k of the bitmap into CPU RAM at once.
// We use slot 5 for this. We need to figure out
// where our pixel lands and bring that into RAM.
pixelRAM = _BITMAP_BASE[_page] + ((int32_t)y * (int32_t)_MAX_X) + (int32_t)x;
pixelRAM = _BITMAP_BASE[_page] + mathUnsignedAddition(mathUnsignedMultiply(y, _MAX_X), (int32_t)x);
block = pixelRAM / EIGHTK;
pixelRAM &= 0x1FFF; // Find offset into this block.
POKE(MMU_MEM_BANK_5, block);
POKE(0xa000 + pixelRAM, _color);
POKE(SWAP_SLOT, block);
POKE(SWAP_ADDR + pixelRAM, _color);
}
void bitmapReset(void) {
int x;
uint32_t realSize;
uint32_t pageBlocks;
_MAX_X = 320;
_MAX_Y = 240;
_PAGE_SIZE = (uint32_t)_MAX_X * (uint32_t)_MAX_Y;
_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;
@ -170,11 +155,11 @@ void bitmapReset(void) {
// Fractional pageBlock. Round up.
pageBlocks++;
}
realSize = pageBlocks * EIGHTK;
realSize = mathUnsignedMultiply(pageBlocks, EIGHTK);
_BITMAP_BASE[0] = 0x10000;
_BITMAP_BASE[1] = _BITMAP_BASE[0] + realSize; // Page 2 = 0x24000
_BITMAP_BASE[2] = _BITMAP_BASE[1] + realSize; // Page 3 = 0x38000
_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]);
@ -182,30 +167,62 @@ void bitmapReset(void) {
textPrint("\nbase2 = "); textPrintInt(_BITMAP_BASE[2]);
*/
// Set palette to a gradient so there's at least *something*.
for (x=0; x<256; x++) bitmapDefineColor(x, x, x, x);
_BITMAP_CLUT[0] = 0;
_BITMAP_CLUT[1] = 0;
_BITMAP_CLUT[2] = 0;
POKE(VKY_LAYER_CTRL_0, 16); // Bitmaps on all layers.
POKE(VKY_LAYER_CTRL_1, 2); // Bitmaps on all layers.
// Turn everthing on for setup.
POKE(VKY_BM0_CTRL, 1);
POKE(VKY_BM1_CTRL, 1);
POKE(VKY_BM2_CTRL, 1);
// Set up bitmap memory.
// 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.
// Enable the first bitmap.
bitmapShowPage(0);
bitmapShowNone();
}
// Clear all pages.
_color = 0;
for (_page=0; _page<3; _page++) bitmapClear();
_page = 0;
_color = 255;
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;
}
}
@ -219,8 +236,15 @@ void bitmapSetPage(byte p) {
}
void bitmapShowPage(byte p) {
POKE(VKY_BM0_CTRL, p == 0 ? 1 : 0); // Enable bitmap 0, GLUT 0.
POKE(VKY_BM1_CTRL, p == 1 ? 1 : 0);
POKE(VKY_BM2_CTRL, p == 2 ? 1 : 0);
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);
}

View file

@ -35,13 +35,15 @@ extern "C"
void bitmapClear(void);
void bitmapDefineColor(byte slot, byte r, byte g, byte b);
void bitmapGetResolution(uint16_t *x, uint16_t *y);
void bitmapLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
void bitmapPutPixel(uint16_t x, uint16_t y);
void bitmapReset(void);
void bitmapSetCLUT(uint16_t clut);
void bitmapSetColor(byte c);
void bitmapSetPage(byte p);
void bitmapSetAddress(byte p, uint32_t a);
void bitmapShowNone(void);
void bitmapShowPage(byte p);

View file

@ -61,5 +61,5 @@ static void dmaWait(void) {
POKE(DMA_CTRL, 0);
// Then wait for a VBL because two DMAs per frame will crash.
waitVerticalBlank();
graphicsWaitVerticalBlank();
}

View file

@ -23,21 +23,20 @@
#include "f256.h"
#include "text.c"
#include "bitmap.c"
#include "kernel.c"
#include "dma.c"
#include "math.c"
char error;
#include "random.c"
#include "text.c"
#include "bitmap.c"
#include "tile.c"
#include "graphics.c"
void f256Init(void) {
// Swap I/O page 0 into bank 6. This is our normal state.
POKE(MMU_IO_CTRL, MMU_IO_PAGE_0);
//POKE(VKY_MSTR_CTRL_0, 1); // Enable text.
//POKE(VKY_MSTR_CTRL_0, 12); // Enable bitmaps.
POKE(VKY_MSTR_CTRL_0, 15); // Enable text and bitmaps.
//POKE(VKY_MSTR_CTRL_0, 63); // Enable text and all graphics.
//POKE(VKY_MSTR_CTRL_1, 20); // Enable FON_OVLY and DBL_Y.
@ -56,33 +55,7 @@ void f256Init(void) {
randomSeed(0); //***TODO*** Use clock or something.
graphicsReset();
textReset();
bitmapReset();
}
uint16_t randomRead(void) {
uint16_t result;
POKE(VKY_RND_CTRL, 1); // Enable.
result = PEEKW(VKY_RNDL);
return result;
}
void randomSeed(uint16_t seed) {
POKEW(VKY_SEEDL, seed);
POKE(VKY_RND_CTRL, 3); // Enable, load seed.
}
void waitVerticalBlank(void) {
//***TODO*** This assumes we're 60hz with graphics enabled.
while (PEEKW(RAST_ROW_L) == 482)
// Spin our wheels.
;
while (PEEKW(RAST_ROW_L) != 482)
// Spin our wheels.
;
}

View file

@ -51,6 +51,11 @@ extern "C"
typedef unsigned char byte;
// Our stuff.
#define SWAP_SLOT MMU_MEM_BANK_5 //***TODO*** Move this to 6 to save 8k.
#define SWAP_ADDR 0xa000
// Things not in the Merlin defs.
#define TEXT_MATRIX 0xc000 // I/O Page 2
#define RAST_ROW_L 0xd01a
@ -80,31 +85,18 @@ typedef struct colorS {
#define PEEKD(addy) ((uint32_t)*(volatile uint32_t *)(addy))
#define POKED(addy,value) (*(volatile uint32_t *)(addy) = (value))
#define VECTOR(member) (size_t)(&((struct call *)0xff00)->member)
#define EVENT(member) (size_t)(&((struct events *)0)->member)
#define CALL(fn) \
asm("jsr %[addy] \n" \
"stz %[err] \n" \
"ror %[err]" \
: [err] "+m"(error) \
: [addy] "i"(VECTOR(fn)) \
: "a", "x", "y", "c", "v");
extern struct event_t event; // The event struct is allocated in crt0.
extern char error;
void f256Init(void);
uint16_t randomRead(void);
void randomSeed(uint16_t seed);
void waitVerticalBlank(void);
#include "text.h"
#include "bitmap.h"
#include "kernel.h"
#include "dma.h"
#include "math.h"
#include "random.h"
#include "text.h"
#include "bitmap.h"
#include "tile.h"
#include "graphics.h"
#ifdef __cplusplus

82
f256lib/graphics.c Normal file
View file

@ -0,0 +1,82 @@
/*
* 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 "graphics.h"
void graphicsDefineColor(uint16_t clut, byte slot, byte r, byte g, byte b) {
byte mmu = PEEK(MMU_IO_CTRL); // Get current MMU state.
byte *write;
POKE(MMU_IO_CTRL, MMU_IO_PAGE_1); // Swap I/O page 1 into bank 6.
write = (byte *)mathUnsignedAddition(clut, mathUnsignedMultiply(slot, 4));
*write++ = b;
*write++ = g;
*write++ = r;
*write++ = 0xff;
POKE(MMU_IO_CTRL, mmu); // Restore MMU state.
}
void graphicsReset() {
int16_t x;
byte y;
uint16_t cluts[] = { GRAPHICS_CLUT_0, GRAPHICS_CLUT_1, GRAPHICS_CLUT_2, GRAPHICS_CLUT_3 };
// Set palettes to a gradient so there's at least *something*.
for (y=0; y<4; y++) {
for (x=0; x<256; x++) {
graphicsDefineColor(cluts[y], x, x, x, x);
}
}
graphicsSetLayerType(0, GRAPHICS_BITMAP_0);
graphicsSetLayerType(1, GRAPHICS_BITMAP_1);
graphicsSetLayerType(2, GRAPHICS_BITMAP_2);
}
void graphicsSetLayerType(byte layer, byte what) {
switch (layer) {
case 0:
POKE(VKY_LAYER_CTRL_0, (PEEK(VKY_LAYER_CTRL_0) & 0xf0) | what);
break;
case 1:
POKE(VKY_LAYER_CTRL_0, (PEEK(VKY_LAYER_CTRL_0) & 0x0f) | (what << 4));
break;
case 2:
POKE(VKY_LAYER_CTRL_1, what);
break;
}
}
void graphicsWaitVerticalBlank(void) {
//***TODO*** This assumes we're 60hz with graphics enabled.
while (PEEKW(RAST_ROW_L) != 482)
// Spin our wheels.
;
}

63
f256lib/graphics.h Normal file
View file

@ -0,0 +1,63 @@
/*
* 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 GRAPHICS_H
#define GRAPHICS_H
#ifdef __cplusplus
extern "C"
{
#endif
#include "f256.h"
// Names to match the library.
#define GRAPHICS_CLUT_0 VKY_GR_CLUT_0
#define GRAPHICS_CLUT_1 VKY_GR_CLUT_1
#define GRAPHICS_CLUT_2 VKY_GR_CLUT_2
#define GRAPHICS_CLUT_3 VKY_GR_CLUT_3
#define GRAPHICS_BITMAP_0 0
#define GRAPHICS_BITMAP_1 1
#define GRAPHICS_BITMAP_2 2
#define GRAPHICS_TILE_0 4
#define GRAPHICS_TILE_1 5
#define GRAPHICS_TILE_2 6
void graphicsDefineColor(uint16_t clut, byte slot, byte r, byte g, byte b);
void graphicsReset();
void graphicsSetLayerType(byte layer, byte what);
void graphicsWaitVerticalBlank(void);
#ifdef __cplusplus
}
#endif
#endif // GRAPHICS_H

42
f256lib/kernel.c Normal file
View file

@ -0,0 +1,42 @@
/*
* 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 "kernel.h"
char error;
/*
RTC_BUFFER .dstruct kernel.time_t
kGetTimeStamp
lda #<RTC_BUFFER
sta kernel.args.buf
lda #>RTC_BUFFER
sta kernel.args.buf+1
lda #size(kernel.time_t)
sta kernel.args.buflen
jsr kernel.Clock.GetTime
rts
*/

57
f256lib/kernel.h Normal file
View file

@ -0,0 +1,57 @@
/*
* 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 KERNEL_H
#define KERNEL_H
#ifdef __cplusplus
extern "C"
{
#endif
#include "f256.h"
#define VECTOR(member) (size_t)(&((struct call *)0xff00)->member)
#define EVENT(member) (size_t)(&((struct events *)0)->member)
#define CALL(fn) \
asm("jsr %[addy] \n" \
"stz %[err] \n" \
"ror %[err]" \
: [err] "+m"(error) \
: [addy] "i"(VECTOR(fn)) \
: "a", "x", "y", "c", "v");
extern struct event_t event; // The event struct is allocated in crt0. Or is it?
extern char error;
#ifdef __cplusplus
}
#endif
#endif // KERNEL_H

View file

@ -24,7 +24,31 @@
#include "math.h"
int16_t mathSignedDivision(int16_t a, int16_t b, int16_t *remainder) {
int16_t mathSignedDivision(int16_t a, int16_t b) {
byte signA = 0; // Zero indicates positive.
byte signB = 0;
int16_t r;
if (a < 0) {
signA = 1;
a = -a;
}
if (b < 0) {
signB = 1;
b = -b;
}
POKEW(DIVU_NUM_L, a);
POKEW(DIVU_DEN_L, b);
r = PEEKW(QUOU_LL);
if (signA + signB == 1) r = -r;
return r;
}
int16_t mathSignedDivisionRemainder(int16_t a, int16_t b, int16_t *remainder) {
byte signA = 0; // Zero indicates positive.
byte signB = 0;
int16_t r;
@ -73,7 +97,21 @@ int32_t mathSignedMultiply(int16_t a, int16_t b) {
}
uint16_t mathUnSignedDivision(uint16_t a, uint16_t b, uint16_t *remainder) {
uint32_t mathUnsignedAddition(uint32_t a, uint32_t b) {
POKED(ADD_A_LL, a);
POKED(ADD_B_LL, b);
return PEEKD(ADD_R_LL);
}
uint16_t mathUnsignedDivision(uint16_t a, uint16_t b) {
POKEW(DIVU_NUM_L, a);
POKEW(DIVU_DEN_L, b);
return PEEKW(QUOU_LL);
}
uint16_t mathUnsignedDivisionRemainder(uint16_t a, uint16_t b, uint16_t *remainder) {
POKEW(DIVU_NUM_L, a);
POKEW(DIVU_DEN_L, b);
*remainder = PEEKW(REMU_HL);

View file

@ -34,9 +34,12 @@ extern "C"
#include "f256.h"
int16_t mathSignedDivision(int16_t a, int16_t b, int16_t *remainder);
int16_t mathSignedDivision(int16_t a, int16_t b);
int16_t mathSignedDivisionRemainder(int16_t a, int16_t b, int16_t *remainder);
int32_t mathSignedMultiply(int16_t a, int16_t b);
uint16_t mathUnSignedDivision(uint16_t a, uint16_t b, uint16_t *remainder);
uint32_t mathUnsignedAddition(uint32_t a, uint32_t b);
uint16_t mathUnsignedDivision(uint16_t a, uint16_t b);
uint16_t mathUnsignedDivisionRemainder(uint16_t a, uint16_t b, uint16_t *remainder);
uint32_t mathUnsignedMultiply(uint16_t a, uint16_t b);

41
f256lib/random.c Normal file
View file

@ -0,0 +1,41 @@
/*
* 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 "random.h"
uint16_t randomRead(void) {
uint16_t result;
POKE(VKY_RND_CTRL, 1); // Enable.
result = PEEKW(VKY_RNDL);
return result;
}
void randomSeed(uint16_t seed) {
POKEW(VKY_SEEDL, seed);
POKE(VKY_RND_CTRL, 3); // Enable, load seed.
}

46
f256lib/random.h Normal file
View file

@ -0,0 +1,46 @@
/*
* 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 RANDOM_H
#define RANDOM_H
#ifdef __cplusplus
extern "C"
{
#endif
#include "f256.h"
uint16_t randomRead(void);
void randomSeed(uint16_t seed);
#ifdef __cplusplus
}
#endif
#endif // RANDOM_H

View file

@ -22,6 +22,7 @@
#include "text.h"
#include "math.h"
colorT textColors[16] = {
@ -55,16 +56,17 @@ static byte _ccolor = 240;
// Clear screen to current text attributes.
void textClear(void) {
byte mmu = PEEK(MMU_IO_CTRL); // Get current MMU state.
int i;
volatile byte *vram = (byte *)TEXT_MATRIX;
byte mmu = PEEK(MMU_IO_CTRL); // Get current MMU state.
int16_t i;
int16_t count = mathUnsignedMultiply(_MAX_COL, _MAX_ROW);
volatile byte *vram = (byte *)TEXT_MATRIX;
POKE(MMU_IO_CTRL, MMU_IO_TEXT); // Swap I/O page 2 into bank 6.
for (i = 0; i < _MAX_COL * _MAX_ROW; i++) *vram++ = 32;
for (i=0; i<count; i++) *vram++ = 32;
POKE(MMU_IO_CTRL, MMU_IO_COLOR); // Swap I/O page 3 into bank 6.
vram = (byte *)TEXT_MATRIX;
for (i = 0; i < _MAX_COL * _MAX_ROW; i++) *vram++ = _ccolor;
for (i=0; i<count; i++) *vram++ = _ccolor;
POKE(MMU_IO_CTRL, mmu); // Restore MMU state.
@ -76,13 +78,13 @@ void textClear(void) {
void textDefineColor(byte slot, byte fr, byte fg, byte fb, byte br, byte bg, byte bb) {
byte *write;
write = (byte *)VKY_TXT_FGLUT + slot * 4;
write = (byte *)VKY_TXT_FGLUT + mathUnsignedMultiply(slot, 4);
*write++ = fb;
*write++ = fg;
*write++ = fr;
*write++ = 0xff;
write = (byte *)VKY_TXT_BGLUT + slot * 4;
write = (byte *)VKY_TXT_BGLUT + mathUnsignedMultiply(slot, 4);
*write++ = bb;
*write++ = bg;
*write++ = br;
@ -108,7 +110,7 @@ void textPrint(char *message) {
int i = 0;
int j = 0;
byte mmu = PEEK(MMU_IO_CTRL); // Get current MMU state.
volatile byte *vram = (byte *)TEXT_MATRIX + (_MAX_COL * _row);
volatile byte *vram = (byte *)mathUnsignedAddition(TEXT_MATRIX, mathUnsignedMultiply(_MAX_COL, _row));
volatile byte *save = 0;
while (message[x] != 0) {
@ -130,7 +132,7 @@ void textPrint(char *message) {
vram = (byte *)TEXT_MATRIX;
POKE(MMU_IO_CTRL, MMU_IO_COLOR); // Swap I/O page 3 into bank 6.
for (j=0; j<2; j++) {
for (i = 0; i < _MAX_COL * (_MAX_ROW - 1); i++) vram[i] = vram[i+_MAX_COL];
for (i = 0; i < mathUnsignedMultiply(_MAX_COL, (_MAX_ROW - 1)); i++) vram[i] = vram[i+_MAX_COL];
POKE(MMU_IO_CTRL, MMU_IO_TEXT); // Swap I/O page 2 into bank 6.
}
// Clear bottom line.
@ -143,7 +145,7 @@ void textPrint(char *message) {
for (i = 0; i < _MAX_COL; i++) *vram++ = 32;
// Set up on bottom line.
_row--;
vram = (byte *)TEXT_MATRIX + (_MAX_ROW - 1);
vram = (byte *)mathUnsignedAddition(TEXT_MATRIX, (_MAX_ROW - 1));
} else {
vram += _MAX_COL;
}
@ -166,7 +168,7 @@ void textPrintInt(int32_t value){
value = -value;
}
if (value > 9) textPrintInt(value / 10);
if (value > 9) textPrintInt(mathUnsignedDivision(value, 10));
c[0] = '0' + (value % 10);
c[1] = 0;
@ -186,8 +188,7 @@ void textReset(void) {
_bcolor = 0;
_ccolor = 240;
POKE(VKY_CRSR_CTRL, 3); // Enable cursor, 1/2s flash.
POKE(VKY_CRSR_CHAR, '_'); // Set cursor shape. (199 = Checkerboard)
textSetCursor('_');
// Set up default text colors.
for (x=0; x<TEXTCOLORS_COUNT; x++)
@ -211,7 +212,10 @@ void textSetColor(byte f, byte b) {
void textSetCursor(byte c) {
// Hack
if (c == 0) c = 32;
POKE(VKY_CRSR_CHAR, c); // Set cursor shape. (199 = Checkerboard)
if (c == 0) {
POKE(VKY_CRSR_CTRL, 0); // No cursor.
} else {
POKE(VKY_CRSR_CTRL, 3); // Enable cursor, 1/2s flash.
POKE(VKY_CRSR_CHAR, c); // Set cursor shape. (199 = Checkerboard)
}
}

26
f256lib/tile.c Normal file
View file

@ -0,0 +1,26 @@
/*
* 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 "tile.h"

43
f256lib/tile.h Normal file
View file

@ -0,0 +1,43 @@
/*
* 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 TILE_H
#define TILE_H
#ifdef __cplusplus
extern "C"
{
#endif
#include "f256.h"
#ifdef __cplusplus
}
#endif
#endif // TILE_H