diff --git a/libs/MetroTRK/build.py b/libs/MetroTRK/build.py index a6ccf070..bf40211e 100644 --- a/libs/MetroTRK/build.py +++ b/libs/MetroTRK/build.py @@ -44,11 +44,11 @@ def main(compile_non_matching, use_ninja, clean_ninja, link): msl_path = pathlib.Path("../MSL_C/include") - flags = "-c -nodefaults -nostdlib -proc gekko -align powerpc -enum int -fp hard -Cpp_exceptions off -sdata 0 -rtti off -DEPPC -DGEKKO -O4,p -inline auto -i . -I- -i include " + flags = "-c -nodefaults -nostdlib -proc gekko -common off -align powerpc -enum int -fp hard -Cpp_exceptions off -use_lmw_stmw on -sdata 0 -sdata2 8 -rostr -rtti off -DEPPC -DGEKKO -O4,p -inline auto -i . -I- -i include " includes = f"-i {msl_path} " flags += includes - default_compiler_path = pathlib.Path("../../Compilers/GC/2.6/") + default_compiler_path = pathlib.Path("../../Compilers/GC/2.5/") compiler_exceptions = { #"source\JSystem\JKernel\JKRHeap.cpp": pathlib.Path("GC/1.2.5/") diff --git a/libs/MetroTRK/csv/TRK_Hollywood_Revolution.csv b/libs/MetroTRK/csv/TRK_Hollywood_Revolution.csv index 660aa21f..6e75eb93 100644 --- a/libs/MetroTRK/csv/TRK_Hollywood_Revolution.csv +++ b/libs/MetroTRK/csv/TRK_Hollywood_Revolution.csv @@ -8,28 +8,28 @@ TRKInitializeEventQueue,nubevent.o,TRK_Hollywood_Revolution.a,true TRKNubWelcome,nubinit.o,TRK_Hollywood_Revolution.a,false TRKTerminateNub,nubinit.o,TRK_Hollywood_Revolution.a,false TRKInitializeNub,nubinit.o,TRK_Hollywood_Revolution.a,false -TRKMessageSend,msg.o,TRK_Hollywood_Revolution.a,false -TRKReadBuffer_ui32,msgbuf.o,TRK_Hollywood_Revolution.a,false -TRKReadBuffer_ui8,msgbuf.o,TRK_Hollywood_Revolution.a,false -TRKReadBuffer1_ui64,msgbuf.o,TRK_Hollywood_Revolution.a,false -TRKAppendBuffer_ui32,msgbuf.o,TRK_Hollywood_Revolution.a,false -TRKAppendBuffer_ui8,msgbuf.o,TRK_Hollywood_Revolution.a,false -TRKAppendBuffer1_ui64,msgbuf.o,TRK_Hollywood_Revolution.a,false -TRKReadBuffer,msgbuf.o,TRK_Hollywood_Revolution.a,false -TRKAppendBuffer,msgbuf.o,TRK_Hollywood_Revolution.a,false -TRKSetBufferPosition,msgbuf.o,TRK_Hollywood_Revolution.a,false -TRKResetBuffer,msgbuf.o,TRK_Hollywood_Revolution.a,false -TRKReleaseBuffer,msgbuf.o,TRK_Hollywood_Revolution.a,false -TRKGetBuffer,msgbuf.o,TRK_Hollywood_Revolution.a,false -TRKGetFreeBuffer,msgbuf.o,TRK_Hollywood_Revolution.a,false -TRKInitializeMessageBuffers,msgbuf.o,TRK_Hollywood_Revolution.a,false -TRKTerminateSerialHandler,serpoll.o,TRK_Hollywood_Revolution.a,false -TRKInitializeSerialHandler,serpoll.o,TRK_Hollywood_Revolution.a,false -TRKProcessInput,serpoll.o,TRK_Hollywood_Revolution.a,false -TRKGetInput,serpoll.o,TRK_Hollywood_Revolution.a,false +TRKMessageSend,msg.o,TRK_Hollywood_Revolution.a,true +TRKReadBuffer_ui32,msgbuf.o,TRK_Hollywood_Revolution.a,true +TRKReadBuffer_ui8,msgbuf.o,TRK_Hollywood_Revolution.a,true +TRKReadBuffer1_ui64,msgbuf.o,TRK_Hollywood_Revolution.a,true +TRKAppendBuffer_ui32,msgbuf.o,TRK_Hollywood_Revolution.a,true +TRKAppendBuffer_ui8,msgbuf.o,TRK_Hollywood_Revolution.a,true +TRKAppendBuffer1_ui64,msgbuf.o,TRK_Hollywood_Revolution.a,true +TRKReadBuffer,msgbuf.o,TRK_Hollywood_Revolution.a,true +TRKAppendBuffer,msgbuf.o,TRK_Hollywood_Revolution.a,true +TRKSetBufferPosition,msgbuf.o,TRK_Hollywood_Revolution.a,true +TRKResetBuffer,msgbuf.o,TRK_Hollywood_Revolution.a,true +TRKReleaseBuffer,msgbuf.o,TRK_Hollywood_Revolution.a,true +TRKGetBuffer,msgbuf.o,TRK_Hollywood_Revolution.a,true +TRKGetFreeBuffer,msgbuf.o,TRK_Hollywood_Revolution.a,true +TRKInitializeMessageBuffers,msgbuf.o,TRK_Hollywood_Revolution.a,true +TRKTerminateSerialHandler,serpoll.o,TRK_Hollywood_Revolution.a,true +TRKInitializeSerialHandler,serpoll.o,TRK_Hollywood_Revolution.a,true +TRKProcessInput,serpoll.o,TRK_Hollywood_Revolution.a,true +TRKGetInput,serpoll.o,TRK_Hollywood_Revolution.a,true TRKTestForPacket,serpoll.o,TRK_Hollywood_Revolution.a,false -usr_put_initialize,usr_put.o,TRK_Hollywood_Revolution.a,false -usr_puts_serial,usr_put.o,TRK_Hollywood_Revolution.a,false +usr_put_initialize,usr_put.o,TRK_Hollywood_Revolution.a,true +usr_puts_serial,usr_put.o,TRK_Hollywood_Revolution.a,true TRKDispatchMessage,dispatch.o,TRK_Hollywood_Revolution.a,false TRKInitializeDispatcher,dispatch.o,TRK_Hollywood_Revolution.a,false TRKDoSetOption,msghndlr.o,TRK_Hollywood_Revolution.a,false diff --git a/libs/MetroTRK/data/MetroTRK.json b/libs/MetroTRK/data/MetroTRK.json index 45555b7a..9148ef2f 100644 --- a/libs/MetroTRK/data/MetroTRK.json +++ b/libs/MetroTRK/data/MetroTRK.json @@ -1,6 +1,6 @@ { "schemaVersion": 1, "label": "MetroTRK", - "message": "2.584%", + "message": "13.854%", "color": "blue" } \ No newline at end of file diff --git a/libs/MetroTRK/docs/PROGRESS.md b/libs/MetroTRK/docs/PROGRESS.md index e931534b..144f9b74 100644 --- a/libs/MetroTRK/docs/PROGRESS.md +++ b/libs/MetroTRK/docs/PROGRESS.md @@ -1,3 +1,3 @@ | Library | Percentage | | ------------- | ------------- | -| [TRK_Hollywood_Revolution](https://github.com/shibbo/RVL_SDK/blob/main/docs/lib/TRK_Hollywood_Revolution.md) | 2.5843503230437905% | +| [TRK_Hollywood_Revolution](https://github.com/shibbo/RVL_SDK/blob/main/docs/lib/TRK_Hollywood_Revolution.md) | 13.854989231873654% | diff --git a/libs/MetroTRK/docs/lib/TRK_Hollywood_Revolution.md b/libs/MetroTRK/docs/lib/TRK_Hollywood_Revolution.md index b840686c..5ab9941b 100644 --- a/libs/MetroTRK/docs/lib/TRK_Hollywood_Revolution.md +++ b/libs/MetroTRK/docs/lib/TRK_Hollywood_Revolution.md @@ -11,10 +11,10 @@ | mainloop.o | 0.0% | 0 / 1 | 0.0% | :x: | nubevent.o | 100.0% | 5 / 5 | 100.0% | :white_check_mark: | nubinit.o | 0.0% | 0 / 3 | 0.0% | :x: -| msg.o | 0.0% | 0 / 1 | 0.0% | :x: -| msgbuf.o | 0.0% | 0 / 14 | 0.0% | :x: -| serpoll.o | 0.0% | 0 / 5 | 0.0% | :x: -| usr_put.o | 0.0% | 0 / 2 | 0.0% | :x: +| msg.o | 100.0% | 1 / 1 | 100.0% | :white_check_mark: +| msgbuf.o | 100.0% | 14 / 14 | 100.0% | :white_check_mark: +| serpoll.o | 51.4018691588785% | 4 / 5 | 80.0% | :eight_pointed_black_star: +| usr_put.o | 100.0% | 2 / 2 | 100.0% | :white_check_mark: | dispatch.o | 0.0% | 0 / 2 | 0.0% | :x: | msghndlr.o | 0.0% | 0 / 16 | 0.0% | :x: | support.o | 0.0% | 0 / 5 | 0.0% | :x: @@ -64,43 +64,43 @@ # msg.o | Symbol | Decompiled? | | ------------- | ------------- | -| TRKMessageSend | :x: | +| TRKMessageSend | :white_check_mark: | # msgbuf.o | Symbol | Decompiled? | | ------------- | ------------- | -| TRKReadBuffer_ui32 | :x: | -| TRKReadBuffer_ui8 | :x: | -| TRKReadBuffer1_ui64 | :x: | -| TRKAppendBuffer_ui32 | :x: | -| TRKAppendBuffer_ui8 | :x: | -| TRKAppendBuffer1_ui64 | :x: | -| TRKReadBuffer | :x: | -| TRKAppendBuffer | :x: | -| TRKSetBufferPosition | :x: | -| TRKResetBuffer | :x: | -| TRKReleaseBuffer | :x: | -| TRKGetBuffer | :x: | -| TRKGetFreeBuffer | :x: | -| TRKInitializeMessageBuffers | :x: | +| TRKReadBuffer_ui32 | :white_check_mark: | +| TRKReadBuffer_ui8 | :white_check_mark: | +| TRKReadBuffer1_ui64 | :white_check_mark: | +| TRKAppendBuffer_ui32 | :white_check_mark: | +| TRKAppendBuffer_ui8 | :white_check_mark: | +| TRKAppendBuffer1_ui64 | :white_check_mark: | +| TRKReadBuffer | :white_check_mark: | +| TRKAppendBuffer | :white_check_mark: | +| TRKSetBufferPosition | :white_check_mark: | +| TRKResetBuffer | :white_check_mark: | +| TRKReleaseBuffer | :white_check_mark: | +| TRKGetBuffer | :white_check_mark: | +| TRKGetFreeBuffer | :white_check_mark: | +| TRKInitializeMessageBuffers | :white_check_mark: | # serpoll.o | Symbol | Decompiled? | | ------------- | ------------- | -| TRKTerminateSerialHandler | :x: | -| TRKInitializeSerialHandler | :x: | -| TRKProcessInput | :x: | -| TRKGetInput | :x: | +| TRKTerminateSerialHandler | :white_check_mark: | +| TRKInitializeSerialHandler | :white_check_mark: | +| TRKProcessInput | :white_check_mark: | +| TRKGetInput | :white_check_mark: | | TRKTestForPacket | :x: | # usr_put.o | Symbol | Decompiled? | | ------------- | ------------- | -| usr_put_initialize | :x: | -| usr_puts_serial | :x: | +| usr_put_initialize | :white_check_mark: | +| usr_puts_serial | :white_check_mark: | # dispatch.o diff --git a/libs/MetroTRK/include/TRK_Types.h b/libs/MetroTRK/include/TRK_Types.h index ea3efa34..405492f3 100644 --- a/libs/MetroTRK/include/TRK_Types.h +++ b/libs/MetroTRK/include/TRK_Types.h @@ -32,4 +32,9 @@ typedef struct TRKBuffer { typedef int bool; +typedef unsigned char ui8; +typedef unsigned short ui16; +typedef unsigned long ui32; +typedef unsigned long long ui64; + #endif // TRK_TYPES_H \ No newline at end of file diff --git a/libs/MetroTRK/include/export/UART.h b/libs/MetroTRK/include/export/UART.h new file mode 100644 index 00000000..d88caaa1 --- /dev/null +++ b/libs/MetroTRK/include/export/UART.h @@ -0,0 +1,8 @@ +#ifndef UART_H +#define UART_H + +int TRKPollUART(void); +int TRKReadUARTN(void *, unsigned long); +int TRKWriteUARTN(const void *, unsigned long); + +#endif // UART_H \ No newline at end of file diff --git a/libs/MetroTRK/include/portable/mem_TRK.h b/libs/MetroTRK/include/portable/mem_TRK.h index 38222c18..bcf44a30 100644 --- a/libs/MetroTRK/include/portable/mem_TRK.h +++ b/libs/MetroTRK/include/portable/mem_TRK.h @@ -3,6 +3,7 @@ #include +void* TRK_memset(void *, int, size_t); void* TRK_memcpy(void *, const void *, size_t); #endif // MEM_TRK_H \ No newline at end of file diff --git a/libs/MetroTRK/include/portable/msgbuf.h b/libs/MetroTRK/include/portable/msgbuf.h index ecc59ad7..254fbacb 100644 --- a/libs/MetroTRK/include/portable/msgbuf.h +++ b/libs/MetroTRK/include/portable/msgbuf.h @@ -2,6 +2,7 @@ #define MSGBUF_H #include "portable/mutex_TRK.h" +#include typedef int MessageBufferID; @@ -10,9 +11,14 @@ typedef struct MessageBuffer { bool fInUse; unsigned int fLength; unsigned int fPosition; - unsigned char fData[0x800]; + unsigned char fData[0x880]; } MessageBuffer; +int TRKReadBuffer(MessageBuffer *, void *, size_t); void TRKReleaseBuffer(MessageBufferID); +int TRKGetFreeBuffer(MessageBufferID *, MessageBuffer **); +int TRKSetBufferPosition(MessageBuffer *, unsigned int); +MessageBuffer* TRKGetBuffer(MessageBufferID); +int TRKAppendBuffer_ui8(MessageBuffer *, const ui8 *, int); #endif // MSGBUF_H \ No newline at end of file diff --git a/libs/MetroTRK/include/portable/nubevent.h b/libs/MetroTRK/include/portable/nubevent.h index da989234..073007c5 100644 --- a/libs/MetroTRK/include/portable/nubevent.h +++ b/libs/MetroTRK/include/portable/nubevent.h @@ -20,4 +20,8 @@ typedef struct NubEvent { MessageBufferID fMessageBufferID; } NubEvent; +void TRKConstructEvent(NubEvent *, NubEventType); +bool TRKGetNextEvent(NubEvent *); +int TRKPostEvent(const NubEvent *); + #endif // NUBEVENT_H \ No newline at end of file diff --git a/libs/MetroTRK/include/portable/nubinit.h b/libs/MetroTRK/include/portable/nubinit.h new file mode 100644 index 00000000..fb086365 --- /dev/null +++ b/libs/MetroTRK/include/portable/nubinit.h @@ -0,0 +1,8 @@ +#ifndef NUBINIT_H +#define NUBINIT_H + +#include "TRK_Types.h" + +extern bool gTRKBigEndian; + +#endif // NUBINIT_H \ No newline at end of file diff --git a/libs/MetroTRK/include/portable/serframe.h b/libs/MetroTRK/include/portable/serframe.h new file mode 100644 index 00000000..fd3fcf30 --- /dev/null +++ b/libs/MetroTRK/include/portable/serframe.h @@ -0,0 +1,6 @@ +#ifndef SERFRAME_H +#define SERFRAME_H + +typedef unsigned char FCSType; + +#endif // SERFRAME_H \ No newline at end of file diff --git a/libs/MetroTRK/include/portable/serpoll.h b/libs/MetroTRK/include/portable/serpoll.h new file mode 100644 index 00000000..ca7e628d --- /dev/null +++ b/libs/MetroTRK/include/portable/serpoll.h @@ -0,0 +1,9 @@ +#ifndef SERPOLL_H +#define SERPOLL_H + +#include "portable/msgbuf.h" +#include "portable/msghndlr.h" + +MessageBufferID TRKTestForPacket(); + +#endif // SERPOLL_H \ No newline at end of file diff --git a/libs/MetroTRK/include/portable/usr_put.h b/libs/MetroTRK/include/portable/usr_put.h new file mode 100644 index 00000000..cee97c9a --- /dev/null +++ b/libs/MetroTRK/include/portable/usr_put.h @@ -0,0 +1,6 @@ +#ifndef USR_PUT_H +#define USR_PUT_H + +int usr_puts_serial(const char *); + +#endif // USR_PUT_H \ No newline at end of file diff --git a/libs/MetroTRK/source/portable/Os/dolphin/usr_put.c b/libs/MetroTRK/source/portable/Os/dolphin/usr_put.c new file mode 100644 index 00000000..0b8477be --- /dev/null +++ b/libs/MetroTRK/source/portable/Os/dolphin/usr_put.c @@ -0,0 +1,27 @@ +#include "portable/msghndlr.h" + +extern void OSReport(const char *); + +void usr_put_initialize() { + +} + +int usr_puts_serial(const char *s) { + int result = 0; + char c; + char buf[2]; + + while ((result == 0) && (c = *s++) != 0) { + int conn = GetTRKConnected(); + + buf[0] = c; + buf[1] = '\0'; + + SetTRKConnected(0); + OSReport(buf); + SetTRKConnected(conn); + result = 0; + } + + return result; +} \ No newline at end of file diff --git a/libs/MetroTRK/source/portable/msg.c b/libs/MetroTRK/source/portable/msg.c new file mode 100644 index 00000000..a11abadc --- /dev/null +++ b/libs/MetroTRK/source/portable/msg.c @@ -0,0 +1,7 @@ +#include "export/UART.h" +#include "portable/msgbuf.h" + +int TRKMessageSend(MessageBuffer *buffer) { + TRKWriteUARTN(buffer->fData, buffer->fLength); + return 0; +} \ No newline at end of file diff --git a/libs/MetroTRK/source/portable/msgbuf.c b/libs/MetroTRK/source/portable/msgbuf.c new file mode 100644 index 00000000..f076764a --- /dev/null +++ b/libs/MetroTRK/source/portable/msgbuf.c @@ -0,0 +1,324 @@ +#include "portable/msgbuf.h" +#include "portable/mem_TRK.h" +#include "portable/usr_put.h" +#include "portable/nubinit.h" + +typedef struct MessageBuffers { + MessageBuffer fBuffers[3]; +} MessageBuffers; + +MessageBuffers gTRKMsgBufs; + +int TRKReadBuffer(MessageBuffer *buffer, void *data, size_t length) { + int err = 0; + unsigned int remaining; + + if (length == 0) { + return err; + } + + remaining = buffer->fLength - buffer->fPosition; + + if (length > remaining) { + err = 770; + length = remaining; + } + + TRK_memcpy(data, buffer->fData + buffer->fPosition, length); + buffer->fPosition += length; + return err; +} + +int TRKReadBuffer1_ui32(MessageBuffer* buffer, ui32 *data) { + int err; + + ui8* bigEndianData; + ui8* byteData; + ui8 swapBuffer[sizeof(data)]; + + if (gTRKBigEndian) { + bigEndianData = (ui8*)data; + } + else { + bigEndianData = swapBuffer; + } + + err = TRKReadBuffer(buffer, (void*)bigEndianData, sizeof(*data)); + + if (!gTRKBigEndian && err == 0) { + byteData = (ui8*)data; + + byteData[0] = bigEndianData[3]; + byteData[1] = bigEndianData[2]; + byteData[2] = bigEndianData[1]; + byteData[3] = bigEndianData[0]; + } + + return err; +} + +int TRKReadBuffer_ui32(MessageBuffer *buffer, ui32* data, int count) { + int err, i; + + for (i = 0, err = 0; err == 0 && i < count; i++) { + err = TRKReadBuffer1_ui32(buffer, &(data[i])); + } + + return err; +} + +int TRKReadBuffer1_ui8(MessageBuffer *buffer, ui8 *data) { + return TRKReadBuffer(buffer, (void*)data, 1); +} + +int TRKReadBuffer_ui8(MessageBuffer *buffer, ui8* data, int count) { + int err, i; + + for (i = 0, err = 0; err == 0 && i < count; i++) { + err = TRKReadBuffer1_ui8(buffer, &(data[i])); + } + + return err; +} + +int TRKReadBuffer1_ui64(MessageBuffer *buffer, ui64* data) { + int err; + + ui8* bigEndianData; + ui8* byteData; + ui8 swapBuffer[sizeof(data)]; + + if (gTRKBigEndian) { + bigEndianData = (ui8*)data; + } + else { + bigEndianData = swapBuffer; + } + + err = TRKReadBuffer(buffer, (void*)bigEndianData, sizeof(*data)); + + if (!gTRKBigEndian && err == 0) { + byteData = (ui8*)data; + + byteData[0] = bigEndianData[7]; + byteData[1] = bigEndianData[6]; + byteData[2] = bigEndianData[5]; + byteData[3] = bigEndianData[4]; + byteData[4] = bigEndianData[3]; + byteData[5] = bigEndianData[2]; + byteData[6] = bigEndianData[1]; + byteData[7] = bigEndianData[0]; + } + + return err; +} + +int TRKAppendBuffer(MessageBuffer *buffer, const void *data, size_t length) { + int err = 0; + unsigned int remaining; + + if (length == 0) { + return err; + } + + remaining = 0x880 - buffer->fPosition; + + if (remaining < length) { + err = 769; + length = remaining; + } + + if (length == 1) { + buffer->fData[buffer->fPosition] = *(unsigned char *)data; + } + else { + TRK_memcpy(buffer->fData + buffer->fPosition, data, length); + } + + buffer->fPosition += length; + buffer->fLength = buffer->fPosition; + return err; +} + +int TRKAppendBuffer1_ui32(MessageBuffer *buffer, const ui32 data) { + ui8* bigEndianData; + ui8* byteData; + ui8 swapBuffer[sizeof(data)]; + + if (gTRKBigEndian) { + bigEndianData = (ui8*)&data; + } + else { + byteData = (ui8*)&data; + bigEndianData = swapBuffer; + + bigEndianData[0] = byteData[3]; + bigEndianData[1] = byteData[2]; + bigEndianData[2] = byteData[1]; + bigEndianData[3] = byteData[0]; + } + + return TRKAppendBuffer(buffer, (const void*)bigEndianData, sizeof(data)); +} + +int TRKAppendBuffer_ui32(MessageBuffer *buffer, const ui32* data, int count) { + int err, i; + + for (i = 0, err = 0; err == 0 && i < count; i++) { + err = TRKAppendBuffer1_ui32(buffer, data[i]); + } + + return err; +} + +int TRKAppendBuffer1_ui8(MessageBuffer *buffer, const ui8 data) { + if (buffer->fPosition >= 0x880) { + return 769; + } + + buffer->fData[buffer->fPosition++] = data; + buffer->fLength++; + return 0; +} + +int TRKAppendBuffer_ui8(MessageBuffer *buffer, const ui8 *data, int count) { + int err, i; + + for (i = 0, err = 0; err == 0 && i < count; i++) { + err = TRKAppendBuffer1_ui8(buffer, data[i]); + } + + return err; +} + +int TRKAppendBuffer1_ui64(MessageBuffer *buffer, const ui64 data) { + ui8* bigEndianData; + ui8* byteData; + ui8 swapBuffer[sizeof(data)]; + + if (gTRKBigEndian) { + bigEndianData = (ui8*)&data; + } + else { + byteData = (ui8*)&data; + bigEndianData = swapBuffer; + + bigEndianData[0] = byteData[7]; + bigEndianData[1] = byteData[6]; + bigEndianData[2] = byteData[5]; + bigEndianData[3] = byteData[4]; + bigEndianData[4] = byteData[3]; + bigEndianData[5] = byteData[2]; + bigEndianData[6] = byteData[1]; + bigEndianData[7] = byteData[0]; + } + + return TRKAppendBuffer(buffer, (const void*)bigEndianData, sizeof(data)); +} + +int TRKAppendBuffer_ui64(MessageBuffer *buffer, const ui64 *data, int count) { + int err, i; + + for (i = 0, err = 0; err == 0 && i < count; i++) { + err = TRKAppendBuffer1_ui64(buffer, data[i]); + } + + return err; +} + +int TRKSetBufferPosition(MessageBuffer *buffer, unsigned int position) { + int err = 0; + + if (position > 0x880) { + err = 769; + } + else { + buffer->fPosition = position; + + if (position > buffer->fLength) { + buffer->fLength = position; + } + } + + return err; +} + +void TRKResetBuffer(MessageBuffer *buffer, int option) { + buffer->fLength = 0; + buffer->fPosition = 0; + + if (option == 0) { + TRK_memset(buffer->fData, 0, 0x880); + } +} + +void TRKReleaseBuffer(MessageBufferID bufferID) { + MessageBuffer* b; + + if (bufferID == -1) { + return; + } + + if (bufferID >= 0 && bufferID < 3) { + b = &gTRKMsgBufs.fBuffers[bufferID]; + TRKAcquireMutex(&b->fMutex); + b->fInUse = 0; + TRKReleaseMutex(&b->fMutex); + } +} + +void TRKSetBufferUsed(MessageBuffer *buffer, bool flag) { + buffer->fInUse = flag; +} + +MessageBuffer* TRKGetBuffer(MessageBufferID bufferID) { + MessageBuffer* result = 0; + + if (bufferID >= 0 && bufferID < 3) { + result = &gTRKMsgBufs.fBuffers[bufferID]; + } + + return result; +} + +int TRKGetFreeBuffer(MessageBufferID *resultBufferID, MessageBuffer ** resultBuffer) { + int result = 768; + MessageBufferID i; + MessageBuffer* b; + *resultBuffer = 0; + + for (i = 0 ; i < 3; i++) { + b = TRKGetBuffer(i); + TRKAcquireMutex(&b->fMutex); + + if (b->fInUse == 0) { + TRKResetBuffer(b, 1); + TRKSetBufferUsed(b, 1); + *resultBuffer = b; + *resultBufferID = i; + result = 0; + i = 3; + } + + TRKReleaseMutex(&b->fMutex); + } + + if (result == 0x300) { + usr_puts_serial("ERROR : No buffer available\n"); + } + + return result; +} + +int TRKInitializeMessageBuffers(void) { + int i; + + for (i = 0; i < 3; i++) { + TRKInitializeMutex(&gTRKMsgBufs.fBuffers[i].fMutex); + TRKAcquireMutex(&gTRKMsgBufs.fBuffers[i].fMutex); + TRKSetBufferUsed(&gTRKMsgBufs.fBuffers[i], 0); + TRKReleaseMutex(&gTRKMsgBufs.fBuffers[i].fMutex); + } + + return 0; +} \ No newline at end of file diff --git a/libs/MetroTRK/source/portable/nubinit.c b/libs/MetroTRK/source/portable/nubinit.c new file mode 100644 index 00000000..a771d48e --- /dev/null +++ b/libs/MetroTRK/source/portable/nubinit.c @@ -0,0 +1,3 @@ +#include "portable/nubinit.h" + +bool gTRKBigEndian; \ No newline at end of file diff --git a/libs/MetroTRK/source/portable/serpoll.c b/libs/MetroTRK/source/portable/serpoll.c new file mode 100644 index 00000000..030fc565 --- /dev/null +++ b/libs/MetroTRK/source/portable/serpoll.c @@ -0,0 +1,77 @@ +#include "portable/msgbuf.h" +#include "portable/msghndlr.h" +#include "portable/nubevent.h" +#include "portable/serframe.h" +#include "export/UART.h" + +typedef enum ReceiverState { + kWaitFlag, + kFoundFlag, + kInFrame, + kFrameOverflow +} ReceiverState; + +typedef struct FramingState { + MessageBufferID fBufferID; + MessageBuffer* fBuffer; + ReceiverState fReceiveState; + bool fEscape; + FCSType fFCS; +} FramingState; + +static FramingState gTRKFramingState; + +int TRKInitializeSerialHandler(void) { + gTRKFramingState.fBufferID = -1; + gTRKFramingState.fReceiveState = 0; + gTRKFramingState.fEscape = 0; + return 0; +} + +int TRKTerminateSerialHandler(void) { + return 0; +} + +void TRKProcessInput(MessageBufferID bufferID) { + NubEvent event; + TRKConstructEvent(&event, kRequestEvent); + event.fMessageBufferID = bufferID; + gTRKFramingState.fBufferID = -1; + TRKPostEvent(&event); +} + +void TRKGetInput(void) { + MessageBuffer* msgBuffer; + MessageBufferID bufferID; + NubEvent event; + + bufferID = TRKTestForPacket(); + + if (bufferID != -1) { + msgBuffer = TRKGetBuffer(bufferID); + TRKConstructEvent(&event, 2); + event.fMessageBufferID = bufferID; + gTRKFramingState.fBufferID = -1; + TRKPostEvent(&event); + } +} + +MessageBufferID TRKTestForPacket(void) { + int dbgErr, err; + MessageBufferID id; + MessageBuffer* b; + int bytes, i; + + bytes = TRKPollUART(); + + if (bytes > 0) { + dbgErr = TRKGetFreeBuffer(&id, &b); + TRKSetBufferPosition(b, 0); + + err = TRKReadUARTN(b->fData, 0x40); + + if (err == 0) { + TRKAppendBuffer_ui8(b, b->fData, 0x40); + } + } +} \ No newline at end of file