diff --git a/bin/overlayhelper.exe b/bin/overlayhelper.exe new file mode 100644 index 0000000..7770bb0 Binary files /dev/null and b/bin/overlayhelper.exe differ diff --git a/bin/overlayhelper/source/bctypes.h b/bin/overlayhelper/source/bctypes.h new file mode 100644 index 0000000..d2b2a5f --- /dev/null +++ b/bin/overlayhelper/source/bctypes.h @@ -0,0 +1,48 @@ +/* + bctypes.h + + Because I need Standard Types +*/ + +#ifndef _bctypes_h +#define _bctypes_h + +typedef signed char i8; +typedef unsigned char u8; +typedef signed short i16; +typedef unsigned short u16; +typedef signed long i32; +typedef unsigned long u32; + +typedef signed long long i64; +typedef unsigned long long u64; + +// If we're using C, I still like having a bool around +#ifndef __cplusplus +typedef i32 bool; +#define false (0) +#define true (!false) +#define nullptr 0 +#endif + +typedef float f32; +typedef float r32; +typedef double f64; +typedef double r64; + + +#define null (0) + +// Odd Types +typedef union { +// u128 ul128; + u64 ul64[2]; + u32 ui32[4]; +} QWdata; + + +#endif // _bctypes_h + +// EOF - bctypes.h + + diff --git a/bin/overlayhelper/source/cfile.cpp b/bin/overlayhelper/source/cfile.cpp new file mode 100644 index 0000000..78cde94 --- /dev/null +++ b/bin/overlayhelper/source/cfile.cpp @@ -0,0 +1,208 @@ +// +// cfile.cpp +// +#include "cfile.h" +#include + +//------------------------------------------------------------------------------ +// Static Helpers + +static bool contains(char x, const char* pSeparators) +{ + while (*pSeparators) + { + if (x == *pSeparators) + { + return true; + } + pSeparators++; + } + + return false; +} + +static std::vector split(const std::string& s, const char* separators) +{ + std::vector output; + std::string::size_type prev_pos = 0, pos = 0; + + for (int index = 0; index < s.length(); ++index) + { + pos = index; + + // if the index is a separator + if (contains(s[index], separators)) + { + // if we've skipped a token, collect it + if (prev_pos != index) + { + output.push_back(s.substr(prev_pos, index-prev_pos)); + + // skip white space here + while (index < s.length()) + { + if (contains(s[index], separators)) + { + ++index; + } + else + { + prev_pos = index; + pos = index; + break; + } + } + } + else + { + prev_pos++; + } + } + } + + output.push_back(s.substr(prev_pos, pos-prev_pos+1)); // Last word + + return output; +} + +// make this string lowercase +static void tolower(std::string& s) +{ + for (int index = 0; index < s.length(); ++index) + { + s[index] = tolower( s[index] ); + } +} + +static std::string next_token(std::vector& tokens, MemoryStream& memStream) +{ + std::string result = ""; + + if (tokens.size() >= 1) + { + result = tokens[0]; + tokens.erase(tokens.cbegin()); + } + else + { + while (memStream.NumBytesAvailable()) + { + std::string lineData = memStream.ReadLine(); + tokens = split(lineData, "[](), \t"); + + if (tokens.size() >= 1) + { + return next_token(tokens, memStream); + } + } + + } + return result; +} + +//------------------------------------------------------------------------------ +CFile::CFile( std::string filepath ) + : m_filepath( filepath ) +{ + FILE* pFile = nullptr; + errno_t err = fopen_s(&pFile, filepath.c_str(), "rb"); + + if (0==err) + { + fseek(pFile, 0, SEEK_END); + size_t length = ftell(pFile); + fseek(pFile, 0, SEEK_SET); + + //printf("\n// CFile %s - %lld bytes\n", filepath.c_str(), length); + //printf("// Reading %s\n", filepath.c_str()); + //printf("//\n"); + //printf("// This file generated by overlayhelper.exe\n"); + //printf("//\n"); + //printf("//\n"); + + u8* pData = new u8[ length ]; + + fread(pData, sizeof(u8), length / sizeof(u8), pFile); + + // Now we have a buffer, with the whole C File + // Looking to parse 2 types of things + // CODE_BLOCK macros + // DATA_BLOCK macros + // INCBIN macros + { + MemoryStream memStream(pData, length); + + while (memStream.NumBytesAvailable()) + { + std::string lineData = memStream.ReadLine(); + + // symbols are case sensitive + + // we care about the first 32 bit value in hex, and the last value on the line, the symbol + std::vector tokens = split(lineData, "[](), \t"); + + if (tokens.size() >= 2) + { + std::string token = next_token(tokens, memStream); + + if (token == "INCBIN") + { + // section + token = next_token(tokens, memStream); + token = next_token(tokens, memStream); + + printf("incbin_%s_start_high = incbin_%s_start>>16;\n", token.c_str(),token.c_str()); + printf("incbin_%s_start_BLOCK = incbin_%s_start/0x2000;\n", token.c_str(),token.c_str()); + printf("incbin_%s_length = incbin_%s_end-incbin_%s_start;\n", token.c_str(),token.c_str(),token.c_str()); + printf("incbin_%s_length_high = incbin_%s_length>>16;\n", token.c_str(),token.c_str()); + + } + else if ( (token == "CODE_BLOCK") || + (token == "DATA_BLOCK") ) + { + // We aren't as good as the C Preprocessor, but we will allow this + // to span 1 line, because RickJr has weird syntax + + // First get the block number + token = next_token(tokens, memStream); + u32 block_num = strtoul(token.c_str(), nullptr, 0); + + // Next we need the function name + do + { + token = next_token(tokens, memStream); + + } while (token=="static" || token =="volatile"); + + // now token has return type + + token = next_token(tokens, memStream); + + // now token finally has our function or data name + printf("%s_BLOCK = %d;\n", token.c_str(), block_num); // just need the block number for mapper + } + } + } + } + + delete[] pData; + + fclose(pFile); + + //printf("\n// Read Completed\n"); + } + else + { + printf("// CFile could not open %s\n", filepath.c_str()); + printf("//\n"); + } +} + +//------------------------------------------------------------------------------ + +CFile::~CFile() +{ +} + +//------------------------------------------------------------------------------ + diff --git a/bin/overlayhelper/source/cfile.h b/bin/overlayhelper/source/cfile.h new file mode 100644 index 0000000..d29cb19 --- /dev/null +++ b/bin/overlayhelper/source/cfile.h @@ -0,0 +1,27 @@ +// +// CFile Class +// + +#ifndef CFILE_H_ +#define CFILE_H_ + +#include +#include + +#include "bctypes.h" +#include "memstream.h" + +//------------------------------------------------------------------------------ + +class CFile +{ +public: + CFile( std::string filepath ); + ~CFile(); + +private: + std::string m_filepath; +}; + +#endif // CFILE_H_ + diff --git a/bin/overlayhelper/source/memstream.h b/bin/overlayhelper/source/memstream.h new file mode 100644 index 0000000..5adc146 --- /dev/null +++ b/bin/overlayhelper/source/memstream.h @@ -0,0 +1,164 @@ +// +// Jason's Memory Stream Helper thing +// Serialize a byte stream into real types +// +// Currently only support Little Endian +// + +#ifndef MEMSTREAM_H_ +#define MEMSTREAM_H_ + + +#include + +// Prototypes +void *memcpy(void *dest, const void *src, size_t n); + +class MemoryStream +{ +public: + MemoryStream(unsigned char* pStreamStart, size_t streamSize) + : m_pStreamStart( pStreamStart ) + , m_streamSize(streamSize) + , m_pStreamCurrent( pStreamStart ) + {} + + ~MemoryStream() + { + m_pStreamStart = nullptr; + m_streamSize = 0; + m_pStreamCurrent = nullptr; + } + + // Really Dumb Reader Template + template + T Read() + { + T result; + + memcpy(&result, m_pStreamCurrent, sizeof(T)); + m_pStreamCurrent += sizeof(T); + + return result; + } + + template + void Read(T& result) + { + memcpy(&result, m_pStreamCurrent, sizeof(result)); + m_pStreamCurrent += sizeof(result); + } + + void ReadBytes(u8* pDest, size_t numBytes) + { + memcpy(pDest, m_pStreamCurrent, numBytes); + m_pStreamCurrent += numBytes; + } + + std::string ReadPString() + { + std::string result; + + u8 length = *m_pStreamCurrent++; + + result.insert(0, (char*)m_pStreamCurrent, (size_t)length); + + m_pStreamCurrent += length; + + return result; + } + + std::string ReadLine() + { + unsigned char* pStreamEnd = m_pStreamStart + m_streamSize; + + std::string result; + + u8 *pEndOfLine = m_pStreamCurrent; + + while (pEndOfLine < pStreamEnd) + { + if ((0x0a != *pEndOfLine) && (0x0d != *pEndOfLine)) + { + pEndOfLine++; + } + else + { + break; + } + } + + size_t length = pEndOfLine - m_pStreamCurrent; + + if (length) + { + // Capture the line + result.insert(0, (char*)m_pStreamCurrent, (size_t)length); + m_pStreamCurrent += length; + } + + // Figure out if that loop broke out because we hit the end of the file + while (pEndOfLine < pStreamEnd) + { + if ((0x0a == *pEndOfLine) || (0x0d == *pEndOfLine)) + { + pEndOfLine++; + } + else + { + break; + } + } + + m_pStreamCurrent = pEndOfLine; + if (m_pStreamCurrent > pStreamEnd) + { + m_pStreamCurrent = pStreamEnd; + } + return result; + + } + + size_t SeekCurrent(int delta) + { + m_pStreamCurrent += delta; + return m_pStreamCurrent - m_pStreamStart; + } + + size_t SeekSet(size_t offset) + { + m_pStreamCurrent = m_pStreamStart + offset; + return m_pStreamCurrent - m_pStreamStart; + } + + unsigned char* GetPointer() { return m_pStreamCurrent; } + + size_t NumBytesAvailable() { + + unsigned char* pStreamEnd = m_pStreamStart + m_streamSize; + + if (pStreamEnd - m_pStreamCurrent >= 0) + { + return pStreamEnd - m_pStreamCurrent; + } + else + { + return 0; + } + } + + + +private: + + unsigned char* m_pStreamStart; + size_t m_streamSize; + + unsigned char* m_pStreamCurrent; + +}; + + +#endif // MEMSTREAM_H_ + + diff --git a/bin/overlayhelper/source/overlayhelper.cpp b/bin/overlayhelper/source/overlayhelper.cpp new file mode 100644 index 0000000..cef6483 --- /dev/null +++ b/bin/overlayhelper/source/overlayhelper.cpp @@ -0,0 +1,70 @@ +// overlayhelper +// +// - generate linker script crap, so that we can resolve addresses larger +// than 32-bit with mos llvm (hacky work around for 16 bit address linker) + +#include +#include + +#include "cfile.h" + +//------------------------------------------------------------------------------ +void helpText() +{ + printf("overlayhelper - v1.0\n"); + printf("--------------------\n"); + printf("Convert macros found in C code, into mos-llvm .ld linker format\n"); + printf("\noverlayhelper [options] \n"); + printf(" -v verbose\n\n"); + + exit(-1); +} + +//------------------------------------------------------------------------------ + +int main(int argc, char* argv[]) +{ + char* pInfilePath = nullptr; + bool bVerbose = false; + + for (int idx = 1; idx < argc; ++idx ) + { + char* arg = argv[ idx ]; + + if ('-' == arg[0]) + { + // Parse as an option + if ('v'==arg[1]) + { + bVerbose = true; + } + } + else if (nullptr == pInfilePath) + { + // Assume the first non-option is an input file path + pInfilePath = argv[ idx ]; + } + else + { + // Oh Crap, we have a non-option, but we don't know what to do with + // it + printf("ERROR: Invalid option, Arg %d = %s\n\n", idx, argv[ idx ]); + helpText(); + } + } + + if (pInfilePath) + { + // Load the .c File + CFile *pCFile = new CFile(std::string(pInfilePath)); + delete pCFile; + } + else + { + helpText(); + } + + + return 0; +} + diff --git a/bin/overlayhelper/vcxproj/overlayhelper.sln b/bin/overlayhelper/vcxproj/overlayhelper.sln new file mode 100644 index 0000000..a2c004f --- /dev/null +++ b/bin/overlayhelper/vcxproj/overlayhelper.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34309.116 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "overlayhelper", "overlayhelper.vcxproj", "{77D5186F-C388-4740-8BA7-23997A659FB8}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {77D5186F-C388-4740-8BA7-23997A659FB8}.Debug|x64.ActiveCfg = Debug|x64 + {77D5186F-C388-4740-8BA7-23997A659FB8}.Debug|x64.Build.0 = Debug|x64 + {77D5186F-C388-4740-8BA7-23997A659FB8}.Debug|x86.ActiveCfg = Debug|Win32 + {77D5186F-C388-4740-8BA7-23997A659FB8}.Debug|x86.Build.0 = Debug|Win32 + {77D5186F-C388-4740-8BA7-23997A659FB8}.Release|x64.ActiveCfg = Release|x64 + {77D5186F-C388-4740-8BA7-23997A659FB8}.Release|x64.Build.0 = Release|x64 + {77D5186F-C388-4740-8BA7-23997A659FB8}.Release|x86.ActiveCfg = Release|Win32 + {77D5186F-C388-4740-8BA7-23997A659FB8}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {4AE0A6E5-5981-4BAE-96E2-DDD5656B0B2E} + EndGlobalSection +EndGlobal diff --git a/bin/overlayhelper/vcxproj/overlayhelper.vcxproj b/bin/overlayhelper/vcxproj/overlayhelper.vcxproj new file mode 100644 index 0000000..b8a9574 --- /dev/null +++ b/bin/overlayhelper/vcxproj/overlayhelper.vcxproj @@ -0,0 +1,147 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {77d5186f-c388-4740-8ba7-23997a659fb8} + overlayhelper + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + $(SolutionDir)\..\.. + + + $(SolutionDir)\..\.. + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/bin/overlayhelper/vcxproj/overlayhelper.vcxproj.filters b/bin/overlayhelper/vcxproj/overlayhelper.vcxproj.filters new file mode 100644 index 0000000..15e4d2b --- /dev/null +++ b/bin/overlayhelper/vcxproj/overlayhelper.vcxproj.filters @@ -0,0 +1,26 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + + + Source Files + + + Source Files + + + Source Files + + + + + Source Files + + + + \ No newline at end of file