mkz80ins.pp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. {
  2. Copyright (c) 2020 by Nikolay Nikolov
  3. Convert z80ins.dat to a set of .inc files for usage with
  4. the Free pascal compiler
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. program mkz80ins;
  12. {$mode objfpc}{$H+}
  13. uses
  14. SysUtils,StrUtils;
  15. const
  16. Version = '1.0.0';
  17. HeaderStr = '{ don''t edit, this file is generated from z80ins.dat; to regenerate, run ''make insdat'' in the compiler directory }';
  18. ParamTypes: array [0..40] of string = (
  19. 'void',
  20. 'r',
  21. 'r''',
  22. 'b',
  23. 'n',
  24. 'p',
  25. 'e',
  26. 'nn',
  27. '0',
  28. '1',
  29. '2',
  30. 'cc',
  31. 'C',
  32. 'NC',
  33. 'Z',
  34. 'NZ',
  35. 'dd',
  36. 'qq',
  37. 'pp',
  38. 'rr',
  39. 'A',
  40. 'I',
  41. 'R',
  42. 'IX',
  43. 'IY',
  44. 'SP',
  45. 'DE',
  46. 'HL',
  47. 'AF',
  48. 'AF''',
  49. '(C)',
  50. '(n)',
  51. '(nn)',
  52. '(BC)',
  53. '(DE)',
  54. '(HL)',
  55. '(SP)',
  56. '(IX)',
  57. '(IY)',
  58. '(IX+d)',
  59. '(IY+d)'
  60. );
  61. type
  62. { TZ80InsDatOutputFiles }
  63. TZ80InsDatOutputFiles = class
  64. public
  65. OpFile: TextFile;
  66. NOpFile: TextFile;
  67. StdOpNames: TextFile;
  68. constructor Create;
  69. destructor Destroy;override;
  70. end;
  71. constructor TZ80InsDatOutputFiles.Create;
  72. begin
  73. AssignFile(OpFile,'z80op.inc');
  74. Rewrite(OpFile);
  75. Writeln(OpFile,HeaderStr);
  76. Writeln(OpFile,'(');
  77. AssignFile(NOpFile,'z80nop.inc');
  78. Rewrite(NOpFile);
  79. Writeln(NOpFile,HeaderStr);
  80. AssignFile(StdOpNames,'z80stdopnames.inc');
  81. Rewrite(StdOpNames);
  82. Writeln(StdOpNames,HeaderStr);
  83. Writeln(StdOpNames,'(');
  84. end;
  85. destructor TZ80InsDatOutputFiles.Destroy;
  86. begin
  87. CloseFile(OpFile);
  88. CloseFile(NOpFile);
  89. CloseFile(StdOpNames);
  90. inherited Destroy;
  91. end;
  92. function FindParamType(const ParamTypeStr: string): Integer;
  93. var
  94. I: Integer;
  95. begin
  96. for I:=Low(ParamTypes) to High(ParamTypes) do
  97. if ParamTypes[I]=ParamTypeStr then
  98. exit(I);
  99. raise Exception.Create('Invalid param type: '''+ParamTypeStr+'''');
  100. end;
  101. var
  102. InsDatFile: TextFile;
  103. OutputFiles: TZ80InsDatOutputFiles=nil;
  104. S, op, ParamsStr, S_Param: string;
  105. FirstIns: Boolean=true;
  106. OpCount: Integer=0;
  107. S_Split, S_Params: TStringArray;
  108. begin
  109. writeln('FPC Z80 Instruction Table Converter Version ',Version);
  110. AssignFile(InsDatFile,'../z80/z80ins.dat');
  111. Reset(InsDatFile);
  112. try
  113. OutputFiles:=TZ80InsDatOutputFiles.Create;
  114. while not EoF(InsDatFile) do
  115. begin
  116. Readln(InsDatFile,S);
  117. S:=Trim(S);
  118. if AnsiStartsStr(';',S) then
  119. continue
  120. else if AnsiStartsStr('[',S) then
  121. begin
  122. op:=Copy(S,2,Length(S)-2);
  123. if not FirstIns then
  124. begin
  125. Writeln(OutputFiles.OpFile,',');
  126. Writeln(OutputFiles.StdOpNames,',');
  127. end;
  128. FirstIns:=False;
  129. Write(OutputFiles.OpFile,'A_'+op);
  130. Write(OutputFiles.StdOpNames,''''+LowerCase(op)+'''');
  131. end
  132. else if S<>'' then
  133. begin
  134. Inc(OpCount);
  135. S_Split:=S.Split(' ',TStringSplitOptions.ExcludeEmpty);
  136. S_Params:=S_Split[0].Split(',',TStringSplitOptions.ExcludeEmpty);
  137. for S_Param in S_Params do
  138. FindParamType(S_Param);
  139. end;
  140. end;
  141. Writeln(OutputFiles.OpFile,');');
  142. Writeln(OutputFiles.StdOpNames,');');
  143. Writeln(OutputFiles.NOpFile,OpCount,';');
  144. finally
  145. FreeAndNil(OutputFiles);
  146. CloseFile(InsDatFile);
  147. end;
  148. end.