WriteMemory allows you to write a block of bytes from memory directly to an open file channel.
FACTS:
* WriteMemory assume the memory that data is being written from, has been previously allocated by the user. Failing to do so will no doubt cause the command to crash.
Mini Tutorial:
This example first dumps a block of memory to disc, then reloads it and compares the result.
; create the FileName for our Temp data file File$=CurrentDir$()+"PB_ReadWriteMemory_TestFile.txt" ; ------------------------------------------- ; Create a Bank to store some values in ; ------------------------------------------- Size=1024 CreateBank 1,size For lp=0 To Size-1 PokeBankByte 1,Lp,lp And 255 Next ; ---------------------------------------- ; Write this memory to the temp file ; ---------------------------------------- WriteFile File$,1 ; Write the Memory Block Size to the file (so we ; can read it back later WriteInt 1,GetBankSize(1) ; Get the Start Address of the bank in memory StartAddress=GetBankPtr(1) ; Get the End addres of this bank in memory EndAddress=StartAddress+GetBankSize(1) ; Dump this section of memory directly to ; the opened file WriteMemory 1,StartAddress,EndAddress ; Dump a string at the end WriteString 1,"End of File" CloseFile 1 ; ---------------------------------------- ; Load the Created File In Bank #2 ; ---------------------------------------- OpenFile File$,1 Size=ReadInt(1) ; create the Bank #2 of the required size CreateBank 2,Size ; Read these bytes directly into memory ReadMemory 1,GetBankPtr(2),size ; Dump a string at the end, just so were sure ; it loading the memory correctly s$=ReadString$(1) Print s$ CloseFile 1 ; Compare banks 1 and 2 MatchingFlag=true For lp=0 To GetBankSize(1)-1 If PeekBankByte(1,lp)<> PeekBankByte(1,lp) Print "bytes at offset don't match:"+Str$(lp) MatchingFlag=false Exit EndIf Next If MatchingFlag=true Print "banks Match" Else Print "banks Don't Match" EndIf ; Display the screena and wait for a key press Sync WaitKey |
This example would output.
|