Exemple #1
0
                public static TAPBlock LoadBlock(byte[] FileData, int StartPos, out int EndPos)
                {
                    EndPos = 0;

                    TAPBlock block = new TAPBlock();

                    block.Length = Word(FileData, StartPos);

                    if (block.Length + StartPos + 2 > FileData.Length)
                    {
                        return(null);
                    }

                    block.Data = new byte[block.Length];
                    Buffer.BlockCopy(FileData, StartPos + 2, block.Data, 0, block.Data.Length);

                    EndPos = StartPos + 2 + block.Length;

                    return(block);
                }
Exemple #2
0
            public static TAPFile Load(string FileName)
            {
                TAPFile file = new TAPFile();

                byte[] data = File.ReadAllBytes(FileName);

                List <TAPBlock> blocks = new List <TAPBlock>();

                int pos = 0;

                while (pos < data.Length)
                {
                    TAPBlock block = TAPBlock.LoadBlock(data, pos, out pos);

                    if (block == null)
                    {
                        return(null);
                    }

                    blocks.Add(block);
                }

                byte[] basicLoader = null;

                while (blocks.Count > 0)
                {
                    if (blocks.Count < 2)
                    {
                        return(null);
                    }

                    TAPBlock headerBlock = blocks[0];
                    blocks.RemoveAt(0);
                    TAPBlock dataBlock = blocks[0];
                    blocks.RemoveAt(0);

                    if (!headerBlock.IsHeader || dataBlock.IsHeader ||
                        !headerBlock.IsValid || !dataBlock.IsValid)
                    {
                        return(null);
                    }

                    TAPHeader realHeader = headerBlock.GetHeader();
                    byte[]    realData   = dataBlock.GetData();

                    if (realHeader.Type == TAPSpectrumDataType.Program)
                    {
                        if (basicLoader != null)
                        {
                            return(null);
                        }

                        basicLoader = realData;
                    }
                    else if (realHeader.Type == TAPSpectrumDataType.Code)
                    {
                        if (realHeader.Length != realData.Length)
                        {
                            return(null);
                        }

                        Buffer.BlockCopy(realData, 0, file.Data, realHeader.Param1, realData.Length);

                        if (realHeader.Param1 < file.StartAddress)
                        {
                            file.StartAddress = realHeader.Param1;
                        }

                        if (realHeader.Param1 + realData.Length > file.EndAddress)
                        {
                            file.EndAddress = realHeader.Param1 + realData.Length;
                        }
                    }
                    else
                    {
                        return(null);
                    }
                }

                if (basicLoader == null)
                {
                    return(null);
                }

                file.Header = FindEntryPoint(basicLoader);

                if (file.Header == null)
                {
                    return(null);
                }

                return(file);
            }