From 023da9030b4461d1bcabc2603691c548e5afa459 Mon Sep 17 00:00:00 2001 From: z00m128 Date: Fri, 7 Dec 2018 23:53:44 +0100 Subject: [PATCH] 64k limit warnigs fixed and BinIncFile reworked by Busy. --- sjasm/directives.cpp | 2 +- sjasm/sjio.cpp | 352 ++++++++++++++++--------------------------- 2 files changed, 134 insertions(+), 220 deletions(-) diff --git a/sjasm/directives.cpp b/sjasm/directives.cpp index 1ccd789c..14daebe0 100644 --- a/sjasm/directives.cpp +++ b/sjasm/directives.cpp @@ -288,7 +288,7 @@ void dirBLOCK() { aint teller,val = 0; if (ParseExpression(lp, teller)) { if ((signed) teller < 0) { - Error("Negative BLOCK?", 0, FATAL); + Warning("Negative BLOCK?", 0, LASTPASS); } if (comma(lp)) { ParseExpression(lp, val); diff --git a/sjasm/sjio.cpp b/sjasm/sjio.cpp index 83cfac46..ef578a08 100644 --- a/sjasm/sjio.cpp +++ b/sjasm/sjio.cpp @@ -198,6 +198,10 @@ void Warning(const char* fout, const char* bd, int type) { delete[] count; + if (type == LASTPASS && pass != 3) { + return; + } + if (pass > LASTPASS) { SPRINTF1(ep, LINEMAX2, "warning: %s", fout); } else { @@ -229,6 +233,25 @@ void Warning(const char* fout, const char* bd, int type) { _COUT ErrorLine _END; } +void CheckRamLimitExceeded() +{ + if (CurAddress >= 0x10000) + { + char buf[64]; + SPRINTF2(buf, 1024, "RAM limit exceeded 0x%X by %s", CurAddress, PseudoORG ? "DISP":"ORG"); + Warning(buf, 0, LASTPASS); + CurAddress &= 0xFFFF; + } + + if (PseudoORG) if (adrdisp >= 0x10000) + { + char buf[64]; + SPRINTF1(buf, 1024, "RAM limit exceeded 0x%X by ORG", adrdisp); + Warning(buf, 0, LASTPASS); + adrdisp &= 0xFFFF; + } +} + void WriteDest() { if (!WBLength) { return; @@ -543,63 +566,26 @@ void CheckPage() { ExitASM(1); } -void Emit(int byte) { +void Emit(int byte) +{ EB[nEB++] = byte; - if (pass == LASTPASS) { - WriteBuffer[WBLength++] = (char) byte; - if (WBLength == DESTBUFLEN) { - WriteDest(); - } - /* (begin add) */ - if (DeviceID) { - if (PseudoORG) { - if (CurAddress >= 0x10000) { - char buf[1024]; - SPRINTF1(buf, 1024, "RAM limit exceeded %lu", CurAddress); - Error(buf, 0, FATAL); - } - *(MemoryPointer++) = (char) byte; - if ((MemoryPointer - Page->RAM) >= (int)Page->Size) { - ++adrdisp; ++CurAddress; - CheckPage(); - return; - } - } else { - if (CurAddress >= 0x10000) { - char buf[1024]; - SPRINTF1(buf, 1024, "RAM limit exceeded %lu", CurAddress); - Error(buf, 0, FATAL); - } - //if (!nulled) { - *(MemoryPointer++) = (char) byte; - //} else { - // MemoryPointer++; - //} - /* if (CurAddress > 0xFFFE || (CurAddress > 0x7FFE && CurAddress < 0x8001) || (CurAddress > 0xBFFE && CurAddress < 0xC001)) { - _COUT CurAddress _ENDL; - }*/ - if ((MemoryPointer - Page->RAM) >= (int)Page->Size) { - ++CurAddress; - CheckPage(); - return; - } - } - } - /* (end add) */ - } - if (PseudoORG) { - ++adrdisp; - } + CheckRamLimitExceeded(); - if (pass != LASTPASS && DeviceID && CurAddress >= 0x10000) { - char buf[1024]; - SPRINTF1(buf, 1024, "RAM limit exceeded %lu", CurAddress); - Error(buf, 0, FATAL); + if (pass == LASTPASS) + { + WriteBuffer[WBLength++] = (char)byte; + if (WBLength == DESTBUFLEN) WriteDest(); + + if (DeviceID) + { + if ((MemoryPointer - Page->RAM) >= (int)Page->Size) CheckPage(); + *(MemoryPointer++) = (char)byte; + } } ++CurAddress; - + if (PseudoORG) ++adrdisp; } void EmitByte(int byte) { @@ -633,60 +619,33 @@ void EmitWords(int* words) { void EmitBlock(aint byte, aint len, bool nulled) { PreviousAddress = CurAddress; - if (len) { - EB[nEB++] = byte; + if (len) EB[nEB++] = byte; + + if (len < 0) + { + CurAddress = (CurAddress + len) & 0xFFFF; + if (PseudoORG) adrdisp = (adrdisp + len) & 0xFFFF; + CheckPage(); } - while (len--) { - if (pass == LASTPASS) { - WriteBuffer[WBLength++] = (char) byte; - if (WBLength == DESTBUFLEN) { - WriteDest(); - } - /* (begin add) */ - if (DeviceID) { - if (PseudoORG) { - if (CurAddress >= 0x10000) { - char buf[1024]; - SPRINTF1(buf, 1024, "RAM limit exceeded %lu", CurAddress); - Error(buf, 0, FATAL); - } - if (!nulled) { - *(MemoryPointer++) = (char) byte; - } else { - MemoryPointer++; - } - if ((MemoryPointer - Page->RAM) >= (int)Page->Size) { - ++adrdisp; ++CurAddress; - CheckPage(); continue; - } - } else { - if (CurAddress >= 0x10000) { - char buf[1024]; - SPRINTF1(buf, 1024, "RAM limit exceeded %lu", CurAddress); - Error(buf, 0, FATAL); - } - if (!nulled) { - *(MemoryPointer++) = (char) byte; - } else { - MemoryPointer++; - } - if ((MemoryPointer - Page->RAM) >= (int)Page->Size) { - ++CurAddress; - CheckPage(); continue; - } - } + else while (len--) + { + CheckRamLimitExceeded(); + + if (pass == LASTPASS) + { + WriteBuffer[WBLength++] = (char)byte; + if (WBLength == DESTBUFLEN) WriteDest(); + + if (DeviceID) + { + if ((MemoryPointer - Page->RAM) >= (int)Page->Size) CheckPage(); + if (!nulled) *MemoryPointer = (char)byte; + + MemoryPointer++; } - /* (end add) */ - } - if (PseudoORG) { - ++adrdisp; - } - if (pass != LASTPASS && DeviceID && CurAddress >= 0x10000) { - char buf[1024]; - SPRINTF1(buf, 1024, "RAM limit exceeded %lu", CurAddress); - Error(buf, 0, FATAL); } ++CurAddress; + if (PseudoORG) ++adrdisp; } } @@ -720,11 +679,11 @@ char* GetPath(char* fname, TCHAR** filenamebegin) { } void BinIncFile(char* fname, int offset, int len) { - char* bp; FILE* bif; int res; - int leng; + int totlen = 0; char* fullFilePath; + fullFilePath = GetPath(fname, NULL); if (*fname == '<') { fname++; @@ -733,129 +692,84 @@ void BinIncFile(char* fname, int offset, int len) { Error("Error opening file", fname, FATAL); } free(fullFilePath); - if (offset > 0) { - bp = new char[offset + 1]; - if (bp == NULL) { - Error("No enough memory!", 0, FATAL); - } - res = fread(bp, 1, offset, bif); - if (res == -1) { - Error("Read error", fname, FATAL); - } - if (res != offset) { - Error("Offset beyond filelength", fname, FATAL); - } + + if (len == -1) len = 0; + if (offset == -1) offset = 0; + + // Get length of file // + if (fseek(bif, 0, SEEK_END)) + Error("Error seeking file (len)", fname, FATAL); + totlen = ftell(bif); + if (totlen < 0) + Error("Error telling file (len)", fname, FATAL); + + + printf("INCBIN: name=%s Offset=%u Len=%u\n", fname, offset, len); + + // Seek to begin of including part // + if (offset > totlen) + Error("Offset overflows file length", fname, FATAL); + if (fseek(bif, offset, SEEK_SET)) + Error("Error seeking file (offs)", fname, FATAL); + if (ftell(bif) != offset) + Error("Error telling file (offs)", fname, FATAL); + + // Check requested data // + if (offset + len > totlen) + Error("Error file too short", fname, FATAL); + + // Getting final length of included data // + if (!len) len = totlen - offset; + if (len > 0x10000) + { + len = 0x10000; + Warning("Included data truncated to 64kB", 0, LASTPASS); } - if (len > 0) { - bp = new char[len + 1]; - if (bp == NULL) { - Error("No enough memory!", 0, FATAL); - } + + if (pass != LASTPASS) + { + CurAddress = (CurAddress + len) & 0xFFFF; + if (PseudoORG) adrdisp = (adrdisp + len) & 0xFFFF; + } + else + { + // Reading data from file // + char* data = new char[len + 1]; + char *bp = data; + + if (bp == NULL) + Error("No enough memory for file", 0, FATAL); + res = fread(bp, 1, len, bif); - if (res == -1) { - Error("Read error", fname, FATAL); - } - if (res != len) { - Error("Unexpected end of file", fname, FATAL); - } - while (len--) { - if (pass == LASTPASS) { + + if (res == -1) + Error("Can't read file (read error)", fname, FATAL); + if (res != len) + Error("Can't read file (no enough data)", fname, FATAL); + + while (len--) + { + CheckRamLimitExceeded(); + + if (pass == LASTPASS) + { WriteBuffer[WBLength++] = *bp; - if (WBLength == DESTBUFLEN) { - WriteDest(); - } - if (DeviceID) { - if (PseudoORG) { - if (CurAddress >= 0x10000) { - char buf[1024]; - SPRINTF1(buf, 1024, "RAM limit exceeded %lu", CurAddress); - Error(buf, 0, FATAL); - } - *(MemoryPointer++) = *bp; - if ((MemoryPointer - Page->RAM) >= (int)Page->Size) { - ++adrdisp; ++CurAddress; - CheckPage(); bp++; continue; - } - } else { - if (CurAddress >= 0x10000) { - char buf[1024]; - SPRINTF1(buf, 1024, "RAM limit exceeded %lu", CurAddress); - Error(buf, 0, FATAL); - } - *(MemoryPointer++) = *bp; - if ((MemoryPointer - Page->RAM) >= (int)Page->Size) { - ++CurAddress; - CheckPage(); bp++; continue; - } - } + if (WBLength == DESTBUFLEN) WriteDest(); + + if (DeviceID) + { + if ((MemoryPointer - Page->RAM) >= (int)Page->Size) CheckPage(); + *MemoryPointer = *bp; + + MemoryPointer++; } - bp++; - } - if (PseudoORG) { - ++adrdisp; - } - if (pass != LASTPASS && DeviceID && CurAddress >= 0x10000) { - char buf[1024]; - SPRINTF1(buf, 1024, "RAM limit exceeded %lu", CurAddress); - Error(buf, 0, FATAL); } + ++bp; ++CurAddress; + if (PseudoORG) ++adrdisp; } - } else { - if (pass == LASTPASS) { - WriteDest(); - } - do { - res = fread(WriteBuffer, 1, DESTBUFLEN, bif); - if (res == -1) { - Error("Read error", fname, FATAL); - } - if (pass == LASTPASS) { - WBLength = res; - if (DeviceID) { - leng = 0; - while (leng != res) { - if (PseudoORG) { - if (CurAddress >= 0x10000) { - Error("RAM limit exceeded", 0, FATAL); - } - *(MemoryPointer++) = (char) WriteBuffer[leng++]; - if ((MemoryPointer - Page->RAM) >= (int)Page->Size) { - ++adrdisp; ++CurAddress; - CheckPage(); - } else { - ++adrdisp; ++CurAddress; - } - } else { - if (CurAddress >= 0x10000) { - Error("RAM limit exceeded", 0, FATAL); - } - *(MemoryPointer++) = (char) WriteBuffer[leng++]; - if ((MemoryPointer - Page->RAM) >= (int)Page->Size) { - ++CurAddress; - CheckPage(); - } else { - ++CurAddress; - } - } - } - } - WriteDest(); - } - if (!DeviceID || pass != LASTPASS) { - if (PseudoORG) { - adrdisp += res; - } - for (int j=0;j < res;j++) { - if (pass != LASTPASS && DeviceID && CurAddress >= 0x10000) { - char buf[1024]; - SPRINTF1(buf, 1024, "RAM limit exceeded %lu", CurAddress); - Error(buf, 0, FATAL); - } - ++CurAddress; - } - } - } while (res == DESTBUFLEN); + CheckRamLimitExceeded(); + delete[] data; } fclose(bif); }