From 9415a2ebc20f65f574c5b8983601ca5615829ff0 Mon Sep 17 00:00:00 2001 From: Scott Duensing Date: Thu, 1 Feb 2024 19:45:09 -0600 Subject: [PATCH] Various fixes. --- examples/pgztest/pgztest.c | 6 +++++- f256lib/api.h | 1 - f256lib/f256.c | 36 ++++++++++++++++++++++++++++++++---- f256lib/f256.h | 3 +++ f256lib/file.c | 8 ++++---- f256lib/file.h | 7 ++----- f256lib/kernel.h | 1 - f256lib/platform.h | 2 +- f256lib/random.c | 11 +++++++++++ f256lib/random.h | 1 + 10 files changed, 59 insertions(+), 17 deletions(-) diff --git a/examples/pgztest/pgztest.c b/examples/pgztest/pgztest.c index ee349a5..793aaee 100644 --- a/examples/pgztest/pgztest.c +++ b/examples/pgztest/pgztest.c @@ -68,15 +68,19 @@ void dirtest(void) { int main(void) { + struct time_t clock; + f256Init(); //dirtest(); while(true) { kernelNextEvent(); + kernelArgs->common.buf = &clock; + kernelArgs->common.buflen = sizeof(struct time_t); kernelCall(Clock.GetTime); textGotoXY(0, 0); - printf("%d.%d ", kernelArgs->time.seconds, kernelArgs->time.centis); + printf("%d.%d ", clock.seconds, clock.centis); } return 0; diff --git a/f256lib/api.h b/f256lib/api.h index 1896ffb..4d9bf3d 100644 --- a/f256lib/api.h +++ b/f256lib/api.h @@ -287,7 +287,6 @@ struct call_args { struct display_t display; struct net_t net; struct timer_t timer; - struct time_t time; }; }; diff --git a/f256lib/f256.c b/f256lib/f256.c index dd2d2c6..798b73f 100644 --- a/f256lib/f256.c +++ b/f256lib/f256.c @@ -61,10 +61,7 @@ void f256Init(void) { tileReset(); spriteReset(); fileReset(); - - //***TODO*** The clock stuff doesn't actually work. - kernelCall(Clock.GetTime); - randomSeed(kernelArgs->time.centis); + randomReset(); } @@ -85,6 +82,22 @@ byte FAR_PEEK(uint32_t address) { } +uint16_t FAR_PEEKW(uint32_t address) { + byte block; + uint16_t result; + + SWAP_IO_SETUP(); + + block = address / EIGHTK; + address &= 0x1FFF; // Find offset into this block. + POKE(SWAP_SLOT, block); + result = PEEKW(SWAP_ADDR + address); + + SWAP_IO_SHUTDOWN(); + + return result; +} + void FAR_POKE(uint32_t address, byte value) { byte block; @@ -98,3 +111,18 @@ void FAR_POKE(uint32_t address, byte value) { SWAP_IO_SHUTDOWN(); } + + + +void FAR_POKEW(uint32_t address, uint16_t value) { + byte block; + + SWAP_IO_SETUP(); + + block = address / EIGHTK; + address &= 0x1FFF; // Find offset into this block. + POKE(SWAP_SLOT, block); + POKEW(SWAP_ADDR + address, value); + + SWAP_IO_SHUTDOWN(); +} diff --git a/f256lib/f256.h b/f256lib/f256.h index d66eaab..ceb4ecf 100644 --- a/f256lib/f256.h +++ b/f256lib/f256.h @@ -147,6 +147,7 @@ typedef struct colorS { #define LOW_BYTE(v) ((byte)(x)) #define HIGH_BYTE(v) ((byte)(((uint16_t)(x)) >> 8)) #define SWAP_NIBBLES(x) ((x & 0x0F) << 4 | (x & 0xF0) >> 4) +#define SWAP_UINT16(x) (((x) >> 8) | ((x) << 8)) #define CHECK_BIT(x, pos) (x & (1UL << pos)) #define TOGGLE_BIT(x, pos) (x ^= (1U << pos)) #define CLEAR_BIT(x, pos) (x &= (~(1U << pos))) @@ -158,7 +159,9 @@ extern byte SOF; void f256Init(void); byte FAR_PEEK(uint32_t address); +uint16_t FAR_PEEKW(uint32_t address); void FAR_POKE(uint32_t address, byte value); +void FAR_POKEW(uint32_t address, uint16_t value); #include "kernel.h" diff --git a/f256lib/file.c b/f256lib/file.c index 9feb542..b3bcd1e 100644 --- a/f256lib/file.c +++ b/f256lib/file.c @@ -105,10 +105,10 @@ int8_t fileMakeDir(char *dir) { } -int16_t *fileOpen(char *fname, char *mode) { - int16_t ret = 0; +uint8_t *fileOpen(char *fname, char *mode) { + uint8_t ret = 0; uint8_t m = 0; // Default to READ. - int16_t *fd; + uint8_t *fd; byte drive; char *c; @@ -132,7 +132,7 @@ int16_t *fileOpen(char *fname, char *mode) { kernelNextEvent(); switch (kernelEventData.type) { case kernelEvent(file.OPENED): - fd = (int16_t *)malloc(sizeof(int16_t)); + fd = (uint8_t *)malloc(sizeof(uint8_t)); *fd = ret; return fd; case kernelEvent(file.NOT_FOUND): diff --git a/f256lib/file.h b/f256lib/file.h index 4bf183b..6abf0db 100644 --- a/f256lib/file.h +++ b/f256lib/file.h @@ -44,7 +44,7 @@ typedef struct fileDirEntS { void fileClose(uint8_t *fd); int8_t fileCloseDir(char *dir); int8_t fileMakeDir(char *dir); -int16_t *fileOpen(char *fname, char *mode); +uint8_t *fileOpen(char *fname, char *mode); char *fileOpenDir(char *name); int16_t fileRead(void *buf, uint16_t nbytes, uint16_t nmemb, uint8_t *fd); fileDirEntT *fileReadDir(char *dir); @@ -56,10 +56,6 @@ int8_t fileUnlink(char *name); int16_t fileWrite(void *buf, uint16_t nbytes, uint16_t nmemb, uint8_t *fd); -// mkdir -// rmdir - - #define _DE_ISREG(t) (t == 0) #define _DE_ISDIR(t) (t == 1) #define _DE_ISLBL(t) (t == 2) @@ -73,6 +69,7 @@ int16_t fileWrite(void *buf, uint16_t nbytes, uint16_t nmemb, uint8_t *fd); #define DIR char #define dirent fileDirEntS #define FILE uint8_t +#define fclose fileClose #define fopen fileOpen #define fread fileRead #define fseek fileSeek diff --git a/f256lib/kernel.h b/f256lib/kernel.h index a741820..206dff1 100644 --- a/f256lib/kernel.h +++ b/f256lib/kernel.h @@ -58,7 +58,6 @@ typedef struct call_args kernelArgsT; extern kernelEventT kernelEventData; extern kernelArgsT *kernelArgs; -extern char kernelError; byte kernelGetPending(void); diff --git a/f256lib/platform.h b/f256lib/platform.h index 3310496..525f70f 100644 --- a/f256lib/platform.h +++ b/f256lib/platform.h @@ -35,7 +35,7 @@ extern "C" void __putchar(char c); -int getchar(void); +int getchar(void); // This is an int so llvm-mos finds it. #ifdef __cplusplus diff --git a/f256lib/random.c b/f256lib/random.c index 3425f4a..cc2cebe 100644 --- a/f256lib/random.c +++ b/f256lib/random.c @@ -34,6 +34,17 @@ uint16_t randomRead(void) { } +void randomReset(void) { + struct time_t clock; + + // Seed the random number generator from the clock. + kernelArgs->common.buf = &clock; + kernelArgs->common.buflen = sizeof(struct time_t); + kernelCall(Clock.GetTime); + randomSeed(mathUnsignedMultiply(clock.seconds, clock.centis)); +} + + void randomSeed(uint16_t seed) { POKEW(VKY_SEEDL, seed); POKE(VKY_RND_CTRL, 3); // Enable, load seed. diff --git a/f256lib/random.h b/f256lib/random.h index 76cd555..9469802 100644 --- a/f256lib/random.h +++ b/f256lib/random.h @@ -35,6 +35,7 @@ extern "C" uint16_t randomRead(void); +void randomReset(void); void randomSeed(uint16_t seed);