diff --git a/ovl-upload.c b/ovl-upload.c index 541a311..dde479b 100644 --- a/ovl-upload.c +++ b/ovl-upload.c @@ -275,18 +275,6 @@ int main() { // Main loop while (1) { - // Check inBuffer for answer - - //~ if( strcmp(inBuffer, OKAY) == 0 ){ - - //~ loadFileID = !loadFileID; - - //~ FntPrint("Change"); - - //~ for(char c = 0; c < sizeof(inBuffer); c++){ inBuffer[c] = 0; } - - //~ } - // Overlay switch if ( overlayFileID != loadFileIDwas ){ @@ -325,6 +313,18 @@ int main() { } + #ifndef USECD + + if (! PCload( &load_all_overlays_here, inBuffer, &overlayFileID ) ){ + + overlayFileID = !overlayFileID; + + loadFileIDwas = !loadFileIDwas; + + }; + + #endif + #ifdef USECD cdread = CdReadFile( (char *)(overlayFile), &load_all_overlays_here, 0); @@ -349,26 +349,11 @@ int main() { // If select is pressed, change overlay - if (PadStatus & PADselect && !timer) { + if ( ( PadStatus & PADselect ) && !timer ) { - // We send the memory address where the file should be loaded, the memory address of the loadFileID, so that the screen is updated when it changes, and the file id. - // format : 00(:)01(:)06(:)04 xx xx xx xx(:)04 xx xx xx xx(:)01 (separators are not send) - // 14 bytes - #ifndef USECD + overlayFileID = !overlayFileID; - PCload( &load_all_overlays_here, inBuffer, &overlayFileID ); - - #endif - - #ifdef USECD - - // We can do that because we only have two files - - overlayFileID = !overlayFileID; - - #endif - - timer = 30; + timer = 150; } @@ -454,12 +439,13 @@ int main() { FntPrint("Hello overlay!\n"); #ifndef USECD + + FntPrint("loadFileIDwas : %d\n", loadFileIDwas); FntPrint("Overlay with id %d loaded at 0x%08x\n", overlayFileID, &load_all_overlays_here ); FntPrint("buffer at %08x : %s\n", inBuffer, inBuffer); - #endif #ifdef USECD diff --git a/ovl-upload.ps-exe b/ovl-upload.ps-exe index 4c25229..2cacabf 100755 Binary files a/ovl-upload.ps-exe and b/ovl-upload.ps-exe differ diff --git a/ovl-upload.py b/ovl-upload.py index a9e7ac2..872c6da 100755 --- a/ovl-upload.py +++ b/ovl-upload.py @@ -38,14 +38,22 @@ pcdrvEscape = '00' pcdrvProtocol = '01' -pcdrvCommand = '06' +# Commands + +LOAD = '06' + +OPEN = '00' + +loadParams = {'memAddr':-1,'flagAddr':-1, 'loadFile':-1} + +openParams = {'fileName':-1,'mode':-1} + +paramBuffer = {} # Names of the overlay files to load. # See l.550 -overlayFile0 = "Overlay.ovl0" - -overlayFile1 = "Overlay.ovl1" +overlayFiles = {'00':"Overlay.ovl0", '01':"Overlay.ovl1" } # Serial connection setup @@ -73,14 +81,8 @@ uniDebugMode = 0 Command = "" -memAddr = "" - -flagAddr = "" - loadFile = -1 -levelId = "" - # One byte #uno = int(1).to_bytes(1, byteorder='little', signed=False) @@ -91,6 +93,8 @@ data = 0 checkSum = 0 +checkSumLen = 9 # Corresponding value in pcdrv.h, l.54 + # If set, it means the data transfer has been initiated Transfer = 0 @@ -225,6 +229,48 @@ def CalculateChecksum( inBytes, skipFirstSector = False): return returnVal; +def getData(dataInBuffer): + + dataLen = 0 + + parsedData = "" + + # Get data length byte + + if len(dataInBuffer) == 2: + + parsedData = dataInBuffer + + dataInBuffer = "" + + return [dataInBuffer, parsedData] # does it break ? + + dataLen = dataInBuffer[:2] + + # We're receiving 8 chars to describe a 4 bytes pointer, hence the * 2 + + dataLen = int(dataLen) * 2 + + dataInBuffer = dataInBuffer[2:] + + # Get actual data + + for b in dataInBuffer : + + if len(parsedData) < dataLen : + + parsedData += b + + # Remove data from buffer + + dataInBuffer = dataInBuffer[dataLen:] + + if DEBUG: + + print( "Data in buffer 1: " + dataInBuffer ) + + return [dataInBuffer, parsedData] + def WriteBytes( inData ): if DEBUG: @@ -370,6 +416,8 @@ def WriteBytes( inData ): numChunk = 0 + return True + # END WHILE DATA def SendBin( inData, memAddr ): @@ -449,17 +497,13 @@ def SendBin( inData, memAddr ): # Send dat data - WriteBytes( inData ) + return WriteBytes( inData ) def resetListener(): - global checkSum, data, Listen, Transfer, dataSize, memAddr, loadFile, flagAddr, levelId + global checkSum, data, Listen, Transfer, dataSize, loadFile - memAddr = "" - - flagAddr = "" - - loadFile = "" + loadFile = -1 checkSum = 0 @@ -469,8 +513,6 @@ def resetListener(): Transfer = 0 - levelId = 0 - Listen = 1 ser.reset_input_buffer() @@ -481,7 +523,7 @@ def main(args): while True: - global checkSum, data, Listen, Transfer, dataSize, memAddr, loadFile, flagAddr, levelId + global checkSum, checkSumLen, data, Listen, Transfer, dataSize, loadFile, paramBuffer # Flush serial buffers to avoid residual data @@ -497,10 +539,6 @@ def main(args): print("Listening for incoming data...") - if DEBUG > 1: - - print("memAddr : " + str(memAddr) + " - loadFile" + str(loadFile )) - while True: # If data on serial, fill buffer @@ -512,12 +550,14 @@ def main(args): if DEBUG > 2: print( "Raw data : " + inputBuffer ) - + if inputBuffer: - # We're expecting the command with format : 00 01 06 08 xx xx xx xx 08 xx xx xx xx 01 xx xx (16 bytes) + print( "Incoming data : " + inputBuffer ) + + # We're expecting the command with format : 00 01 06 04 xx xx xx xx 04 xx xx xx xx 01 xx xx xx xx xx (38 Bytes) - if len(inputBuffer) == 38: + if len(inputBuffer) == 37: if DEBUG: @@ -525,7 +565,7 @@ def main(args): # Get the checksum and remove it from the buffer - CmdCheckSum = inputBuffer[-10:] + CmdCheckSum = inputBuffer[-checkSumLen:] if DEBUG: @@ -541,12 +581,6 @@ def main(args): print( "Computed ChkSm: " + str(dataCheckSum) ) - # Not using the checksum for now - - # ~ dataCheckSum = 0 - - # ~ CmdCheckSum = 0 - # Check if int(dataCheckSum) == int(CmdCheckSum): @@ -573,85 +607,63 @@ def main(args): inputBuffer = inputBuffer[2:] - if inputBuffer[:2] == pcdrvCommand: + if DEBUG: + + print( "Received Cmd: " + inputBuffer[:2] ) + + # Command bytes are LOAD == 06 + + if inputBuffer[:2] == LOAD: + + # Set corresponding parameters and mode - if DEBUG: + paramTmp = loadParams - print( "Received Cmd: " + inputBuffer[:2] ) + Command = LOAD + + # Command butes are OPEN == 00 + + elif inputBuffer[:2] == OPEN: - # Command byte is valid (06), remove it from buffer and continue + paramTmp = openParams + + Command = OPEN + + else: - inputBuffer = inputBuffer[2:] + print("Command not recognized : got " + inputBuffer[:2] ) - dataInBuffer = inputBuffer - - if DEBUG: - - print( "Data in buffer: " + dataInBuffer ) - - dataLen = 0 - - # Get data length byte - - dataLen = dataInBuffer[:2] - - dataInBuffer = dataInBuffer[2:] - - # Get actual data - - for b in dataInBuffer : - - if len(memAddr) < int(dataLen) : - - memAddr += b - - # Remove data from buffer - - dataInBuffer = dataInBuffer[int(dataLen):] - - if DEBUG > 1: - - print( "Data in buffer 1: " + dataInBuffer ) - - dataLen = 0 - - dataLen = dataInBuffer[:2] - - dataInBuffer = dataInBuffer[2:] - - # Get actual data - - for b in dataInBuffer : - - if len(flagAddr) < int(dataLen) : - - flagAddr += b - - # Remove data from buffer - - dataInBuffer = dataInBuffer[int(dataLen):] - - if DEBUG > 1: - - print( "Data in buffer 2: " + dataInBuffer ) - - # We should only have two bytes remaining - - if len(dataInBuffer) == 2: - - loadFile = int(dataInBuffer) - - if DEBUG: - - print( memAddr + " - " + flagAddr + " - " + str(loadFile) ) + break - ser.reset_input_buffer() - - inputBuffer = "" + # Command byte is valid , remove it from buffer and continue + + inputBuffer = inputBuffer[2:] + + dataInBuffer = inputBuffer + + # For each parameter, populate corresponding data + + for param in paramTmp: + + dataInBuffer, paramTmp[param] = getData(dataInBuffer) + + if DEBUG: + + for param in paramTmp: - Listen = 0 - - break + print(param + ":" + paramTmp[param] + " - ") + + # Commit parsed data to param buffer + + paramBuffer = paramTmp + + ser.reset_input_buffer() + + inputBuffer = "" + + Listen = 0 + + break else: print("Command checksum not matching ! Aborting...") @@ -670,98 +682,81 @@ def main(args): break - if memAddr and flagAddr: + if len(paramBuffer): - # Remove separator and ';1' at end of the string + # Check that no param is undefined ( != -1 ) - # ~ fileClean = loadFile.split(';')[0][1:] - fileID = loadFile - - print("Received addresses and file ID : " + memAddr + " - " + flagAddr + " - " + str(fileID)) - - # TODO : replace with a proper level naming scheme - # right now, we're receiving currently loaded file - # so we have to switch manually here. - - binFileName = "" - - if fileID == 0: - - binFileName = overlayFile1 - - levelId = 1 - - if fileID == 1: + for param in paramBuffer: - binFileName = overlayFile0 - - levelId = 0 - - if DEBUG: - - print( + if paramBuffer[param] == -1: + + print("Error : parameter " + param + " is undefined.") + + break + + if Command == LOAD: + + if DEBUG > 1: + + print("Received addresses and file ID : " + paramBuffer['memAddr'] + " - " + paramBuffer['flagAddr'] + " - " + paramBuffer['loadFile'] ) - "Load Data to : " + memAddr + "\n" + + binFileName = overlayFiles[ paramBuffer['loadFile'] ] + + if DEBUG: + + print( - "Reset flag at: " + flagAddr + "\n" + - - "FileID : " + str(loadFile) + "\n" + - - "Bin : " + binFileName + " - ID : " + str(levelId) + "Load Data to : " + paramBuffer['memAddr'] + "\n" + + + "Reset flag at: " + paramBuffer['flagAddr'] + "\n" + + + "LoadFile : " + paramBuffer['loadFile'] + "\n" + + + "Bin : " + binFileName + + ) - ) - - # Open file as binary if bin filename is defined - - if binFileName: + # Open file as binary if bin filename is defined + + # ~ if binFileName: binFile = open( dataFolder + binFileName, 'rb' ) data = binFile.read() - Transfer = 1 - - else: + print("Initializing data transfer...") - print(" No filename provided, doing nothing ") + if not uniDebugMode: + + # Set unirom to debugmode - sent : "DEBG" - received : "DEBGOKAY" + + setDEBG() + + # Send data + + if SendBin( data, paramBuffer['memAddr'] ): + + time.sleep( sleepTime ) + + # Send ACK + + SendBin( b'OKYA' , paramBuffer['flagAddr']) + + # Reset everything resetListener() - - # If Init was set, initialize transfer and send data - - if Transfer: - - print("Initializing data transfer...") - - if not uniDebugMode: - - # Set unirom to debugmode - sent : "DEBG" - received : "DEBGOKAY" - - setDEBG() - - # Send level data - - SendBin( data, memAddr ) - - # Set level changed flag - - if DEBUG: - print("Sending value " + str( levelId.to_bytes(1, byteorder='little', signed=False) ) + " to " + flagAddr ) + print("DONE!") + + # ~ else: + + # ~ print(" No filename provided, doing nothing. ") + + # ~ resetListener() - time.sleep( sleepTime ) - - # ~ SendBin( levelId.to_bytes(1, byteorder='little', signed=False) , flagAddr) - - time.sleep( sleepTime ) - - SendBin( b'OKYA' , flagAddr) - - # Reset everything - - resetListener() - - print("DONE!") + if Command == OPEN: + + print("Received OPEN. Not yet implemented !") return 0 diff --git a/pcdrv.c b/pcdrv.c index 228a737..c2e5d9f 100644 --- a/pcdrv.c +++ b/pcdrv.c @@ -1,5 +1,15 @@ #include "pcdrv.h" + +void wait(){ + + for(u_int wait = 0; wait < 100; wait++){ + + wait = wait; + + } +}; + static char sio_read(){ char c; @@ -111,8 +121,6 @@ u_short waitForSIODone( volatile u_char * bufferAddress){ if( strcmp(buffer, OKYA) == 0 ){ - FntPrint("WORX!\n\n"); - SIOdone = 1; break; @@ -123,7 +131,7 @@ u_short waitForSIODone( volatile u_char * bufferAddress){ // Avoid infinite loop - if ( i > 10000 ){ + if ( i > 1000 ){ break; @@ -131,6 +139,8 @@ u_short waitForSIODone( volatile u_char * bufferAddress){ } + for (short i; i < 4;i++){ bufferAddress[i] = 0; } + return SIOdone; }; @@ -138,66 +148,63 @@ u_short waitForSIODone( volatile u_char * bufferAddress){ u_short PCload( u_long * loadAddress, volatile u_char * bufferAddress, u_char * overlayFileID ) { // Send filename , load address, and flag address - // E.G : 00 01 06 04 8001000 04 80010001 01 + 0000000000 <- cmd checksum - + // Returns 1 if ok, 0 else + // E.G : 00 01 06 04 80010000 08 80010001 01 + 0000000000 <- cmd checksum + // 00 01 06 08 8003edf8 08 8001f0f0 00 2439964735 -> 38 Bytes + char commandBuffer[28]; - sprintf(commandBuffer, "%02u%02u%02u08%08x08%08x%02u", ESCAPE, PROTOCOL, LOAD, loadAddress, bufferAddress, *overlayFileID); + sprintf(commandBuffer, "%02u%02u%02u%02u%08x%02u%08x%02u", ESCAPE, PROTOCOL, LOAD, sizeof(loadAddress), loadAddress,sizeof(bufferAddress), bufferAddress, *overlayFileID); u_int cmdChecksum = djbHash(commandBuffer, 28); - printf("%s%10u", commandBuffer, cmdChecksum); + printf("%s%*u", commandBuffer, CHECKSUM_LEN, cmdChecksum); // Need delay ? - - for(u_int wait = 0; wait < 100; wait++){ - - wait = wait; - - } - - if( waitForSIODone(bufferAddress) ) { - - *overlayFileID = !*overlayFileID; - - }; + wait(); + + return waitForSIODone(bufferAddress); }; -int PCopen( const char * filename, u_char mode ){ +int PCopen( const char * filename, u_char mode, volatile u_char * bufferAddress ){ - // Open filename in mode - // Returns file descriptor or -1 if fail - // E.G : 00 01 00 04 48454C4F 00 + 0000000000 <- cmd checksum + // Open filename in mode + // Returns file descriptor or -1 if fail + // E.G : 00 01 00 04 48454C4F 00 + 0000000000 <- cmd checksum 18 + CHECKSUM_LEN B - u_int bufferLen = 10 + strLen( filename ); - - char commandBuffer[ bufferLen ]; + u_int bufferLen = 10 + strlen( filename ); - sprintf(commandBuffer, "%02u%02u%02u%02u%s%02u", ESCAPE, PROTOCOL, OPEN, bufferLen, filename, mode); + char commandBuffer[ bufferLen ]; - u_int cmdChecksum = djbHash( commandBuffer, bufferLen); - - printf("%s%10u", commandBuffer, cmdChecksum); + sprintf(commandBuffer, "%02u%02u%02u%02u%*s%02u", ESCAPE, PROTOCOL, OPEN, strlen( filename ), strlen( filename ), filename, mode); + u_int cmdChecksum = djbHash( commandBuffer, bufferLen); + + printf("%s%*u", commandBuffer, CHECKSUM_LEN, cmdChecksum); + + wait(); + + return waitForSIODone(bufferAddress); } // WIP : Build command for use with putchar instead of printf void BuildCmd(){ - //~ // Build command in the buffer + + // Build command in the buffer - u_char escape = 0; // Hypothetical Escape char for unirom + u_char escape = 0x00; // Hypothetical Escape char for unirom - u_char protocol = 1; // Hypothetical protocol indicator for unirom + u_char protocol = 0x01; // Hypothetical protocol indicator for unirom - //~ // Command is 14 B data + 5 B checksum + // Command is 18 B data + 10 B checksum - u_char commandBuffer[19] = {0}; + char commandBuffer[28] = {0}; - u_char checkSumLen = 5; + u_char checkSumLen = 10; short i = 0; @@ -207,7 +214,6 @@ void BuildCmd(){ // FntPrint("%x\n", loadAddress); - while( i < sizeof( commandBuffer) - checkSumLen ){ if( i == 0 ){ diff --git a/pcdrv.h b/pcdrv.h index 567e74a..9b2d9f0 100644 --- a/pcdrv.h +++ b/pcdrv.h @@ -28,28 +28,30 @@ #include #include #include -#include +//~ #include #include -#define ESCAPE 00 // Hypothetical Escape char for unirom +#define ESCAPE 0x00 // Hypothetical Escape char for unirom -#define PROTOCOL 01 +#define PROTOCOL 0x01 //pcdrv commands -#define OPEN 00 -#define CLOSE 01 -#define SEEK 02 -#define READ 03 -#define WRITE 04 -#define CREATE 05 -#define LOAD 06 +#define OPEN 0x00 +#define CLOSE 0x01 +#define SEEK 0x02 +#define READ 0x03 +#define WRITE 0x04 +#define CREATE 0x05 +#define LOAD 0x06 // flags parameters -#define O_RDONLY 0 -#define O_WRONLY 1 -#define O_RDWR 2 +#define O_RDONLY 0x00 +#define O_WRONLY 0x01 +#define O_RDWR 0x02 + +#define CHECKSUM_LEN 9 static char sio_read(); @@ -69,7 +71,7 @@ void sendRU32(uint32_t data); u_short PCload( u_long * loadAddress, volatile u_char * bufferAddress, u_char * overlayFileID ); -int PCopen( const char * filename, int mode ); +int PCopen( const char * filename, u_char mode, volatile u_char * bufferAddress ); int PCcreate(const char * filename, int attributes );