//! \brief close this stream public override void Close() { m_bDisposed = true; if (null != m_MemorySpace) { //! memory space m_MemorySpace.BeginUpdateMemorySpaceEvent -= new VirtualMemorySpaceImage.BeginUpdateMemorySpace(m_MemorySpace_BeginUpdateMemorySpaceEvent); m_MemorySpace.EndUpdateMemorySpaceEvent -= new VirtualMemorySpaceImage.EndUpdateMemorySpace(m_MemorySpace_EndUpdateMemorySpaceEvent); m_MemorySpace.UpdateMemorySpaceEvent -= new VirtualMemorySpaceImage.UpdateMemorySpace(m_MemorySpace_UpdateMemorySpaceEvent); m_MemorySpace.LoadMemoryBlockEvent -= new VirtualMemorySpaceImage.LoadMemoryBlock(LoadMemoryBlockFromHexFile); m_MemorySpace = null; } //! file stream if (null != m_FileStream) { m_FileStream.Dispose(); m_FileStream = null; } base.Close(); }
//! \brief load memory block from hex file private Boolean LoadMemoryBlockFromHexFile(UInt32 tTargetAddress, ref Byte[] tData, Int32 tSize) { if (m_Access == FileAccess.Write) { return(false); } else if (null == m_FileStream) { return(false); } else if (!m_FileStream.CanRead) { return(false); } VirtualMemorySpace tMemorySpace = new VirtualMemorySpace(); tMemorySpace.SpaceLength = UInt32.MaxValue; StreamReader tStreamReader = null; try { FileStream tFileStream = null; if (m_Access == FileAccess.Read) { tFileStream = new FileStream(m_FilePath, m_FileMode, m_Access, FileShare.Read); } else { tFileStream = new FileStream(m_FilePath, m_FileMode, m_Access, FileShare.ReadWrite); } tStreamReader = new StreamReader(tFileStream); if (null == tStreamReader) { return(false); } } catch (Exception Err) { Err.ToString(); tStreamReader.Close(); return(false); } String tRecordStr = null; try { Boolean bSeeEOF = false; UInt32 tAddress = 0; do { tRecordStr = tStreamReader.ReadLine(); if (null != tRecordStr) { Record tRecord = Record.Parse(tRecordStr); if (null == tRecord) { break; } switch (tRecord.RecordType) { case Record.Type.DATA_RECORD: if ( ((tRecord.LoadOffset + tAddress) < tTargetAddress) && ((tRecord.LoadOffset + tAddress + tRecord.Data.Length) <= tTargetAddress) ) { continue; } else if ( ((tRecord.LoadOffset + tAddress) > tTargetAddress) && ((tTargetAddress + tSize) <= (tRecord.LoadOffset + tAddress)) ) { break; } tMemorySpace.Write(tAddress + tRecord.LoadOffset, tRecord.Data); break; case Record.Type.END_OF_FILE_RECORD: bSeeEOF = true; break; case Record.Type.EXTEND_SEGMENT_ADDRESS_RECORD: tAddress = ((ExtendSegmentAddressRecord)tRecord).ExtendSegmentBaseAddress; break; case Record.Type.START_SEGMENT_ADDRESS_RECORD: tAddress = ((StartSegmentAddressRecord)tRecord).StartSegmentAddress; break; case Record.Type.EXTEND_LINEAR_ADDRESS_RECORD: tAddress = ((ExtendLinearAddressRecord)tRecord).UpperLinearBaseAddress; break; case Record.Type.START_LINEAR_ADDRESS_RECORD: tAddress = ((StartLinearAddressRecord)tRecord).StartLinearAddress; break; default: break; } } }while (!bSeeEOF); } catch (Exception Err) { Err.ToString(); } finally { tStreamReader.Dispose(); } return(tMemorySpace.Read(tTargetAddress, ref tData, tSize)); }