Ejemplo n.º 1
0
        private static FlashCRCData VerifyHexFile(byte[] hexfile)
        {
            // Here we want to find the right hex file.
            //TextReader tr = new StreamReader(FileName);
            // Read entire file into a buffer, and remove all the colons.
            string HexFile = Encoding.UTF8.GetString(hexfile, 0, hexfile.Length);/*tr.ReadToEnd();*/

            //tr.Close();
            string[]  HexFileLines = HexFile.Split(':');
            HexLine[] hexLines     = new HexLine[HexFileLines.Length];
            int       i            = 0;

            foreach (string HexLineString in HexFileLines)
            {
                if (HexLineString.Length > 5 && !HexLineString.Equals("00000001FF\r\n"))
                {
                    //Convert into numeric
                    hexLines[i].RecDataLen = Convert.ToByte(HexLineString.Substring(0, 2), 16);
                    hexLines[i].Address    = Convert.ToUInt32(HexLineString.Substring(2, 4), 16);
                    hexLines[i].RecType    = Convert.ToByte(HexLineString.Substring(6, 2), 16);
                    hexLines[i].Data       = StringToByteArray(HexLineString.Substring(8, hexLines[i].RecDataLen * 2));
                    hexLines[i].Checksum   = Convert.ToByte(HexLineString.Substring(8 + hexLines[i].RecDataLen * 2, 2), 16);
                    //Calculate the virtual Flash Space.
                    i++;
                }
            }
            hexLines = hexLines.Take(i).ToArray();
            return(VerifyFlash(hexLines));
        }
Ejemplo n.º 2
0
        private HexLine ParseLine(string line)
        {
            line = line.Trim();

            if (line[0] != ':')
            {
                throw new HexFileException("No leading ':' in row");
            }

            //remove the ":" from the record
            line = line.Substring(1, (line.Length - 1));

            // Validate checksum
            if (!ValidateChecksum(line))
            {
                throw new HexFileException("Invalid checksum");
            }

            HexLine hexLine = new HexLine();

            hexLine.recordLength = Convert.ToByte(line.Substring(0, 2), 16);
            hexLine.addressField = Convert.ToUInt16(line.Substring(2, 4), 16);
            hexLine.recordType   = (HexRecordType)Convert.ToByte(line.Substring(6, 2), 16);
            hexLine.dataPayload  = line.Substring(8, hexLine.recordLength * 2);

            return(hexLine);
        }
Ejemplo n.º 3
0
        private void ProcessLine(string line)
        {
            HexLine hexLine = HexLine.Parse(line);


            Address baseAddress = 0x000000; //always 0, not used currently

            switch (hexLine.RecordType)
            {
            case RecordType.Data:
                List <Word> words       = hexLine.Data.ToLittleEndians();           //Reading little endian data
                Address     byteAddress = (Address)(baseAddress + hexLine.Address); //not used. correcting address if it is Extended Address
                Address     wordAddress = (Address)(byteAddress >> 1);              //Byte to Word address
                blocks.Add(new MemoryBlock(wordAddress, words));
                break;

            case RecordType.EndOfFile:
                MergeBlocks();
                break;

            case RecordType.ExtendedSegmentAddress:
            //baseAddress = ((Address)hexLine.Data.ReadBigEndianWord(0)) << 4;
            //break;
            case RecordType.ExtendedLinearAddress:
            //baseAddress = ((Address)hexLine.Data.ReadBigEndianWord(0)) << 16;
            //break;
            case RecordType.StartSegmentAddress:
            case RecordType.StartLinearAddress:
                throw new NotSupportedException();

            default:
                throw new Exception($"Unknown {nameof(RecordType)} {hexLine.RecordType:X}");
            }
        }
Ejemplo n.º 4
0
	void createMap(){
		
        hexLines = new HexLine[height];
        for (int i = 0; i < height; i ++)
        {
            hexLines[i] = new HexLine();
            hexLines[i].columns = new HexInfo[width];
        }

		for (int x = 0; x < width; x++) {

			for (int y = 0; y < height; y++) {

				float xPos = x * xOffset;

				if (y % 2 == 1) 
				{
					xPos += xOffset/2;
				}

                //Instantiate hex
               
                GameObject Hex_go;

				if (x >= 3)
					Hex_go = (GameObject)Instantiate (hexprefab3, new Vector3 (xPos, 0, y * zOffset), Quaternion.identity);
				else 
					Hex_go = (GameObject)Instantiate (hexprefab1, new Vector3 (xPos, 0, y * zOffset), Quaternion.identity);

                HexInfo hexInfo = Hex_go.GetComponentInChildren<HexInfo> ();
				hexInfo.x = x;
				hexInfo.y = y;
				hexInfo.Nucli = false;
				hexInfo.ColorDensity = 0;
				hexInfo.Clickable = true;
                hexInfo.map = this;
				hexInfo.HexColor = 'W';

					hexLines [y].columns [x] = hexInfo;

					//Rename hexes with coordenate names
					Hex_go.name = "Hex_" + x + "_" + y;

					//Group hexes in a GameObject parent called "Hex"
					Hex_go.transform.SetParent (this.transform);

					Hex_go.isStatic = true;

				if (x < 3)
					hexInfo.Clickable = false;

			}
		}
	}
Ejemplo n.º 5
0
    //public Color defaultColor = Color.white;

    void Awake()
    {
        new Setting();

        gridCanvas   = GetComponentInChildren <Canvas>();
        hexMesh      = GetComponentInChildren <HexMesh>();
        lineRenderer = GetComponentInChildren <HexLine>();

        width  = Setting.GetWidth();
        height = Setting.GetHeight();
        cells  = new HexCell[height * width];

        for (int z = 0, i = 0; z < height; z++)
        {
            for (int x = 0; x < width; x++)
            {
                CreateCell(x, z, i++);
            }
        }
    }
Ejemplo n.º 6
0
            public static HexLine Parse(string line)
            {
                if (line[0] != ':')
                {
                    throw new FormatException("Must start with colon ':'");
                }

                if ((line.Length - 1) % 2 != 0)
                {
                    throw new FormatException("Line length must be even (excluding colon ':'");
                }

                var bytes = ReadAllBytes(line);

                var hexLine = new HexLine();

                hexLine.Checksum = bytes.Last();
                Byte calculatedChecksum = (Byte)(~bytes.DropLast().Sum(_ => _) + 1);

                if (hexLine.Checksum != calculatedChecksum)
                {
                    throw new ArgumentException("Checksum does not match");
                }


                hexLine.RecordType = (RecordType)(bytes[3]);
                hexLine.Address    = (Address)bytes.ReadBigEndianWord(1);
                hexLine.ByteCount  = bytes[0];
                if (hexLine.ByteCount != bytes.Count - 5)
                {
                    throw new ArgumentException("Checksum is correct but has less/more data");
                }

                if (hexLine.ByteCount != 0)
                {
                    hexLine.Data = bytes.GetRange(4, hexLine.ByteCount);
                }

                return(hexLine);
            }
Ejemplo n.º 7
0
        public bool Parse(string fn, int maxBlockSize = 256)
        {
            var ret = false;

            try
            {
                using (var reader = File.OpenText(fn))
                {
                    string line;
                    UInt32 addrPrefix = 0;
                    while ((line = reader.ReadLine()) != null)
                    {
                        HexLine hexLine = new HexLine();
                        if (!hexLine.Parse(line))
                        {
                            return(false);
                        }

                        if (hexLine.Type == HexLine.HexLineType.ExtendedLinearAddress)
                        {
                            addrPrefix = hexLine.Addr << 16;
                        }
                        else if (hexLine.Type == HexLine.HexLineType.StartLinearAddress)
                        {
                            EntryPoint = hexLine.Addr;
                        }
                        else if (hexLine.Type == HexLine.HexLineType.EndOfFile)
                        {
                            ret = true;
                            break;
                        }
                        else if (hexLine.Type == HexLine.HexLineType.Data)
                        {
                            var         addr  = addrPrefix | hexLine.Addr;
                            MemoryBlock block = null;
                            try
                            {
                                block = Blocks.Find(v =>
                                                    v.StartAddr < addr && v.StartAddr + v.Length == addr && v.Length + hexLine.Content.Length <= maxBlockSize);
                            }
                            catch (Exception)
                            {
                                // ignore
                            }
                            if (block == null)
                            {
                                block           = new MemoryBlock();
                                block.StartAddr = addr;
                                Blocks.Add(block);
                            }
                            block.Content.AddRange(hexLine.Content);
                        }
                    }
                }
            }
            catch (Exception)
            {
                // ignore
            }

            return(ret);
        }
Ejemplo n.º 8
0
        private static FlashCRCData VerifyFlash(HexLine[] hexLines)
        {
            uint ProgLen;
            uint VirtualFlashAdrs;
            uint ProgAddress;

            // 5 MB Virtual Flash.
            byte[] VirtualFlash = new byte[5 * 1024 * 1024];
            for (int i = 0; i < 5 * 1024 * 1024; i++) // initialize virtual flash to all FFs
            {
                VirtualFlash[i] = 0xFF;
            }
            uint ExtLinAddress = 0;
            uint ExtSegAddress = 0;
            uint MaxAddress    = 0;
            uint MinAddress    = 0xFFFFFFFF;

            // Reset max address and min address.

            for (int i = 0; i < hexLines.Length; i++)
            {
                HexLine HexRecordSt = hexLines[i];
                switch (HexRecordSt.RecType)
                {
                case 0:      //Record Type 00, data record.\
                    //Dont need the following line since we have extacted this data elsewhere.
                    //HexRecordSt.Address = (((HexRec[1] << 8) & 0x0000FF00) | (HexRec[2] & 0x000000FF)) & (0x0000FFFF);
                    HexRecordSt.Address = HexRecordSt.Address + ExtLinAddress + ExtSegAddress;

                    ProgAddress = (HexRecordSt.Address | 0x80000000);

                    if (ProgAddress < 0x9FC00000)     // Make sure we are not writing boot sector.
                    {
                        if (MaxAddress < (ProgAddress + HexRecordSt.RecDataLen))
                        {
                            MaxAddress = ProgAddress + HexRecordSt.RecDataLen;
                        }

                        if (MinAddress > ProgAddress)
                        {
                            MinAddress = ProgAddress;
                        }

                        VirtualFlashAdrs = (ProgAddress - 0x9D000000);     // Program address to local virtual flash address

                        foreach (byte CurrData in HexRecordSt.Data)
                        {
                            VirtualFlash[VirtualFlashAdrs++] = CurrData;
                        }
                        //memcpy((void *)&VirtualFlash[VirtualFlashAdrs], HexRecordSt.Data, HexRecordSt.RecDataLen);
                    }
                    break;

                case 02:      // Record Type 02, defines 4 to 19 of the data address.
                    ExtSegAddress = (((uint)HexRecordSt.Data[0] << 16) & 0x00FF0000u) | (((uint)HexRecordSt.Data[1] << 8) & 0x0000FF00u);
                    ExtLinAddress = 0;
                    break;

                case 04:
                    ExtLinAddress = (((uint)HexRecordSt.Data[0] << 24) & 0xFF000000) | (((uint)HexRecordSt.Data[1] << 16) & 0x00FF0000);
                    ExtSegAddress = 0;
                    break;


                case 01:      //Record Type 01
                default:
                    ExtSegAddress = 0;
                    ExtLinAddress = 0;
                    break;
                }
            }

            MinAddress -= MinAddress % 4;
            MaxAddress += MaxAddress % 4;

            ProgLen = MaxAddress - MinAddress;
            //StartAdress = HexRecordSt.MinAddress;
            VirtualFlashAdrs = (MinAddress - 0x9D000000);
            //*crc = CalculateCrc((char*)&VirtualFlash[VirtualFlashAdrs], *);
            byte[] Temp  = VirtualFlash.Skip((int)VirtualFlashAdrs).Take((int)ProgLen).ToArray(); // array of flash memory to be programmed
            byte[] Temp2 = VirtualFlash.Skip((int)0x1000).Take((int)0x2FC).Concat(VirtualFlash.Skip((int)0x1348).Take((int)0x7DCB8)).ToArray();

            return(new FlashCRCData
            {
                CRC = CalculateCrc(Temp, (int)ProgLen),
                Length = ProgLen,
                StartAddress = MinAddress,
                flashMD5 = GetMd5Hash(Temp2)
            });
        }
Ejemplo n.º 9
0
        private HexLine ParseLine(string line)
        {
            line = line.Trim();

            if (line[0] != ':')
                throw new HexFileException("No leading ':' in row");

            //remove the ":" from the record
            line = line.Substring(1, (line.Length - 1));

            // Validate checksum
            if (!ValidateChecksum(line))
                throw new HexFileException("Invalid checksum");

            HexLine hexLine = new HexLine();

            hexLine.recordLength = Convert.ToByte(line.Substring(0, 2), 16);
            hexLine.addressField = Convert.ToUInt16(line.Substring(2, 4), 16);
            hexLine.recordType = (HexRecordType)Convert.ToByte(line.Substring(6, 2), 16);
            hexLine.dataPayload = line.Substring(8, hexLine.recordLength * 2);

            return hexLine;
        }
Ejemplo n.º 10
0
        private void LoadFromStream(Stream stream)
        {
            blocks = new List <MemoryBlock>();

            string line;
            bool   hexFileEOF = false;

            var    currentBlock           = new MemoryBlock();
            UInt64 currentExtendedAddress = 0;

            Size = 0;

            using (var reader = new StreamReader(stream))
            {
                while (((line = reader.ReadLine()) != "") && (hexFileEOF == false))
                {
                    HexLine hexLine = ParseLine(line);

                    switch (hexLine.recordType)
                    {
                    case HexRecordType.EOF:
                        hexFileEOF = true;
                        break;

                    case HexRecordType.ExtendedLinearAddress:
                        currentExtendedAddress = Convert.ToUInt64(hexLine.dataPayload, 16) << 16;

                        // Add a new memory block section if
                        //   1: the current block has data (size > 0)
                        //   2: the next block has a different starting address to the current block
                        //   3: the next block address does not already exist in the array.
                        if ((currentBlock.size > 0) && (currentExtendedAddress != currentBlock.startAddress) && (blocks.Count(b => b.startAddress == currentExtendedAddress) == 0))
                        {
                            blocks.Add(currentBlock);
                            currentBlock = new MemoryBlock(currentExtendedAddress);
                        }
                        break;

                    case HexRecordType.Data:
                        // Calculate the maximum possible address using the given data
                        // Actual maximum is 65536 bytes (2^16).
                        UInt64 size = (ulong)hexLine.recordLength + (ulong)hexLine.addressField;
                        if (size > currentBlock.size)
                        {
                            currentBlock.size = size;
                        }

                        // Assuming each memory block is 65K in size,
                        // load the data buffer with data at the given address
                        uint offset = 0;
                        for (byte j = 0; j < hexLine.recordLength; j++)
                        {
                            uint addr = (uint)hexLine.addressField + j - offset;

                            // Address exceeds the currently allocated data block,
                            // create a new block at the current address
                            if (addr >= currentBlock.data.Length)     //65536
                            {
                                // Limit the block size
                                currentBlock.size = (uint)currentBlock.data.Length;

                                // Split the memory block
                                blocks.Add(currentBlock);
                                currentBlock = new MemoryBlock(addr + currentExtendedAddress);

                                // Wrap the address around into the new block
                                offset = (uint)currentBlock.data.Length;
                                addr  -= offset;
                            }

                            currentBlock.data[addr] = Convert.ToByte(hexLine.dataPayload.Substring(j * 2, 2), 16);
                        }

                        // Note that if a data line is missing, the data bytes are simply left as '0'
                        //TODO: are they supposed to be set to 0xFF?

                        break;

                    default:
                        throw new HexFileException(String.Format("Unsupported hex record type '{0}'", hexLine.recordType.ToString()));
                    }
                }
            }

            // Finally add the last block used
            blocks.Add(currentBlock);

            Size = 0;
            foreach (var block in blocks)
            {
                Size += block.size;
            }


            /*Console.WriteLine("Num blocks: {0}", blocks.Count);
             *
             * UInt64 totalSize = 0;
             * foreach (var block in blocks)
             * {
             *  //Console.WriteLine("{2}\n\tstartAddress:{0}\n\tsize:{1}", block.startAddress, block.size, block.ToString());
             *  Console.WriteLine(block);
             *  totalSize += block.size;
             * }
             * Console.WriteLine("Total size:{0} bytes", totalSize);*/
        }