From f642221028fcab5387bc232a73e9ad6078b950fe Mon Sep 17 00:00:00 2001 From: Scott Duensing Date: Sun, 7 Jan 2024 19:13:36 -0600 Subject: [PATCH] Library now assumes I/O page 0 is active at all times. Cube using math coprocessor for multiplication. --- examples/cube/cube.c | 7 +++-- f256lib/bitmap.c | 5 ---- f256lib/dma.c | 12 --------- f256lib/f256.c | 30 +++------------------ f256lib/f256.h | 2 +- f256lib/math.c | 64 ++++++++++++++++++++++++++++++++++++++++++++ f256lib/math.h | 6 +++++ f256lib/text.c | 19 +------------ 8 files changed, 81 insertions(+), 64 deletions(-) diff --git a/examples/cube/cube.c b/examples/cube/cube.c index e3584d2..d6b0170 100644 --- a/examples/cube/cube.c +++ b/examples/cube/cube.c @@ -32,8 +32,11 @@ #define TO_DBL(x) (((double)x)/(double)(1<> FIX_PREC) -#define fix_sqr(a) ((a * a) >> FIX_PREC) +int16_t _remainder; + +#define fix_mul(a,b) (mathSignedMultiply(a, b) >> FIX_PREC) +#define fix_sqr(a) (mathSignedMultiply(a, a) >> FIX_PREC) +//#define fix_div(a,b) mathSignedDivision((a << FIX_PREC), b, &_remainder) #define fix_div(a,b) ((a << FIX_PREC) / b) #define SIN_SIZE 512 diff --git a/f256lib/bitmap.c b/f256lib/bitmap.c index f22fa27..c37250d 100644 --- a/f256lib/bitmap.c +++ b/f256lib/bitmap.c @@ -156,7 +156,6 @@ void bitmapPutPixel(uint16_t x, uint16_t y) { void bitmapReset(void) { - byte mmu = PEEK(MMU_IO_CTRL); // Get current MMU state. int x; uint32_t realSize; uint32_t pageBlocks; @@ -186,8 +185,6 @@ void bitmapReset(void) { // Set palette to a gradient so there's at least *something*. for (x=0; x<256; x++) bitmapDefineColor(x, x, x, x); - POKE(MMU_IO_CTRL, MMU_IO_PAGE_0); // Swap I/O page 0 into bank 6. - POKE(VKY_LAYER_CTRL_0, 16); // Bitmaps on all layers. POKE(VKY_LAYER_CTRL_1, 2); // Bitmaps on all layers. @@ -204,8 +201,6 @@ void bitmapReset(void) { // Enable the first bitmap. bitmapShowPage(0); - POKE(MMU_IO_CTRL, mmu); // Restore MMU state. - // Clear all pages. _color = 0; for (_page=0; _page<3; _page++) bitmapClear(); diff --git a/f256lib/dma.c b/f256lib/dma.c index 34c98dd..6690355 100644 --- a/f256lib/dma.c +++ b/f256lib/dma.c @@ -28,10 +28,6 @@ static void dmaWait(void); void dmaFill(uint32_t start, uint32_t length, byte value) { - byte mmu = PEEK(MMU_IO_CTRL); // Get current MMU state. - - POKE(MMU_IO_CTRL, MMU_IO_PAGE_0); // Swap I/O page 0 into bank 6. - POKE(DMA_CTRL, DMA_CTRL_FILL | DMA_CTRL_ENABLE); POKE(DMA_FILL_VAL, value); POKEA(DMA_DST_ADDR, start); @@ -39,17 +35,11 @@ void dmaFill(uint32_t start, uint32_t length, byte value) { POKE(DMA_CTRL, PEEK(DMA_CTRL) | DMA_CTRL_START); dmaWait(); - - POKE(MMU_IO_CTRL, mmu); // Restore MMU state. } void dma2dFill(uint32_t start, uint16_t width, uint16_t height, uint16_t stride, byte value) { - byte mmu = PEEK(MMU_IO_CTRL); // Get current MMU state. - - POKE(MMU_IO_CTRL, MMU_IO_PAGE_0); // Swap I/O page 0 into bank 6. - POKE(DMA_CTRL, DMA_CTRL_2D | DMA_CTRL_ENABLE); POKE(DMA_FILL_VAL, value); POKEA(DMA_DST_ADDR, start); @@ -59,8 +49,6 @@ void dma2dFill(uint32_t start, uint16_t width, uint16_t height, uint16_t stride, POKE(DMA_CTRL, PEEK(DMA_CTRL) | DMA_CTRL_START); dmaWait(); - - POKE(MMU_IO_CTRL, mmu); // Restore MMU state. } diff --git a/f256lib/f256.c b/f256lib/f256.c index 19263b4..7dedf79 100644 --- a/f256lib/f256.c +++ b/f256lib/f256.c @@ -26,18 +26,15 @@ #include "text.c" #include "bitmap.c" #include "dma.c" -#include "math.h" +#include "math.c" char error; void f256Init(void) { - byte mmu = PEEK(MMU_IO_CTRL); // Get current MMU state. - - asm("sei"); - - POKE(MMU_IO_CTRL, MMU_IO_PAGE_0); // Swap I/O page 0 into bank 6. + // 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. @@ -46,8 +43,6 @@ void f256Init(void) { //POKE(VKY_MSTR_CTRL_1, 20); // Enable FON_OVLY and DBL_Y. POKE(VKY_MSTR_CTRL_1, 4); // Enable DBL_Y. - POKE(MMU_IO_CTRL, mmu); // Restore MMU state. - POKE(MMU_MEM_CTRL, 0xb3); // MLUT editing enabled, editing 3, 3 is active. // Set all memory slots to be CPU memory. POKE(MMU_MEM_BANK_0, 0); @@ -55,7 +50,7 @@ void f256Init(void) { POKE(MMU_MEM_BANK_2, 2); POKE(MMU_MEM_BANK_3, 3); POKE(MMU_MEM_BANK_4, 4); - POKE(MMU_MEM_BANK_5, 5); + POKE(MMU_MEM_BANK_5, 5); // Don't use this - it's for the library. // MMU_MEM_BANK_6 is always mapped to I/O. // MMU_MEM_BANK_7 belongs to the MicroKernel. @@ -67,37 +62,22 @@ void f256Init(void) { uint16_t randomRead(void) { - byte mmu = PEEK(MMU_IO_CTRL); // Get current MMU state. uint16_t result; - POKE(MMU_IO_CTRL, MMU_IO_PAGE_0); // Swap I/O page 0 into bank 6. - POKE(VKY_RND_CTRL, 1); // Enable. result = PEEKW(VKY_RNDL); - POKE(MMU_IO_CTRL, mmu); // Restore MMU state. - return result; } void randomSeed(uint16_t seed) { - byte mmu = PEEK(MMU_IO_CTRL); // Get current MMU state. - - POKE(MMU_IO_CTRL, MMU_IO_PAGE_0); // Swap I/O page 0 into bank 6. - POKEW(VKY_SEEDL, seed); POKE(VKY_RND_CTRL, 3); // Enable, load seed. - - POKE(MMU_IO_CTRL, mmu); // Restore MMU state. } void waitVerticalBlank(void) { - byte mmu = PEEK(MMU_IO_CTRL); // Get current MMU state. - - POKE(MMU_IO_CTRL, MMU_IO_PAGE_0); // Swap I/O page 0 into bank 6. - //***TODO*** This assumes we're 60hz with graphics enabled. while (PEEKW(RAST_ROW_L) == 482) // Spin our wheels. @@ -105,6 +85,4 @@ void waitVerticalBlank(void) { while (PEEKW(RAST_ROW_L) != 482) // Spin our wheels. ; - - POKE(MMU_IO_CTRL, mmu); // Restore MMU state. } diff --git a/f256lib/f256.h b/f256lib/f256.h index 83d58d4..10bf959 100644 --- a/f256lib/f256.h +++ b/f256lib/f256.h @@ -77,7 +77,7 @@ typedef struct colorS { #define POKEA(addy, value) POKE(addy, value & 0xFF); POKE(addy + 1, (value >> 8) & 0xFF); POKE(addy + 2, (value >> 16) & 0xFF) // Double-word (four bytes) -#define PEEKD(addy) ((uint_32)*(volatile uint32_t *)(addy)) +#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) diff --git a/f256lib/math.c b/f256lib/math.c index 28f7784..3643a18 100644 --- a/f256lib/math.c +++ b/f256lib/math.c @@ -22,3 +22,67 @@ #include "math.h" + + +int16_t mathSignedDivision(int16_t a, int16_t b, int16_t *remainder) { + 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_DEN_L, b); + POKEW(DIVU_NUM_L, a); + r = PEEKW(QUOU_LL); + *remainder = PEEKW(REMU_HL); + + if (signA + signB == 1) r = -r; + + return r; +} + + +int32_t mathSignedMultiply(int16_t a, int16_t b) { + byte signA = 0; // Zero indicates positive. + byte signB = 0; + int32_t r; + + if (a < 0) { + signA = 1; + a = -a; + } + if (b < 0) { + signB = 1; + b = -b; + } + + POKEW(MULU_A_L, a); + POKEW(MULU_B_L, b); + r = PEEKD(MULU_LL); + + if (signA + signB == 1) r = -r; + + return r; +} + + +uint16_t mathUnSignedDivision(uint16_t a, uint16_t b, uint16_t *remainder) { + POKEW(DIVU_DEN_L, b); + POKEW(DIVU_NUM_L, a); + *remainder = PEEKW(REMU_HL); + return PEEKW(QUOU_LL); +} + + +uint32_t mathUnsignedMultiply(uint16_t a, uint16_t b) { + POKEW(MULU_A_L, a); + POKEW(MULU_B_L, b); + return PEEKD(MULU_LL); +} diff --git a/f256lib/math.h b/f256lib/math.h index 5df8d51..c3e9a66 100644 --- a/f256lib/math.h +++ b/f256lib/math.h @@ -34,6 +34,12 @@ extern "C" #include "f256.h" +int16_t mathSignedDivision(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 mathUnsignedMultiply(uint16_t a, uint16_t b); + + #ifdef __cplusplus } #endif diff --git a/f256lib/text.c b/f256lib/text.c index ab93c28..6a90aea 100644 --- a/f256lib/text.c +++ b/f256lib/text.c @@ -55,8 +55,8 @@ static byte _ccolor = 240; // Clear screen to current text attributes. void textClear(void) { - int i; byte mmu = PEEK(MMU_IO_CTRL); // Get current MMU state. + int i; volatile byte *vram = (byte *)TEXT_MATRIX; POKE(MMU_IO_CTRL, MMU_IO_TEXT); // Swap I/O page 2 into bank 6. @@ -74,11 +74,8 @@ void textClear(void) { // Define text color. void textDefineColor(byte slot, byte fr, byte fg, byte fb, byte br, byte bg, byte bb) { - byte mmu = PEEK(MMU_IO_CTRL); // Get current MMU state. byte *write; - POKE(MMU_IO_CTRL, MMU_IO_PAGE_0); // Swap I/O page 0 into bank 6. - write = (byte *)VKY_TXT_FGLUT + slot * 4; *write++ = fb; *write++ = fg; @@ -90,18 +87,11 @@ void textDefineColor(byte slot, byte fr, byte fg, byte fb, byte br, byte bg, byt *write++ = bg; *write++ = br; *write++ = 0xff; - - POKE(MMU_IO_CTRL, mmu); // Restore MMU state. } // Move cursor. void textGotoXY(byte x, byte y) { - byte mmu; - - mmu = PEEK(MMU_IO_CTRL); // Get current MMU state. - POKE(MMU_IO_CTRL, MMU_IO_PAGE_0); // Swap I/O page 0 into bank 6. - _col = x; POKE(VKY_CRSR_X_L, _col); // Set cursor X position. POKE(VKY_CRSR_X_H, 0); @@ -109,8 +99,6 @@ void textGotoXY(byte x, byte y) { _row = y; POKE(VKY_CRSR_Y_L, _row); // Set cursor Y position. POKE(VKY_CRSR_Y_H, 0); - - POKE(MMU_IO_CTRL, mmu); // Restore MMU state. } @@ -188,7 +176,6 @@ void textPrintInt(int32_t value){ // Reset display to text, "standard" colors. void textReset(void) { - byte mmu = PEEK(MMU_IO_CTRL); // Get current MMU state. byte x; byte y; @@ -199,8 +186,6 @@ void textReset(void) { _bcolor = 0; _ccolor = 240; - POKE(MMU_IO_CTRL, MMU_IO_PAGE_0); // Swap I/O page 0 into bank 6. - POKE(VKY_CRSR_CTRL, 3); // Enable cursor, 1/2s flash. POKE(VKY_CRSR_CHAR, '_'); // Set cursor shape. (199 = Checkerboard) @@ -214,8 +199,6 @@ void textReset(void) { textColors[x].g, textColors[x].b); - POKE(MMU_IO_CTRL, mmu); // Restore MMU state. - textClear(); }