ihxreader.pas 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. { IHX (Intel Hex format) to TZX (ZX Spectrum tape file format) convertor tool.
  2. This file contains the IHX writer code.
  3. Copyright (C) 2020 Nikolay Nikolov <[email protected]>
  4. This source is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 2 of the License, or (at your option)
  7. any later version.
  8. This code is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  11. details.
  12. A copy of the GNU General Public License is available on the World Wide Web
  13. at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
  14. to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1335, USA.
  16. }
  17. unit ihxreader;
  18. {$mode objfpc}{$H+}
  19. interface
  20. uses
  21. Classes, SysUtils;
  22. type
  23. { TIHXReader }
  24. TIHXReader = class
  25. private
  26. FOrigin: Word;
  27. FInternalData: array [0..$FFFF] of Byte;
  28. public
  29. Data: array of Byte;
  30. procedure ReadIHXFile(const FileName: string);
  31. property Origin: Word read FOrigin;
  32. end;
  33. implementation
  34. { TIHXReader }
  35. procedure TIHXReader.ReadIHXFile(const FileName: string);
  36. var
  37. InF: TextFile;
  38. S: string;
  39. I: Integer;
  40. LineByteCount: Byte;
  41. LineAddress: Word;
  42. MinAddress, MaxAddress: LongInt;
  43. RecordType: Byte;
  44. Checksum, ExpectedChecksum: Byte;
  45. B: Byte;
  46. begin
  47. MinAddress := -1;
  48. MaxAddress := -1;
  49. FOrigin := 0;
  50. SetLength(Data, 0);
  51. AssignFile(InF, FileName);
  52. Reset(InF);
  53. try
  54. while not EoF(InF) do
  55. begin
  56. ReadLn(InF, S);
  57. S:=UpperCase(Trim(S));
  58. if S='' then
  59. continue;
  60. if Length(S)<11 then
  61. raise Exception.Create('Line too short');
  62. if S[1]<>':' then
  63. raise Exception.Create('Line must start with '':''');
  64. for I:=2 to Length(S) do
  65. if not (S[I] in ['0'..'9','A'..'F']) then
  66. raise Exception.Create('Line contains an invalid character');
  67. LineByteCount:=StrToInt('$'+Copy(S,2,2));
  68. if (LineByteCount*2+11)<>Length(S) then
  69. raise Exception.Create('Invalid line length');
  70. LineAddress:=StrToInt('$'+Copy(S,4,4));
  71. RecordType:=StrToInt('$'+Copy(S,8,2));
  72. Checksum:=StrToInt('$'+Copy(S,Length(S)-1,2));
  73. ExpectedChecksum := Byte(LineByteCount + RecordType + Byte(LineAddress) + Byte(LineAddress shr 8));
  74. for I:=0 to LineByteCount-1 do
  75. begin
  76. B := StrToInt('$' + Copy(S, 10 + 2*I, 2));
  77. ExpectedChecksum := Byte(ExpectedChecksum + B);
  78. end;
  79. ExpectedChecksum := Byte(-ExpectedChecksum);
  80. if ExpectedChecksum <> Checksum then
  81. raise Exception.Create('Invalid checksum');
  82. case RecordType of
  83. 0:
  84. begin
  85. if (MinAddress = -1) or (LineAddress < MinAddress) then
  86. MinAddress := LineAddress;
  87. if (MaxAddress = -1) or (MaxAddress < (LineAddress + LineByteCount - 1)) then
  88. MaxAddress := LineAddress + LineByteCount - 1;
  89. if MaxAddress > High(FInternalData) then
  90. raise Exception.CreateFmt('Data exceeds %d bytes', [High(FInternalData) + 1]);
  91. for I:=0 to LineByteCount-1 do
  92. begin
  93. B := StrToInt('$' + Copy(S, 10 + 2*I, 2));
  94. FInternalData[LineAddress + I] := B;
  95. end;
  96. end;
  97. 1:
  98. begin
  99. { end of file }
  100. break;
  101. end;
  102. end;
  103. end;
  104. FOrigin := MinAddress;
  105. SetLength(Data, MaxAddress - MinAddress + 1);
  106. Move(FInternalData[MinAddress], Data[0], MaxAddress - MinAddress + 1);
  107. finally
  108. CloseFile(InF);
  109. end;
  110. end;
  111. end.