Esempio n. 1
0
        public override void Close()
        {
            if (null != m_MemorySpace)
            {
                //! memory space
                m_MemorySpace.BeginUpdateMemorySpaceEvent -= new VirtualMemorySpaceImage.BeginUpdateMemorySpace(OnBeginUpdateMemorySpaceEvent);
                m_MemorySpace.EndUpdateMemorySpaceEvent   -= new VirtualMemorySpaceImage.EndUpdateMemorySpace(OnEndUpdateMemorySpaceEvent);
                m_MemorySpace.UpdateMemorySpaceEvent      -= new VirtualMemorySpaceImage.UpdateMemorySpace(OnUpdateMemorySpaceEvent);
                m_MemorySpace.LoadMemoryBlockEvent        -= new VirtualMemorySpaceImage.LoadMemoryBlock(LoadMemoryBlockFromTargetFile);

                do
                {
                    if (m_FileAccess == System.IO.FileAccess.Read)
                    {
                        break;
                    }
                    //! write memory block to the file
                    OnWriteMemoryToFile();
                } while (false);

                m_MemorySpace = null;
            }

            base.Close();
        }
        public MemorySpaceConverter(VirtualMemorySpace tSpace, IConverter tConverter)
        {
            m_tMemorySpace = tSpace;
            m_tConverter   = tConverter;

            Initialization();
        }
        public void Refresh()
        {
            if (null == m_tMemorySpace || null == m_tConverter)
            {
                return;
            }

            VirtualMemorySpace tMemorySpace = new VirtualMemorySpace();

            //! update alignment
            tMemorySpace.Alignment = m_tConverter.Alignment;

            //! get all blocks
            MemoryBlock[] tBlocks = m_tMemorySpace.MemoryBlocks;

            //! erase all
            if (null == tBlocks)
            {
                return;
            }
            else if (0 == tBlocks.Length)
            {
                return;
            }

            //! convert blocks
            foreach (MemoryBlock tBlock in tBlocks)
            {
                UInt32 wAlignment = m_tConverter.Alignment;
                UInt32 wAddress   = tBlock.Address;
                wAddress -= wAddress % wAlignment;
                for (UInt32 n = 0; n < tBlock.Size; n += wAlignment)
                {
                    MemoryBlock tConvertedBlock = m_tConverter.Convert(tBlock, n + wAddress);
                    tMemorySpace.Write(tConvertedBlock);
                }
            }

            m_tConvertedMemorySpace = tMemorySpace;
        }
        //! \brief load memory block from hex file
        protected override Boolean LoadMemoryBlockFromTargetFile(UInt32 tTargetAddress, ref Byte[] tData, Int32 tSize)
        {
            if (m_FileAccess == FileAccess.Write)
            {
                return(false);
            }
            else if (null == m_File)
            {
                return(false);
            }
            else if (!m_File.CanRead)
            {
                return(false);
            }

            VirtualMemorySpace tMemorySpace = new VirtualMemorySpace();

            tMemorySpace.SpaceLength = UInt32.MaxValue;

            StreamReader tStreamReader = null;

            try
            {
                FileStream tFileStream = null;
                if (m_FileAccess == FileAccess.Read)
                {
                    tFileStream = new FileStream(m_Path, m_FileMode, m_FileAccess, FileShare.Read);
                }
                else
                {
                    tFileStream = new FileStream(m_Path, m_FileMode, m_FileAccess, FileShare.ReadWrite);
                }

                tStreamReader = new StreamReader(tFileStream);
                if (null == tStreamReader)
                {
                    return(false);
                }
            }
            catch (Exception)
            {
                tStreamReader.Close();
                return(false);
            }

            String tRecordStr = null;

            try
            {
                Boolean bSeeEOF  = false;
                UInt32  tAddress = 0;
                do
                {
                    tRecordStr = tStreamReader.ReadLine();
                    if (null != tRecordStr)
                    {
                        HEXRecord tRecord = HEXRecord.Parse(tRecordStr);
                        if (null == tRecord)
                        {
                            break;
                        }
                        switch (tRecord.RecordType)
                        {
                        case HEXRecord.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;
                            }
                            UInt32 tLoadAddress = tAddress + tRecord.LoadOffset;
                            if (this.Offset < 0)
                            {
                                tLoadAddress -= (UInt32)Math.Abs(this.Offset);
                            }
                            else
                            {
                                tLoadAddress += (UInt32)this.Offset;
                            }
                            tMemorySpace.Write(tLoadAddress, tRecord.Data);
                            break;

                        case HEXRecord.Type.END_OF_FILE_RECORD:
                            bSeeEOF = true;
                            break;

                        case HEXRecord.Type.EXTEND_SEGMENT_ADDRESS_RECORD:
                            tAddress = ((ExtendSegmentAddressRecord)tRecord).ExtendSegmentBaseAddress;
                            break;

                        case HEXRecord.Type.EXTEND_LINEAR_ADDRESS_RECORD:
                            tAddress = ((ExtendLinearAddressRecord)tRecord).UpperLinearBaseAddress;
                            break;

                        default:
                            break;
                        }
                    }
                }while (!bSeeEOF);
            }
            catch (Exception)
            {
            }
            finally
            {
                tStreamReader.Dispose();
            }

            return(tMemorySpace.Read(tTargetAddress, ref tData, tSize));
        }