ppu.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. const
  27. CRC_array_Size = 200000;
  28. type
  29. tcrc_array = array[0..crc_array_size] of dword;
  30. pcrc_array = ^tcrc_array;
  31. {$endif Test_Double_checksum}
  32. const
  33. { only update this version if something change in the tppuheader:
  34. * the unit flags listed below
  35. * the format of the header itself
  36. This number cannot become bigger than 255 (it's stored in a byte) }
  37. CurrentPPUVersion = 208;
  38. { for any other changes to the ppu format, increase this version number
  39. (it's a cardinal) }
  40. CurrentPPULongVersion = 13;
  41. { unit flags }
  42. uf_big_endian = $000004;
  43. uf_in_library = $000020; { is the file in another file than <ppufile>.* ? }
  44. uf_smart_linked = $000040; { the ppu can be smartlinked }
  45. uf_static_linked = $000080; { the ppu can be linked static }
  46. uf_shared_linked = $000100; { the ppu can be linked shared }
  47. uf_lto_linked = $000200; { the ppu can be used with LTO }
  48. uf_no_link = $000400; { unit has no .o generated, but can still have external linking! }
  49. uf_little_endian = $001000;
  50. uf_fpu_emulation = $008000; { this unit was compiled with fpu emulation on }
  51. type
  52. { bestreal is defined based on the target architecture }
  53. ppureal=bestreal;
  54. { set type aliases. We cast all sets to a corresponding array type so
  55. that if the set changes size, compilation will fail and we know that
  56. we have have to increase the ppu version }
  57. tppuset1 = array[0..0] of byte;
  58. tppuset2 = array[0..1] of byte;
  59. { tppuset3 = array[0..2] of byte; (sets of 3 bytes are rounded up to 4 bytes) }
  60. tppuset4 = array[0..3] of byte;
  61. tppuset5 = array[0..4] of byte;
  62. tppuset6 = array[0..5] of byte;
  63. tppuset7 = array[0..6] of byte;
  64. tppuset8 = array[0..7] of byte;
  65. tppuset9 = array[0..8] of byte;
  66. tppuset10 = array[0..9] of byte;
  67. tppuset11 = array[0..10] of byte;
  68. tppuset12 = array[0..11] of byte;
  69. tppuset13 = array[0..12] of byte;
  70. tppuset14 = array[0..13] of byte;
  71. tppuset15 = array[0..14] of byte;
  72. tppuset16 = array[0..15] of byte;
  73. tppuset17 = array[0..16] of byte;
  74. tppuset18 = array[0..17] of byte;
  75. tppuset19 = array[0..18] of byte;
  76. tppuset20 = array[0..19] of byte;
  77. tppuset21 = array[0..20] of byte;
  78. tppuset22 = array[0..21] of byte;
  79. tppuset23 = array[0..22] of byte;
  80. tppuset24 = array[0..23] of byte;
  81. tppuset25 = array[0..24] of byte;
  82. tppuset26 = array[0..25] of byte;
  83. tppuset27 = array[0..26] of byte;
  84. tppuset28 = array[0..27] of byte;
  85. tppuset29 = array[0..28] of byte;
  86. tppuset30 = array[0..29] of byte;
  87. tppuset31 = array[0..30] of byte;
  88. tppuset32 = array[0..31] of byte;
  89. tppuerror=(ppuentrytoobig,ppuentryerror);
  90. tppuheader=record
  91. common : tentryheader;
  92. checksum : cardinal; { checksum for this ppufile }
  93. interface_checksum : cardinal;
  94. deflistsize,
  95. symlistsize : longint;
  96. indirect_checksum: cardinal;
  97. end;
  98. tppuentry=tentry;
  99. tunitasmlisttype=(ualt_public,ualt_extern);
  100. { tppufile }
  101. tppufile=class(tentryfile)
  102. {$ifdef Test_Double_checksum}
  103. public
  104. interface_read_crc_index,
  105. interface_write_crc_index,
  106. indirect_read_crc_index,
  107. indirect_write_crc_index,
  108. implementation_read_crc_index,
  109. implementation_write_crc_index : cardinal;
  110. interface_crc_array,
  111. indirect_crc_array,
  112. implementation_crc_array : pcrc_array;
  113. CRCFile : text;
  114. private
  115. {$endif def Test_Double_checksum}
  116. protected
  117. procedure newheader;override;
  118. function readheader: longint;override;
  119. function outputallowed: boolean;override;
  120. //procedure doputdata(const b;len:integer);override;
  121. function getheadersize:longint;override;
  122. function getheaderaddr:pentryheader;override;
  123. procedure resetfile;override;
  124. {$ifdef DEBUG_PPU}
  125. procedure ppu_log(st :string);override;
  126. {$endif}
  127. public
  128. header : tppuheader;
  129. { crc for the entire unit }
  130. crc,
  131. { crc for the interface definitions in this unit }
  132. interface_crc,
  133. { crc of all object/class definitions in the interface of this unit, xor'ed
  134. by the crc's of all object/class definitions in the interfaces of units
  135. used by this unit. Reason: see mantis #13840 }
  136. indirect_crc : cardinal;
  137. do_crc,
  138. do_interface_crc,
  139. do_indirect_crc : boolean;
  140. crc_only : boolean; { used to calculate interface_crc before implementation }
  141. constructor Create(const fn:string);
  142. destructor destroy;override;
  143. function CheckPPUId:boolean;
  144. {read}
  145. { nothing special currently }
  146. {write}
  147. function createfile:boolean;override;
  148. procedure writeheader;override;
  149. procedure putdata(const b;len:integer);override;
  150. end;
  151. implementation
  152. uses
  153. {$ifdef Test_Double_checksum}
  154. comphook,
  155. {$endif def Test_Double_checksum}
  156. fpccrc;
  157. {$ifdef Test_Double_checksum}
  158. {$ifdef TEST_CRC_ERROR}
  159. const
  160. CRC_Interface_Change_Message_Level=V_Error;
  161. CRC_Implementation_Change_Message_Level=V_Error;
  162. CRC_Indirect_Change_Message_Level=V_Error;
  163. {$else : not TEST_CRC_ERROR}
  164. const
  165. CRC_Interface_Change_Message_Level=V_Warning;
  166. CRC_Implementation_Change_Message_Level=V_Note;
  167. CRC_Indirect_Change_Message_Level=V_Note;
  168. {$endif : not TEST_CRC_ERROR}
  169. {$endif Test_Double_checksum}
  170. function swapendian_ppureal(d:ppureal):ppureal;
  171. type ppureal_bytes=array[0..sizeof(d)-1] of byte;
  172. var i:0..sizeof(d)-1;
  173. begin
  174. for i:=low(ppureal_bytes) to high(ppureal_bytes) do
  175. ppureal_bytes(swapendian_ppureal)[i]:=ppureal_bytes(d)[high(ppureal_bytes)-i];
  176. end;
  177. {*****************************************************************************
  178. TPPUFile
  179. *****************************************************************************}
  180. constructor tppufile.Create(const fn:string);
  181. begin
  182. inherited Create(fn);
  183. crc_only:=false;
  184. {$ifdef Test_Double_checksum}
  185. if not assigned(interface_crc_array) then
  186. begin
  187. new(interface_crc_array);
  188. fillchar(interface_crc_array^,sizeof(interface_crc_array),#$ff);
  189. end;
  190. if not assigned(indirect_crc_array) then
  191. begin
  192. new(indirect_crc_array);
  193. fillchar(indirect_crc_array^,sizeof(indirect_crc_array),#$ff);
  194. end;
  195. if not assigned(implementation_crc_array) then
  196. begin
  197. new(implementation_crc_array);
  198. fillchar(implementation_crc_array^,sizeof(implementation_crc_array),#$ff);
  199. end;
  200. {$endif Test_Double_checksum}
  201. end;
  202. destructor tppufile.destroy;
  203. begin
  204. {$ifdef Test_Double_checksum}
  205. if assigned(interface_crc_array) then
  206. dispose(interface_crc_array);
  207. interface_crc_array:=nil;
  208. if assigned(indirect_crc_array) then
  209. dispose(indirect_crc_array);
  210. indirect_crc_array:=nil;
  211. if assigned(implementation_crc_array) then
  212. dispose(implementation_crc_array);
  213. implementation_crc_array:=nil;
  214. {$endif Test_Double_checksum}
  215. inherited destroy;
  216. end;
  217. function tppufile.CheckPPUId:boolean;
  218. begin
  219. CheckPPUId:=((Header.common.Id[1]='P') and
  220. (Header.common.Id[2]='P') and
  221. (Header.common.Id[3]='U'));
  222. end;
  223. procedure tppufile.newheader;
  224. var
  225. s : string;
  226. begin
  227. fillchar(header,sizeof(tppuheader),0);
  228. str(currentppuversion,s);
  229. while length(s)<3 do
  230. s:='0'+s;
  231. with header.common do
  232. begin
  233. Id[1]:='P';
  234. Id[2]:='P';
  235. Id[3]:='U';
  236. Ver[1]:=s[1];
  237. Ver[2]:=s[2];
  238. Ver[3]:=s[3];
  239. end;
  240. end;
  241. function tppufile.readheader: longint;
  242. begin
  243. if fsize<sizeof(tppuheader) then
  244. exit(-1);
  245. result:=f.Read(header,sizeof(tppuheader));
  246. { The header is always stored in little endian order }
  247. { therefore swap if on a big endian machine }
  248. {$IFDEF ENDIAN_BIG}
  249. header.common.compiler := swapendian(header.common.compiler);
  250. header.common.cpu := swapendian(header.common.cpu);
  251. header.common.target := swapendian(header.common.target);
  252. header.common.flags := swapendian(header.common.flags);
  253. header.common.size := swapendian(header.common.size);
  254. header.checksum := swapendian(header.checksum);
  255. header.interface_checksum := swapendian(header.interface_checksum);
  256. header.indirect_checksum := swapendian(header.indirect_checksum);
  257. header.deflistsize:=swapendian(header.deflistsize);
  258. header.symlistsize:=swapendian(header.symlistsize);
  259. { the PPU DATA is stored in native order }
  260. change_endian := (header.common.flags and uf_little_endian) = uf_little_endian;
  261. {$ELSE not ENDIAN_BIG}
  262. change_endian := (header.common.flags and uf_big_endian) = uf_big_endian;
  263. {$ENDIF}
  264. end;
  265. function tppufile.outputallowed: boolean;
  266. begin
  267. result:=not crc_only;
  268. end;
  269. {$ifdef DEBUG_PPU}
  270. procedure tppufile.ppu_log(st :string);
  271. begin
  272. inherited ppu_log(st);
  273. if flog_open then
  274. begin
  275. if do_crc and (ppu_log_idx < bufstart+bufidx) then
  276. begin
  277. writeln(flog,'New crc : ',hexstr(dword(crc),8));
  278. writeln(flog,'New interface crc : ',hexstr(dword(interface_crc),8));
  279. writeln(flog,'New indirect crc : ',hexstr(dword(indirect_crc),8));
  280. ppu_log_idx:=bufstart+bufidx;
  281. end;
  282. end;
  283. {$ifdef IN_PPUDUMP}
  284. if update_crc then
  285. begin
  286. writeln('New crc : ',hexstr(dword(crc),8));
  287. writeln('New interface crc : ',hexstr(dword(interface_crc),8));
  288. writeln('New indirect crc : ',hexstr(dword(indirect_crc),8));
  289. end;
  290. {$endif}
  291. end;
  292. {$endif}
  293. {*****************************************************************************
  294. TPPUFile Reading
  295. *****************************************************************************}
  296. { nothing special currently }
  297. {*****************************************************************************
  298. TPPUFile Writing
  299. *****************************************************************************}
  300. function tppufile.createfile:boolean;
  301. begin
  302. {$ifdef INTFPPU}
  303. if crc_only then
  304. begin
  305. fname:=fname+'.intf';
  306. crc_only:=false;
  307. end;
  308. {$endif}
  309. result:=inherited createfile;
  310. end;
  311. procedure tppufile.writeheader;
  312. var
  313. opos : integer;
  314. begin
  315. if crc_only then
  316. exit;
  317. { flush buffer }
  318. writebuf;
  319. { update size (w/o header!) in the header }
  320. header.common.size:=bufstart-sizeof(tppuheader);
  321. { set the endian flag }
  322. {$ifndef FPC_BIG_ENDIAN}
  323. header.common.flags := header.common.flags or uf_little_endian;
  324. {$else not FPC_BIG_ENDIAN}
  325. header.common.flags := header.common.flags or uf_big_endian;
  326. { Now swap the header.common in the correct endian (always little endian) }
  327. header.common.compiler := swapendian(header.common.compiler);
  328. header.common.cpu := swapendian(header.common.cpu);
  329. header.common.target := swapendian(header.common.target);
  330. header.common.flags := swapendian(header.common.flags);
  331. header.common.size := swapendian(header.common.size);
  332. header.checksum := swapendian(header.checksum);
  333. header.interface_checksum := swapendian(header.interface_checksum);
  334. header.indirect_checksum := swapendian(header.indirect_checksum);
  335. header.deflistsize:=swapendian(header.deflistsize);
  336. header.symlistsize:=swapendian(header.symlistsize);
  337. {$endif not FPC_BIG_ENDIAN}
  338. { write header and restore filepos after it }
  339. opos:=f.Position;
  340. f.Position:=0;
  341. f.Write(header,sizeof(tppuheader));
  342. f.Position:=opos;
  343. end;
  344. procedure tppufile.putdata(const b;len:integer);
  345. {$ifdef Test_Double_checksum}
  346. var
  347. pb : pbyte;
  348. ind : integer;
  349. {$endif Test_Double_checksum}
  350. begin
  351. if do_crc then
  352. begin
  353. crc:=UpdateCrc32(crc,b,len);
  354. {$ifdef Test_Double_checksum}
  355. if crc_only then
  356. begin
  357. implementation_crc_array^[implementation_write_crc_index]:=crc;
  358. {$ifdef Test_Double_checksum_write}
  359. Write(CRCFile,'imp_crc ',implementation_write_crc_index:6,' $',hexstr(crc,8),' ',len);
  360. pb:=@b;
  361. for ind:=0 to len-1 do
  362. Write(CRCFile,' ',hexstr(pb[ind],2));
  363. Writeln(CRCFile);
  364. {$endif Test_Double_checksum_write}
  365. if implementation_write_crc_index<crc_array_size then
  366. inc(implementation_write_crc_index);
  367. end
  368. else
  369. begin
  370. if (implementation_read_crc_index<crc_array_size) and
  371. (implementation_crc_array^[implementation_read_crc_index]<>crc) then
  372. begin
  373. do_comment(CRC_implementation_Change_Message_Level,'implementation CRC changed at index '+tostr(implementation_read_crc_index));
  374. if CRC_implementation_Change_Message_Level=V_Error then
  375. do_internalerror(2020113001);
  376. {$ifdef Test_Double_checksum_write}
  377. Writeln(CRCFile,'!!!imp_crc ',implementation_read_crc_index:5,'$',hexstr(crc,8),'<>$',hexstr(implementation_crc_array^[implementation_read_crc_index],8));
  378. end
  379. else
  380. begin
  381. Writeln(CRCFile,'imp_crc ',implementation_read_crc_index:5,' OK');
  382. {$endif Test_Double_checksum_write}
  383. end;
  384. inc(implementation_read_crc_index);
  385. end;
  386. {$endif def Test_Double_checksum}
  387. if do_interface_crc then
  388. begin
  389. interface_crc:=UpdateCrc32(interface_crc,b,len);
  390. {$ifdef Test_Double_checksum}
  391. if crc_only then
  392. begin
  393. interface_crc_array^[interface_write_crc_index]:=interface_crc;
  394. {$ifdef Test_Double_checksum_write}
  395. Write(CRCFile,'int_crc ',interface_write_crc_index:5,' $',hexstr(interface_crc,8),' ',len);
  396. pb:=@b;
  397. for ind:=0 to len-1 do
  398. Write(CRCFile,' ',hexstr(pb[ind],2));
  399. Writeln(CRCFile);
  400. {$endif Test_Double_checksum_write}
  401. if interface_write_crc_index<crc_array_size then
  402. inc(interface_write_crc_index);
  403. end
  404. else
  405. begin
  406. if (interface_read_crc_index<crc_array_size) and
  407. (interface_crc_array^[interface_read_crc_index]<>interface_crc) then
  408. begin
  409. do_comment(CRC_Interface_Change_Message_Level,'interface CRC changed at index '+tostr(interface_read_crc_index));
  410. if CRC_interface_Change_Message_Level=V_Error then
  411. do_internalerror(2020113002);
  412. {$ifdef Test_Double_checksum_write}
  413. Writeln(CRCFile,'!!!int_crc ',interface_read_crc_index:5,'$',hexstr(interface_crc,8),'<>$',hexstr(interface_crc_array^[interface_read_crc_index],8));
  414. end
  415. else
  416. begin
  417. Writeln(CRCFile,'int_crc ',interface_read_crc_index:5,' OK');
  418. {$endif Test_Double_checksum_write}
  419. end;
  420. inc(interface_read_crc_index);
  421. end;
  422. {$endif def Test_Double_checksum}
  423. { indirect crc must only be calculated for the interface; changes
  424. to a class in the implementation cannot require another unit to
  425. be recompiled }
  426. if do_indirect_crc then
  427. begin
  428. indirect_crc:=UpdateCrc32(indirect_crc,b,len);
  429. {$ifdef Test_Double_checksum}
  430. if crc_only then
  431. begin
  432. indirect_crc_array^[indirect_write_crc_index]:=indirect_crc;
  433. {$ifdef Test_Double_checksum_write}
  434. Write(CRCFile,'ind_crc ',indirect_write_crc_index:5,' $',hexstr(indirect_crc,8),' ',len);
  435. pb:=@b;
  436. for ind:=0 to len-1 do
  437. Write(CRCFile,' ',hexstr(pb[ind],2));
  438. Writeln(CRCFile);
  439. {$endif Test_Double_checksum_write}
  440. if indirect_write_crc_index<crc_array_size then
  441. inc(indirect_write_crc_index);
  442. end
  443. else
  444. begin
  445. if (indirect_read_crc_index<crc_array_size) and
  446. (indirect_crc_array^[indirect_read_crc_index]<>indirect_crc) then
  447. begin
  448. do_comment(CRC_Indirect_Change_Message_Level,'Indirect CRC changed at index '+tostr(indirect_read_crc_index));
  449. if CRC_indirect_Change_Message_Level=V_Error then
  450. do_internalerror(2020113003);
  451. {$ifdef Test_Double_checksum_write}
  452. Writeln(CRCFile,'!!!ind_crc ',indirect_read_crc_index:5,'$',hexstr(indirect_crc,8),'<>$',hexstr(indirect_crc_array^[indirect_read_crc_index],8));
  453. end
  454. else
  455. begin
  456. Writeln(CRCFile,'ind_crc ',indirect_read_crc_index:5,' OK');
  457. {$endif Test_Double_checksum_write}
  458. end;
  459. inc(indirect_read_crc_index);
  460. end;
  461. {$endif def Test_Double_checksum}
  462. end;
  463. end;
  464. end;
  465. inherited putdata(b,len);
  466. (*if not crc_only then
  467. writedata(b,len);
  468. inc(entryidx,len);*)
  469. end;
  470. function tppufile.getheadersize: longint;
  471. begin
  472. result:=sizeof(header);
  473. end;
  474. function tppufile.getheaderaddr: pentryheader;
  475. begin
  476. result:=@header;
  477. end;
  478. procedure tppufile.resetfile;
  479. begin
  480. {$ifdef Test_Double_checksum_write}
  481. if (crc<>0) or (interface_crc<>0) or (indirect_crc<>0) then
  482. Writeln(CRCFile,'!!! tppufile.reset called',
  483. ' implementation_crc=$',hexstr(crc,8),
  484. ' interface_crc=$',hexstr(interface_crc,8),
  485. ' indirect_crc=$',hexstr(indirect_crc,8),
  486. ' implementation_crc_size=',implementation_write_crc_index,
  487. ' interface_crc_size=',interface_write_crc_index,
  488. ' indirect_crc_size=',indirect_write_crc_index);
  489. {$endif Test_Double_checksum_write}
  490. crc:=0;
  491. interface_crc:=0;
  492. indirect_crc:=0;
  493. do_interface_crc:=true;
  494. do_indirect_crc:=false;
  495. do_crc:=true;
  496. end;
  497. end.