allow Read32FromFile to return signed value
[pforth] / csrc / pf_save.c
index 6a9d195..80b4c1e 100644 (file)
@@ -213,8 +213,8 @@ PF_FLOAT ReadFloatLittleEndian( const PF_FLOAT *addr )
 /***************************************************************/\r
 void WriteCellBigEndian( uint8_t *addr, ucell_t data )\r
 {\r
 /***************************************************************/\r
 void WriteCellBigEndian( uint8_t *addr, ucell_t data )\r
 {\r
-       // Write should be in order of increasing address\r
-       // to optimize for burst writes to DRAM.\r
+       /* Write should be in order of increasing address \r
+        * to optimize for burst writes to DRAM. */\r
        if( sizeof(ucell_t) == 8 )\r
        {\r
                *addr++ = (uint8_t) (data>>56);\r
        if( sizeof(ucell_t) == 8 )\r
        {\r
                *addr++ = (uint8_t) (data>>56);\r
@@ -247,11 +247,11 @@ void Write16BigEndian( uint8_t *addr, uint16_t data )
 /***************************************************************/\r
 void WriteCellLittleEndian( uint8_t *addr, ucell_t data )\r
 {\r
 /***************************************************************/\r
 void WriteCellLittleEndian( uint8_t *addr, ucell_t data )\r
 {\r
-       // Write should be in order of increasing address\r
-       // to optimize for burst writes to DRAM.\r
+       /* Write should be in order of increasing address \r
+        * to optimize for burst writes to DRAM. */\r
        if( sizeof(ucell_t) == 8 )\r
        {\r
        if( sizeof(ucell_t) == 8 )\r
        {\r
-               *addr++ = (uint8_t) data;  // LSB at near end\r
+               *addr++ = (uint8_t) data;  /* LSB at near end */\r
                data = data >> 8;\r
                *addr++ = (uint8_t) data;\r
                data = data >> 8;\r
                data = data >> 8;\r
                *addr++ = (uint8_t) data;\r
                data = data >> 8;\r
@@ -343,6 +343,33 @@ error:
        return -1;\r
 }\r
 \r
        return -1;\r
 }\r
 \r
+/* Convert dictionary info chunk between native and on-disk (big-endian). */\r
+static void\r
+convertDictionaryInfoWrite (DictionaryInfoChunk *sd)\r
+{\r
+/* Convert all fields in DictionaryInfoChunk from Native to BigEndian. \r
+ * This assumes they are all 32-bit integers.\r
+ */\r
+       int   i;\r
+       uint32_t *p = (uint32_t *) sd;\r
+       for (i=0; i<((int)(sizeof(*sd)/sizeof(uint32_t))); i++)\r
+       {\r
+               Write32BigEndian( (uint8_t *)&p[i], p[i] );\r
+       }\r
+}\r
+\r
+static void\r
+convertDictionaryInfoRead (DictionaryInfoChunk *sd)\r
+{\r
+/* Convert all fields in structure from BigEndian to Native. */\r
+       int   i;\r
+       uint32_t *p = (uint32_t *) sd;\r
+       for (i=0; i<((int)(sizeof(*sd)/sizeof(uint32_t))); i++)\r
+       {\r
+               p[i] = Read32BigEndian( (uint8_t *)&p[i] );\r
+       }\r
+}\r
+\r
 /****************************************************************\r
 ** Save Dictionary in File.\r
 ** If EntryPoint is NULL, save as development environment.\r
 /****************************************************************\r
 ** Save Dictionary in File.\r
 ** If EntryPoint is NULL, save as development environment.\r
@@ -356,7 +383,6 @@ cell_t ffSaveForth( const char *FileName, ExecToken EntryPoint, cell_t NameSize,
        uint32_t NameChunkSize = 0;\r
        uint32_t CodeChunkSize;\r
        uint32_t relativeCodePtr;\r
        uint32_t NameChunkSize = 0;\r
        uint32_t CodeChunkSize;\r
        uint32_t relativeCodePtr;\r
-       int   i;\r
 \r
        fid = sdOpenFile( FileName, "wb" );\r
        if( fid == NULL )\r
 \r
        fid = sdOpenFile( FileName, "wb" );\r
        if( fid == NULL )\r
@@ -423,7 +449,7 @@ cell_t ffSaveForth( const char *FileName, ExecToken EntryPoint, cell_t NameSize,
                uint32_t relativeHeaderPtr;\r
 /* Development mode. */\r
                SD.sd_RelContext = ABS_TO_NAMEREL(gVarContext);\r
                uint32_t relativeHeaderPtr;\r
 /* Development mode. */\r
                SD.sd_RelContext = ABS_TO_NAMEREL(gVarContext);\r
-               relativeHeaderPtr = ABS_TO_NAMEREL(gCurrentDictionary->dic_HeaderPtr.Byte);\r
+               relativeHeaderPtr = ABS_TO_NAMEREL(gCurrentDictionary->dic_HeaderPtr);\r
                SD.sd_RelHeaderPtr = relativeHeaderPtr;\r
 \r
 /* How much real name space is there? */\r
                SD.sd_RelHeaderPtr = relativeHeaderPtr;\r
 \r
 /* How much real name space is there? */\r
@@ -445,16 +471,7 @@ cell_t ffSaveForth( const char *FileName, ExecToken EntryPoint, cell_t NameSize,
        SD.sd_CodeSize = CodeSize;\r
 \r
        \r
        SD.sd_CodeSize = CodeSize;\r
 \r
        \r
-/* Convert all fields in DictionaryInfoChunk from Native to BigEndian. \r
- * This assumes they are all 32-bit integers.\r
- */\r
-       {\r
-               uint32_t *p = (uint32_t *) &SD;\r
-               for( i=0; i<((int)(sizeof(SD)/sizeof(uint32_t))); i++ )\r
-               {\r
-                       Write32BigEndian( (uint8_t *)&p[i], p[i] );\r
-               }\r
-       }\r
+       convertDictionaryInfoWrite (&SD);\r
 \r
        if( WriteChunkToFile( fid, ID_P4DI, (char *) &SD, sizeof(DictionaryInfoChunk) ) < 0 ) goto error;\r
 \r
 \r
        if( WriteChunkToFile( fid, ID_P4DI, (char *) &SD, sizeof(DictionaryInfoChunk) ) < 0 ) goto error;\r
 \r
@@ -496,7 +513,7 @@ error:
 #ifndef PF_NO_FILEIO\r
 \r
 /***************************************************************/\r
 #ifndef PF_NO_FILEIO\r
 \r
 /***************************************************************/\r
-static uint32_t Read32FromFile( FileStream *fid, uint32_t *ValPtr )\r
+static int32_t Read32FromFile( FileStream *fid, uint32_t *ValPtr )\r
 {\r
        int32_t numr;\r
        uint8_t pad[4];\r
 {\r
        int32_t numr;\r
        uint8_t pad[4];\r
@@ -517,7 +534,6 @@ PForthDictionary pfLoadDictionary( const char *FileName, ExecToken *EntryPointPt
        uint32_t FormSize;\r
        uint32_t BytesLeft;\r
        uint32_t numr;\r
        uint32_t FormSize;\r
        uint32_t BytesLeft;\r
        uint32_t numr;\r
-       int   i;\r
        int   isDicBigEndian;\r
 \r
 DBUG(("pfLoadDictionary( %s )\n", FileName ));\r
        int   isDicBigEndian;\r
 \r
 DBUG(("pfLoadDictionary( %s )\n", FileName ));\r
@@ -568,15 +584,8 @@ DBUG(("pfLoadDictionary( %s )\n", FileName ));
                        if( numr != ChunkSize ) goto read_error;\r
                        BytesLeft -= ChunkSize;\r
                        \r
                        if( numr != ChunkSize ) goto read_error;\r
                        BytesLeft -= ChunkSize;\r
                        \r
-/* Convert all fields in structure from BigEndian to Native. */\r
-                       {\r
-                               uint32_t *p = (uint32_t *) sd;\r
-                               for( i=0; i<((int)(sizeof(*sd)/sizeof(uint32_t))); i++ )\r
-                               {\r
-                                       p[i] = Read32BigEndian( (uint8_t *)&p[i] );\r
-                               }\r
-                       }\r
-                               \r
+                       convertDictionaryInfoRead (sd);\r
+\r
                        isDicBigEndian = sd->sd_Flags & SD_F_BIG_ENDIAN_DIC;\r
 \r
                        if( !gVarQuiet )\r
                        isDicBigEndian = sd->sd_Flags & SD_F_BIG_ENDIAN_DIC;\r
 \r
                        if( !gVarQuiet )\r
@@ -644,14 +653,14 @@ DBUG(("pfLoadDictionary( %s )\n", FileName ));
                        gCurrentDictionary = dic;\r
                        if( sd->sd_NameSize > 0 )\r
                        {\r
                        gCurrentDictionary = dic;\r
                        if( sd->sd_NameSize > 0 )\r
                        {\r
-                               gVarContext = (char *) NAMEREL_TO_ABS(sd->sd_RelContext); /* Restore context. */\r
-                               gCurrentDictionary->dic_HeaderPtr.Byte = (uint8_t *)\r
+                               gVarContext = NAMEREL_TO_ABS(sd->sd_RelContext); /* Restore context. */\r
+                               gCurrentDictionary->dic_HeaderPtr = (ucell_t)(uint8_t *)\r
                                        NAMEREL_TO_ABS(sd->sd_RelHeaderPtr);\r
                        }\r
                        else\r
                        {\r
                                gVarContext = 0;\r
                                        NAMEREL_TO_ABS(sd->sd_RelHeaderPtr);\r
                        }\r
                        else\r
                        {\r
                                gVarContext = 0;\r
-                               gCurrentDictionary->dic_HeaderPtr.Byte = NULL;\r
+                               gCurrentDictionary->dic_HeaderPtr = (ucell_t)NULL;\r
                        }\r
                        gCurrentDictionary->dic_CodePtr.Byte = (uint8_t *) CODEREL_TO_ABS(sd->sd_RelCodePtr);\r
                        gNumPrimitives = sd->sd_NumPrimitives;  /* Must match compiled dictionary. */\r
                        }\r
                        gCurrentDictionary->dic_CodePtr.Byte = (uint8_t *) CODEREL_TO_ABS(sd->sd_RelCodePtr);\r
                        gNumPrimitives = sd->sd_NumPrimitives;  /* Must match compiled dictionary. */\r
@@ -665,7 +674,7 @@ DBUG(("pfLoadDictionary( %s )\n", FileName ));
                        pfReportError("pfLoadDictionary", PF_ERR_NO_SHELL );\r
                        goto error;\r
 #else\r
                        pfReportError("pfLoadDictionary", PF_ERR_NO_SHELL );\r
                        goto error;\r
 #else\r
-                       if( NAME_BASE == NULL )\r
+                       if( NAME_BASE == 0 )\r
                        {\r
                                pfReportError("pfLoadDictionary", PF_ERR_NO_NAMES );\r
                                goto error;\r
                        {\r
                                pfReportError("pfLoadDictionary", PF_ERR_NO_NAMES );\r
                                goto error;\r
@@ -680,7 +689,7 @@ DBUG(("pfLoadDictionary( %s )\n", FileName ));
                                pfReportError("pfLoadDictionary", PF_ERR_TOO_BIG);\r
                                goto error;\r
                        }\r
                                pfReportError("pfLoadDictionary", PF_ERR_TOO_BIG);\r
                                goto error;\r
                        }\r
-                       numr = sdReadFile( NAME_BASE, 1, ChunkSize, fid );\r
+                       numr = sdReadFile( (char *) NAME_BASE, 1, ChunkSize, fid );\r
                        if( numr != ChunkSize ) goto read_error;\r
                        BytesLeft -= ChunkSize;\r
 #endif /* PF_NO_SHELL */\r
                        if( numr != ChunkSize ) goto read_error;\r
                        BytesLeft -= ChunkSize;\r
 #endif /* PF_NO_SHELL */\r
@@ -697,7 +706,7 @@ DBUG(("pfLoadDictionary( %s )\n", FileName ));
                                pfReportError("pfLoadDictionary", PF_ERR_TOO_BIG);\r
                                goto error;\r
                        }\r
                                pfReportError("pfLoadDictionary", PF_ERR_TOO_BIG);\r
                                goto error;\r
                        }\r
-                       numr = sdReadFile( CODE_BASE, 1, ChunkSize, fid );\r
+                       numr = sdReadFile( (uint8_t *) CODE_BASE, 1, ChunkSize, fid );\r
                        if( numr != ChunkSize ) goto read_error;\r
                        BytesLeft -= ChunkSize;\r
                        break;\r
                        if( numr != ChunkSize ) goto read_error;\r
                        BytesLeft -= ChunkSize;\r
                        break;\r
@@ -711,7 +720,7 @@ DBUG(("pfLoadDictionary( %s )\n", FileName ));
 \r
        sdCloseFile( fid );\r
 \r
 \r
        sdCloseFile( fid );\r
 \r
-       if( NAME_BASE != NULL)\r
+       if( NAME_BASE != 0)\r
        {\r
                cell_t Result;\r
 /* Find special words in dictionary for global XTs. */\r
        {\r
                cell_t Result;\r
 /* Find special words in dictionary for global XTs. */\r
@@ -800,18 +809,18 @@ PForthDictionary pfLoadStaticDictionary( void )
        gCurrentDictionary = dic = pfCreateDictionary( NewNameSize, NewCodeSize );\r
        if( !dic ) goto nomem_error;\r
 \r
        gCurrentDictionary = dic = pfCreateDictionary( NewNameSize, NewCodeSize );\r
        if( !dic ) goto nomem_error;\r
 \r
-       pfCopyMemory( dic->dic_HeaderBase, MinDicNames, sizeof(MinDicNames) );\r
-       pfCopyMemory( dic->dic_CodeBase, MinDicCode, sizeof(MinDicCode) );\r
+       pfCopyMemory( (uint8_t *) dic->dic_HeaderBase, MinDicNames, sizeof(MinDicNames) );\r
+       pfCopyMemory( (uint8_t *) dic->dic_CodeBase, MinDicCode, sizeof(MinDicCode) );\r
        DBUG(("Static data copied to newly allocated dictionaries.\n"));\r
 \r
        dic->dic_CodePtr.Byte = (uint8_t *) CODEREL_TO_ABS(CODEPTR);\r
        gNumPrimitives = NUM_PRIMITIVES;\r
 \r
        DBUG(("Static data copied to newly allocated dictionaries.\n"));\r
 \r
        dic->dic_CodePtr.Byte = (uint8_t *) CODEREL_TO_ABS(CODEPTR);\r
        gNumPrimitives = NUM_PRIMITIVES;\r
 \r
-       if( NAME_BASE != NULL)\r
+       if( NAME_BASE != 0)\r
        {\r
 /* Setup name space. */\r
        {\r
 /* Setup name space. */\r
-               dic->dic_HeaderPtr.Byte = (uint8_t *) NAMEREL_TO_ABS(HEADERPTR);\r
-               gVarContext = (char *) NAMEREL_TO_ABS(RELCONTEXT); /* Restore context. */\r
+               dic->dic_HeaderPtr = (ucell_t)(uint8_t *) NAMEREL_TO_ABS(HEADERPTR);\r
+               gVarContext = NAMEREL_TO_ABS(RELCONTEXT); /* Restore context. */\r
 \r
 /* Find special words in dictionary for global XTs. */\r
                if( (Result = FindSpecialXTs()) < 0 )\r
 \r
 /* Find special words in dictionary for global XTs. */\r
                if( (Result = FindSpecialXTs()) < 0 )\r