/// <summary> /// Write an address record (type 02, 04 or 05) to the underlying stream /// </summary> /// <param name="addressType">The <see cref="IntelHex32AddressType" /> address record type to write to the stream</param> /// <param name="address">The address value to write to the stream. This is either the segment address (type 02) or the upper word of a 32 bit address (type 04 or 05).</param> /// <exception cref="ArgumentOutOfRangeException">If the <paramref name="addressType"/> is not a member of <see cref="IntelHex32AddressType"/></exception> /// <exception cref="ArgumentOutOfRangeException">If the <paramref name="addressType"/> is an <see cref="IntelHex32AddressType.ExtendedSegmentAddress"/> and <paramref name="address"/> is > 0x10000</exception> public void WriteAddress(IntelHexAddressType addressType, int address) { if (!Enum.IsDefined(typeof(IntelHexAddressType), addressType)) { throw new ArgumentOutOfRangeException(nameof(addressType), $"Value [{addressType}] in not a value of [{nameof(IntelHexAddressType)}]"); } if ((addressType == IntelHexAddressType.ExtendedSegmentAddress) && (address > maxSegAddr)) { throw new ArgumentOutOfRangeException(nameof(address), $"Value must be less than 0x{maxSegAddr:X}"); } var addressData = FormatAddress(addressType, address); WriteHexRecord((IntelHexRecordType)addressType, 0, addressData); }
private static List <byte> FormatAddress(IntelHexAddressType addressType, int address) { var result = new List <byte>(); var shift = (byte)(addressType == IntelHexAddressType.ExtendedSegmentAddress ? 4 : 0); shift = (byte)(addressType == IntelHexAddressType.ExtendedLinearAddress ? 16 : shift); var addressBytes = BitConverter.GetBytes(address >> shift); if (addressType == IntelHexAddressType.StartLinearAddress) { result.Add(addressBytes[3]); result.Add(addressBytes[2]); } result.Add(addressBytes[1]); result.Add(addressBytes[0]); return(result); }