ihxutil.lpr 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. { IHX (Intel Hex format) utility program
  2. This is the main program of the tool.
  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. program ihxutil;
  18. {$mode objfpc}{$H+}
  19. uses
  20. Classes, SysUtils, CustApp, ihxreader, tzxwriter, zxbasic
  21. { you can add units after this };
  22. const
  23. ShortOptions = 'hb:c:t:';
  24. LongOptions: array [0..0] of string = (
  25. 'help'
  26. );
  27. type
  28. TOutputType = (
  29. otTZX,
  30. otBin
  31. );
  32. { TIHXUtil }
  33. TIHXUtil = class(TCustomApplication)
  34. private
  35. FInputFileName: string;
  36. FOutputFileName: string;
  37. FBasicProgramName: string;
  38. FBinaryProgramName: string;
  39. FInputImage: TIHXReader;
  40. FOutputFile: TStream;
  41. FOutputType: TOutputType;
  42. FTapeWriter: TTZXWriter;
  43. protected
  44. procedure DoRun; override;
  45. public
  46. constructor Create(TheOwner: TComponent); override;
  47. destructor Destroy; override;
  48. procedure WriteHelp; virtual;
  49. end;
  50. { TIHX2TZX }
  51. procedure TIHXUtil.DoRun;
  52. var
  53. ErrorMsg, t: String;
  54. NonOptions: TStringArray;
  55. BasicProgram: AnsiString;
  56. begin
  57. if ParamCount = 0 then
  58. begin
  59. WriteHelp;
  60. Terminate;
  61. Exit;
  62. end;
  63. // quick check parameters
  64. ErrorMsg:=CheckOptions(ShortOptions, LongOptions);
  65. if ErrorMsg<>'' then
  66. raise Exception.Create(ErrorMsg);
  67. // parse parameters
  68. if HasOption('h', 'help') then begin
  69. WriteHelp;
  70. Terminate;
  71. Exit;
  72. end;
  73. if HasOption('b', '') then
  74. FBasicProgramName := GetOptionValue('b', '');
  75. if HasOption('c', '') then
  76. FBinaryProgramName := GetOptionValue('c', '');
  77. if HasOption('t', '') then begin
  78. t := GetOptionValue('t', '');
  79. case t of
  80. 'tzx': FOutputType := otTZX;
  81. 'bin': FOutputType := otBin;
  82. else
  83. raise Exception.CreateFmt('Invalid option for output type parameter: %s', [t]);
  84. end;
  85. end else
  86. FOutputType := otTZX;
  87. NonOptions := GetNonOptions(ShortOptions, LongOptions);
  88. if Length(NonOptions) = 0 then
  89. raise Exception.Create('Missing input file');
  90. if Length(NonOptions) > 2 then
  91. raise Exception.Create('Too many files specified');
  92. FInputFileName := NonOptions[0];
  93. if Length(NonOptions) >= 2 then
  94. FOutputFileName := NonOptions[1]
  95. else
  96. FOutputFileName := ChangeFileExt(FInputFileName, '.tzx');
  97. { add your program here }
  98. FInputImage.ReadIHXFile(FInputFileName);
  99. FOutputFile := TFileStream.Create(FOutputFileName, fmCreate);
  100. case FOutputType of
  101. otTZX: begin
  102. FTapeWriter := TTZXWriter.Create(FOutputFile);
  103. BasicProgram := BAS_EncodeLine(10, ' '+BC_LOAD+'"" '+BC_CODE) +
  104. BAS_EncodeLine(20, ' '+BC_PRINT+BC_USR+BAS_EncodeNumber(FInputImage.Origin));
  105. FTapeWriter.AppendProgramFile(FBasicProgramName, 10, Length(BasicProgram), BasicProgram[1], Length(BasicProgram));
  106. FTapeWriter.AppendCodeFile(FBinaryProgramName, FInputImage.Origin, FInputImage.Data[0], Length(FInputImage.Data));
  107. end;
  108. otBin: begin
  109. FOutputFile.Write(FInputImage.Data[0], Length(FInputImage.Data));
  110. end;
  111. end;
  112. // stop program loop
  113. Terminate;
  114. end;
  115. constructor TIHXUtil.Create(TheOwner: TComponent);
  116. begin
  117. inherited Create(TheOwner);
  118. StopOnException:=True;
  119. FInputImage := TIHXReader.Create;
  120. FBasicProgramName := 'basic';
  121. FBinaryProgramName := 'test';
  122. end;
  123. destructor TIHXUtil.Destroy;
  124. begin
  125. FreeAndNil(FInputImage);
  126. FreeAndNil(FTapeWriter);
  127. FreeAndNil(FOutputFile);
  128. inherited Destroy;
  129. end;
  130. procedure TIHXUtil.WriteHelp;
  131. begin
  132. { add your help code here }
  133. writeln('Usage: ', ExeName, ' [options] ihx_file [out_file]');
  134. Writeln('Options: -b <name> specify the name of the BASIC loader program on the tape');
  135. Writeln(' -c <name> specify the name of the machine code program on the tape');
  136. Writeln(' -t <type> specify the output type; valid types are:');
  137. Writeln(' tzx ZX Spectrum Tape file (default)');
  138. Writeln(' bin Flat binary file (e.g. MSX-DOS COM)');
  139. Writeln(' -h display this help');
  140. Writeln(' --help display this help');
  141. end;
  142. var
  143. Application: TIHXUtil;
  144. begin
  145. Application:=TIHXUtil.Create(nil);
  146. Application.Title:='ihxutil';
  147. Application.Run;
  148. Application.Free;
  149. end.