]> Shamusworld >> Repos - rmac/blobdiff - sect.c
Long forgotten helper script which is used to create tables for VS builds
[rmac] / sect.c
diff --git a/sect.c b/sect.c
index 9c1a586b18dbd68d92bf3b06cf9edf72e2432776..c7580631faa89cf9534f206bdfd34847cab5b13f 100644 (file)
--- a/sect.c
+++ b/sect.c
@@ -9,6 +9,7 @@
 #include "sect.h"
 #include "6502.h"
 #include "direct.h"
+#include "dsp56k.h"
 #include "error.h"
 #include "expr.h"
 #include "listing.h"
@@ -75,11 +76,14 @@ void InitSection(void)
                MakeSection(i, 0);
 
        // Construct default sections, make TEXT the current section
-       MakeSection(ABS,   SUSED | SABS | SBSS);        // ABS
-       MakeSection(TEXT,  SUSED | TEXT       );        // TEXT
-       MakeSection(DATA,  SUSED | DATA       );        // DATA
-       MakeSection(BSS,   SUSED | BSS  | SBSS);        // BSS
-       MakeSection(M6502, SUSED | TEXT       );        // 6502 code section
+       MakeSection(ABS,     SUSED | SABS | SBSS);      // ABS
+       MakeSection(TEXT,    SUSED | TEXT       );      // TEXT
+       MakeSection(DATA,    SUSED | DATA       );      // DATA
+       MakeSection(BSS,     SUSED | BSS  | SBSS);      // BSS
+       MakeSection(M6502,   SUSED | TEXT       );      // 6502 code section
+       MakeSection(M56001P, SUSED | SABS       );      // DSP 56001 Program RAM
+       MakeSection(M56001X, SUSED | SABS       );      // DSP 56001 X RAM
+       MakeSection(M56001Y, SUSED | SABS       );      // DSP 56001 Y RAM
 
        // Switch to TEXT for starters
        SwitchSection(TEXT);
@@ -128,9 +132,13 @@ void SwitchSection(int sno)
                // For 6502 mode, add the last org'd address
 // Why?
 /*
-Because the way this is set up it treats the 6502 assembly space as a single 64K space (+ 16 bytes, for some reason) and just bobbles around inside that space and uses a stack of org "pointers" to show where the data ended up.
+Because the way this is set up it treats the 6502 assembly space as a single 64K space (+ 16 bytes, for some unknown reason) and just bobbles around inside that space and uses a stack of org "pointers" to show where the data ended up.
 
-This is a piss poor way to handle things, and for fucks sake, we can do better than this!
+This is a shitty way to handle things, and we can do better than this!  :-P
+
+Really, there's no reason to have the 6502 (or DSP56001 for that matter) have their own private sections for this kind of thing, as there's literally *no* chance that it would be mingled with 68K+ code.  It should be able to use the TEXT, DATA & BSS sections just like the 68K.
+
+Or should it?  After looking at the code, maybe it's better to keep the 56001 sections segregated from the rest.  But we can still make the 6502 stuff better.
 */
                if (m6502)
                        chptr = cp->chptr + orgaddr;
@@ -151,7 +159,7 @@ void SaveSection(void)
        sp->sloc = sloc;
        sp->orgaddr = orgaddr;
 
-       if (scode != NULL)                              // Bailout code chunk
+       if (scode != NULL)                              // Bailout code chunk (if any)
                scode->ch_size = ch_size;
 }
 
@@ -210,29 +218,43 @@ void chcheck(uint32_t amt)
        DEBUG { printf("    amt (adjusted)=%u\n", amt); }
        SECT * p = &sect[cursect];
        CHUNK * cp = malloc(sizeof(CHUNK) + amt);
+       int first = 0;
 
-       // First chunk in section
        if (scode == NULL)
        {
+               // First chunk in section
                cp->chprev = NULL;
                p->sfcode = cp;
+               first = 1;
        }
-       // Add chunk to other chunks
        else
        {
+               // Add second and on to previous chunk
                cp->chprev = scode;
                scode->chnext = cp;
                scode->ch_size = ch_size;       // Save old chunk's globals
        }
 
        // Setup chunk and global vars
-       cp->chloc = sloc;
+/*
+So, whenever there's an ORG in a 56K section, it sets sloc TO THE ADDRESS IN THE ORG.  Also, the loc/sloc are incremented by 1s, which means to alias correctly to the byte-oriented memory model we have here, we have to fix that kind of crap.
+*/
+       cp->chloc = sloc; // <-- HERE'S THE PROBLEM FOR 56K  :-/
        cp->chnext = NULL;
        challoc = cp->challoc = amt;
        ch_size = cp->ch_size = 0;
        chptr = cp->chptr = ((uint8_t *)cp) + sizeof(CHUNK);
        scode = p->scode = cp;
 
+       // A quick kludge
+/*
+OK, so this is a bit shite, but at least it gets things working the way they should.  The right way to do this is not rely on sloc & friends for the right fixup address but to have an accurate model of the thing.  That will probably come with v2.0.1  :-P
+
+So the problem is, d_org sets sloc to the address of the ORG statement, and that gives an incorrect base for the fixup.  And so when a second (or later) chunk is allocated, it gets set wrong.  Further complicating things is that the orgaddress *does not* get used in a typical way with the DSP56001 code, and, as such, causes incorrect addresses to be generated.  All that has to be dealt with in order to get this right and do away with this kludge.
+*/
+       if (((cursect == M56001P) || (cursect == M56001X) || (cursect == M56001Y)) && !first)
+               cp->chloc = cp->chprev->chloc + cp->chprev->ch_size;
+
        return;
 }
 
@@ -267,7 +289,8 @@ int AddFixup(uint32_t attr, uint32_t loc, TOKEN * fexpr)
        if (attr & FUMASKDSP)
        {
                attr |= FU_56001;
-               _orgaddr = orgaddr;
+               // Save the exact spot in this chunk where the fixup should go
+               _orgaddr = chptr - scode->chptr + scode->chloc;
        }
 
        // Allocate space for the fixup + any expression
@@ -349,6 +372,9 @@ int ResolveFixups(int sno)
                // than this.
                SetFilenameForErrorReporting();
 
+               if ((sno == M56001P) || (sno == M56001X) || (sno == M56001Y) || (sno == M56001L))
+                       loc = fup->orgaddr;
+
                // Search for chunk containing location to fix up; compute a
                // pointer to the location (in the chunk). Often we will find the
                // Fixup is in the "cached" chunk, so the linear-search is seldom
@@ -386,6 +412,7 @@ int ResolveFixups(int sno)
                // Complex expression
                if (dw & FU_EXPR)
                {
+                       // evexpr presumably issues the errors/warnings here
                        if (evexpr(fup->expr, &eval, &eattr, &esym) != OK)
                                continue;
                }
@@ -403,6 +430,8 @@ int ResolveFixups(int sno)
                        // If the symbol is not defined, but global, set esym to sy
                        if ((eattr & (GLOBAL | DEFINED)) == GLOBAL)
                                esym = sy;
+
+                       DEBUG { printf("               name: %s, value: $%" PRIX64 "\n", sy->sname, sy->svalue); }
                }
 
                uint16_t tdb = eattr & TDB;
@@ -426,12 +455,18 @@ int ResolveFixups(int sno)
                //
                // PC-relative fixups must be DEFINED and either in the same section
                // (whereupon the subtraction takes place) or ABS (with no subtract).
-               if (dw & FU_PCREL)
+               if ((dw & FU_PCREL) || (dw & FU_PCRELX))
                {
                        if (eattr & DEFINED)
                        {
                                if (tdb == sno)
+                               {
                                        eval -= loc;
+
+                                       // In this instruction the PC is located a DWORD away
+                                       if (dw & FU_PCRELX)
+                                               eval += 2;
+                               }
                                else if (tdb)
                                {
                                        // Allow cross-section PCREL fixups in Alcyon mode
@@ -454,6 +489,10 @@ int ResolveFixups(int sno)
                                                }
 
                                                eval -= loc;
+
+                                               // In this instruction the PC is located a DWORD away
+                                               if (dw & FU_PCRELX)
+                                                       eval += 2;
                                        }
                                        else
                                        {
@@ -466,8 +505,15 @@ int ResolveFixups(int sno)
                                        warn("unoptimized short branch");
                        }
                        else if (obj_format == MWC)
+                       {
                                eval -= loc;
 
+                               // In this instruction the PC is located a DWORD away
+                               if (dw & FU_PCRELX)
+                                       eval += 2;
+                       }
+
+                       // Be sure to clear any TDB flags, since we handled it just now
                        tdb = 0;
                        eattr &= ~TDB;
                }
@@ -566,16 +612,7 @@ int ResolveFixups(int sno)
                case FU_WORD:
                        if ((dw & FUMASKRISC) == FU_JR)
                        {
-#if 0
-                               int reg;
-
-                               if (fup->orgaddr)
-                                       reg = (signed)((eval - (fup->orgaddr + 2)) / 2);
-                               else
-                                       reg = (signed)((eval - (loc + 2)) / 2);
-#else
                                int reg = (signed)((eval - ((fup->orgaddr ? fup->orgaddr : loc) + 2)) / 2);
-#endif
 
                                if ((reg < -16) || (reg > 15))
                                {
@@ -727,8 +764,8 @@ int ResolveFixups(int sno)
                                uint64_t addr = eval;
 
 //Hmm, not sure how this can be set, since it's only set if it's a DSP56001 fixup or a FU_JR...  :-/
-                               if (fup->orgaddr)
-                                       addr = fup->orgaddr;
+//                             if (fup->orgaddr)
+//                                     addr = fup->orgaddr;
 
                                eval = (quad & 0xFFFFFC0000FFFFFFLL) | ((addr & 0x3FFFF8) << 21);
                        }
@@ -743,8 +780,8 @@ int ResolveFixups(int sno)
                                uint64_t addr = eval;
 
 //Hmm, not sure how this can be set, since it's only set if it's a DSP56001 fixup or a FU_JR...  :-/
-                               if (fup->orgaddr)
-                                       addr = fup->orgaddr;
+//                             if (fup->orgaddr)
+//                                     addr = fup->orgaddr;
 
                                eval = (quad & 0x000007FFFFFFFFFFLL) | ((addr & 0xFFFFF8) << 40);
                        }
@@ -798,7 +835,7 @@ int ResolveFixups(int sno)
                        case FU_DSPADR12:
                                if (eval >= 0x1000)
                                {
-                                       error("address out of range ($000-$FFF)");
+                                       error("address out of range ($0-$FFF)");
                                        break;
                                }
 
@@ -809,22 +846,22 @@ int ResolveFixups(int sno)
                        // This is a full DSP word containing Effective Address Extension
                        case FU_DSPADR24:
                        case FU_DSPIMM24:
-                               if (eval >= 0x100000)
+                               if (eval >= 0x1000000)
                                {
-                                       error("value out of range ($000-$FFFFFF)");
+                                       error("value out of range ($0-$FFFFFF)");
                                        break;
                                }
 
-                               *locp++ = (uint32_t)eval >> 16;
-                               *locp++ = ((uint32_t)eval >> 8) & 0xFF;
-                               *locp++ = (uint32_t)eval & 0xFF;
+                               locp[0] = (uint8_t)((eval >> 16) & 0xFF);
+                               locp[1] = (uint8_t)((eval >> 8) & 0xFF);
+                               locp[2] = (uint8_t)(eval & 0xFF);
                                break;
 
                        // This is a 16bit absolute address into a 24bit field
                        case FU_DSPADR16:
                                if (eval >= 0x10000)
                                {
-                                       error("address out of range ($0000-$FFFF)");
+                                       error("address out of range ($0-$FFFF)");
                                        break;
                                }
 
@@ -838,7 +875,7 @@ int ResolveFixups(int sno)
                        case FU_DSPIMM12:
                                if (eval >= 0x1000)
                                {
-                                       error("immediate out of range ($000-$FFF)");
+                                       error("immediate out of range ($0-$FFF)");
                                        break;
                                }
 
@@ -851,7 +888,7 @@ int ResolveFixups(int sno)
                        case FU_DSPIMM8:
                                if (eval >= 0x100)
                                {
-                                       error("immediate out of range ($00-$FF)");
+                                       error("immediate out of range ($0-$FF)");
                                        break;
                                }
 
@@ -948,6 +985,12 @@ int ResolveAllFixups(void)
        ResolveFixups(DATA);
        DEBUG printf("Resolving 6502 sections...\n");
        ResolveFixups(M6502);           // Fixup 6502 section (if any)
+       DEBUG printf("Resolving DSP56001 P: sections...\n");
+       ResolveFixups(M56001P);         // Fixup 56001 P: section (if any)
+       DEBUG printf("Resolving DSP56001 X: sections...\n");
+       ResolveFixups(M56001X);         // Fixup 56001 X: section (if any)
+       DEBUG printf("Resolving DSP56001 Y: sections...\n");
+       ResolveFixups(M56001Y);         // Fixup 56001 Y: section (if any)
 
        return 0;
 }