/// <summary> /// Parses the contents of a file into a memory image. /// </summary> /// <param name="filepath">Path to the SREC file</param> /// <returns>The parsed memory image</returns> public static RawMemory ParseFile(string filepath) { using (StreamReader file = new StreamReader(filepath)) { var parser = new TextRecordParser(); // parse each line as a record while (parser.ParseNextRecord(file, RecordRegex, CountCharOffset)) { // check if checksum is correct var chksum = (byte)(parser.ByteCount + (parser.Address >> 24) + (parser.Address >> 16) + (parser.Address >> 8) + parser.Address + parser.DataChecksum); chksum ^= parser.Checksum; if (chksum != 0xFF) { throw new ArgumentException(String.Format("The selected srec file has incorrect checksum (line {0}).", parser.CurrentLine)); } switch ((RecordType)parser.RecordType) { case RecordType.Data16b: case RecordType.Data24b: case RecordType.Data32b: parser.SaveRecordData(); break; case RecordType.StartAddress16b: case RecordType.StartAddress24b: case RecordType.StartAddress32b: parser.FlushSegment(); break; #region not used case RecordType.Header: break; case RecordType.Count16b: case RecordType.Count24b: break; #endregion } } if (parser.CurrentLine == 0) { throw new ArgumentException("The selected file is empty or has invalid format."); } return(parser.Memory); } }
/// <summary> /// Parses the contents of a file into a memory image. /// </summary> /// <param name="filepath">Path to the HEX file</param> /// <returns>The parsed memory image</returns> public static RawMemory ParseFile(string filepath) { using (StreamReader file = new StreamReader(filepath)) { bool fileended = false; var parser = new TextRecordParser(); // parse each line as a record while (!fileended && parser.ParseNextRecord(file, RecordRegex, CountCharOffset)) { // check if checksum is correct var chksum = (byte)(parser.ByteCount + (parser.Address >> 8) + parser.Address + parser.RecordType + parser.DataChecksum); // this is equal to taking 2's complement chksum += parser.Checksum; if (chksum != 0) { throw new ArgumentException(String.Format("The selected hex file has incorrect checksum (line {0}).", parser.CurrentLine)); } switch ((RecordType)parser.RecordType) { case RecordType.Data: parser.SaveRecordData(); break; case RecordType.EndOfFile: // flush the last segment parser.FlushSegment(); fileended = true; break; case RecordType.ExtendedLinearAddress: // check the byte count here instead of coding it into the regex if (parser.ByteCount != 2) { throw new ArgumentException(String.Format("The selected hex file has invalid record format (line {0}).", parser.CurrentLine)); } parser.AddressOffset = ((uint)parser.Data[0] << 24) | ((uint)parser.Data[1] << 16); break; case RecordType.ExtendedSegmentAddress: // check the byte count here instead of coding it into the regex if (parser.ByteCount != 2) { throw new ArgumentException(String.Format("The selected hex file has invalid record format (line {0}).", parser.CurrentLine)); } parser.AddressOffset = ((uint)parser.Data[0] << 12) | ((uint)parser.Data[1] << 4); break; #region not used case RecordType.StartLinearAddress: // check the byte count here instead of coding it into the regex if (parser.ByteCount != 4) { throw new ArgumentException(String.Format("The selected hex file has invalid record format (line {0}).", parser.CurrentLine)); } break; case RecordType.StartSegmentAddress: // check the byte count here instead of coding it into the regex if (parser.ByteCount != 4) { throw new ArgumentException(String.Format("The selected hex file has invalid record format (line {0}).", parser.CurrentLine)); } break; #endregion } } if (parser.CurrentLine == 0) { throw new ArgumentException("The selected file is empty or has invalid format."); } if (!fileended) { throw new ArgumentException("The selected hex file is missing hex format End Of File line."); } return(parser.Memory); } }