ppu.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Routines to read/write ppu files
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ppu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. constexp,entfile;
  22. { Also write the ppu if only crc if done, this can be used with ppudump to
  23. see the differences between the intf and implementation }
  24. { define INTFPPU}
  25. {$ifdef Test_Double_checksum}
  26. var
  27. CRCFile : text;
  28. const
  29. CRC_array_Size = 200000;
  30. type
  31. tcrc_array = array[0..crc_array_size] of dword;
  32. pcrc_array = ^tcrc_array;
  33. {$endif Test_Double_checksum}
  34. const
  35. { only update this version if something change in the tppuheader:
  36. * the unit flags listed below
  37. * the format of the header itslf
  38. This number cannot become bigger than 255 (it's stored in a byte) }
  39. CurrentPPUVersion = 207;
  40. { for any other changes to the ppu format, increase this version number
  41. (it's a cardinal) }
  42. CurrentPPULongVersion = 1;
  43. { unit flags }
  44. uf_big_endian = $000004;
  45. uf_in_library = $000020; { is the file in another file than <ppufile>.* ? }
  46. uf_smart_linked = $000040; { the ppu can be smartlinked }
  47. uf_static_linked = $000080; { the ppu can be linked static }
  48. uf_shared_linked = $000100; { the ppu can be linked shared }
  49. uf_no_link = $000400; { unit has no .o generated, but can still have external linking! }
  50. uf_little_endian = $001000;
  51. uf_fpu_emulation = $008000; { this unit was compiled with fpu emulation on }
  52. type
  53. { bestreal is defined based on the target architecture }
  54. ppureal=bestreal;
  55. tppuerror=(ppuentrytoobig,ppuentryerror);
  56. tppuheader=record
  57. common : tentryheader;
  58. checksum : cardinal; { checksum for this ppufile }
  59. interface_checksum : cardinal;
  60. deflistsize,
  61. symlistsize : longint;
  62. indirect_checksum: cardinal;
  63. end;
  64. tppuentry=tentry;
  65. tunitasmlisttype=(ualt_public,ualt_extern);
  66. { tppufile }
  67. tppufile=class(tentryfile)
  68. {$ifdef Test_Double_checksum}
  69. public
  70. crcindex,
  71. crc_index,
  72. crcindex2,
  73. crc_index2 : cardinal;
  74. crc_test,
  75. crc_test2 : pcrc_array;
  76. private
  77. {$endif def Test_Double_checksum}
  78. protected
  79. procedure newheader;override;
  80. function readheader: longint;override;
  81. function outputallowed: boolean;override;
  82. //procedure doputdata(const b;len:integer);override;
  83. function getheadersize:longint;override;
  84. function getheaderaddr:pentryheader;override;
  85. procedure resetfile;override;
  86. public
  87. header : tppuheader;
  88. { crc for the entire unit }
  89. crc,
  90. { crc for the interface definitions in this unit }
  91. interface_crc,
  92. { crc of all object/class definitions in the interface of this unit, xor'ed
  93. by the crc's of all object/class definitions in the interfaces of units
  94. used by this unit. Reason: see mantis #13840 }
  95. indirect_crc : cardinal;
  96. do_crc,
  97. do_interface_crc,
  98. do_indirect_crc : boolean;
  99. crc_only : boolean; { used to calculate interface_crc before implementation }
  100. constructor Create(const fn:string);
  101. destructor destroy;override;
  102. function CheckPPUId:boolean;
  103. {read}
  104. { nothing special currently }
  105. {write}
  106. function createfile:boolean;override;
  107. procedure writeheader;override;
  108. procedure putdata(const b;len:integer);override;
  109. end;
  110. implementation
  111. uses
  112. {$ifdef Test_Double_checksum}
  113. comphook,
  114. {$endif def Test_Double_checksum}
  115. fpccrc;
  116. function swapendian_ppureal(d:ppureal):ppureal;
  117. type ppureal_bytes=array[0..sizeof(d)-1] of byte;
  118. var i:0..sizeof(d)-1;
  119. begin
  120. for i:=low(ppureal_bytes) to high(ppureal_bytes) do
  121. ppureal_bytes(swapendian_ppureal)[i]:=ppureal_bytes(d)[high(ppureal_bytes)-i];
  122. end;
  123. {*****************************************************************************
  124. TPPUFile
  125. *****************************************************************************}
  126. constructor tppufile.Create(const fn:string);
  127. begin
  128. inherited Create(fn);
  129. crc_only:=false;
  130. {$ifdef Test_Double_checksum}
  131. if not assigned(crc_test) then
  132. new(crc_test);
  133. if not assigned(crc_test2) then
  134. new(crc_test2);
  135. {$endif Test_Double_checksum}
  136. end;
  137. destructor tppufile.destroy;
  138. begin
  139. {$ifdef Test_Double_checksum}
  140. if assigned(crc_test) then
  141. dispose(crc_test);
  142. crc_test:=nil;
  143. if assigned(crc_test2) then
  144. dispose(crc_test2);
  145. crc_test2:=nil;
  146. {$endif Test_Double_checksum}
  147. inherited destroy;
  148. end;
  149. function tppufile.CheckPPUId:boolean;
  150. begin
  151. CheckPPUId:=((Header.common.Id[1]='P') and
  152. (Header.common.Id[2]='P') and
  153. (Header.common.Id[3]='U'));
  154. end;
  155. procedure tppufile.newheader;
  156. var
  157. s : string;
  158. begin
  159. fillchar(header,sizeof(tppuheader),0);
  160. str(currentppuversion,s);
  161. while length(s)<3 do
  162. s:='0'+s;
  163. with header.common do
  164. begin
  165. Id[1]:='P';
  166. Id[2]:='P';
  167. Id[3]:='U';
  168. Ver[1]:=s[1];
  169. Ver[2]:=s[2];
  170. Ver[3]:=s[3];
  171. end;
  172. end;
  173. function tppufile.readheader: longint;
  174. begin
  175. if fsize<sizeof(tppuheader) then
  176. exit(0);
  177. result:=f.Read(header,sizeof(tppuheader));
  178. { The header is always stored in little endian order }
  179. { therefore swap if on a big endian machine }
  180. {$IFDEF ENDIAN_BIG}
  181. header.common.compiler := swapendian(header.common.compiler);
  182. header.common.cpu := swapendian(header.common.cpu);
  183. header.common.target := swapendian(header.common.target);
  184. header.common.flags := swapendian(header.common.flags);
  185. header.common.size := swapendian(header.common.size);
  186. header.checksum := swapendian(header.checksum);
  187. header.interface_checksum := swapendian(header.interface_checksum);
  188. header.indirect_checksum := swapendian(header.indirect_checksum);
  189. header.deflistsize:=swapendian(header.deflistsize);
  190. header.symlistsize:=swapendian(header.symlistsize);
  191. { the PPU DATA is stored in native order }
  192. change_endian := (header.common.flags and uf_little_endian) = uf_little_endian;
  193. {$ELSE not ENDIAN_BIG}
  194. change_endian := (header.common.flags and uf_big_endian) = uf_big_endian;
  195. {$ENDIF}
  196. end;
  197. function tppufile.outputallowed: boolean;
  198. begin
  199. result:=not crc_only;
  200. end;
  201. {*****************************************************************************
  202. TPPUFile Reading
  203. *****************************************************************************}
  204. { nothing special currently }
  205. {*****************************************************************************
  206. TPPUFile Writing
  207. *****************************************************************************}
  208. function tppufile.createfile:boolean;
  209. begin
  210. {$ifdef INTFPPU}
  211. if crc_only then
  212. begin
  213. fname:=fname+'.intf';
  214. crc_only:=false;
  215. end;
  216. {$endif}
  217. result:=inherited createfile;
  218. end;
  219. procedure tppufile.writeheader;
  220. var
  221. opos : integer;
  222. begin
  223. if crc_only then
  224. exit;
  225. { flush buffer }
  226. writebuf;
  227. { update size (w/o header!) in the header }
  228. header.common.size:=bufstart-sizeof(tppuheader);
  229. { set the endian flag }
  230. {$ifndef FPC_BIG_ENDIAN}
  231. header.common.flags := header.common.flags or uf_little_endian;
  232. {$else not FPC_BIG_ENDIAN}
  233. header.common.flags := header.common.flags or uf_big_endian;
  234. { Now swap the header.common in the correct endian (always little endian) }
  235. header.common.compiler := swapendian(header.common.compiler);
  236. header.common.cpu := swapendian(header.common.cpu);
  237. header.common.target := swapendian(header.common.target);
  238. header.common.flags := swapendian(header.common.flags);
  239. header.common.size := swapendian(header.common.size);
  240. header.checksum := swapendian(header.checksum);
  241. header.interface_checksum := swapendian(header.interface_checksum);
  242. header.indirect_checksum := swapendian(header.indirect_checksum);
  243. header.deflistsize:=swapendian(header.deflistsize);
  244. header.symlistsize:=swapendian(header.symlistsize);
  245. {$endif not FPC_BIG_ENDIAN}
  246. { write header and restore filepos after it }
  247. opos:=f.Position;
  248. f.Position:=0;
  249. f.Write(header,sizeof(tppuheader));
  250. f.Position:=opos;
  251. end;
  252. procedure tppufile.putdata(const b;len:integer);
  253. begin
  254. if do_crc then
  255. begin
  256. crc:=UpdateCrc32(crc,b,len);
  257. {$ifdef Test_Double_checksum}
  258. if crc_only then
  259. begin
  260. crc_test2^[crc_index2]:=crc;
  261. {$ifdef Test_Double_checksum_write}
  262. Writeln(CRCFile,crc);
  263. {$endif Test_Double_checksum_write}
  264. if crc_index2<crc_array_size then
  265. inc(crc_index2);
  266. end
  267. else
  268. begin
  269. if (crcindex2<crc_array_size) and (crcindex2<crc_index2) and
  270. (crc_test2^[crcindex2]<>crc) then
  271. Do_comment(V_Note,'impl CRC changed');
  272. {$ifdef Test_Double_checksum_write}
  273. Writeln(CRCFile,crc);
  274. {$endif Test_Double_checksum_write}
  275. inc(crcindex2);
  276. end;
  277. {$endif def Test_Double_checksum}
  278. if do_interface_crc then
  279. begin
  280. interface_crc:=UpdateCrc32(interface_crc,b,len);
  281. {$ifdef Test_Double_checksum}
  282. if crc_only then
  283. begin
  284. crc_test^[crc_index]:=interface_crc;
  285. {$ifdef Test_Double_checksum_write}
  286. Writeln(CRCFile,interface_crc);
  287. {$endif Test_Double_checksum_write}
  288. if crc_index<crc_array_size then
  289. inc(crc_index);
  290. end
  291. else
  292. begin
  293. if (crcindex<crc_array_size) and (crcindex<crc_index) and
  294. (crc_test^[crcindex]<>interface_crc) then
  295. Do_comment(V_Warning,'CRC changed');
  296. {$ifdef Test_Double_checksum_write}
  297. Writeln(CRCFile,interface_crc);
  298. {$endif Test_Double_checksum_write}
  299. inc(crcindex);
  300. end;
  301. {$endif def Test_Double_checksum}
  302. { indirect crc must only be calculated for the interface; changes
  303. to a class in the implementation cannot require another unit to
  304. be recompiled }
  305. if do_indirect_crc then
  306. indirect_crc:=UpdateCrc32(indirect_crc,b,len);
  307. end;
  308. end;
  309. inherited putdata(b,len);
  310. (*if not crc_only then
  311. writedata(b,len);
  312. inc(entryidx,len);*)
  313. end;
  314. function tppufile.getheadersize: longint;
  315. begin
  316. result:=sizeof(header);
  317. end;
  318. function tppufile.getheaderaddr: pentryheader;
  319. begin
  320. result:=@header;
  321. end;
  322. procedure tppufile.resetfile;
  323. begin
  324. crc:=0;
  325. interface_crc:=0;
  326. indirect_crc:=0;
  327. do_interface_crc:=true;
  328. do_indirect_crc:=false;
  329. do_crc:=true;
  330. end;
  331. end.