diff --git a/build-distro.sh b/build-distro.sh index 538345d..523ca1a 100755 --- a/build-distro.sh +++ b/build-distro.sh @@ -97,8 +97,12 @@ pushd distro if [[ -f ../f256dev.7z ]]; then rm ../f256dev.7z fi - 7z a ../f256dev.7z . + if [[ -f ../f256dev.tar.xz ]]; then + rm ../f256dev.tar.xz + fi + tar cjf ../f256dev.tar.xz . + popd popd diff --git a/distro/f256-install.bat b/distro/f256-install.bat index 182a10e..6ccd174 100644 --- a/distro/f256-install.bat +++ b/distro/f256-install.bat @@ -41,8 +41,6 @@ set REMOTE4=https://github.com/pweingar/FoenixMgr/archive/refs/heads/master.zip set LOCAL4=master.zip -if exist f256dev\ rd /s /q f256dev - if exist "%ProgramFiles%\7-Zip\7z.exe" ( set UNPACK="%ProgramFiles%\7-Zip\7z.exe" ) else ( @@ -86,6 +84,7 @@ if errorlevel 1 ( %LOCAL3% /install /passive +if exist f256dev\ rd /s /q f256dev md f256dev pushd f256dev %UNPACK% x -y ..\%LOCAL1% @@ -116,8 +115,8 @@ pushd f256dev echo @"%%~dp0mos-clang.exe" --config mos-f256.cfg %%* > llvm-mos\bin\mos-f256-clang.bat popd -echo @"%~dp0f256dev\f256build.bat" "%%~dp0" %%* > f256build.bat -echo @"%~dp0f256dev\f256run.bat" "%%~dp0" %%* > f256run.bat +if not exist f256build.bat echo @"%~dp0f256dev\f256build.bat" "%%~dp0" %%* > f256build.bat +if not exist f256run.bat echo @"%~dp0f256dev\f256run.bat" "%%~dp0" %%* > f256run.bat if not exist foenixmgr.ini ( echo [DEFAULT] > foenixmgr.ini diff --git a/distro/f256-install.sh b/distro/f256-install.sh index 6bd98ea..0648553 100755 --- a/distro/f256-install.sh +++ b/distro/f256-install.sh @@ -43,18 +43,13 @@ else LOCAL1=llvm-mos-macos.tar.xz fi -REMOTE2=https://kangaroopunch.com/files/serve/shared/llvm-mos-f256.tar.xz +REMOTE2=https://kangaroopunch.com/files/serve/shared/f256dev.tar.xz LOCAL2=llvm-mos-f256.tar.xz REMOTE3=https://github.com/pweingar/FoenixMgr/archive/refs/heads/master.zip LOCAL3=master.zip -if [[ -d $(basename -s .tar.xz ${LOCAL1}) ]]; then - echo "You already have an llvm-mos directory. Exiting." - exit 1 -fi - PYTHON=$(which python) if [[ $? == 1 ]]; then PYTHON=$(which python3) @@ -96,42 +91,66 @@ else fi fi -tar xf ${LOCAL1} +if [[ -z ${MAC} ]]; then + xattr -d com.apple.quarantine ${LOCAL1} + xattr -d com.apple.quarantine ${LOCAL2} + xattr -d com.apple.quarantine ${LOCAL3} +fi + +if [[ -d f256dev ]]; then + rm -rf f256dev +fi +mkdir -p f256dev +pushd f256dev + tar xf ${LOCAL1} + unzip -o ${LOCAL3} +popd + tar xf ${LOCAL2} -unzip -o ${LOCAL3} rm ${LOCAL1} rm ${LOCAL2} rm ${LOCAL3} -rm -f overlay.windows -if [[ -z ${MAC} ]]; then - rm -f overlay.macos - mv overlay.linux overlay -else - rm -f overlay.linux - mv overlay.macos overlay +pushd f256dev + rm -f overlay.windows + if [[ -z ${MAC} ]]; then + rm -f overlay.macos + mv overlay.linux overlay + else + rm -f overlay.linux + mv overlay.macos overlay + fi + + rm -f *.bat + + pushd llvm-mos/bin + ln -s mos-clang++ mos-f256-clang++ + ln -s mos-clang-cpp mos-f256-clang-cpp + ln -s mos-clang mos-f256-clang + popd + + mv FoenixMgr-master FoenixMgr + pushd FoenixMgr + ${PIP} install -r requirements.txt + popd +popd + +if [[ ! -e f256build.sh ]]; then + ln -s f256dev/f256build.sh . +fi +if [[ ! -e f256run.sh ]]; then + ln -s f256dev/f256run.sh . fi -rm -f *.bat - -mv FoenixMgr-master FoenixMgr -pushd FoenixMgr - ${PIP} install -r requirements.txt -popd - -echo "[DEFAULT]" > foenixmgr.ini -echo "port=/dev/ttyUSB0" >> foenixmgr.ini -echo "labels=sample.lbl" >> foenixmgr.ini -echo "flash_address=380000" >> foenixmgr.ini -echo "chunk_size=1024" >> foenixmgr.ini -echo "cpu=65c02" >> foenixmgr.ini - -pushd llvm-mos/bin - ln -s mos-clang++ mos-f256-clang++ - ln -s mos-clang-cpp mos-f256-clang-cpp - ln -s mos-clang mos-f256-clang -popd +if [[ ! -f foenixmgr.ini ]]; then + echo "[DEFAULT]" > foenixmgr.ini + echo "port=/dev/ttyUSB0" >> foenixmgr.ini + echo "labels=sample.lbl" >> foenixmgr.ini + echo "flash_address=380000" >> foenixmgr.ini + echo "chunk_size=1024" >> foenixmgr.ini + echo "cpu=65c02" >> foenixmgr.ini +fi echo echo ----------------------------------------------------------------------------- diff --git a/distro/f256build.sh b/distro/f256build.sh index 999b7c8..2b9e768 100755 --- a/distro/f256build.sh +++ b/distro/f256build.sh @@ -27,7 +27,16 @@ # -ROOT=$(pwd) +SCRIPT_PATH="${BASH_SOURCE}" +while [ -L "${SCRIPT_PATH}" ]; do + SCRIPT_DIR="$(cd -P "$(dirname "${SCRIPT_PATH}")" >/dev/null 2>&1 && pwd)" + SCRIPT_PATH="$(readlink "${SCRIPT_PATH}")" + [[ ${SCRIPT_PATH} != /* ]] && SCRIPT_PATH="${SCRIPT_DIR}/${SCRIPT_PATH}" +done +SCRIPT_PATH="$(readlink -f "${SCRIPT_PATH}")" +SCRIPT_DIR="$(cd -P "$(dirname -- "${SCRIPT_PATH}")" >/dev/null 2>&1 && pwd)" + +ROOT=${SCRIPT_DIR} if [[ -z ${1} ]]; then echo No project folder provided. @@ -48,7 +57,7 @@ if [[ $? == 1 ]]; then fi fi -PROJECT=${ROOT}/${1} +PROJECT="$(cd "$(dirname "$1")"; pwd)/$(basename "$1")" SOURCE=${PROJECT}/src BUILD=${PROJECT}/.builddir NAME=$(basename ${PROJECT}) @@ -59,11 +68,14 @@ pushd "${PROJECT}" > /dev/null mkdir -p .builddir cd .builddir "${ROOT}/overlay" 5 "${BUILD}" "${SOURCE}" - if [[ ! -f "${PROJECT}/f256.ld" ]]; then - cp -f "${ROOT}/llvm-mos/mos-platform/f256/lib/link.ld" "${PROJECT}/f256.ld" + if [[ -f "${PROJECT}/f256.ld" ]]; then + cp -f "${PROJECT}/f256.ld" f256.ld + fi + if [[ ! -f f256.ld ]]; then + cp -f "${ROOT}/llvm-mos/mos-platform/f256/lib/link.ld" f256.ld fi export PATH=${ROOT}/llvm-mos/bin:${PATH} - mos-f256-clang -T ../f256.ld -Wl,-Map=${NAME}.map -o ${NAME} -I"${ROOT}" -Os -Wall -lm *.c + mos-f256-clang -T f256.ld -Wl,-Map=${NAME}.map -o ${NAME} -I"${ROOT}" -Os -Wall -lm *.c if [[ $? == 0 ]]; then mv ${NAME} ${NAME}.pgz llvm-nm ${NAME}.elf > ${NAME}.sym diff --git a/distro/f256run.sh b/distro/f256run.sh index ff34c19..4e381c8 100755 --- a/distro/f256run.sh +++ b/distro/f256run.sh @@ -27,7 +27,16 @@ # -ROOT=$(pwd) +SCRIPT_PATH="${BASH_SOURCE}" +while [ -L "${SCRIPT_PATH}" ]; do + SCRIPT_DIR="$(cd -P "$(dirname "${SCRIPT_PATH}")" >/dev/null 2>&1 && pwd)" + SCRIPT_PATH="$(readlink "${SCRIPT_PATH}")" + [[ ${SCRIPT_PATH} != /* ]] && SCRIPT_PATH="${SCRIPT_DIR}/${SCRIPT_PATH}" +done +SCRIPT_PATH="$(readlink -f "${SCRIPT_PATH}")" +SCRIPT_DIR="$(cd -P "$(dirname -- "${SCRIPT_PATH}")" >/dev/null 2>&1 && pwd)" + +ROOT=${SCRIPT_DIR} if [[ -z ${1} ]]; then echo "No project folder provided." @@ -43,7 +52,7 @@ if [[ $? == 1 ]]; then fi fi -PROJECT=${ROOT}/${1} +PROJECT="$(cd "$(dirname "$1")"; pwd)/$(basename "$1")" NAME=$(basename ${PROJECT}) if [[ ! -f "${PROJECT}/${NAME}.pgz" ]]; then echo "No PGZ found." diff --git a/f256lib/f256.h b/f256lib/f256.h index 8f487a8..4a52c97 100644 --- a/f256lib/f256.h +++ b/f256lib/f256.h @@ -129,8 +129,8 @@ typedef struct colorS { // Allow embedding binary data into program #define IBSTR2(x) #x #define IBSTR(x) IBSTR2(x) -#define EMBED(INCBIN_SECTION, name, file) \ - __asm__(".section " INCBIN_SECTION ",\"aR\" \n" \ +#define EMBED(name, file, address) \ + __asm__(".section binary." IBSTR(name) ",\"aR\" \n" \ ".global incbin_" IBSTR(name) "_start\n" \ ".balign 16\n" \ "incbin_" IBSTR(name) "_start:\n" \ @@ -141,8 +141,8 @@ typedef struct colorS { "incbin_" IBSTR(name) "_end:\n" \ ".byte 0\n" \ ); \ - extern __attribute__((aligned(16))) const char incbin_ ## name ## _start[]; \ - extern const char incbin_ ## name ## _end[] + extern __attribute__((aligned(16))) const char ## name ## _start[]; \ + extern const char ## name ## _end[] // Single-byte diff --git a/tools/overlay/overlay.c b/tools/overlay/overlay.c index d079e5d..10e36e8 100644 --- a/tools/overlay/overlay.c +++ b/tools/overlay/overlay.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -37,10 +38,21 @@ #define BLOCK_COUNT 15 // Could be up to 56 blocks, 8-64. f256.ld needs updated for more. +typedef struct EmbedS { + char *name; + char *filename; + uint64_t address; + struct EmbedS *next; +} EmbedT; + + unsigned char _currentBank = 0; // 0 == lower 64k unsigned char _totalBanks = 0; // Not including lower 64k char *_bankName[BLOCK_COUNT]; // Far blocks (8+), shifted down 8. +EmbedT *_embeds = NULL; +EmbedT *_embedsHead = NULL; + void findBank(char *name); void parseCFile(char *filename, char *targetFile, FILE *trampoline, char *trampolineFile, int swapSlot); @@ -79,25 +91,29 @@ void findBank(char *name) { void parseCFile(char *filename, char *targetFile, FILE *trampoline, char *trampolineFile, int swapSlot) { - FILE *in; - FILE *out; - int c; - char arguments[BUFFER_SIZE]; - char buffer[BUFFER_SIZE]; - char *functionName; - char *start; - char *a; - char *b; - bool isPointer; - bool found; - int x; - int line = 1; - int comments = 0; - bool inComment = false; - int brackets = 0; - int crSinceStart = 0; - int pos = 0; - char *segDef = "#define SEGMENT_"; + FILE *in; + FILE *out; + int c; + char arguments[BUFFER_SIZE]; + char buffer[BUFFER_SIZE]; + char *functionName; + char *start; + char *a; + char *b; + char *d; + char *e; + char *f; + bool isPointer; + bool found; + int x; + int line = 1; + int comments = 0; + bool inComment = false; + int brackets = 0; + int crSinceStart = 0; + int pos = 0; + char *segDef = "#define SEGMENT_"; + EmbedT *newEmbed; /* * This parser sucks. @@ -268,6 +284,7 @@ void parseCFile(char *filename, char *targetFile, FILE *trampoline, char *trampo } else { // Brackets // Is this a segment definition? "#define SEGMENT_" + // ***TODO*** This doesn't allow the definition to be indented. found = false; if (pos > strlen(segDef)) { found = true; @@ -283,7 +300,88 @@ void parseCFile(char *filename, char *targetFile, FILE *trampoline, char *trampo findBank(&buffer[strlen(segDef)]); } - // Write to output in case they depend on the define for some odd reason. + // Is this an EMBED? "EMBED(name, file, address);" + found = false; // This is tracking if we're quoted or not. + start = strstr(buffer, "EMBED"); + if (start != NULL) { + // Scan for opening paren, three arguments. + // We use 'c' to track what we're currently looking for. + c = 0; + a = NULL; // Start of first argument. + b = NULL; // End of first argument. + d = NULL; // Start of second argument. + e = NULL; // End of second argument. + f = NULL; // Start of third argument. + for (x=0; xname = strdup(a); + *b = x; + // Filename. + x = *e; + *e = 0; + newEmbed->name = strdup(d); + *e = x; + // Address. + newEmbed->address = atol(f); + // Add to list. + if (_embeds == NULL) { + _embeds = newEmbed; + _embedsHead = _embeds; + } else { + _embedsHead->next = newEmbed; + _embedsHead = newEmbed; + } + } + } + + // Write to output. fprintf(out, "%s\n", buffer); } // Brackets. @@ -322,22 +420,28 @@ void trimEnd(char *string) { int main(int argc, char *argv[]) { - FILE *out; - int x; - int r; - int nearSlot; - char *targetDir; - char *sourceDir; - char *trampolineFile; - char *linkerFile; - char *cFile; - char *targetFile; - char *thisDir; - int sourceDirOffset; - int thisOffset; - DIR *dir; - struct dirent *dirent; - struct stat fileStat; + FILE *out; + FILE *out2; + FILE *out3; + FILE *in; + int x; + int r; + int nearSlot; + char *targetDir; + char *sourceDir; + char *trampolineFile; + char *linkerFile; + char *linkerFile2; + char *linkerFile3; + char *cFile; + char *targetFile; + char *thisDir; + int sourceDirOffset; + int thisOffset; + uint64_t length; + DIR *dir; + struct dirent *dirent; + struct stat fileStat; /* * Command line: @@ -456,7 +560,78 @@ int main(int argc, char *argv[]) { fclose(out); free(trampolineFile); - // Generate linker script data. + // Generate linker script declaration data. + linkerFile = utilCreateString("%s%cdeclare.ld", targetDir, utilGetPathSeparator()); + out = fopen(linkerFile, "wt"); + if (out == NULL) { + fprintf(stderr, "ERROR! Cannot create %s!\n", linkerFile); + free(targetDir); + free(linkerFile); + return 1; + } + + // Generate linker script MEMORY data. + linkerFile2 = utilCreateString("%s%cmemory.ld", targetDir, utilGetPathSeparator()); + out2 = fopen(linkerFile2, "wt"); + if (out2 == NULL) { + fprintf(stderr, "ERROR! Cannot create %s!\n", linkerFile2); + free(targetDir); + free(linkerFile); + free(linkerFile2); + fclose(out); + return 1; + } + + // Generate linker script SECTIONS data. + linkerFile3 = utilCreateString("%s%csections.ld", targetDir, utilGetPathSeparator()); + out3 = fopen(linkerFile3, "wt"); + if (out3 == NULL) { + fprintf(stderr, "ERROR! Cannot create %s!\n", linkerFile3); + free(targetDir); + free(linkerFile); + free(linkerFile2); + free(linkerFile3); + fclose(out); + fclose(out2); + return 1; + } + + // Populate three linker sections. + _embedsHead = _embeds; + while (_embedsHead != NULL) { + // Declaration. + in = fopen(_embedsHead->filename, "rb"); // ***TODO*** This path will be wrong. + if (in == NULL) { + free(targetDir); + free(linkerFile); + free(linkerFile2); + free(linkerFile3); + fclose(out); + fclose(out2); + fclose(out3); + return 1; + } + fseek(in, 0, SEEK_END); + length = ftell(in); + fclose(in); + fprintf(out, " __%s_lma = %lx;\n", _embedsHead->name, _embedsHead->address); + fprintf(out, " __%s_size = %lx;\n\n", _embedsHead->name, length); + // MEMORY. + fprintf(out2, " %s : ORIGIN = __%s_lma, LENGTH = __%s_size\n", _embedsHead->name, _embedsHead->name, _embedsHead->name); + // SECTIONS. + fprintf(out3, " .%s : { *(.%s .%s*) } >%s end_%s = .;\n", _embedsHead->name, _embedsHead->name, _embedsHead->name, _embedsHead->name, _embedsHead->name); + + _embedsHead = _embedsHead->next; + } + + free(linkerFile); + free(linkerFile2); + free(linkerFile3); + fclose(out); + fclose(out2); + fclose(out3); + + // Generate linker script OUTPUT_FORMAT data. linkerFile = utilCreateString("%s%coutput.ld", targetDir, utilGetPathSeparator()); out = fopen(linkerFile, "wt"); if (out == NULL) { @@ -472,6 +647,15 @@ int main(int argc, char *argv[]) { fprintf(out, " BYTE(0x00)\n"); fprintf(out, " TRIM(block%d)\n\n", x+8); } + _embedsHead = _embeds; + while (_embedsHead != NULL) { + fprintf(out, " SHORT(ORIGIN(%s))\n", _embedsHead->name); + fprintf(out, " BYTE(ORIGIN(%s)>>16)\n", _embedsHead->name); + fprintf(out, " SHORT(end_%s - __%s_lma)\n", _embedsHead->name, _embedsHead->name); + fprintf(out, " BYTE((end_%s - __%s_lma)>>16)\n", _embedsHead->name, _embedsHead->name); + fprintf(out, " TRIM(%s)\n\n", _embedsHead->name); + _embedsHead = _embedsHead->next; + } fclose(out); free(linkerFile); @@ -480,6 +664,14 @@ int main(int argc, char *argv[]) { free(_bankName[x]); } free(targetDir); + _embedsHead = _embeds; + while (_embedsHead != NULL) { + free(_embedsHead->name); + free(_embedsHead->filename); + _embeds = _embedsHead; + _embedsHead = _embedsHead->next; + free(_embeds); + } return 0; }