From f071adf8f590bb2d2ceee7d93bea120c9d754788 Mon Sep 17 00:00:00 2001 From: XaviDCR92 Date: Mon, 24 Jul 2017 22:39:16 +0200 Subject: [PATCH] * Some improvements made to improve peformance and stability. - Removed Pad module (useless). * Some functions removed from System and Gfx. --- Source/Font.c | 15 -- Source/Gfx.c | 47 +---- Source/Gfx.h | 8 +- Source/Makefile | 4 +- Source/Pad.c | 530 ------------------------------------------------ Source/Pad.h | 62 ------ Source/Serial.c | 41 ++-- Source/System.c | 406 +------------------------------------ Source/System.h | 17 -- Source/main.c | 10 +- 10 files changed, 39 insertions(+), 1101 deletions(-) delete mode 100644 Source/Pad.c delete mode 100644 Source/Pad.h diff --git a/Source/Font.c b/Source/Font.c index 3e729f3..c4b1491 100644 --- a/Source/Font.c +++ b/Source/Font.c @@ -104,21 +104,6 @@ void FontPrintText(TYPE_FONT * ptrFont, short x, short y, char* str, ...) va_list ap; - if(ptrFont->flags & FONT_1HZ_FLASH) - { - if(Gfx1HzFlash() == false) - { - return; - } - } - else if(ptrFont->flags & FONT_2HZ_FLASH) - { - if(Gfx2HzFlash() == false) - { - return; - } - } - va_start(ap, str); result = vsnprintf( _internal_text, diff --git a/Source/Gfx.c b/Source/Gfx.c index 03b33d2..b9ffb63 100644 --- a/Source/Gfx.c +++ b/Source/Gfx.c @@ -99,9 +99,6 @@ static uint8_t global_lum; // information for development purposes. static bool GfxDevMenuEnableFlag; -static bool five_hundred_ms_show; -static bool one_second_show; - void GfxSwapBuffers(void) { // Consistency check @@ -181,17 +178,7 @@ void GfxSetPrimitiveList(void) } void GfxDrawScene_Fast(void) -{ - if(System1SecondTick() == true) - { - one_second_show = one_second_show? false:true; - } - - if(System500msTick() == true) - { - five_hundred_ms_show = five_hundred_ms_show? false:true; - } - +{ GfxSwapBuffers(); FontCyclic(); GsDrawList(); @@ -227,8 +214,6 @@ void GfxSortSprite(GsSprite * spr) unsigned char aux_tpage = spr->tpage; short aux_w = spr->w; short aux_x = spr->x; - bool has_1hz_flash = spr->attribute & GFX_1HZ_FLASH; - bool has_2hz_flash = spr->attribute & GFX_2HZ_FLASH; if( (spr->w <= 0) || (spr->h <= 0) ) { @@ -240,14 +225,6 @@ void GfxSortSprite(GsSprite * spr) { return; } - else if(has_2hz_flash && Gfx2HzFlash() == false) - { - return; - } - else if(has_1hz_flash && Gfx1HzFlash() == false) - { - return; - } if(global_lum != NORMAL_LUMINANCE) { @@ -278,12 +255,7 @@ void GfxSortSprite(GsSprite * spr) spr->b -= NORMAL_LUMINANCE - global_lum; } } - - if(has_1hz_flash == true) - { - spr->attribute &= ~(GFX_1HZ_FLASH); - } - + if(spr->w > MAX_SIZE_FOR_GSSPRITE) { // GsSprites can't be bigger than 256x256, so since display @@ -307,11 +279,6 @@ void GfxSortSprite(GsSprite * spr) GsSortSprite(spr); } - if(has_1hz_flash == true) - { - spr->attribute |= GFX_1HZ_FLASH; - } - spr->r = aux_r; spr->g = aux_g; spr->b = aux_b; @@ -559,16 +526,6 @@ void GfxSaveDisplayData(GsSprite *spr) while(GfxIsGPUBusy() == true); } -bool Gfx1HzFlash(void) -{ - return one_second_show; -} - -bool Gfx2HzFlash(void) -{ - return five_hundred_ms_show; -} - bool GfxTPageOffsetFromVRAMPosition(GsSprite * spr, short x, short y) { if( (x >= VRAM_W) || (x < 0) || (y >= VRAM_H) || (y < 0) ) diff --git a/Source/Gfx.h b/Source/Gfx.h index 76eaff2..b2b8295 100644 --- a/Source/Gfx.h +++ b/Source/Gfx.h @@ -8,7 +8,7 @@ #include "Global_Inc.h" #include "System.h" -#include "Pad.h" + /* ************************************* * Defines @@ -96,12 +96,6 @@ void GfxSetSplitScreen(uint8_t playerIndex); void GfxDisableSplitScreen(void); -// Switches between true and false every 1 exact second (used for flashing effects) -bool Gfx1HzFlash(void); - -// Switches between true and false every 500 milliseconds (used for flashing effects) -bool Gfx2HzFlash(void); - void GfxDrawScene_NoSwap(void); void GfxDevMenuEnable(void); diff --git a/Source/Makefile b/Source/Makefile index ed9324f..f20974f 100644 --- a/Source/Makefile +++ b/Source/Makefile @@ -11,7 +11,7 @@ PROJECT_DIR = ~/OpenSend INIT_ADDR=0x801A0000 ELF2EXE = elf2exe -ELF2EXE_FLAGS = -mark="A homebrew game created with PSXSDK" -init_addr=$(INIT_ADDR) +ELF2EXE_FLAGS = -mark="Open-source PSX-EXE loader created with PSXSDK" -init_addr=$(INIT_ADDR) LICENSE_FILE = /usr/local/psxsdk/share/licenses/infoeur.dat PSXSDK_DIR = /usr/local/psxsdk/bin @@ -36,7 +36,7 @@ rebuild: remove build build: clean objects $(PROJECT).elf $(PROJECT).exe -objects: $(addprefix $(OBJ_DIR)/,main.o System.o Gfx.o Pad.o \ +objects: $(addprefix $(OBJ_DIR)/,main.o System.o Gfx.o \ LoadMenu.o EndAnimation.o \ Font.o Serial.o) diff --git a/Source/Pad.c b/Source/Pad.c deleted file mode 100644 index ef56f08..0000000 --- a/Source/Pad.c +++ /dev/null @@ -1,530 +0,0 @@ -/* ************************************* - * Includes - * *************************************/ - -#include "Pad.h" - -/* ************************************* - * Defines - * *************************************/ - -#define PAD_ONE 0 -#define PAD_TWO 1 -#define PAD_CHEAT_TIMEOUT 20 // 2 units * 100 ms/unit = 2000 ms -#define PAD_MAX_CHEATS 16 - -/* ************************************** - * Structs and enums * - * *************************************/ - -enum -{ - - PAD_CROSS_INDEX = 0, - PAD_SQUARE_INDEX, - PAD_TRIANGLE_INDEX, - PAD_CIRCLE_INDEX, - - PAD_DOWN_INDEX, - PAD_LEFT_INDEX, - PAD_UP_INDEX, - PAD_RIGHT_INDEX, - - PAD_L1_INDEX, - PAD_L2_INDEX, - - PAD_R1_INDEX, - PAD_R2_INDEX, - - NUMBER_OF_KEYS - -}; - - -/* ************************************* - * Local Prototypes - * *************************************/ - -static void PadOneVibrationHandler(void); -static void PadTwoVibrationHandler(void); -static void PadCheatHandler(uint8_t n_pad); -static void PadOneCleanCheatArray(void); -static void PadTwoCleanCheatArray(void); -static psx_pad_state PadOneGetState(void); -uint8_t PadGetKeyIndex(unsigned short key); - -/* ************************************* - * Local Variables - * *************************************/ - -// Pad data -static unsigned short pad1; -static unsigned short pad2; - -// Pad data from previous frame -static unsigned short previous_pad1; -static unsigned short previous_pad2; - -// Vibration timers -static uint16_t pad1_vibration_timer; -static uint16_t pad2_vibration_timer; - -// Vibration strenght data (big motor) -static uint8_t pad1_big_vibration_force; -static uint8_t pad2_big_vibration_force; - -// Vibration strenght data (small motor) -static uint8_t pad1_small_vibration_force; -static uint8_t pad2_small_vibration_force; - -// Timers for each key pressed (used for PadXXKeyRepeat() ) -static uint8_t pad1_keys_repeat[NUMBER_OF_KEYS]; -static uint8_t pad2_keys_repeat[NUMBER_OF_KEYS]; - -static unsigned short pad1_last_key_single_pressed; -static unsigned short pad2_last_key_single_pressed; - -// These arrays include last 16 buttons pressed by user and keeps them -// for cheating purposes. They are cleaned if no keys are pressed during -// PAD_CHEAT_TIMEOUT milliseconds. -static unsigned short pad1_cheat_array[CHEAT_ARRAY_SIZE]; -static unsigned short pad2_cheat_array[CHEAT_ARRAY_SIZE]; - -// Pointers to timers which clean padX_cheat_array. -static TYPE_TIMER* pad1_cheat_timer; -static TYPE_TIMER* pad2_cheat_timer; - -static TYPE_CHEAT * cheatsArray[PAD_MAX_CHEATS]; - -psx_pad_state PadOneGetState(void) -{ - psx_pad_state PadOne; - - PSX_PollPad_Fast(PAD_ONE,&PadOne); - - return PadOne; -} - -bool PadOneConnected(void) -{ - psx_pad_state PadOne = PadOneGetState(); - - if(PadOne.status != PAD_STATUS_OK) - { - return false; - } - - return true; -} - -bool PadOneAnyKeyPressed(void) -{ - return (bool)pad1; -} - -bool PadOneDirectionKeyPressed(void) -{ - return ( (PadOneKeyPressed(PAD_UP) == true) - || - (PadOneKeyPressed(PAD_LEFT) == true) - || - (PadOneKeyPressed(PAD_RIGHT) == true) - || - (PadOneKeyPressed(PAD_DOWN) == true) ); -} - -bool PadOneDirectionKeyReleased(void) -{ - return ( (PadOneKeyReleased(PAD_UP) == true) - || - (PadOneKeyReleased(PAD_LEFT) == true) - || - (PadOneKeyReleased(PAD_RIGHT) == true) - || - (PadOneKeyReleased(PAD_DOWN) == true) ); -} - -bool PadTwoDirectionKeyReleased(void) -{ - return ( (PadTwoKeyReleased(PAD_UP) == true) - || - (PadTwoKeyReleased(PAD_LEFT) == true) - || - (PadTwoKeyReleased(PAD_RIGHT) == true) - || - (PadTwoKeyReleased(PAD_DOWN) == true) ); -} - -bool PadTwoDirectionKeyPressed(void) -{ - return ( (PadTwoKeyPressed(PAD_UP) == true) - || - (PadTwoKeyPressed(PAD_LEFT) == true) - || - (PadTwoKeyPressed(PAD_RIGHT) == true) - || - (PadTwoKeyPressed(PAD_DOWN) == true) ); -} - -bool PadTwoAnyKeyPressed(void) -{ - return (bool)pad2; -} - -bool PadOneKeyPressed(unsigned short key) -{ - return (bool)( pad1 & key ); -} - -bool PadTwoKeyPressed(unsigned short key) -{ - return (bool)( pad2 & key ); -} - -bool PadOneKeySinglePress(unsigned short key) -{ - return (bool)( !(previous_pad1 & key) && (pad1 & key) ); -} - -bool PadTwoKeySinglePress(unsigned short key) -{ - return (bool)( !(previous_pad2 & key) && (pad2 & key) ); -} - -bool PadOneKeyRepeat(unsigned short key, uint8_t time) -{ - uint8_t key_index = PadGetKeyIndex(key); - - if(key_index == NUMBER_OF_KEYS) - { - return false; - } - - pad1_keys_repeat[key_index]++; - - if(pad1_keys_repeat[key_index] >= time) - { - pad1_keys_repeat[key_index] = 0; - return true; - } - - return false; -} - -bool PadTwoKeyRepeat(unsigned short key, uint8_t time) -{ - uint8_t key_index = PadGetKeyIndex(key); - - if(key_index == NUMBER_OF_KEYS) - { - return false; - } - - pad2_keys_repeat[key_index]++; - - if(pad2_keys_repeat[key_index] >= time) - { - pad2_keys_repeat[key_index] = 0; - return true; - } - - return false; -} - -void PadOneVibrationHandler(void) -{ - if(PadOneIsVibrationEnabled() == true) - { - pad_enable_vibration(PAD_ONE); - pad_set_vibration(PAD_ONE,pad1_small_vibration_force,pad1_big_vibration_force); - pad1_vibration_timer--; - } -} - -void PadTwoVibrationHandler(void) -{ - if(PadTwoIsVibrationEnabled() == true) - { - pad_enable_vibration(PAD_TWO); - pad_set_vibration(PAD_TWO,pad2_small_vibration_force,pad2_big_vibration_force); - pad2_vibration_timer--; - } -} - -bool PadOneIsVibrationEnabled(void) -{ - return (pad1_vibration_timer & true); -} - -bool PadTwoIsVibrationEnabled(void) -{ - return (pad2_vibration_timer & true); -} - -bool UpdatePads(void) -{ - PadOneVibrationHandler(); - - PadTwoVibrationHandler(); - - PadCheatHandler(PAD_ONE); - - PadCheatHandler(PAD_TWO); - - // Get now-old pad data - previous_pad1 = pad1; - previous_pad2 = pad2; - - PSX_ReadPad(&pad1,&pad2); - - if(PadOneConnected() == false) - { - return false; - } - - if(!(previous_pad1 & pad1) ) - { - pad1_last_key_single_pressed = pad1; - } - else - { - pad1_last_key_single_pressed = 0; - } - - if(!(previous_pad2 & pad2) ) - { - pad2_last_key_single_pressed = pad2; - } - else - { - pad2_last_key_single_pressed = 0; - } - - return true; -} - -bool PadOneKeyReleased(unsigned short key) -{ - return ( !(pad1 & key) && (previous_pad1 & key) ); -} - -bool PadTwoKeyReleased(unsigned short key) -{ - return ( !(pad2 & key) && (previous_pad2 & key) ); -} - -uint8_t PadGetKeyIndex(unsigned short key) -{ - switch(key) - { - case PAD_CROSS: - return PAD_CROSS_INDEX; - break; - - case PAD_SQUARE: - return PAD_SQUARE_INDEX; - break; - - case PAD_TRIANGLE: - return PAD_TRIANGLE_INDEX; - break; - - case PAD_CIRCLE: - return PAD_CIRCLE_INDEX; - break; - - case PAD_DOWN: - return PAD_DOWN_INDEX; - break; - - case PAD_LEFT: - return PAD_LEFT_INDEX; - break; - - case PAD_UP: - return PAD_UP_INDEX; - break; - - case PAD_RIGHT: - return PAD_RIGHT_INDEX; - break; - - case PAD_L1: - return PAD_L1_INDEX; - break; - - case PAD_R1: - return PAD_R1_INDEX; - break; - - case PAD_L2: - return PAD_L2_INDEX; - break; - - case PAD_R2: - return PAD_R2_INDEX; - break; - - default: - return NUMBER_OF_KEYS; - break; - } -} - -unsigned short* PadOneGetAddress(void) -{ - return &pad1; -} - -void PadClearData(void) -{ - pad1 = 0; - pad2 = 0; - - previous_pad1 = 0; - previous_pad2 = 0; -} - -void PadInit(void) -{ - pad1_cheat_timer = SystemCreateTimer(PAD_CHEAT_TIMEOUT,true /* Repeat flag */,&PadOneCleanCheatArray); - pad2_cheat_timer = SystemCreateTimer(PAD_CHEAT_TIMEOUT,true /* Repeat flag */,&PadTwoCleanCheatArray); - - memset(cheatsArray,0, sizeof(cheatsArray)); -} - -void PadCheatHandler(uint8_t n_pad) -{ - unsigned short available_keys[12] = { PAD_LEFT, PAD_RIGHT, PAD_UP, PAD_DOWN, - PAD_L2, PAD_R2, PAD_L1, PAD_R1, - PAD_TRIANGLE, PAD_CIRCLE, PAD_CROSS, PAD_SQUARE }; - - uint8_t i; - uint8_t keys_released = 0; - unsigned short key; - uint8_t j; - bool (*pressed_callback)(unsigned short); - void (*clean_callback)(void); - bool success = false; - unsigned short* cheat_array; - TYPE_TIMER* timer; - - switch(n_pad) - { - case PAD_ONE: - pressed_callback = &PadOneKeySinglePress; - cheat_array = pad1_cheat_array; - clean_callback = &PadOneCleanCheatArray; - timer = pad1_cheat_timer; - break; - - case PAD_TWO: - pressed_callback = &PadTwoKeySinglePress; - cheat_array = pad2_cheat_array; - clean_callback = &PadTwoCleanCheatArray; - timer = pad2_cheat_timer; - break; - - default: - dprintf("Invalid pad called for PadCheatHandler()!\n"); - return; - } - - for(i = 0; i < PAD_MAX_CHEATS; i++) - { - if(cheatsArray[i] != NULL) - { - if(SystemArrayCompare(cheat_array, cheatsArray[i]->Combination, CHEAT_ARRAY_SIZE) == true) - { - if(cheatsArray[i]->Callback != NULL) - { - if(clean_callback != NULL) - { - clean_callback(); - } - - cheatsArray[i]->Callback(); - - return; - } - } - } - } - - for(i = 0; i < sizeof(available_keys) / sizeof(unsigned short); i++) - { - if(pressed_callback(available_keys[i]) == true) - { - SystemTimerRestart(timer); - key = available_keys[i]; - keys_released++; - } - } - - if(keys_released != 1) - { - return; - } - - // Check for full array (return success = true if an empty array - // element was found. - for(j = 0; j < CHEAT_ARRAY_SIZE; j++) - { - if(cheat_array[j] == 0) - { - success = true; - break; - } - } - - if(success == false) - { - if(clean_callback != NULL) - { - // Overrun - clean_callback(); - } - } - - cheat_array[j] = key; -} - -bool PadAddCheat(TYPE_CHEAT * cheat) -{ - static uint8_t idx = 0; - - if(idx >= PAD_MAX_CHEATS) - { - dprintf("Maximum number of cheats exceeded!\n"); - return false; - } - - cheatsArray[idx++] = cheat; - - return true; -} - -void PadOneCleanCheatArray(void) -{ - memset(pad1_cheat_array,0,sizeof(unsigned short) * CHEAT_ARRAY_SIZE); -} - -void PadTwoCleanCheatArray(void) -{ - memset(pad2_cheat_array,0,sizeof(unsigned short) * CHEAT_ARRAY_SIZE); -} - -unsigned short* PadGetPlayerOneCheatArray(void) -{ - return pad1_cheat_array; -} - -unsigned short PadOneGetLastKeySinglePressed(void) -{ - return pad1_last_key_single_pressed; -} - -unsigned short PadTwoGetLastKeySinglePressed(void) -{ - return pad2_last_key_single_pressed; -} diff --git a/Source/Pad.h b/Source/Pad.h deleted file mode 100644 index 5b5fa1d..0000000 --- a/Source/Pad.h +++ /dev/null @@ -1,62 +0,0 @@ -#ifndef __PAD_HEADER__ -#define __PAD_HEADER__ - -/* ************************************* - * Includes - * *************************************/ - -#include "Global_Inc.h" -#include "System.h" -#include "GameStructures.h" - -/* ************************************* - * Defines - * *************************************/ - -#define PAD_ALWAYS_REPEAT 1 - -/* ************************************* - * Global prototypes - * *************************************/ - -void PadInit(void); -void PadClearData(void); - -bool PadOneConnected(void); - -bool PadOneAnyKeyPressed(void); -bool PadTwoAnyKeyPressed(void); - -bool PadOneKeyPressed(unsigned short key); -bool PadTwoKeyPressed(unsigned short key); - -bool PadOneKeyRepeat(unsigned short key, uint8_t time); -bool PadTwoKeyRepeat(unsigned short key, uint8_t time); - -bool PadOneKeyReleased(unsigned short key); -bool PadTwoKeyReleased(unsigned short key); - -bool PadOneKeySinglePress(unsigned short key); -bool PadTwoKeySinglePress(unsigned short key); - -unsigned short PadOneGetLastKeySinglePressed(void); -unsigned short PadTwoGetLastKeySinglePressed(void); - -bool PadOneDirectionKeyPressed(void); -bool PadTwoDirectionKeyPressed(void); - -bool PadOneDirectionKeyReleased(void); -bool PadTwoDirectionKeyReleased(void); - -bool UpdatePads(void); -bool PadOneIsVibrationEnabled(void); -bool PadTwoIsVibrationEnabled(void); - -bool PadAddCheat(TYPE_CHEAT * cheat); - -unsigned short* PadGetPlayerOneCheatArray(void); - -// Experimental (to be removed) -unsigned short* PadOneGetAddress(void); - -#endif //__PAD_HEADER__ diff --git a/Source/Serial.c b/Source/Serial.c index c66b32f..2e2a1a4 100644 --- a/Source/Serial.c +++ b/Source/Serial.c @@ -80,16 +80,28 @@ void ISR_Serial(void) SERIAL_STATE_TEXT_Y = Y_SCREEN_RESOLUTION >> 1, }; - if((GfxIsGPUBusy() == true) || (serial_busy == true) ) + SystemIncreaseGlobalTimer(); + + if( (GfxIsGPUBusy() == true) || (SystemIsBusy() == true) ) { return; } - GsSortGPoly4(&SerialBg); - FontSetFlags(&SmallFont, FONT_BLEND_EFFECT | FONT_H_CENTERED); - GfxSetGlobalLuminance(0); + if(SerialState == SERIAL_STATE_READING_EXE_DATA) + { + if(System1SecondTick() == false) + { + return; + } + else + { + FontSetFlags(&SmallFont, FONT_H_CENTERED); + } + } + + GsSortGPoly4(&SerialBg); switch(SerialState) { @@ -130,7 +142,7 @@ void ISR_Serial(void) break; } - FontSetFlags(&SmallFont, FONT_NOFLAGS); + FontSetFlags(&SmallFont, FONT_H_CENTERED); if(RAMDest_Address != 0) { @@ -174,12 +186,10 @@ void SerialInit(void) { uint8_t receivedBytes; - SerialState = SERIAL_STATE_INIT; - - dprintf("SerialInit...\n"); - SetVBlankHandler(&ISR_Serial); + SerialState = SERIAL_STATE_INIT; + SIOStart(SERIAL_BAUDRATE); SerialState = SERIAL_STATE_STANDBY; @@ -195,7 +205,6 @@ void SerialInit(void) if(receivedBytes != 99) { dprintf("Did not receive input magic number!\n"); - SetVBlankHandler(&ISR_SystemDefaultVBlank); return; } @@ -217,11 +226,10 @@ bool SerialRead(uint8_t* ptrArray, size_t nBytes) totalBytes = nBytes; serial_busy = true; - SystemDisableVBlankInterrupt(); if(nBytes == 0) { - dprintf("SerialRead: invalid size %d\n", nBytes); + SerialWrite("SerialRead: invalid size %d\n", strnlen("SerialRead: invalid size %d\n", 30)); return false; } @@ -237,19 +245,18 @@ bool SerialRead(uint8_t* ptrArray, size_t nBytes) serial_busy = false; - SystemEnableVBlankInterrupt(); - return true; } bool SerialWrite(void* ptrArray, size_t nBytes) { serial_busy = true; + SystemDisableVBlankInterrupt(); if(nBytes == 0) { - dprintf("SerialWrite: invalid size %d\n", nBytes); + SerialWrite("SerialRead: invalid size %d\n", strnlen("SerialRead: invalid size %d\n", 30)); return false; } @@ -263,9 +270,9 @@ bool SerialWrite(void* ptrArray, size_t nBytes) }while(--nBytes); - serial_busy = false; - SystemEnableVBlankInterrupt(); + serial_busy = false; + return true; } diff --git a/Source/System.c b/Source/System.c index e636925..852fea1 100644 --- a/Source/System.c +++ b/Source/System.c @@ -13,13 +13,12 @@ #define END_STACK_PATTERN (uint32_t) 0x18022015 #define BEGIN_STACK_ADDRESS (uint32_t*) 0x801FFF00 #define STACK_SIZE 0x1000 -#define I_MASK (*(unsigned int*)0x1F801074) +#define I_MASK (*(volatile unsigned int*)0x1F801074) /* ************************************* * Local Prototypes * *************************************/ - -static void SystemCheckTimer(bool* timer, uint64_t* last_timer, uint8_t step); + static void SystemSetStackPattern(void); /* ************************************* @@ -36,14 +35,8 @@ static bool rand_seed; static volatile bool refresh_needed; //Timers static bool one_second_timer; -static bool hundred_ms_timer; -static bool five_hundred_ms_timer; -//Emergency mode flag. Toggled on pad connected/disconnected -static bool emergency_mode; //Critical section is entered (i.e.: when accessing fopen() or other BIOS functions static volatile bool system_busy; -//Timer array. -static TYPE_TIMER timer_array[SYSTEM_MAX_TIMERS]; /* ******************************************************************* * @@ -77,10 +70,6 @@ void SystemInit(void) #endif //_PAL_MODE_ //SPU init SsInit(); - //Reset all user-handled timers - SystemResetTimers(); - //Pads init - PadInit(); //Set Drawing Environment GfxInitDrawEnv(); //Set Display Environment @@ -251,92 +240,7 @@ void SystemDisableScreenRefresh(void) bool System1SecondTick(void) { - return one_second_timer; -} - -/* ******************************************************************* - * - * @name: bool System100msTick(void) - * - * @author: Xavier Del Campo - * - * @return: bool variable with a 1-cycle-length pulse that gets - * set every 100 milliseconds. - * - * *******************************************************************/ - -bool System100msTick(void) -{ - return hundred_ms_timer; -} - -/* ******************************************************************* - * - * @name bool System500msTick(void) - * - * @author: Xavier Del Campo - * - * @return: bool variable with a 1-cycle-length pulse that gets - * set every 500 milliseconds. - * - * *******************************************************************/ - -bool System500msTick(void) -{ - return five_hundred_ms_timer; -} - -/* ******************************************************************* - * - * @name void SystemRunTimers(void) - * - * @author: Xavier Del Campo - * - * @brief: general timer handler - * - * @remarks: 1 second, 500 ms and 100 ms ticks get updated here. - * - * *******************************************************************/ - -void SystemRunTimers(void) -{ - static uint64_t last_one_second_tick; - static uint64_t last_100_ms_tick; - static uint64_t last_500_ms_tick; - - SystemCheckTimer(&one_second_timer, &last_one_second_tick, REFRESH_FREQUENCY); - -#ifdef _PAL_MODE_ - SystemCheckTimer(&hundred_ms_timer, &last_100_ms_tick, 2 /* 2 * 50 ms = 100 ms */); - SystemCheckTimer(&five_hundred_ms_timer, &last_500_ms_tick, 10 /* 10 * 50 ms = 500 ms */); -#else // _PAL_MODE_ - SystemCheckTimer(&hundred_ms_timer, &last_100_ms_tick, 3); -#endif // _PAL_MODE_ - -} - -/* ******************************************************************************** - * - * @name void SystemCheckTimer(bool* timer, uint64_t* last_timer, uint8_t step) - * - * @author: Xavier Del Campo - * - * @brief: Checks if needed time step has been elapsed. If true, flag gets set. - * - * *******************************************************************************/ - -void SystemCheckTimer(bool* timer, uint64_t* last_timer, uint8_t step) -{ - if(*timer == true) - { - *timer = false; - } - - if(global_timer >= (*last_timer + step) ) - { - *timer = true; - *last_timer = global_timer; - } + return !(global_timer % REFRESH_FREQUENCY); } /* **************************************************************************************** @@ -459,23 +363,6 @@ void SystemClearBuffer(void) memset(file_buffer, 0, sizeof(file_buffer)); } -/* ****************************************************************** - * - * @name void SystemWaitCycles(uint32_t cycles) - * - * @author: Xavier Del Campo - * - * @return: halts program execution for n-"cycles" - * - * *****************************************************************/ - -void SystemWaitCycles(uint32_t cycles) -{ - uint64_t currentTime = global_timer; - - while(global_timer < (currentTime + cycles) ); -} - /* ****************************************************************** * * @name uint32_t SystemRand(uint32_t min, uint32_t max) @@ -494,38 +381,6 @@ uint32_t SystemRand(uint32_t min, uint32_t max) return rand() % (max - min + 1) + min; } -/* *********************************************************************** - * - * @name void SystemSetEmergencyMode(bool value) - * - * @author: Xavier Del Campo - * - * @brief: Sets emergency mode flag. - * - * @remarks: emergency mode is set once that a controller is unplugged. - * - * ***********************************************************************/ - -void SystemSetEmergencyMode(bool value) -{ - emergency_mode = value; -} - -/* *********************************************************************** - * - * @name bool SystemGetEmergencyMode(void) - * - * @author: Xavier Del Campo - * - * @return: returns emergency mode flag. - * - * ***********************************************************************/ - -bool SystemGetEmergencyMode(void) -{ - return emergency_mode; -} - /* *********************************************************************** * * @name volatile bool SystemIsBusy(void) @@ -541,211 +396,6 @@ volatile bool SystemIsBusy(void) return system_busy; } -/* **************************************************************************** - * - * @name bool SystemContains_u8(uint8_t value, uint8_t* buffer, size_t sz) - * - * @author: Xavier Del Campo - * - * @brief: checks if a certain value is contained in a buffer with size "sz". - * - * @return: true if value is contained inside buffer, false otherwise. - * - * ****************************************************************************/ - -bool SystemContains_u8(uint8_t value, uint8_t* buffer, size_t sz) -{ - size_t i = 0; - - for(i = 0; i < sz; i++) - { - if(buffer[i] == value) - { - return true; - } - } - - return false; -} - -/* **************************************************************************** - * - * @name bool SystemContains_u16(uint16_t value, uint16_t* buffer, size_t sz) - * - * @author: Xavier Del Campo - * - * @brief: checks if a certain value is contained in a buffer with size "sz". - * Variant for u16 variables. - * - * @return: true if value is contained inside buffer, false otherwise. - * - * ****************************************************************************/ - -bool SystemContains_u16(uint16_t value, uint16_t* buffer, size_t sz) -{ - size_t i = 0; - - for(i = 0; i < sz; i++) - { - if(buffer[i] == value) - { - return true; - } - } - - return false; -} - -/* ******************************************************************************************** - * - * @name TYPE_TIMER* SystemCreateTimer(uint32_t t, bool rf, void (*timer_callback)(void) ) - * - * @author: Xavier Del Campo - * - * @brief: fills a TYPE_TIMER structure with input parameters - * - * @param: uint32_t t: - * Timeout value (1 unit = 10 ms) - * bool rf: - * Repeat flag - * void (*timer_callback)(void) - * Function to be called on timeout - * - * @return: pointer to TYPE_TIMER structure if filled correctly, NULL pointer otherwise. - * - * ********************************************************************************************/ - -TYPE_TIMER* SystemCreateTimer(uint32_t t, bool rf, void (*timer_callback)(void) ) -{ - bool success = false; - uint8_t i; - - if(t == 0) - { - dprintf("Cannot create timer with time == 0!\n"); - return NULL; - } - - for(i = 0; i < SYSTEM_MAX_TIMERS; i++) - { - if(timer_array[i].busy == false) - { - timer_array[i].Timeout_Callback = timer_callback; - timer_array[i].time = t; - timer_array[i].orig_time = t; - timer_array[i].repeat_flag = rf; - timer_array[i].busy = true; - success = true; - break; - } - } - - if(success == false) - { - dprintf("Could not find any free timer!\n"); - return NULL; - } - - return &timer_array[i]; -} - -/* ******************************************* - * - * @name void SystemResetTimers(void) - * - * @author: Xavier Del Campo - * - * @brief: reportedly, removes all timers. - * - * *******************************************/ - -void SystemResetTimers(void) -{ - uint8_t i; - - for(i = 0; i < SYSTEM_MAX_TIMERS; i++) - { - SystemTimerRemove(&timer_array[i]); - } -} - -/* ***************************************************** - * - * @name void SystemUserTimersHandler(void) - * - * @author: Xavier Del Campo - * - * @brief: reportedly, handles all available timers. - * - * @remarks: calls callback on timeout. - * - * *****************************************************/ - -void SystemUserTimersHandler(void) -{ - uint8_t i; - - for(i = 0; i < SYSTEM_MAX_TIMERS; i++) - { - if(timer_array[i].busy == true) - { - if(System100msTick() == true) - { - timer_array[i].time--; - - if(timer_array[i].time == 0) - { - timer_array[i].Timeout_Callback(); - - if(timer_array[i].repeat_flag == true) - { - timer_array[i].time = timer_array[i].orig_time; - } - } - } - } - } -} - -/* ********************************************************************* - * - * @name void SystemUserTimersHandler(void) - * - * @author: Xavier Del Campo - * - * @brief: sets time left for TYPE_TIMER instance to initial value. - * - * @remarks: specially used when TYPE_TIMER.rf is enabled. - * - * *********************************************************************/ - -void SystemTimerRestart(TYPE_TIMER* timer) -{ - timer->time = timer->orig_time; -} - -/* ********************************************************************* - * - * @name void SystemTimerRemove(TYPE_TIMER* timer) - * - * @author: Xavier Del Campo - * - * @brief: Resets timer parameters to default values so timer instance - * can be recycled. - * - * @remarks: - * - * *********************************************************************/ - -void SystemTimerRemove(TYPE_TIMER* timer) -{ - timer->time = 0; - timer->orig_time = 0; - timer->Timeout_Callback = NULL; - timer->busy = false; - timer->repeat_flag = false; -} - bool SystemArrayCompare(unsigned short* arr1, unsigned short* arr2, size_t sz) { size_t i; @@ -826,62 +476,18 @@ int32_t SystemIndexOfStringArray(char* str, char** array) return -1; } - -int32_t SystemIndexOf_U16(uint16_t value, uint16_t* array, uint32_t sz) -{ - int32_t i; - - for(i = 0; i < sz; i++) - { - if(value == array[i]) - { - return i; - } - } - - return -1; -} - -int32_t SystemIndexOf_U8(uint8_t value, uint8_t* array, uint32_t from, uint32_t sz) -{ - int32_t i; - - for(i = from; i < sz; i++) - { - if(value == array[i]) - { - return i; - } - } - - return -1; -} - void SystemCyclicHandler(void) -{ - if(UpdatePads() == false) - { - SystemSetEmergencyMode(true); - } - else - { - SystemSetEmergencyMode(false); - } - - SystemRunTimers(); - - SystemUserTimersHandler(); - +{ SystemDisableScreenRefresh(); SystemCheckStack(); } void SystemDisableVBlankInterrupt(void) { - I_MASK &= ~(0x0001); + I_MASK &= ~(0x00000001); } void SystemEnableVBlankInterrupt(void) { - I_MASK |= (0x0001); + I_MASK |= (0x00000001); } diff --git a/Source/System.h b/Source/System.h index 89f6be4..2994366 100644 --- a/Source/System.h +++ b/Source/System.h @@ -52,9 +52,6 @@ bool SystemIsRandSeedSet(void); // Stops program flow during X cycles void SystemWaitCycles(uint32_t cycles); -// To be called from GfxDrawScene after each cycle -void SystemRunTimers(void); - // 1 cycle-length flag with a frequency of 1 Hz bool System1SecondTick(void); @@ -88,26 +85,12 @@ bool SystemContains_u8(uint8_t value, uint8_t* buffer, size_t sz); // Overload for uint16_t bool SystemContains_u16(uint16_t value, uint16_t* buffer, size_t sz); -// Creates a timer instance wiht a determined value and associates it to a callback -// Once time expires, callback is automatically called right after GfxDrawScene(). -// Time is expressed so that t = 100 ms e.g.: 2 seconds = 20. -TYPE_TIMER* SystemCreateTimer(uint32_t t, bool rf, void (*timer_callback)(void) ); - // Reportedly, sets all timer data to zero. void SystemResetTimers(void); // To be called every cycle (i.e.: inside GfxDrawScene() ). void SystemUserTimersHandler(void); -// Sets timer remaining time to initial value. -void SystemTimerRestart(TYPE_TIMER* timer); - -// Flushes a timer pointed to by timer. -void SystemTimerRemove(TYPE_TIMER* timer); - -// Compares two arrays of unsigned short type. -bool SystemArrayCompare(unsigned short* arr1, unsigned short* arr2, size_t sz); - // Looks for string "str" inside a string array pointed to by "array". // Returns index inside string array on success, -1 if not found. int32_t SystemIndexOfStringArray(char* str, char** array); diff --git a/Source/main.c b/Source/main.c index 6e2b227..650f907 100644 --- a/Source/main.c +++ b/Source/main.c @@ -54,6 +54,8 @@ int main(void) uint32_t i; void (*exeAddress)(void); + GfxSetGlobalLuminance(0); + SerialInit(); // Read PSX-EXE header (32 bytes will be enough). @@ -117,8 +119,6 @@ int main(void) SerialSetState(SERIAL_STATE_READING_EXE_DATA); - SystemDisableVBlankInterrupt(); - while(GfxIsGPUBusy() == true); for(i = 0; i < ExeSize; i += EXE_DATA_PACKET_SIZE) @@ -143,16 +143,14 @@ int main(void) SerialWrite(ACK_BYTE_STRING, sizeof(uint8_t)); // Write ACK } - SystemEnableVBlankInterrupt(); - - //SystemLoadFileToBuffer("cdrom:\\AIRPORT.EXE;1", 2048, (uint8_t*)0x80010000, (uint32_t) (0x801A0000 - 0x80010000) ); - SetVBlankHandler(&ISR_SystemDefaultVBlank); // Make a pretty animation before exeting OpenSend application. EndAnimation(); + PSX_DeInit(); + // PSX-EXE has been successfully loaded into RAM. Run executable! //dprintf("Entering exe...\n");