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