コード例 #1
0
        //! \brief read bytes
        public override Int32 Read(Byte[] array, Int32 offset, Int32 count)
        {
            if (m_Access == FileAccess.Write)
            {
                throw new NotSupportedException();;
            }
            else if ((null == m_FileStream) || (null == m_MemorySpace))
            {
                throw new ObjectDisposedException("HexFileStream");
            }

            else if (!m_FileStream.CanRead)
            {
                throw new NotSupportedException();
            }

            else if (null == array)
            {
                throw new ArgumentNullException();
            }
            else if ((0 == array.Length) || (0 == count))
            {
                return(0);
            }
            else if ((offset < 0) || (count < 0) || (array.Length < (offset + count)))
            {
                throw new ArgumentOutOfRangeException();
            }

            Byte[] tBuffer = new Byte[count];
            if (!m_MemorySpace.Read(m_AccessPointer, tBuffer))
            {
                throw new IOException();
            }
            m_AccessPointer += (UInt32)count;

            //! copy buffer
            tBuffer.CopyTo(array, offset);

            return(count);
        }
コード例 #2
0
        //! \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));
        }