From aef920567b4515cef0673992a4eaa2546b08c43c Mon Sep 17 00:00:00 2001 From: ZERICO2005 <71151164+ZERICO2005@users.noreply.github.com> Date: Sat, 2 Dec 2023 23:25:37 -0700 Subject: [PATCH] v0.77.2 | 2023/12/02 23:21 | Fixed minor bugs and things, also found out the timing funcction I used had milisecond precision instead of nanosecond precision, but now it's fixed. Started to write new and improved code, as I have learnt a lot more about C/C++ from coding ABS-Fractal-Explorer --- .gitignore | 1 - lib/.gitignore | 2 - makefile | 38 --- {bin => src/Super CSV/obj}/.gitignore | 0 src/global.h | 4 +- src/mineSweeper.h | 14 +- src/optimized.asm | 343 ++++++++++++++++++++++++++ src/optimized.h | 20 ++ src/prime2D.h | 14 +- src/subMenu.h | 12 +- src/x86render.h | 58 +++-- 11 files changed, 429 insertions(+), 77 deletions(-) delete mode 100644 lib/.gitignore delete mode 100644 makefile rename {bin => src/Super CSV/obj}/.gitignore (100%) create mode 100644 src/optimized.asm create mode 100644 src/optimized.h diff --git a/.gitignore b/.gitignore index 1189f77..587ada4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -obj/ src/gfx/*.c src/gfx/*.h .DS_Store diff --git a/lib/.gitignore b/lib/.gitignore deleted file mode 100644 index c96a04f..0000000 --- a/lib/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!.gitignore \ No newline at end of file diff --git a/makefile b/makefile deleted file mode 100644 index dfe2cf5..0000000 --- a/makefile +++ /dev/null @@ -1,38 +0,0 @@ -# DEPRECATED -# USE CMAKELISTS.TXT INSTEAD - -#g++ main.cpp ./glad/src/glad.c -I./glad/include -o prog.exe -lmingw32 -lSDL2main -lSDL2 - -#SRC_FILES = $(wildcard $(SRC_DIR)/*.cpp) -#OBJ_FILES = $(patsubst $(SRC_DIR)/%.cpp,$(BIN_DIR)/%.o,$(SRC_FILES)) $(BIN_DIR)/glad.o - -#Compilier -CC = g++ -# Directories -SRC_DIR = ./src ./glad/src -OBJ_DIR = ./obj -BIN_DIR = ./bin - -# Output -BIN_OUTPUT = ./bin/Super-Sweeper-v77.exe -# Sources -SRC_FILES = ./src/main.c ./glad/src/glad.c - -# Flags -CFLAGS = -I./glad/include -LDFLAGS = -lmingw32 -lSDL2main -lSDL2 -mwindows -static-libgcc -static-libstdc++ -OBJ = $(patsubst $(SRC_DIR)/%.cpp,$(OBJ_DIR)/%.o,$(SRC_FILES)) - -$(BIN_OUTPUT): $(OBJ) - $(CC) $(OBJ) $(CFLAGS) -o $@ $(LDFLAGS) -O3 - -($OBJ_DIR)/%.o: %.cpp - $(CC) $(CFLAGS) -c $< -o $@ - -($OBJ_DIR)/%.o: %.c - $(CC) $(CFLAGS) -c $< -o $@ - -clean: - rm -f $(BIN_DIR)/* - -.PHONY: $(BIN_OUTPUT) clean \ No newline at end of file diff --git a/bin/.gitignore b/src/Super CSV/obj/.gitignore similarity index 100% rename from bin/.gitignore rename to src/Super CSV/obj/.gitignore diff --git a/src/global.h b/src/global.h index a9bc56e..feeeef4 100644 --- a/src/global.h +++ b/src/global.h @@ -168,10 +168,10 @@ uint24_t cursorAllow = 0; /* Legacy */ const uint8_t hiddenText[43] = { //SuperSweeper V:X.XX.X ZERICO2005 YYYY/MM/DD 82,110,105,94,107,82,112,94,94,105,94,107,32,85,39, /* Text */ - 0,34,7,7,34,1, /* Version */ + 0,34,7,7,34,2, /* Version */ 32,89,68,81,72,66,78,2,0,0,5,32, /* Text */ 2,0,2,3,36, /* Year */ - 0,6,36,0,3, /* Month Day */ + 1,2,36,0,2, /* Month Day */ }; const uint8_t hiddenTextLength = 43; diff --git a/src/mineSweeper.h b/src/mineSweeper.h index 61a55cd..9047613 100644 --- a/src/mineSweeper.h +++ b/src/mineSweeper.h @@ -298,14 +298,20 @@ void expression(unsigned char e) { //Updates the smiley face } void flash() { //uint8_t x - for (uint8_t j = 0; j < 32; j++) { + for (uint8_t j = 0; j <= 32; j++) { uint32_t flashTime = timer_Get(1); uint8_t i = 0; for (uint16_t *f = paletteRAM; f < paletteRAM + 0x100; f++) { - *f = (linear0((initialPalette[i] >> 10) & 31, 31, 31, j) << 10) + (linear0((initialPalette[i] >> 5) & 31, 31, 31, j) << 5) + (linear0(initialPalette[i] & 31, 31, 31, j)); + #define flashLinear(y0,x) ((((y0) * (32 - (x))) + (31 * (x))) >> 5) + *f = ( + (flashLinear((initialPalette[i] >> 10) & 31,j) << 10) + + (flashLinear((initialPalette[i] >> 5) & 31,j) << 5) + + flashLinear(initialPalette[i] & 31,j) + ); i++; + #undef flashLinear } - while (deltaTime(timer_Get(1),flashTime) < 1536); + while (deltaTime(timer_Get(1),flashTime) < 1472); } } @@ -796,7 +802,7 @@ void drawGame() { horiz((disX * 2) + posX, i + disY - 2, disX * (sizeX) - 1); } if (!(disX & 1)) { //% 2 - gColor = *videoMode & 2 ? 0x07 : 0x00; //Inverted Colors + gColor = (darkMode != 0) ? 0x07 : 0x00; //Inverted Colors vert(319, 0, 240); } //Border Lines diff --git a/src/optimized.asm b/src/optimized.asm new file mode 100644 index 0000000..27d24de --- /dev/null +++ b/src/optimized.asm @@ -0,0 +1,343 @@ + + assume adl=1 + + section .text + public _fillRect + public _fillText + public _horiz + public _vert + public _plot + public _plotFast + public _addPointer + public _fillMemory + ; +9 high, +10 mid, +11, low + + _fillMemory: + ; +3 value, +4 base, +7 len + ; -1 color + ld hl,-7 ; 2*3 + 1*1 byte + push ix ; adds 2*3 bytes + ld ix, 0 ; put 0 in ix + add ix, sp ; add sp to ix + add hl, sp ; add sp to hl + ld sp, hl ; put hl in sp + + + ld a,(ix+3) ;Load Value + ld de,(ix+4) ;Load Base + inc de + ld bc,(ix+7) ;Load Length + dec bc + ld (hl),a ;Writes Value to Address + ldir ; Where the magic happens + + ld sp, ix + pop ix + ret + + + _fillRect: + ; +6 x1, +9 y1, +12 x2, +15 y2 + ; -3 jump, -6 base, -7 color, -8 address + ld hl,-8 ; (4 * 3) + (2 * 3) byte + push ix ; adds (2 * 3) bytes + ld ix, 0 ; put 0 in ix + add ix, sp ; add sp to ix + add hl, sp ; add sp to hl + ld sp, hl ; put hl in sp + + ld hl,($D657F0) + ld a,(hl) ;D052F4 + ld (ix-7),a + + ld hl,320 ;320 - x2 + dec (ix+12) + ld de,(ix+12) ;loads x2 into de + sbc hl,de ;subtracts + ld (ix-3),hl ;store jump + +; ld hl,0 ;23 vs 20 clock cycles, so this is slower +; ld de,(ix+10) ; multiply by 256 method A +; ld e,0 ; +; ld deu,(ix+10) ; multiply by 256 method B +; ld d,(ix+11) ; +; add hl,de ; copys de to hl +; ld f,0 ; divide hl by 4 +; rr h +; rr l +; ld f,0 +; rr h +; rr l +; add hl,de ; * 320 + + ;0xD40000 + x1 + (y1 * 320) + ld hl,(ix+9) ; loads y1 + add hl,hl ; 2 + add hl,hl ; 4 + add hl,hl ; 8 + add hl,hl ; 16 + add hl,hl ; 32 + add hl,hl ; 64 + push hl ; store 64 in base temporarily + add hl,hl ; 128 + add hl,hl ; 256 + pop de ; loads 64 inside of base + add hl,de ; add de to hl (256 + 64) + + + + ld de,$D40000 + add hl,de ; add VRAM + ld de,(ix+6) ; load x1 + add hl,de ; add x1 +; ld (ix-18),hl ; store 320 in base + + inc (ix-3) ;Jump related + + ld a,(ix+15) ; loads y2 + loopY0: + ld (ix-8),a + ld a,(ix-7) +; ld hl + ld (ix-6),hl ;load hl into base + ld de,(ix-6) ;set de to base + inc de + ld bc,(ix+12) + ld (hl),a;Writes color to screen + ldir + ld de,(ix-3) + add hl,de + ld a,(ix-8) + dec a + jr nz,loopY0 + + ld sp, ix + pop ix + ret + + _horiz: + ; +6 x1, +9 y, +12 x2 + ; -1 color + ld hl,-1 ; 0*3 + 1*1 byte + push ix ; adds 2*3 bytes + ld ix, 0 ; put 0 in ix + add ix, sp ; add sp to ix + add hl, sp ; add sp to hl + ld sp, hl ; put hl in sp + + ld hl,($D657F0) ; Loads color in a two step process + ld a,(hl) ;D052F4 +; ld (ix-3),a ; No need to store it this time + + ;0xD40000 + x1 + x2 + (y * 320) + ld hl,(ix+9) ; loads y + add hl,hl ; 2 + add hl,hl ; 4 + add hl,hl ; 8 + add hl,hl ; 16 + add hl,hl ; 32 + add hl,hl ; 64 + push hl ; store 64 in base temporarily + add hl,hl ; 128 + add hl,hl ; 256 + pop de ; loads 64 inside of base + add hl,de ; add de to hl (256 + 64) + ld de,$D40000 + add hl,de ; add VRAM + ld de,(ix+6) ; load x1 + add hl,de ; add x1 +; ld (ix-6),hl ; store 320 in base + + push hl ;load hl into base + pop de ;set de to base + inc de + ld bc,(ix+12) + dec bc + ld (hl),a;Writes color to screen + ldir ; Where the magic happens + + ld sp, ix + pop ix + ret + + + + _vert: + ; +6 x, +9 y1, +12 y2 + push ix + ld ix, 0 + add ix, sp + + ld hl,($D657F0) + ld c,(hl) ;D052F4 + + ;0xD40000 + x1 + (y1 * 320) + ld hl,(ix+9) ; loads y1 + add hl,hl ; 2 + add hl,hl ; 4 + add hl,hl ; 8 + add hl,hl ; 16 + add hl,hl ; 32 + add hl,hl ; 64 + push hl ; store 64 in base temporarily + add hl,hl ; 128 + add hl,hl ; 256 + pop de ; loads 64 inside of base + add hl,de ; add de to hl (256 + 64) + ld de,$D40000 + add hl,de ; add VRAM + ld de,(ix+6) ; load x1 + add hl,de ; add x1 + + ld de,320 + + ld a,(ix+12) ; loads y2 + + loopY1: + + ld (hl),c ;Writes color to screen + add hl,de + dec a + + jr nz,loopY1 + + pop ix + ret + + + _plot: + ; +6 x, +9 y + push ix + ld ix, 0 + add ix, sp + + ld hl,($D657F0) + ld a,(hl) + + ;0xD40000 + x1 + x2 + (y * 320) + ld hl,(ix+9) ; loads y + add hl,hl ; 2 + add hl,hl ; 4 + add hl,hl ; 8 + add hl,hl ; 16 + add hl,hl ; 32 + add hl,hl ; 64 + push hl ; store 64 in base temporarily + add hl,hl ; 128 + add hl,hl ; 256 + pop de ; loads 64 inside of base + add hl,de ; add de to hl (256 + 64) + ld de,$D40000 + add hl,de ; add VRAM + ld de,(ix+6) ; load x + add hl,de ; add x + + ld (hl),a + + pop ix + ret + + _plotFast: + ; +6 z + push ix + ld ix, 0 + add ix, sp + + ld hl,($D657F0) ; Loads color in a two step process + ld a,(hl) ;D052F4 + ld de,(ix+6) + ld (de),a + + pop ix + ret + + _addPointer: + ; +6 x, +9 y + push ix + ld ix, 0 + add ix, sp + + ld hl,(ix+6) ; Loads the Address of X + ld hl,(hl) ; Loads the value at the Address of X + ld de,(ix+9) ; Y to add + add hl,de + ex de,hl ; Swaps HL and DE + ld hl,(ix+6) ; Loads the Address of X + ld (hl),de + + pop ix + ret + + + + + _fillText: + ; +6 x1, +9 y1, +12 x2, +15 y2 + ; -1 color, -4 jump, -7 base + ld hl,-3 ; 1*3 + 0*1 byte + push ix ; adds 2*3 bytes + ld ix, 0 ; put 0 in ix + add ix, sp ; add sp to ix + add hl, sp ; add sp to hl + ld sp, hl ; put hl in sp + + ld hl,($D657F0) + ld d,(hl) ;Close to $D052F4 + + ld hl,320 ;320 - x2 + dec (ix+12) + ld de,(ix+12) ;loads x2 into de + sbc hl,de ;subtracts + ld (ix-4),hl ;store jump + + ;0xD40000 + x1 + (y1 * 320) + ld hl,(ix+9) ; loads y1 + add hl,hl ; 2 + add hl,hl ; 4 + add hl,hl ; 8 + add hl,hl ; 16 + add hl,hl ; 32 + add hl,hl ; 64 + push hl ; store 64 in base temporarily + add hl,hl ; 128 + add hl,hl ; 256 + pop de ; loads 64 inside of base + add hl,de ; add de to hl (256 + 64) + ld de,$D40000 + add hl,de ; add VRAM + ld de,(ix+6) ; load x1 + add hl,de ; add x1 + ld (ix-7),hl + + ld de,320 + + ld b,(ix+15) ; loads y2 + ld c,(ix+12) ; loads x2 + loopYF: + loopXF: + + ld hl,(ix-7) + ld a,(hl) + dec a ;tests if a is 0 + jr nz,loopZF + ld (ix-7),d ;Writes color to screen + loopZF: + + inc (ix-7) + dec b + jr nz,loopXF + + + push de + ld de,(ix-4) + add hl,de ; Adds jump for the next line + pop de + dec c + jr nz,loopYF + + ld sp, ix + pop ix + ret + + + \ No newline at end of file diff --git a/src/optimized.h b/src/optimized.h new file mode 100644 index 0000000..156626f --- /dev/null +++ b/src/optimized.h @@ -0,0 +1,20 @@ +/* +** Author: zerico2005 (2023) +** Project: Super-Sweeper +** License: MIT License +** A copy of the MIT License should be included with +** this project. If not, see https://opensource.org/license/MIT +*/ + +/* Only include this if targetting Ti84CE/eZ80 */ + +#include +void fillRect(uint24_t x1, uint24_t y1, uint24_t x2, uint8_t y2); // y2 < 256 +//void fillText(uint24_t x1, uint24_t y1, uint24_t x2, uint8_t y2); // y2 < 256 +void horiz(uint24_t x1, uint24_t y, uint24_t x2); +void vert(uint24_t x, uint24_t y1, uint24_t y2); +//void plot(uint24_t x, uint24_t y); +//void plotFast(uint8_t* z); +void fillMemory(uint8_t value, uint24_t base, uint24_t len); +//void addPointer(uint24_t x, uint24_t y); +//void fillScreen(); \ No newline at end of file diff --git a/src/prime2D.h b/src/prime2D.h index de0d774..3154c35 100644 --- a/src/prime2D.h +++ b/src/prime2D.h @@ -215,6 +215,7 @@ uint8_t linear(uint8_t y0, uint8_t y1, uint8_t x0, uint8_t x1, uint8_t x) { //Li uint8_t linear0(uint8_t y0, uint8_t y1, uint8_t x1, uint8_t x) { //x0 = 0, Optomized Linear Interpolation algorithm return ((y0 * (x1 - x)) + (y1 * x)) / x1; } + void shiftScreen() { //Moves the frame base around uint8_t change = 0; if (DEBUG & keyReady) { @@ -267,10 +268,15 @@ void keyReset(uint8_t type) { } void keyPress() { //Makes the game run at a constant FPS - uint24_t frameDelta = 0; //stores the time between frames uint24_t frameTime = 0; //stores timer_Get(1) - do { + + // static fp64 nStart = 0.0; + // static fp64 nFinish = 0.0; + // nFinish = getDecimalTime(); + // printf("\nTime: %.2lfms FPS: %.2lf",(nFinish - nStart) * 1000.0, 1.0 / (nFinish - nStart)); + // fflush(stdout); + do { frameTime = timer_Get(1); //frameTime and seed are the same so I only need to call timer_Get(1) once if (frameTime >= frameStart) { frameDelta = frameTime - frameStart; @@ -278,7 +284,8 @@ void keyPress() { //Makes the game run at a constant FPS frameDelta = (~(frameStart - frameTime)) + 1; } } while (frameDelta < FPS); - + // nStart = getDecimalTime(); + systemTime = frameTime; if (fpsCounter) { @@ -306,6 +313,7 @@ void keyPress() { //Makes the game run at a constant FPS status = FORCEQUIT; } + return; } /* Keyboard */ diff --git a/src/subMenu.h b/src/subMenu.h index 257272f..7602b81 100644 --- a/src/subMenu.h +++ b/src/subMenu.h @@ -172,8 +172,9 @@ void buildPattern() { //Draws it too gColor = 0; text6x8(268,45,25); gColor = 7; - plotFast(VRAM + ((320 * 47) + 270)); - plotFast(VRAM + ((320 * 48) + 269)); + plot(270,47); + plot(269,48); + drawPattern(); } @@ -263,8 +264,7 @@ void boardCent() { gColor = 0; text6x8(195, yCord, (percent % 10)); text6x8(188, yCord, ((percent / 10) % 10)); - text6x8(181, yCord, ((percent / 100))); //DEBUG - + // text6x8(181, yCord, ((percent / 100))); //DEBUG if (w) { yCord -= 24; @@ -379,8 +379,8 @@ void testGraphic() { text6x8(220,testGY+3,gMine); text6x8(228,testGY+3,gFlag); gColor = White; - plot(222,testGY+5); - plot(221,testGY+6); + plot(205,testGY+22); + plot(204,testGY+23); gColor = Red; fillRect(228,testGY+4,4,3); uint24_t x = 236; diff --git a/src/x86render.h b/src/x86render.h index a2f1f81..c90e0b5 100644 --- a/src/x86render.h +++ b/src/x86render.h @@ -35,6 +35,9 @@ typedef int16_t i16; typedef int32_t i32; typedef int64_t i64; +typedef float fp32; +typedef double fp64; + /* Pointers */ uint8_t _VRAM[153600]; @@ -121,6 +124,29 @@ struct bound expandedPos[56] = { {1,35,4,2},{1,36,4,1} //On }; +/* Internal Time Functions */ + fp64 getDecimalTime() { + struct timespec tp; + if (clock_gettime(CLOCK_REALTIME, &tp) == 0) { + uint64_t nanoTime = tp.tv_sec * 1000000000 + tp.tv_nsec; + return (fp64)nanoTime / 1.0e9; + } else { + perror("clock_gettime"); + return 0.0; + } + } + + uint64_t getNanoTime() { + struct timespec tp; + if (clock_gettime(CLOCK_REALTIME, &tp) == 0) { + uint64_t nanoTime = tp.tv_sec * 1000000000 + tp.tv_nsec; + return nanoTime; + } else { + perror("clock_gettime"); + return 0; + } + } + void outputVRAM(); void initLCDcontroller(); void copyFrame(); @@ -145,12 +171,12 @@ size_t ti_Read(void *data, size_t size, size_t count, uint8_t handle) { /* Replacement Libary Functions */ uint24_t timer_Get(uint8_t timer) { - outputVRAM(); - double c = ( (double)clock() ) / ( (double)CLOCKS_PER_SEC ); //Time in seconds - c *= 32768.0; - uint32_t t = ((uint32_t)c) % 16777216; - uint24_t g = t; - return g; + outputVRAM(); + fp64 timer32K = getDecimalTime(); //Time in seconds + timer32K *= 32768.0; + uint32_t t = ((uint32_t)timer32K) % 16777216; + uint24_t g = t; + return g; } const uint8_t* KEYS; @@ -309,20 +335,14 @@ void kb_Scan() { void panFrame() { uint32_t lcdBase = 0xD00000 | (*frameBase & 0x7FFF8); if (lcdBase > 0xD65800 || lcdBase < 0xD40000 - (resX * resY)) { - for (u32 r = 0; r < 153600; r++) { - videoCopy[r] = 0x00; - } + memset(videoCopy,0,153600); return; } if (lcdBase == 0xD40000) { //Fast Case - for (u32 r = 0; r < 153600; r++) { - videoCopy[r] = _VRAM[r]; - } + memcpy(videoCopy,_VRAM,153600); } else if (lcdBase < 0xD40000) { //Above u32 dif = 0xD40000 - lcdBase; - for (u32 r = 0; r < dif; r++) { - videoCopy[r] = 0x00; - } + memset(videoCopy,0,dif); for (u32 r = 0; r < 153600 - dif; r++) { if (r + dif >= 0 && r + dif < 153600 - 1) { videoCopy[r + dif] = _VRAM[r]; @@ -339,9 +359,7 @@ void panFrame() { videoCopy[r] = 0x00; } } - for (u32 r = 153600 - dif; r < 153600; r++) { - videoCopy[r] = 0x00; - } + memset(&videoCopy[153600 - dif],0,dif); } } @@ -353,9 +371,7 @@ void outputVRAM() { //printf("%X,%X,%X\n",*frameBase,0xD40000,dif); fflush(stdout); panFrame(); - for (u16 s = 0; s < 256; s++) { - color16[s] = _paletteRAM[s]; - } + memcpy(color16,_paletteRAM,256); displayFrame(); }